





'''simple docstring'''


import torch

from accelerate import Accelerator, DistributedType


class 		UpperCamelCase__     :
      '''simple docstring'''






      def __enter__(     self )				->  Tuple:
        if self.enabled:
          __lowerCAmelCase		:    Any			     =  self.model.no_sync()
          self.model_sync_obj.__enter__()

        return self




      def __exit__(     self     , SCREAMING_SNAKE_CASE     , SCREAMING_SNAKE_CASE     , SCREAMING_SNAKE_CASE )				->  List[str]:
        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     , SCREAMING_SNAKE_CASE     , SCREAMING_SNAKE_CASE     , SCREAMING_SNAKE_CASE     , SCREAMING_SNAKE_CASE = True )				->  List[Any]:
        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		:    Union[str, Any]			     =  enabled and accelerator.distributed_type != DistributedType.NO
        __lowerCAmelCase		:    Optional[int]			     =  0
        if self.enabled:
          __lowerCAmelCase		:    int			     =  accelerator
          __lowerCAmelCase		:    Union[str, Any]			     =  model
          __lowerCAmelCase		:    List[str]			     =  local_sgd_steps




      def 	snake_case  (     self )				->  Dict:
        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 )				->  Tuple:
        self.accelerator.wait_for_everyone()
        with self.accelerator.autocast():
          for param in self.model.parameters():
            __lowerCAmelCase		:    Optional[int]			     =  self.accelerator.reduce(param.data     , reduction='mean' )








"""simple docstring"""





def     _A    (    UpperCamelCase_	:			str)      ->		int:
   '''simple docstring'''


   __lowercase								=		0
   for plain_chr in plain_text:
      __lowercase								=		(
          ord(UpperCamelCase_) + (hash_value << 6) + (hash_value << 16) - hash_value
      )
   return hash_value




