


'''simple docstring'''
import torch

from accelerate import Accelerator, DistributedType






class 					lowerCAmelCase_     :






       def __enter__(   self      )->			int:
            if self.enabled:
                 _A				=       self.model.no_sync()
                 self.model_sync_obj.__enter__()

            return self






       def __exit__(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )->			int:
            if self.enabled:
                 # Average all models on exit
                 self._sync_and_avg_model_params()
                 self.model_sync_obj.__exit__(_UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )






       def __init__(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase = True      )->			Optional[Any]:
            if accelerator.distributed_type not in [
                DistributedType.NO,
                DistributedType.MULTI_CPU,
                DistributedType.MULTI_GPU,
            ]:
                 raise NotImplementedError('LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)'      )
            _A				=       enabled and accelerator.distributed_type != DistributedType.NO
            _A				=       0
            if self.enabled:
                 _A				=       accelerator
                 _A				=       model
                 _A				=       local_sgd_steps






       def      UpperCamelCase	(   self      )->			Union[str, Any]:
            self.num_steps += 1
            if not self.enabled:
                 return

            if self.num_steps % self.local_sgd_steps == 0:
                 self._sync_and_avg_model_params()






       def      UpperCamelCase	(   self      )->			int:
            self.accelerator.wait_for_everyone()
            with self.accelerator.autocast():
                 for param in self.model.parameters():
                      _A				=       self.accelerator.reduce(param.data ,    reduction='mean'      )









'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


lowerCAmelCase  =				{
    """configuration_segformer""": ["""SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SegformerConfig""", """SegformerOnnxConfig"""]
}

try:
    if not is_vision_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCAmelCase  =				["""SegformerFeatureExtractor"""]
    lowerCAmelCase  =				["""SegformerImageProcessor"""]

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCAmelCase  =				[
        """SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """SegformerDecodeHead""",
        """SegformerForImageClassification""",
        """SegformerForSemanticSegmentation""",
        """SegformerLayer""",
        """SegformerModel""",
        """SegformerPreTrainedModel""",
    ]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCAmelCase  =				[
        """TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """TFSegformerDecodeHead""",
        """TFSegformerForImageClassification""",
        """TFSegformerForSemanticSegmentation""",
        """TFSegformerModel""",
        """TFSegformerPreTrainedModel""",
    ]


if TYPE_CHECKING:
    from .configuration_segformer import SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SegformerConfig, SegformerOnnxConfig

    try:
        if not is_vision_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .feature_extraction_segformer import SegformerFeatureExtractor
        from .image_processing_segformer import SegformerImageProcessor

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_segformer import (
            SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
            SegformerDecodeHead,
            SegformerForImageClassification,
            SegformerForSemanticSegmentation,
            SegformerLayer,
            SegformerModel,
            SegformerPreTrainedModel,
        )
    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_segformer import (
            TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
            TFSegformerDecodeHead,
            TFSegformerForImageClassification,
            TFSegformerForSemanticSegmentation,
            TFSegformerModel,
            TFSegformerPreTrainedModel,
        )

else:
    import sys

    lowerCAmelCase  =				_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





