

'''simple docstring'''
from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


_UpperCAmelCase  :		str  =		logging.get_logger(__name__)

_UpperCAmelCase  :		Any  =		{
    '''Helsinki-NLP/opus-mt-en-de''': '''https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json''',
    # See all Marian models at https://huggingface.co/models?filter=marian
}

class 						__magic_name__   (							__SCREAMING_SNAKE_CASE ):
 UpperCamelCase__     =       'marian'
 UpperCamelCase__     =       ['past_key_values']
 UpperCamelCase__     =       {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}







 def __init__(       self  , snake_case_=5_81_01  , snake_case_=None  , snake_case_=10_24  , snake_case_=12  , snake_case_=40_96  , snake_case_=16  , snake_case_=12  , snake_case_=40_96  , snake_case_=16  , snake_case_=0.0  , snake_case_=0.0  , snake_case_=True  , snake_case_=True  , snake_case_="gelu"  , snake_case_=10_24  , snake_case_=0.1  , snake_case_=0.0  , snake_case_=0.0  , snake_case_=0.02  , snake_case_=5_81_00  , snake_case_=False  , snake_case_=5_81_00  , snake_case_=0  , snake_case_=0  , snake_case_=True  , **snake_case_  , ):
   lowercase									=vocab_size
   lowercase									=decoder_vocab_size or vocab_size
   lowercase									=max_position_embeddings
   lowercase									=d_model
   lowercase									=encoder_ffn_dim
   lowercase									=encoder_layers
   lowercase									=encoder_attention_heads
   lowercase									=decoder_ffn_dim
   lowercase									=decoder_layers
   lowercase									=decoder_attention_heads
   lowercase									=dropout
   lowercase									=attention_dropout
   lowercase									=activation_dropout
   lowercase									=activation_function
   lowercase									=init_std
   lowercase									=encoder_layerdrop
   lowercase									=decoder_layerdrop
   lowercase									=use_cache
   lowercase									=encoder_layers
   lowercase									=scale_embedding  # scale factor will be sqrt(d_model) if True
   lowercase									=share_encoder_decoder_embeddings
   super().__init__(
       pad_token_id=snake_case_  , eos_token_id=snake_case_  , is_encoder_decoder=snake_case_  , decoder_start_token_id=snake_case_  , forced_eos_token_id=snake_case_  , **snake_case_  , )

class 						__magic_name__   (							__SCREAMING_SNAKE_CASE ):







 @property
 # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
 def 						_A(       self   ):
   if self.task in ["default", "seq2seq-lm"]:
     lowercase									=OrderedDict(
         [
             ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
             ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
         ]   )

     if self.use_past:
       lowercase									={0: '''batch'''}
       lowercase									={0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
     else:
       lowercase									={0: '''batch''', 1: '''decoder_sequence'''}
       lowercase									={0: '''batch''', 1: '''decoder_sequence'''}

     if self.use_past:
       self.fill_with_past_key_values_(snake_case_  , direction='''inputs'''   )
   elif self.task == "causal-lm":
     # TODO: figure this case out.
     lowercase									=OrderedDict(
         [
             ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
             ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
         ]   )
     if self.use_past:
       lowercase							, lowercase									=self.num_layers
       for i in range(snake_case_   ):
         lowercase									={0: '''batch''', 2: '''past_sequence + sequence'''}
         lowercase									={0: '''batch''', 2: '''past_sequence + sequence'''}
   else:
     lowercase									=OrderedDict(
         [
             ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
             ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
             ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}),
             ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}),
         ]   )

   return common_inputs







 @property
 # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
 def 						_A(       self   ):
   if self.task in ["default", "seq2seq-lm"]:
     lowercase									=super().outputs
   else:
     lowercase									=super(snake_case_  , self   ).outputs
     if self.use_past:
       lowercase							, lowercase									=self.num_layers
       for i in range(snake_case_   ):
         lowercase									={0: '''batch''', 2: '''past_sequence + sequence'''}
         lowercase									={0: '''batch''', 2: '''past_sequence + sequence'''}
   return common_outputs







 def 						_A(       self  , snake_case_  , snake_case_ = -1  , snake_case_ = -1  , snake_case_ = False  , snake_case_ = None  , ):
   lowercase									=self._generate_dummy_inputs_for_encoder_and_decoder(
       snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_   )

   # Generate decoder inputs
   lowercase									=seq_length if not self.use_past else 1
   lowercase									=self._generate_dummy_inputs_for_encoder_and_decoder(
       snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_   )
   lowercase									={f'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
   lowercase									=dict(**snake_case_  , **snake_case_   )

   if self.use_past:
     if not is_torch_available():
       raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''   )
     else:
       import torch
     lowercase							, lowercase									=common_inputs['''input_ids'''].shape
     lowercase									=common_inputs['''decoder_input_ids'''].shape[1]
     lowercase							, lowercase									=self.num_attention_heads
     lowercase									=(
         batch,
         num_encoder_attention_heads,
         encoder_seq_length,
         self._config.hidden_size // num_encoder_attention_heads,
     )
     lowercase									=decoder_seq_length + 3
     lowercase									=(
         batch,
         num_decoder_attention_heads,
         decoder_past_length,
         self._config.hidden_size // num_decoder_attention_heads,
     )

     lowercase									=torch.cat(
         [common_inputs['''decoder_attention_mask'''], torch.ones(snake_case_  , snake_case_   )]  , dim=1   )

     lowercase									=[]
     # If the number of encoder and decoder layers are present in the model configuration, both are considered
     lowercase							, lowercase									=self.num_layers
     lowercase									=min(snake_case_  , snake_case_   )
     lowercase									=max(snake_case_  , snake_case_   ) - min_num_layers
     lowercase									='''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder'''

     for _ in range(snake_case_   ):
       common_inputs["past_key_values"].append(
           (
               torch.zeros(snake_case_   ),
               torch.zeros(snake_case_   ),
               torch.zeros(snake_case_   ),
               torch.zeros(snake_case_   ),
           )   )
     # TODO: test this.
     lowercase									=encoder_shape if remaining_side_name == '''encoder''' else decoder_shape
     for _ in range(snake_case_  , snake_case_   ):
       common_inputs["past_key_values"].append((torch.zeros(snake_case_   ), torch.zeros(snake_case_   ))   )
   return common_inputs







 def 						_A(       self  , snake_case_  , snake_case_ = -1  , snake_case_ = -1  , snake_case_ = False  , snake_case_ = None  , ):
   lowercase									=self._generate_dummy_inputs_for_encoder_and_decoder(
       snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_   )

   if self.use_past:
     if not is_torch_available():
       raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''   )
     else:
       import torch
     lowercase							, lowercase									=common_inputs['''input_ids'''].shape
     # Not using the same length for past_key_values
     lowercase									=seqlen + 2
     lowercase							, lowercase									=self.num_layers
     lowercase							, lowercase									=self.num_attention_heads
     lowercase									=(
         batch,
         num_encoder_attention_heads,
         past_key_values_length,
         self._config.hidden_size // num_encoder_attention_heads,
     )

     lowercase									=common_inputs['''attention_mask'''].dtype
     lowercase									=torch.cat(
         [common_inputs['''attention_mask'''], torch.ones(snake_case_  , snake_case_  , dtype=snake_case_   )]  , dim=1   )
     lowercase									=[
         (torch.zeros(snake_case_   ), torch.zeros(snake_case_   )) for _ in range(snake_case_   )
     ]
   return common_inputs







 def 						_A(       self  , snake_case_  , snake_case_ = -1  , snake_case_ = -1  , snake_case_ = False  , snake_case_ = None  , ):
   # Copied from OnnxConfig.generate_dummy_inputs
   # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
   # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
   lowercase									=compute_effective_axis_dimension(
       snake_case_  , fixed_dimension=OnnxConfig.default_fixed_batch  , num_token_to_add=0   )

   # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
   lowercase									=tokenizer.num_special_tokens_to_add(snake_case_   )
   lowercase									=compute_effective_axis_dimension(
       snake_case_  , fixed_dimension=OnnxConfig.default_fixed_sequence  , num_token_to_add=snake_case_   )

   # Generate dummy inputs according to compute batch and sequence
   lowercase									=[''' '''.join([tokenizer.unk_token]   ) * seq_length] * batch_size
   lowercase									=dict(tokenizer(snake_case_  , return_tensors=snake_case_   )   )
   return common_inputs







 def 						_A(       self  , snake_case_  , snake_case_ = -1  , snake_case_ = -1  , snake_case_ = False  , snake_case_ = None  , ):
   if self.task in ["default", "seq2seq-lm"]:
     lowercase									=self._generate_dummy_inputs_for_default_and_seqaseq_lm(
         snake_case_  , batch_size=snake_case_  , seq_length=snake_case_  , is_pair=snake_case_  , framework=snake_case_   )

   else:
     lowercase									=self._generate_dummy_inputs_for_causal_lm(
         snake_case_  , batch_size=snake_case_  , seq_length=snake_case_  , is_pair=snake_case_  , framework=snake_case_   )

   return common_inputs







 def 						_A(       self  , snake_case_  , snake_case_  , snake_case_  , snake_case_   ):
   if self.task in ["default", "seq2seq-lm"]:
     lowercase									=super()._flatten_past_key_values_(snake_case_  , snake_case_  , snake_case_  , snake_case_   )
   else:
     lowercase									=super(snake_case_  , self   )._flatten_past_key_values_(
         snake_case_  , snake_case_  , snake_case_  , snake_case_   )







 @property
 def 						_A(       self   ):
   return 1E-4


"""simple docstring"""




import functools
import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

import huggingface_hub.utils as hf_hub_utils
from tqdm import auto as tqdm_lib


A__ :       int				= threading.Lock()
A__ :       Optional[logging.Handler]				= None

A__ :       str				= {
    'debug': logging.DEBUG,
    'info': logging.INFO,
    'warning': logging.WARNING,
    'error': logging.ERROR,
    'critical': logging.CRITICAL,
}

A__ :       str				= logging.WARNING

A__ :       Union[str, Any]				= True




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _lowercase:      List[Any]   			=  os.getenv('''TRANSFORMERS_VERBOSITY'''			,					_UpperCamelCase    )
  if env_level_str:
    if env_level_str in log_levels:
      return log_levels[env_level_str]
    else:
      logging.getLogger().warning(
          f'''Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, '''
          f'''has to be one of: { ", ".join(log_levels.keys()    ) }'''    )
  return _default_log_level




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return __name__.split('''.'''    )[0]




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return logging.getLogger(_get_library_name()    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  global _default_handler

  with _lock:
    if _default_handler:
      # This library has already configured the library root logger.
      return
    _lowercase:      int   			=  logging.StreamHandler()  # Set sys.stderr as stream.
    _lowercase:      Dict   			=  sys.stderr.flush

    # Apply our default configuration to the library root logger.
    _lowercase:      Dict   			=  _get_library_root_logger()
    library_root_logger.addHandler(_default_handler    )
    library_root_logger.setLevel(_get_default_logging_level()    )
    _lowercase:      Optional[Any]   			=  False




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  global _default_handler

  with _lock:
    if not _default_handler:
      return

    _lowercase:      Tuple   			=  _get_library_root_logger()
    library_root_logger.removeHandler(_default_handler    )
    library_root_logger.setLevel(logging.NOTSET    )
    _lowercase:      Dict   			=  None




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return log_levels




def 			_lowerCAmelCase		(		_UpperCamelCase = None    ):






  """simple docstring"""



  if name is None:
    _lowercase:      Tuple   			=  _get_library_name()

  _configure_library_root_logger()
  return logging.getLogger(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _configure_library_root_logger()
  return _get_library_root_logger().getEffectiveLevel()




def 			_lowerCAmelCase		(		_UpperCamelCase    ):






  """simple docstring"""



  _configure_library_root_logger()
  _get_library_root_logger().setLevel(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return set_verbosity(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return set_verbosity(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return set_verbosity(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  return set_verbosity(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _configure_library_root_logger()

  assert _default_handler is not None
  _get_library_root_logger().removeHandler(_default_handler    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _configure_library_root_logger()

  assert _default_handler is not None
  _get_library_root_logger().addHandler(_default_handler    )




def 			_lowerCAmelCase		(		_UpperCamelCase    ):






  """simple docstring"""



  _configure_library_root_logger()

  assert handler is not None
  _get_library_root_logger().addHandler(_UpperCamelCase    )




def 			_lowerCAmelCase		(		_UpperCamelCase    ):






  """simple docstring"""



  _configure_library_root_logger()

  assert handler is not None and handler not in _get_library_root_logger().handlers
  _get_library_root_logger().removeHandler(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _configure_library_root_logger()
  _lowercase:      str   			=  False




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _configure_library_root_logger()
  _lowercase:      List[str]   			=  True




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _lowercase:      Any   			=  _get_library_root_logger().handlers

  for handler in handlers:
    _lowercase:      int   			=  logging.Formatter('''[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s'''    )
    handler.setFormatter(_UpperCamelCase    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  _lowercase:      List[str]   			=  _get_library_root_logger().handlers

  for handler in handlers:
    handler.setFormatter(_UpperCamelCase    )




def 			_lowerCAmelCase		(		self			,					*_UpperCamelCase			,					**_UpperCamelCase    ):






  """simple docstring"""



  _lowercase:      Any   			=  os.getenv('''TRANSFORMERS_NO_ADVISORY_WARNINGS'''			,					_UpperCamelCase    )
  if no_advisory_warnings:
    return
  self.warning(*_UpperCamelCase			,					**_UpperCamelCase    )


A__ :       Optional[int]				= warning_advice




@functools.lru_cache(_UpperCamelCase    )
def 			_lowerCAmelCase		(		self			,					*_UpperCamelCase			,					**_UpperCamelCase    ):






  """simple docstring"""



  self.warning(*_UpperCamelCase			,					**_UpperCamelCase    )


A__ :       List[Any]				= warning_once


class       __magic_name__							:
       def __init__(  self			,					*A_			,					**A_							)  ->						Any:  # pylint: disable=unused-argument





         """simple docstring"""




         _lowercase:      Tuple   			=  args[0] if args else None
       def __iter__(  self							)  ->						Union[str, Any]:





         """simple docstring"""




         return iter(self._iterator							)
       def __getattr__(  self			,					A_							)  ->						List[Any]:





         """simple docstring"""




         def empty_fn(*A_			,					**A_							):  # pylint: disable=unused-argument
           return

         return empty_fn
       def __enter__(  self							)  ->						Tuple:





         """simple docstring"""




         return self



       def __exit__(  self			,					A_			,					A_			,					A_							)  ->						Optional[Any]:





         """simple docstring"""




         return


class       __magic_name__							:
       def __call__(  self			,					*A_			,					**A_							)  ->						Dict:





         """simple docstring"""




         if _tqdm_active:
           return tqdm_lib.tqdm(*A_			,					**A_							)
         else:
           return EmptyTqdm(*A_			,					**A_							)
       def 				lowercase_				(  self			,					*A_			,					**A_							)  ->						List[str]:





         """simple docstring"""




         _lowercase:      Optional[Any]   			=  None
         if _tqdm_active:
           return tqdm_lib.tqdm.set_lock(*A_			,					**A_							)



       def 				lowercase_				(  self							)  ->						Optional[Any]:





         """simple docstring"""




         if _tqdm_active:
           return tqdm_lib.tqdm.get_lock()

A__ :       str				= _tqdm_cls()




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  global _tqdm_active
  return bool(_tqdm_active    )




def 			_lowerCAmelCase		(		):






  """simple docstring"""



  global _tqdm_active
  _lowercase:      str   			=  True
  hf_hub_utils.enable_progress_bars()







def 			_lowerCAmelCase		(		):






  """simple docstring"""



  global _tqdm_active
  _lowercase:      Union[str, Any]   			=  False
  hf_hub_utils.disable_progress_bars()
