






'''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 							_UpperCAmelCase    (		snake_case      ):




    def __init__(      self	:							List[Any]	,  a	:							Union[str, Any]	,  a	:							Tuple=1_3	,  a	:							Union[str, Any]=7	,  a	:							Optional[Any]=True	,  a	:							Union[str, Any]=True	,  a	:							Optional[Any]=False	,  a	:							List[str]=True	,  a	:							Optional[int]=9_9	,  a	:							Dict=3_2	,  a	:							int=5	,  a	:							Any=4	,  a	:							List[str]=3_7	,  a	:							Union[str, Any]="gelu"	,  a	:							Tuple=0.1	,  a	:							Tuple=0.1	,  a	:							Tuple=5_1_2	,  a	:							Optional[Any]=1_6	,  a	:							int=2	,  a	:							int=0.02	,  a	:							List[Any]=3	,  a	:							Optional[Any]=4	,  a	:							Optional[Any]=None	,  ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  parent
       lowercase_						:     Dict							     =  batch_size
       lowercase_						:     List[str]							     =  seq_length
       lowercase_						:     Any							     =  is_training
       lowercase_						:     str							     =  use_input_mask
       lowercase_						:     Optional[int]							     =  use_token_type_ids
       lowercase_						:     Any							     =  use_labels
       lowercase_						:     str							     =  vocab_size
       lowercase_						:     List[Any]							     =  hidden_size
       lowercase_						:     Optional[int]							     =  num_hidden_layers
       lowercase_						:     Dict							     =  num_attention_heads
       lowercase_						:     List[str]							     =  intermediate_size
       lowercase_						:     List[str]							     =  hidden_act
       lowercase_						:     Tuple							     =  hidden_dropout_prob
       lowercase_						:     Tuple							     =  attention_probs_dropout_prob
       lowercase_						:     Dict							     =  max_position_embeddings
       lowercase_						:     Dict							     =  type_vocab_size
       lowercase_						:     str							     =  type_sequence_label_size
       lowercase_						:     Union[str, Any]							     =  initializer_range
       lowercase_						:     int							     =  num_labels
       lowercase_						:     str							     =  num_choices
       lowercase_						:     int							     =  scope




    def 					lowerCAmelCase__					(      self	:							str      ):






       '''simple docstring'''






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

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

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

       lowercase_						:     List[Any]							     =  self.get_config()

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




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''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 					lowerCAmelCase__					(      self	:							int	,  a	:							Tuple	,  a	:							List[Any]	,  a	:							Dict	,  a	:							Tuple	,  a	:							Any	,  a	:							Union[str, Any]      ):






       '''simple docstring'''






       lowercase_						:     int							     =  DistilBertModel(config=a      )
       model.to(a      )
       model.eval()
       lowercase_						:     List[str]							     =  model(a	,  a      )
       lowercase_						:     Optional[Any]							     =  model(a      )
       self.parent.assertEqual(result.last_hidden_state.shape	,  (self.batch_size, self.seq_length, self.hidden_size)      )




    def 					lowerCAmelCase__					(      self	:							Tuple	,  a	:							List[str]	,  a	:							Any	,  a	:							Dict	,  a	:							List[Any]	,  a	:							Optional[Any]	,  a	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  DistilBertForMaskedLM(config=a      )
       model.to(a      )
       model.eval()
       lowercase_						:     Union[str, Any]							     =  model(a	,  attention_mask=a	,  labels=a      )
       self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.seq_length, self.vocab_size)      )




    def 					lowerCAmelCase__					(      self	:							Tuple	,  a	:							Optional[Any]	,  a	:							Tuple	,  a	:							Optional[int]	,  a	:							Tuple	,  a	:							str	,  a	:							List[str]      ):






       '''simple docstring'''






       lowercase_						:     str							     =  DistilBertForQuestionAnswering(config=a      )
       model.to(a      )
       model.eval()
       lowercase_						:     List[str]							     =  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 					lowerCAmelCase__					(      self	:							List[str]	,  a	:							Tuple	,  a	:							Dict	,  a	:							str	,  a	:							List[str]	,  a	:							int	,  a	:							Union[str, Any]      ):






       '''simple docstring'''






       lowercase_						:     Dict							     =  self.num_labels
       lowercase_						:     str							     =  DistilBertForSequenceClassification(a      )
       model.to(a      )
       model.eval()
       lowercase_						:     Union[str, Any]							     =  model(a	,  attention_mask=a	,  labels=a      )
       self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.num_labels)      )




    def 					lowerCAmelCase__					(      self	:							str	,  a	:							int	,  a	:							Dict	,  a	:							int	,  a	:							Optional[int]	,  a	:							List[Any]	,  a	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     Any							     =  self.num_labels
       lowercase_						:     List[str]							     =  DistilBertForTokenClassification(config=a      )
       model.to(a      )
       model.eval()

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




    def 					lowerCAmelCase__					(      self	:							List[Any]	,  a	:							str	,  a	:							str	,  a	:							List[Any]	,  a	:							Tuple	,  a	:							Dict	,  a	:							List[str]      ):






       '''simple docstring'''






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





    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     str							     =  self.prepare_config_and_inputs()
       ((lowercase_)     ,		(lowercase_)     ,		(lowercase_)     ,		(lowercase_)     ,		(lowercase_)     ,		(lowercase_))						:     Union[str, Any]							     =  config_and_inputs
       lowercase_						:     Optional[Any]							     =  {"input_ids": input_ids, "attention_mask": input_mask}
       return config, inputs_dict


@require_torch
class 							_UpperCAmelCase    (		snake_case					,       snake_case					,       unittest.TestCase      ):
    __lowerCamelCase: Optional[Any]									=     (
        (
            DistilBertModel,
            DistilBertForMaskedLM,
            DistilBertForMultipleChoice,
            DistilBertForQuestionAnswering,
            DistilBertForSequenceClassification,
            DistilBertForTokenClassification,
        )
        if is_torch_available()
        else None
    )
    __lowerCamelCase: str									=     (
        {
            'feature-extraction': DistilBertModel,
            'fill-mask': DistilBertForMaskedLM,
            'question-answering': DistilBertForQuestionAnswering,
            'text-classification': DistilBertForSequenceClassification,
            'token-classification': DistilBertForTokenClassification,
            'zero-shot': DistilBertForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    __lowerCamelCase: Union[str, Any]									=     True
    __lowerCamelCase: int									=     True
    __lowerCamelCase: Tuple									=     True
    __lowerCamelCase: Tuple									=     True




    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  DistilBertModelTester(self      )
       lowercase_						:     Dict							     =  ConfigTester(self	,  config_class=a	,  dim=3_7      )




    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       self.config_tester.run_common_tests()




    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*a      )




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






       '''simple docstring'''






       lowercase_						:     Tuple							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*a      )




    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*a      )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     str							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*a      )




    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''simple docstring'''






       lowercase_						:     List[str]							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*a      )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*a      )




    @slow
    def 					lowerCAmelCase__					(      self	:							Union[str, Any]      ):






       '''simple docstring'''






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





    @slow
    @require_torch_gpu
    def 					lowerCAmelCase__					(      self	:							Union[str, Any]      ):






       '''simple docstring'''






       lowercase_     ,		lowercase_						:     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

          lowercase_						:     List[str]							     =  True
          lowercase_						:     str							     =  model_class(config=a      )

          lowercase_						:     Union[str, Any]							     =  self._prepare_for_class(a	,  a      )
          lowercase_						:     Tuple							     =  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"      )      )
             lowercase_						:     Union[str, Any]							     =  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 							_UpperCAmelCase    (		unittest.TestCase      ):




    @slow
    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     int							     =  DistilBertModel.from_pretrained("distilbert-base-uncased"      )
       lowercase_						:     List[Any]							     =  torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]      )
       lowercase_						:     Optional[int]							     =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
       with torch.no_grad():
          lowercase_						:     Dict							     =  model(a	,  attention_mask=a      )[0]
       lowercase_						:     Dict							     =  torch.Size((1, 1_1, 7_6_8)      )
       self.assertEqual(output.shape	,  a      )
       lowercase_						:     Union[str, Any]							     =  torch.tensor(
           [[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]      )

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












'''simple docstring'''
import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
 from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
 from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
 from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


UpperCamelCase__			=      DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

UpperCamelCase__			=      'main'
# Default branch name
UpperCamelCase__			=      'f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'
# One particular commit (not the top of `main`)
UpperCamelCase__			=      'aaaaaaa'
# This commit does not exist, so we should 404.

UpperCamelCase__			=      'd9e9f15bc825e4b2c9249e9578f884bbcb5e3684'
# Sha-1 of config.json on the top of `main`, for checking purposes
UpperCamelCase__			=      '4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'




@contextlib.contextmanager
def 		__SCREAMING_SNAKE_CASE       (	):


   """simple docstring"""




   print("Welcome!"				)
   yield
   print("Bye!"				)




@contextlib.contextmanager
def 		__SCREAMING_SNAKE_CASE       (	):


   """simple docstring"""




   print("Bonjour!"				)
   yield
   print("Au revoir!"				)


class 							_UpperCAmelCase    (		unittest.TestCase      ):




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       assert transformers.__spec__ is not None
       assert importlib.util.find_spec("transformers"      ) is not None


class 							_UpperCAmelCase    (		unittest.TestCase      ):




    @unittest.mock.patch("sys.stdout"	,  new_callable=io.StringIO      )
    def 					lowerCAmelCase__					(      self	:							str	,  a	:							List[str]      ):






       '''simple docstring'''






       with ContextManagers([]      ):
          print("Transformers are awesome!"      )
       # The print statement adds a new line at the end of the output
       self.assertEqual(mock_stdout.getvalue()	,  "Transformers are awesome!\n"      )




    @unittest.mock.patch("sys.stdout"	,  new_callable=io.StringIO      )
    def 					lowerCAmelCase__					(      self	:							List[str]	,  a	:							Optional[int]      ):






       '''simple docstring'''






       with ContextManagers([context_en()]      ):
          print("Transformers are awesome!"      )
       # The output should be wrapped with an English welcome and goodbye
       self.assertEqual(mock_stdout.getvalue()	,  "Welcome!\nTransformers are awesome!\nBye!\n"      )




    @unittest.mock.patch("sys.stdout"	,  new_callable=io.StringIO      )
    def 					lowerCAmelCase__					(      self	:							Tuple	,  a	:							List[Any]      ):






       '''simple docstring'''






       with ContextManagers([context_fr(), context_en()]      ):
          print("Transformers are awesome!"      )
       # The output should be wrapped with an English and French welcome and goodbye
       self.assertEqual(mock_stdout.getvalue()	,  "Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n"      )




    @require_torch
    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       self.assertEqual(find_labels(a      )	,  ["labels"]      )
       self.assertEqual(find_labels(a      )	,  ["labels", "next_sentence_label"]      )
       self.assertEqual(find_labels(a      )	,  ["start_positions", "end_positions"]      )


       class 							_UpperCAmelCase    (		snake_case      ):
           pass


       self.assertEqual(find_labels(a      )	,  ["labels"]      )




    @require_tf
    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       self.assertEqual(find_labels(a      )	,  ["labels"]      )
       self.assertEqual(find_labels(a      )	,  ["labels", "next_sentence_label"]      )
       self.assertEqual(find_labels(a      )	,  ["start_positions", "end_positions"]      )


       class 							_UpperCAmelCase    (		snake_case      ):
           pass


       self.assertEqual(find_labels(a      )	,  ["labels"]      )





    @require_flax
    def 					lowerCAmelCase__					(      self	:							str      ):






       '''simple docstring'''






       self.assertEqual(find_labels(a      )	,  []      )
       self.assertEqual(find_labels(a      )	,  []      )
       self.assertEqual(find_labels(a      )	,  []      )


       class 							_UpperCAmelCase    (		snake_case      ):
           pass


       self.assertEqual(find_labels(a      )	,  []      )




