





"""simple docstring"""





import unittest

from transformers import MobileBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    MODEL_FOR_PRETRAINING_MAPPING,
						    MobileBertForMaskedLM,
						    MobileBertForMultipleChoice,
						    MobileBertForNextSentencePrediction,
						    MobileBertForPreTraining,
						    MobileBertForQuestionAnswering,
						    MobileBertForSequenceClassification,
						    MobileBertForTokenClassification,
						    MobileBertModel,
						)





class        _lowerCamelCase      :


						def __init__(self   ,							__a   ,							__a=13   ,							__a=7   ,							__a=True   ,							__a=True   ,							__a=True   ,							__a=True   ,							__a=99   ,							__a=64   ,							__a=32   ,							__a=5   ,							__a=4   ,							__a=37   ,							__a="gelu"   ,							__a=0.1   ,							__a=0.1   ,							__a=5_12   ,							__a=16   ,							__a=2   ,							__a=0.02   ,							__a=3   ,							__a=4   ,							__a=None   ,							) ->   List[str]:
								UpperCamelCase   =	parent
								UpperCamelCase   =	batch_size
								UpperCamelCase   =	seq_length
								UpperCamelCase   =	is_training
								UpperCamelCase   =	use_input_mask
								UpperCamelCase   =	use_token_type_ids
								UpperCamelCase   =	use_labels
								UpperCamelCase   =	vocab_size
								UpperCamelCase   =	hidden_size
								UpperCamelCase   =	embedding_size
								UpperCamelCase   =	num_hidden_layers
								UpperCamelCase   =	num_attention_heads
								UpperCamelCase   =	intermediate_size
								UpperCamelCase   =	hidden_act
								UpperCamelCase   =	hidden_dropout_prob
								UpperCamelCase   =	attention_probs_dropout_prob
								UpperCamelCase   =	max_position_embeddings
								UpperCamelCase   =	type_vocab_size
								UpperCamelCase   =	type_sequence_label_size
								UpperCamelCase   =	initializer_range
								UpperCamelCase   =	num_labels
								UpperCamelCase   =	num_choices
								UpperCamelCase   =	scope


						def     snake_case_   (self		) ->   Union[str, Any]:
								UpperCamelCase   =	ids_tensor([self.batch_size, self.seq_length]   ,							self.vocab_size		)

								UpperCamelCase   =	None
								if self.use_input_mask:
										UpperCamelCase   =	random_attention_mask([self.batch_size, self.seq_length]		)

								UpperCamelCase   =	None
								if self.use_token_type_ids:
										UpperCamelCase   =	ids_tensor([self.batch_size, self.seq_length]   ,							self.type_vocab_size		)

								UpperCamelCase   =	None
								UpperCamelCase   =	None
								UpperCamelCase   =	None
								if self.use_labels:
										UpperCamelCase   =	ids_tensor([self.batch_size]   ,							self.type_sequence_label_size		)
										UpperCamelCase   =	ids_tensor([self.batch_size, self.seq_length]   ,							self.num_labels		)
										UpperCamelCase   =	ids_tensor([self.batch_size]   ,							self.num_choices		)

								UpperCamelCase   =	self.get_config()

								return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


						def     snake_case_   (self		) ->   Union[str, Any]:
								return MobileBertConfig(
								    vocab_size=self.vocab_size   ,							hidden_size=self.hidden_size   ,							num_hidden_layers=self.num_hidden_layers   ,							num_attention_heads=self.num_attention_heads   ,							intermediate_size=self.intermediate_size   ,							embedding_size=self.embedding_size   ,							hidden_act=self.hidden_act   ,							hidden_dropout_prob=self.hidden_dropout_prob   ,							attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,							max_position_embeddings=self.max_position_embeddings   ,							type_vocab_size=self.type_vocab_size   ,							is_decoder=__a   ,							initializer_range=self.initializer_range   ,							)


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a		) ->   Union[str, Any]:
								UpperCamelCase   =	MobileBertModel(config=__a		)
								model.to(__a		)
								model.eval()
								UpperCamelCase   =	model(__a   ,							attention_mask=__a   ,							token_type_ids=__a		)
								UpperCamelCase   =	model(__a   ,							token_type_ids=__a		)
								UpperCamelCase   =	model(__a		)

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


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a		) ->   Dict:
								UpperCamelCase   =	MobileBertForMaskedLM(config=__a		)
								model.to(__a		)
								model.eval()
								UpperCamelCase   =	model(__a   ,							attention_mask=__a   ,							token_type_ids=__a   ,							labels=__a		)
								self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.seq_length, self.vocab_size)		)


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a		) ->   Tuple:
								UpperCamelCase   =	MobileBertForNextSentencePrediction(config=__a		)
								model.to(__a		)
								model.eval()
								UpperCamelCase   =	model(
								    __a   ,							attention_mask=__a   ,							token_type_ids=__a   ,							labels=__a   ,							)
								self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, 2)		)


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a		) ->   List[Any]:
								UpperCamelCase   =	MobileBertForPreTraining(config=__a		)
								model.to(__a		)
								model.eval()
								UpperCamelCase   =	model(
								    __a   ,							attention_mask=__a   ,							token_type_ids=__a   ,							labels=__a   ,							next_sentence_label=__a   ,							)
								self.parent.assertEqual(result.prediction_logits.shape   ,							(self.batch_size, self.seq_length, self.vocab_size)		)
								self.parent.assertEqual(result.seq_relationship_logits.shape   ,							(self.batch_size, 2)		)


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a		) ->   List[str]:
								UpperCamelCase   =	MobileBertForQuestionAnswering(config=__a		)
								model.to(__a		)
								model.eval()
								UpperCamelCase   =	model(
								    __a   ,							attention_mask=__a   ,							token_type_ids=__a   ,							start_positions=__a   ,							end_positions=__a   ,							)
								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   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a		) ->   str:
								UpperCamelCase   =	self.num_labels
								UpperCamelCase   =	MobileBertForSequenceClassification(__a		)
								model.to(__a		)
								model.eval()
								UpperCamelCase   =	model(__a   ,							attention_mask=__a   ,							token_type_ids=__a   ,							labels=__a		)
								self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.num_labels)		)


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a		) ->   Union[str, Any]:
								UpperCamelCase   =	self.num_labels
								UpperCamelCase   =	MobileBertForTokenClassification(config=__a		)
								model.to(__a		)
								model.eval()
								UpperCamelCase   =	model(__a   ,							attention_mask=__a   ,							token_type_ids=__a   ,							labels=__a		)
								self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.seq_length, self.num_labels)		)


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a   ,							__a		) ->   Tuple:
								UpperCamelCase   =	self.num_choices
								UpperCamelCase   =	MobileBertForMultipleChoice(config=__a		)
								model.to(__a		)
								model.eval()
								UpperCamelCase   =	input_ids.unsqueeze(1		).expand(-1   ,							self.num_choices   ,							-1		).contiguous()
								UpperCamelCase   =	token_type_ids.unsqueeze(1		).expand(-1   ,							self.num_choices   ,							-1		).contiguous()
								UpperCamelCase   =	input_mask.unsqueeze(1		).expand(-1   ,							self.num_choices   ,							-1		).contiguous()
								UpperCamelCase   =	model(
								    __a   ,							attention_mask=__a   ,							token_type_ids=__a   ,							labels=__a   ,							)
								self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.num_choices)		)




						def     snake_case_   (self		) ->   str:
								UpperCamelCase   =	self.prepare_config_and_inputs()
								(
								    (
								    UpperCamelCase
								)   ,				(
								    UpperCamelCase
								)   ,				(
								    UpperCamelCase
								)   ,				(
								    UpperCamelCase
								)   ,				(
								    UpperCamelCase
								)   ,				(
								    UpperCamelCase
								)   ,				(
								    UpperCamelCase
								)   ,				
								)   =	config_and_inputs
								UpperCamelCase   =	{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
								return config, inputs_dict





@require_torch
class        _lowerCamelCase      (       _lowercase  ,			_lowercase  ,			unittest.TestCase ):
						UpperCAmelCase_ =			(
						    (
						        MobileBertModel,
						        MobileBertForMaskedLM,
						        MobileBertForMultipleChoice,
						        MobileBertForNextSentencePrediction,
						        MobileBertForPreTraining,
						        MobileBertForQuestionAnswering,
						        MobileBertForSequenceClassification,
						        MobileBertForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCAmelCase_ =			(
						    {
						        "feature-extraction": MobileBertModel,
						        "fill-mask": MobileBertForMaskedLM,
						        "question-answering": MobileBertForQuestionAnswering,
						        "text-classification": MobileBertForSequenceClassification,
						        "token-classification": MobileBertForTokenClassification,
						        "zero-shot": MobileBertForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						UpperCAmelCase_ =			True


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a=False		) ->   Tuple:
								UpperCamelCase   =	super()._prepare_for_class(__a   ,							__a   ,							return_labels=__a		)

								if return_labels:
										if model_class in get_values(__a		):
												UpperCamelCase   =	torch.zeros(
												    (self.model_tester.batch_size, self.model_tester.seq_length)   ,							dtype=torch.long   ,							device=__a		)
												UpperCamelCase   =	torch.zeros(
												    self.model_tester.batch_size   ,							dtype=torch.long   ,							device=__a		)
								return inputs_dict


						def     snake_case_   (self		) ->   int:
								UpperCamelCase   =	MobileBertModelTester(self		)
								UpperCamelCase   =	ConfigTester(self   ,							config_class=__a   ,							hidden_size=37		)


						def     snake_case_   (self		) ->   Optional[int]:
								self.config_tester.run_common_tests()


						def     snake_case_   (self		) ->   List[str]:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_model(*__a		)


						def     snake_case_   (self		) ->   List[Any]:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_masked_lm(*__a		)


						def     snake_case_   (self		) ->   int:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__a		)


						def     snake_case_   (self		) ->   List[Any]:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__a		)


						def     snake_case_   (self		) ->   List[Any]:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_pretraining(*__a		)


						def     snake_case_   (self		) ->   Union[str, Any]:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_question_answering(*__a		)


						def     snake_case_   (self		) ->   Union[str, Any]:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__a		)




						def     snake_case_   (self		) ->   Optional[Any]:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_token_classification(*__a		)



def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		return torch.tensor(
		    _SCREAMING_SNAKE_CASE  ,	dtype=torch.long  ,	device=_SCREAMING_SNAKE_CASE  ,	)


lowerCAmelCase__     			=			1E-3






@require_torch
@require_sentencepiece
@require_tokenizers
class        _lowerCamelCase      (       unittest.TestCase ):


						@slow
						def     snake_case_   (self		) ->   Optional[int]:
								UpperCamelCase   =	MobileBertModel.from_pretrained("google/mobilebert-uncased"		).to(__a		)
								UpperCamelCase   =	_long_tensor([[1_01, 71_10, 10_05, 10_56, 20_23, 1_13_33, 1_74_13, 10_29, 1_02]]		)
								with torch.no_grad():
										UpperCamelCase   =	model(__a		)[0]
								UpperCamelCase   =	torch.Size((1, 9, 5_12)		)
								self.assertEqual(output.shape   ,							__a		)
								UpperCamelCase   =	torch.tensor(
								    [
								        [
								            [-2.4_7_3_6_5_2_6e0_7, 8.2_6_9_1_6_5_6e0_4, 1.6_5_2_1_8_3_8e0_5],
								            [-5.7_5_4_1_7_0_4e-0_1, 3.9_0_5_6_0_2_2e0_0, 4.4_0_1_1_5_0_7e0_0],
								            [2.6_0_4_7_3_5_9e0_0, 1.5_6_7_7_6_5_2e0_0, -1.7_3_2_4_1_8_8e-0_1],
								        ]
								    ]   ,							device=__a   ,							)

								# MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a
								# ~1 difference, it's therefore not a good idea to measure using addition.
								# Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the
								# result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE
								UpperCamelCase   =	torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE		)
								UpperCamelCase   =	torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE		)

								self.assertTrue(lower_bound and upper_bound		)













"""simple docstring"""





import os

from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home


lowerCAmelCase__     			=			HUGGINGFACE_HUB_CACHE


lowerCAmelCase__     			=			'''config.json'''
lowerCAmelCase__     			=			'''diffusion_pytorch_model.bin'''
lowerCAmelCase__     			=			'''diffusion_flax_model.msgpack'''
lowerCAmelCase__     			=			'''model.onnx'''
lowerCAmelCase__     			=			'''diffusion_pytorch_model.safetensors'''
lowerCAmelCase__     			=			'''weights.pb'''
lowerCAmelCase__     			=			'''https://huggingface.co'''
lowerCAmelCase__     			=			default_cache_path
lowerCAmelCase__     			=			'''diffusers_modules'''
lowerCAmelCase__     			=			os.getenv('''HF_MODULES_CACHE''', os.path.join(hf_cache_home, '''modules'''))
lowerCAmelCase__     			=			['''fp16''', '''non-ema''']
lowerCAmelCase__     			=			'''.self_attn'''






