

"""simple docstring"""






import posixpath
from pathlib import Path
from unittest.mock import patch

import pytest
from fsspec.implementations.local import AbstractFileSystem, LocalFileSystem, stringify_path
from fsspec.registry import _registry as _fsspec_registry






class       _UpperCAmelCase     (						__a):
			__a	:    Optional[Any]			   =							'mock'
			def __init__(						self ,						*_A ,						_A ,						**_A       )      ->      int:






				'''simple docstring'''







				super().__init__()
				_UpperCAmelCase      :							Any       =   LocalFileSystem(*_A ,						**_A       )
				_UpperCAmelCase      :							Optional[int]       =   Path(_A       ).resolve().as_posix() + """/"""
			def 							__snake_case	(						self ,						_A ,						*_A ,						**_A       )      ->      Any:






				'''simple docstring'''







				_UpperCAmelCase      :							str       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				return self._fs.mkdir(_A ,						*_A ,						**_A       )
			def 							__snake_case	(						self ,						_A ,						*_A ,						**_A       )      ->      Tuple:






				'''simple docstring'''







				_UpperCAmelCase      :							Tuple       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				return self._fs.makedirs(_A ,						*_A ,						**_A       )
			def 							__snake_case	(						self ,						_A       )      ->      str:






				'''simple docstring'''







				_UpperCAmelCase      :							Tuple       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				return self._fs.rmdir(_A       )
			def 							__snake_case	(						self ,						_A ,						_A=True ,						*_A ,						**_A       )      ->      Dict:






				'''simple docstring'''







				_UpperCAmelCase      :							Optional[Any]       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				_UpperCAmelCase      :							str       =   self._fs.ls(_A ,						detail=_A ,						*_A ,						**_A       )
				if detail:
					return [{**info, "name": info["name"][len(self.local_root_dir       ) :]} for info in out]
				else:
					return [name[len(self.local_root_dir       ) :] for name in out]
			def 							__snake_case	(						self ,						_A ,						*_A ,						**_A       )      ->      Optional[Any]:






				'''simple docstring'''







				_UpperCAmelCase      :							Dict       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				_UpperCAmelCase      :							int       =   dict(self._fs.info(_A ,						*_A ,						**_A       )       )
				_UpperCAmelCase      :							List[str]       =   out["""name"""][len(self.local_root_dir       ) :]
				return out
			def 							__snake_case	(						self ,						_A ,						_A ,						*_A ,						**_A       )      ->      int:






				'''simple docstring'''







				_UpperCAmelCase      :							Any       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				_UpperCAmelCase      :							Any       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				return self._fs.cp_file(_A ,						_A ,						*_A ,						**_A       )
			def 							__snake_case	(						self ,						_A ,						*_A ,						**_A       )      ->      Dict:






				'''simple docstring'''







				_UpperCAmelCase      :							int       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				return self._fs.rm_file(_A ,						*_A ,						**_A       )
			def 							__snake_case	(						self ,						_A ,						*_A ,						**_A       )      ->      Tuple:






				'''simple docstring'''







				_UpperCAmelCase      :							List[str]       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				return self._fs.rm(_A ,						*_A ,						**_A       )
			def 							__snake_case	(						self ,						_A ,						*_A ,						**_A       )      ->      Tuple:






				'''simple docstring'''







				_UpperCAmelCase      :							Union[str, Any]       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				return self._fs._open(_A ,						*_A ,						**_A       )
			def 							__snake_case	(						self ,						_A       )      ->      Union[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase      :							Dict       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				return self._fs.created(_A       )
			def 							__snake_case	(						self ,						_A       )      ->      Union[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase      :							Optional[int]       =   posixpath.join(self.local_root_dir ,						self._strip_protocol(_A       )       )
				return self._fs.modified(_A       )
			@classmethod
			def 							__snake_case	(						cls ,						_A       )      ->      Union[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase      :							List[str]       =   stringify_path(_A       )
				if path.startswith("""mock://"""       ):
					_UpperCAmelCase      :							int       =   path[7:]
				return path






class       _UpperCAmelCase     (						__a):
			__a	:    Any			   =							'tmp'
			__a	:    Optional[Any]			   =							None
			def __init__(						self ,						*_A ,						**_A       )      ->      Union[str, Any]:






				'''simple docstring'''







				assert self.tmp_dir is not None, "TmpDirFileSystem.tmp_dir is not set"
				super().__init__(*_A ,						**_A ,						local_root_dir=self.tmp_dir ,						auto_mkdir=_A       )
			@classmethod
			def 							__snake_case	(						cls ,						_A       )      ->      Any:






				'''simple docstring'''







				_UpperCAmelCase      :							Any       =   stringify_path(_A       )
				if path.startswith("""tmp://"""       ):
					_UpperCAmelCase      :							List[str]       =   path[6:]
				return path
@pytest.fixture
def  UpperCamelCase   (     )			->     Any:
	_UpperCAmelCase      :							List[str]       =   MockFileSystem
	_UpperCAmelCase      :							Optional[Any]       =   TmpDirFileSystem
	yield
	del _fsspec_registry["mock"]
	del _fsspec_registry["tmp"]
@pytest.fixture
def  UpperCamelCase   (     _lowerCAmelCase       :  Dict,					_lowerCAmelCase       :  Dict	)			->     Dict:
	_UpperCAmelCase      :							Dict       =   tmp_path_factory.mktemp("""mockfs"""	)
	return MockFileSystem(local_root_dir=__A,					auto_mkdir=__A	)





@pytest.fixture
def  UpperCamelCase   (     _lowerCAmelCase       :  List[str],					_lowerCAmelCase       :  int	)			->     Optional[Any]:
	_UpperCAmelCase      :							Tuple       =   tmp_path_factory.mktemp("""tmpfs"""	)
	with patch.object(__A,					"""tmp_dir""",					__A	):
		yield TmpDirFileSystem()




"""simple docstring"""






from collections import OrderedDict
from typing import Any, Mapping, Optional

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


lowerCamelCase__			:					Dict							  =					logging.get_logger(__name__)

lowerCamelCase__			:					Optional[Any]							  =					{
    '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/config.json''',
    # See all Blenderbot models at https://huggingface.co/models?filter=blenderbot
}






class       _UpperCAmelCase     (						__a):
			__a	:    Optional[int]			   =							"""blenderbot"""
			__a	:    Tuple			   =							["""past_key_values"""]
			__a	:    int			   =							{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}
			def __init__(						self ,						_A=80_08 ,						_A=1_28 ,						_A=2 ,						_A=1_02_40 ,						_A=32 ,						_A=24 ,						_A=1_02_40 ,						_A=32 ,						_A=0.0 ,						_A=0.0 ,						_A=True ,						_A=True ,						_A="gelu" ,						_A=25_60 ,						_A=0.1 ,						_A=0.0 ,						_A=0.0 ,						_A=0.02 ,						_A=1 ,						_A=False ,						_A=0 ,						_A=1 ,						_A=2 ,						_A=3 ,						_A=2 ,						**_A ,						)      ->      Union[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase      :							Any       =   vocab_size
				_UpperCAmelCase      :							Dict       =   max_position_embeddings
				_UpperCAmelCase      :							Optional[int]       =   d_model
				_UpperCAmelCase      :							Optional[Any]       =   encoder_ffn_dim
				_UpperCAmelCase      :							Optional[Any]       =   encoder_layers
				_UpperCAmelCase      :							List[Any]       =   encoder_attention_heads
				_UpperCAmelCase      :							str       =   decoder_ffn_dim
				_UpperCAmelCase      :							int       =   decoder_layers
				_UpperCAmelCase      :							List[str]       =   decoder_attention_heads
				_UpperCAmelCase      :							List[Any]       =   dropout
				_UpperCAmelCase      :							List[str]       =   attention_dropout
				_UpperCAmelCase      :							int       =   activation_dropout
				_UpperCAmelCase      :							Union[str, Any]       =   activation_function
				_UpperCAmelCase      :							List[str]       =   init_std
				_UpperCAmelCase      :							Dict       =   encoder_layerdrop
				_UpperCAmelCase      :							Union[str, Any]       =   decoder_layerdrop
				_UpperCAmelCase      :							str       =   use_cache
				_UpperCAmelCase      :							Optional[int]       =   encoder_layers
				_UpperCAmelCase      :							Union[str, Any]       =   scale_embedding  # scale factor will be sqrt(d_model) if True

				super().__init__(
				    pad_token_id=_A ,						bos_token_id=_A ,						eos_token_id=_A ,						is_encoder_decoder=_A ,						decoder_start_token_id=_A ,						encoder_no_repeat_ngram_size=_A ,						forced_eos_token_id=_A ,						**_A ,						)






class       _UpperCAmelCase     (						__a):
			@property
			def 							__snake_case	(						self       )      ->      Mapping[str, Mapping[int, str]]:






				'''simple docstring'''







				if self.task in ["default", "seq2seq-lm"]:
					_UpperCAmelCase      :							Tuple       =   OrderedDict(
					    [
					        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
					        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
					    ]       )
					if self.use_past:
						_UpperCAmelCase      :							List[str]       =   {0: """batch"""}
						_UpperCAmelCase      :							List[str]       =   {0: """batch""", 1: """past_decoder_sequence + sequence"""}
					else:
						_UpperCAmelCase      :							Optional[int]       =   {0: """batch""", 1: """decoder_sequence"""}
						_UpperCAmelCase      :							int       =   {0: """batch""", 1: """decoder_sequence"""}
					if self.use_past:
						self.fill_with_past_key_values_(_A ,						direction="""inputs"""       )
				elif self.task == "causal-lm":
					_UpperCAmelCase      :							List[str]       =   OrderedDict(
					    [
					        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
					        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
					    ]       )
					if self.use_past:
						_UpperCAmelCase  ,				_UpperCAmelCase      :							Optional[Any]       =   self.num_layers
						for i in range(_A       ):
							_UpperCAmelCase      :							Any       =   {0: """batch""", 2: """past_sequence + sequence"""}
							_UpperCAmelCase      :							Dict       =   {0: """batch""", 2: """past_sequence + sequence"""}
				else:
					_UpperCAmelCase      :							List[Any]       =   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 							__snake_case	(						self       )      ->      Mapping[str, Mapping[int, str]]:






				'''simple docstring'''







				if self.task in ["default", "seq2seq-lm"]:
					_UpperCAmelCase      :							Optional[Any]       =   super().outputs
				else:
					_UpperCAmelCase      :							Optional[int]       =   super(_A ,						self       ).outputs
					if self.use_past:
						_UpperCAmelCase  ,				_UpperCAmelCase      :							int       =   self.num_layers
						for i in range(_A       ):
							_UpperCAmelCase      :							Tuple       =   {0: """batch""", 2: """past_sequence + sequence"""}
							_UpperCAmelCase      :							Optional[Any]       =   {0: """batch""", 2: """past_sequence + sequence"""}
				return common_outputs
			def 							__snake_case	(						self ,						_A ,						_A = -1 ,						_A = -1 ,						_A = False ,						_A = None ,						)      ->      Mapping[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase      :							Dict       =   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
				    _A ,						_A ,						_A ,						_A ,						_A       )
				# Generate decoder inputs
				_UpperCAmelCase      :							Any       =   seq_length if not self.use_past else 1
				_UpperCAmelCase      :							List[str]       =   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
				    _A ,						_A ,						_A ,						_A ,						_A       )
				_UpperCAmelCase      :							str       =   {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
				_UpperCAmelCase      :							Dict       =   dict(**_A ,						**_A       )

				if self.use_past:
					if not is_torch_available():
						raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""       )
					else:
						import torch
					_UpperCAmelCase  ,				_UpperCAmelCase      :							Tuple       =   common_inputs["""input_ids"""].shape
					_UpperCAmelCase      :							List[Any]       =   common_inputs["""decoder_input_ids"""].shape[1]
					_UpperCAmelCase  ,				_UpperCAmelCase      :							List[str]       =   self.num_attention_heads
					_UpperCAmelCase      :							List[str]       =   (
					    batch,
					    num_encoder_attention_heads,
					    encoder_seq_length,
					    self._config.hidden_size // num_encoder_attention_heads,
					)
					_UpperCAmelCase      :							List[str]       =   decoder_seq_length
					_UpperCAmelCase      :							Tuple       =   (
					    batch,
					    num_decoder_attention_heads,
					    decoder_past_length,
					    self._config.hidden_size // num_decoder_attention_heads,
					)
					_UpperCAmelCase      :							Tuple       =   torch.cat(
					    [common_inputs["""decoder_attention_mask"""], torch.ones(_A ,						_A       )] ,						dim=1       )
					_UpperCAmelCase      :							Tuple       =   []
					_UpperCAmelCase  ,				_UpperCAmelCase      :							int       =   self.num_layers

					for _ in range(_A       ):
						common_inputs["past_key_values"].append(
						    (
						        torch.zeros(_A       ),
						        torch.zeros(_A       ),
						        torch.zeros(_A       ),
						        torch.zeros(_A       ),
						    )       )
				return common_inputs
			def 							__snake_case	(						self ,						_A ,						_A = -1 ,						_A = -1 ,						_A = False ,						_A = None ,						)      ->      Mapping[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase      :							Union[str, Any]       =   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
				    _A ,						_A ,						_A ,						_A ,						_A       )

				if self.use_past:
					if not is_torch_available():
						raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""       )
					else:
						import torch
					_UpperCAmelCase  ,				_UpperCAmelCase      :							Any       =   common_inputs["""input_ids"""].shape
					_UpperCAmelCase      :							Union[str, Any]       =   seqlen
					_UpperCAmelCase  ,				_UpperCAmelCase      :							Optional[Any]       =   self.num_layers
					_UpperCAmelCase  ,				_UpperCAmelCase      :							Tuple       =   self.num_attention_heads
					_UpperCAmelCase      :							Optional[Any]       =   (
					    batch,
					    num_encoder_attention_heads,
					    past_key_values_length,
					    self._config.hidden_size // num_encoder_attention_heads,
					)
					_UpperCAmelCase      :							str       =   common_inputs["""attention_mask"""].dtype
					_UpperCAmelCase      :							Tuple       =   torch.cat(
					    [common_inputs["""attention_mask"""], torch.ones(_A ,						_A ,						dtype=_A       )] ,						dim=1       )
					_UpperCAmelCase      :							str       =   [
					    (torch.zeros(_A       ), torch.zeros(_A       )) for _ in range(_A       )
					]
				return common_inputs
			def 							__snake_case	(						self ,						_A ,						_A = -1 ,						_A = -1 ,						_A = False ,						_A = None ,						)      ->      Mapping[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase      :							str       =   compute_effective_axis_dimension(
				    _A ,						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
				_UpperCAmelCase      :							List[str]       =   tokenizer.num_special_tokens_to_add(_A       )
				_UpperCAmelCase      :							Tuple       =   compute_effective_axis_dimension(
				    _A ,						fixed_dimension=OnnxConfig.default_fixed_sequence ,						num_token_to_add=_A       )

				# Generate dummy inputs according to compute batch and sequence
				_UpperCAmelCase      :							str       =   [""" """.join([tokenizer.unk_token]       ) * seq_length] * batch_size
				_UpperCAmelCase      :							str       =   dict(tokenizer(_A ,						return_tensors=_A       )       )
				return common_inputs
			def 							__snake_case	(						self ,						_A ,						_A = -1 ,						_A = -1 ,						_A = False ,						_A = None ,						)      ->      Mapping[str, Any]:






				'''simple docstring'''







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

				elif self.task == "causal-lm":
					_UpperCAmelCase      :							int       =   self._generate_dummy_inputs_for_causal_lm(
					    _A ,						batch_size=_A ,						seq_length=_A ,						is_pair=_A ,						framework=_A       )
				else:
					_UpperCAmelCase      :							Tuple       =   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    _A ,						batch_size=_A ,						seq_length=_A ,						is_pair=_A ,						framework=_A       )

				return common_inputs
			def 							__snake_case	(						self ,						_A ,						_A ,						_A ,						_A       )      ->      Optional[int]:






				'''simple docstring'''







				if self.task in ["default", "seq2seq-lm"]:
					_UpperCAmelCase      :							Union[str, Any]       =   super()._flatten_past_key_values_(_A ,						_A ,						_A ,						_A       )
				else:
					_UpperCAmelCase      :							Tuple       =   super(_A ,						self       )._flatten_past_key_values_(
					    _A ,						_A ,						_A ,						_A       )
			def 							__snake_case	(						self ,						_A ,						_A       )      ->      Optional[Any]:






				'''simple docstring'''







				if direction not in ["inputs", "outputs"]:
					raise ValueError(f'''direction must either be "inputs" or "outputs", but {direction} was given'''       )

				_UpperCAmelCase      :							Any       =   """past_key_values""" if direction == """inputs""" else """present"""
				_UpperCAmelCase  ,				_UpperCAmelCase      :							Any       =   self.num_layers

				_UpperCAmelCase      :							List[str]       =   """past_encoder_sequence"""
				_UpperCAmelCase      :							int       =   """past_decoder_sequence""" if direction == """inputs""" else """past_decoder_sequence + sequence"""

				for i in range(_A       ):
					_UpperCAmelCase      :							Union[str, Any]       =   {0: """batch""", 2: decoder_sequence}
					_UpperCAmelCase      :							Optional[int]       =   {0: """batch""", 2: decoder_sequence}
					_UpperCAmelCase      :							Tuple       =   {0: """batch""", 2: encoder_sequence}
					_UpperCAmelCase      :							Tuple       =   {0: """batch""", 2: encoder_sequence}





