

'''simple docstring'''





import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFGPTaLMHeadModel, is_keras_nlp_available, is_tf_available
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import require_keras_nlp, require_tf, slow


if is_tf_available():
  import tensorflow as tf

if is_keras_nlp_available():
  from transformers.models.gpta import TFGPTaTokenizer


lowercase_             =     ['gpt2']
lowercase_             =     'gpt2'

if is_tf_available():





  class 				__lowerCAmelCase  (      tf.Module							):




    def __init__(       self							,     lowerCAmelCase      )						->				str:
          '''simple docstring'''






          super().__init__()
          _lowercase			 =tokenizer
          _lowercase			 =AutoConfig.from_pretrained(lowerCAmelCase      )
          _lowercase			 =TFGPTaLMHeadModel.from_config(lowerCAmelCase      )




    @tf.function(input_signature=(tf.TensorSpec((None,)							,     tf.string							,     name='text'      ),)      )
    def 						A__  (       self							,     lowerCAmelCase      )						->				Dict:
          '''simple docstring'''






          _lowercase			 =self.tokenizer(lowerCAmelCase      )
          _lowercase			 =tokenized['input_ids'].to_tensor()

          _lowercase			 =tf.cast(input_ids_dense > 0							,     tf.intaa      )
          # input_mask = tf.reshape(input_mask, [-1, MAX_SEQ_LEN])

          _lowercase			 =self.model(input_ids=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )['logits']

          return outputs





@require_tf
@require_keras_nlp
class 				__lowerCAmelCase  (      unittest.TestCase							):




  def 						A__  (       self      )						->				Dict:
        '''simple docstring'''






        super().setUp()

        _lowercase			 =[GPTaTokenizer.from_pretrained(lowerCAmelCase      ) for checkpoint in (TOKENIZER_CHECKPOINTS)]
        _lowercase			 =[TFGPTaTokenizer.from_pretrained(lowerCAmelCase      ) for checkpoint in TOKENIZER_CHECKPOINTS]
        assert len(self.tokenizers      ) == len(self.tf_tokenizers      )

        _lowercase			 =[
            'This is a straightforward English test sentence.',
            'This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.',
            'Now we\'re going to add some Chinese: 一 二 三 一二三',
            'And some much more rare Chinese: 齉 堃 齉堃',
            'Je vais aussi écrire en français pour tester les accents',
            'Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ',
        ]
        _lowercase			 =list(zip(self.test_sentences							,     self.test_sentences[::-1]      )      )




  def 						A__  (       self      )						->				List[str]:
        '''simple docstring'''






        for tokenizer, tf_tokenizer in zip(self.tokenizers							,     self.tf_tokenizers      ):
              for test_inputs in self.test_sentences:
                    _lowercase			 =tokenizer([test_inputs]							,     return_tensors='tf'      )
                    _lowercase			 =tf_tokenizer([test_inputs]      )

                    for key in python_outputs.keys():
                          # convert them to numpy to avoid messing with ragged tensors
                          _lowercase			 =python_outputs[key].numpy()
                          _lowercase			 =tf_outputs[key].numpy()

                          self.assertTrue(tf.reduce_all(python_outputs_values.shape == tf_outputs_values.shape      )      )
                          self.assertTrue(tf.reduce_all(tf.cast(lowerCAmelCase							,     tf.intaa      ) == tf_outputs_values      )      )




  @slow
  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        for tf_tokenizer in self.tf_tokenizers:
              _lowercase			 =tf.function(lowerCAmelCase      )
              for test_inputs in self.test_sentences:
                    _lowercase			 =tf.constant(lowerCAmelCase      )
                    _lowercase			 =compiled_tokenizer(lowerCAmelCase      )
                    _lowercase			 =tf_tokenizer(lowerCAmelCase      )

                    for key in eager_outputs.keys():
                          self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]      )      )




  @slow
  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        for tf_tokenizer in self.tf_tokenizers:
              _lowercase			 =ModelToSave(tokenizer=lowerCAmelCase      )
              _lowercase			 =tf.convert_to_tensor([self.test_sentences[0]]      )
              _lowercase			 =model.serving(lowerCAmelCase      )  # Build model with some sample inputs
              with TemporaryDirectory() as tempdir:
                    _lowercase			 =Path(lowerCAmelCase      ) / 'saved.model'
                    tf.saved_model.save(lowerCAmelCase							,     lowerCAmelCase							,     signatures={'serving_default': model.serving}      )
                    _lowercase			 =tf.saved_model.load(lowerCAmelCase      )
              _lowercase			 =loaded_model.signatures['serving_default'](lowerCAmelCase      )['output_0']
              # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
              self.assertTrue(tf.reduce_all(out == loaded_output      )      )




  @slow
  def 						A__  (       self      )						->				Union[str, Any]:
        '''simple docstring'''






        for tf_tokenizer in self.tf_tokenizers:
              _lowercase			 =tf.convert_to_tensor([self.test_sentences[0]]      )
              _lowercase			 =tf_tokenizer(lowerCAmelCase      )  # Build model with some sample inputs

              _lowercase			 =tf_tokenizer.get_config()
              _lowercase			 =TFGPTaTokenizer.from_config(lowerCAmelCase      )
              _lowercase			 =model_from_config(lowerCAmelCase      )

              for key in from_config_output.keys():
                    self.assertTrue(tf.reduce_all(from_config_output[key] == out[key]      )      )




  @slow
  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        for tf_tokenizer in self.tf_tokenizers:
              # for the test to run
              _lowercase			 =123_123

              for max_length in [3, 5, 1_024]:
                    _lowercase			 =tf.convert_to_tensor([self.test_sentences[0]]      )
                    _lowercase			 =tf_tokenizer(lowerCAmelCase							,     max_length=lowerCAmelCase      )

                    _lowercase			 =out['input_ids'].numpy().shape[1]

                    assert out_length == max_length





import os
def   a   (    )					->   Optional[int]:





      """simple docstring"""



      with open(os.path.dirname(A__  ) + '/grid.txt'  ) as f:
            _lowercase			 =[]  # noqa: E741
            for _ in range(20  ):
                  l.append([int(A__  ) for x in f.readline().split()]  )

            _lowercase			 =0

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

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

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

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


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


