





"""simple docstring"""


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      __SCREAMING_SNAKE_CASE    (						_UpperCamelCase   ):







  '''simple docstring'''


  def __init__(		self   :       List[str]					,   __a   :       str					,   __a   :       Union[str, Any]=13					,   __a   :       int=7					,   __a   :       Optional[int]=True					,   __a   :       List[str]=True					,   __a   :       List[str]=False					,   __a   :       Optional[int]=True					,   __a   :       List[Any]=99					,   __a   :       Union[str, Any]=32					,   __a   :       Optional[int]=5					,   __a   :       Union[str, Any]=4					,   __a   :       List[str]=37					,   __a   :       Optional[int]="gelu"					,   __a   :       Any=0.1					,   __a   :       List[Any]=0.1					,   __a   :       Union[str, Any]=512					,   __a   :       Tuple=16					,   __a   :       Union[str, Any]=2					,   __a   :       List[Any]=0.02					,   __a   :       Tuple=3					,   __a   :       Dict=4					,   __a   :       List[Any]=None					,   )		->				Any:
        _UpperCamelCase      :	List[Any]						=       parent
        _UpperCamelCase      :	Tuple						=       batch_size
        _UpperCamelCase      :	Optional[int]						=       seq_length
        _UpperCamelCase      :	Optional[Any]						=       is_training
        _UpperCamelCase      :	int						=       use_input_mask
        _UpperCamelCase      :	Any						=       use_token_type_ids
        _UpperCamelCase      :	Any						=       use_labels
        _UpperCamelCase      :	Any						=       vocab_size
        _UpperCamelCase      :	Optional[Any]						=       hidden_size
        _UpperCamelCase      :	List[Any]						=       num_hidden_layers
        _UpperCamelCase      :	Any						=       num_attention_heads
        _UpperCamelCase      :	List[str]						=       intermediate_size
        _UpperCamelCase      :	Any						=       hidden_act
        _UpperCamelCase      :	Dict						=       hidden_dropout_prob
        _UpperCamelCase      :	str						=       attention_probs_dropout_prob
        _UpperCamelCase      :	List[str]						=       max_position_embeddings
        _UpperCamelCase      :	Optional[int]						=       type_vocab_size
        _UpperCamelCase      :	Dict						=       type_sequence_label_size
        _UpperCamelCase      :	Optional[Any]						=       initializer_range
        _UpperCamelCase      :	Union[str, Any]						=       num_labels
        _UpperCamelCase      :	Optional[Any]						=       num_choices
        _UpperCamelCase      :	int						=       scope

  def    __SCREAMING_SNAKE_CASE				(		self   :       List[str]	)		->				Tuple:
        _UpperCamelCase      :	List[Any]						=       ids_tensor([self.batch_size, self.seq_length]					,   self.vocab_size	)

        _UpperCamelCase      :	Union[str, Any]						=       None
        if self.use_input_mask:
              _UpperCamelCase      :	Optional[Any]						=       random_attention_mask([self.batch_size, self.seq_length]	)

        _UpperCamelCase      :	Any						=       None
        _UpperCamelCase      :	List[Any]						=       None
        _UpperCamelCase      :	Tuple						=       None
        if self.use_labels:
              _UpperCamelCase      :	int						=       ids_tensor([self.batch_size]					,   self.type_sequence_label_size	)
              _UpperCamelCase      :	Optional[Any]						=       ids_tensor([self.batch_size, self.seq_length]					,   self.num_labels	)
              _UpperCamelCase      :	List[str]						=       ids_tensor([self.batch_size]					,   self.num_choices	)

        _UpperCamelCase      :	Optional[int]						=       self.get_config()

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

  def    __SCREAMING_SNAKE_CASE				(		self   :       Optional[int]	)		->				int:
        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   :       Union[str, Any]					,   __a   :       Any					,   __a   :       str					,   __a   :       int					,   __a   :       List[Any]					,   __a   :       int					,   __a   :       Any	)		->				str:
        _UpperCamelCase      :	Any						=       DistilBertModel(config=__a	)
        model.to(__a	)
        model.eval()
        _UpperCamelCase      :	Dict						=       model(__a					,   __a	)
        _UpperCamelCase      :	str						=       model(__a	)
        self.parent.assertEqual(result.last_hidden_state.shape					,   (self.batch_size, self.seq_length, self.hidden_size)	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       Tuple					,   __a   :       int					,   __a   :       Optional[Any]					,   __a   :       List[Any]					,   __a   :       List[str]					,   __a   :       Optional[Any]					,   __a   :       Any	)		->				int:
        _UpperCamelCase      :	List[Any]						=       DistilBertForMaskedLM(config=__a	)
        model.to(__a	)
        model.eval()
        _UpperCamelCase      :	Optional[Any]						=       model(__a					,   attention_mask=__a					,   labels=__a	)
        self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.vocab_size)	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       Optional[int]					,   __a   :       Tuple					,   __a   :       Tuple					,   __a   :       int					,   __a   :       Dict					,   __a   :       Tuple					,   __a   :       Optional[Any]	)		->				Optional[Any]:
        _UpperCamelCase      :	Union[str, Any]						=       DistilBertForQuestionAnswering(config=__a	)
        model.to(__a	)
        model.eval()
        _UpperCamelCase      :	Union[str, Any]						=       model(
            __a					,   attention_mask=__a					,   start_positions=__a					,   end_positions=__a	)
        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   :       Union[str, Any]					,   __a   :       int					,   __a   :       Any					,   __a   :       Optional[int]					,   __a   :       Optional[int]					,   __a   :       int					,   __a   :       List[str]	)		->				Optional[int]:
        _UpperCamelCase      :	str						=       self.num_labels
        _UpperCamelCase      :	List[str]						=       DistilBertForSequenceClassification(__a	)
        model.to(__a	)
        model.eval()
        _UpperCamelCase      :	List[Any]						=       model(__a					,   attention_mask=__a					,   labels=__a	)
        self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.num_labels)	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       Union[str, Any]					,   __a   :       Optional[int]					,   __a   :       Any					,   __a   :       str					,   __a   :       Optional[Any]					,   __a   :       Tuple					,   __a   :       Dict	)		->				str:
        _UpperCamelCase      :	Optional[int]						=       self.num_labels
        _UpperCamelCase      :	Dict						=       DistilBertForTokenClassification(config=__a	)
        model.to(__a	)
        model.eval()

        _UpperCamelCase      :	Optional[int]						=       model(__a					,   attention_mask=__a					,   labels=__a	)
        self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.num_labels)	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       Dict					,   __a   :       List[Any]					,   __a   :       int					,   __a   :       Dict					,   __a   :       str					,   __a   :       Union[str, Any]					,   __a   :       Union[str, Any]	)		->				Optional[int]:
        _UpperCamelCase      :	Tuple						=       self.num_choices
        _UpperCamelCase      :	List[Any]						=       DistilBertForMultipleChoice(config=__a	)
        model.to(__a	)
        model.eval()
        _UpperCamelCase      :	Tuple						=       input_ids.unsqueeze(1	).expand(-1					,   self.num_choices					,   -1	).contiguous()
        _UpperCamelCase      :	str						=       input_mask.unsqueeze(1	).expand(-1					,   self.num_choices					,   -1	).contiguous()
        _UpperCamelCase      :	int						=       model(
            __a					,   attention_mask=__a					,   labels=__a					,   )
        self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.num_choices)	)


  def    __SCREAMING_SNAKE_CASE				(		self   :       Union[str, Any]	)		->				Union[str, Any]:
        _UpperCamelCase      :	Any						=       self.prepare_config_and_inputs()
        ((_UpperCamelCase),  (_UpperCamelCase),  (_UpperCamelCase),  (_UpperCamelCase),  (_UpperCamelCase),  (_UpperCamelCase))      :	List[str]						=       config_and_inputs
        _UpperCamelCase      :	Tuple						=       {"input_ids": input_ids, "attention_mask": input_mask}
        return config, inputs_dict





@require_torch
class      __SCREAMING_SNAKE_CASE    (						_UpperCamelCase		,  _UpperCamelCase		,  unittest.TestCase   ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__							:Any	     =					(
      (
          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__							:Optional[int]	     =					True
  SCREAMING_SNAKE_CASE__							:Optional[Any]	     =					True
  SCREAMING_SNAKE_CASE__							:str	     =					True

  def    __SCREAMING_SNAKE_CASE				(		self   :       Any	)		->				Tuple:
        _UpperCamelCase      :	Tuple						=       DistilBertModelTester(self	)
        _UpperCamelCase      :	Optional[Any]						=       ConfigTester(self					,   config_class=__a					,   dim=37	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       Optional[Any]	)		->				Dict:
        self.config_tester.run_common_tests()

  def    __SCREAMING_SNAKE_CASE				(		self   :       List[str]	)		->				Tuple:
        _UpperCamelCase      :	str						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_model(*__a	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       List[Any]	)		->				Optional[int]:
        _UpperCamelCase      :	Optional[Any]						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_masked_lm(*__a	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       str	)		->				List[Any]:
        _UpperCamelCase      :	List[Any]						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_question_answering(*__a	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       List[str]	)		->				List[str]:
        _UpperCamelCase      :	List[str]						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_sequence_classification(*__a	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       List[str]	)		->				Any:
        _UpperCamelCase      :	Dict						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_token_classification(*__a	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       Tuple	)		->				Dict:
        _UpperCamelCase      :	Optional[Any]						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_multiple_choice(*__a	)

  @slow
  def    __SCREAMING_SNAKE_CASE				(		self   :       str	)		->				str:
        for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _UpperCamelCase      :	Optional[int]						=       DistilBertModel.from_pretrained(__a	)
              self.assertIsNotNone(__a	)


  @slow
  @require_torch_gpu
  def    __SCREAMING_SNAKE_CASE				(		self   :       str	)		->				Tuple:
        _UpperCamelCase,  _UpperCamelCase      :	str						=       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

              _UpperCamelCase      :	Tuple						=       True
              _UpperCamelCase      :	Tuple						=       model_class(config=__a	)

              _UpperCamelCase      :	int						=       self._prepare_for_class(__a					,   __a	)
              _UpperCamelCase      :	str						=       torch.jit.trace(
                  __a					,   (inputs_dict["input_ids"].to("cpu"	), inputs_dict["attention_mask"].to("cpu"	))	)

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





@require_torch
class      __SCREAMING_SNAKE_CASE    (						unittest.TestCase   ):







  '''simple docstring'''


  @slow
  def    __SCREAMING_SNAKE_CASE				(		self   :       str	)		->				Union[str, Any]:
        _UpperCamelCase      :	List[Any]						=       DistilBertModel.from_pretrained("distilbert-base-uncased"	)
        _UpperCamelCase      :	int						=       torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]	)
        _UpperCamelCase      :	Any						=       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
        with torch.no_grad():
              _UpperCamelCase      :	Any						=       model(__a					,   attention_mask=__a	)[0]
        _UpperCamelCase      :	str						=       torch.Size((1, 11, 768)	)
        self.assertEqual(output.shape					,   __a	)
        _UpperCamelCase      :	Optional[int]						=       torch.tensor(
            [[[-0.16_39, 0.32_99, 0.16_48], [-0.17_46, 0.32_89, 0.17_10], [-0.18_84, 0.33_57, 0.18_10]]]	)

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







"""simple docstring"""


from collections import defaultdict
from math import ceil, sqrt




def 					lowercase__    (					lowercase_ = 1_000_000		,lowercase_ = 10			)					->		int:




      """simple docstring"""

      _UpperCamelCase      :	defaultdict						=       defaultdict(lowercase_			)

      for outer_width in range(3		,(t_limit // 4) + 2			):
            if outer_width * outer_width > t_limit:
                  _UpperCamelCase      :	Any						=       max(
                      ceil(sqrt(outer_width * outer_width - t_limit			)			)		,1			)
            else:
                  _UpperCamelCase      :	str						=       1

            hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2

            for hole_width in range(lowercase_		,outer_width - 1		,2			):
                  count[outer_width * outer_width - hole_width * hole_width] += 1

      return sum(1 for n in count.values() if 1 <= n <= 10			)


if __name__ == "__main__":
  print(f"""{solution() = }""")
