


'''simple docstring'''





import unittest

import numpy as np

from transformers import DistilBertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
					import jax.numpy as jnp

					from transformers.models.distilbert.modeling_flax_distilbert import (
					    FlaxDistilBertForMaskedLM,
					    FlaxDistilBertForMultipleChoice,
					    FlaxDistilBertForQuestionAnswering,
					    FlaxDistilBertForSequenceClassification,
					    FlaxDistilBertForTokenClassification,
					    FlaxDistilBertModel,
					)
class 						a__					(					unittest.TestCase   ):





			'''simple docstring'''

			def __init__(  self							:			Any			,    lowerCAmelCase_							:			Optional[int]			,    lowerCAmelCase_							:			List[Any]=13			,    lowerCAmelCase_							:			int=7			,    lowerCAmelCase_							:			str=True			,    lowerCAmelCase_							:			List[str]=True			,    lowerCAmelCase_							:			Union[str, Any]=True			,    lowerCAmelCase_							:			Dict=True			,    lowerCAmelCase_							:			List[str]=99			,    lowerCAmelCase_							:			Any=32			,    lowerCAmelCase_							:			Any=5			,    lowerCAmelCase_							:			List[str]=4			,    lowerCAmelCase_							:			int=37			,    lowerCAmelCase_							:			List[str]="gelu"			,    lowerCAmelCase_							:			Any=0.1			,    lowerCAmelCase_							:			Dict=0.1			,    lowerCAmelCase_							:			int=512			,    lowerCAmelCase_							:			Optional[int]=16			,    lowerCAmelCase_							:			Dict=2			,    lowerCAmelCase_							:			List[str]=0.02			,    lowerCAmelCase_							:			Dict=4			,    )      ->		Dict:
						__A=	parent
						__A=	batch_size
						__A=	seq_length
						__A=	is_training
						__A=	use_attention_mask
						__A=	use_token_type_ids
						__A=	use_labels
						__A=	vocab_size
						__A=	hidden_size
						__A=	num_hidden_layers
						__A=	num_attention_heads
						__A=	intermediate_size
						__A=	hidden_act
						__A=	hidden_dropout_prob
						__A=	attention_probs_dropout_prob
						__A=	max_position_embeddings
						__A=	type_vocab_size
						__A=	type_sequence_label_size
						__A=	initializer_range
						__A=	num_choices
			def 					lowerCAmelCase     (  self							:			Optional[int]	)      ->		List[str]:
						__A=	ids_tensor([self.batch_size, self.seq_length]			,    self.vocab_size	)

						__A=	None
						if self.use_attention_mask:
									__A=	random_attention_mask([self.batch_size, self.seq_length]	)

						__A=	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			,    tie_weights_=lowerCAmelCase_			,    )

						return config, input_ids, attention_mask

			def 					lowerCAmelCase     (  self							:			List[Any]	)      ->		str:
						__A=	self.prepare_config_and_inputs()
						__A,	__A,	__A=	config_and_inputs
						__A=	{'input_ids': input_ids, 'attention_mask': attention_mask}
						return config, inputs_dict


@require_flax
class 						a__					(					a_      ,  unittest.TestCase   ):





			'''simple docstring'''

			A						:				str					      =			(
			    (
			        FlaxDistilBertModel,
			        FlaxDistilBertForMaskedLM,
			        FlaxDistilBertForMultipleChoice,
			        FlaxDistilBertForQuestionAnswering,
			        FlaxDistilBertForSequenceClassification,
			        FlaxDistilBertForTokenClassification,
			        FlaxDistilBertForQuestionAnswering,
			    )
			    if is_flax_available()
			    else ()
			)
			def 					lowerCAmelCase     (  self							:			Union[str, Any]	)      ->		Tuple:
						__A=	FlaxDistilBertModelTester(self	)

			@slow
			def 					lowerCAmelCase     (  self							:			Optional[Any]	)      ->		Union[str, Any]:
						for model_class_name in self.all_model_classes:
									__A=	model_class_name.from_pretrained('distilbert-base-uncased'	)
									__A=	model(np.ones((1, 1)	)	)
									self.assertIsNotNone(lowerCAmelCase_	)


@require_flax
class 						a__					(					unittest.TestCase   ):





			'''simple docstring'''

			@slow
			def 					lowerCAmelCase     (  self							:			List[Any]	)      ->		str:
						__A=	FlaxDistilBertModel.from_pretrained('distilbert-base-uncased'	)
						__A=	np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]	)
						__A=	np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
						__A=	model(lowerCAmelCase_			,    attention_mask=lowerCAmelCase_	)[0]
						__A=	(1, 11, 768)
						self.assertEqual(output.shape			,    lowerCAmelCase_	)
						__A=	np.array([[[-0.16_39, 0.32_99, 0.16_48], [-0.17_46, 0.32_89, 0.17_10], [-0.18_84, 0.33_57, 0.18_10]]]	)

						self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]			,    lowerCAmelCase_			,    atol=1E-4	)	)







'''simple docstring'''





from __future__ import annotations

import unittest

from transformers import is_tf_available, is_torch_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow


if is_tf_available():
					from transformers import (
					    AutoConfig,
					    BertConfig,
					    GPTaConfig,
					    TaConfig,
					    TFAutoModel,
					    TFAutoModelForCausalLM,
					    TFAutoModelForMaskedLM,
					    TFAutoModelForPreTraining,
					    TFAutoModelForQuestionAnswering,
					    TFAutoModelForSeqaSeqLM,
					    TFAutoModelForSequenceClassification,
					    TFAutoModelWithLMHead,
					    TFBertForMaskedLM,
					    TFBertForPreTraining,
					    TFBertForQuestionAnswering,
					    TFBertForSequenceClassification,
					    TFBertModel,
					    TFGPTaLMHeadModel,
					    TFRobertaForMaskedLM,
					    TFTaForConditionalGeneration,
					)
					from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
					from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
					from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST

if is_torch_available():
					from transformers import (
					    AutoModel,
					    AutoModelForCausalLM,
					    AutoModelForMaskedLM,
					    AutoModelForPreTraining,
					    AutoModelForQuestionAnswering,
					    AutoModelForSeqaSeqLM,
					    AutoModelForSequenceClassification,
					    AutoModelWithLMHead,
					    BertForMaskedLM,
					    BertForPreTraining,
					    BertForQuestionAnswering,
					    BertForSequenceClassification,
					    BertModel,
					    GPTaLMHeadModel,
					    RobertaForMaskedLM,
					    TaForConditionalGeneration,
					)
@is_pt_tf_cross_test
class 						a__					(					unittest.TestCase   ):





			'''simple docstring'''

			@slow
			def 					lowerCAmelCase     (  self							:			List[str]	)      ->		List[Any]:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModel.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModel.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Tuple	)      ->		Optional[Any]:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForPreTraining.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForPreTraining.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			int	)      ->		Optional[int]:
						for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForCausalLM.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									__A,	__A=	TFAutoModelForCausalLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForCausalLM.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									__A,	__A=	AutoModelForCausalLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Union[str, Any]	)      ->		Optional[Any]:
						for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Union[str, Any]	)      ->		Optional[int]:
						for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForMaskedLM.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									__A,	__A=	TFAutoModelForMaskedLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForMaskedLM.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									__A,	__A=	AutoModelForMaskedLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Tuple	)      ->		Dict:
						for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									__A,	__A=	TFAutoModelForSeqaSeqLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									__A,	__A=	AutoModelForSeqaSeqLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Optional[Any]	)      ->		str:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForSequenceClassification.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Union[str, Any]	)      ->		List[Any]:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForQuestionAnswering.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForQuestionAnswering.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			def 					lowerCAmelCase     (  self							:			int	)      ->		List[str]:
						__A=	TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
						self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
						self.assertEqual(model.num_parameters()			,    14_410	)
						self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_	)			,    14_410	)

						__A=	AutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
						self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
						self.assertEqual(model.num_parameters()			,    14_410	)
						self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_	)			,    14_410	)

			def 					lowerCAmelCase     (  self							:			Optional[Any]	)      ->		Tuple:
						__A=	TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
						self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
						self.assertEqual(model.num_parameters()			,    14_410	)
						self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_	)			,    14_410	)

						__A=	AutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
						self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
						self.assertEqual(model.num_parameters()			,    14_410	)
						self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_	)			,    14_410	)



