





'''simple docstring'''







import unittest

from transformers import GPTNeoXJapaneseConfig, is_torch_available
from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer
from transformers.testing_utils import 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 GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel
class 				__SCREAMING_SNAKE_CASE  :






				"""simple docstring"""







				def __init__(  self     :					Union[str, Any]					,				__a     :					Tuple					,				__a     :					List[str]=13					,				__a     :					int=7					,				__a     :					List[str]=True					,				__a     :					Dict=True					,				__a     :					Optional[int]=True					,				__a     :					Union[str, Any]=True					,				__a     :					Optional[int]=99					,				__a     :					str=32					,				__a     :					Dict=5					,				__a     :					Optional[Any]=4					,				__a     :					List[str]=4					,				__a     :					Dict="gelu"					,				__a     :					Any=0.0					,				__a     :					List[str]=0.1					,				__a     :					int=True					,				__a     :					Optional[Any]=5_12					,				__a     :					int=16					,				__a     :					Union[str, Any]=2					,				__a     :					Any=0.02					,				__a     :					Any=3					,				__a     :					int=4					,				__a     :					Optional[Any]=None					,				):
								_a								= parent
								_a								= batch_size
								_a								= seq_length
								_a								= is_training
								_a								= use_input_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_multiple_size
								_a								= hidden_act
								_a								= hidden_dropout
								_a								= attention_dropout
								_a								= weight_tying
								_a								= max_position_embeddings
								_a								= type_vocab_size
								_a								= type_sequence_label_size
								_a								= initializer_range
								_a								= num_labels
								_a								= num_choices
								_a								= scope




				def   UpperCamelCase__   (  self     :					Optional[Any]					):
								_a								= ids_tensor([self.batch_size, self.seq_length]					,				self.vocab_size					)

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

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

								_a								= self.get_config()

								return config, input_ids, input_mask, token_labels




				def   UpperCamelCase__   (  self     :					Optional[int]					):
								return GPTNeoXJapaneseConfig(
								    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_multiple_size=self.intermediate_multiple_size					,				hidden_act=self.hidden_act					,				hidden_dropout=self.hidden_dropout					,				attention_dropout=self.attention_dropout					,				weight_tying=self.weight_tying					,				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     :					Tuple					):
								_a	,					_a	,					_a	,					_a								= self.prepare_config_and_inputs()

								_a								= True

								return config, input_ids, input_mask, token_labels




				def   UpperCamelCase__   (  self     :					int					,				__a     :					Optional[Any]					,				__a     :					Dict					,				__a     :					str					):
								_a								= GPTNeoXJapaneseModel(config=__a					)
								model.to(__a					)
								model.eval()
								_a								= model(__a					,				attention_mask=__a					)
								_a								= model(__a					)
								self.parent.assertEqual(result.last_hidden_state.shape					,				(self.batch_size, self.seq_length, self.hidden_size)					)




				def   UpperCamelCase__   (  self     :					Optional[Any]					,				__a     :					str					,				__a     :					int					,				__a     :					int					):
								_a								= True
								_a								= GPTNeoXJapaneseModel(__a					)
								model.to(__a					)
								model.eval()
								_a								= model(__a					,				attention_mask=__a					)
								self.parent.assertEqual(result.last_hidden_state.shape					,				(self.batch_size, self.seq_length, self.hidden_size)					)




				def   UpperCamelCase__   (  self     :					Optional[Any]					,				__a     :					Optional[int]					,				__a     :					int					,				__a     :					Optional[int]					,				__a     :					List[str]					):
								_a								= GPTNeoXJapaneseForCausalLM(config=__a					)
								model.to(__a					)
								model.eval()
								_a								= model(__a					,				attention_mask=__a					,				labels=__a					)
								self.parent.assertEqual(result.logits.shape					,				(self.batch_size, self.seq_length, self.vocab_size)					)




				def   UpperCamelCase__   (  self     :					str					,				__a     :					Union[str, Any]					,				__a     :					Optional[Any]					,				__a     :					Optional[int]					):
								_a								= True
								_a								= GPTNeoXJapaneseForCausalLM(config=__a					)
								model.to(__a					)
								model.eval()

								# first forward pass
								_a								= model(__a					,				attention_mask=__a					,				use_cache=__a					)
								_a								= outputs.past_key_values

								# create hypothetical multiple next token and extent to next_input_ids
								_a								= ids_tensor((self.batch_size, 3)					,				config.vocab_size					)
								_a								= ids_tensor((self.batch_size, 3)					,				vocab_size=2					)

								# append to next input_ids and
								_a								= torch.cat([input_ids, next_tokens]					,				dim=-1					)
								_a								= torch.cat([input_mask, next_mask]					,				dim=-1					)

								_a								= model(__a					,				attention_mask=__a					,				output_hidden_states=__a					)
								_a								= output_from_no_past["hidden_states"][0]
								_a								= model(
								    __a					,				attention_mask=__a					,				past_key_values=__a					,				output_hidden_states=__a					,				)["hidden_states"][0]

								# select random slice
								_a								= ids_tensor((1,)					,				output_from_past.shape[-1]					).item()
								_a								= output_from_no_past[:, -3:, random_slice_idx].detach()
								_a								= output_from_past[:, :, random_slice_idx].detach()

								self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]					)

								# test that outputs are equal for slice
								self.parent.assertTrue(torch.allclose(__a					,				__a					,				atol=1e-3					)					)




				def   UpperCamelCase__   (  self     :					Optional[int]					):
								_a								= self.prepare_config_and_inputs()
								_a	,					_a	,					_a	,					_a								= config_and_inputs
								_a								= {"input_ids": input_ids, "attention_mask": input_mask}
								return config, inputs_dict


@require_torch
class 				__SCREAMING_SNAKE_CASE  (snake_case_       ,							snake_case_       ,							unittest.TestCase      ):






				"""simple docstring"""







				__a            =(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
				__a            =(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
				__a            =(
				    {'feature-extraction': GPTNeoXJapaneseModel, 'text-generation': GPTNeoXJapaneseForCausalLM}
				    if is_torch_available()
				    else {}
				)
				__a            =False
				__a            =False
				__a            =False
				__a            =False




				def   UpperCamelCase__   (  self     :					Union[str, Any]					):
								_a								= GPTNeoXJapaneseModelTester(self					)
								_a								= ConfigTester(self					,				config_class=__a					,				hidden_size=37					)




				def   UpperCamelCase__   (  self     :					Tuple					):
								self.config_tester.run_common_tests()




				def   UpperCamelCase__   (  self     :					Dict					):
								_a	,					_a	,					_a	,					_a								= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(__a					,				__a					,				__a					)




				def   UpperCamelCase__   (  self     :					Dict					):
								_a	,					_a	,					_a	,					_a								= self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_model_as_decoder(__a					,				__a					,				__a					)




				def   UpperCamelCase__   (  self     :					Any					):
								# This regression test was failing with PyTorch < 1.3
								_a	,					_a	,					_a	,					_a								= self.model_tester.prepare_config_and_inputs_for_decoder()

								_a								= None

								self.model_tester.create_and_check_model_as_decoder(__a					,				__a					,				__a					)




				def   UpperCamelCase__   (  self     :					Dict					):
								_a	,					_a	,					_a	,					_a								= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_decoder_model_past_large_inputs(__a					,				__a					,				__a					)




				def   UpperCamelCase__   (  self     :					Tuple					):
								_a								= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_causal_lm(*__a					)




				@slow
				def   UpperCamelCase__   (  self     :					Dict					):
								_a								= "abeja/gpt-neox-japanese-2.7b"

								_a								= ["データサイエンティストとは、", "100年後に必要とされる会社は、", "フルリモートの環境で働くために必要なことは、", "国境の長いトンネルを抜けると", "美味しい日本食といえば、"]

								_a								= [
								    "データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。",
								    "100年後に必要とされる会社は、「人」が中心の会社です。",
								    "フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。",
								    "国境の長いトンネルを抜けると、そこは雪国だった。",
								    "美味しい日本食といえば、やっぱりお寿司ですよね。",
								]

								_a								= GPTNeoXJapaneseTokenizer.from_pretrained(__a					)
								_a								= GPTNeoXJapaneseForCausalLM.from_pretrained(__a					)

								_a								= []
								for prompt in prompts:
												_a								= tokenizer(__a					,				return_tensors="pt"					).input_ids
												_a								= model.generate(__a					,				max_length=50					)
												_a								= tokenizer.batch_decode(__a					,				skip_special_tokens=__a					)
												predicted_outputs += generated_string
								self.assertListEqual(__a					,				__a					)








import argparse
import os

import torch

from transformers import (
    XLNetConfig,
    XLNetForQuestionAnswering,
    XLNetForSequenceClassification,
    XLNetLMHeadModel,
    load_tf_weights_in_xlnet,
)
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


lowercase  							=					{
    "cola": 2,
    "mnli": 3,
    "mrpc": 2,
    "sst-2": 2,
    "sts-b": 1,
    "qqp": 2,
    "qnli": 2,
    "rte": 2,
    "wnli": 2,
}


logging.set_verbosity_info()





def 						__UpperCAmelCase			(     a_							,     a_							,     a_							,     a_=None):
							# Initialise PyTorch model
							snake_case_				=							XLNetConfig.from_json_file(a_)

							snake_case_				=							finetuning_task.lower() if finetuning_task is not None else ''
							if finetuning_task in GLUE_TASKS_NUM_LABELS:
														print(f'''Building PyTorch XLNetForSequenceClassification model from configuration: {config}''')
														snake_case_				=							finetuning_task
														snake_case_				=							GLUE_TASKS_NUM_LABELS[finetuning_task]
														snake_case_				=							XLNetForSequenceClassification(a_)
							elif "squad" in finetuning_task:
														snake_case_				=							finetuning_task
														snake_case_				=							XLNetForQuestionAnswering(a_)
							else:
														snake_case_				=							XLNetLMHeadModel(a_)

							# Load weights from tf checkpoint
							load_tf_weights_in_xlnet(a_							,     a_							,     a_)

							# Save pytorch-model
							snake_case_				=							os.path.join(a_							,     a_)
							snake_case_				=							os.path.join(a_							,     a_)
							print(f'''Save PyTorch model to {os.path.abspath(a_)}''')
							torch.save(model.state_dict()							,     a_)
							print(f'''Save configuration file to {os.path.abspath(a_)}''')
							with open(a_							,     'w'							,     encoding='utf-8') as f:
														f.write(config.to_json_string())


if __name__ == "__main__":
	lowercase  							=					argparse.ArgumentParser()
	# Required parameters
	parser.add_argument(
	    "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
	)
	parser.add_argument(
	    "--xlnet_config_file",
	    default=None,
	    type=str,
	    required=True,
	    help=(
	        "The config json file corresponding to the pre-trained XLNet model. \n"
	        "This specifies the model architecture."
	    ),
	)
	parser.add_argument(
	    "--pytorch_dump_folder_path",
	    default=None,
	    type=str,
	    required=True,
	    help="Path to the folder to store the PyTorch model or dataset/vocab.",
	)
	parser.add_argument(
	    "--finetuning_task",
	    default=None,
	    type=str,
	    help="Name of a task on which the XLNet TensorFlow model was fine-tuned",
	)
	lowercase  							=					parser.parse_args()
	print(args)

	convert_xlnet_checkpoint_to_pytorch(
	    args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task
	)





