code: 
  
  
  '''simple docstring'''
  
  from __future__ import annotations
  
  def _SCREAMING_SNAKE_CASE   (A						,				A						,				A ) -> int:
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         assert n > 1 and a > 0 and greatest_common_divisor(A						,				A ) == 1
         ((lowercase__)    ,(lowercase__)    ,(lowercase__))       =				extended_gcd(A						,				A )  # Implemented below
         lowercase__       =				(b * s) % n
         return x
  
  def _SCREAMING_SNAKE_CASE   (A						,				A ) -> int:
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         ((lowercase__)    ,(lowercase__))       =				extended_euclid(A						,				A )  # Implemented below
         if b < 0:
                lowercase__       =				(b % n + n) % n
         return b
  
  def _SCREAMING_SNAKE_CASE   (A						,				A						,				A ) -> int:
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         lowercase__       =				invert_modulo(A						,				A )
         lowercase__       =				(b * s) % n
         return x
  
  def _SCREAMING_SNAKE_CASE   (A						,				A ) -> tuple[int, int, int]:
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         assert a >= 0 and b >= 0
  
         if b == 0:
                lowercase__    ,lowercase__    ,lowercase__       =				a, 1, 0
         else:
                ((lowercase__)    ,(lowercase__)    ,(lowercase__))       =				extended_gcd(A						,				a % b )
                lowercase__       =				q
                lowercase__       =				p - q * (a // b)
  
         assert a % d == 0 and b % d == 0
         assert d == a * x + b * y
  
         return (d, x, y)
  
  def _SCREAMING_SNAKE_CASE   (A						,				A ) -> tuple[int, int]:
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         if b == 0:
                return (1, 0)
         ((lowercase__)    ,(lowercase__))       =				extended_euclid(A						,				a % b )
         lowercase__       =				a // b
         return (y, x - k * y)
  
  def _SCREAMING_SNAKE_CASE   (A						,				A ) -> int:
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         if a < b:
                lowercase__    ,lowercase__       =				b, a
  
         while a % b != 0:
                lowercase__    ,lowercase__       =				b, a % b
  
         return b
  
  
  if __name__ == "__main__":
   from doctest import testmod
  
   testmod(name='modular_division', verbose=True)
   testmod(name='modular_division2', verbose=True)
   testmod(name='invert_modulo', verbose=True)
   testmod(name='extended_gcd', verbose=True)
   testmod(name='extended_euclid', verbose=True)
   testmod(name='greatest_common_divisor', verbose=True)
  
  
  
  
  
style_context: 
  
  
  '''simple docstring'''
  
  import torch
  
  from accelerate import Accelerator, DistributedType
  class 						__lowerCAmelCase		:
  
  
  
  
  
        '''simple docstring'''
  
  
  
  
  
  
        def __enter__(self					:					List[Any]				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               if self.enabled:
                      lowercase__       =				self.model.no_sync()
                      self.model_sync_obj.__enter__()
  
               return self
  
  
  
        def __exit__(self					:					List[str]     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					Optional[int]     ,		UpperCamelCase					:					Optional[int]				):
  
               '''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					:					Dict     ,		UpperCamelCase					:					Accelerator     ,		UpperCamelCase					:					torch.nn.Module     ,		UpperCamelCase					:					int     ,		UpperCamelCase					:					bool = 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__       =				enabled and accelerator.distributed_type != DistributedType.NO
               lowercase__       =				0
               if self.enabled:
                      lowercase__       =				accelerator
                      lowercase__       =				model
                      lowercase__       =				local_sgd_steps
  
  
  
        def    UpperCamelCase__	(self					:					List[str]				):
  
               '''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    UpperCamelCase__	(self					:					Any				):
  
               '''simple docstring'''
  
  
  
  
  
  
  
               self.accelerator.wait_for_everyone()
               with self.accelerator.autocast():
                      for param in self.model.parameters():
                             lowercase__       =				self.accelerator.reduce(param.data     ,		reduction='''mean'''				)
  
  
  
  
  