


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

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


SCREAMING_SNAKE_CASE__       			=				logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__       			=				{
    """gpt2""": """https://huggingface.co/gpt2/resolve/main/config.json""",
    """gpt2-medium""": """https://huggingface.co/gpt2-medium/resolve/main/config.json""",
    """gpt2-large""": """https://huggingface.co/gpt2-large/resolve/main/config.json""",
    """gpt2-xl""": """https://huggingface.co/gpt2-xl/resolve/main/config.json""",
    """distilgpt2""": """https://huggingface.co/distilgpt2/resolve/main/config.json""",
}







class     __lowerCamelCase     (  __a     ):


							"""simple docstring"""

							lowerCAmelCase__          =				"gpt2"
							lowerCAmelCase__          =				["past_key_values"]
							lowerCAmelCase__          =				{
							    "hidden_size": "n_embd",
							    "max_position_embeddings": "n_positions",
							    "num_attention_heads": "n_head",
							    "num_hidden_layers": "n_layer",
							}




							def __init__(  self						,       UpperCAmelCase=50257						,       UpperCAmelCase=1024						,       UpperCAmelCase=768						,       UpperCAmelCase=12						,       UpperCAmelCase=12						,       UpperCAmelCase=None						,       UpperCAmelCase="gelu_new"						,       UpperCAmelCase=0.1						,       UpperCAmelCase=0.1						,       UpperCAmelCase=0.1						,       UpperCAmelCase=1e-5						,       UpperCAmelCase=0.02						,       UpperCAmelCase="cls_index"						,       UpperCAmelCase=True						,       UpperCAmelCase=None						,       UpperCAmelCase=True						,       UpperCAmelCase=0.1						,       UpperCAmelCase=True						,       UpperCAmelCase=True						,       UpperCAmelCase=50256						,       UpperCAmelCase=50256						,       UpperCAmelCase=False						,       UpperCAmelCase=False						,       **UpperCAmelCase						,       )     ->       Dict:






								'''simple docstring'''






								lowercase_			 = vocab_size
								lowercase_			 = n_positions
								lowercase_			 = n_embd
								lowercase_			 = n_layer
								lowercase_			 = n_head
								lowercase_			 = n_inner
								lowercase_			 = activation_function
								lowercase_			 = resid_pdrop
								lowercase_			 = embd_pdrop
								lowercase_			 = attn_pdrop
								lowercase_			 = layer_norm_epsilon
								lowercase_			 = initializer_range
								lowercase_			 = summary_type
								lowercase_			 = summary_use_proj
								lowercase_			 = summary_activation
								lowercase_			 = summary_first_dropout
								lowercase_			 = summary_proj_to_labels
								lowercase_			 = scale_attn_weights
								lowercase_			 = use_cache
								lowercase_			 = scale_attn_by_inverse_layer_idx
								lowercase_			 = reorder_and_upcast_attn

								lowercase_			 = bos_token_id
								lowercase_			 = eos_token_id

								super().__init__(bos_token_id=lowerCAmelCase_						,       eos_token_id=lowerCAmelCase_						,       **lowerCAmelCase_  )







class     __lowerCamelCase     (  __a     ):


							"""simple docstring"""

							def __init__(  self						,       UpperCAmelCase						,       UpperCAmelCase = "default"						,       UpperCAmelCase = None						,       UpperCAmelCase = False						,       )     ->       List[str]:






								'''simple docstring'''






								super().__init__(lowerCAmelCase_						,       task=lowerCAmelCase_						,       patching_specs=lowerCAmelCase_						,       use_past=lowerCAmelCase_  )
								if not getattr(self._config						,       "pad_token_id"						,       lowerCAmelCase_  ):
									# TODO: how to do that better?
									lowercase_			 = 0




							@property
							def 				A__      (  self  )     ->       Mapping[str, Mapping[int, str]]:






								'''simple docstring'''






								lowercase_			 = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}}  )
								if self.use_past:
									self.fill_with_past_key_values_(lowerCAmelCase_						,       direction="inputs"  )
									lowercase_			 = {0: "batch", 1: "past_sequence + sequence"}
								else:
									lowercase_			 = {0: "batch", 1: "sequence"}

								return common_inputs




							@property
							def 				A__      (  self  )     ->       int:






								'''simple docstring'''






								return self._config.n_layer




							@property
							def 				A__      (  self  )     ->       int:






								'''simple docstring'''






								return self._config.n_head




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






								'''simple docstring'''






								lowercase_			 = super(lowerCAmelCase_						,       self  ).generate_dummy_inputs(
								    lowerCAmelCase_						,       batch_size=lowerCAmelCase_						,       seq_length=lowerCAmelCase_						,       is_pair=lowerCAmelCase_						,       framework=lowerCAmelCase_  )

								# We need to order the input in the way they appears in the forward()
								lowercase_			 = OrderedDict({"input_ids": common_inputs["input_ids"]}  )

								# Need to add the past_keys
								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_			 = (
										    batch,
										    self.num_attention_heads,
										    past_key_values_length,
										    self._config.hidden_size // self.num_attention_heads,
										)
										lowercase_			 = [
										    (torch.zeros(lowerCAmelCase_  ), torch.zeros(lowerCAmelCase_  )) for _ in range(self.num_layers  )
										]

								lowercase_			 = common_inputs["attention_mask"]
								if self.use_past:
									lowercase_			 = ordered_inputs["attention_mask"].dtype
									lowercase_			 = torch.cat(
									    [ordered_inputs["attention_mask"], torch.ones(lowerCAmelCase_						,       lowerCAmelCase_						,       dtype=lowerCAmelCase_  )]						,       dim=1  )

								return ordered_inputs




							@property
							def 				A__      (  self  )     ->       int:






								'''simple docstring'''






								return 13






'''simple docstring'''


from dataclasses import dataclass
from typing import Callable, Optional

import datasets







@dataclass
class 						UpperCAmelCase						(		datasets.BuilderConfig):


 '''simple docstring'''




 __magic_name__       :			Optional[Callable]	=   None
 __magic_name__       :			Optional[dict]	=   None
 __magic_name__       :			Optional[datasets.Features]	=   None




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



      """simple docstring"""







      assert self.generator is not None, "generator must be specified"

      if self.gen_kwargs is None:
           a_									={}







class 						UpperCAmelCase						(		datasets.GeneratorBasedBuilder):


 '''simple docstring'''




 __magic_name__       :			Tuple	=   GeneratorConfig




 def    lowercase_	( self)			->   Tuple:



      """simple docstring"""







      return datasets.DatasetInfo(features=self.config.features)




 def    lowercase_	( self				,	lowerCAmelCase_)			->   int:



      """simple docstring"""







      return [datasets.SplitGenerator(name=datasets.Split.TRAIN				,	gen_kwargs=self.config.gen_kwargs)]




 def    lowercase_	( self				,	**lowerCAmelCase_)			->   List[Any]:



      """simple docstring"""







      for idx, ex in enumerate(self.config.generator(**lowerCAmelCase_)):
           yield idx, ex


