


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







       def __init__(			self: Tuple,A_: List[str],):






            '''simple docstring'''




            __UpperCamelCase =			parent
            __UpperCamelCase =			13
            __UpperCamelCase =			7
            __UpperCamelCase =			True
            __UpperCamelCase =			True
            __UpperCamelCase =			False
            __UpperCamelCase =			True
            __UpperCamelCase =			99
            __UpperCamelCase =			32
            __UpperCamelCase =			2
            __UpperCamelCase =			4
            __UpperCamelCase =			37
            __UpperCamelCase =			'''gelu'''
            __UpperCamelCase =			0.1
            __UpperCamelCase =			0.1
            __UpperCamelCase =			512
            __UpperCamelCase =			16
            __UpperCamelCase =			2
            __UpperCamelCase =			0.0_2
            __UpperCamelCase =			3
            __UpperCamelCase =			4
            __UpperCamelCase =			None







       def 		snake_case_	(			self: Tuple					):






            '''simple docstring'''




            __UpperCamelCase =			ids_tensor([self.batch_size, self.seq_length],self.vocab_size					)

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

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

            __UpperCamelCase =			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 		snake_case_	(			self: str,A_: Tuple,A_: str,A_: Any,A_: str,A_: Dict,A_: Dict					):






            '''simple docstring'''




            __UpperCamelCase =			TFDistilBertModel(config=__SCREAMING_SNAKE_CASE					)
            __UpperCamelCase =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}

            __UpperCamelCase =			model(__SCREAMING_SNAKE_CASE					)

            __UpperCamelCase =			[input_ids, input_mask]

            __UpperCamelCase =			model(__SCREAMING_SNAKE_CASE					)

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







       def 		snake_case_	(			self: Optional[int],A_: Tuple,A_: Tuple,A_: int,A_: Optional[int],A_: Optional[Any],A_: str					):






            '''simple docstring'''




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







       def 		snake_case_	(			self: Optional[int],A_: Optional[int],A_: int,A_: Optional[int],A_: Any,A_: List[Any],A_: int					):






            '''simple docstring'''




            __UpperCamelCase =			TFDistilBertForQuestionAnswering(config=__SCREAMING_SNAKE_CASE					)
            __UpperCamelCase =			{
                '''input_ids''': input_ids,
                '''attention_mask''': input_mask,
            }
            __UpperCamelCase =			model(__SCREAMING_SNAKE_CASE					)
            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 		snake_case_	(			self: Tuple,A_: Optional[Any],A_: str,A_: Dict,A_: Any,A_: int,A_: Union[str, Any]					):






            '''simple docstring'''




            __UpperCamelCase =			self.num_labels
            __UpperCamelCase =			TFDistilBertForSequenceClassification(__SCREAMING_SNAKE_CASE					)
            __UpperCamelCase =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
            __UpperCamelCase =			model(__SCREAMING_SNAKE_CASE					)
            self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels)					)







       def 		snake_case_	(			self: int,A_: int,A_: Dict,A_: Optional[int],A_: Optional[Any],A_: int,A_: Any					):






            '''simple docstring'''




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







       def 		snake_case_	(			self: str,A_: Optional[int],A_: List[Any],A_: Any,A_: str,A_: List[Any],A_: Tuple					):






            '''simple docstring'''




            __UpperCamelCase =			self.num_labels
            __UpperCamelCase =			TFDistilBertForTokenClassification(__SCREAMING_SNAKE_CASE					)
            __UpperCamelCase =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
            __UpperCamelCase =			model(__SCREAMING_SNAKE_CASE					)
            self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.num_labels)					)







       def 		snake_case_	(			self: Optional[int]					):






            '''simple docstring'''




            __UpperCamelCase =			self.prepare_config_and_inputs()
            (__UpperCamelCase) =			config_and_inputs
            __UpperCamelCase =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
            return config, inputs_dict



@require_tf
class 	__lowerCamelCase			(UpperCamelCase_				,      UpperCamelCase_				,      unittest.TestCase						):
       _lowercase											=     (
           (
               TFDistilBertModel,
               TFDistilBertForMaskedLM,
               TFDistilBertForQuestionAnswering,
               TFDistilBertForSequenceClassification,
               TFDistilBertForTokenClassification,
               TFDistilBertForMultipleChoice,
           )
           if is_tf_available()
           else None
       )
       _lowercase											=     (
           {
               """feature-extraction""": TFDistilBertModel,
               """fill-mask""": TFDistilBertForMaskedLM,
               """question-answering""": TFDistilBertForQuestionAnswering,
               """text-classification""": TFDistilBertForSequenceClassification,
               """token-classification""": TFDistilBertForTokenClassification,
               """zero-shot""": TFDistilBertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       _lowercase											=     False
       _lowercase											=     False







       def 		snake_case_	(			self: Any					):






            '''simple docstring'''




            __UpperCamelCase =			TFDistilBertModelTester(self					)
            __UpperCamelCase =			ConfigTester(self,config_class=__SCREAMING_SNAKE_CASE,dim=37					)







       def 		snake_case_	(			self: int					):






            '''simple docstring'''




            self.config_tester.run_common_tests()







       def 		snake_case_	(			self: Optional[int]					):






            '''simple docstring'''




            __UpperCamelCase =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_model(*__SCREAMING_SNAKE_CASE					)







       def 		snake_case_	(			self: Optional[int]					):






            '''simple docstring'''




            __UpperCamelCase =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_masked_lm(*__SCREAMING_SNAKE_CASE					)







       def 		snake_case_	(			self: str					):






            '''simple docstring'''




            __UpperCamelCase =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_question_answering(*__SCREAMING_SNAKE_CASE					)







       def 		snake_case_	(			self: Tuple					):






            '''simple docstring'''




            __UpperCamelCase =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_sequence_classification(*__SCREAMING_SNAKE_CASE					)







       def 		snake_case_	(			self: Optional[int]					):






            '''simple docstring'''




            __UpperCamelCase =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_multiple_choice(*__SCREAMING_SNAKE_CASE					)







       def 		snake_case_	(			self: Optional[Any]					):






            '''simple docstring'''




            __UpperCamelCase =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_token_classification(*__SCREAMING_SNAKE_CASE					)







       @slow
       def 		snake_case_	(			self: Tuple					):






            '''simple docstring'''




            for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]					):
                 __UpperCamelCase =			TFDistilBertModel.from_pretrained(__SCREAMING_SNAKE_CASE					)
                 self.assertIsNotNone(__SCREAMING_SNAKE_CASE					)



@require_tf
class 	__lowerCamelCase			(unittest.TestCase						):







       @slow
       def 		snake_case_	(			self: List[str]					):






            '''simple docstring'''




            __UpperCamelCase =			TFDistilBertModel.from_pretrained('distilbert-base-uncased'					)
            __UpperCamelCase =			tf.constant([[0, 1, 2, 3, 4, 5]]					)
            __UpperCamelCase =			model(__SCREAMING_SNAKE_CASE					)[0]

            __UpperCamelCase =			[1, 6, 768]
            self.assertEqual(output.shape,__SCREAMING_SNAKE_CASE					)

            __UpperCamelCase =			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],__SCREAMING_SNAKE_CASE,atol=1E-4					)




def 				SCREAMING_SNAKE_CASE_   (      UpperCamelCase__			,		UpperCamelCase__			,		UpperCamelCase__			,		UpperCamelCase__			,		UpperCamelCase__		):
      if index == number_of_items:
            return 0
      UpperCamelCase__     : str											=					0
      UpperCamelCase__     : Optional[Any]											=					0
      UpperCamelCase__     : int											=					knapsack(UpperCamelCase__			,		UpperCamelCase__			,		UpperCamelCase__			,		UpperCamelCase__			,		index + 1		)
      if weights[index] <= max_weight:
            UpperCamelCase__     : List[Any]											=					values[index] + knapsack(
                UpperCamelCase__			,		UpperCamelCase__			,		UpperCamelCase__			,		max_weight - weights[index]			,		index + 1		)
      return max(UpperCamelCase__			,		UpperCamelCase__		)


if __name__ == "__main__":
    import doctest

    doctest.testmod()


