





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



    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.0_2     ,			__A=3     ,			__A=4     ,			__A=None     ,			):
     __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 					__lowerCamelCase						(  self					):
     __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 					__lowerCamelCase						(  self					):
     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 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					=   self.prepare_config_and_inputs()

     __UpperCAmelCase					=   True

     return config, input_ids, input_mask, token_labels



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A					):
     __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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A					):
     __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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A					):
     __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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A					):
     __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 					__lowerCamelCase						(  self					):
     __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 UpperCAmelCase       (		UpperCAmelCase_					,			UpperCAmelCase_					,			unittest.TestCase							):
    _A :			List[str]						   =       (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
    _A :			List[Any]						   =       (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
    _A :			str						   =       (
        {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM}
        if is_torch_available()
        else {}
    )
    _A :			str						   =       False
    _A :			str						   =       False
    _A :			int						   =       False
    _A :			str						   =       False



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   GPTNeoXJapaneseModelTester(self					)
     __UpperCAmelCase					=   ConfigTester(self     ,			config_class=__A     ,			hidden_size=37					)



    def 					__lowerCamelCase						(  self					):
     self.config_tester.run_common_tests()



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(__A     ,			__A     ,			__A					)



    def 					__lowerCamelCase						(  self					):
     __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 					__lowerCamelCase						(  self					):
     # This regression test was failing with PyTorch < 1.3
     __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 					__lowerCamelCase						(  self					):
     __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 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_causal_lm(*__A					)




    @slow
    def 					__lowerCamelCase						(  self					):
     __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'''




from __future__ import annotations







def _lowerCAmelCase     (      _lowerCAmelCase    )->     int:
 if not nums:
  return 0
 __UpperCAmelCase					=   nums[0]
 __UpperCAmelCase					=   0
 for num in nums[1:]:
  __UpperCAmelCase					,    __UpperCAmelCase					=   (
      max_excluding + num,
      max(_lowerCAmelCase       ,    _lowerCAmelCase    ),
  )
 return max(_lowerCAmelCase       ,    _lowerCAmelCase    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()





