


'''simple docstring'''






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


__UpperCAmelCase    				=							logging.get_logger(__name__)

__UpperCAmelCase    				=							{
    '''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 		a__				(							a__  ):





							'''simple docstring'''






							lowercase__			:   List[str]							=      "gpt2"
							lowercase__			:   Any							=      ["past_key_values"]
							lowercase__			:   Union[str, Any]							=      {
							    "hidden_size": "n_embd",
							    "max_position_embeddings": "n_positions",
							    "num_attention_heads": "n_head",
							    "num_hidden_layers": "n_layer",
							}







							def __init__(		self					,    lowerCamelCase_=5_02_57					,    lowerCamelCase_=10_24					,    lowerCamelCase_=7_68					,    lowerCamelCase_=12					,    lowerCamelCase_=12					,    lowerCamelCase_=None					,    lowerCamelCase_="gelu_new"					,    lowerCamelCase_=0.1					,    lowerCamelCase_=0.1					,    lowerCamelCase_=0.1					,    lowerCamelCase_=1e-5					,    lowerCamelCase_=0.02					,    lowerCamelCase_="cls_index"					,    lowerCamelCase_=True					,    lowerCamelCase_=None					,    lowerCamelCase_=True					,    lowerCamelCase_=0.1					,    lowerCamelCase_=True					,    lowerCamelCase_=True					,    lowerCamelCase_=5_02_56					,    lowerCamelCase_=5_02_56					,    lowerCamelCase_=False					,    lowerCamelCase_=False					,    **lowerCamelCase_					,    )       ->							Dict:
														lowerCAmelCase__          =	vocab_size
														lowerCAmelCase__          =	n_positions
														lowerCAmelCase__          =	n_embd
														lowerCAmelCase__          =	n_layer
														lowerCAmelCase__          =	n_head
														lowerCAmelCase__          =	n_inner
														lowerCAmelCase__          =	activation_function
														lowerCAmelCase__          =	resid_pdrop
														lowerCAmelCase__          =	embd_pdrop
														lowerCAmelCase__          =	attn_pdrop
														lowerCAmelCase__          =	layer_norm_epsilon
														lowerCAmelCase__          =	initializer_range
														lowerCAmelCase__          =	summary_type
														lowerCAmelCase__          =	summary_use_proj
														lowerCAmelCase__          =	summary_activation
														lowerCAmelCase__          =	summary_first_dropout
														lowerCAmelCase__          =	summary_proj_to_labels
														lowerCAmelCase__          =	scale_attn_weights
														lowerCAmelCase__          =	use_cache
														lowerCAmelCase__          =	scale_attn_by_inverse_layer_idx
														lowerCAmelCase__          =	reorder_and_upcast_attn

														lowerCAmelCase__          =	bos_token_id
														lowerCAmelCase__          =	eos_token_id

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




class 		a__				(							a__  ):





							'''simple docstring'''






							def __init__(		self					,    lowerCamelCase_					,    lowerCamelCase_ = "default"					,    lowerCamelCase_ = None					,    lowerCamelCase_ = False					,    )       ->							Any:
														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?
																					lowerCAmelCase__          =	0







							@property
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Mapping[str, Mapping[int, str]]:
														lowerCAmelCase__          =	OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}   )
														if self.use_past:
																					self.fill_with_past_key_values_(lowerCamelCase_					,    direction='''inputs'''   )
																					lowerCAmelCase__          =	{0: '''batch''', 1: '''past_sequence + sequence'''}
														else:
																					lowerCAmelCase__          =	{0: '''batch''', 1: '''sequence'''}

														return common_inputs







							@property
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							int:
														return self._config.n_layer







							@property
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							int:
														return self._config.n_head







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_ = -1					,    lowerCamelCase_ = -1					,    lowerCamelCase_ = False					,    lowerCamelCase_ = None					,    )       ->							Mapping[str, Any]:
														lowerCAmelCase__          =	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()
														lowerCAmelCase__          =	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

																												lowerCAmelCase__   ,					lowerCAmelCase__          =	common_inputs['''input_ids'''].shape
																												# Not using the same length for past_key_values
																												lowerCAmelCase__          =	seqlen + 2
																												lowerCAmelCase__          =	(
																												    batch,
																												    self.num_attention_heads,
																												    past_key_values_length,
																												    self._config.hidden_size // self.num_attention_heads,
																												)
																												lowerCAmelCase__          =	[
																												    (torch.zeros(lowerCamelCase_   ), torch.zeros(lowerCamelCase_   )) for _ in range(self.num_layers   )
																												]

														lowerCAmelCase__          =	common_inputs['''attention_mask''']
														if self.use_past:
																					lowerCAmelCase__          =	ordered_inputs['''attention_mask'''].dtype
																					lowerCAmelCase__          =	torch.cat(
																					    [ordered_inputs['''attention_mask'''], torch.ones(lowerCamelCase_					,    lowerCamelCase_					,    dtype=lowerCamelCase_   )]					,    dim=1   )

														return ordered_inputs







							@property
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							int:
														return 13



'''simple docstring'''






from __future__ import annotations

from collections.abc import Callable
from typing import Generic, TypeVar

__UpperCAmelCase    				=							TypeVar('''T''')
__UpperCAmelCase    				=							TypeVar('''U''')




class 		a__				(							Generic[T, U]  ):





							'''simple docstring'''






							def __init__(		self					,    lowerCamelCase_					,    lowerCamelCase_   )       ->							Union[str, Any]:
														lowerCAmelCase__          =	key
														lowerCAmelCase__          =	val
														lowerCAmelCase__          =	0
														lowerCAmelCase__          =	None
														lowerCAmelCase__          =	None







							def __repr__(		self   )       ->							str:
														return "Node: key: {}, val: {}, freq: {}, has next: {}, has prev: {}".format(
														    self.key					,    self.val					,    self.freq					,    self.next is not None					,    self.prev is not None   )




class 		a__				(							Generic[T, U]  ):





							'''simple docstring'''






							def __init__(		self   )       ->							None:
														lowerCAmelCase__          =	DoubleLinkedListNode(lowerCamelCase_					,    lowerCamelCase_   )
														lowerCAmelCase__          =	DoubleLinkedListNode(lowerCamelCase_					,    lowerCamelCase_   )
														lowerCAmelCase__   ,					lowerCAmelCase__          =	self.rear, self.head







							def __repr__(		self   )       ->							str:
														lowerCAmelCase__          =	['''DoubleLinkedList''']
														lowerCAmelCase__          =	self.head
														while node.next is not None:
																					rep.append(str(lowerCamelCase_   )   )
																					lowerCAmelCase__          =	node.next
														rep.append(str(self.rear   )   )
														return ",\n    ".join(lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_   )       ->							None:
														lowerCAmelCase__          =	self.rear.prev

														# All nodes other than self.head are guaranteed to have non-None previous
														assert previous is not None

														lowerCAmelCase__          =	node
														lowerCAmelCase__          =	previous
														lowerCAmelCase__          =	node
														lowerCAmelCase__          =	self.rear
														node.freq += 1
														self._position_node(lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_   )       ->							None:

														while node.prev is not None and node.prev.freq > node.freq:
																					# swap node with previous node
																					lowerCAmelCase__          =	node.prev

																					lowerCAmelCase__          =	previous_node.prev
																					lowerCAmelCase__          =	node.prev
																					lowerCAmelCase__          =	previous_node
																					lowerCAmelCase__          =	node







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_   )       ->							DoubleLinkedListNode[T, U] | None:

														if node.prev is None or node.next is None:
																					return None

														lowerCAmelCase__          =	node.next
														lowerCAmelCase__          =	node.prev
														lowerCAmelCase__          =	None
														lowerCAmelCase__          =	None
														return node




class 		a__				(							Generic[T, U]  ):





							'''simple docstring'''






							lowercase__			:   dict[Callable[[T], U], LFUCache[T, U]]							=      {}







							def __init__(		self					,    lowerCamelCase_   )       ->							Any:
														lowerCAmelCase__          =	DoubleLinkedList()
														lowerCAmelCase__          =	capacity
														lowerCAmelCase__          =	0
														lowerCAmelCase__          =	0
														lowerCAmelCase__          =	0
														lowerCAmelCase__          =	{}







							def __repr__(		self   )       ->							str:
														return (
														    F"""CacheInfo(hits={self.hits}, misses={self.miss}, """
														    F"""capacity={self.capacity}, current_size={self.num_keys})"""
														)







							def __contains__(		self					,    lowerCamelCase_   )       ->							bool:
														return key in self.cache







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_   )       ->							U | None:

														if key in self.cache:
																					self.hits += 1
																					lowerCAmelCase__          =	self.cache[key]
																					lowerCAmelCase__          =	self.list.remove(self.cache[key]   )
																					assert node == value_node

																					# node is guaranteed not None because it is in self.cache
																					assert node is not None
																					self.list.add(lowerCamelCase_   )
																					return node.val
														self.miss += 1
														return None







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_   )       ->							None:

														if key not in self.cache:
																					if self.num_keys >= self.capacity:
																												# delete first node when over capacity
																												lowerCAmelCase__          =	self.list.head.next

																												# guaranteed to have a non-None first node when num_keys > 0
																												# explain to type checker via assertions
																												assert first_node is not None
																												assert first_node.key is not None
																												assert self.list.remove(lowerCamelCase_   ) is not None
																												# first_node guaranteed to be in list

																												del self.cache[first_node.key]
																												self.num_keys -= 1
																					lowerCAmelCase__          =	DoubleLinkedListNode(lowerCamelCase_					,    lowerCamelCase_   )
																					self.list.add(self.cache[key]   )
																					self.num_keys += 1

														else:
																					lowerCAmelCase__          =	self.list.remove(self.cache[key]   )
																					assert node is not None  # node guaranteed to be in list
																					lowerCAmelCase__          =	value
																					self.list.add(lowerCamelCase_   )







							@classmethod
							def   __SCREAMING_SNAKE_CASE							(		cls					,    lowerCamelCase_ = 1_28   )       ->							Callable[[Callable[[T], U]], Callable[..., U]]:

														def cache_decorator_inner(lowerCamelCase_   ) -> Callable[..., U]:
																					def cache_decorator_wrapper(*lowerCamelCase_   ) -> U:
																												if func not in cls.decorator_function_to_instance_map:
																																			lowerCAmelCase__          =	LFUCache(lowerCamelCase_   )

																												lowerCAmelCase__          =	cls.decorator_function_to_instance_map[func].get(args[0]   )
																												if result is None:
																																			lowerCAmelCase__          =	func(*lowerCamelCase_   )
																																			cls.decorator_function_to_instance_map[func].put(args[0]					,    lowerCamelCase_   )
																												return result

																					def cache_info() -> LFUCache[T, U]:
																												return cls.decorator_function_to_instance_map[func]

																					setattr(lowerCamelCase_					,    '''cache_info'''					,    lowerCamelCase_   )  # noqa: B010

																					return cache_decorator_wrapper

														return cache_decorator_inner


if __name__ == "__main__":
		import doctest

		doctest.testmod()