






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			:


      def __init__(	self					,						__UpperCamelCase					,						)->	Union[str, 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.0_2
           __lowerCAmelCase								=					3
           __lowerCAmelCase								=					4
           __lowerCAmelCase								=					None


      def  __UpperCAmelCase		(	self	)->	Dict:
           __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  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)->	Union[str, Any]:
           __lowerCAmelCase								=					TFDistilBertModel(config=__UpperCamelCase	)
           __lowerCAmelCase								=					{'''input_ids''': input_ids, '''attention_mask''': input_mask}

           __lowerCAmelCase								=					model(__UpperCamelCase	)

           __lowerCAmelCase								=					[input_ids, input_mask]

           __lowerCAmelCase								=					model(__UpperCamelCase	)

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


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)->	Tuple:
           __lowerCAmelCase								=					TFDistilBertForMaskedLM(config=__UpperCamelCase	)
           __lowerCAmelCase								=					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
           __lowerCAmelCase								=					model(__UpperCamelCase	)
           self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.vocab_size)	)


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


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


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)->	Dict:
           __lowerCAmelCase								=					self.num_labels
           __lowerCAmelCase								=					TFDistilBertForTokenClassification(__UpperCamelCase	)
           __lowerCAmelCase								=					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
           __lowerCAmelCase								=					model(__UpperCamelCase	)
           self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.num_labels)	)


      def  __UpperCAmelCase		(	self	)->	Dict:
           __lowerCAmelCase								=					self.prepare_config_and_inputs()
           (__lowerCAmelCase)								=					config_and_inputs
           __lowerCAmelCase								=					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
           return config, inputs_dict



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


      def  __UpperCAmelCase		(	self	)->	str:
           __lowerCAmelCase								=					TFDistilBertModelTester(self	)
           __lowerCAmelCase								=					ConfigTester(self					,						config_class=__UpperCamelCase					,						dim=3_7	)


      def  __UpperCAmelCase		(	self	)->	Dict:
           self.config_tester.run_common_tests()


      def  __UpperCAmelCase		(	self	)->	Tuple:
           __lowerCAmelCase								=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_model(*__UpperCamelCase	)


      def  __UpperCAmelCase		(	self	)->	Any:
           __lowerCAmelCase								=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_masked_lm(*__UpperCamelCase	)


      def  __UpperCAmelCase		(	self	)->	List[Any]:
           __lowerCAmelCase								=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_question_answering(*__UpperCamelCase	)


      def  __UpperCAmelCase		(	self	)->	str:
           __lowerCAmelCase								=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_sequence_classification(*__UpperCamelCase	)


      def  __UpperCAmelCase		(	self	)->	List[str]:
           __lowerCAmelCase								=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_multiple_choice(*__UpperCamelCase	)


      def  __UpperCAmelCase		(	self	)->	Union[str, Any]:
           __lowerCAmelCase								=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_token_classification(*__UpperCamelCase	)


      @slow
      def  __UpperCAmelCase		(	self	)->	Dict:

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



@require_tf
class 							_UpperCamelCase			(unittest.TestCase			):


      @slow
      def  __UpperCAmelCase		(	self	)->	List[Any]:
           __lowerCAmelCase								=					TFDistilBertModel.from_pretrained("distilbert-base-uncased"	)
           __lowerCAmelCase								=					tf.constant([[0, 1, 2, 3, 4, 5]]	)
           __lowerCAmelCase								=					model(__UpperCamelCase	)[0]

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

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














from __future__ import annotations





def       __lowerCAmelCase      (						__snake_case					, __snake_case					, __snake_case       ):
     __lowerCAmelCase								=					list(range(len(__snake_case       )       )       )
     __lowerCAmelCase								=					[v / w for v, w in zip(__snake_case					, __snake_case       )]
     index.sort(key=lambda __snake_case       : ratio[i]					, reverse=__snake_case       )

     __lowerCAmelCase								=					0
     __lowerCAmelCase								=					[0] * len(__snake_case       )
     for i in index:
          if weight[i] <= capacity:
               __lowerCAmelCase								=					1
               max_value += value[i]
               capacity -= weight[i]
          else:
               __lowerCAmelCase								=					capacity / weight[i]
               max_value += value[i] * capacity / weight[i]
               break

     return max_value, fractions


if __name__ == "__main__":
 import doctest

 doctest.testmod()






