

"""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 					_lowerCAmelCase  (	lowercase   ):







 """simple docstring"""







 def __init__(			self							:			Optional[int],					UpperCAmelCase__							:			Tuple,					UpperCAmelCase__							:			List[str]=1_3,					UpperCAmelCase__							:			Union[str, Any]=7,					UpperCAmelCase__							:			Optional[int]=True,					UpperCAmelCase__							:			List[str]=True,					UpperCAmelCase__							:			Optional[Any]=False,					UpperCAmelCase__							:			Union[str, Any]=True,					UpperCAmelCase__							:			Any=9_9,					UpperCAmelCase__							:			List[Any]=3_2,					UpperCAmelCase__							:			int=5,					UpperCAmelCase__							:			Dict=4,					UpperCAmelCase__							:			Dict=3_7,					UpperCAmelCase__							:			List[str]="gelu",					UpperCAmelCase__							:			Optional[int]=0.1,					UpperCAmelCase__							:			Optional[Any]=0.1,					UpperCAmelCase__							:			Union[str, Any]=5_1_2,					UpperCAmelCase__							:			int=1_6,					UpperCAmelCase__							:			int=2,					UpperCAmelCase__							:			Optional[Any]=0.02,					UpperCAmelCase__							:			Optional[int]=3,					UpperCAmelCase__							:			Tuple=4,					UpperCAmelCase__							:			List[str]=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 					_lowercase (			self							:			Any	):
    __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 					_lowercase (			self							:			str	):
    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 					_lowercase (			self							:			Dict,					UpperCAmelCase__							:			List[str],					UpperCAmelCase__							:			Union[str, Any],					UpperCAmelCase__							:			str,					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Optional[Any],					UpperCAmelCase__							:			str	):
    __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 					_lowercase (			self							:			Optional[int],					UpperCAmelCase__							:			Optional[Any],					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			Union[str, Any],					UpperCAmelCase__							:			List[str],					UpperCAmelCase__							:			List[Any],					UpperCAmelCase__							:			List[Any]	):
    __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 					_lowercase (			self							:			Optional[int],					UpperCAmelCase__							:			List[str],					UpperCAmelCase__							:			Optional[Any],					UpperCAmelCase__							:			Union[str, Any],					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			Optional[Any],					UpperCAmelCase__							:			Optional[Any]	):
    __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 					_lowercase (			self							:			Optional[Any],					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			Union[str, Any],					UpperCAmelCase__							:			Tuple,					UpperCAmelCase__							:			Tuple,					UpperCAmelCase__							:			Tuple,					UpperCAmelCase__							:			Tuple	):
    __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 					_lowercase (			self							:			Any,					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			Union[str, Any],					UpperCAmelCase__							:			int,					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			int	):
    __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 					_lowercase (			self							:			Optional[int],					UpperCAmelCase__							:			Tuple,					UpperCAmelCase__							:			List[Any],					UpperCAmelCase__							:			List[Any],					UpperCAmelCase__							:			List[str],					UpperCAmelCase__							:			Tuple,					UpperCAmelCase__							:			Any	):
    __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 					_lowercase (			self							:			Optional[Any]	):
    __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 					_lowerCAmelCase  (	lowercase     ,lowercase     ,unittest.TestCase   ):







 """simple docstring"""







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


 def 					_lowercase (			self							:			Tuple	):
    __lowercase								=						DistilBertModelTester(self	)
    __lowercase								=						ConfigTester(self,					config_class=UpperCAmelCase__,					dim=3_7	)


 def 					_lowercase (			self							:			Optional[int]	):
    self.config_tester.run_common_tests()


 def 					_lowercase (			self							:			List[str]	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_model(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			int	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			List[str]	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			str	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			str	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			Optional[Any]	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCAmelCase__	)


 @slow
 def 					_lowercase (			self							:			List[str]	):
    for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowercase								=						DistilBertModel.from_pretrained(UpperCAmelCase__	)
       self.assertIsNotNone(UpperCAmelCase__	)



 @slow
 @require_torch_gpu
 def 					_lowercase (			self							:			Any	):
    __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 					_lowerCAmelCase  (	unittest.TestCase   ):







 """simple docstring"""







 @slow
 def 					_lowercase (			self							:			List[str]	):
    __lowercase								=						DistilBertModel.from_pretrained("distilbert-base-uncased"	)
    __lowercase								=						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								=						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, 1_1, 7_6_8)	)
    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	)	)







import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import TaBlock, TaConfig, TaLayerNorm

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin





class  __lowerCAmelCase     (					SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE							):




  @register_to_config
  def __init__(				self							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase = False							,   )  -> Tuple:
        '''simple docstring'''






        super().__init__()

        _lowercase	       =nn.Embedding(lowerCAmelCase							,   lowerCAmelCase					)

        _lowercase	       =nn.Embedding(lowerCAmelCase							,   lowerCAmelCase					)
        _lowercase	       =False

        _lowercase	       =nn.Dropout(p=lowerCAmelCase					)

        _lowercase	       =TaConfig(
            vocab_size=lowerCAmelCase							,   d_model=lowerCAmelCase							,   num_heads=lowerCAmelCase							,   d_kv=lowerCAmelCase							,   d_ff=lowerCAmelCase							,   dropout_rate=lowerCAmelCase							,   feed_forward_proj=lowerCAmelCase							,   is_decoder=lowerCAmelCase							,   is_encoder_decoder=lowerCAmelCase							,   )

        _lowercase	       =nn.ModuleList()
        for lyr_num in range(lowerCAmelCase					):
              _lowercase	       =TaBlock(lowerCAmelCase					)
              self.encoders.append(lowerCAmelCase					)

        _lowercase	       =TaLayerNorm(lowerCAmelCase					)
        _lowercase	       =nn.Dropout(p=lowerCAmelCase					)




  def   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase					)  -> Dict:
        '''simple docstring'''






        _lowercase	       =self.token_embedder(lowerCAmelCase					)

        _lowercase	       =encoder_input_tokens.shape[1]
        _lowercase	       =torch.arange(lowerCAmelCase							,   device=encoder_input_tokens.device					)
        x += self.position_encoding(lowerCAmelCase					)

        _lowercase	       =self.dropout_pre(lowerCAmelCase					)

        # inverted the attention mask
        _lowercase	       =encoder_input_tokens.size()
        _lowercase	       =self.get_extended_attention_mask(lowerCAmelCase							,   lowerCAmelCase					)

        for lyr in self.encoders:
              _lowercase	       =lyr(lowerCAmelCase							,   lowerCAmelCase					)[0]
        _lowercase	       =self.layer_norm(lowerCAmelCase					)

        return self.dropout_post(lowerCAmelCase					), encoder_inputs_mask


