

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__		 =     logging.get_logger(__name__)

UpperCamelCase__		 =     {
    '''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     __snake_case    (				snake_case__  ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE				    =							"marian"
       __SCREAMING_SNAKE_CASE				    =							["past_key_values"]
       __SCREAMING_SNAKE_CASE				    =							{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}


       def __init__(		self      , _UpperCamelCase=5_81_01      , _UpperCamelCase=None      , _UpperCamelCase=10_24      , _UpperCamelCase=12      , _UpperCamelCase=40_96      , _UpperCamelCase=16      , _UpperCamelCase=12      , _UpperCamelCase=40_96      , _UpperCamelCase=16      , _UpperCamelCase=0.0      , _UpperCamelCase=0.0      , _UpperCamelCase=True      , _UpperCamelCase=True      , _UpperCamelCase="gelu"      , _UpperCamelCase=10_24      , _UpperCamelCase=0.1      , _UpperCamelCase=0.0      , _UpperCamelCase=0.0      , _UpperCamelCase=0.02      , _UpperCamelCase=5_81_00      , _UpperCamelCase=False      , _UpperCamelCase=5_81_00      , _UpperCamelCase=0      , _UpperCamelCase=0      , _UpperCamelCase=True      , **_UpperCamelCase      , )      ->							int:






              """simple docstring"""


              __snake_case       	=							vocab_size
              __snake_case       	=							decoder_vocab_size or vocab_size
              __snake_case       	=							max_position_embeddings
              __snake_case       	=							d_model
              __snake_case       	=							encoder_ffn_dim
              __snake_case       	=							encoder_layers
              __snake_case       	=							encoder_attention_heads
              __snake_case       	=							decoder_ffn_dim
              __snake_case       	=							decoder_layers
              __snake_case       	=							decoder_attention_heads
              __snake_case       	=							dropout
              __snake_case       	=							attention_dropout
              __snake_case       	=							activation_dropout
              __snake_case       	=							activation_function
              __snake_case       	=							init_std
              __snake_case       	=							encoder_layerdrop
              __snake_case       	=							decoder_layerdrop
              __snake_case       	=							use_cache
              __snake_case       	=							encoder_layers
              __snake_case       	=							scale_embedding  # scale factor will be sqrt(d_model) if True
              __snake_case       	=							share_encoder_decoder_embeddings
              super().__init__(
                  pad_token_id=_UpperCamelCase      , eos_token_id=_UpperCamelCase      , is_encoder_decoder=_UpperCamelCase      , decoder_start_token_id=_UpperCamelCase      , forced_eos_token_id=_UpperCamelCase      , **_UpperCamelCase      , )






class     __snake_case    (				snake_case__  ):
       """simple docstring"""







       @property
       # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
       def 			a		(		self   )      ->							Mapping[str, Mapping[int, str]]:






              """simple docstring"""


              if self.task in ["default", "seq2seq-lm"]:
                     __snake_case       	=							OrderedDict(
                         [
                             ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                             ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                         ]   )

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

                     if self.use_past:
                            self.fill_with_past_key_values_(_UpperCamelCase      , direction="""inputs"""   )
              elif self.task == "causal-lm":
                     # TODO: figure this case out.
                     __snake_case       	=							OrderedDict(
                         [
                             ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                             ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                         ]   )
                     if self.use_past:
                            __snake_case	,    __snake_case       	=							self.num_layers
                            for i in range(_UpperCamelCase   ):
                                   __snake_case       	=							{0: """batch""", 2: """past_sequence + sequence"""}
                                   __snake_case       	=							{0: """batch""", 2: """past_sequence + sequence"""}
              else:
                     __snake_case       	=							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   )      ->							Mapping[str, Mapping[int, str]]:






              """simple docstring"""


              if self.task in ["default", "seq2seq-lm"]:
                     __snake_case       	=							super().outputs
              else:
                     __snake_case       	=							super(_UpperCamelCase      , self   ).outputs
                     if self.use_past:
                            __snake_case	,    __snake_case       	=							self.num_layers
                            for i in range(_UpperCamelCase   ):
                                   __snake_case       	=							{0: """batch""", 2: """past_sequence + sequence"""}
                                   __snake_case       	=							{0: """batch""", 2: """past_sequence + sequence"""}
              return common_outputs


       def 			a		(		self      , _UpperCamelCase      , _UpperCamelCase = -1      , _UpperCamelCase = -1      , _UpperCamelCase = False      , _UpperCamelCase = None      , )      ->							Mapping[str, Any]:






              """simple docstring"""


              __snake_case       	=							self._generate_dummy_inputs_for_encoder_and_decoder(
                  _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase   )

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

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

                     __snake_case       	=							torch.cat(
                         [common_inputs["""decoder_attention_mask"""], torch.ones(_UpperCamelCase      , _UpperCamelCase   )]      , dim=1   )

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

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


       def 			a		(		self      , _UpperCamelCase      , _UpperCamelCase = -1      , _UpperCamelCase = -1      , _UpperCamelCase = False      , _UpperCamelCase = None      , )      ->							Mapping[str, Any]:






              """simple docstring"""


              __snake_case       	=							self._generate_dummy_inputs_for_encoder_and_decoder(
                  _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase   )

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

                     __snake_case       	=							common_inputs["""attention_mask"""].dtype
                     __snake_case       	=							torch.cat(
                         [common_inputs["""attention_mask"""], torch.ones(_UpperCamelCase      , _UpperCamelCase      , dtype=_UpperCamelCase   )]      , dim=1   )
                     __snake_case       	=							[
                         (torch.zeros(_UpperCamelCase   ), torch.zeros(_UpperCamelCase   )) for _ in range(_UpperCamelCase   )
                     ]
              return common_inputs


       def 			a		(		self      , _UpperCamelCase      , _UpperCamelCase = -1      , _UpperCamelCase = -1      , _UpperCamelCase = False      , _UpperCamelCase = None      , )      ->							Mapping[str, Any]:






              """simple docstring"""


              __snake_case       	=							compute_effective_axis_dimension(
                  _UpperCamelCase      , 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
              __snake_case       	=							tokenizer.num_special_tokens_to_add(_UpperCamelCase   )
              __snake_case       	=							compute_effective_axis_dimension(
                  _UpperCamelCase      , fixed_dimension=OnnxConfig.default_fixed_sequence      , num_token_to_add=_UpperCamelCase   )

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


       def 			a		(		self      , _UpperCamelCase      , _UpperCamelCase = -1      , _UpperCamelCase = -1      , _UpperCamelCase = False      , _UpperCamelCase = None      , )      ->							Mapping[str, Any]:






              """simple docstring"""


              if self.task in ["default", "seq2seq-lm"]:
                     __snake_case       	=							self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                         _UpperCamelCase      , batch_size=_UpperCamelCase      , seq_length=_UpperCamelCase      , is_pair=_UpperCamelCase      , framework=_UpperCamelCase   )

              else:
                     __snake_case       	=							self._generate_dummy_inputs_for_causal_lm(
                         _UpperCamelCase      , batch_size=_UpperCamelCase      , seq_length=_UpperCamelCase      , is_pair=_UpperCamelCase      , framework=_UpperCamelCase   )

              return common_inputs


       def 			a		(		self      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase   )      ->							str:






              """simple docstring"""


              if self.task in ["default", "seq2seq-lm"]:
                     __snake_case       	=							super()._flatten_past_key_values_(_UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase   )
              else:
                     __snake_case       	=							super(_UpperCamelCase      , self   )._flatten_past_key_values_(
                         _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase   )






       @property
       def 			a		(		self   )      ->							float:






              """simple docstring"""


              return 1E-4




def 						lowerCamelCase__      ( __A    :int	,__A    :int		):


       """simple docstring"""




       if a < 0 or b < 0:
              raise ValueError("""the value of both inputs must be positive"""		)

       __snake_case       	=							str(bin(__A		)		)[2:]  # remove the leading "0b"
       __snake_case       	=							str(bin(__A		)		)[2:]  # remove the leading "0b"

       __snake_case       	=							max(len(__A		)	,len(__A		)		)

       return "0b" + "".join(
           str(int(char_a == """1""" and char_b == """1"""		)		)
           for char_a, char_b in zip(a_binary.zfill(__A		)	,b_binary.zfill(__A		)		)		)


if __name__ == "__main__":
    import doctest

    doctest.testmod()

