




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     __lowerCAmelCase (       __magic_name__			,				__magic_name__			,				unittest.TestCase ):

			"""simple docstring"""







			snake_case_   		=       (
			    (
			        TFMobileBertModel,
			        TFMobileBertForMaskedLM,
			        TFMobileBertForNextSentencePrediction,
			        TFMobileBertForPreTraining,
			        TFMobileBertForQuestionAnswering,
			        TFMobileBertForSequenceClassification,
			        TFMobileBertForTokenClassification,
			        TFMobileBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			snake_case_   		=       (
			    {
			        '''feature-extraction''': TFMobileBertModel,
			        '''fill-mask''': TFMobileBertForMaskedLM,
			        '''question-answering''': TFMobileBertForQuestionAnswering,
			        '''text-classification''': TFMobileBertForSequenceClassification,
			        '''token-classification''': TFMobileBertForTokenClassification,
			        '''zero-shot''': TFMobileBertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			snake_case_   		=       False
			snake_case_   		=       False


			def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__=False							)    ->      str:



							'''simple docstring'''
							__lowerCamelCase		       =    super()._prepare_for_class(lowerCamelCase__			,						lowerCamelCase__			,						return_labels=lowerCamelCase__							)

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

							return inputs_dict







			class     __lowerCAmelCase (       __magic_name__ ):

						"""simple docstring"""







						def __init__(      self			,						lowerCamelCase__			,						lowerCamelCase__=13			,						lowerCamelCase__=7			,						lowerCamelCase__=True			,						lowerCamelCase__=True			,						lowerCamelCase__=True			,						lowerCamelCase__=True			,						lowerCamelCase__=99			,						lowerCamelCase__=32			,						lowerCamelCase__=32			,						lowerCamelCase__=2			,						lowerCamelCase__=4			,						lowerCamelCase__=37			,						lowerCamelCase__="gelu"			,						lowerCamelCase__=0.1			,						lowerCamelCase__=0.1			,						lowerCamelCase__=512			,						lowerCamelCase__=16			,						lowerCamelCase__=2			,						lowerCamelCase__=0.02			,						lowerCamelCase__=3			,						lowerCamelCase__=4			,						lowerCamelCase__=None			,						)    ->      List[str]:



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


						def       lowercase_    (      self							)    ->      Optional[int]:



										'''simple docstring'''
										__lowerCamelCase		       =    ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size							)

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

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

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

										__lowerCamelCase		       =    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       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__							)    ->      Optional[int]:



										'''simple docstring'''
										__lowerCamelCase		       =    TFMobileBertModel(config=lowerCamelCase__							)
										__lowerCamelCase		       =    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
										__lowerCamelCase		       =    model(lowerCamelCase__							)

										__lowerCamelCase		       =    [input_ids, input_mask]
										__lowerCamelCase		       =    model(lowerCamelCase__							)

										__lowerCamelCase		       =    model(lowerCamelCase__							)

										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       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__							)    ->      List[Any]:



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


						def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__							)    ->      List[str]:



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


						def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__							)    ->      Any:



										'''simple docstring'''
										__lowerCamelCase		       =    TFMobileBertForPreTraining(config=lowerCamelCase__							)
										__lowerCamelCase		       =    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
										__lowerCamelCase		       =    model(lowerCamelCase__							)
										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       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__							)    ->      Any:



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


						def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__							)    ->      Dict:



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


						def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__							)    ->      Any:



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


						def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__							)    ->      Dict:



										'''simple docstring'''
										__lowerCamelCase		       =    TFMobileBertForQuestionAnswering(config=lowerCamelCase__							)
										__lowerCamelCase		       =    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
										__lowerCamelCase		       =    model(lowerCamelCase__							)
										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							)    ->      int:



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




			def       lowercase_    (      self							)    ->      Union[str, Any]:



							'''simple docstring'''
							__lowerCamelCase		       =    TFMobileBertModelTest.TFMobileBertModelTester(self							)
							__lowerCamelCase		       =    ConfigTester(self			,						config_class=lowerCamelCase__			,						hidden_size=37							)


			def       lowercase_    (      self							)    ->      Tuple:



							'''simple docstring'''
							self.config_tester.run_common_tests()


			def       lowercase_    (      self							)    ->      Optional[int]:



							'''simple docstring'''
							__lowerCamelCase		       =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*lowerCamelCase__							)


			def       lowercase_    (      self							)    ->      List[Any]:



							'''simple docstring'''
							__lowerCamelCase		       =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCamelCase__							)


			def       lowercase_    (      self							)    ->      Dict:



							'''simple docstring'''
							__lowerCamelCase		       =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCamelCase__							)


			def       lowercase_    (      self							)    ->      List[str]:



							'''simple docstring'''
							__lowerCamelCase		       =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCamelCase__							)


			def       lowercase_    (      self							)    ->      Optional[int]:



							'''simple docstring'''
							__lowerCamelCase		       =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCamelCase__							)


			def       lowercase_    (      self							)    ->      Union[str, Any]:



							'''simple docstring'''
							__lowerCamelCase		       =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCamelCase__							)


			def       lowercase_    (      self							)    ->      Dict:



							'''simple docstring'''
							__lowerCamelCase		       =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCamelCase__							)


			def       lowercase_    (      self							)    ->      Dict:



							'''simple docstring'''
							__lowerCamelCase		       =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCamelCase__							)






			@slow
			def       lowercase_    (      self							)    ->      List[Any]:



							'''simple docstring'''
							# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							for model_name in ["google/mobilebert-uncased"]:
											__lowerCamelCase		       =    TFMobileBertModel.from_pretrained(lowerCamelCase__							)
											self.assertIsNotNone(lowerCamelCase__							)







@require_tf
class     __lowerCAmelCase (       unittest.TestCase ):

			"""simple docstring"""







			@slow
			def       lowercase_    (      self							)    ->      List[Any]:



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

							__lowerCamelCase		       =    [1, 6, 30_522]
							self.assertEqual(output.shape			,						lowerCamelCase__							)

							__lowerCamelCase		       =    tf.constant(
							    [
							        [
							            [-4.5_91_95_47, -9.24_82_95, -9.64_52_56],
							            [-6.7_30_61_75, -6.44_02_84, -6.6_05_28_37],
							            [-7.2_74_35_06, -6.7_84_79_15, -6.02_46_73],
							        ]
							    ]							)
							tf.debugging.assert_near(output[:, :3, :3]			,						lowerCamelCase__			,						atol=1e-4							)








import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging


if TYPE_CHECKING:
					from transformers.pipelines.conversational import Conversation


__A        =    logging.get_logger(__name__)

__A        =    {"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"}

__A        =    {
    "tokenizer_file": {
        "EleutherAI/gpt-neox-20b": "https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json",
    },
}

__A        =    {
    "gpt-neox-20b": 20_48,
}







class     __lowerCAmelCase (       __magic_name__ ):

			"""simple docstring"""







			snake_case_   		=       VOCAB_FILES_NAMES
			snake_case_   		=       PRETRAINED_VOCAB_FILES_MAP
			snake_case_   		=       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			snake_case_   		=       ['''input_ids''', '''attention_mask''']


			def __init__(      self			,						lowerCamelCase__=None			,						lowerCamelCase__=None			,						lowerCamelCase__=None			,						lowerCamelCase__="<|endoftext|>"			,						lowerCamelCase__="<|endoftext|>"			,						lowerCamelCase__="<|endoftext|>"			,						lowerCamelCase__=False			,						**lowerCamelCase__			,						)    ->      int:



							'''simple docstring'''
							super().__init__(
							    lowerCamelCase__			,						lowerCamelCase__			,						tokenizer_file=lowerCamelCase__			,						unk_token=lowerCamelCase__			,						bos_token=lowerCamelCase__			,						eos_token=lowerCamelCase__			,						add_prefix_space=lowerCamelCase__			,						**lowerCamelCase__			,						)

							__lowerCamelCase		       =    json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()							)
							if pre_tok_state.get('add_prefix_space'			,						lowerCamelCase__							) != add_prefix_space:
											__lowerCamelCase		       =    getattr(lowerCamelCase__			,						pre_tok_state.pop('type'							)							)
											__lowerCamelCase		       =    add_prefix_space
											__lowerCamelCase		       =    pre_tok_class(**lowerCamelCase__							)

							__lowerCamelCase		       =    add_prefix_space


			def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__ = None							)    ->      Tuple[str]:



							'''simple docstring'''
							__lowerCamelCase		       =    self._tokenizer.model.save(lowerCamelCase__			,						name=lowerCamelCase__							)
							return tuple(lowerCamelCase__							)






			def       lowercase_    (      self			,						lowerCamelCase__							)    ->      List[int]:



							'''simple docstring'''
							__lowerCamelCase		       =    []
							for is_user, text in conversation.iter_texts():
											input_ids.extend(self.encode(lowerCamelCase__			,						add_special_tokens=lowerCamelCase__							) + [self.eos_token_id]							)

							if len(lowerCamelCase__							) > self.model_max_length:
											__lowerCamelCase		       =    input_ids[-self.model_max_length :]
							return input_ids


