
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 			A_  (     __lowerCamelCase  ):




	'''simple docstring'''


	def __init__(			self						,       snake_case						,       snake_case=13						,       snake_case=7						,       snake_case=True						,       snake_case=True						,       snake_case=False						,       snake_case=True						,       snake_case=99						,       snake_case=32						,       snake_case=5						,       snake_case=4						,       snake_case=37						,       snake_case="gelu"						,       snake_case=0.1						,       snake_case=0.1						,       snake_case=512						,       snake_case=16						,       snake_case=2						,       snake_case=0.02						,       snake_case=3						,       snake_case=4						,       snake_case=None						,       ):
		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       SCREAMING_SNAKE_CASE__		(			self			):
		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       SCREAMING_SNAKE_CASE__		(			self			):
		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       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case			):
		lowercase	    =       DistilBertModel(config=snake_case			)
		model.to(snake_case			)
		model.eval()
		lowercase	    =       model(snake_case						,       snake_case			)
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, self.seq_length, self.hidden_size)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case			):
		lowercase	    =       DistilBertForMaskedLM(config=snake_case			)
		model.to(snake_case			)
		model.eval()
		lowercase	    =       model(snake_case						,       attention_mask=snake_case						,       labels=snake_case			)
		self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.seq_length, self.vocab_size)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case			):
		lowercase	    =       DistilBertForQuestionAnswering(config=snake_case			)
		model.to(snake_case			)
		model.eval()
		lowercase	    =       model(
		    snake_case						,       attention_mask=snake_case						,       start_positions=snake_case						,       end_positions=snake_case			)
		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       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case			):
		lowercase	    =       self.num_labels
		lowercase	    =       DistilBertForSequenceClassification(snake_case			)
		model.to(snake_case			)
		model.eval()
		lowercase	    =       model(snake_case						,       attention_mask=snake_case						,       labels=snake_case			)
		self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.num_labels)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case			):
		lowercase	    =       self.num_labels
		lowercase	    =       DistilBertForTokenClassification(config=snake_case			)
		model.to(snake_case			)
		model.eval()

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





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case			):
		lowercase	    =       self.num_choices
		lowercase	    =       DistilBertForMultipleChoice(config=snake_case			)
		model.to(snake_case			)
		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(
		    snake_case						,       attention_mask=snake_case						,       labels=snake_case						,       )
		self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.num_choices)			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		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 			A_  (     __lowerCamelCase ,      __lowerCamelCase ,      unittest.TestCase  ):




	'''simple docstring'''


	_UpperCamelCase	:	Optional[int]									=			(
	    (
	        DistilBertModel,
	        DistilBertForMaskedLM,
	        DistilBertForMultipleChoice,
	        DistilBertForQuestionAnswering,
	        DistilBertForSequenceClassification,
	        DistilBertForTokenClassification,
	    )
	    if is_torch_available()
	    else None
	)
	_UpperCamelCase	:	str									=			(
	    {
	        """feature-extraction""": DistilBertModel,
	        """fill-mask""": DistilBertForMaskedLM,
	        """question-answering""": DistilBertForQuestionAnswering,
	        """text-classification""": DistilBertForSequenceClassification,
	        """token-classification""": DistilBertForTokenClassification,
	        """zero-shot""": DistilBertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	_UpperCamelCase	:	str									=			True
	_UpperCamelCase	:	List[Any]									=			True
	_UpperCamelCase	:	Optional[int]									=			True
	_UpperCamelCase	:	Tuple									=			True





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       DistilBertModelTester(self			)
		lowercase	    =       ConfigTester(self						,       config_class=snake_case						,       dim=37			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		self.config_tester.run_common_tests()





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_model(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_masked_lm(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_question_answering(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_sequence_classification(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_token_classification(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_multiple_choice(*snake_case			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			lowercase	    =       DistilBertModel.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)





	@slow
	@require_torch_gpu
	def       SCREAMING_SNAKE_CASE__		(			self			):
		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=snake_case			)

			lowercase	    =       self._prepare_for_class(snake_case						,       snake_case			)
			lowercase	    =       torch.jit.trace(
			    snake_case						,       (inputs_dict['input_ids'].to('cpu'			), inputs_dict['attention_mask'].to('cpu'			))			)

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







@require_torch
class 			A_  (     unittest.TestCase  ):




	'''simple docstring'''


	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       DistilBertModel.from_pretrained('distilbert-base-uncased'			)
		lowercase	    =       torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]			)
		lowercase	    =       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
		with torch.no_grad():
			lowercase	    =       model(snake_case						,       attention_mask=snake_case			)[0]
		lowercase	    =       torch.Size((1, 11, 768)			)
		self.assertEqual(output.shape						,       snake_case			)
		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]						,       snake_case						,       atol=1E-4			)			)



'''simple docstring'''

from math import factorial

snake_case									=    {str(digit): factorial(digit) for digit in range(10)}


def        UpperCAmelCase_ (  lowerCamelCase_			):







 """simple docstring"""


 if not isinstance(lowerCamelCase_      ,			lowerCamelCase_			):
  raise TypeError("Parameter number must be int"			)

 if number < 0:
  raise ValueError("Parameter number must be greater than or equal to 0"			)

 # Converts number in string to iterate on its digits and adds its factorial.
 return sum(DIGIT_FACTORIAL[digit] for digit in str(lowerCamelCase_			)			)


def        UpperCAmelCase_ (  lowerCamelCase_ = 6_0      ,			lowerCamelCase_ = 1_0_0_0_0_0_0			):







 """simple docstring"""


 if not isinstance(lowerCamelCase_      ,			lowerCamelCase_			) or not isinstance(lowerCamelCase_      ,			lowerCamelCase_			):
  raise TypeError("Parameters chain_length and number_limit must be int"			)

 if chain_length <= 0 or number_limit <= 0:
  raise ValueError(
      "Parameters chain_length and number_limit must be greater than 0"			)

 # the counter for the chains with the exact desired length
 lowerCAmelCase__     :   str     		=	0
 # the cached sizes of the previous chains
 lowerCAmelCase__     :   dict[int, int]     		=	{}

 for start_chain_element in range(1      ,			lowerCamelCase_			):
  # The temporary set will contain the elements of the chain
  lowerCAmelCase__     :   Any     		=	set()
  lowerCAmelCase__     :   int     		=	0

  # Stop computing the chain when you find a cached size, a repeating item or the
  # length is greater then the desired one.
  lowerCAmelCase__     :   Dict     		=	start_chain_element
  while (
      chain_element not in chain_sets_lengths
      and chain_element not in chain_set
      and chain_set_length <= chain_length
  ):
   chain_set.add(lowerCamelCase_			)
   chain_set_length += 1
   lowerCAmelCase__     :   Dict     		=	digit_factorial_sum(lowerCamelCase_			)

  if chain_element in chain_sets_lengths:
   chain_set_length += chain_sets_lengths[chain_element]

  lowerCAmelCase__     :   Optional[Any]     		=	chain_set_length

  # If chain contains the exact amount of elements increase the counter
  if chain_set_length == chain_length:
   chains_counter += 1

 return chains_counter


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     print(f'{solution()}')





