


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




       '''simple docstring'''



       def __init__(						self				:				Union[str, Any]		,				__a				:				Any		,				)					->List[Any]:
          lowerCamelCase_						:   List[Any]		= parent
          lowerCamelCase_						:   List[Any]		= 13
          lowerCamelCase_						:   Dict		= 7
          lowerCamelCase_						:   int		= True
          lowerCamelCase_						:   Dict		= True
          lowerCamelCase_						:   Optional[Any]		= False
          lowerCamelCase_						:   Union[str, Any]		= True
          lowerCamelCase_						:   str		= 99
          lowerCamelCase_						:   List[str]		= 32
          lowerCamelCase_						:   Union[str, Any]		= 2
          lowerCamelCase_						:   List[str]		= 4
          lowerCamelCase_						:   Union[str, Any]		= 37
          lowerCamelCase_						:   int		= """gelu"""
          lowerCamelCase_						:   Dict		= 0.1
          lowerCamelCase_						:   int		= 0.1
          lowerCamelCase_						:   Dict		= 512
          lowerCamelCase_						:   Any		= 16
          lowerCamelCase_						:   List[str]		= 2
          lowerCamelCase_						:   Tuple		= 0.02
          lowerCamelCase_						:   Tuple		= 3
          lowerCamelCase_						:   List[Any]		= 4
          lowerCamelCase_						:   List[str]		= None



       def 				_lowerCAmelCase							(						self				:				Dict							)					->Union[str, Any]:
          lowerCamelCase_						:   int		= ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size							)

          lowerCamelCase_						:   List[str]		= None
          if self.use_input_mask:
             lowerCamelCase_						:   List[str]		= random_attention_mask([self.batch_size, self.seq_length]							)

          lowerCamelCase_						:   Dict		= None
          lowerCamelCase_						:   Optional[Any]		= None
          lowerCamelCase_						:   Any		= None
          if self.use_labels:
             lowerCamelCase_						:   List[str]		= 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_						:   List[str]		= ids_tensor([self.batch_size]		,				self.num_choices							)

          lowerCamelCase_						:   Dict		= 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 				_lowerCAmelCase							(						self				:				Optional[int]		,				__a				:				int		,				__a				:				Tuple		,				__a				:				Optional[Any]		,				__a				:				Union[str, Any]		,				__a				:				List[Any]		,				__a				:				List[Any]							)					->int:
          lowerCamelCase_						:   Tuple		= TFDistilBertModel(config=__a							)
          lowerCamelCase_						:   Tuple		= {"""input_ids""": input_ids, """attention_mask""": input_mask}

          lowerCamelCase_						:   Optional[int]		= model(__a							)

          lowerCamelCase_						:   int		= [input_ids, input_mask]

          lowerCamelCase_						:   int		= model(__a							)

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



       def 				_lowerCAmelCase							(						self				:				Any		,				__a				:				str		,				__a				:				Tuple		,				__a				:				Union[str, Any]		,				__a				:				Tuple		,				__a				:				Dict		,				__a				:				Union[str, Any]							)					->Optional[int]:
          lowerCamelCase_						:   Dict		= TFDistilBertForMaskedLM(config=__a							)
          lowerCamelCase_						:   Union[str, Any]		= {"""input_ids""": input_ids, """attention_mask""": input_mask}
          lowerCamelCase_						:   Union[str, Any]		= model(__a							)
          self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.seq_length, self.vocab_size)							)



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



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



       def 				_lowerCAmelCase							(						self				:				Dict		,				__a				:				Union[str, Any]		,				__a				:				Any		,				__a				:				List[str]		,				__a				:				int		,				__a				:				str		,				__a				:				int							)					->str:
          lowerCamelCase_						:   Dict		= self.num_labels
          lowerCamelCase_						:   Optional[int]		= TFDistilBertForTokenClassification(__a							)
          lowerCamelCase_						:   List[Any]		= {"""input_ids""": input_ids, """attention_mask""": input_mask}
          lowerCamelCase_						:   str		= model(__a							)
          self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.seq_length, self.num_labels)							)




       def 				_lowerCAmelCase							(						self				:				Optional[Any]							)					->Optional[Any]:
          lowerCamelCase_						:   str		= self.prepare_config_and_inputs()
          ((lowerCamelCase_),	(lowerCamelCase_),	(lowerCamelCase_),	(lowerCamelCase_),	(lowerCamelCase_),	(lowerCamelCase_))						:   Dict		= config_and_inputs
          lowerCamelCase_						:   Optional[int]		= {"""input_ids""": input_ids, """attention_mask""": input_mask}
          return config, inputs_dict







@require_tf
class   SCREAMING_SNAKE_CASE_			(a__						,       a__						,       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 				_lowerCAmelCase							(						self				:				Dict							)					->List[str]:
          lowerCamelCase_						:   List[Any]		= TFDistilBertModelTester(self							)
          lowerCamelCase_						:   Dict		= ConfigTester(self		,				config_class=__a		,				dim=37							)



       def 				_lowerCAmelCase							(						self				:				Tuple							)					->Optional[Any]:
          self.config_tester.run_common_tests()



       def 				_lowerCAmelCase							(						self				:				List[Any]							)					->Optional[int]:
          lowerCamelCase_						:   int		= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_model(*__a							)



       def 				_lowerCAmelCase							(						self				:				Any							)					->Union[str, Any]:
          lowerCamelCase_						:   List[str]		= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_masked_lm(*__a							)



       def 				_lowerCAmelCase							(						self				:				Optional[Any]							)					->List[str]:
          lowerCamelCase_						:   List[str]		= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_question_answering(*__a							)



       def 				_lowerCAmelCase							(						self				:				Dict							)					->List[str]:
          lowerCamelCase_						:   List[str]		= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_sequence_classification(*__a							)



       def 				_lowerCAmelCase							(						self				:				Tuple							)					->List[str]:
          lowerCamelCase_						:   Optional[int]		= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_multiple_choice(*__a							)



       def 				_lowerCAmelCase							(						self				:				int							)					->Tuple:
          lowerCamelCase_						:   Optional[int]		= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_token_classification(*__a							)




       @slow
       def 				_lowerCAmelCase							(						self				:				Tuple							)					->Optional[Any]:
          for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]							):
             lowerCamelCase_						:   Optional[Any]		= TFDistilBertModel.from_pretrained(__a							)
             self.assertIsNotNone(__a							)







@require_tf
class   SCREAMING_SNAKE_CASE_			(unittest.TestCase						):




       '''simple docstring'''



       @slow
       def 				_lowerCAmelCase							(						self				:				int							)					->str:
          lowerCamelCase_						:   Union[str, Any]		= TFDistilBertModel.from_pretrained("""distilbert-base-uncased"""							)
          lowerCamelCase_						:   Any		= tf.constant([[0, 1, 2, 3, 4, 5]]							)
          lowerCamelCase_						:   Optional[Any]		= model(__a							)[0]

          lowerCamelCase_						:   Union[str, Any]		= [1, 6, 768]
          self.assertEqual(output.shape		,				__a							)

          lowerCamelCase_						:   Any		= tf.constant(
              [
                  [
                      [0.19_261_885, -0.13_732_955, 0.4_119_799],
                      [0.22_150_156, -0.07_422_661, 0.39_037_204],
                      [0.22_756_018, -0.0_896_414, 0.3_701_467],
                  ]
              ]							)
          tf.debugging.assert_near(output[:, :3, :3]		,				__a		,				atol=1e-4							)









def    __lowerCamelCase   (      A__						: int						,						A__						: int					)     ->    int:
   return int((input_a, input_a).count(0					) == 0					)


def    __lowerCamelCase   (      )     ->    None:
   assert and_gate(0						,						0					) == 0
   assert and_gate(0						,						1					) == 0
   assert and_gate(1						,						0					) == 0
   assert and_gate(1						,						1					) == 1


if __name__ == "__main__":
      test_and_gate()
      print(and_gate(1, 0))
      print(and_gate(0, 0))
      print(and_gate(0, 1))
      print(and_gate(1, 1))





