






"""simple docstring"""



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        a     :







    def __init__(			self							,		_lowerCamelCase							,		):
      lowercase							    =       parent
      lowercase							    =       1_3
      lowercase							    =       7
      lowercase							    =       True
      lowercase							    =       True
      lowercase							    =       False
      lowercase							    =       True
      lowercase							    =       9_9
      lowercase							    =       3_2
      lowercase							    =       2
      lowercase							    =       4
      lowercase							    =       3_7
      lowercase							    =       'gelu'
      lowercase							    =       0.1
      lowercase							    =       0.1
      lowercase							    =       5_1_2
      lowercase							    =       1_6
      lowercase							    =       2
      lowercase							    =       0.0_2
      lowercase							    =       3
      lowercase							    =       4
      lowercase							    =       None







    def 			UpperCamelCase_      (			self       ):
      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							    =       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 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       TFDistilBertModel(config=_lowerCamelCase       )
      lowercase							    =       {'input_ids': input_ids, 'attention_mask': input_mask}

      lowercase							    =       model(_lowerCamelCase       )

      lowercase							    =       [input_ids, input_mask]

      lowercase							    =       model(_lowerCamelCase       )

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







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       TFDistilBertForMaskedLM(config=_lowerCamelCase       )
      lowercase							    =       {'input_ids': input_ids, 'attention_mask': input_mask}
      lowercase							    =       model(_lowerCamelCase       )
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.seq_length, self.vocab_size)       )







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       TFDistilBertForQuestionAnswering(config=_lowerCamelCase       )
      lowercase							    =       {
          'input_ids': input_ids,
          'attention_mask': input_mask,
      }
      lowercase							    =       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 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       self.num_labels
      lowercase							    =       TFDistilBertForSequenceClassification(_lowerCamelCase       )
      lowercase							    =       {'input_ids': input_ids, 'attention_mask': input_mask}
      lowercase							    =       model(_lowerCamelCase       )
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.num_labels)       )







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       self.num_choices
      lowercase							    =       TFDistilBertForMultipleChoice(_lowerCamelCase       )
      lowercase							    =       tf.tile(tf.expand_dims(_lowerCamelCase							,		1       )							,		(1, self.num_choices, 1)       )
      lowercase							    =       tf.tile(tf.expand_dims(_lowerCamelCase							,		1       )							,		(1, self.num_choices, 1)       )
      lowercase							    =       {
          'input_ids': multiple_choice_inputs_ids,
          'attention_mask': multiple_choice_input_mask,
      }
      lowercase							    =       model(_lowerCamelCase       )
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.num_choices)       )







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       self.num_labels
      lowercase							    =       TFDistilBertForTokenClassification(_lowerCamelCase       )
      lowercase							    =       {'input_ids': input_ids, 'attention_mask': input_mask}
      lowercase							    =       model(_lowerCamelCase       )
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.seq_length, self.num_labels)       )







    def 			UpperCamelCase_      (			self       ):
      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_tf
class        a     (   a_,				a_,				unittest.TestCase	):
    UpperCAmelCase_ :     Dict	     =(
        (
            TFDistilBertModel,
            TFDistilBertForMaskedLM,
            TFDistilBertForQuestionAnswering,
            TFDistilBertForSequenceClassification,
            TFDistilBertForTokenClassification,
            TFDistilBertForMultipleChoice,
        )
        if is_tf_available()
        else None
    )
    UpperCAmelCase_ :     Union[str, Any]	     =(
        {
            "feature-extraction": TFDistilBertModel,
            "fill-mask": TFDistilBertForMaskedLM,
            "question-answering": TFDistilBertForQuestionAnswering,
            "text-classification": TFDistilBertForSequenceClassification,
            "token-classification": TFDistilBertForTokenClassification,
            "zero-shot": TFDistilBertForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    UpperCAmelCase_ :     List[str]	     =False
    UpperCAmelCase_ :     Optional[int]	     =False







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       TFDistilBertModelTester(self       )
      lowercase							    =       ConfigTester(self							,		config_class=_lowerCamelCase							,		dim=3_7       )







    def 			UpperCamelCase_      (			self       ):
      self.config_tester.run_common_tests()







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_model(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_masked_lm(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_question_answering(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_sequence_classification(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_multiple_choice(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_token_classification(*_lowerCamelCase       )







    @slow
    def 			UpperCamelCase_      (			self       ):
      for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]       ):
        lowercase							    =       TFDistilBertModel.from_pretrained(_lowerCamelCase       )
        self.assertIsNotNone(_lowerCamelCase       )


@require_tf
class        a     (   unittest.TestCase	):







    @slow
    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       TFDistilBertModel.from_pretrained('distilbert-base-uncased'       )
      lowercase							    =       tf.constant([[0, 1, 2, 3, 4, 5]]       )
      lowercase							    =       model(_lowerCamelCase       )[0]

      lowercase							    =       [1, 6, 7_6_8]
      self.assertEqual(output.shape							,		_lowerCamelCase       )

      lowercase							    =       tf.constant(
          [
              [
                  [0.1_9_2_6_1_8_8_5, -0.1_3_7_3_2_9_5_5, 0.4_1_1_9_7_9_9],
                  [0.2_2_1_5_0_1_5_6, -0.0_7_4_2_2_6_6_1, 0.3_9_0_3_7_2_0_4],
                  [0.2_2_7_5_6_0_1_8, -0.0_8_9_6_4_1_4, 0.3_7_0_1_4_6_7],
              ]
          ]       )
      tf.debugging.assert_near(output[:, :3, :3]							,		_lowerCamelCase							,		atol=1e-4       )










"""simple docstring"""



from __future__ import annotations

from collections.abc import Iterator
from typing import Generic, TypeVar

_UpperCamelCase		:  Any						=		TypeVar('T')

class        a     (   Generic[T]	):







    def __init__(			self							,		_lowerCamelCase       ):
      lowercase							    =       data
      lowercase							    =       None







    def __str__(			self       ):
      return F'{self.data}'


class        a     (   Generic[T]	):







    def __init__(			self       ):
      lowercase							    =       None







    def __iter__(			self       ):
      lowercase							    =       self.top
      while node:
        yield node.data
        lowercase							    =       node.next







    def __str__(			self       ):
      return "->".join([str(_lowerCamelCase       ) for item in self]       )







    def __len__(			self       ):
      return len(tuple(iter(self       )       )       )







    def 			UpperCamelCase_      (			self       ):
      return self.top is None







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase       ):
      lowercase							    =       Node(_lowerCamelCase       )
      if not self.is_empty():
        lowercase							    =       self.top
      lowercase							    =       node







    def 			UpperCamelCase_      (			self       ):
      if self.is_empty():
        raise IndexError('pop from empty stack'       )
      assert isinstance(self.top							,		_lowerCamelCase       )
      lowercase							    =       self.top
      lowercase							    =       self.top.next
      return pop_node.data







    def 			UpperCamelCase_      (			self       ):
      if self.is_empty():
        raise IndexError('peek from empty stack'       )

      assert self.top is not None
      return self.top.data







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       None


if __name__ == "__main__":
    from doctest import testmod

    testmod()


