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			:       Tuple					,	UpperCamelCase__			:       List[str]					,	):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					int           =							parent
												SCREAMING_SNAKE_CASE							:					Any           =							13
												SCREAMING_SNAKE_CASE							:					Any           =							7
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							True
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							True
												SCREAMING_SNAKE_CASE							:					Tuple           =							False
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							True
												SCREAMING_SNAKE_CASE							:					Dict           =							99
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							32
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							2
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							4
												SCREAMING_SNAKE_CASE							:					List[str]           =							37
												SCREAMING_SNAKE_CASE							:					str           =							'''gelu'''
												SCREAMING_SNAKE_CASE							:					Dict           =							0.1
												SCREAMING_SNAKE_CASE							:					List[Any]           =							0.1
												SCREAMING_SNAKE_CASE							:					List[str]           =							512
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							16
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							2
												SCREAMING_SNAKE_CASE							:					List[str]           =							0.02
												SCREAMING_SNAKE_CASE							:					Any           =							3
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							4
												SCREAMING_SNAKE_CASE							:					int           =							None


						def  __A ( self			:       List[str]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Tuple           =							ids_tensor([self.batch_size, self.seq_length]					,	self.vocab_size		)

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

												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							None
												SCREAMING_SNAKE_CASE							:					Any           =							None
												SCREAMING_SNAKE_CASE							:					str           =							None
												if self.use_labels:
																		SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							ids_tensor([self.batch_size]					,	self.type_sequence_label_size		)
																		SCREAMING_SNAKE_CASE							:					Tuple           =							ids_tensor([self.batch_size, self.seq_length]					,	self.num_labels		)
																		SCREAMING_SNAKE_CASE							:					List[Any]           =							ids_tensor([self.batch_size]					,	self.num_choices		)

												SCREAMING_SNAKE_CASE							:					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  __A ( self			:       Any					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       Any					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       Optional[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					int           =							TFDistilBertModel(config=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					int           =							{'''input_ids''': input_ids, '''attention_mask''': input_mask}

												SCREAMING_SNAKE_CASE							:					List[str]           =							model(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					str           =							[input_ids, input_mask]

												SCREAMING_SNAKE_CASE							:					Tuple           =							model(UpperCamelCase__		)

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


						def  __A ( self			:       List[Any]					,	UpperCamelCase__			:       Any					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       Optional[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[str]           =							TFDistilBertForMaskedLM(config=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					int           =							{'''input_ids''': input_ids, '''attention_mask''': input_mask}
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							model(UpperCamelCase__		)
												self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.seq_length, self.vocab_size)		)


						def  __A ( self			:       List[Any]					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       Optional[int]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							TFDistilBertForQuestionAnswering(config=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					int           =							{
												    '''input_ids''': input_ids,
												    '''attention_mask''': input_mask,
												}
												SCREAMING_SNAKE_CASE							:					List[str]           =							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  __A ( self			:       str					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       Dict		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Dict           =							self.num_labels
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							TFDistilBertForSequenceClassification(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					str           =							{'''input_ids''': input_ids, '''attention_mask''': input_mask}
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							model(UpperCamelCase__		)
												self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.num_labels)		)


						def  __A ( self			:       int					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       Any					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Dict		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							self.num_choices
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							TFDistilBertForMultipleChoice(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							tf.tile(tf.expand_dims(UpperCamelCase__					,	1		)					,	(1, self.num_choices, 1)		)
												SCREAMING_SNAKE_CASE							:					List[Any]           =							tf.tile(tf.expand_dims(UpperCamelCase__					,	1		)					,	(1, self.num_choices, 1)		)
												SCREAMING_SNAKE_CASE							:					Tuple           =							{
												    '''input_ids''': multiple_choice_inputs_ids,
												    '''attention_mask''': multiple_choice_input_mask,
												}
												SCREAMING_SNAKE_CASE							:					Tuple           =							model(UpperCamelCase__		)
												self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.num_choices)		)


						def  __A ( self			:       List[Any]					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       Union[str, Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Dict           =							self.num_labels
												SCREAMING_SNAKE_CASE							:					Tuple           =							TFDistilBertForTokenClassification(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							{'''input_ids''': input_ids, '''attention_mask''': input_mask}
												SCREAMING_SNAKE_CASE							:					str           =							model(UpperCamelCase__		)
												self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.seq_length, self.num_labels)		)



						def  __A ( self			:       List[str]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					int           =							self.prepare_config_and_inputs()
												(SCREAMING_SNAKE_CASE)							:					Union[str, Any]           =							config_and_inputs
												SCREAMING_SNAKE_CASE							:					Tuple           =							{'''input_ids''': input_ids, '''attention_mask''': input_mask}
												return config, inputs_dict






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


						def  __A ( self			:       Dict		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							TFDistilBertModelTester(self		)
												SCREAMING_SNAKE_CASE							:					int           =							ConfigTester(self					,	config_class=UpperCamelCase__					,	dim=37		)


						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												self.config_tester.run_common_tests()


						def  __A ( self			:       Optional[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_model(*UpperCamelCase__		)


						def  __A ( self			:       Tuple		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase__		)


						def  __A ( self			:       Optional[int]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Any           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase__		)


						def  __A ( self			:       Any		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase__		)


						def  __A ( self			:       Optional[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase__		)


						def  __A ( self			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase__		)



						@slow
						def  __A ( self			:       List[Any]		):





												'''simple docstring'''





												for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]		):
																		SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							TFDistilBertModel.from_pretrained(UpperCamelCase__		)
																		self.assertIsNotNone(UpperCamelCase__		)






@require_tf
class       lowercase__		(     unittest.TestCase):


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





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''		)
												SCREAMING_SNAKE_CASE							:					Tuple           =							tf.constant([[0, 1, 2, 3, 4, 5]]		)
												SCREAMING_SNAKE_CASE							:					List[Any]           =							model(UpperCamelCase__		)[0]

												SCREAMING_SNAKE_CASE							:					str           =							[1, 6, 768]
												self.assertEqual(output.shape					,	UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							tf.constant(
												    [
												        [
												            [0.1926_1885, -0.1373_2955, 0.411_9799],
												            [0.2215_0156, -0.0742_2661, 0.3903_7204],
												            [0.2275_6018, -0.089_6414, 0.370_1467],
												        ]
												    ]		)
												tf.debugging.assert_near(output[:, :3, :3]					,	UpperCamelCase__					,	atol=1E-4		)

import math
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils import SchedulerMixin, SchedulerOutput





class       lowercase__		(     UpperCamelCase_			,							UpperCamelCase_):
						UpperCamelCase_  =	1


						@register_to_config
						def __init__( self			:       List[str]					,	UpperCamelCase__			:       int = 1000					,	UpperCamelCase__			:       Optional[Union[np.ndarray, List[float]]] = None		):





												'''simple docstring'''





												self.set_timesteps(UpperCamelCase__		)

												# standard deviation of the initial noise distribution
												SCREAMING_SNAKE_CASE							:					str           =							1.0

												# For now we only support F-PNDM, i.e. the runge-kutta method
												# For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf
												# mainly at formula (9), (12), (13) and the Algorithm 2.
												SCREAMING_SNAKE_CASE							:					Tuple           =							4

												# running values
												SCREAMING_SNAKE_CASE							:					int           =							[]


						def  __A ( self			:       Dict					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Union[str, torch.device] = None		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[str]           =							num_inference_steps
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							torch.linspace(1					,	0					,	num_inference_steps + 1		)[:-1]
												SCREAMING_SNAKE_CASE							:					Tuple           =							torch.cat([steps, torch.tensor([0.0]		)]		)

												if self.config.trained_betas is not None:
																		SCREAMING_SNAKE_CASE							:					int           =							torch.tensor(self.config.trained_betas					,	dtype=torch.floataa		)
												else:
																		SCREAMING_SNAKE_CASE							:					Optional[Any]           =							torch.sin(steps * math.pi / 2		) ** 2

												SCREAMING_SNAKE_CASE							:					Dict           =							(1.0 - self.betas**2) ** 0.5

												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							(torch.atana(self.betas					,	self.alphas		) / math.pi * 2)[:-1]
												SCREAMING_SNAKE_CASE							:					List[str]           =							timesteps.to(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Any           =							[]


						def  __A ( self			:       Tuple					,	UpperCamelCase__			:       torch.FloatTensor					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       torch.FloatTensor					,	UpperCamelCase__			:       bool = True					,	):





												'''simple docstring'''





												if self.num_inference_steps is None:
																		raise ValueError(
																		    '''Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler'''		)

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							(self.timesteps == timestep).nonzero().item()
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							timestep_index + 1

												SCREAMING_SNAKE_CASE							:					int           =							sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index]
												self.ets.append(UpperCamelCase__		)

												if len(self.ets		) == 1:
																		SCREAMING_SNAKE_CASE							:					Dict           =							self.ets[-1]
												elif len(self.ets		) == 2:
																		SCREAMING_SNAKE_CASE							:					Optional[int]           =							(3 * self.ets[-1] - self.ets[-2]) / 2
												elif len(self.ets		) == 3:
																		SCREAMING_SNAKE_CASE							:					str           =							(23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12
												else:
																		SCREAMING_SNAKE_CASE							:					Optional[Any]           =							(1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4])

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							self._get_prev_sample(UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__		)

												if not return_dict:
																		return (prev_sample,)

												return SchedulerOutput(prev_sample=UpperCamelCase__		)


						def  __A ( self			:       Optional[Any]					,	UpperCamelCase__			:       torch.FloatTensor					,	*UpperCamelCase__			:       List[str]					,	**UpperCamelCase__			:       Optional[Any]		):





												'''simple docstring'''





												return sample


						def  __A ( self			:       Union[str, Any]					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       Any					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       Dict		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Any           =							self.alphas[timestep_index]
												SCREAMING_SNAKE_CASE							:					List[str]           =							self.betas[timestep_index]

												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							self.alphas[prev_timestep_index]
												SCREAMING_SNAKE_CASE							:					Tuple           =							self.betas[prev_timestep_index]

												SCREAMING_SNAKE_CASE							:					Dict           =							(sample - sigma * ets) / max(UpperCamelCase__					,	1E-8		)
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							next_alpha * pred + ets * next_sigma

												return prev_sample



						def __len__( self			:       int		):





												'''simple docstring'''





												return self.config.num_train_timesteps





