


'''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      ,				__magic_name__      ,				)					->							Dict:
      _a			      =							parent
      _a			      =							13
      _a			      =							7
      _a			      =							True
      _a			      =							True
      _a			      =							False
      _a			      =							True
      _a			      =							99
      _a			      =							32
      _a			      =							2
      _a			      =							4
      _a			      =							37
      _a			      =							'gelu'
      _a			      =							0.1
      _a			      =							0.1
      _a			      =							5_12
      _a			      =							16
      _a			      =							2
      _a			      =							0.0_2
      _a			      =							3
      _a			      =							4
      _a			      =							None
 def 							__UpperCAmelCase		( self						)					->							Optional[Any]:
      _a			      =							ids_tensor([self.batch_size, self.seq_length]      ,				self.vocab_size						)

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

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

      _a			      =							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      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__						)					->							Any:
      _a			      =							TFDistilBertModel(config=__magic_name__						)
      _a			      =							{'input_ids': input_ids, 'attention_mask': input_mask}

      _a			      =							model(__magic_name__						)

      _a			      =							[input_ids, input_mask]

      _a			      =							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__						)					->							Optional[int]:
      _a			      =							TFDistilBertForMaskedLM(config=__magic_name__						)
      _a			      =							{'input_ids': input_ids, 'attention_mask': input_mask}
      _a			      =							model(__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__						)					->							str:
      _a			      =							TFDistilBertForQuestionAnswering(config=__magic_name__						)
      _a			      =							{
          'input_ids': input_ids,
          'attention_mask': input_mask,
      }
      _a			      =							model(__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__						)					->							Optional[int]:
      _a			      =							self.num_labels
      _a			      =							TFDistilBertForSequenceClassification(__magic_name__						)
      _a			      =							{'input_ids': input_ids, 'attention_mask': input_mask}
      _a			      =							model(__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__						)					->							str:
      _a			      =							self.num_choices
      _a			      =							TFDistilBertForMultipleChoice(__magic_name__						)
      _a			      =							tf.tile(tf.expand_dims(__magic_name__      ,				1						)      ,				(1, self.num_choices, 1)						)
      _a			      =							tf.tile(tf.expand_dims(__magic_name__      ,				1						)      ,				(1, self.num_choices, 1)						)
      _a			      =							{
          'input_ids': multiple_choice_inputs_ids,
          'attention_mask': multiple_choice_input_mask,
      }
      _a			      =							model(__magic_name__						)
      self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.num_choices)						)
 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__						)					->							Tuple:
      _a			      =							self.num_labels
      _a			      =							TFDistilBertForTokenClassification(__magic_name__						)
      _a			      =							{'input_ids': input_ids, 'attention_mask': input_mask}
      _a			      =							model(__magic_name__						)
      self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.seq_length, self.num_labels)						)




 def 							__UpperCAmelCase		( self						)					->							Optional[Any]:
      _a			      =							self.prepare_config_and_inputs()
      ((_a)      ,   (_a)      ,   (_a)      ,   (_a)      ,   (_a)      ,   (_a))			      =							config_and_inputs
      _a			      =							{'input_ids': input_ids, 'attention_mask': input_mask}
      return config, inputs_dict





@require_tf
class  a     (     _SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				unittest.TestCase       ):
 _lowerCAmelCase 					=	(
     (
         TFDistilBertModel,
         TFDistilBertForMaskedLM,
         TFDistilBertForQuestionAnswering,
         TFDistilBertForSequenceClassification,
         TFDistilBertForTokenClassification,
         TFDistilBertForMultipleChoice,
     )
     if is_tf_available()
     else None
 )
 _lowerCAmelCase 					=	(
     {
         """feature-extraction""": TFDistilBertModel,
         """fill-mask""": TFDistilBertForMaskedLM,
         """question-answering""": TFDistilBertForQuestionAnswering,
         """text-classification""": TFDistilBertForSequenceClassification,
         """token-classification""": TFDistilBertForTokenClassification,
         """zero-shot""": TFDistilBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 _lowerCAmelCase 					=	False
 _lowerCAmelCase 					=	False
 def 							__UpperCAmelCase		( self						)					->							Dict:
      _a			      =							TFDistilBertModelTester(self						)
      _a			      =							ConfigTester(self      ,				config_class=__magic_name__      ,				dim=37						)
 def 							__UpperCAmelCase		( self						)					->							List[str]:
      self.config_tester.run_common_tests()
 def 							__UpperCAmelCase		( self						)					->							Dict:
      _a			      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_model(*__magic_name__						)
 def 							__UpperCAmelCase		( self						)					->							Any:
      _a			      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_masked_lm(*__magic_name__						)
 def 							__UpperCAmelCase		( self						)					->							Dict:
      _a			      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_question_answering(*__magic_name__						)
 def 							__UpperCAmelCase		( self						)					->							List[str]:
      _a			      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_sequence_classification(*__magic_name__						)
 def 							__UpperCAmelCase		( self						)					->							Optional[int]:
      _a			      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_multiple_choice(*__magic_name__						)
 def 							__UpperCAmelCase		( self						)					->							Union[str, Any]:
      _a			      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_token_classification(*__magic_name__						)




 @slow
 def 							__UpperCAmelCase		( self						)					->							Any:
      for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]						):
           _a			      =							TFDistilBertModel.from_pretrained(__magic_name__						)
           self.assertIsNotNone(__magic_name__						)





@require_tf
class  a     (     unittest.TestCase       ):
 @slow
 def 							__UpperCAmelCase		( self						)					->							Dict:
      _a			      =							TFDistilBertModel.from_pretrained('distilbert-base-uncased'						)
      _a			      =							tf.constant([[0, 1, 2, 3, 4, 5]]						)
      _a			      =							model(__magic_name__						)[0]

      _a			      =							[1, 6, 7_68]
      self.assertEqual(output.shape      ,				__magic_name__						)

      _a			      =							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]      ,				__magic_name__      ,				atol=1e-4						)





'''simple docstring'''






class  a     :
 def __init__( self      ,				__magic_name__						)					->							Optional[int]:
      _a			      =							n
      _a			      =							[None] * self.n
      _a			      =							0  # index of the first element
      _a			      =							0
      _a			      =							0
 def __len__( self						)					->							int:
      return self.size
 def 							__UpperCAmelCase		( self						)					->							bool:
      return self.size == 0
 def 							__UpperCAmelCase		( self						)					->							Optional[Any]:
      return False if self.is_empty() else self.array[self.front]
 def 							__UpperCAmelCase		( self      ,				__magic_name__						)					->							Optional[Any]:
      if self.size >= self.n:
           raise Exception('QUEUE IS FULL'						)

      _a			      =							data
      _a			      =							(self.rear + 1) % self.n
      self.size += 1
      return self




 def 							__UpperCAmelCase		( self						)					->							Any:
      if self.size == 0:
           raise Exception('UNDERFLOW'						)

      _a			      =							self.array[self.front]
      _a			      =							None
      _a			      =							(self.front + 1) % self.n
      self.size -= 1
      return temp

