




"""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 					lowercase:







 '''simple docstring'''





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




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  parent
   _snake_case							:		Optional[int]								=  batch_size
   _snake_case							:		str								=  seq_length
   _snake_case							:		Dict								=  is_training
   _snake_case							:		Optional[int]								=  use_input_mask
   _snake_case							:		Dict								=  use_token_type_ids
   _snake_case							:		str								=  use_labels
   _snake_case							:		Tuple								=  vocab_size
   _snake_case							:		int								=  hidden_size
   _snake_case							:		List[Any]								=  num_hidden_layers
   _snake_case							:		List[str]								=  num_attention_heads
   _snake_case							:		str								=  intermediate_multiple_size
   _snake_case							:		List[str]								=  hidden_act
   _snake_case							:		Dict								=  hidden_dropout
   _snake_case							:		List[Any]								=  attention_dropout
   _snake_case							:		Optional[Any]								=  weight_tying
   _snake_case							:		int								=  max_position_embeddings
   _snake_case							:		List[Any]								=  type_vocab_size
   _snake_case							:		int								=  type_sequence_label_size
   _snake_case							:		Any								=  initializer_range
   _snake_case							:		str								=  num_labels
   _snake_case							:		Tuple								=  num_choices
   _snake_case							:		Optional[Any]								=  scope


 def  UpperCamelCase_   (				self:		str		):




   '''simple docstring'''



   _snake_case							:		Dict								=  ids_tensor([self.batch_size, self.seq_length],      self.vocab_size		)

   _snake_case							:		str								=  None
   if self.use_input_mask:
     _snake_case							:		Optional[Any]								=  random_attention_mask([self.batch_size, self.seq_length]		)

   _snake_case							:		Optional[int]								=  None
   if self.use_labels:
     _snake_case							:		Union[str, Any]								=  ids_tensor([self.batch_size, self.seq_length],      self.num_labels		)

   _snake_case							:		List[str]								=  self.get_config()

   return config, input_ids, input_mask, token_labels


 def  UpperCamelCase_   (				self:		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:		Dict		):




   '''simple docstring'''



   _snake_case		,					_snake_case		,					_snake_case		,					_snake_case							:		List[str]								=  self.prepare_config_and_inputs()

   _snake_case							:		Optional[Any]								=  True

   return config, input_ids, input_mask, token_labels


 def  UpperCamelCase_   (				self:		List[str],      a_:		Optional[int],      a_:		Optional[int],      a_:		Optional[int]		):




   '''simple docstring'''



   _snake_case							:		List[str]								=  GPTNeoXJapaneseModel(config=a_		)
   model.to(a_		)
   model.eval()
   _snake_case							:		List[str]								=  model(a_,      attention_mask=a_		)
   _snake_case							:		Union[str, Any]								=  model(a_		)
   self.parent.assertEqual(result.last_hidden_state.shape,      (self.batch_size, self.seq_length, self.hidden_size)		)


 def  UpperCamelCase_   (				self:		str,      a_:		Tuple,      a_:		Optional[int],      a_:		str		):




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  True
   _snake_case							:		int								=  GPTNeoXJapaneseModel(a_		)
   model.to(a_		)
   model.eval()
   _snake_case							:		Optional[int]								=  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:		List[str],      a_:		int,      a_:		Union[str, Any],      a_:		Any,      a_:		List[str]		):




   '''simple docstring'''



   _snake_case							:		List[str]								=  GPTNeoXJapaneseForCausalLM(config=a_		)
   model.to(a_		)
   model.eval()
   _snake_case							:		Optional[int]								=  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:		int,      a_:		Optional[int],      a_:		str,      a_:		Optional[int]		):




   '''simple docstring'''



   _snake_case							:		Tuple								=  True
   _snake_case							:		Optional[Any]								=  GPTNeoXJapaneseForCausalLM(config=a_		)
   model.to(a_		)
   model.eval()

   # first forward pass
   _snake_case							:		List[Any]								=  model(a_,      attention_mask=a_,      use_cache=a_		)
   _snake_case							:		List[str]								=  outputs.past_key_values

   # create hypothetical multiple next token and extent to next_input_ids
   _snake_case							:		List[Any]								=  ids_tensor((self.batch_size, 3),      config.vocab_size		)
   _snake_case							:		Dict								=  ids_tensor((self.batch_size, 3),      vocab_size=2		)

   # append to next input_ids and
   _snake_case							:		Dict								=  torch.cat([input_ids, next_tokens],      dim=-1		)
   _snake_case							:		Optional[int]								=  torch.cat([input_mask, next_mask],      dim=-1		)

   _snake_case							:		Union[str, Any]								=  model(a_,      attention_mask=a_,      output_hidden_states=a_		)
   _snake_case							:		Optional[Any]								=  output_from_no_past["""hidden_states"""][0]
   _snake_case							:		Optional[int]								=  model(
       a_,      attention_mask=a_,      past_key_values=a_,      output_hidden_states=a_,      )["""hidden_states"""][0]

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




   '''simple docstring'''



   _snake_case							:		Tuple								=  self.prepare_config_and_inputs()
   _snake_case		,					_snake_case		,					_snake_case		,					_snake_case							:		Any								=  config_and_inputs
   _snake_case							:		Optional[int]								=  {"""input_ids""": input_ids, """attention_mask""": input_mask}
   return config, inputs_dict







@require_torch
class 					lowercase(     __a       ,			__a       ,			unittest.TestCase ):







 '''simple docstring'''





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


 def  UpperCamelCase_   (				self:		Tuple		):




   '''simple docstring'''



   _snake_case							:		List[str]								=  GPTNeoXJapaneseModelTester(self		)
   _snake_case							:		Union[str, Any]								=  ConfigTester(self,      config_class=a_,      hidden_size=37		)


 def  UpperCamelCase_   (				self:		List[str]		):




   '''simple docstring'''



   self.config_tester.run_common_tests()


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




   '''simple docstring'''



   _snake_case		,					_snake_case		,					_snake_case		,					_snake_case							:		Dict								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(a_,      a_,      a_		)


 def  UpperCamelCase_   (				self:		int		):




   '''simple docstring'''



   _snake_case		,					_snake_case		,					_snake_case		,					_snake_case							:		Tuple								=  self.model_tester.prepare_config_and_inputs_for_decoder()
   self.model_tester.create_and_check_model_as_decoder(a_,      a_,      a_		)


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




   '''simple docstring'''



   _snake_case		,					_snake_case		,					_snake_case		,					_snake_case							:		int								=  self.model_tester.prepare_config_and_inputs_for_decoder()

   _snake_case							:		Union[str, Any]								=  None

   self.model_tester.create_and_check_model_as_decoder(a_,      a_,      a_		)


 def  UpperCamelCase_   (				self:		List[str]		):




   '''simple docstring'''



   _snake_case		,					_snake_case		,					_snake_case		,					_snake_case							:		Union[str, Any]								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_decoder_model_past_large_inputs(a_,      a_,      a_		)


 def  UpperCamelCase_   (				self:		Optional[int]		):




   '''simple docstring'''



   _snake_case							:		Tuple								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_causal_lm(*a_		)


 @slow
 def  UpperCamelCase_   (				self:		int		):




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  """abeja/gpt-neox-japanese-2.7b"""

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

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

   _snake_case							:		Union[str, Any]								=  GPTNeoXJapaneseTokenizer.from_pretrained(a_		)
   _snake_case							:		str								=  GPTNeoXJapaneseForCausalLM.from_pretrained(a_		)

   _snake_case							:		Tuple								=  []
   for prompt in prompts:
     _snake_case							:		List[Any]								=  tokenizer(a_,      return_tensors="""pt"""		).input_ids
     _snake_case							:		Tuple								=  model.generate(a_,      max_length=50		)
     _snake_case							:		str								=  tokenizer.batch_decode(a_,      skip_special_tokens=a_		)
     predicted_outputs += generated_string
   self.assertListEqual(a_,      a_		)








"""simple docstring"""


import os
from typing import Dict, List, Tuple, TypeVar, Union


A_						=				TypeVar('''T''')

A_						=				Union[List[T], Tuple[T, ...]]
A_						=				Union[T, List[T], Dict[str, T]]
A_						=				Union[str, bytes, os.PathLike]


