
'''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__      :







				def __init__(       self						,				A_						,				A_=13						,				A_=7						,				A_=True						,				A_=True						,				A_=True						,				A_=True						,				A_=99						,				A_=32						,				A_=5						,				A_=4						,				A_=4						,				A_="gelu"						,				A_=0.0						,				A_=0.1						,				A_=True						,				A_=512						,				A_=16						,				A_=2						,				A_=0.02						,				A_=3						,				A_=4						,				A_=None						,				)->		int:







											'''simple docstring'''






											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   			=     num_hidden_layers
											UpperCamelCase   			=     num_attention_heads
											UpperCamelCase   			=     intermediate_multiple_size
											UpperCamelCase   			=     hidden_act
											UpperCamelCase   			=     hidden_dropout
											UpperCamelCase   			=     attention_dropout
											UpperCamelCase   			=     weight_tying
											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 						UpperCAmelCase_    (       self     )->		Tuple:







											'''simple docstring'''






											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_labels:
																		UpperCamelCase   			=     ids_tensor([self.batch_size, self.seq_length]						,				self.num_labels     )

											UpperCamelCase   			=     self.get_config()

											return config, input_ids, input_mask, token_labels







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







											'''simple docstring'''






											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     )->		List[str]:







											'''simple docstring'''






											UpperCamelCase			,	UpperCamelCase			,	UpperCamelCase			,	UpperCamelCase   			=     self.prepare_config_and_inputs()

											UpperCamelCase   			=     True

											return config, input_ids, input_mask, token_labels







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







											'''simple docstring'''






											UpperCamelCase   			=     GPTNeoXJapaneseModel(config=A_     )
											model.to(A_     )
											model.eval()
											UpperCamelCase   			=     model(A_						,				attention_mask=A_     )
											UpperCamelCase   			=     model(A_     )
											self.parent.assertEqual(result.last_hidden_state.shape						,				(self.batch_size, self.seq_length, self.hidden_size)     )







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







											'''simple docstring'''






											UpperCamelCase   			=     True
											UpperCamelCase   			=     GPTNeoXJapaneseModel(A_     )
											model.to(A_     )
											model.eval()
											UpperCamelCase   			=     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						,				A_						,				A_						,				A_						,				A_     )->		List[Any]:







											'''simple docstring'''






											UpperCamelCase   			=     GPTNeoXJapaneseForCausalLM(config=A_     )
											model.to(A_     )
											model.eval()
											UpperCamelCase   			=     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						,				A_						,				A_						,				A_     )->		Optional[int]:







											'''simple docstring'''






											UpperCamelCase   			=     True
											UpperCamelCase   			=     GPTNeoXJapaneseForCausalLM(config=A_     )
											model.to(A_     )
											model.eval()

											# first forward pass
											UpperCamelCase   			=     model(A_						,				attention_mask=A_						,				use_cache=A_     )
											UpperCamelCase   			=     outputs.past_key_values

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

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

											UpperCamelCase   			=     model(A_						,				attention_mask=A_						,				output_hidden_states=A_     )
											UpperCamelCase   			=     output_from_no_past['hidden_states'][0]
											UpperCamelCase   			=     model(
											    A_						,				attention_mask=A_						,				past_key_values=A_						,				output_hidden_states=A_						,				)['hidden_states'][0]

											# select random slice
											UpperCamelCase   			=     ids_tensor((1,)						,				output_from_past.shape[-1]     ).item()
											UpperCamelCase   			=     output_from_no_past[:, -3:, random_slice_idx].detach()
											UpperCamelCase   			=     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     )->		Union[str, Any]:







											'''simple docstring'''






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


@require_torch
class 			SCREAMING_SNAKE_CASE__      (     snake_case_							,       snake_case_							,       unittest.TestCase):
				lowerCAmelCase_     			=							(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
				lowerCAmelCase_     			=							(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
				lowerCAmelCase_     			=							(
				    {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM}
				    if is_torch_available()
				    else {}
				)
				lowerCAmelCase_     			=							False
				lowerCAmelCase_     			=							False
				lowerCAmelCase_     			=							False
				lowerCAmelCase_     			=							False







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







											'''simple docstring'''






											UpperCamelCase   			=     GPTNeoXJapaneseModelTester(self     )
											UpperCamelCase   			=     ConfigTester(self						,				config_class=A_						,				hidden_size=37     )







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







											'''simple docstring'''






											self.config_tester.run_common_tests()







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







											'''simple docstring'''






											UpperCamelCase			,	UpperCamelCase			,	UpperCamelCase			,	UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(A_						,				A_						,				A_     )







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







											'''simple docstring'''






											UpperCamelCase			,	UpperCamelCase			,	UpperCamelCase			,	UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_model_as_decoder(A_						,				A_						,				A_     )







				def 						UpperCAmelCase_    (       self     )->		Dict:







											'''simple docstring'''






											UpperCamelCase			,	UpperCamelCase			,	UpperCamelCase			,	UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs_for_decoder()

											UpperCamelCase   			=     None

											self.model_tester.create_and_check_model_as_decoder(A_						,				A_						,				A_     )







				def 						UpperCAmelCase_    (       self     )->		Dict:







											'''simple docstring'''






											UpperCamelCase			,	UpperCamelCase			,	UpperCamelCase			,	UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_decoder_model_past_large_inputs(A_						,				A_						,				A_     )







				def 						UpperCAmelCase_    (       self     )->		Any:







											'''simple docstring'''






											UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_causal_lm(*A_     )







				@slow
				def 						UpperCAmelCase_    (       self     )->		Tuple:







											'''simple docstring'''






											UpperCamelCase   			=     'abeja/gpt-neox-japanese-2.7b'

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

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

											UpperCamelCase   			=     GPTNeoXJapaneseTokenizer.from_pretrained(A_     )
											UpperCamelCase   			=     GPTNeoXJapaneseForCausalLM.from_pretrained(A_     )

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






'''simple docstring'''
def 		A_(     A						:		list[int]):
							UpperCamelCase   			=     []
							if len(A) == 1:
														return [nums.copy()]
							for _ in range(len(A)):
														UpperCamelCase   			=     nums.pop(0)
														UpperCamelCase   			=     permute(A)
														for perm in permutations:
																					perm.append(A)
														result.extend(A)
														nums.append(A)
							return result







def 		A_(     A						:		str):

							def backtrack(A						:		str):
														if start == len(A) - 1:
																					output.append(nums[:])
														else:
																					for i in range(A		,     len(A)):
																												UpperCamelCase			,	UpperCamelCase   			=     nums[i], nums[start]
																												backtrack(start + 1)
																												UpperCamelCase			,	UpperCamelCase   			=     nums[i], nums[start]  # backtrack

							UpperCamelCase   			=     []
							backtrack(0)
							return output


if __name__ == "__main__":
						import doctest

						# use res to print the data in permute2 function
						lowerCAmelCase					:    Dict           = permutea([1, 2, 3])
						print(res)
						doctest.testmod()




