





'''simple docstring'''




from typing import Optional

import numpy as np
import torch
from torch import nn
from transformers import GPTaConfig, GPTaLMHeadModel
from transformers.modeling_utils import ModuleUtilsMixin

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin







class   A_	(  __snake_case							,						__snake_case							,						__snake_case			):
 _lowerCamelCase  : Union[str, Any]           =							[R"""h\.\d+\.attn\.bias""", R"""h\.\d+\.attn\.masked_bias"""]




 @register_to_config
 def __init__(					self       :    int	,			snake_case_       :    int	,			snake_case_       :    int	,			snake_case_       :    Optional[int] = None	,			snake_case_       :    int = 5_0_2_5_7	,			snake_case_       :    int = 1_0_2_4	,			snake_case_       :    int = 7_6_8	,			snake_case_       :    int = 1_2	,			snake_case_       :    int = 1_2	,			snake_case_       :    Optional[int] = None	,			snake_case_       :    str = "gelu_new"	,			snake_case_       :    float = 0.1	,			snake_case_       :    float = 0.1	,			snake_case_       :    float = 0.1	,			snake_case_       :    float = 1e-5	,			snake_case_       :    float = 0.0_2	,			snake_case_       :    bool = True	,			snake_case_       :    bool = True	,			snake_case_       :    bool = False	,			snake_case_       :    bool = False	,			):
      super().__init__()

      _UpperCAmelCase     							=   prefix_length

      if prefix_inner_dim != n_embd and prefix_hidden_dim is None:
           raise ValueError(
               f'`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and'
               f' `n_embd`: {n_embd} are not equal.'    )

      _UpperCAmelCase     							=   prefix_inner_dim
      _UpperCAmelCase     							=   prefix_hidden_dim

      _UpperCAmelCase     							=   (
          nn.Linear(self.prefix_inner_dim	,			self.prefix_hidden_dim    )
          if self.prefix_hidden_dim is not None
          else nn.Identity()
      )
      _UpperCAmelCase     							=   (
          nn.Linear(self.prefix_hidden_dim	,			__SCREAMING_SNAKE_CASE    ) if self.prefix_hidden_dim is not None else nn.Identity()
      )

      _UpperCAmelCase     							=   GPTaConfig(
          vocab_size=__SCREAMING_SNAKE_CASE	,			n_positions=__SCREAMING_SNAKE_CASE	,			n_embd=__SCREAMING_SNAKE_CASE	,			n_layer=__SCREAMING_SNAKE_CASE	,			n_head=__SCREAMING_SNAKE_CASE	,			n_inner=__SCREAMING_SNAKE_CASE	,			activation_function=__SCREAMING_SNAKE_CASE	,			resid_pdrop=__SCREAMING_SNAKE_CASE	,			embd_pdrop=__SCREAMING_SNAKE_CASE	,			attn_pdrop=__SCREAMING_SNAKE_CASE	,			layer_norm_epsilon=__SCREAMING_SNAKE_CASE	,			initializer_range=__SCREAMING_SNAKE_CASE	,			scale_attn_weights=__SCREAMING_SNAKE_CASE	,			use_cache=__SCREAMING_SNAKE_CASE	,			scale_attn_by_inverse_layer_idx=__SCREAMING_SNAKE_CASE	,			reorder_and_upcast_attn=__SCREAMING_SNAKE_CASE	,			)
      _UpperCAmelCase     							=   GPTaLMHeadModel(__SCREAMING_SNAKE_CASE    )




 def      lowercase	(					self       :    Tuple	,			snake_case_       :    torch.Tensor	,			snake_case_       :    torch.Tensor	,			snake_case_       :    Optional[torch.Tensor] = None	,			snake_case_       :    Optional[torch.Tensor] = None	,			):
      _UpperCAmelCase     							=   self.transformer.transformer.wte(__SCREAMING_SNAKE_CASE    )
      _UpperCAmelCase     							=   self.encode_prefix(__SCREAMING_SNAKE_CASE    )
      _UpperCAmelCase     							=   self.decode_prefix(__SCREAMING_SNAKE_CASE    )
      _UpperCAmelCase     							=   torch.cat((prefix_embeds, embedding_text)	,			dim=1    )

      if labels is not None:
           _UpperCAmelCase     							=   self.get_dummy_token(input_ids.shape[0]	,			input_ids.device    )
           _UpperCAmelCase     							=   torch.cat((dummy_token, input_ids)	,			dim=1    )
      _UpperCAmelCase     							=   self.transformer(inputs_embeds=__SCREAMING_SNAKE_CASE	,			labels=__SCREAMING_SNAKE_CASE	,			attention_mask=__SCREAMING_SNAKE_CASE    )
      if self.prefix_hidden_dim is not None:
           return out, hidden
      else:
           return out




 def      lowercase	(					self       :    Tuple	,			snake_case_       :    int	,			snake_case_       :    torch.device    ):
      return torch.zeros(__SCREAMING_SNAKE_CASE	,			self.prefix_length	,			dtype=torch.intaa	,			device=__SCREAMING_SNAKE_CASE    )




 def      lowercase	(					self       :    Union[str, Any]	,			snake_case_       :    List[str]    ):
      return self.encode_prefix(__SCREAMING_SNAKE_CASE    )




 @torch.no_grad()
 def      lowercase	(					self       :    Optional[Any]	,			snake_case_       :    int	,			snake_case_       :    Optional[int]	,			snake_case_       :    List[str]    ):
      _UpperCAmelCase     							=   torch.split(__SCREAMING_SNAKE_CASE	,			1	,			dim=0    )
      _UpperCAmelCase     							=   []
      _UpperCAmelCase     							=   []
      for feature in features:
           _UpperCAmelCase     							=   self.decode_prefix(feature.to(__SCREAMING_SNAKE_CASE    )    )  # back to the clip feature
           # Only support beam search for now
           _UpperCAmelCase							,  _UpperCAmelCase     							=   self.generate_beam(
               input_embeds=__SCREAMING_SNAKE_CASE	,			device=__SCREAMING_SNAKE_CASE	,			eos_token_id=__SCREAMING_SNAKE_CASE    )
           generated_tokens.append(output_tokens[0]    )
           generated_seq_lengths.append(seq_lengths[0]    )
      _UpperCAmelCase     							=   torch.stack(__SCREAMING_SNAKE_CASE    )
      _UpperCAmelCase     							=   torch.stack(__SCREAMING_SNAKE_CASE    )
      return generated_tokens, generated_seq_lengths




 @torch.no_grad()
 def      lowercase	(					self       :    Optional[Any]	,			snake_case_       :    Optional[int]=None	,			snake_case_       :    int=None	,			snake_case_       :    int=None	,			snake_case_       :    int = 5	,			snake_case_       :    int = 6_7	,			snake_case_       :    float = 1.0	,			snake_case_       :    Optional[int] = None	,			):
      _UpperCAmelCase     							=   eos_token_id
      _UpperCAmelCase     							=   None
      _UpperCAmelCase     							=   None
      _UpperCAmelCase     							=   torch.ones(__SCREAMING_SNAKE_CASE	,			device=__SCREAMING_SNAKE_CASE	,			dtype=torch.int    )
      _UpperCAmelCase     							=   torch.zeros(__SCREAMING_SNAKE_CASE	,			device=__SCREAMING_SNAKE_CASE	,			dtype=torch.bool    )

      if input_embeds is not None:
           _UpperCAmelCase     							=   input_embeds
      else:
           _UpperCAmelCase     							=   self.transformer.transformer.wte(__SCREAMING_SNAKE_CASE    )

      for i in range(__SCREAMING_SNAKE_CASE    ):
           _UpperCAmelCase     							=   self.transformer(inputs_embeds=__SCREAMING_SNAKE_CASE    )
           _UpperCAmelCase     							=   outputs.logits
           _UpperCAmelCase     							=   logits[:, -1, :] / (temperature if temperature > 0 else 1.0)
           _UpperCAmelCase     							=   logits.softmax(-1    ).log()

           if scores is None:
                _UpperCAmelCase							,  _UpperCAmelCase     							=   logits.topk(__SCREAMING_SNAKE_CASE	,			-1    )
                _UpperCAmelCase     							=   generated.expand(__SCREAMING_SNAKE_CASE	,			*generated.shape[1:]    )
                _UpperCAmelCase							,  _UpperCAmelCase     							=   next_tokens.permute(1	,			0    ), scores.squeeze(0    )
                if tokens is None:
                     _UpperCAmelCase     							=   next_tokens
                else:
                     _UpperCAmelCase     							=   tokens.expand(__SCREAMING_SNAKE_CASE	,			*tokens.shape[1:]    )
                     _UpperCAmelCase     							=   torch.cat((tokens, next_tokens)	,			dim=1    )
           else:
                _UpperCAmelCase     							=   -float(np.inf    )
                _UpperCAmelCase     							=   0
                _UpperCAmelCase     							=   scores[:, None] + logits
                seq_lengths[~is_stopped] += 1
                _UpperCAmelCase     							=   scores_sum / seq_lengths[:, None]
                _UpperCAmelCase							,  _UpperCAmelCase     							=   scores_sum_average.view(-1    ).topk(__SCREAMING_SNAKE_CASE	,			-1    )
                _UpperCAmelCase     							=   next_tokens // scores_sum.shape[1]
                _UpperCAmelCase     							=   seq_lengths[next_tokens_source]
                _UpperCAmelCase     							=   next_tokens % scores_sum.shape[1]
                _UpperCAmelCase     							=   next_tokens.unsqueeze(1    )
                _UpperCAmelCase     							=   tokens[next_tokens_source]
                _UpperCAmelCase     							=   torch.cat((tokens, next_tokens)	,			dim=1    )
                _UpperCAmelCase     							=   generated[next_tokens_source]
                _UpperCAmelCase     							=   scores_sum_average * seq_lengths
                _UpperCAmelCase     							=   is_stopped[next_tokens_source]

           _UpperCAmelCase     							=   self.transformer.transformer.wte(next_tokens.squeeze()    ).view(generated.shape[0]	,			1	,			-1    )
           _UpperCAmelCase     							=   torch.cat((generated, next_token_embed)	,			dim=1    )
           _UpperCAmelCase     							=   is_stopped + next_tokens.eq(__SCREAMING_SNAKE_CASE    ).squeeze()
           if is_stopped.all():
                break

      _UpperCAmelCase     							=   scores / seq_lengths
      _UpperCAmelCase     							=   scores.argsort(descending=__SCREAMING_SNAKE_CASE    )
      # tokens tensors are already padded to max_seq_length
      _UpperCAmelCase     							=   [tokens[i] for i in order]
      _UpperCAmelCase     							=   torch.stack(__SCREAMING_SNAKE_CASE	,			dim=0    )
      _UpperCAmelCase     							=   torch.tensor([seq_lengths[i] for i in order]	,			dtype=seq_lengths.dtype    )
      return output_texts, seq_lengths












"""simple docstring"""





import os







def      lowerCamelCase__       (							)      ->				List[Any]:
  with open(os.path.dirname(_lowerCamelCase ) + '/grid.txt' ) as f:
    lowerCamelCase_         =						[]  # noqa: E741
    for _ in range(20 ):
      l.append([int(_lowerCamelCase ) for x in f.readline().split()] )

    lowerCamelCase_         =						0

    # right
    for i in range(20 ):
      for j in range(17 ):
        lowerCamelCase_         =						l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3]
        if temp > maximum:
          lowerCamelCase_         =						temp

        # down
    for i in range(17 ):
      for j in range(20 ):
        lowerCamelCase_         =						l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j]
        if temp > maximum:
          lowerCamelCase_         =						temp

        # diagonal 1
    for i in range(17 ):
      for j in range(17 ):
        lowerCamelCase_         =						l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3]
        if temp > maximum:
          lowerCamelCase_         =						temp

        # diagonal 2
    for i in range(17 ):
      for j in range(3		,				20 ):
        lowerCamelCase_         =						l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3]
        if temp > maximum:
          lowerCamelCase_         =						temp
    return maximum


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




