'''simple docstring'''




import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import BertGenerationDecoder, BertGenerationEncoder


class       UpperCAmelCase__  :






			"""simple docstring"""
			def __init__(self       ,				_a       ,				_a=13       ,				_a=7       ,				_a=True       ,				_a=True       ,				_a=99       ,				_a=32       ,				_a=5       ,				_a=4       ,				_a=37       ,				_a="gelu"       ,				_a=0.1       ,				_a=0.1       ,				_a=50       ,				_a=0.02       ,				_a=True       ,				_a=None       ,				)    ->     Optional[int]:
								lowercase_					:       Optional[Any]     				=					parent
								lowercase_					:       List[str]     				=					batch_size
								lowercase_					:       Optional[int]     				=					seq_length
								lowercase_					:       Union[str, Any]     				=					is_training
								lowercase_					:       List[Any]     				=					use_input_mask
								lowercase_					:       str     				=					vocab_size
								lowercase_					:       Union[str, Any]     				=					hidden_size
								lowercase_					:       Union[str, Any]     				=					num_hidden_layers
								lowercase_					:       Union[str, Any]     				=					num_attention_heads
								lowercase_					:       List[str]     				=					intermediate_size
								lowercase_					:       int     				=					hidden_act
								lowercase_					:       Union[str, Any]     				=					hidden_dropout_prob
								lowercase_					:       List[str]     				=					attention_probs_dropout_prob
								lowercase_					:       Tuple     				=					max_position_embeddings
								lowercase_					:       List[Any]     				=					initializer_range
								lowercase_					:       Union[str, Any]     				=					use_labels
								lowercase_					:       str     				=					scope
			def 		_lowerCamelCase (self				)    ->     Dict:
								lowercase_					:       str     				=					ids_tensor([self.batch_size, self.seq_length]       ,				self.vocab_size				)

								lowercase_					:       List[str]     				=					None
								if self.use_input_mask:
													lowercase_					:       Dict     				=					random_attention_mask([self.batch_size, self.seq_length]				)

								if self.use_labels:
													lowercase_					:       List[str]     				=					ids_tensor([self.batch_size, self.seq_length]       ,				self.vocab_size				)

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

								return config, input_ids, input_mask, token_labels
			def 		_lowerCamelCase (self				)    ->     List[str]:
								return BertGenerationConfig(
								    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_size=self.intermediate_size       ,				hidden_act=self.hidden_act       ,				hidden_dropout_prob=self.hidden_dropout_prob       ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,				max_position_embeddings=self.max_position_embeddings       ,				is_decoder=A_       ,				initializer_range=self.initializer_range       ,				)
			def 		_lowerCamelCase (self				)    ->     Optional[Any]:
								(
								    lowercase_					
								)					:       Dict     				=					self.prepare_config_and_inputs()

								lowercase_					:       Optional[Any]     				=					True
								lowercase_					:       Union[str, Any]     				=					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]				)
								lowercase_					:       Dict     				=					ids_tensor([self.batch_size, self.seq_length]       ,				vocab_size=2				)

								return (
								    config,
								    input_ids,
								    input_mask,
								    token_labels,
								    encoder_hidden_states,
								    encoder_attention_mask,
								)
			def 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a       ,				_a       ,				**_a       ,				)    ->     List[str]:
								lowercase_					:       Optional[Any]     				=					BertGenerationEncoder(config=A_				)
								model.to(A_				)
								model.eval()
								lowercase_					:       List[str]     				=					model(A_       ,				attention_mask=A_				)
								lowercase_					:       int     				=					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       ,				_a       ,				_a       ,				_a       ,				**_a       ,				)    ->     Tuple:
								lowercase_					:       str     				=					True
								lowercase_					:       Tuple     				=					BertGenerationEncoder(config=A_				)
								model.to(A_				)
								model.eval()
								lowercase_					:       Dict     				=					model(
								    A_       ,				attention_mask=A_       ,				encoder_hidden_states=A_       ,				encoder_attention_mask=A_       ,				)
								lowercase_					:       Optional[int]     				=					model(
								    A_       ,				attention_mask=A_       ,				encoder_hidden_states=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       ,				_a       ,				_a       ,				**_a       ,				)    ->     Union[str, Any]:
								lowercase_					:       Union[str, Any]     				=					True
								lowercase_					:       Tuple     				=					True
								lowercase_					:       Optional[int]     				=					BertGenerationDecoder(config=A_				).to(A_				).eval()

								# first forward pass
								lowercase_					:       str     				=					model(
								    A_       ,				attention_mask=A_       ,				encoder_hidden_states=A_       ,				encoder_attention_mask=A_       ,				use_cache=A_       ,				)
								lowercase_					:       Dict     				=					outputs.past_key_values

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

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

								lowercase_					:       List[str]     				=					model(
								    A_       ,				attention_mask=A_       ,				encoder_hidden_states=A_       ,				encoder_attention_mask=A_       ,				output_hidden_states=A_       ,				)["hidden_states"][0]
								lowercase_					:       Union[str, Any]     				=					model(
								    A_       ,				attention_mask=A_       ,				encoder_hidden_states=A_       ,				encoder_attention_mask=A_       ,				past_key_values=A_       ,				output_hidden_states=A_       ,				)["hidden_states"][0]

								# select random slice
								lowercase_					:       List[Any]     				=					ids_tensor((1,)       ,				output_from_past.shape[-1]				).item()
								lowercase_					:       Dict     				=					output_from_no_past[:, -3:, random_slice_idx].detach()
								lowercase_					:       Optional[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(A_       ,				A_       ,				atol=1e-3				)				)
			def 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a       ,				_a       ,				*_a       ,				)    ->     int:
								lowercase_					:       Any     				=					BertGenerationDecoder(A_				)
								model.to(A_				)
								model.eval()
								lowercase_					:       Union[str, Any]     				=					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				)    ->     Optional[int]:
								lowercase_					:       Optional[Any]     				=					self.prepare_config_and_inputs()
								lowercase_					:       Dict     				=					{"input_ids": input_ids, "attention_mask": input_mask}
								return config, inputs_dict



@require_torch
class       UpperCAmelCase__  (       __snake_case					,     __snake_case					,     __snake_case					,     unittest.TestCase			):






			"""simple docstring"""
			A							:       str     =    (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
			A							:       Optional[int]     =    (BertGenerationDecoder,) if is_torch_available() else ()
			A							:       List[str]     =    (
			    {'feature-extraction': BertGenerationEncoder, 'text-generation': BertGenerationDecoder}
			    if is_torch_available()
			    else {}
			)
			def 		_lowerCamelCase (self				)    ->     Dict:
								lowercase_					:       str     				=					BertGenerationEncoderTester(self				)
								lowercase_					:       Optional[Any]     				=					ConfigTester(self       ,				config_class=A_       ,				hidden_size=37				)
			def 		_lowerCamelCase (self				)    ->     Any:
								self.config_tester.run_common_tests()
			def 		_lowerCamelCase (self				)    ->     Dict:
								lowercase_					:       int     				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*A_				)
			def 		_lowerCamelCase (self				)    ->     List[str]:
								lowercase_					:       List[Any]     				=					self.model_tester.prepare_config_and_inputs()
								lowercase_					:       Dict     				=					"bert"
								self.model_tester.create_and_check_model(A_       ,				A_       ,				A_       ,				A_				)
			def 		_lowerCamelCase (self				)    ->     Any:
								lowercase_					:       str     				=					self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_model_as_decoder(*A_				)
			def 		_lowerCamelCase (self				)    ->     Dict:
								lowercase_					:       str     				=					self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_decoder_model_past_large_inputs(*A_				)
			def 		_lowerCamelCase (self				)    ->     Optional[int]:
								(
								    lowercase_					
								)					:       Tuple     				=					self.model_tester.prepare_config_and_inputs_for_decoder()

								lowercase_					:       List[str]     				=					None

								self.model_tester.create_and_check_model_as_decoder(
								    A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				)
			def 		_lowerCamelCase (self				)    ->     str:
								lowercase_					:       Optional[int]     				=					self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_for_causal_lm(*A_				)





			@slow
			def 		_lowerCamelCase (self				)    ->     Optional[Any]:
								lowercase_					:       List[Any]     				=					BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'				)
								self.assertIsNotNone(A_				)



@require_torch
class       UpperCAmelCase__  (       unittest.TestCase			):






			"""simple docstring"""
			@slow
			def 		_lowerCamelCase (self				)    ->     Dict:
								lowercase_					:       Optional[int]     				=					BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'				)
								lowercase_					:       Tuple     				=					torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]				)
								with torch.no_grad():
													lowercase_					:       Union[str, Any]     				=					model(A_				)[0]
								lowercase_					:       List[str]     				=					torch.Size([1, 8, 1_024]				)
								self.assertEqual(output.shape       ,				A_				)
								lowercase_					:       int     				=					torch.tensor(
								    [[[0.17_75, 0.00_83, -0.03_21], [1.60_02, 0.12_87, 0.39_12], [2.14_73, 0.57_91, 0.60_66]]]				)
								self.assertTrue(torch.allclose(output[:, :3, :3]       ,				A_       ,				atol=1e-4				)				)



@require_torch
class       UpperCAmelCase__  (       unittest.TestCase			):






			"""simple docstring"""
			@slow
			def 		_lowerCamelCase (self				)    ->     List[Any]:
								lowercase_					:       List[str]     				=					BertGenerationDecoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'				)
								lowercase_					:       List[Any]     				=					torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]				)
								with torch.no_grad():
													lowercase_					:       int     				=					model(A_				)[0]
								lowercase_					:       Optional[int]     				=					torch.Size([1, 8, 50_358]				)
								self.assertEqual(output.shape       ,				A_				)
								lowercase_					:       List[Any]     				=					torch.tensor(
								    [[[-0.57_88, -2.59_94, -3.70_54], [0.04_38, 4.79_97, 1.87_95], [1.58_62, 6.64_09, 4.46_38]]]				)
								self.assertTrue(torch.allclose(output[:, :3, :3]       ,				A_       ,				atol=1e-4				)				)





'''simple docstring'''




import os

def _UpperCamelCase   (       SCREAMING_SNAKE_CASE_							):
     lowercase_					:       List[Any]     				=					len(grid[0]							)
     lowercase_					:       Union[str, Any]     				=					len(SCREAMING_SNAKE_CASE_							)

     lowercase_					:       Union[str, Any]     				=					0
     lowercase_					:       int     				=					0
     lowercase_					:       Union[str, Any]     				=					0

     # Check vertically, horizontally, diagonally at the same time (only works
     # for nxn grid)
     for i in range(SCREAMING_SNAKE_CASE_							):
          for j in range(n_rows - 3							):
               lowercase_					:       Dict     				=					grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]
               lowercase_					:       Optional[Any]     				=					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:
                    lowercase_					:       Tuple     				=					(
                        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:
                    lowercase_					:       int     				=					(
                        grid[i][j]
                        * grid[i - 1][j + 1]
                        * grid[i - 2][j + 2]
                        * grid[i - 3][j + 3]
                    )

               lowercase_					:       Optional[int]     				=					max(
                   SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_  ,       SCREAMING_SNAKE_CASE_							)
               if max_product > largest:
                    lowercase_					:       List[Any]     				=					max_product

     return largest

def _UpperCamelCase   (       ):
     lowercase_					:       int     				=					[]
     with open(os.path.dirname(SCREAMING_SNAKE_CASE_							) + '/grid.txt'							) as file:
          for line in file:
               grid.append(line.strip('\n'							).split(' '							)							)

     lowercase_					:       Dict     				=					[[int(SCREAMING_SNAKE_CASE_							) for i in grid[j]] for j in range(len(SCREAMING_SNAKE_CASE_							)							)]

     return largest_product(SCREAMING_SNAKE_CASE_							)


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




