
'''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__						(       lowercase_ ):



    def __init__(	self      :       List[str],__A      :       List[Any],__A      :       str=1_3,__A      :       List[Any]=7,__A      :       List[Any]=True,__A      :       Dict=True,__A      :       int=False,__A      :       List[str]=True,__A      :       Optional[int]=9_9,__A      :       Optional[Any]=3_2,__A      :       Any=5,__A      :       Union[str, Any]=4,__A      :       Optional[int]=3_7,__A      :       Union[str, Any]="gelu",__A      :       Any=0.1,__A      :       List[Any]=0.1,__A      :       Union[str, Any]=5_1_2,__A      :       str=1_6,__A      :       Optional[int]=2,__A      :       str=0.02,__A      :       Any=3,__A      :       Union[str, Any]=4,__A      :       List[Any]=None,):
          _lowerCamelCase				:   str								=   parent
          _lowerCamelCase				:   Union[str, Any]								=   batch_size
          _lowerCamelCase				:   str								=   seq_length
          _lowerCamelCase				:   int								=   is_training
          _lowerCamelCase				:   str								=   use_input_mask
          _lowerCamelCase				:   int								=   use_token_type_ids
          _lowerCamelCase				:   str								=   use_labels
          _lowerCamelCase				:   Optional[Any]								=   vocab_size
          _lowerCamelCase				:   Optional[int]								=   hidden_size
          _lowerCamelCase				:   Tuple								=   num_hidden_layers
          _lowerCamelCase				:   str								=   num_attention_heads
          _lowerCamelCase				:   Tuple								=   intermediate_size
          _lowerCamelCase				:   str								=   hidden_act
          _lowerCamelCase				:   List[Any]								=   hidden_dropout_prob
          _lowerCamelCase				:   Optional[Any]								=   attention_probs_dropout_prob
          _lowerCamelCase				:   Union[str, Any]								=   max_position_embeddings
          _lowerCamelCase				:   str								=   type_vocab_size
          _lowerCamelCase				:   Tuple								=   type_sequence_label_size
          _lowerCamelCase				:   Any								=   initializer_range
          _lowerCamelCase				:   Union[str, Any]								=   num_labels
          _lowerCamelCase				:   Tuple								=   num_choices
          _lowerCamelCase				:   Any								=   scope



    def   lowerCamelCase_  (	self      :       Optional[Any]     ):
          _lowerCamelCase				:   int								=   ids_tensor([self.batch_size, self.seq_length],self.vocab_size     )

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

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

          _lowerCamelCase				:   int								=   self.get_config()

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



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



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



    def   lowerCamelCase_  (	self      :       Optional[int],__A      :       int,__A      :       Tuple,__A      :       List[str],__A      :       Tuple,__A      :       List[Any],__A      :       Dict     ):
          _lowerCamelCase				:   Dict								=   self.num_labels
          _lowerCamelCase				:   int								=   DistilBertForTokenClassification(config=__A     )
          model.to(__A     )
          model.eval()

          _lowerCamelCase				:   List[str]								=   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      :       str,__A      :       str,__A      :       str,__A      :       str,__A      :       str,__A      :       Union[str, Any],__A      :       List[Any]     ):
          _lowerCamelCase				:   Any								=   self.num_choices
          _lowerCamelCase				:   int								=   DistilBertForMultipleChoice(config=__A     )
          model.to(__A     )
          model.eval()
          _lowerCamelCase				:   Optional[Any]								=   input_ids.unsqueeze(1     ).expand(-1,self.num_choices,-1     ).contiguous()
          _lowerCamelCase				:   List[str]								=   input_mask.unsqueeze(1     ).expand(-1,self.num_choices,-1     ).contiguous()
          _lowerCamelCase				:   Any								=   model(
              __A,attention_mask=__A,labels=__A,)
          self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_choices)     )



    def   lowerCamelCase_  (	self      :       List[Any]     ):
          _lowerCamelCase				:   Optional[int]								=   self.prepare_config_and_inputs()
          ((_lowerCamelCase)						,		(_lowerCamelCase)						,		(_lowerCamelCase)						,		(_lowerCamelCase)						,		(_lowerCamelCase)						,		(_lowerCamelCase))				:   List[str]								=   config_and_inputs
          _lowerCamelCase				:   Optional[int]								=   {"input_ids": input_ids, "attention_mask": input_mask}
          return config, inputs_dict


@require_torch
class  UpperCAmelCase__						(       lowercase_						,	lowercase_						,	unittest.TestCase ):
    lowerCAmelCase_				    =		(
        (
            DistilBertModel,
            DistilBertForMaskedLM,
            DistilBertForMultipleChoice,
            DistilBertForQuestionAnswering,
            DistilBertForSequenceClassification,
            DistilBertForTokenClassification,
        )
        if is_torch_available()
        else None
    )
    lowerCAmelCase_				    =		(
        {
            '''feature-extraction''': DistilBertModel,
            '''fill-mask''': DistilBertForMaskedLM,
            '''question-answering''': DistilBertForQuestionAnswering,
            '''text-classification''': DistilBertForSequenceClassification,
            '''token-classification''': DistilBertForTokenClassification,
            '''zero-shot''': DistilBertForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    lowerCAmelCase_				    =		True
    lowerCAmelCase_				    =		True
    lowerCAmelCase_				    =		True
    lowerCAmelCase_				    =		True



    def   lowerCamelCase_  (	self      :       str     ):
          _lowerCamelCase				:   Dict								=   DistilBertModelTester(self     )
          _lowerCamelCase				:   Optional[Any]								=   ConfigTester(self,config_class=__A,dim=3_7     )



    def   lowerCamelCase_  (	self      :       int     ):
          self.config_tester.run_common_tests()



    def   lowerCamelCase_  (	self      :       Optional[int]     ):
          _lowerCamelCase				:   int								=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_model(*__A     )



    def   lowerCamelCase_  (	self      :       Union[str, Any]     ):
          _lowerCamelCase				:   List[str]								=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_masked_lm(*__A     )



    def   lowerCamelCase_  (	self      :       Dict     ):
          _lowerCamelCase				:   Optional[Any]								=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_question_answering(*__A     )



    def   lowerCamelCase_  (	self      :       Tuple     ):
          _lowerCamelCase				:   List[str]								=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_sequence_classification(*__A     )



    def   lowerCamelCase_  (	self      :       int     ):
          _lowerCamelCase				:   int								=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_token_classification(*__A     )



    def   lowerCamelCase_  (	self      :       Tuple     ):
          _lowerCamelCase				:   Union[str, Any]								=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_multiple_choice(*__A     )



    @slow
    def   lowerCamelCase_  (	self      :       Dict     ):
          for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _lowerCamelCase				:   str								=   DistilBertModel.from_pretrained(__A     )
                self.assertIsNotNone(__A     )



    @slow
    @require_torch_gpu
    def   lowerCamelCase_  (	self      :       Any     ):
          _lowerCamelCase						,		_lowerCamelCase				:   Dict								=   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

                _lowerCamelCase				:   List[str]								=   True
                _lowerCamelCase				:   int								=   model_class(config=__A     )

                _lowerCamelCase				:   str								=   self._prepare_for_class(__A,__A     )
                _lowerCamelCase				:   Dict								=   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"     )     )
                      _lowerCamelCase				:   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      :       Optional[Any]     ):
          _lowerCamelCase				:   Any								=   DistilBertModel.from_pretrained("distilbert-base-uncased"     )
          _lowerCamelCase				:   Dict								=   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]]     )
          _lowerCamelCase				:   Optional[int]								=   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
          with torch.no_grad():
                _lowerCamelCase				:   Union[str, Any]								=   model(__A,attention_mask=__A     )[0]
          _lowerCamelCase				:   List[Any]								=   torch.Size((1, 1_1, 7_6_8)     )
          self.assertEqual(output.shape,__A     )
          _lowerCamelCase				:   Optional[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'''






from typing import TYPE_CHECKING

from ...utils import _LazyModule


UpperCAmelCase_       :   Tuple   =       {'tokenization_wav2vec2_phoneme': ['Wav2Vec2PhonemeCTCTokenizer']}


if TYPE_CHECKING:
 from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer
else:
 import sys

 UpperCAmelCase_       :   Tuple   =       _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)