




'''simple docstring'''





import torch

from accelerate import Accelerator, DistributedType




class 				_snake_case	:




			def __enter__(      self							)       ->       Union[str, Any]:

										'''simple docstring'''




										if self.enabled:
																	snake_case_			      =   self.model.no_sync()
																	self.model_sync_obj.__enter__()

										return self




			def __exit__(      self       ,							a__       ,							a__       ,							a__							)       ->       str:

										'''simple docstring'''




										if self.enabled:
																	# Average all models on exit
																	self._sync_and_avg_model_params()
																	self.model_sync_obj.__exit__(a__       ,							a__       ,							a__							)




			def __init__(      self       ,							a__       ,							a__       ,							a__       ,							a__ = True							)       ->       List[str]:

										'''simple docstring'''




										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)"							)
										snake_case_			      =   enabled and accelerator.distributed_type != DistributedType.NO
										snake_case_			      =   0
										if self.enabled:
																	snake_case_			      =   accelerator
																	snake_case_			      =   model
																	snake_case_			      =   local_sgd_steps




			def      lowerCAmelCase__       (      self							)       ->       Union[str, Any]:

										'''simple docstring'''




										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      lowerCAmelCase__       (      self							)       ->       Tuple:

										'''simple docstring'''




										self.accelerator.wait_for_everyone()
										with self.accelerator.autocast():
																	for param in self.model.parameters():
																								snake_case_			      =   self.accelerator.reduce(param.data       ,							reduction="mean"							)










'''simple docstring'''





from typing import TYPE_CHECKING

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


_SCREAMING_SNAKE_CASE							:     Dict   	=				{
    "configuration_mobilevitv2": [
        "MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "MobileViTV2Config",
        "MobileViTV2OnnxConfig",
    ],
}


try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_SCREAMING_SNAKE_CASE							:     Optional[Any]   	=				[
		    "MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST",
		    "MobileViTV2ForImageClassification",
		    "MobileViTV2ForSemanticSegmentation",
		    "MobileViTV2Model",
		    "MobileViTV2PreTrainedModel",
		]

if TYPE_CHECKING:
		from .configuration_mobilevitva import (
		    MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    MobileViTVaConfig,
		    MobileViTVaOnnxConfig,
		)

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mobilevitva import (
				    MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
				    MobileViTVaForImageClassification,
				    MobileViTVaForSemanticSegmentation,
				    MobileViTVaModel,
				    MobileViTVaPreTrainedModel,
				)

else:
		import sys

		_SCREAMING_SNAKE_CASE							:     Any   	=				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




