



"""simple docstring"""

from __future__ import annotations

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

A_						:    Any				=      TypeVar('T')
A_						:    int				=      TypeVar('U')
class     lowerCAmelCase__						(	Generic[T, U] ):


	'''simple docstring'''







	def __init__(      self				:       Dict     ,       _SCREAMING_SNAKE_CASE				:       T | None     ,       _SCREAMING_SNAKE_CASE				:       U | None    )						->	Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     key
					SCREAMING_SNAKE_CASE						:							Optional[int]							=     val
					SCREAMING_SNAKE_CASE						:							DoubleLinkedListNode[T, U] | None							=     None
					SCREAMING_SNAKE_CASE						:							DoubleLinkedListNode[T, U] | None							=     None







	def __repr__(      self				:       Any    )						->	str:





					"""simple docstring"""


					return (
					    f"""Node: key: {self.key}, val: {self.val}, """
					    f"""has next: {bool(self.next    )}, has prev: {bool(self.prev    )}"""
					)




class     lowerCAmelCase__						(	Generic[T, U] ):


	'''simple docstring'''







	def __init__(      self				:       str    )						->	None:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							DoubleLinkedListNode[T, U]							=     DoubleLinkedListNode(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							DoubleLinkedListNode[T, U]							=     DoubleLinkedListNode(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE						:							Optional[Any]							=     self.rear, self.head





	def __repr__(      self				:       Tuple    )						->	str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							List[str]							=     ['DoubleLinkedList']
					SCREAMING_SNAKE_CASE						:							List[Any]							=     self.head
					while node.next is not None:
									rep.append(str(_SCREAMING_SNAKE_CASE    )    )
									SCREAMING_SNAKE_CASE						:							Dict							=     node.next
					rep.append(str(self.rear    )    )
					return ",\n    ".join(_SCREAMING_SNAKE_CASE    )





	def 			_lowerCAmelCase     (      self				:       Optional[int]     ,       _SCREAMING_SNAKE_CASE				:       DoubleLinkedListNode[T, U]    )						->	None:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     self.rear.prev

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

					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     node
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     previous
					SCREAMING_SNAKE_CASE						:							Dict							=     node
					SCREAMING_SNAKE_CASE						:							Optional[int]							=     self.rear







	def 			_lowerCAmelCase     (      self				:       Union[str, Any]     ,       _SCREAMING_SNAKE_CASE				:       DoubleLinkedListNode[T, U]    )						->	DoubleLinkedListNode[T, U] | None:





					"""simple docstring"""


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

					SCREAMING_SNAKE_CASE						:							List[Any]							=     node.next
					SCREAMING_SNAKE_CASE						:							List[str]							=     node.prev
					SCREAMING_SNAKE_CASE						:							Optional[int]							=     None
					SCREAMING_SNAKE_CASE						:							Any							=     None
					return node




class     lowerCAmelCase__						(	Generic[T, U] ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE	:		dict[Callable[[T], U], LRUCache[T, U]]  						= {}





	def __init__(      self				:       str     ,       _SCREAMING_SNAKE_CASE				:       int    )						->	Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							DoubleLinkedList[T, U]							=     DoubleLinkedList()
					SCREAMING_SNAKE_CASE						:							str							=     capacity
					SCREAMING_SNAKE_CASE						:							Tuple							=     0
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     0
					SCREAMING_SNAKE_CASE						:							Dict							=     0
					SCREAMING_SNAKE_CASE						:							dict[T, DoubleLinkedListNode[T, U]]							=     {}





	def __repr__(      self				:       Tuple    )						->	str:





					"""simple docstring"""


					return (
					    f"""CacheInfo(hits={self.hits}, misses={self.miss}, """
					    f"""capacity={self.capacity}, current size={self.num_keys})"""
					)





	def __contains__(      self				:       Tuple     ,       _SCREAMING_SNAKE_CASE				:       T    )						->	bool:





					"""simple docstring"""


					return key in self.cache





	def 			_lowerCAmelCase     (      self				:       Union[str, Any]     ,       _SCREAMING_SNAKE_CASE				:       T    )						->	U | None:





					"""simple docstring"""


					if key in self.cache:
									self.hits += 1
									SCREAMING_SNAKE_CASE						:							DoubleLinkedListNode[T, U]							=     self.cache[key]
									SCREAMING_SNAKE_CASE						:							Any							=     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(_SCREAMING_SNAKE_CASE    )
									return node.val
					self.miss += 1
					return None





	def 			_lowerCAmelCase     (      self				:       List[str]     ,       _SCREAMING_SNAKE_CASE				:       T     ,       _SCREAMING_SNAKE_CASE				:       U    )						->	None:





					"""simple docstring"""


					if key not in self.cache:
									if self.num_keys >= self.capacity:
													# delete first node (oldest) when over capacity
													SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     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(_SCREAMING_SNAKE_CASE    ) is not None
													)  # node guaranteed to be in list assert node.key is not None

													del self.cache[first_node.key]
													self.num_keys -= 1
									SCREAMING_SNAKE_CASE						:							List[str]							=     DoubleLinkedListNode(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )
									self.list.add(self.cache[key]    )
									self.num_keys += 1

					else:
									# bump node to the end of the list, update value
									SCREAMING_SNAKE_CASE						:							List[Any]							=     self.list.remove(self.cache[key]    )
									assert node is not None  # node guaranteed to be in list
									SCREAMING_SNAKE_CASE						:							str							=     value
									self.list.add(_SCREAMING_SNAKE_CASE    )







	@classmethod
	def 			_lowerCAmelCase     (      cls				:       Any     ,       _SCREAMING_SNAKE_CASE				:       int = 128    )						->	Callable[[Callable[[T], U]], Callable[..., U]]:





					"""simple docstring"""


					def cache_decorator_inner(_SCREAMING_SNAKE_CASE				:       Callable[[T], U]    ) -> Callable[..., U]:
									def cache_decorator_wrapper(*_SCREAMING_SNAKE_CASE				:       T    ) -> U:
													if func not in cls.decorator_function_to_instance_map:
																	SCREAMING_SNAKE_CASE						:							List[Any]							=     LRUCache(_SCREAMING_SNAKE_CASE    )

													SCREAMING_SNAKE_CASE						:							int							=     cls.decorator_function_to_instance_map[func].get(args[0]    )
													if result is None:
																	SCREAMING_SNAKE_CASE						:							Dict							=     func(*_SCREAMING_SNAKE_CASE    )
																	cls.decorator_function_to_instance_map[func].put(args[0]     ,       _SCREAMING_SNAKE_CASE    )
													return result

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

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

									return cache_decorator_wrapper

					return cache_decorator_inner


if __name__ == "__main__":
							import doctest

							doctest.testmod()










"""simple docstring"""

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


A_						:    Optional[int]				=      {
    'configuration_llama': ['LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LlamaConfig'],
}

try:
							if not is_sentencepiece_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A_						:    int				=      ['LlamaTokenizer']

try:
							if not is_tokenizers_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A_						:    List[str]				=      ['LlamaTokenizerFast']

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A_						:    List[str]				=      [
							    'LlamaForCausalLM',
							    'LlamaModel',
							    'LlamaPreTrainedModel',
							    'LlamaForSequenceClassification',
							]


if TYPE_CHECKING:
							from .configuration_llama import LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP, LlamaConfig

							try:
														if not is_sentencepiece_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_llama import LlamaTokenizer

							try:
														if not is_tokenizers_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_llama_fast import LlamaTokenizerFast

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_llama import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaPreTrainedModel


else:
							import sys

							A_						:    str				=      _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





