





"""simple docstring"""






import warnings

from .state import AcceleratorState, GradientState


warnings.filterwarnings('''ignore''', category=UserWarning, module='''torch.optim.lr_scheduler''')



class 	lowerCAmelCase					:






    def __init__(   self    ,  a__    ,  a__    ,  a__ = True    ,  a__ = False     ):
           _UpperCAmelCase     =     scheduler
           _UpperCAmelCase     =     optimizers if isinstance(a__    ,  (list, tuple)     ) else [optimizers]
           _UpperCAmelCase     =     split_batches
           _UpperCAmelCase     =     step_with_optimizer
           _UpperCAmelCase     =     GradientState()






    def 							__A (   self    ,  *a__    ,  **a__     ):
           if not self.step_with_optimizer:
                  # No link between scheduler and optimizer -> just step
                  self.scheduler.step(*a__    ,  **a__     )
                  return

           # Otherwise, first make sure the optimizer was stepped.
           if not self.gradient_state.sync_gradients:
                  if self.gradient_state.adjust_scheduler:
                         self.scheduler._step_count += 1
                  return

           for opt in self.optimizers:
                  if opt.step_was_skipped:
                         return
           if self.split_batches:
                  # Split batches -> the training dataloader batch size is not changed so one step per training step
                  self.scheduler.step(*a__    ,  **a__     )
           else:
                  # Otherwise the training dataloader batch size was multiplied by `num_processes`, so we need to do
                  # num_processes steps per training step
                  _UpperCAmelCase     =     AcceleratorState().num_processes
                  for _ in range(a__     ):
                         # Special case when using OneCycle and `drop_last` was not used
                         if hasattr(self.scheduler    ,  'total_steps'     ):
                                if self.scheduler._step_count <= self.scheduler.total_steps:
                                       self.scheduler.step(*a__    ,  **a__     )
                         else:
                                self.scheduler.step(*a__    ,  **a__     )






    def 							__A (   self     ):
           return self.scheduler.get_last_lr()






    def 							__A (   self     ):
           return self.scheduler.state_dict()






    def 							__A (   self    ,  a__     ):
           self.scheduler.load_state_dict(a__     )






    def 							__A (   self     ):
           return self.scheduler.get_lr()






    def 							__A (   self    ,  *a__    ,  **a__     ):
           return self.scheduler.print_lr(*a__    ,  **a__     )













"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase_			=							logging.get_logger(__name__)

lowerCAmelCase_			=							{
    '''microsoft/trocr-base-handwritten''': (
        '''https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json'''
    ),
    # See all TrOCR models at https://huggingface.co/models?filter=trocr
}



class 	lowerCAmelCase					(      snake_case							):
       lowerCAmelCase__						 =  """trocr"""
       lowerCAmelCase__						 =  ["""past_key_values"""]
       lowerCAmelCase__						 =  {
           """num_attention_heads""": """decoder_attention_heads""",
           """hidden_size""": """d_model""",
           """num_hidden_layers""": """decoder_layers""",
       }






       def __init__(   self    ,  a__=5_02_65    ,  a__=10_24    ,  a__=12    ,  a__=16    ,  a__=40_96    ,  a__="gelu"    ,  a__=5_12    ,  a__=0.1    ,  a__=0.0    ,  a__=0.0    ,  a__=2    ,  a__=0.02    ,  a__=0.0    ,  a__=True    ,  a__=False    ,  a__=True    ,  a__=True    ,  a__=1    ,  a__=0    ,  a__=2    ,  **a__    ,  ):
              _UpperCAmelCase     =     vocab_size
              _UpperCAmelCase     =     d_model
              _UpperCAmelCase     =     decoder_layers
              _UpperCAmelCase     =     decoder_attention_heads
              _UpperCAmelCase     =     decoder_ffn_dim
              _UpperCAmelCase     =     activation_function
              _UpperCAmelCase     =     max_position_embeddings
              _UpperCAmelCase     =     dropout
              _UpperCAmelCase     =     attention_dropout
              _UpperCAmelCase     =     activation_dropout
              _UpperCAmelCase     =     init_std
              _UpperCAmelCase     =     decoder_layerdrop
              _UpperCAmelCase     =     use_cache
              _UpperCAmelCase     =     scale_embedding
              _UpperCAmelCase     =     use_learned_position_embeddings
              _UpperCAmelCase     =     layernorm_embedding

              super().__init__(
                  pad_token_id=a__    ,  bos_token_id=a__    ,  eos_token_id=a__    ,  decoder_start_token_id=a__    ,  **a__    ,  )






