





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


  '''simple docstring'''







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


    '''simple docstring'''





    _snake_case   :				int														=	parent
    _snake_case   :				List[Any]														=	batch_size
    _snake_case   :				Optional[int]														=	seq_length
    _snake_case   :				Tuple														=	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   :				Tuple														=	hidden_size
    _snake_case   :				Union[str, Any]														=	num_hidden_layers
    _snake_case   :				str														=	num_attention_heads
    _snake_case   :				List[Any]														=	intermediate_multiple_size
    _snake_case   :				Any														=	hidden_act
    _snake_case   :				Tuple														=	hidden_dropout
    _snake_case   :				int														=	attention_dropout
    _snake_case   :				int														=	weight_tying
    _snake_case   :				Union[str, Any]														=	max_position_embeddings
    _snake_case   :				Union[str, Any]														=	type_vocab_size
    _snake_case   :				int														=	type_sequence_label_size
    _snake_case   :				List[str]														=	initializer_range
    _snake_case   :				int														=	num_labels
    _snake_case   :				Any														=	num_choices
    _snake_case   :				Tuple														=	scope







  def UpperCamelCase_ (      self      :					Any							):


    '''simple docstring'''





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

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

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

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

    return config, input_ids, input_mask, token_labels







  def UpperCamelCase_ (      self      :					Union[str, 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=lowerCAmelCase_      ,				initializer_range=self.initializer_range      ,				)







  def UpperCamelCase_ (      self      :					Optional[Any]							):


    '''simple docstring'''





    _snake_case							,					_snake_case							,					_snake_case							,					_snake_case   :				str														=	self.prepare_config_and_inputs()

    _snake_case   :				Any														=	True

    return config, input_ids, input_mask, token_labels







  def UpperCamelCase_ (      self      :					Optional[Any]      ,				UpperCamelCase      :					Optional[int]      ,				UpperCamelCase      :					Any      ,				UpperCamelCase      :					int							):


    '''simple docstring'''





    _snake_case   :				str														=	GPTNeoXJapaneseModel(config=lowerCAmelCase_							)
    model.to(lowerCAmelCase_							)
    model.eval()
    _snake_case   :				Optional[int]														=	model(lowerCAmelCase_      ,				attention_mask=lowerCAmelCase_							)
    _snake_case   :				int														=	model(lowerCAmelCase_							)
    self.parent.assertEqual(result.last_hidden_state.shape      ,				(self.batch_size, self.seq_length, self.hidden_size)							)







  def UpperCamelCase_ (      self      :					Optional[Any]      ,				UpperCamelCase      :					Any      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					List[str]							):


    '''simple docstring'''





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







  def UpperCamelCase_ (      self      :					Dict      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					int      ,				UpperCamelCase      :					List[Any]							):


    '''simple docstring'''





    _snake_case   :				str														=	GPTNeoXJapaneseForCausalLM(config=lowerCAmelCase_							)
    model.to(lowerCAmelCase_							)
    model.eval()
    _snake_case   :				List[str]														=	model(lowerCAmelCase_      ,				attention_mask=lowerCAmelCase_      ,				labels=lowerCAmelCase_							)
    self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.seq_length, self.vocab_size)							)







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


    '''simple docstring'''





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

    # first forward pass
    _snake_case   :				int														=	model(lowerCAmelCase_      ,				attention_mask=lowerCAmelCase_      ,				use_cache=lowerCAmelCase_							)
    _snake_case   :				Tuple														=	outputs.past_key_values

    # create hypothetical multiple next token and extent to next_input_ids
    _snake_case   :				Dict														=	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   :				List[Any]														=	torch.cat([input_ids, next_tokens]      ,				dim=-1							)
    _snake_case   :				Tuple														=	torch.cat([input_mask, next_mask]      ,				dim=-1							)

    _snake_case   :				str														=	model(lowerCAmelCase_      ,				attention_mask=lowerCAmelCase_      ,				output_hidden_states=lowerCAmelCase_							)
    _snake_case   :				Optional[Any]														=	output_from_no_past['hidden_states'][0]
    _snake_case   :				Union[str, Any]														=	model(
        lowerCAmelCase_      ,				attention_mask=lowerCAmelCase_      ,				past_key_values=lowerCAmelCase_      ,				output_hidden_states=lowerCAmelCase_      ,				)['hidden_states'][0]

    # select random slice
    _snake_case   :				Optional[Any]														=	ids_tensor((1,)      ,				output_from_past.shape[-1]							).item()
    _snake_case   :				Optional[int]														=	output_from_no_past[:, -3:, random_slice_idx].detach()
    _snake_case   :				Optional[int]														=	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(lowerCAmelCase_      ,				lowerCAmelCase_      ,				atol=1e-3							)							)







  def UpperCamelCase_ (      self      :					str							):


    '''simple docstring'''





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




@require_torch
class        _lowerCAmelCase    (			__lowerCAmelCase							,    __lowerCAmelCase							,    unittest.TestCase							):


  '''simple docstring'''
  a_							:       str      					=(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
  a_							:       str      					=(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
  a_							:       Dict      					=(
      {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
      if is_torch_available()
      else {}
  )
  a_							:       Any      					=False
  a_							:       List[Any]      					=False
  a_							:       Union[str, Any]      					=False
  a_							:       str      					=False







  def UpperCamelCase_ (      self      :					List[str]							):


    '''simple docstring'''





    _snake_case   :				str														=	GPTNeoXJapaneseModelTester(self							)
    _snake_case   :				Optional[int]														=	ConfigTester(self      ,				config_class=lowerCAmelCase_      ,				hidden_size=37							)







  def UpperCamelCase_ (      self      :					Optional[int]							):


    '''simple docstring'''





    self.config_tester.run_common_tests()







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


    '''simple docstring'''





    _snake_case							,					_snake_case							,					_snake_case							,					_snake_case   :				List[Any]														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(lowerCAmelCase_      ,				lowerCAmelCase_      ,				lowerCAmelCase_							)







  def UpperCamelCase_ (      self      :					Optional[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(lowerCAmelCase_      ,				lowerCAmelCase_      ,				lowerCAmelCase_							)







  def UpperCamelCase_ (      self      :					Optional[int]							):


    '''simple docstring'''





    _snake_case							,					_snake_case							,					_snake_case							,					_snake_case   :				Optional[int]														=	self.model_tester.prepare_config_and_inputs_for_decoder()

    _snake_case   :				Dict														=	None

    self.model_tester.create_and_check_model_as_decoder(lowerCAmelCase_      ,				lowerCAmelCase_      ,				lowerCAmelCase_							)







  def UpperCamelCase_ (      self      :					Optional[Any]							):


    '''simple docstring'''





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







  def UpperCamelCase_ (      self      :					List[Any]							):


    '''simple docstring'''





    _snake_case   :				Dict														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_causal_lm(*lowerCAmelCase_							)







  @slow
  def UpperCamelCase_ (      self      :					Tuple							):


    '''simple docstring'''





    _snake_case   :				List[Any]														=	'abeja/gpt-neox-japanese-2.7b'

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

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

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

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











def     lowerCamelCase_	(      lowerCAmelCase:	int				)->		int:

  if not isinstance(lowerCAmelCase    ,							lowerCAmelCase				):
    _snake_case   :				Union[str, Any]														=	F"""Input value of [number={number}] must be an integer"""
    raise TypeError(lowerCAmelCase				)

  if number < 1:
    _snake_case   :				int														=	F"""Input value of [number={number}] must be > 0"""
    raise ValueError(lowerCAmelCase				)

  _snake_case   :				int														=	1

  for i in range(1    ,							lowerCAmelCase				):
    current_number *= 4 * i - 2
    current_number //= i + 1

  return current_number


if __name__ == "__main__":
      import doctest

      doctest.testmod()




