
import torch

from accelerate import Accelerator, DistributedType
class 			lowercase__:



	"""simple docstring"""




	def __enter__(	self  :      str	)	->					List[Any]:
						if self.enabled:
											lowercase_					  = self.model.no_sync()
											self.model_sync_obj.__enter__()

						return self
	def __exit__(	self  :      Optional[int]	,							SCREAMING_SNAKE_CASE_  :      int	,							SCREAMING_SNAKE_CASE_  :      int	,							SCREAMING_SNAKE_CASE_  :      Union[str, Any]	)	->					int:
						if self.enabled:
											# Average all models on exit
											self._sync_and_avg_model_params()
											self.model_sync_obj.__exit__(__SCREAMING_SNAKE_CASE	,							__SCREAMING_SNAKE_CASE	,							__SCREAMING_SNAKE_CASE	)
	def __init__(	self  :      Optional[int]	,							SCREAMING_SNAKE_CASE_  :      Optional[Any]	,							SCREAMING_SNAKE_CASE_  :      Optional[int]	,							SCREAMING_SNAKE_CASE_  :      Optional[Any]	,							SCREAMING_SNAKE_CASE_  :      Any = True	)	->					int:
						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)'''	)
						lowercase_					  = enabled and accelerator.distributed_type != DistributedType.NO
						lowercase_					  = 0
						if self.enabled:
											lowercase_					  = accelerator
											lowercase_					  = model
											lowercase_					  = local_sgd_steps
	def 						_lowercase  (	self  :      Tuple	)	->					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 						_lowercase  (	self  :      Optional[int]	)	->					Any:
						self.accelerator.wait_for_everyone()
						with self.accelerator.autocast():
											for param in self.model.parameters():
																lowercase_					  = self.accelerator.reduce(param.data	,							reduction='''mean'''	)






from typing import TYPE_CHECKING

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


__a        = {
    'configuration_donut_swin': ['DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DonutSwinConfig'],
    'processing_donut': ['DonutProcessor'],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__a        = [
						    'DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'DonutSwinModel',
						    'DonutSwinPreTrainedModel',
						]

try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__a        = ['DonutFeatureExtractor']
						__a        = ['DonutImageProcessor']


if TYPE_CHECKING:
						from .configuration_donut_swin import DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, DonutSwinConfig
						from .processing_donut import DonutProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_donut_swin import (
												    DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
												    DonutSwinModel,
												    DonutSwinPreTrainedModel,
												)

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .feature_extraction_donut import DonutFeatureExtractor
												from .image_processing_donut import DonutImageProcessor

else:
						import sys

						__a        = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




