


'''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__    :





							'''simple docstring'''


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






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= ids_tensor([self.batch_size, self.seq_length]       ,   self.vocab_size    )

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

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

											_SCREAMING_SNAKE_CASE					:			List[Any]							= 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       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case    ):
											_SCREAMING_SNAKE_CASE					:			Any							= TFDistilBertModel(config=_SCREAMING_SNAKE_CASE    )
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= {"""input_ids""": input_ids, """attention_mask""": input_mask}

											_SCREAMING_SNAKE_CASE					:			Optional[int]							= model(_SCREAMING_SNAKE_CASE    )

											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= [input_ids, input_mask]

											_SCREAMING_SNAKE_CASE					:			int							= model(_SCREAMING_SNAKE_CASE    )

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






							def UpperCAmelCase_      (       self       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case    ):
											_SCREAMING_SNAKE_CASE					:			Any							= TFDistilBertForMaskedLM(config=_SCREAMING_SNAKE_CASE    )
											_SCREAMING_SNAKE_CASE					:			Any							= {"""input_ids""": input_ids, """attention_mask""": input_mask}
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= model(_SCREAMING_SNAKE_CASE    )
											self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.vocab_size)    )






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






							def UpperCAmelCase_      (       self       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case    ):
											_SCREAMING_SNAKE_CASE					:			str							= self.num_choices
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= TFDistilBertForMultipleChoice(_SCREAMING_SNAKE_CASE    )
											_SCREAMING_SNAKE_CASE					:			Any							= tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE       ,   1    )       ,   (1, self.num_choices, 1)    )
											_SCREAMING_SNAKE_CASE					:			List[Any]							= tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE       ,   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					:			Dict							= model(_SCREAMING_SNAKE_CASE    )
											self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_choices)    )






							def UpperCAmelCase_      (       self       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case    ):
											_SCREAMING_SNAKE_CASE					:			Dict							= self.num_labels
											_SCREAMING_SNAKE_CASE					:			List[str]							= TFDistilBertForTokenClassification(_SCREAMING_SNAKE_CASE    )
											_SCREAMING_SNAKE_CASE					:			List[str]							= {"""input_ids""": input_ids, """attention_mask""": input_mask}
											_SCREAMING_SNAKE_CASE					:			Dict							= model(_SCREAMING_SNAKE_CASE    )
											self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.num_labels)    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			str							= self.prepare_config_and_inputs()
											((_SCREAMING_SNAKE_CASE)						,			(_SCREAMING_SNAKE_CASE)						,			(_SCREAMING_SNAKE_CASE)						,			(_SCREAMING_SNAKE_CASE)						,			(_SCREAMING_SNAKE_CASE)						,			(_SCREAMING_SNAKE_CASE))					:			Dict							= config_and_inputs
											_SCREAMING_SNAKE_CASE					:			Dict							= {"""input_ids""": input_ids, """attention_mask""": input_mask}
											return config, inputs_dict





@require_tf
class        lowercase__    (		_snake_case	,	_snake_case	,	unittest.TestCase   ):





							'''simple docstring'''


							A_				: Any       =    (
							    (
							        TFDistilBertModel,
							        TFDistilBertForMaskedLM,
							        TFDistilBertForQuestionAnswering,
							        TFDistilBertForSequenceClassification,
							        TFDistilBertForTokenClassification,
							        TFDistilBertForMultipleChoice,
							    )
							    if is_tf_available()
							    else None
							)
							A_				: List[str]       =    (
							    {
							        'feature-extraction': TFDistilBertModel,
							        'fill-mask': TFDistilBertForMaskedLM,
							        'question-answering': TFDistilBertForQuestionAnswering,
							        'text-classification': TFDistilBertForSequenceClassification,
							        'token-classification': TFDistilBertForTokenClassification,
							        'zero-shot': TFDistilBertForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							A_				: str       =    False
							A_				: str       =    False






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= TFDistilBertModelTester(self    )
											_SCREAMING_SNAKE_CASE					:			str							= ConfigTester(self       ,   config_class=_SCREAMING_SNAKE_CASE       ,   dim=37    )






							def UpperCAmelCase_      (       self    ):
											self.config_tester.run_common_tests()






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Dict							= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_model(*_SCREAMING_SNAKE_CASE    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			int							= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_masked_lm(*_SCREAMING_SNAKE_CASE    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_question_answering(*_SCREAMING_SNAKE_CASE    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Any							= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_sequence_classification(*_SCREAMING_SNAKE_CASE    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			List[Any]							= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_multiple_choice(*_SCREAMING_SNAKE_CASE    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Tuple							= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_token_classification(*_SCREAMING_SNAKE_CASE    )






							@slow
							def UpperCAmelCase_      (       self    ):





											for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]    ):
															_SCREAMING_SNAKE_CASE					:			Optional[Any]							= TFDistilBertModel.from_pretrained(_SCREAMING_SNAKE_CASE    )
															self.assertIsNotNone(_SCREAMING_SNAKE_CASE    )





@require_tf
class        lowercase__    (		unittest.TestCase   ):





							'''simple docstring'''


							@slow
							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Tuple							= TFDistilBertModel.from_pretrained("""distilbert-base-uncased"""    )
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= tf.constant([[0, 1, 2, 3, 4, 5]]    )
											_SCREAMING_SNAKE_CASE					:			Any							= model(_SCREAMING_SNAKE_CASE    )[0]

											_SCREAMING_SNAKE_CASE					:			Tuple							= [1, 6, 768]
											self.assertEqual(output.shape       ,   _SCREAMING_SNAKE_CASE    )

											_SCREAMING_SNAKE_CASE					:			str							= tf.constant(
											    [
											        [
											            [0.19261885, -0.13732955, 0.4119799],
											            [0.22150156, -0.07422661, 0.39037204],
											            [0.22756018, -0.0896414, 0.3701467],
											        ]
											    ]    )
											tf.debugging.assert_near(output[:, :3, :3]       ,   _SCREAMING_SNAKE_CASE       ,   atol=1e-4    )








from typing import Any

def 			lowerCAmelCase__					(     a__:						list     ,    a__:						list     ,    a__:						dict     ,    a__:						dict     ,    a__:						dict     ,    )    ->						list:


							'''simple docstring'''





							_validation(
							    a__     ,    a__     ,    a__     ,    a__     ,    a__     ,    )
							# Creates data structures and fill initial step
							_UpperCAmelCase											=     {}
							_UpperCAmelCase											=     {}
							for state in states_space:
														_UpperCAmelCase											=     observations_space[0]
														_UpperCAmelCase											=     (
														    initial_probabilities[state] * emission_probabilities[state][observation]
														)
														_UpperCAmelCase											=     None

							# Fills the data structure with the probabilities of
							# different transitions and pointers to previous states
							for o in range(1     ,    len(a__			)			):
														_UpperCAmelCase											=     observations_space[o]
														_UpperCAmelCase											=     observations_space[o - 1]
														for state in states_space:
																					# Calculates the argmax for probability function
																					_UpperCAmelCase											=     ''
																					_UpperCAmelCase											=     -1
																					for k_state in states_space:
																												_UpperCAmelCase											=     (
																												    probabilities[(k_state, prior_observation)]
																												    * transition_probabilities[k_state][state]
																												    * emission_probabilities[state][observation]
																												)
																												if probability > max_probability:
																																			_UpperCAmelCase											=     probability
																																			_UpperCAmelCase											=     k_state

            # Update probabilities and pointers dicts
																					_UpperCAmelCase											=     (
																					    probabilities[(arg_max, prior_observation)]
																					    * transition_probabilities[arg_max][state]
																					    * emission_probabilities[state][observation]
																					)

																					_UpperCAmelCase											=     arg_max

    # The final observation
							_UpperCAmelCase											=     observations_space[len(a__			) - 1]

							# argmax for given final observation
							_UpperCAmelCase											=     ''
							_UpperCAmelCase											=     -1
							for k_state in states_space:
														_UpperCAmelCase											=     probabilities[(k_state, final_observation)]
														if probability > max_probability:
																					_UpperCAmelCase											=     probability
																					_UpperCAmelCase											=     k_state
							_UpperCAmelCase											=     arg_max

							# Process pointers backwards
							_UpperCAmelCase											=     last_state
							_UpperCAmelCase											=     []
							for o in range(len(a__			) - 1     ,    -1     ,    -1			):
														result.append(a__			)
														_UpperCAmelCase											=     pointers[previous, observations_space[o]]
							result.reverse()

							return result

def 			lowerCAmelCase__					(     a__:						Any     ,    a__:						Any     ,    a__:						Any     ,    a__:						Any     ,    a__:						Any     ,    )    ->						None:


							'''simple docstring'''





							_validate_not_empty(
							    a__     ,    a__     ,    a__     ,    a__     ,    a__     ,    )
							_validate_lists(a__     ,    a__			)
							_validate_dicts(
							    a__     ,    a__     ,    a__			)

def 			lowerCAmelCase__					(     a__:						Any     ,    a__:						Any     ,    a__:						Any     ,    a__:						Any     ,    a__:						Any     ,    )    ->						None:


							'''simple docstring'''





							if not all(
							    [
							        observations_space,
							        states_space,
							        initial_probabilities,
							        transition_probabilities,
							        emission_probabilities,
							    ]			):
														raise ValueError('There\'s an empty parameter'			)

def 			lowerCAmelCase__					(     a__:						Any     ,    a__:						Any			)    ->						None:


							'''simple docstring'''





							_validate_list(a__     ,    'observations_space'			)
							_validate_list(a__     ,    'states_space'			)

def 			lowerCAmelCase__					(     a__:						Any     ,    a__:						str			)    ->						None:


							'''simple docstring'''





							if not isinstance(_object     ,    a__			):
														_UpperCAmelCase											=     F'''{var_name} must be a list'''
														raise ValueError(a__			)
							else:
														for x in _object:
																					if not isinstance(a__     ,    a__			):
																												_UpperCAmelCase											=     F'''{var_name} must be a list of strings'''
																												raise ValueError(a__			)

def 			lowerCAmelCase__					(     a__:						Any     ,    a__:						Any     ,    a__:						Any     ,    )    ->						None:


							'''simple docstring'''





							_validate_dict(a__     ,    'initial_probabilities'     ,    a__			)
							_validate_nested_dict(a__     ,    'transition_probabilities'			)
							_validate_nested_dict(a__     ,    'emission_probabilities'			)

def 			lowerCAmelCase__					(     a__:						Any     ,    a__:						str			)    ->						None:


							'''simple docstring'''





							_validate_dict(_object     ,    a__     ,    a__			)
							for x in _object.values():
														_validate_dict(a__     ,    a__     ,    a__     ,    a__			)

def 			lowerCAmelCase__					(     a__:						Any     ,    a__:						str     ,    a__:						type     ,    a__:						bool = False			)    ->						None:


							'''simple docstring'''





							if not isinstance(_object     ,    a__			):
														_UpperCAmelCase											=     F'''{var_name} must be a dict'''
														raise ValueError(a__			)
							if not all(isinstance(a__     ,    a__			) for x in _object			):
														_UpperCAmelCase											=     F'''{var_name} all keys must be strings'''
														raise ValueError(a__			)
							if not all(isinstance(a__     ,    a__			) for x in _object.values()			):
														_UpperCAmelCase											=     'nested dictionary ' if nested else ''
														_UpperCAmelCase											=     F'''{var_name} {nested_text}all values must be {value_type.__name__}'''
														raise ValueError(a__			)


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

		testmod()
