code: 
  
  
  '''simple docstring'''
  
  
  
  import torch
  
  from accelerate import Accelerator, DistributedType
  
  
  
  
  
  class      UpperCAmelCase_						:
  
  
  
  
  
      """simple docstring"""
  
  
  
  
  
  
      def __enter__(							self			)       ->  List[Any]:
           if self.enabled:
                __lowerCamelCase     :			Optional[Any]              =	self.model.no_sync()
                self.model_sync_obj.__enter__()
  
           return self
  
  
  
  
  
  
  
      def __exit__(							self    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_			)       ->  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    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_ = 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)'			)
           __lowerCamelCase     :			Dict              =	enabled and accelerator.distributed_type != DistributedType.NO
           __lowerCamelCase     :			int              =	0
           if self.enabled:
                __lowerCamelCase     :			Optional[Any]              =	accelerator
                __lowerCamelCase     :			Tuple              =	model
                __lowerCamelCase     :			Tuple              =	local_sgd_steps
  
  
  
  
  
  
  
      def    lowercase_       (							self			)       ->  List[str]:
           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			)       ->  List[Any]:
           self.accelerator.wait_for_everyone()
           with self.accelerator.autocast():
                for param in self.model.parameters():
                     __lowerCamelCase     :			List[Any]              =	self.accelerator.reduce(param.data    ,  reduction='mean'			)
  
  
  
  
  
  
  
style_context: 
  
  
  '''simple docstring'''
  
  
  
  def     UpperCAmelCase__      (			UpperCAmelCase_					:       str							)  ->	int:
       __lowerCamelCase     :			Tuple              =	str(UpperCAmelCase_							).strip()
       if not bin_string:
            raise ValueError('Empty string was passed to the function'							)
       __lowerCamelCase     :			Optional[int]              =	bin_string[0] == '-'
       if is_negative:
            __lowerCamelCase     :			List[Any]              =	bin_string[1:]
       if not all(char in '01' for char in bin_string							):
            raise ValueError('Non-binary value was passed to the function'							)
       __lowerCamelCase     :			Optional[int]              =	0
       for char in bin_string:
            __lowerCamelCase     :			str              =	2 * decimal_number + int(UpperCAmelCase_							)
       return -decimal_number if is_negative else decimal_number
  
  
  if __name__ == "__main__":
        from doctest import testmod
  
        testmod()
  
  
  
  
  
  
  