


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



	def __init__(					self ,					snake_case__ ,					):
		lowerCAmelCase					:    int      							= parent
		lowerCAmelCase					:    List[Any]      							= 13
		lowerCAmelCase					:    str      							= 7
		lowerCAmelCase					:    Any      							= True
		lowerCAmelCase					:    str      							= True
		lowerCAmelCase					:    Dict      							= False
		lowerCAmelCase					:    List[str]      							= True
		lowerCAmelCase					:    Tuple      							= 99
		lowerCAmelCase					:    Optional[int]      							= 32
		lowerCAmelCase					:    Tuple      							= 2
		lowerCAmelCase					:    Optional[int]      							= 4
		lowerCAmelCase					:    str      							= 37
		lowerCAmelCase					:    str      							= 'gelu'
		lowerCAmelCase					:    List[str]      							= 0.1
		lowerCAmelCase					:    Union[str, Any]      							= 0.1
		lowerCAmelCase					:    Optional[int]      							= 512
		lowerCAmelCase					:    List[str]      							= 16
		lowerCAmelCase					:    int      							= 2
		lowerCAmelCase					:    Optional[int]      							= 0.0_2
		lowerCAmelCase					:    Tuple      							= 3
		lowerCAmelCase					:    Any      							= 4
		lowerCAmelCase					:    int      							= None



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    str      							= ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size    )

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

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

		lowerCAmelCase					:    int      							= 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 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__    ):
		lowerCAmelCase					:    Dict      							= TFDistilBertModel(config=__UpperCamelCase    )
		lowerCAmelCase					:    Optional[Any]      							= {'input_ids': input_ids, 'attention_mask': input_mask}

		lowerCAmelCase					:    List[str]      							= model(__UpperCamelCase    )

		lowerCAmelCase					:    List[str]      							= [input_ids, input_mask]

		lowerCAmelCase					:    Tuple      							= model(__UpperCamelCase    )

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



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__    ):
		lowerCAmelCase					:    Optional[int]      							= TFDistilBertForMaskedLM(config=__UpperCamelCase    )
		lowerCAmelCase					:    Any      							= {'input_ids': input_ids, 'attention_mask': input_mask}
		lowerCAmelCase					:    Tuple      							= model(__UpperCamelCase    )
		self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.vocab_size)    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__    ):
		lowerCAmelCase					:    Union[str, Any]      							= TFDistilBertForQuestionAnswering(config=__UpperCamelCase    )
		lowerCAmelCase					:    int      							= {
		    'input_ids': input_ids,
		    'attention_mask': input_mask,
		}
		lowerCAmelCase					:    int      							= 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 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__    ):
		lowerCAmelCase					:    Dict      							= self.num_labels
		lowerCAmelCase					:    int      							= TFDistilBertForSequenceClassification(__UpperCamelCase    )
		lowerCAmelCase					:    Tuple      							= {'input_ids': input_ids, 'attention_mask': input_mask}
		lowerCAmelCase					:    int      							= model(__UpperCamelCase    )
		self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__    ):
		lowerCAmelCase					:    Union[str, Any]      							= self.num_choices
		lowerCAmelCase					:    List[Any]      							= TFDistilBertForMultipleChoice(__UpperCamelCase    )
		lowerCAmelCase					:    Union[str, Any]      							= tf.tile(tf.expand_dims(__UpperCamelCase ,					1    ) ,					(1, self.num_choices, 1)    )
		lowerCAmelCase					:    Optional[int]      							= tf.tile(tf.expand_dims(__UpperCamelCase ,					1    ) ,					(1, self.num_choices, 1)    )
		lowerCAmelCase					:    Optional[int]      							= {
		    'input_ids': multiple_choice_inputs_ids,
		    'attention_mask': multiple_choice_input_mask,
		}
		lowerCAmelCase					:    Any      							= model(__UpperCamelCase    )
		self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_choices)    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__    ):
		lowerCAmelCase					:    Dict      							= self.num_labels
		lowerCAmelCase					:    List[Any]      							= TFDistilBertForTokenClassification(__UpperCamelCase    )
		lowerCAmelCase					:    int      							= {'input_ids': input_ids, 'attention_mask': input_mask}
		lowerCAmelCase					:    Tuple      							= model(__UpperCamelCase    )
		self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.num_labels)    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Any      							= self.prepare_config_and_inputs()
		((lowerCAmelCase)							,   (lowerCAmelCase)							,   (lowerCAmelCase)							,   (lowerCAmelCase)							,   (lowerCAmelCase)							,   (lowerCAmelCase))					:    Tuple      							= config_and_inputs
		lowerCAmelCase					:    int      							= {'input_ids': input_ids, 'attention_mask': input_mask}
		return config, inputs_dict



@require_tf
class  lowerCAmelCase				(				a      ,				a      ,				unittest.TestCase							):
	_lowerCamelCase   :					List[Any]			      = (
	    (
	        TFDistilBertModel,
	        TFDistilBertForMaskedLM,
	        TFDistilBertForQuestionAnswering,
	        TFDistilBertForSequenceClassification,
	        TFDistilBertForTokenClassification,
	        TFDistilBertForMultipleChoice,
	    )
	    if is_tf_available()
	    else None
	)
	_lowerCamelCase   :					List[Any]			      = (
	    {
	        """feature-extraction""": TFDistilBertModel,
	        """fill-mask""": TFDistilBertForMaskedLM,
	        """question-answering""": TFDistilBertForQuestionAnswering,
	        """text-classification""": TFDistilBertForSequenceClassification,
	        """token-classification""": TFDistilBertForTokenClassification,
	        """zero-shot""": TFDistilBertForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	_lowerCamelCase   :					List[Any]			      = False
	_lowerCamelCase   :					List[Any]			      = False



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Optional[Any]      							= TFDistilBertModelTester(self    )
		lowerCAmelCase					:    str      							= ConfigTester(self ,					config_class=__UpperCamelCase ,					dim=37    )



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



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Any      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_model(*__UpperCamelCase    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Any      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_masked_lm(*__UpperCamelCase    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Optional[int]      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_question_answering(*__UpperCamelCase    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Dict      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_sequence_classification(*__UpperCamelCase    )



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



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Tuple      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_token_classification(*__UpperCamelCase    )



	@slow
	def 				lowercase     (					self    ):
		for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]    ):
			lowerCAmelCase					:    Dict      							= TFDistilBertModel.from_pretrained(__UpperCamelCase    )
			self.assertIsNotNone(__UpperCamelCase    )



@require_tf
class  lowerCAmelCase				(				unittest.TestCase							):



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

		lowerCAmelCase					:    Union[str, Any]      							= [1, 6, 768]
		self.assertEqual(output.shape ,					__UpperCamelCase    )

		lowerCAmelCase					:    List[Any]      							= 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    )




'''simple docstring'''




def 			__UpperCamelCase    (    _A    :				List[str]				)    ->      Optional[Any]:




	"""simple docstring"""





	if not head:
		return True
	# split the list to two parts
	lowerCAmelCase							,   lowerCAmelCase					:    str      							= head.next, head
	while fast and fast.next:
		lowerCAmelCase					:    Optional[int]      							= fast.next.next
		lowerCAmelCase					:    int      							= slow.next
	lowerCAmelCase					:    int      							= slow.next
	lowerCAmelCase					:    Optional[Any]      							= None  # Don't forget here! But forget still works!
	# reverse the second part
	lowerCAmelCase					:    List[Any]      							= None
	while second:
		lowerCAmelCase					:    List[Any]      							= second.next
		lowerCAmelCase					:    Union[str, Any]      							= node
		lowerCAmelCase					:    Optional[Any]      							= second
		lowerCAmelCase					:    Any      							= nxt
	# compare two parts
	# second part has the same or one less node
	while node:
		if node.val != head.val:
			return False
		lowerCAmelCase					:    Optional[Any]      							= node.next
		lowerCAmelCase					:    Tuple      							= head.next
	return True

def 			__UpperCamelCase    (    _A    :				Optional[Any]				)    ->      Optional[int]:




	"""simple docstring"""





	if not head or not head.next:
		return True

	# 1. Get the midpoint (slow)
	lowerCAmelCase					:    Optional[int]      							= head
	while fast and fast.next:
		lowerCAmelCase							,   lowerCAmelCase					:    Optional[Any]      							= fast.next.next, slow.next

	# 2. Push the second half into the stack
	lowerCAmelCase					:    Tuple      							= [slow.val]
	while slow.next:
		lowerCAmelCase					:    Tuple      							= slow.next
		stack.append(slow.val				)

	# 3. Comparison
	while stack:
		if stack.pop() != cur.val:
			return False
		lowerCAmelCase					:    Union[str, Any]      							= cur.next

	return True






def 			__UpperCamelCase    (    _A    :				Tuple				)    ->      Optional[int]:




	"""simple docstring"""





	if not head or not head.next:
		return True
	lowerCAmelCase					:    Optional[int]      							= {}
	lowerCAmelCase					:    int      							= 0
	while head:
		if head.val in d:
			d[head.val].append(_A				)
		else:
			lowerCAmelCase					:    Any      							= [pos]
		lowerCAmelCase					:    int      							= head.next
		pos += 1
	lowerCAmelCase					:    str      							= pos - 1
	lowerCAmelCase					:    Optional[Any]      							= 0
	for v in d.values():
		if len(_A				) % 2 != 0:
			middle += 1
		else:
			lowerCAmelCase					:    Any      							= 0
			for i in range(0					,   len(_A				)				):
				if v[i] + v[len(_A				) - 1 - step] != checksum:
					return False
				step += 1
		if middle > 1:
			return False
	return True
