


from __future__ import annotations


def   a						(				SCREAMING_SNAKE_CASE_   :		Dict				,      SCREAMING_SNAKE_CASE_   :		Tuple				,      SCREAMING_SNAKE_CASE_   :		Optional[int]							):






       """simple docstring"""





       assert (
           c % greatest_common_divisor(__A				,      __A							) == 0
       )  # greatest_common_divisor(a,b) function implemented below
       (UpperCamelCase)     :						List[str] 						=		extended_gcd(__A				,      __A							)  # extended_gcd(a,b) function implemented below
       UpperCamelCase     :						Optional[Any] 						=		c / d
       return (r * x, r * y)


def   a						(				SCREAMING_SNAKE_CASE_   :		List[Any]				,      SCREAMING_SNAKE_CASE_   :		Union[str, Any]				,      SCREAMING_SNAKE_CASE_   :		Union[str, Any]				,      SCREAMING_SNAKE_CASE_   :		Dict = 2							):






       """simple docstring"""





       (UpperCamelCase)     :						List[str] 						=		diophantine(__A				,      __A				,      __A							)  # Initial value
       UpperCamelCase     :						List[str] 						=		greatest_common_divisor(__A				,      __A							)
       UpperCamelCase     :						Any 						=		a // d
       UpperCamelCase     :						Any 						=		b // d

       for i in range(__A							):
              UpperCamelCase     :						Optional[Any] 						=		xa + i * q
              UpperCamelCase     :						int 						=		ya - i * p
              print(__A				,      __A							)


def   a						(				SCREAMING_SNAKE_CASE_   :		List[Any]				,      SCREAMING_SNAKE_CASE_   :		int							):






       """simple docstring"""





       if a < b:
              UpperCamelCase     :						str 						=		b, a

       while a % b != 0:
              UpperCamelCase     :						int 						=		b, a % b

       return b


def   a						(				SCREAMING_SNAKE_CASE_   :		Dict				,      SCREAMING_SNAKE_CASE_   :		List[Any]							):






       """simple docstring"""





       assert a >= 0 and b >= 0

       if b == 0:
              UpperCamelCase     :						Optional[int] 						=		a, 1, 0
       else:
              (UpperCamelCase)     :						Union[str, Any] 						=		extended_gcd(__A				,      a % b							)
              UpperCamelCase     :						int 						=		q
              UpperCamelCase     :						Any 						=		p - q * (a // b)

       assert a % d == 0 and b % d == 0
       assert d == a * x + b * y

       return (d, x, y)


if __name__ == "__main__":
  from doctest import testmod

  testmod(name="diophantine", verbose=True)
  testmod(name="diophantine_all_soln", verbose=True)
  testmod(name="extended_gcd", verbose=True)
  testmod(name="greatest_common_divisor", verbose=True)


import torch

from accelerate import Accelerator, DistributedType




class 						__snake_case						:





      '''simple docstring'''






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


          """simple docstring"""


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

          return self



      def __exit__(       self  :					List[str]     ,	_UpperCamelCase  :					Any     ,	_UpperCamelCase  :					Union[str, Any]     ,	_UpperCamelCase  :					int)					->Optional[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  :					Union[str, Any]     ,	_UpperCamelCase  :					Accelerator     ,	_UpperCamelCase  :					torch.nn.Module     ,	_UpperCamelCase  :					int     ,	_UpperCamelCase  :					bool = True)					->int:


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



      def 						_SCREAMING_SNAKE_CASE     (       self  :					Union[str, Any])					->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 						_SCREAMING_SNAKE_CASE     (       self  :					Optional[Any])					->List[str]:


          """simple docstring"""


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




