





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 SCREAMING_SNAKE_CASE_  :






						def __init__( self : List[str]						,				_A : Dict						,				_A : str=13						,				_A : str=7						,				_A : List[Any]=True						,				_A : Any=True						,				_A : Optional[int]=True						,				_A : List[str]=True						,				_A : int=99						,				_A : int=64						,				_A : Any=32						,				_A : Tuple=5						,				_A : Optional[Any]=4						,				_A : Optional[Any]=37						,				_A : Dict="gelu"						,				_A : List[str]=0.1						,				_A : str=0.1						,				_A : Union[str, Any]=512						,				_A : List[Any]=16						,				_A : int=2						,				_A : Union[str, Any]=0.0_2						,				_A : int=3						,				_A : List[str]=4						,				_A : Any=None						,				)							->      List[Any]:







									"""simple docstring"""


									snake_case_						:     Any     =		parent
									snake_case_						:     str     =		batch_size
									snake_case_						:     Optional[int]     =		seq_length
									snake_case_						:     Dict     =		is_training
									snake_case_						:     Optional[int]     =		use_input_mask
									snake_case_						:     Optional[Any]     =		use_token_type_ids
									snake_case_						:     Union[str, Any]     =		use_labels
									snake_case_						:     Union[str, Any]     =		vocab_size
									snake_case_						:     Optional[int]     =		hidden_size
									snake_case_						:     Optional[int]     =		embedding_size
									snake_case_						:     int     =		num_hidden_layers
									snake_case_						:     List[str]     =		num_attention_heads
									snake_case_						:     List[Any]     =		intermediate_size
									snake_case_						:     Optional[int]     =		hidden_act
									snake_case_						:     Any     =		hidden_dropout_prob
									snake_case_						:     Optional[int]     =		attention_probs_dropout_prob
									snake_case_						:     Tuple     =		max_position_embeddings
									snake_case_						:     str     =		type_vocab_size
									snake_case_						:     Union[str, Any]     =		type_sequence_label_size
									snake_case_						:     Any     =		initializer_range
									snake_case_						:     str     =		num_labels
									snake_case_						:     List[Any]     =		num_choices
									snake_case_						:     int     =		scope






						def UpperCAmelCase_			( self : List[str]		)							->      Tuple:







									"""simple docstring"""


									snake_case_						:     Optional[int]     =		ids_tensor([self.batch_size, self.seq_length]						,				self.vocab_size		)

									snake_case_						:     Union[str, Any]     =		None
									if self.use_input_mask:
												snake_case_						:     Tuple     =		random_attention_mask([self.batch_size, self.seq_length]		)

									snake_case_						:     Dict     =		None
									if self.use_token_type_ids:
												snake_case_						:     Union[str, Any]     =		ids_tensor([self.batch_size, self.seq_length]						,				self.type_vocab_size		)

									snake_case_						:     Dict     =		None
									snake_case_						:     str     =		None
									snake_case_						:     Any     =		None
									if self.use_labels:
												snake_case_						:     str     =		ids_tensor([self.batch_size]						,				self.type_sequence_label_size		)
												snake_case_						:     str     =		ids_tensor([self.batch_size, self.seq_length]						,				self.num_labels		)
												snake_case_						:     List[str]     =		ids_tensor([self.batch_size]						,				self.num_choices		)

									snake_case_						:     str     =		self.get_config()

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






						def UpperCAmelCase_			( self : Any		)							->      int:







									"""simple docstring"""


									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 UpperCAmelCase_			( self : List[str]						,				_A : Tuple						,				_A : str						,				_A : int						,				_A : int						,				_A : Tuple						,				_A : int						,				_A : List[str]		)							->      str:







									"""simple docstring"""


									snake_case_						:     Optional[Any]     =		MobileBertModel(config=_A		)
									model.to(_A		)
									model.eval()
									snake_case_						:     Any     =		model(_A						,				attention_mask=_A						,				token_type_ids=_A		)
									snake_case_						:     List[str]     =		model(_A						,				token_type_ids=_A		)
									snake_case_						:     Dict     =		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 UpperCAmelCase_			( self : Optional[int]						,				_A : int						,				_A : Optional[Any]						,				_A : Tuple						,				_A : Union[str, Any]						,				_A : Optional[Any]						,				_A : List[Any]						,				_A : str		)							->      Union[str, Any]:







									"""simple docstring"""


									snake_case_						:     str     =		MobileBertForMaskedLM(config=_A		)
									model.to(_A		)
									model.eval()
									snake_case_						:     Optional[int]     =		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 UpperCAmelCase_			( self : Tuple						,				_A : str						,				_A : List[Any]						,				_A : Tuple						,				_A : Union[str, Any]						,				_A : Dict						,				_A : Dict						,				_A : Any		)							->      int:







									"""simple docstring"""


									snake_case_						:     Any     =		MobileBertForNextSentencePrediction(config=_A		)
									model.to(_A		)
									model.eval()
									snake_case_						:     str     =		model(
									    _A						,				attention_mask=_A						,				token_type_ids=_A						,				labels=_A						,				)
									self.parent.assertEqual(result.logits.shape						,				(self.batch_size, 2)		)






						def UpperCAmelCase_			( self : List[str]						,				_A : Optional[Any]						,				_A : int						,				_A : Dict						,				_A : int						,				_A : str						,				_A : Optional[int]						,				_A : Any		)							->      Union[str, Any]:







									"""simple docstring"""


									snake_case_						:     List[Any]     =		MobileBertForPreTraining(config=_A		)
									model.to(_A		)
									model.eval()
									snake_case_						:     List[str]     =		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 UpperCAmelCase_			( self : Optional[int]						,				_A : List[Any]						,				_A : str						,				_A : Dict						,				_A : Optional[Any]						,				_A : Optional[Any]						,				_A : List[str]						,				_A : Union[str, Any]		)							->      str:







									"""simple docstring"""


									snake_case_						:     str     =		MobileBertForQuestionAnswering(config=_A		)
									model.to(_A		)
									model.eval()
									snake_case_						:     Dict     =		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 UpperCAmelCase_			( self : str						,				_A : int						,				_A : Optional[int]						,				_A : Dict						,				_A : Optional[int]						,				_A : str						,				_A : List[Any]						,				_A : Tuple		)							->      Optional[int]:







									"""simple docstring"""


									snake_case_						:     Dict     =		self.num_labels
									snake_case_						:     int     =		MobileBertForSequenceClassification(_A		)
									model.to(_A		)
									model.eval()
									snake_case_						:     Any     =		model(_A						,				attention_mask=_A						,				token_type_ids=_A						,				labels=_A		)
									self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_labels)		)






						def UpperCAmelCase_			( self : Any						,				_A : Optional[int]						,				_A : Optional[Any]						,				_A : Optional[Any]						,				_A : Optional[Any]						,				_A : Union[str, Any]						,				_A : Optional[Any]						,				_A : Optional[int]		)							->      Union[str, Any]:







									"""simple docstring"""


									snake_case_						:     Tuple     =		self.num_labels
									snake_case_						:     Dict     =		MobileBertForTokenClassification(config=_A		)
									model.to(_A		)
									model.eval()
									snake_case_						:     Union[str, Any]     =		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 UpperCAmelCase_			( self : str						,				_A : Any						,				_A : List[Any]						,				_A : Union[str, Any]						,				_A : Tuple						,				_A : Optional[int]						,				_A : int						,				_A : Tuple		)							->      List[Any]:







									"""simple docstring"""


									snake_case_						:     Optional[int]     =		self.num_choices
									snake_case_						:     List[Any]     =		MobileBertForMultipleChoice(config=_A		)
									model.to(_A		)
									model.eval()
									snake_case_						:     Optional[Any]     =		input_ids.unsqueeze(1		).expand(-1						,				self.num_choices						,				-1		).contiguous()
									snake_case_						:     List[Any]     =		token_type_ids.unsqueeze(1		).expand(-1						,				self.num_choices						,				-1		).contiguous()
									snake_case_						:     Tuple     =		input_mask.unsqueeze(1		).expand(-1						,				self.num_choices						,				-1		).contiguous()
									snake_case_						:     Union[str, Any]     =		model(
									    _A						,				attention_mask=_A						,				token_type_ids=_A						,				labels=_A						,				)
									self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_choices)		)






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







									"""simple docstring"""


									snake_case_						:     Dict     =		self.prepare_config_and_inputs()
									(
									    snake_case_
									)						:     Any     =		config_and_inputs
									snake_case_						:     Dict     =		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
									return config, inputs_dict
@require_torch
class SCREAMING_SNAKE_CASE_  (   snake_case_    ,			snake_case_    ,			unittest.TestCase   ):
						__magic_name__:     List[str]	 =    (
						    (
						        MobileBertModel,
						        MobileBertForMaskedLM,
						        MobileBertForMultipleChoice,
						        MobileBertForNextSentencePrediction,
						        MobileBertForPreTraining,
						        MobileBertForQuestionAnswering,
						        MobileBertForSequenceClassification,
						        MobileBertForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						__magic_name__:     Optional[Any]	 =    (
						    {
						        "feature-extraction": MobileBertModel,
						        "fill-mask": MobileBertForMaskedLM,
						        "question-answering": MobileBertForQuestionAnswering,
						        "text-classification": MobileBertForSequenceClassification,
						        "token-classification": MobileBertForTokenClassification,
						        "zero-shot": MobileBertForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						__magic_name__:     List[Any]	 =    True






						def UpperCAmelCase_			( self : Optional[Any]						,				_A : int						,				_A : str						,				_A : Union[str, Any]=False		)							->      List[Any]:







									"""simple docstring"""


									snake_case_						:     Optional[int]     =		super()._prepare_for_class(_A						,				_A						,				return_labels=_A		)

									if return_labels:
												if model_class in get_values(_A		):
															snake_case_						:     Optional[Any]     =		torch.zeros(
															    (self.model_tester.batch_size, self.model_tester.seq_length)						,				dtype=torch.long						,				device=_A		)
															snake_case_						:     Any     =		torch.zeros(
															    self.model_tester.batch_size						,				dtype=torch.long						,				device=_A		)
									return inputs_dict






						def UpperCAmelCase_			( self : List[str]		)							->      Optional[Any]:







									"""simple docstring"""


									snake_case_						:     Tuple     =		MobileBertModelTester(self		)
									snake_case_						:     Optional[Any]     =		ConfigTester(self						,				config_class=_A						,				hidden_size=37		)






						def UpperCAmelCase_			( self : Optional[Any]		)							->      Any:







									"""simple docstring"""


									self.config_tester.run_common_tests()






						def UpperCAmelCase_			( self : str		)							->      Optional[int]:







									"""simple docstring"""


									snake_case_						:     List[Any]     =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_model(*_A		)






						def UpperCAmelCase_			( self : int		)							->      int:







									"""simple docstring"""


									snake_case_						:     Any     =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_masked_lm(*_A		)






						def UpperCAmelCase_			( self : int		)							->      Any:







									"""simple docstring"""


									snake_case_						:     Optional[int]     =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_A		)






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







									"""simple docstring"""


									snake_case_						:     str     =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_A		)






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







									"""simple docstring"""


									snake_case_						:     str     =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_pretraining(*_A		)






						def UpperCAmelCase_			( self : Optional[int]		)							->      Any:







									"""simple docstring"""


									snake_case_						:     Tuple     =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_question_answering(*_A		)






						def UpperCAmelCase_			( self : Any		)							->      Optional[int]:







									"""simple docstring"""


									snake_case_						:     Dict     =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_A		)






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







									"""simple docstring"""


									snake_case_						:     List[str]     =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_token_classification(*_A		)


def 							SCREAMING_SNAKE_CASE__  (      __a				):
			return torch.tensor(
			    __a				,	dtype=torch.long				,	device=__a				,	)


_SCREAMING_SNAKE_CASE 		=						1e-3


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






						@slow
						def UpperCAmelCase_			( self : Dict		)							->      Optional[Any]:







									"""simple docstring"""


									snake_case_						:     Optional[Any]     =		MobileBertModel.from_pretrained('google/mobilebert-uncased'		).to(_A		)
									snake_case_						:     Tuple     =		_long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]		)
									with torch.no_grad():
												snake_case_						:     Union[str, Any]     =		model(_A		)[0]
									snake_case_						:     Any     =		torch.Size((1, 9, 512)		)
									self.assertEqual(output.shape						,				_A		)
									snake_case_						:     Dict     =		torch.tensor(
									    [
									        [
									            [-2.473_6526E07, 8.269_1656E04, 1.652_1838E05],
									            [-5.754_1704E-01, 3.905_6022E00, 4.401_1507E00],
									            [2.604_7359E00, 1.567_7652E00, -1.732_4188E-01],
									        ]
									    ]						,				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
									snake_case_						:     List[Any]     =		torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE		)
									snake_case_						:     Optional[int]     =		torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE		)

									self.assertTrue(lower_bound and upper_bound		)








import argparse

from transformers import (
    TapasConfig,
    TapasForMaskedLM,
    TapasForQuestionAnswering,
    TapasForSequenceClassification,
    TapasModel,
    TapasTokenizer,
    load_tf_weights_in_tapas,
)
from transformers.utils import logging


logging.set_verbosity_info()


def 							SCREAMING_SNAKE_CASE__  (      __a				,	__a				,	__a				,	__a				,	__a				):
	# Initialise PyTorch model.
	# If you want to convert a checkpoint that uses absolute position embeddings, make sure to set reset_position_index_per_cell of
	# TapasConfig to False.

	# initialize configuration from json file
	snake_case_						:     Optional[int]     =		TapasConfig.from_json_file(__a				)
	# set absolute/relative position embeddings parameter
	snake_case_						:     List[Any]     =		reset_position_index_per_cell

	# set remaining parameters of TapasConfig as well as the model based on the task
	if task == "SQA":
		snake_case_						:     int     =		TapasForQuestionAnswering(config=__a				)
	elif task == "WTQ":
		# run_task_main.py hparams
		snake_case_						:     Optional[int]     =		4
		snake_case_						:     List[str]     =		True
		# hparam_utils.py hparams
		snake_case_						:     Optional[Any]     =		0.664694
		snake_case_						:     Dict     =		0.207951
		snake_case_						:     Tuple     =		0.121194
		snake_case_						:     Dict     =		True
		snake_case_						:     int     =		True
		snake_case_						:     int     =		False
		snake_case_						:     str     =		0.0352513

		snake_case_						:     int     =		TapasForQuestionAnswering(config=__a				)
	elif task == "WIKISQL_SUPERVISED":
		# run_task_main.py hparams
		snake_case_						:     int     =		4
		snake_case_						:     Optional[int]     =		False
		# hparam_utils.py hparams
		snake_case_						:     str     =		36.4519
		snake_case_						:     Optional[Any]     =		0.903421
		snake_case_						:     List[Any]     =		222.088
		snake_case_						:     Optional[int]     =		True
		snake_case_						:     Optional[Any]     =		True
		snake_case_						:     str     =		True
		snake_case_						:     int     =		0.763141

		snake_case_						:     str     =		TapasForQuestionAnswering(config=__a				)
	elif task == "TABFACT":
		snake_case_						:     List[Any]     =		TapasForSequenceClassification(config=__a				)
	elif task == "MLM":
		snake_case_						:     Optional[int]     =		TapasForMaskedLM(config=__a				)
	elif task == "INTERMEDIATE_PRETRAINING":
		snake_case_						:     Tuple     =		TapasModel(config=__a				)
	else:
		raise ValueError(f"""Task {task} not supported."""				)

	print(f"""Building PyTorch model from configuration: {config}"""				)
	# Load weights from tf checkpoint
	load_tf_weights_in_tapas(__a				,	__a				,	__a				)

	# Save pytorch-model (weights and configuration)
	print(f"""Save PyTorch model to {pytorch_dump_path}"""				)
	model.save_pretrained(__a				)

	# Save tokenizer files
	print(f"""Save tokenizer files to {pytorch_dump_path}"""				)
	snake_case_						:     str     =		TapasTokenizer(vocab_file=tf_checkpoint_path[:-10] + 'vocab.txt'				,	model_max_length=5_12				)
	tokenizer.save_pretrained(__a				)

	print('Used relative position embeddings:'				,	model.config.reset_position_index_per_cell				)


if __name__ == "__main__":
						_SCREAMING_SNAKE_CASE 		=						argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    """--task""", default="""SQA""", type=str, help="""Model task for which to convert a checkpoint. Defaults to SQA."""
						)
						parser.add_argument(
						    """--reset_position_index_per_cell""",
						    default=False,
						    action="""store_true""",
						    help="""Whether to use relative position embeddings or not. Defaults to True.""",
						)
						parser.add_argument(
						    """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
						)
						parser.add_argument(
						    """--tapas_config_file""",
						    default=None,
						    type=str,
						    required=True,
						    help=(
						        """The config json file corresponding to the pre-trained TAPAS model. \n"""
						        """This specifies the model architecture."""
						    ),
						)
						parser.add_argument(
						    """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
						)
						_SCREAMING_SNAKE_CASE 		=						parser.parse_args()
						convert_tf_checkpoint_to_pytorch(
						    args.task,
						    args.reset_position_index_per_cell,
						    args.tf_checkpoint_path,
						    args.tapas_config_file,
						    args.pytorch_dump_path,
						)

