






"""simple docstring"""





import torch

from accelerate import Accelerator, DistributedType







class 		_a       :





   '''simple docstring'''






   def __enter__(   self)	-> int:




          '''simple docstring'''




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

          return self





   def __exit__(   self			,				UpperCAmelCase_			,				UpperCAmelCase_			,				UpperCAmelCase_)	-> List[Any]:




          '''simple docstring'''




          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]:




          '''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__:    Tuple        =      0
          if self.enabled:
                 lowercase__:    List[Any]        =      accelerator
                 lowercase__:    int        =      model
                 lowercase__:    Optional[Any]        =      local_sgd_steps





   def __lowercase    (   self)	-> Optional[int]:




          '''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 __lowercase    (   self)	-> Any:




          '''simple docstring'''




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














"""simple docstring"""





def     A(  snake_case_		):
       """simple docstring"""





       if not isinstance(snake_case_						,       snake_case_		):
              raise ValueError("Input series is not valid, valid series - [1, 2/3, 2]"		)
       if len(snake_case_		) == 0:
              raise ValueError("Input list must be a non empty list"		)
       if len(snake_case_		) == 1 and series[0] != 0:
              return True
       lowercase__:    str        =      []
       lowercase__:    Optional[int]        =      len(snake_case_		)
       for i in range(0						,       snake_case_		):
              if series[i] == 0:
                     raise ValueError("Input series cannot have 0 as an element"		)
              rec_series.append(1 / series[i]		)
       lowercase__:    Optional[int]        =      rec_series[1] - rec_series[0]
       for index in range(2						,       snake_case_		):
              if rec_series[index] - rec_series[index - 1] != common_diff:
                     return False
       return True






def     A(  snake_case_		):
       """simple docstring"""





       if not isinstance(snake_case_						,       snake_case_		):
              raise ValueError("Input series is not valid, valid series - [2, 4, 6]"		)
       if len(snake_case_		) == 0:
              raise ValueError("Input list must be a non empty list"		)
       lowercase__:    Optional[int]        =      0
       for val in series:
              answer += 1 / val
       return len(snake_case_		) / answer


if __name__ == "__main__":
  import doctest

  doctest.testmod()






