






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


lowerCamelCase					    =	logging.get_logger(__name__)

lowerCamelCase					    =	{
    '''facebook/mbart-large-cc25''': '''https://huggingface.co/facebook/mbart-large-cc25/resolve/main/config.json''',
    # See all MBART models at https://huggingface.co/models?filter=mbart
}

class        snake_case_				(							lowercase_   ):







   """simple docstring"""



   __lowerCAmelCase  =    """mbart"""
   __lowerCAmelCase  =    ["""past_key_values"""]
   __lowerCAmelCase  =    {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}

   def __init__(     self:						List[str] ,							snake_case__:						Dict=50_265 ,							snake_case__:						int=1_024 ,							snake_case__:						List[Any]=12 ,							snake_case__:						List[Any]=4_096 ,							snake_case__:						Any=16 ,							snake_case__:						int=12 ,							snake_case__:						int=4_096 ,							snake_case__:						List[str]=16 ,							snake_case__:						Tuple=0.0 ,							snake_case__:						Dict=0.0 ,							snake_case__:						str=True ,							snake_case__:						Union[str, Any]=True ,							snake_case__:						Union[str, Any]="gelu" ,							snake_case__:						List[Any]=1_024 ,							snake_case__:						Any=0.1 ,							snake_case__:						List[Any]=0.0 ,							snake_case__:						Union[str, Any]=0.0 ,							snake_case__:						List[str]=0.02 ,							snake_case__:						Optional[int]=0.0 ,							snake_case__:						Any=False ,							snake_case__:						str=1 ,							snake_case__:						Optional[int]=0 ,							snake_case__:						str=2 ,							snake_case__:						Tuple=2 ,							**snake_case__:						Optional[int] ,							)	->		Optional[Any]:


       """simple docstring"""



       _lowercase     			=						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     			=						classifier_dropout
       _lowercase     			=						use_cache
       _lowercase     			=						encoder_layers
       _lowercase     			=						scale_embedding  # scale factor will be sqrt(d_model) if True
       super().__init__(
           pad_token_id=snake_case__ ,							bos_token_id=snake_case__ ,							eos_token_id=snake_case__ ,							is_encoder_decoder=snake_case__ ,							forced_eos_token_id=snake_case__ ,							**snake_case__ ,							)






class        snake_case_				(							lowercase_   ):







   """simple docstring"""



   @property
   def       SCREAMING_SNAKE_CASE__					(     self:						Optional[Any]						)	->		Mapping[str, Mapping[int, str]]:


       """simple docstring"""



       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
   def       SCREAMING_SNAKE_CASE__					(     self:						Dict						)	->		Mapping[str, Mapping[int, str]]:


       """simple docstring"""



       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       SCREAMING_SNAKE_CASE__					(     self:						Optional[Any] ,							snake_case__:						PreTrainedTokenizer ,							snake_case__:						int = -1 ,							snake_case__:						int = -1 ,							snake_case__:						bool = False ,							snake_case__:						Optional[TensorType] = None ,							)	->		Mapping[str, Any]:


       """simple docstring"""



       _lowercase     			=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           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_sequence_classification_and_question_answering(
           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       SCREAMING_SNAKE_CASE__					(     self:						List[Any] ,							snake_case__:						PreTrainedTokenizer ,							snake_case__:						int = -1 ,							snake_case__:						int = -1 ,							snake_case__:						bool = False ,							snake_case__:						Optional[TensorType] = None ,							)	->		Mapping[str, Any]:


       """simple docstring"""



       _lowercase     			=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           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       SCREAMING_SNAKE_CASE__					(     self:						Optional[int] ,							snake_case__:						PreTrainedTokenizer ,							snake_case__:						int = -1 ,							snake_case__:						int = -1 ,							snake_case__:						bool = False ,							snake_case__:						Optional[TensorType] = None ,							)	->		Mapping[str, Any]:


       """simple docstring"""



       _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       SCREAMING_SNAKE_CASE__					(     self:						List[str] ,							snake_case__:						PreTrainedTokenizer ,							snake_case__:						int = -1 ,							snake_case__:						int = -1 ,							snake_case__:						bool = False ,							snake_case__:						Optional[TensorType] = None ,							)	->		Mapping[str, Any]:


       """simple docstring"""



       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__						)

       elif self.task == "causal-lm":
           _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__						)
       else:
           _lowercase     			=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
               snake_case__ ,							batch_size=snake_case__ ,							seq_length=snake_case__ ,							is_pair=snake_case__ ,							framework=snake_case__						)

       return common_inputs

   def       SCREAMING_SNAKE_CASE__					(     self:						str ,							snake_case__:						Optional[Any] ,							snake_case__:						int ,							snake_case__:						List[Any] ,							snake_case__:						Dict						)	->		Union[str, Any]:


       """simple docstring"""



       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__						)








import json

import requests

from .fetch_github_info import AUTHENTICATED_USER_ENDPOINT, fetch_github_info
def 							UpperCAmelCase   ( _lowercase   ):


    '''simple docstring'''



    class        snake_case_				:







       """simple docstring"""



       def __init__(     self:						Tuple ,							snake_case__:						Optional[int]						)	->		None:


           """simple docstring"""



           assert isinstance(snake_case__ ,							(bytes, str)						)
           _lowercase     			=						content

       def       SCREAMING_SNAKE_CASE__					(     self:						Tuple						)	->		str:


           """simple docstring"""



           return json.loads(self.content						)

    def mock_response(*_lowercase,   **_lowercase   ):
        assert args[0] == AUTHENTICATED_USER_ENDPOINT
        assert "Authorization" in kwargs["headers"]
        assert kwargs["headers"]["Authorization"].startswith('token '   )
        assert "Accept" in kwargs["headers"]
        return FakeResponse(b'{"login":"test","id":1}'   )

    monkeypatch.setattr(_lowercase,   'get',   _lowercase   )
    _lowercase     			=						fetch_github_info('token'   )
    assert result["login"] == "test"
    assert result["id"] == 1
