

from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
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 import (
						    TF_MODEL_FOR_PRETRAINING_MAPPING,
						    TFMobileBertForMaskedLM,
						    TFMobileBertForMultipleChoice,
						    TFMobileBertForNextSentencePrediction,
						    TFMobileBertForPreTraining,
						    TFMobileBertForQuestionAnswering,
						    TFMobileBertForSequenceClassification,
						    TFMobileBertForTokenClassification,
						    TFMobileBertModel,
						)

@require_tf
class 				__UpperCamelCase				(					_UpperCAmelCase  ,						_UpperCAmelCase  ,						unittest.TestCase			):
	_lowercase  :   int       				=  (
	    (
	        TFMobileBertModel,
	        TFMobileBertForMaskedLM,
	        TFMobileBertForNextSentencePrediction,
	        TFMobileBertForPreTraining,
	        TFMobileBertForQuestionAnswering,
	        TFMobileBertForSequenceClassification,
	        TFMobileBertForTokenClassification,
	        TFMobileBertForMultipleChoice,
	    )
	    if is_tf_available()
	    else ()
	)
	_lowercase  :   Tuple       				=  (
	    {
	        'feature-extraction': TFMobileBertModel,
	        'fill-mask': TFMobileBertForMaskedLM,
	        'question-answering': TFMobileBertForQuestionAnswering,
	        'text-classification': TFMobileBertForSequenceClassification,
	        'token-classification': TFMobileBertForTokenClassification,
	        'zero-shot': TFMobileBertForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	_lowercase  :   Tuple       				=  False
	_lowercase  :   List[Any]       				=  False







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple  , __UpperCamelCase: Tuple  , __UpperCamelCase: Tuple  , __UpperCamelCase: Optional[Any]=False      ):

						'''simple docstring'''





						__magic_name__							=    super()._prepare_for_class(__UpperCamelCase  , __UpperCamelCase  , return_labels=__UpperCamelCase      )

						if return_labels:
											if model_class in get_values(__UpperCamelCase      ):
																__magic_name__							=    tf.zeros(self.model_tester.batch_size  , dtype=tf.intaa      )

						return inputs_dict

	class 				__UpperCamelCase				(					_UpperCAmelCase			):







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

							'''simple docstring'''





							__magic_name__							=    parent
							__magic_name__							=    batch_size
							__magic_name__							=    seq_length
							__magic_name__							=    is_training
							__magic_name__							=    use_input_mask
							__magic_name__							=    use_token_type_ids
							__magic_name__							=    use_labels
							__magic_name__							=    vocab_size
							__magic_name__							=    hidden_size
							__magic_name__							=    num_hidden_layers
							__magic_name__							=    num_attention_heads
							__magic_name__							=    intermediate_size
							__magic_name__							=    hidden_act
							__magic_name__							=    hidden_dropout_prob
							__magic_name__							=    attention_probs_dropout_prob
							__magic_name__							=    max_position_embeddings
							__magic_name__							=    type_vocab_size
							__magic_name__							=    type_sequence_label_size
							__magic_name__							=    initializer_range
							__magic_name__							=    num_labels
							__magic_name__							=    num_choices
							__magic_name__							=    scope
							__magic_name__							=    embedding_size







		def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

							'''simple docstring'''





							__magic_name__							=    ids_tensor([self.batch_size, self.seq_length]  , self.vocab_size      )

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

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

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

							__magic_name__							=    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  , 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  , initializer_range=self.initializer_range  , embedding_size=self.embedding_size  , )

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







		def 	_SCREAMING_SNAKE_CASE							(   self: Optional[Any]  , __UpperCamelCase: str  , __UpperCamelCase: Optional[int]  , __UpperCamelCase: List[Any]  , __UpperCamelCase: List[str]  , __UpperCamelCase: Optional[Any]  , __UpperCamelCase: Optional[Any]  , __UpperCamelCase: Optional[Any]      ):

							'''simple docstring'''





							__magic_name__							=    TFMobileBertModel(config=__UpperCamelCase      )
							__magic_name__							=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
							__magic_name__							=    model(__UpperCamelCase      )

							__magic_name__							=    [input_ids, input_mask]
							__magic_name__							=    model(__UpperCamelCase      )

							__magic_name__							=    model(__UpperCamelCase      )

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

							'''simple docstring'''





							__magic_name__							=    TFMobileBertForMaskedLM(config=__UpperCamelCase      )
							__magic_name__							=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
							__magic_name__							=    model(__UpperCamelCase      )
							self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.seq_length, self.vocab_size)      )







		def 	_SCREAMING_SNAKE_CASE							(   self: Union[str, Any]  , __UpperCamelCase: Optional[int]  , __UpperCamelCase: Tuple  , __UpperCamelCase: Tuple  , __UpperCamelCase: Optional[Any]  , __UpperCamelCase: Optional[int]  , __UpperCamelCase: Union[str, Any]  , __UpperCamelCase: Tuple      ):

							'''simple docstring'''





							__magic_name__							=    TFMobileBertForNextSentencePrediction(config=__UpperCamelCase      )
							__magic_name__							=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
							__magic_name__							=    model(__UpperCamelCase      )
							self.parent.assertEqual(result.logits.shape  , (self.batch_size, 2)      )







		def 	_SCREAMING_SNAKE_CASE							(   self: List[Any]  , __UpperCamelCase: Optional[Any]  , __UpperCamelCase: int  , __UpperCamelCase: str  , __UpperCamelCase: List[str]  , __UpperCamelCase: Optional[Any]  , __UpperCamelCase: Tuple  , __UpperCamelCase: int      ):

							'''simple docstring'''





							__magic_name__							=    TFMobileBertForPreTraining(config=__UpperCamelCase      )
							__magic_name__							=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
							__magic_name__							=    model(__UpperCamelCase      )
							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 	_SCREAMING_SNAKE_CASE							(   self: Union[str, Any]  , __UpperCamelCase: Optional[int]  , __UpperCamelCase: List[Any]  , __UpperCamelCase: str  , __UpperCamelCase: Any  , __UpperCamelCase: Optional[int]  , __UpperCamelCase: Optional[Any]  , __UpperCamelCase: Optional[Any]      ):

							'''simple docstring'''





							__magic_name__							=    self.num_labels
							__magic_name__							=    TFMobileBertForSequenceClassification(config=__UpperCamelCase      )
							__magic_name__							=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
							__magic_name__							=    model(__UpperCamelCase      )
							self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.num_labels)      )







		def 	_SCREAMING_SNAKE_CASE							(   self: List[Any]  , __UpperCamelCase: Any  , __UpperCamelCase: Union[str, Any]  , __UpperCamelCase: Optional[int]  , __UpperCamelCase: List[Any]  , __UpperCamelCase: Dict  , __UpperCamelCase: Optional[Any]  , __UpperCamelCase: Optional[int]      ):

							'''simple docstring'''





							__magic_name__							=    self.num_choices
							__magic_name__							=    TFMobileBertForMultipleChoice(config=__UpperCamelCase      )
							__magic_name__							=    tf.tile(tf.expand_dims(__UpperCamelCase  , 1      )  , (1, self.num_choices, 1)      )
							__magic_name__							=    tf.tile(tf.expand_dims(__UpperCamelCase  , 1      )  , (1, self.num_choices, 1)      )
							__magic_name__							=    tf.tile(tf.expand_dims(__UpperCamelCase  , 1      )  , (1, self.num_choices, 1)      )
							__magic_name__							=    {
							    'input_ids': multiple_choice_inputs_ids,
							    'attention_mask': multiple_choice_input_mask,
							    'token_type_ids': multiple_choice_token_type_ids,
							}
							__magic_name__							=    model(__UpperCamelCase      )
							self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.num_choices)      )







		def 	_SCREAMING_SNAKE_CASE							(   self: Union[str, Any]  , __UpperCamelCase: Any  , __UpperCamelCase: Any  , __UpperCamelCase: str  , __UpperCamelCase: Optional[int]  , __UpperCamelCase: List[Any]  , __UpperCamelCase: Dict  , __UpperCamelCase: List[Any]      ):

							'''simple docstring'''





							__magic_name__							=    self.num_labels
							__magic_name__							=    TFMobileBertForTokenClassification(config=__UpperCamelCase      )
							__magic_name__							=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
							__magic_name__							=    model(__UpperCamelCase      )
							self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.seq_length, self.num_labels)      )







		def 	_SCREAMING_SNAKE_CASE							(   self: Union[str, Any]  , __UpperCamelCase: List[str]  , __UpperCamelCase: Union[str, Any]  , __UpperCamelCase: Any  , __UpperCamelCase: str  , __UpperCamelCase: List[str]  , __UpperCamelCase: str  , __UpperCamelCase: List[str]      ):

							'''simple docstring'''





							__magic_name__							=    TFMobileBertForQuestionAnswering(config=__UpperCamelCase      )
							__magic_name__							=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
							__magic_name__							=    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 	_SCREAMING_SNAKE_CASE							(   self: str      ):

							'''simple docstring'''





							__magic_name__							=    self.prepare_config_and_inputs()
							(
							    (
							    __magic_name__
							),	(
							    __magic_name__
							),	(
							    __magic_name__
							),	(
							    __magic_name__
							),	(
							    __magic_name__
							),	(
							    __magic_name__
							),	(
							    __magic_name__
							),	
							)							=    config_and_inputs
							__magic_name__							=    {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
							return config, inputs_dict







	def 	_SCREAMING_SNAKE_CASE							(   self: List[Any]      ):

						'''simple docstring'''





						__magic_name__							=    TFMobileBertModelTest.TFMobileBertModelTester(self      )
						__magic_name__							=    ConfigTester(self  , config_class=__UpperCamelCase  , hidden_size=37      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Any      ):

						'''simple docstring'''





						self.config_tester.run_common_tests()







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						__magic_name__							=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_model(*__UpperCamelCase      )







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

						'''simple docstring'''





						__magic_name__							=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_masked_lm(*__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: int      ):

						'''simple docstring'''





						__magic_name__							=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: int      ):

						'''simple docstring'''





						__magic_name__							=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[str]      ):

						'''simple docstring'''





						__magic_name__							=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_pretraining(*__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Any      ):

						'''simple docstring'''





						__magic_name__							=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_question_answering(*__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Dict      ):

						'''simple docstring'''





						__magic_name__							=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__UpperCamelCase      )







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

						'''simple docstring'''





						__magic_name__							=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_token_classification(*__UpperCamelCase      )







	@slow
	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[Any]      ):

						'''simple docstring'''





						for model_name in ["google/mobilebert-uncased"]:
											__magic_name__							=    TFMobileBertModel.from_pretrained(__UpperCamelCase      )
											self.assertIsNotNone(__UpperCamelCase      )




@require_tf
class 				__UpperCamelCase				(					unittest.TestCase			):







	@slow
	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[Any]      ):

						'''simple docstring'''





						__magic_name__							=    TFMobileBertForPreTraining.from_pretrained('google/mobilebert-uncased'      )
						__magic_name__							=    tf.constant([[0, 1, 2, 3, 4, 5]]      )
						__magic_name__							=    model(__UpperCamelCase      )[0]

						__magic_name__							=    [1, 6, 3_05_22]
						self.assertEqual(output.shape  , __UpperCamelCase      )

						__magic_name__							=    tf.constant(
						    [
						        [
						            [-4.5919547, -9.248295, -9.645256],
						            [-6.7306175, -6.440284, -6.6052837],
						            [-7.2743506, -6.7847915, -6.024673],
						        ]
						    ]      )
						tf.debugging.assert_near(output[:, :3, :3]  , __UpperCamelCase  , atol=1E-4      )







from typing import List, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


A__						=							logging.get_logger(__name__)

A__						=							{
    "huggingface/time-series-transformer-tourism-monthly": (
        "https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json"
    ),
    # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer
}

class 				__UpperCamelCase				(					SCREAMING_SNAKE_CASE			):
	_lowercase  :   Optional[int]       				=  "time_series_transformer"
	_lowercase  :   Dict       				=  {
	    "hidden_size": "d_model",
	    "num_attention_heads": "encoder_attention_heads",
	    "num_hidden_layers": "encoder_layers",
	}







	def __init__(   self: str  , __UpperCamelCase: Optional[int] = None  , __UpperCamelCase: Optional[int] = None  , __UpperCamelCase: str = "student_t"  , __UpperCamelCase: str = "nll"  , __UpperCamelCase: int = 1  , __UpperCamelCase: List[int] = [1, 2, 3, 4, 5, 6, 7]  , __UpperCamelCase: Optional[Union[str, bool]] = "mean"  , __UpperCamelCase: int = 0  , __UpperCamelCase: int = 0  , __UpperCamelCase: int = 0  , __UpperCamelCase: int = 0  , __UpperCamelCase: Optional[List[int]] = None  , __UpperCamelCase: Optional[List[int]] = None  , __UpperCamelCase: int = 32  , __UpperCamelCase: int = 32  , __UpperCamelCase: int = 2  , __UpperCamelCase: int = 2  , __UpperCamelCase: int = 2  , __UpperCamelCase: int = 2  , __UpperCamelCase: bool = True  , __UpperCamelCase: str = "gelu"  , __UpperCamelCase: int = 64  , __UpperCamelCase: float = 0.1  , __UpperCamelCase: float = 0.1  , __UpperCamelCase: float = 0.1  , __UpperCamelCase: float = 0.1  , __UpperCamelCase: float = 0.1  , __UpperCamelCase: int = 1_00  , __UpperCamelCase: float = 0.02  , __UpperCamelCase: Any=True  , **__UpperCamelCase: Optional[Any]  , ):

						'''simple docstring'''





						__magic_name__							=    prediction_length
						__magic_name__							=    context_length or prediction_length
						__magic_name__							=    distribution_output
						__magic_name__							=    loss
						__magic_name__							=    input_size
						__magic_name__							=    num_time_features
						__magic_name__							=    lags_sequence
						__magic_name__							=    scaling
						__magic_name__							=    num_dynamic_real_features
						__magic_name__							=    num_static_real_features
						__magic_name__							=    num_static_categorical_features
						if cardinality and num_static_categorical_features > 0:
											if len(__UpperCamelCase      ) != num_static_categorical_features:
																raise ValueError(
																    'The cardinality should be a list of the same length as `num_static_categorical_features`'      )
											__magic_name__							=    cardinality
						else:
											__magic_name__							=    [0]
						if embedding_dimension and num_static_categorical_features > 0:
											if len(__UpperCamelCase      ) != num_static_categorical_features:
																raise ValueError(
																    'The embedding dimension should be a list of the same length as `num_static_categorical_features`'      )
											__magic_name__							=    embedding_dimension
						else:
											__magic_name__							=    [min(50  , (cat + 1) // 2      ) for cat in self.cardinality]
						__magic_name__							=    num_parallel_samples

						# Transformer architecture configuration
						__magic_name__							=    input_size * len(__UpperCamelCase      ) + self._number_of_features
						__magic_name__							=    d_model
						__magic_name__							=    encoder_attention_heads
						__magic_name__							=    decoder_attention_heads
						__magic_name__							=    encoder_ffn_dim
						__magic_name__							=    decoder_ffn_dim
						__magic_name__							=    encoder_layers
						__magic_name__							=    decoder_layers

						__magic_name__							=    dropout
						__magic_name__							=    attention_dropout
						__magic_name__							=    activation_dropout
						__magic_name__							=    encoder_layerdrop
						__magic_name__							=    decoder_layerdrop

						__magic_name__							=    activation_function
						__magic_name__							=    init_std

						__magic_name__							=    use_cache

						super().__init__(is_encoder_decoder=__UpperCamelCase  , **__UpperCamelCase      )







	@property
	def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

						'''simple docstring'''





						return (
						    sum(self.embedding_dimension      )
						    + self.num_dynamic_real_features
						    + self.num_time_features
						    + self.num_static_real_features
						    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
						)




