
'''simple docstring'''







import torch

from accelerate import Accelerator, DistributedType






class 				lowerCAmelCase__     :






					def __enter__(     self	):


											"""simple docstring"""





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

											return self






					def __exit__(     self       ,							__SCREAMING_SNAKE_CASE       ,							__SCREAMING_SNAKE_CASE       ,							__SCREAMING_SNAKE_CASE	):


											"""simple docstring"""





											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       ,							__SCREAMING_SNAKE_CASE       ,							__SCREAMING_SNAKE_CASE       ,							__SCREAMING_SNAKE_CASE       ,							__SCREAMING_SNAKE_CASE = True	):


											"""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)'''	)
											lowercase_			:  Any									= enabled and accelerator.distributed_type != DistributedType.NO
											lowercase_			:  Optional[int]									= 0
											if self.enabled:
																	lowercase_			:  int									= accelerator
																	lowercase_			:  Dict									= model
																	lowercase_			:  Optional[Any]									= local_sgd_steps






					def 			_snake_case					(     self	):


											"""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 			_snake_case					(     self	):


											"""simple docstring"""





											self.accelerator.wait_for_everyone()
											with self.accelerator.autocast():
																	for param in self.model.parameters():
																							lowercase_			:  Union[str, Any]									= self.accelerator.reduce(param.data       ,							reduction='''mean'''	)








'''simple docstring'''







def       snake_case_	(       __SCREAMING_SNAKE_CASE      :				int = 1000							):



						"""simple docstring"""





						lowercase_			:  List[Any]									= 0
						for i in range(__SCREAMING_SNAKE_CASE							):
												if i % 3 == 0 or i % 5 == 0:
																		result += i
						return result


if __name__ == "__main__":
							print(f"""{solution() = }""")



