





"""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 		lowercase					:





      def __init__(       self      ,						lowercase      ,						)		->     Optional[int]:
            lowerCAmelCase  						=       parent
            lowerCAmelCase  						=       13
            lowerCAmelCase  						=       7
            lowerCAmelCase  						=       True
            lowerCAmelCase  						=       True
            lowerCAmelCase  						=       False
            lowerCAmelCase  						=       True
            lowerCAmelCase  						=       99
            lowerCAmelCase  						=       32
            lowerCAmelCase  						=       2
            lowerCAmelCase  						=       4
            lowerCAmelCase  						=       37
            lowerCAmelCase  						=       """gelu"""
            lowerCAmelCase  						=       0.1
            lowerCAmelCase  						=       0.1
            lowerCAmelCase  						=       512
            lowerCAmelCase  						=       16
            lowerCAmelCase  						=       2
            lowerCAmelCase  						=       0.02
            lowerCAmelCase  						=       3
            lowerCAmelCase  						=       4
            lowerCAmelCase  						=       None





      def     _snake_case			(       self )		->     str:
            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     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase )		->     List[Any]:
            lowerCAmelCase  						=       TFDistilBertModel(config=lowercase )
            lowerCAmelCase  						=       {"""input_ids""": input_ids, """attention_mask""": input_mask}

            lowerCAmelCase  						=       model(lowercase )

            lowerCAmelCase  						=       [input_ids, input_mask]

            lowerCAmelCase  						=       model(lowercase )

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





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase )		->     Optional[int]:
            lowerCAmelCase  						=       TFDistilBertForMaskedLM(config=lowercase )
            lowerCAmelCase  						=       {"""input_ids""": input_ids, """attention_mask""": input_mask}
            lowerCAmelCase  						=       model(lowercase )
            self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.seq_length, self.vocab_size) )





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase )		->     List[Any]:
            lowerCAmelCase  						=       TFDistilBertForQuestionAnswering(config=lowercase )
            lowerCAmelCase  						=       {
                """input_ids""": input_ids,
                """attention_mask""": input_mask,
            }
            lowerCAmelCase  						=       model(lowercase )
            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      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase )		->     List[Any]:
            lowerCAmelCase  						=       self.num_labels
            lowerCAmelCase  						=       TFDistilBertForSequenceClassification(lowercase )
            lowerCAmelCase  						=       {"""input_ids""": input_ids, """attention_mask""": input_mask}
            lowerCAmelCase  						=       model(lowercase )
            self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.num_labels) )





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase )		->     Any:
            lowerCAmelCase  						=       self.num_choices
            lowerCAmelCase  						=       TFDistilBertForMultipleChoice(lowercase )
            lowerCAmelCase  						=       tf.tile(tf.expand_dims(lowercase      ,						1 )      ,						(1, self.num_choices, 1) )
            lowerCAmelCase  						=       tf.tile(tf.expand_dims(lowercase      ,						1 )      ,						(1, self.num_choices, 1) )
            lowerCAmelCase  						=       {
                """input_ids""": multiple_choice_inputs_ids,
                """attention_mask""": multiple_choice_input_mask,
            }
            lowerCAmelCase  						=       model(lowercase )
            self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.num_choices) )





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase )		->     int:
            lowerCAmelCase  						=       self.num_labels
            lowerCAmelCase  						=       TFDistilBertForTokenClassification(lowercase )
            lowerCAmelCase  						=       {"""input_ids""": input_ids, """attention_mask""": input_mask}
            lowerCAmelCase  						=       model(lowercase )
            self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.seq_length, self.num_labels) )





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







@require_tf
class 		lowercase					(					_UpperCAmelCase     ,  _UpperCAmelCase     ,  unittest.TestCase					):
      _SCREAMING_SNAKE_CASE					=      (
          (
              TFDistilBertModel,
              TFDistilBertForMaskedLM,
              TFDistilBertForQuestionAnswering,
              TFDistilBertForSequenceClassification,
              TFDistilBertForTokenClassification,
              TFDistilBertForMultipleChoice,
          )
          if is_tf_available()
          else None
      )
      _SCREAMING_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 {}
      )
      _SCREAMING_SNAKE_CASE					=      False
      _SCREAMING_SNAKE_CASE					=      False





      def     _snake_case			(       self )		->     Dict:
            lowerCAmelCase  						=       TFDistilBertModelTester(self )
            lowerCAmelCase  						=       ConfigTester(self      ,						config_class=lowercase      ,						dim=37 )





      def     _snake_case			(       self )		->     str:
            self.config_tester.run_common_tests()





      def     _snake_case			(       self )		->     int:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_model(*lowercase )





      def     _snake_case			(       self )		->     Any:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_masked_lm(*lowercase )





      def     _snake_case			(       self )		->     Optional[Any]:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_question_answering(*lowercase )





      def     _snake_case			(       self )		->     Union[str, Any]:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowercase )





      def     _snake_case			(       self )		->     List[Any]:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowercase )





      def     _snake_case			(       self )		->     str:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_token_classification(*lowercase )





      @slow
      def     _snake_case			(       self )		->     List[str]:
            for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1] ):
                  lowerCAmelCase  						=       TFDistilBertModel.from_pretrained(lowercase )
                  self.assertIsNotNone(lowercase )







@require_tf
class 		lowercase					(					unittest.TestCase					):





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

            lowerCAmelCase  						=       [1, 6, 768]
            self.assertEqual(output.shape      ,						lowercase )

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








from __future__ import annotations

import time
from collections.abc import Sequence
from random import randint

from matplotlib import pyplot as plt






def        a_		(       __lowercase   :	Sequence[float]       ,					__lowercase   :	int       ,					__lowercase   :	int ) ->  tuple[int | None, int | None, float]:
			if not arr:
						return None, None, 0
			if low == high:
						return low, high, arr[low]

			_snake_case		=			(low + high) // 2
			_snake_case	,					_snake_case	,					_snake_case		=			max_subarray(__lowercase       ,					__lowercase       ,					__lowercase )
			_snake_case	,					_snake_case	,					_snake_case		=			max_subarray(__lowercase       ,					mid + 1       ,					__lowercase )
			_snake_case	,					_snake_case	,					_snake_case		=			max_cross_sum(__lowercase       ,					__lowercase       ,					__lowercase       ,					__lowercase )
			if left_sum >= right_sum and left_sum >= cross_sum:
						return left_low, left_high, left_sum
			elif right_sum >= left_sum and right_sum >= cross_sum:
						return right_low, right_high, right_sum
			return cross_left, cross_right, cross_sum






def        a_		(       __lowercase   :	Sequence[float]       ,					__lowercase   :	int       ,					__lowercase   :	int       ,					__lowercase   :	int ) ->  tuple[int, int, float]:
			_snake_case	,					_snake_case		=			float('-inf' ), -1
			_snake_case	,					_snake_case		=			float('-inf' ), -1

			_snake_case		=			0
			for i in range(__lowercase       ,					low - 1       ,					-1 ):
						summ += arr[i]
						if summ > left_sum:
									_snake_case		=			summ
									_snake_case		=			i

			_snake_case		=			0
			for i in range(mid + 1       ,					high + 1 ):
						summ += arr[i]
						if summ > right_sum:
									_snake_case		=			summ
									_snake_case		=			i

			return max_left, max_right, (left_sum + right_sum)






def        a_		(       __lowercase   :	int ) ->  float:
			_snake_case		=			[randint(1       ,					__lowercase ) for _ in range(__lowercase )]
			_snake_case		=			time.time()
			max_subarray(__lowercase       ,					0       ,					input_size - 1 )
			_snake_case		=			time.time()
			return end - start






def        a_		(       ) ->  None:
			_snake_case		=			[10, 100, 1_000, 10_000, 50_000, 100_000, 200_000, 300_000, 400_000, 500_000]
			_snake_case		=			[time_max_subarray(__lowercase ) for input_size in input_sizes]
			print('No of Inputs\t\tTime Taken' )
			for input_size, runtime in zip(__lowercase       ,					__lowercase ):
						print(__lowercase       ,					'\t\t'       ,					__lowercase )
			plt.plot(__lowercase       ,					__lowercase )
			plt.xlabel('Number of Inputs' )
			plt.ylabel('Time taken in seconds' )
			plt.show()


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

			testmod()