





from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers.models.distilbert.modeling_tf_distilbert import (
         TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
         TFDistilBertForMaskedLM,
         TFDistilBertForMultipleChoice,
         TFDistilBertForQuestionAnswering,
         TFDistilBertForSequenceClassification,
         TFDistilBertForTokenClassification,
         TFDistilBertModel,
     )



class 				_UpperCAmelCase    :

  """simple docstring"""


  def __init__(				self :		Optional[Any]					,  lowerCAmelCase_ :		int					,  )							->						List[Any]:
     __lowerCAmelCase								=	parent
     __lowerCAmelCase								=	1_3
     __lowerCAmelCase								=	7
     __lowerCAmelCase								=	True
     __lowerCAmelCase								=	True
     __lowerCAmelCase								=	False
     __lowerCAmelCase								=	True
     __lowerCAmelCase								=	9_9
     __lowerCAmelCase								=	3_2
     __lowerCAmelCase								=	2
     __lowerCAmelCase								=	4
     __lowerCAmelCase								=	3_7
     __lowerCAmelCase								=	'gelu'
     __lowerCAmelCase								=	0.1
     __lowerCAmelCase								=	0.1
     __lowerCAmelCase								=	5_1_2
     __lowerCAmelCase								=	1_6
     __lowerCAmelCase								=	2
     __lowerCAmelCase								=	0.02
     __lowerCAmelCase								=	3
     __lowerCAmelCase								=	4
     __lowerCAmelCase								=	None






  def 				lowercase		(				self :		int     )							->						Optional[int]:
     __lowerCAmelCase								=	ids_tensor([self.batch_size, self.seq_length]					,  self.vocab_size     )

     __lowerCAmelCase								=	None
     if self.use_input_mask:
        __lowerCAmelCase								=	random_attention_mask([self.batch_size, self.seq_length]     )

     __lowerCAmelCase								=	None
     __lowerCAmelCase								=	None
     __lowerCAmelCase								=	None
     if self.use_labels:
        __lowerCAmelCase								=	ids_tensor([self.batch_size]					,  self.type_sequence_label_size     )
        __lowerCAmelCase								=	ids_tensor([self.batch_size, self.seq_length]					,  self.num_labels     )
        __lowerCAmelCase								=	ids_tensor([self.batch_size]					,  self.num_choices     )

     __lowerCAmelCase								=	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					,  )

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






  def 				lowercase		(				self :		List[Any]					,  lowerCAmelCase_ :		List[str]					,  lowerCAmelCase_ :		Optional[int]					,  lowerCAmelCase_ :		Optional[Any]					,  lowerCAmelCase_ :		int					,  lowerCAmelCase_ :		Union[str, Any]					,  lowerCAmelCase_ :		Tuple     )							->						Union[str, Any]:
     __lowerCAmelCase								=	TFDistilBertModel(config=lowerCAmelCase_     )
     __lowerCAmelCase								=	{'input_ids': input_ids, 'attention_mask': input_mask}

     __lowerCAmelCase								=	model(lowerCAmelCase_     )

     __lowerCAmelCase								=	[input_ids, input_mask]

     __lowerCAmelCase								=	model(lowerCAmelCase_     )

     self.parent.assertEqual(result.last_hidden_state.shape					,  (self.batch_size, self.seq_length, self.hidden_size)     )






  def 				lowercase		(				self :		Any					,  lowerCAmelCase_ :		int					,  lowerCAmelCase_ :		int					,  lowerCAmelCase_ :		Optional[int]					,  lowerCAmelCase_ :		List[Any]					,  lowerCAmelCase_ :		Union[str, Any]					,  lowerCAmelCase_ :		Optional[int]     )							->						List[str]:
     __lowerCAmelCase								=	TFDistilBertForMaskedLM(config=lowerCAmelCase_     )
     __lowerCAmelCase								=	{'input_ids': input_ids, 'attention_mask': input_mask}
     __lowerCAmelCase								=	model(lowerCAmelCase_     )
     self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.vocab_size)     )






  def 				lowercase		(				self :		List[str]					,  lowerCAmelCase_ :		str					,  lowerCAmelCase_ :		Any					,  lowerCAmelCase_ :		Any					,  lowerCAmelCase_ :		Union[str, Any]					,  lowerCAmelCase_ :		Dict					,  lowerCAmelCase_ :		Any     )							->						Tuple:
     __lowerCAmelCase								=	TFDistilBertForQuestionAnswering(config=lowerCAmelCase_     )
     __lowerCAmelCase								=	{
         'input_ids': input_ids,
         'attention_mask': input_mask,
     }
     __lowerCAmelCase								=	model(lowerCAmelCase_     )
     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]					,  lowerCAmelCase_ :		Optional[Any]					,  lowerCAmelCase_ :		Union[str, Any]					,  lowerCAmelCase_ :		Any					,  lowerCAmelCase_ :		int					,  lowerCAmelCase_ :		Any					,  lowerCAmelCase_ :		int     )							->						Dict:
     __lowerCAmelCase								=	self.num_labels
     __lowerCAmelCase								=	TFDistilBertForSequenceClassification(lowerCAmelCase_     )
     __lowerCAmelCase								=	{'input_ids': input_ids, 'attention_mask': input_mask}
     __lowerCAmelCase								=	model(lowerCAmelCase_     )
     self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.num_labels)     )






  def 				lowercase		(				self :		List[str]					,  lowerCAmelCase_ :		Union[str, Any]					,  lowerCAmelCase_ :		Any					,  lowerCAmelCase_ :		Any					,  lowerCAmelCase_ :		List[str]					,  lowerCAmelCase_ :		Dict					,  lowerCAmelCase_ :		int     )							->						Dict:
     __lowerCAmelCase								=	self.num_choices
     __lowerCAmelCase								=	TFDistilBertForMultipleChoice(lowerCAmelCase_     )
     __lowerCAmelCase								=	tf.tile(tf.expand_dims(lowerCAmelCase_					,  1     )					,  (1, self.num_choices, 1)     )
     __lowerCAmelCase								=	tf.tile(tf.expand_dims(lowerCAmelCase_					,  1     )					,  (1, self.num_choices, 1)     )
     __lowerCAmelCase								=	{
         'input_ids': multiple_choice_inputs_ids,
         'attention_mask': multiple_choice_input_mask,
     }
     __lowerCAmelCase								=	model(lowerCAmelCase_     )
     self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.num_choices)     )






  def 				lowercase		(				self :		Union[str, Any]					,  lowerCAmelCase_ :		Optional[int]					,  lowerCAmelCase_ :		Tuple					,  lowerCAmelCase_ :		Union[str, Any]					,  lowerCAmelCase_ :		Tuple					,  lowerCAmelCase_ :		Any					,  lowerCAmelCase_ :		Optional[Any]     )							->						Optional[int]:
     __lowerCAmelCase								=	self.num_labels
     __lowerCAmelCase								=	TFDistilBertForTokenClassification(lowerCAmelCase_     )
     __lowerCAmelCase								=	{'input_ids': input_ids, 'attention_mask': input_mask}
     __lowerCAmelCase								=	model(lowerCAmelCase_     )
     self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.num_labels)     )






  def 				lowercase		(				self :		Optional[int]     )							->						str:
     __lowerCAmelCase								=	self.prepare_config_and_inputs()
     ((__lowerCAmelCase)	,	(__lowerCAmelCase)	,	(__lowerCAmelCase)	,	(__lowerCAmelCase)	,	(__lowerCAmelCase)	,	(__lowerCAmelCase))								=	config_and_inputs
     __lowerCAmelCase								=	{'input_ids': input_ids, 'attention_mask': input_mask}
     return config, inputs_dict





@require_tf
class 				_UpperCAmelCase    (    _UpperCamelCase		,    _UpperCamelCase		,    unittest.TestCase    ):

  """simple docstring"""


  a_        = (
      (
          TFDistilBertModel,
          TFDistilBertForMaskedLM,
          TFDistilBertForQuestionAnswering,
          TFDistilBertForSequenceClassification,
          TFDistilBertForTokenClassification,
          TFDistilBertForMultipleChoice,
      )
      if is_tf_available()
      else None
  )
  a_        = (
      {
          """feature-extraction""": TFDistilBertModel,
          """fill-mask""": TFDistilBertForMaskedLM,
          """question-answering""": TFDistilBertForQuestionAnswering,
          """text-classification""": TFDistilBertForSequenceClassification,
          """token-classification""": TFDistilBertForTokenClassification,
          """zero-shot""": TFDistilBertForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  a_        = False
  a_        = False






  def 				lowercase		(				self :		List[str]     )							->						Optional[int]:
     __lowerCAmelCase								=	TFDistilBertModelTester(self     )
     __lowerCAmelCase								=	ConfigTester(self					,  config_class=lowerCAmelCase_					,  dim=3_7     )






  def 				lowercase		(				self :		Tuple     )							->						Any:
     self.config_tester.run_common_tests()






  def 				lowercase		(				self :		Optional[int]     )							->						str:
     __lowerCAmelCase								=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_model(*lowerCAmelCase_     )






  def 				lowercase		(				self :		Any     )							->						Tuple:
     __lowerCAmelCase								=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCAmelCase_     )






  def 				lowercase		(				self :		int     )							->						str:
     __lowerCAmelCase								=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCAmelCase_     )






  def 				lowercase		(				self :		str     )							->						int:
     __lowerCAmelCase								=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCAmelCase_     )






  def 				lowercase		(				self :		List[str]     )							->						Union[str, Any]:
     __lowerCAmelCase								=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowerCAmelCase_     )






  def 				lowercase		(				self :		Dict     )							->						Optional[int]:
     __lowerCAmelCase								=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_token_classification(*lowerCAmelCase_     )






  @slow
  def 				lowercase		(				self :		List[str]     )							->						List[str]:
     for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]     ):
        __lowerCAmelCase								=	TFDistilBertModel.from_pretrained(lowerCAmelCase_     )
        self.assertIsNotNone(lowerCAmelCase_     )





@require_tf
class 				_UpperCAmelCase    (    unittest.TestCase    ):

  """simple docstring"""


  @slow
  def 				lowercase		(				self :		Optional[int]     )							->						List[Any]:
     __lowerCAmelCase								=	TFDistilBertModel.from_pretrained('distilbert-base-uncased'     )
     __lowerCAmelCase								=	tf.constant([[0, 1, 2, 3, 4, 5]]     )
     __lowerCAmelCase								=	model(lowerCAmelCase_     )[0]

     __lowerCAmelCase								=	[1, 6, 7_6_8]
     self.assertEqual(output.shape					,  lowerCAmelCase_     )

     __lowerCAmelCase								=	tf.constant(
         [
             [
                 [0.19_26_18_85, -0.13_73_29_55, 0.4_11_97_99],
                 [0.22_15_01_56, -0.07_42_26_61, 0.39_03_72_04],
                 [0.22_75_60_18, -0.0_89_64_14, 0.3_70_14_67],
             ]
         ]     )
     tf.debugging.assert_near(output[:, :3, :3]					,  lowerCAmelCase_					,  atol=1e-4     )








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


_snake_case  :    List[Any]						    =  {
    'configuration_biogpt': ['BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BioGptConfig'],
    'tokenization_biogpt': ['BioGptTokenizer'],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _snake_case  :    Optional[int]						    =  [
         'BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST',
         'BioGptForCausalLM',
         'BioGptForTokenClassification',
         'BioGptForSequenceClassification',
         'BioGptModel',
         'BioGptPreTrainedModel',
     ]


if TYPE_CHECKING:
     from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig
     from .tokenization_biogpt import BioGptTokenizer

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_biogpt import (
              BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST,
              BioGptForCausalLM,
              BioGptForSequenceClassification,
              BioGptForTokenClassification,
              BioGptModel,
              BioGptPreTrainedModel,
          )


else:
     import sys

     _snake_case  :    List[str]						    =  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

