


import os
import tempfile
import unittest

from transformers import DistilBertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    DistilBertForMaskedLM,
							    DistilBertForMultipleChoice,
							    DistilBertForQuestionAnswering,
							    DistilBertForSequenceClassification,
							    DistilBertForTokenClassification,
							    DistilBertModel,
							)







class        UpperCamelCase__   (	__magic_name__   ):

					def __init__(     self       :   Any							,					UpperCamelCase__       :   List[str]							,					UpperCamelCase__       :   List[str]=13							,					UpperCamelCase__       :   Optional[int]=7							,					UpperCamelCase__       :   List[Any]=True							,					UpperCamelCase__       :   Optional[Any]=True							,					UpperCamelCase__       :   Any=False							,					UpperCamelCase__       :   Union[str, Any]=True							,					UpperCamelCase__       :   Tuple=99							,					UpperCamelCase__       :   List[Any]=32							,					UpperCamelCase__       :   str=5							,					UpperCamelCase__       :   List[str]=4							,					UpperCamelCase__       :   Tuple=37							,					UpperCamelCase__       :   Optional[Any]="gelu"							,					UpperCamelCase__       :   List[str]=0.1							,					UpperCamelCase__       :   Union[str, Any]=0.1							,					UpperCamelCase__       :   List[Any]=512							,					UpperCamelCase__       :   List[Any]=16							,					UpperCamelCase__       :   Union[str, Any]=2							,					UpperCamelCase__       :   Optional[int]=0.02							,					UpperCamelCase__       :   str=3							,					UpperCamelCase__       :   str=4							,					UpperCamelCase__       :   Tuple=None							,					):






										'''simple docstring'''






										lowercase_  							=			parent
										lowercase_  							=			batch_size
										lowercase_  							=			seq_length
										lowercase_  							=			is_training
										lowercase_  							=			use_input_mask
										lowercase_  							=			use_token_type_ids
										lowercase_  							=			use_labels
										lowercase_  							=			vocab_size
										lowercase_  							=			hidden_size
										lowercase_  							=			num_hidden_layers
										lowercase_  							=			num_attention_heads
										lowercase_  							=			intermediate_size
										lowercase_  							=			hidden_act
										lowercase_  							=			hidden_dropout_prob
										lowercase_  							=			attention_probs_dropout_prob
										lowercase_  							=			max_position_embeddings
										lowercase_  							=			type_vocab_size
										lowercase_  							=			type_sequence_label_size
										lowercase_  							=			initializer_range
										lowercase_  							=			num_labels
										lowercase_  							=			num_choices
										lowercase_  							=			scope

					def   UpperCAmelCase__       (     self       :   Any       ):






										'''simple docstring'''






										lowercase_  							=			ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size       )

										lowercase_  							=			None
										if self.use_input_mask:
															lowercase_  							=			random_attention_mask([self.batch_size, self.seq_length]       )

										lowercase_  							=			None
										lowercase_  							=			None
										lowercase_  							=			None
										if self.use_labels:
															lowercase_  							=			ids_tensor([self.batch_size]							,					self.type_sequence_label_size       )
															lowercase_  							=			ids_tensor([self.batch_size, self.seq_length]							,					self.num_labels       )
															lowercase_  							=			ids_tensor([self.batch_size]							,					self.num_choices       )

										lowercase_  							=			self.get_config()

										return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

					def   UpperCAmelCase__       (     self       :   str       ):






										'''simple docstring'''






										return DistilBertConfig(
										    vocab_size=self.vocab_size							,					dim=self.hidden_size							,					n_layers=self.num_hidden_layers							,					n_heads=self.num_attention_heads							,					hidden_dim=self.intermediate_size							,					hidden_act=self.hidden_act							,					dropout=self.hidden_dropout_prob							,					attention_dropout=self.attention_probs_dropout_prob							,					max_position_embeddings=self.max_position_embeddings							,					initializer_range=self.initializer_range							,					)

					def   UpperCAmelCase__       (     self       :   Tuple							,					UpperCamelCase__       :   Any							,					UpperCamelCase__       :   int							,					UpperCamelCase__       :   Optional[int]							,					UpperCamelCase__       :   Optional[Any]							,					UpperCamelCase__       :   Optional[Any]							,					UpperCamelCase__       :   Optional[int]       ):






										'''simple docstring'''






										lowercase_  							=			DistilBertModel(config=UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()
										lowercase_  							=			model(UpperCamelCase__							,					UpperCamelCase__       )
										lowercase_  							=			model(UpperCamelCase__       )
										self.parent.assertEqual(result.last_hidden_state.shape							,					(self.batch_size, self.seq_length, self.hidden_size)       )

					def   UpperCAmelCase__       (     self       :   Union[str, Any]							,					UpperCamelCase__       :   Union[str, Any]							,					UpperCamelCase__       :   List[Any]							,					UpperCamelCase__       :   str							,					UpperCamelCase__       :   Union[str, Any]							,					UpperCamelCase__       :   int							,					UpperCamelCase__       :   Any       ):






										'''simple docstring'''






										lowercase_  							=			DistilBertForMaskedLM(config=UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()
										lowercase_  							=			model(UpperCamelCase__							,					attention_mask=UpperCamelCase__							,					labels=UpperCamelCase__       )
										self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.seq_length, self.vocab_size)       )

					def   UpperCAmelCase__       (     self       :   str							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   Optional[Any]							,					UpperCamelCase__       :   str							,					UpperCamelCase__       :   List[Any]							,					UpperCamelCase__       :   Optional[Any]							,					UpperCamelCase__       :   Dict       ):






										'''simple docstring'''






										lowercase_  							=			DistilBertForQuestionAnswering(config=UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()
										lowercase_  							=			model(
										    UpperCamelCase__							,					attention_mask=UpperCamelCase__							,					start_positions=UpperCamelCase__							,					end_positions=UpperCamelCase__       )
										self.parent.assertEqual(result.start_logits.shape							,					(self.batch_size, self.seq_length)       )
										self.parent.assertEqual(result.end_logits.shape							,					(self.batch_size, self.seq_length)       )

					def   UpperCAmelCase__       (     self       :   Optional[Any]							,					UpperCamelCase__       :   List[str]							,					UpperCamelCase__       :   int							,					UpperCamelCase__       :   Optional[Any]							,					UpperCamelCase__       :   List[Any]							,					UpperCamelCase__       :   Optional[int]							,					UpperCamelCase__       :   Optional[int]       ):






										'''simple docstring'''






										lowercase_  							=			self.num_labels
										lowercase_  							=			DistilBertForSequenceClassification(UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()
										lowercase_  							=			model(UpperCamelCase__							,					attention_mask=UpperCamelCase__							,					labels=UpperCamelCase__       )
										self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.num_labels)       )

					def   UpperCAmelCase__       (     self       :   Union[str, Any]							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   Tuple							,					UpperCamelCase__       :   Union[str, Any]							,					UpperCamelCase__       :   Dict							,					UpperCamelCase__       :   List[str]							,					UpperCamelCase__       :   List[str]       ):






										'''simple docstring'''






										lowercase_  							=			self.num_labels
										lowercase_  							=			DistilBertForTokenClassification(config=UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()

										lowercase_  							=			model(UpperCamelCase__							,					attention_mask=UpperCamelCase__							,					labels=UpperCamelCase__       )
										self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.seq_length, self.num_labels)       )

					def   UpperCAmelCase__       (     self       :   int							,					UpperCamelCase__       :   Any							,					UpperCamelCase__       :   List[Any]							,					UpperCamelCase__       :   Optional[Any]							,					UpperCamelCase__       :   Optional[Any]							,					UpperCamelCase__       :   List[str]							,					UpperCamelCase__       :   List[Any]       ):






										'''simple docstring'''






										lowercase_  							=			self.num_choices
										lowercase_  							=			DistilBertForMultipleChoice(config=UpperCamelCase__       )
										model.to(UpperCamelCase__       )
										model.eval()
										lowercase_  							=			input_ids.unsqueeze(1       ).expand(-1							,					self.num_choices							,					-1       ).contiguous()
										lowercase_  							=			input_mask.unsqueeze(1       ).expand(-1							,					self.num_choices							,					-1       ).contiguous()
										lowercase_  							=			model(
										    UpperCamelCase__							,					attention_mask=UpperCamelCase__							,					labels=UpperCamelCase__							,					)
										self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.num_choices)       )







					def   UpperCAmelCase__       (     self       :   int       ):






										'''simple docstring'''






										lowercase_  							=			self.prepare_config_and_inputs()
										((lowercase_)							,					(lowercase_)							,					(lowercase_)							,					(lowercase_)							,					(lowercase_)							,					(lowercase_))  							=			config_and_inputs
										lowercase_  							=			{"""input_ids""": input_ids, """attention_mask""": input_mask}
										return config, inputs_dict







@require_torch
class        UpperCamelCase__   (	__magic_name__							,				__magic_name__							,				unittest.TestCase   ):
					__SCREAMING_SNAKE_CASE       :  int						=   (
					    (
					        DistilBertModel,
					        DistilBertForMaskedLM,
					        DistilBertForMultipleChoice,
					        DistilBertForQuestionAnswering,
					        DistilBertForSequenceClassification,
					        DistilBertForTokenClassification,
					    )
					    if is_torch_available()
					    else None
					)
					__SCREAMING_SNAKE_CASE       :  Dict						=   (
					    {
					        'feature-extraction': DistilBertModel,
					        'fill-mask': DistilBertForMaskedLM,
					        'question-answering': DistilBertForQuestionAnswering,
					        'text-classification': DistilBertForSequenceClassification,
					        'token-classification': DistilBertForTokenClassification,
					        'zero-shot': DistilBertForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					__SCREAMING_SNAKE_CASE       :  List[str]						=   True
					__SCREAMING_SNAKE_CASE       :  Dict						=   True
					__SCREAMING_SNAKE_CASE       :  Any						=   True
					__SCREAMING_SNAKE_CASE       :  str						=   True

					def   UpperCAmelCase__       (     self       :   Optional[Any]       ):






										'''simple docstring'''






										lowercase_  							=			DistilBertModelTester(self       )
										lowercase_  							=			ConfigTester(self							,					config_class=UpperCamelCase__							,					dim=37       )

					def   UpperCAmelCase__       (     self       :   List[str]       ):






										'''simple docstring'''






										self.config_tester.run_common_tests()

					def   UpperCAmelCase__       (     self       :   Union[str, Any]       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_model(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   Optional[Any]       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   Tuple       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   int       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   Union[str, Any]       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   Tuple       ):






										'''simple docstring'''






										lowercase_  							=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase__       )

					@slow
					def   UpperCAmelCase__       (     self       :   Optional[int]       ):






										'''simple docstring'''






										for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowercase_  							=			DistilBertModel.from_pretrained(UpperCamelCase__       )
															self.assertIsNotNone(UpperCamelCase__       )







					@slow
					@require_torch_gpu
					def   UpperCAmelCase__       (     self       :   List[Any]       ):






										'''simple docstring'''






										lowercase_							,					lowercase_  							=			self.model_tester.prepare_config_and_inputs_for_common()
										for model_class in self.all_model_classes:
															# BertForMultipleChoice behaves incorrectly in JIT environments.
															if model_class == DistilBertForMultipleChoice:
																				return

															lowercase_  							=			True
															lowercase_  							=			model_class(config=UpperCamelCase__       )

															lowercase_  							=			self._prepare_for_class(UpperCamelCase__							,					UpperCamelCase__       )
															lowercase_  							=			torch.jit.trace(
															    UpperCamelCase__							,					(inputs_dict["""input_ids"""].to("""cpu"""       ), inputs_dict["""attention_mask"""].to("""cpu"""       ))       )

															with tempfile.TemporaryDirectory() as tmp:
																				torch.jit.save(UpperCamelCase__							,					os.path.join(UpperCamelCase__							,					"""traced_model.pt"""       )       )
																				lowercase_  							=			torch.jit.load(os.path.join(UpperCamelCase__							,					"""traced_model.pt"""       )							,					map_location=UpperCamelCase__       )
																				loaded(inputs_dict["""input_ids"""].to(UpperCamelCase__       )							,					inputs_dict["""attention_mask"""].to(UpperCamelCase__       )       )







@require_torch
class        UpperCamelCase__   (	unittest.TestCase   ):

					@slow
					def   UpperCAmelCase__       (     self       :   str       ):






										'''simple docstring'''






										lowercase_  							=			DistilBertModel.from_pretrained("""distilbert-base-uncased"""       )
										lowercase_  							=			torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]       )
										lowercase_  							=			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
										with torch.no_grad():
															lowercase_  							=			model(UpperCamelCase__							,					attention_mask=UpperCamelCase__       )[0]
										lowercase_  							=			torch.Size((1, 11, 768)       )
										self.assertEqual(output.shape							,					UpperCamelCase__       )
										lowercase_  							=			torch.tensor(
										    [[[-0.1_639, 0.3_299, 0.1_648], [-0.1_746, 0.3_289, 0.1_710], [-0.1_884, 0.3_357, 0.1_810]]]       )

										self.assertTrue(torch.allclose(output[:, 1:4, 1:4]							,					UpperCamelCase__							,					atol=1e-4       )       )






from __future__ import annotations

import sys
from collections import deque
from typing import Generic, TypeVar

a					  =     TypeVar('T')







class        UpperCamelCase__   (	Generic[T]   ):
					__SCREAMING_SNAKE_CASE       :  deque[T]  # Cache store of keys
					__SCREAMING_SNAKE_CASE       :  set[T]  # References of the keys in cache
					__SCREAMING_SNAKE_CASE       :  int						=   10  # Maximum capacity of cache

					def __init__(     self       :   str							,					UpperCamelCase__       :   int       ):






										'''simple docstring'''






										lowercase_  							=			deque()
										lowercase_  							=			set()
										if not n:
															lowercase_  							=			sys.maxsize
										elif n < 0:
															raise ValueError("""n should be an integer greater than 0."""       )
										else:
															lowercase_  							=			n

					def   UpperCAmelCase__       (     self       :   Optional[int]							,					UpperCamelCase__       :   T       ):






										'''simple docstring'''






										if x not in self.key_reference:
															if len(self.dq_store       ) == LRUCache._MAX_CAPACITY:
																				lowercase_  							=			self.dq_store.pop()
																				self.key_reference.remove(UpperCamelCase__       )
										else:
															self.dq_store.remove(UpperCamelCase__       )

										self.dq_store.appendleft(UpperCamelCase__       )
										self.key_reference.add(UpperCamelCase__       )

					def   UpperCAmelCase__       (     self       :   List[Any]       ):






										'''simple docstring'''






										for k in self.dq_store:
															print(UpperCamelCase__       )







					def __repr__(     self       :   Optional[Any]       ):






										'''simple docstring'''






										return F'''LRUCache({self._MAX_CAPACITY}) => {list(self.dq_store       )}'''


if __name__ == "__main__":
							import doctest

							doctest.testmod()

							a					  =     LRUCache(4)
							lru_cache.refer('A')
							lru_cache.refer(2)
							lru_cache.refer(3)
							lru_cache.refer('A')
							lru_cache.refer(4)
							lru_cache.refer(5)
							lru_cache.display()

							print(lru_cache)
							assert str(lru_cache) == "LRUCache(4) => [5, 4, 'A', 3]"


