
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__		(	__SCREAMING_SNAKE_CASE):
      def __init__(      self    ,  __magic_name__    ,  __magic_name__=1_3    ,  __magic_name__=7    ,  __magic_name__=True    ,  __magic_name__=True    ,  __magic_name__=False    ,  __magic_name__=True    ,  __magic_name__=9_9    ,  __magic_name__=3_2    ,  __magic_name__=5    ,  __magic_name__=4    ,  __magic_name__=3_7    ,  __magic_name__="gelu"    ,  __magic_name__=0.1    ,  __magic_name__=0.1    ,  __magic_name__=5_1_2    ,  __magic_name__=1_6    ,  __magic_name__=2    ,  __magic_name__=0.02    ,  __magic_name__=3    ,  __magic_name__=4    ,  __magic_name__=None    ,  ):
       lowerCamelCase		:	Optional[int]							=      parent
       lowerCamelCase		:	Optional[Any]							=      batch_size
       lowerCamelCase		:	Optional[Any]							=      seq_length
       lowerCamelCase		:	Optional[Any]							=      is_training
       lowerCamelCase		:	Optional[int]							=      use_input_mask
       lowerCamelCase		:	str							=      use_token_type_ids
       lowerCamelCase		:	Any							=      use_labels
       lowerCamelCase		:	Optional[Any]							=      vocab_size
       lowerCamelCase		:	List[Any]							=      hidden_size
       lowerCamelCase		:	Optional[int]							=      num_hidden_layers
       lowerCamelCase		:	Union[str, Any]							=      num_attention_heads
       lowerCamelCase		:	Optional[Any]							=      intermediate_size
       lowerCamelCase		:	List[Any]							=      hidden_act
       lowerCamelCase		:	Union[str, Any]							=      hidden_dropout_prob
       lowerCamelCase		:	Any							=      attention_probs_dropout_prob
       lowerCamelCase		:	Tuple							=      max_position_embeddings
       lowerCamelCase		:	Union[str, Any]							=      type_vocab_size
       lowerCamelCase		:	List[str]							=      type_sequence_label_size
       lowerCamelCase		:	Optional[int]							=      initializer_range
       lowerCamelCase		:	str							=      num_labels
       lowerCamelCase		:	Union[str, Any]							=      num_choices
       lowerCamelCase		:	List[Any]							=      scope
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Tuple							=      ids_tensor([self.batch_size, self.seq_length]    ,  self.vocab_size      )

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

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

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

       return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
      def 						UpperCamelCase__							(      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 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__      ):
       lowerCamelCase		:	List[str]							=      DistilBertModel(config=__magic_name__      )
       model.to(__magic_name__      )
       model.eval()
       lowerCamelCase		:	str							=      model(__magic_name__    ,  __magic_name__      )
       lowerCamelCase		:	Optional[int]							=      model(__magic_name__      )
       self.parent.assertEqual(result.last_hidden_state.shape    ,  (self.batch_size, self.seq_length, self.hidden_size)      )
      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__      ):
       lowerCamelCase		:	List[Any]							=      DistilBertForMaskedLM(config=__magic_name__      )
       model.to(__magic_name__      )
       model.eval()
       lowerCamelCase		:	Tuple							=      model(__magic_name__    ,  attention_mask=__magic_name__    ,  labels=__magic_name__      )
       self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.seq_length, self.vocab_size)      )
      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__      ):
       lowerCamelCase		:	Union[str, Any]							=      DistilBertForQuestionAnswering(config=__magic_name__      )
       model.to(__magic_name__      )
       model.eval()
       lowerCamelCase		:	Any							=      model(
           __magic_name__    ,  attention_mask=__magic_name__    ,  start_positions=__magic_name__    ,  end_positions=__magic_name__      )
       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 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__      ):
       lowerCamelCase		:	List[Any]							=      self.num_labels
       lowerCamelCase		:	List[Any]							=      DistilBertForSequenceClassification(__magic_name__      )
       model.to(__magic_name__      )
       model.eval()
       lowerCamelCase		:	int							=      model(__magic_name__    ,  attention_mask=__magic_name__    ,  labels=__magic_name__      )
       self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.num_labels)      )
      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__      ):
       lowerCamelCase		:	Any							=      self.num_labels
       lowerCamelCase		:	Optional[Any]							=      DistilBertForTokenClassification(config=__magic_name__      )
       model.to(__magic_name__      )
       model.eval()

       lowerCamelCase		:	str							=      model(__magic_name__    ,  attention_mask=__magic_name__    ,  labels=__magic_name__      )
       self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.seq_length, self.num_labels)      )
      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__      ):
       lowerCamelCase		:	str							=      self.num_choices
       lowerCamelCase		:	List[str]							=      DistilBertForMultipleChoice(config=__magic_name__      )
       model.to(__magic_name__      )
       model.eval()
       lowerCamelCase		:	List[str]							=      input_ids.unsqueeze(1      ).expand(-1    ,  self.num_choices    ,  -1      ).contiguous()
       lowerCamelCase		:	Any							=      input_mask.unsqueeze(1      ).expand(-1    ,  self.num_choices    ,  -1      ).contiguous()
       lowerCamelCase		:	Optional[Any]							=      model(
           __magic_name__    ,  attention_mask=__magic_name__    ,  labels=__magic_name__    ,  )
       self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.num_choices)      )






      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	List[Any]							=      self.prepare_config_and_inputs()
       ((lowerCamelCase)			,    (lowerCamelCase)			,    (lowerCamelCase)			,    (lowerCamelCase)			,    (lowerCamelCase)			,    (lowerCamelCase))		:	Any							=      config_and_inputs
       lowerCamelCase		:	Union[str, Any]							=      {"""input_ids""": input_ids, """attention_mask""": input_mask}
       return config, inputs_dict





@require_torch
class 							A__		(	__SCREAMING_SNAKE_CASE  ,   __SCREAMING_SNAKE_CASE  ,   unittest.TestCase):
      _UpperCAmelCase				:    Any           =			(
          (
              DistilBertModel,
              DistilBertForMaskedLM,
              DistilBertForMultipleChoice,
              DistilBertForQuestionAnswering,
              DistilBertForSequenceClassification,
              DistilBertForTokenClassification,
          )
          if is_torch_available()
          else None
      )
      _UpperCAmelCase				:    List[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				:    Dict           =			True
      _UpperCAmelCase				:    str           =			True
      _UpperCAmelCase				:    str           =			True
      _UpperCAmelCase				:    Union[str, Any]           =			True
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	List[Any]							=      DistilBertModelTester(self      )
       lowerCamelCase		:	Optional[Any]							=      ConfigTester(self    ,  config_class=__magic_name__    ,  dim=3_7      )
      def 						UpperCamelCase__							(      self      ):
       self.config_tester.run_common_tests()
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Optional[Any]							=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*__magic_name__      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	int							=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*__magic_name__      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	List[str]							=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*__magic_name__      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Tuple							=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*__magic_name__      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Tuple							=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*__magic_name__      )
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Dict							=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*__magic_name__      )
      @slow
      def 						UpperCamelCase__							(      self      ):
       for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowerCamelCase		:	List[str]							=      DistilBertModel.from_pretrained(__magic_name__      )
        self.assertIsNotNone(__magic_name__      )






      @slow
      @require_torch_gpu
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase			,    lowerCamelCase		:	int							=      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		:	Any							=      True
        lowerCamelCase		:	List[Any]							=      model_class(config=__magic_name__      )

        lowerCamelCase		:	Any							=      self._prepare_for_class(__magic_name__    ,  __magic_name__      )
        lowerCamelCase		:	Optional[Any]							=      torch.jit.trace(
            __magic_name__    ,  (inputs_dict["""input_ids"""].to("""cpu"""      ), inputs_dict["""attention_mask"""].to("""cpu"""      ))      )

        with tempfile.TemporaryDirectory() as tmp:
         torch.jit.save(__magic_name__    ,  os.path.join(__magic_name__    ,  """traced_model.pt"""      )      )
         lowerCamelCase		:	List[str]							=      torch.jit.load(os.path.join(__magic_name__    ,  """traced_model.pt"""      )    ,  map_location=__magic_name__      )
         loaded(inputs_dict["""input_ids"""].to(__magic_name__      )    ,  inputs_dict["""attention_mask"""].to(__magic_name__      )      )





@require_torch
class 							A__		(	unittest.TestCase):
      @slow
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Optional[int]							=      DistilBertModel.from_pretrained("""distilbert-base-uncased"""      )
       lowerCamelCase		:	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]]      )
       lowerCamelCase		:	Union[str, Any]							=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
       with torch.no_grad():
        lowerCamelCase		:	List[str]							=      model(__magic_name__    ,  attention_mask=__magic_name__      )[0]
       lowerCamelCase		:	Any							=      torch.Size((1, 1_1, 7_6_8)      )
       self.assertEqual(output.shape    ,  __magic_name__      )
       lowerCamelCase		:	Tuple							=      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]    ,  __magic_name__    ,  atol=1e-4      )      )






from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
      import numpy as np
      import tensorflow as tf

      from transformers import TFCamembertModel





@require_tf
@require_sentencepiece
@require_tokenizers
class 							A__		(	unittest.TestCase):
      @slow
      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	Union[str, Any]							=      TFCamembertModel.from_pretrained("""jplu/tf-camembert-base"""      )

       lowerCamelCase		:	Any							=      tf.convert_to_tensor(
           [[5, 1_2_1, 1_1, 6_6_0, 1_6, 7_3_0, 2_5_5_4_3, 1_1_0, 8_3, 6]]    ,  dtype=tf.intaa    ,  )  # J'aime le camembert !"

       lowerCamelCase		:	str							=      model(__magic_name__      )["""last_hidden_state"""]
       lowerCamelCase		:	Union[str, Any]							=      tf.TensorShape((1, 1_0, 7_6_8)      )
       self.assertEqual(output.shape    ,  __magic_name__      )
       # compare the actual values for a slice.
       lowerCamelCase		:	Dict							=      tf.convert_to_tensor(
           [[[-0.0_254, 0.0_235, 0.1_027], [0.0_606, -0.1_811, -0.0_418], [-0.1_561, -0.1_127, 0.2_687]]]    ,  dtype=tf.floataa    ,  )
       # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
       # camembert.eval()
       # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

       self.assertTrue(np.allclose(output[:, :3, :3].numpy()    ,  expected_slice.numpy()    ,  atol=1e-4      )      )




