



'''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  ,__UpperCAmelCase  ,__UpperCAmelCase=13  ,__UpperCAmelCase=7  ,__UpperCAmelCase=True  ,__UpperCAmelCase=True  ,__UpperCAmelCase=True  ,__UpperCAmelCase=True  ,__UpperCAmelCase=99  ,__UpperCAmelCase=32  ,__UpperCAmelCase=5  ,__UpperCAmelCase=4  ,__UpperCAmelCase=4  ,__UpperCAmelCase="gelu"  ,__UpperCAmelCase=0.0  ,__UpperCAmelCase=0.1  ,__UpperCAmelCase=True  ,__UpperCAmelCase=512  ,__UpperCAmelCase=16  ,__UpperCAmelCase=2  ,__UpperCAmelCase=0.0_2  ,__UpperCAmelCase=3  ,__UpperCAmelCase=4  ,__UpperCAmelCase=None  ,)   ->		str:
      lowerCAmelCase__			:  Optional[int]											= parent
      lowerCAmelCase__			:  Any											= batch_size
      lowerCAmelCase__			:  Optional[Any]											= seq_length
      lowerCAmelCase__			:  Optional[int]											= is_training
      lowerCAmelCase__			:  Tuple											= use_input_mask
      lowerCAmelCase__			:  Optional[Any]											= use_token_type_ids
      lowerCAmelCase__			:  int											= use_labels
      lowerCAmelCase__			:  Union[str, Any]											= vocab_size
      lowerCAmelCase__			:  List[Any]											= hidden_size
      lowerCAmelCase__			:  Tuple											= num_hidden_layers
      lowerCAmelCase__			:  int											= num_attention_heads
      lowerCAmelCase__			:  Dict											= intermediate_multiple_size
      lowerCAmelCase__			:  Tuple											= hidden_act
      lowerCAmelCase__			:  Tuple											= hidden_dropout
      lowerCAmelCase__			:  List[str]											= attention_dropout
      lowerCAmelCase__			:  Optional[Any]											= weight_tying
      lowerCAmelCase__			:  List[Any]											= max_position_embeddings
      lowerCAmelCase__			:  Tuple											= type_vocab_size
      lowerCAmelCase__			:  Optional[Any]											= type_sequence_label_size
      lowerCAmelCase__			:  Dict											= initializer_range
      lowerCAmelCase__			:  List[Any]											= num_labels
      lowerCAmelCase__			:  Dict											= num_choices
      lowerCAmelCase__			:  List[str]											= scope




  def 							UpperCAmelCase_     (   self				)   ->		str:
      lowerCAmelCase__			:  Any											= ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size				)

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

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

      lowerCAmelCase__			:  Optional[int]											= self.get_config()

      return config, input_ids, input_mask, token_labels




  def 							UpperCAmelCase_     (   self				)   ->		Any:
      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=__UpperCAmelCase  ,initializer_range=self.initializer_range  ,)




  def 							UpperCAmelCase_     (   self				)   ->		Tuple:
      lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__			:  Optional[int]											= self.prepare_config_and_inputs()

      lowerCAmelCase__			:  List[Any]											= True

      return config, input_ids, input_mask, token_labels




  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)   ->		Union[str, Any]:
      lowerCAmelCase__			:  List[str]											= GPTNeoXJapaneseModel(config=__UpperCAmelCase				)
      model.to(__UpperCAmelCase				)
      model.eval()
      lowerCAmelCase__			:  Dict											= model(__UpperCAmelCase  ,attention_mask=__UpperCAmelCase				)
      lowerCAmelCase__			:  Union[str, Any]											= model(__UpperCAmelCase				)
      self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, self.seq_length, self.hidden_size)				)




  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)   ->		List[str]:
      lowerCAmelCase__			:  Union[str, Any]											= True
      lowerCAmelCase__			:  List[str]											= GPTNeoXJapaneseModel(__UpperCAmelCase				)
      model.to(__UpperCAmelCase				)
      model.eval()
      lowerCAmelCase__			:  str											= model(__UpperCAmelCase  ,attention_mask=__UpperCAmelCase				)
      self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, self.seq_length, self.hidden_size)				)




  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)   ->		List[str]:
      lowerCAmelCase__			:  Dict											= GPTNeoXJapaneseForCausalLM(config=__UpperCAmelCase				)
      model.to(__UpperCAmelCase				)
      model.eval()
      lowerCAmelCase__			:  List[str]											= model(__UpperCAmelCase  ,attention_mask=__UpperCAmelCase  ,labels=__UpperCAmelCase				)
      self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.vocab_size)				)




  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)   ->		Tuple:
      lowerCAmelCase__			:  Optional[int]											= True
      lowerCAmelCase__			:  str											= GPTNeoXJapaneseForCausalLM(config=__UpperCAmelCase				)
      model.to(__UpperCAmelCase				)
      model.eval()

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

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

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

      lowerCAmelCase__			:  Optional[Any]											= model(__UpperCAmelCase  ,attention_mask=__UpperCAmelCase  ,output_hidden_states=__UpperCAmelCase				)
      lowerCAmelCase__			:  List[str]											= output_from_no_past["""hidden_states"""][0]
      lowerCAmelCase__			:  int											= model(
          __UpperCAmelCase  ,attention_mask=__UpperCAmelCase  ,past_key_values=__UpperCAmelCase  ,output_hidden_states=__UpperCAmelCase  ,)["""hidden_states"""][0]

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






  def 							UpperCAmelCase_     (   self				)   ->		Union[str, Any]:
      lowerCAmelCase__			:  Optional[int]											= self.prepare_config_and_inputs()
      lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__			:  List[Any]											= config_and_inputs
      lowerCAmelCase__			:  List[Any]											= {"""input_ids""": input_ids, """attention_mask""": input_mask}
      return config, inputs_dict







@require_torch
class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_						,			SCREAMING_SNAKE_CASE_						,			unittest.TestCase					):
  '''simple docstring'''




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




  def 							UpperCAmelCase_     (   self				)   ->		List[str]:
      lowerCAmelCase__			:  Tuple											= GPTNeoXJapaneseModelTester(self				)
      lowerCAmelCase__			:  Dict											= ConfigTester(self  ,config_class=__UpperCAmelCase  ,hidden_size=37				)




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




  def 							UpperCAmelCase_     (   self				)   ->		List[Any]:
      lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__			:  Dict											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)




  def 							UpperCAmelCase_     (   self				)   ->		str:
      lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__			:  Optional[int]											= self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_model_as_decoder(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)




  def 							UpperCAmelCase_     (   self				)   ->		Optional[Any]:
      # This regression test was failing with PyTorch < 1.3
      lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__			:  List[str]											= self.model_tester.prepare_config_and_inputs_for_decoder()

      lowerCAmelCase__			:  str											= None

      self.model_tester.create_and_check_model_as_decoder(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)




  def 							UpperCAmelCase_     (   self				)   ->		Optional[Any]:
      lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__				,       lowerCAmelCase__			:  str											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_decoder_model_past_large_inputs(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)




  def 							UpperCAmelCase_     (   self				)   ->		Union[str, Any]:
      lowerCAmelCase__			:  Optional[int]											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_causal_lm(*__UpperCAmelCase				)






  @slow
  def 							UpperCAmelCase_     (   self				)   ->		List[str]:
      lowerCAmelCase__			:  Optional[Any]											= """abeja/gpt-neox-japanese-2.7b"""

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

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

      lowerCAmelCase__			:  Any											= GPTNeoXJapaneseTokenizer.from_pretrained(__UpperCAmelCase				)
      lowerCAmelCase__			:  Tuple											= GPTNeoXJapaneseForCausalLM.from_pretrained(__UpperCAmelCase				)

      lowerCAmelCase__			:  Dict											= []
      for prompt in prompts:
          lowerCAmelCase__			:  Any											= tokenizer(__UpperCAmelCase  ,return_tensors="""pt"""				).input_ids
          lowerCAmelCase__			:  List[str]											= model.generate(__UpperCAmelCase  ,max_length=50				)
          lowerCAmelCase__			:  Optional[int]											= tokenizer.batch_decode(__UpperCAmelCase  ,skip_special_tokens=__UpperCAmelCase				)
          predicted_outputs += generated_string
      self.assertListEqual(__UpperCAmelCase  ,__UpperCAmelCase				)







'''simple docstring'''





import os
def        _SCREAMING_SNAKE_CASE  (	UpperCamelCase      ):



    """simple docstring"""





    lowerCAmelCase__			:  List[str]											= len(grid[0]      )
    lowerCAmelCase__			:  int											= len(UpperCamelCase      )

    lowerCAmelCase__			:  Optional[int]											= 0
    lowerCAmelCase__			:  Tuple											= 0
    lowerCAmelCase__			:  Optional[Any]											= 0

    # Check vertically, horizontally, diagonally at the same time (only works
    # for nxn grid)
    for i in range(UpperCamelCase      ):
        for j in range(n_rows - 3      ):
            lowerCAmelCase__			:  str											= grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]
            lowerCAmelCase__			:  Optional[int]											= grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]

            # Left-to-right diagonal (\) product
            if i < n_columns - 3:
                lowerCAmelCase__			:  Optional[int]											= (
                    grid[i][j]
                    * grid[i + 1][j + 1]
                    * grid[i + 2][j + 2]
                    * grid[i + 3][j + 3]
                )

            # Right-to-left diagonal(/) product
            if i > 2:
                lowerCAmelCase__			:  Tuple											= (
                    grid[i][j]
                    * grid[i - 1][j + 1]
                    * grid[i - 2][j + 2]
                    * grid[i - 3][j + 3]
                )

            lowerCAmelCase__			:  Dict											= max(
                UpperCamelCase							,   UpperCamelCase							,   UpperCamelCase							,   UpperCamelCase      )
            if max_product > largest:
                lowerCAmelCase__			:  Any											= max_product

    return largest
def        _SCREAMING_SNAKE_CASE  (	):



    """simple docstring"""





    lowerCAmelCase__			:  List[str]											= []
    with open(os.path.dirname(UpperCamelCase      ) + """/grid.txt"""      ) as file:
        for line in file:
            grid.append(line.strip("""\n"""      ).split(""" """      )      )

    lowerCAmelCase__			:  Dict											= [[int(UpperCamelCase      ) for i in grid[j]] for j in range(len(UpperCamelCase      )      )]

    return largest_product(UpperCamelCase      )


if __name__ == "__main__":
 print(solution())


