
'''simple docstring'''






from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
 from PIL import Image

 from transformers import ViTImageProcessor

class 			UpperCAmelCase__    :



    def __init__(						self   :							List[Any],__A   :							Dict,__A   :							Optional[int]=1_3,__A   :							Optional[Any]=3_0,__A   :							Any=2,__A   :							Any=3,__A   :							List[Any]=True,__A   :							List[str]=True,__A   :							List[str]=3_2,__A   :							List[str]=2,__A   :							List[Any]=4,__A   :							int=3_7,__A   :							Any="gelu",__A   :							Optional[Any]=0.1,__A   :							List[Any]=0.1,__A   :							Optional[int]=1_0,__A   :							Optional[int]=0.02,__A   :							Any=3,__A   :							Optional[Any]=None,):
          _lowerCamelCase					: Any		    = parent
          _lowerCamelCase					: Union[str, Any]		    = batch_size
          _lowerCamelCase					: Dict		    = image_size
          _lowerCamelCase					: Union[str, Any]		    = patch_size
          _lowerCamelCase					: str		    = num_channels
          _lowerCamelCase					: int		    = is_training
          _lowerCamelCase					: int		    = use_labels
          _lowerCamelCase					: Optional[int]		    = hidden_size
          _lowerCamelCase					: Optional[Any]		    = num_hidden_layers
          _lowerCamelCase					: Dict		    = num_attention_heads
          _lowerCamelCase					: Optional[Any]		    = intermediate_size
          _lowerCamelCase					: str		    = hidden_act
          _lowerCamelCase					: List[str]		    = hidden_dropout_prob
          _lowerCamelCase					: Optional[int]		    = attention_probs_dropout_prob
          _lowerCamelCase					: str		    = type_sequence_label_size
          _lowerCamelCase					: int		    = initializer_range
          _lowerCamelCase					: Union[str, Any]		    = scope

          # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
          _lowerCamelCase					: str		    = (image_size // patch_size) ** 2
          _lowerCamelCase					: Optional[Any]		    = num_patches + 1



    def 	lowerCamelCase_	(						self   :							List[str]  ):
          _lowerCamelCase					: List[Any]		    = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

          _lowerCamelCase					: int		    = None
          if self.use_labels:
                _lowerCamelCase					: Dict		    = ids_tensor([self.batch_size],self.type_sequence_label_size  )

          _lowerCamelCase					: Tuple		    = self.get_config()

          return config, pixel_values, labels



    def 	lowerCamelCase_	(						self   :							Tuple  ):
          return ViTConfig(
              image_size=self.image_size,patch_size=self.patch_size,num_channels=self.num_channels,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,is_decoder=__A,initializer_range=self.initializer_range,)



    def 	lowerCamelCase_	(						self   :							Any,__A   :							Any,__A   :							Dict,__A   :							Any  ):
          _lowerCamelCase					: int		    = TFViTModel(config=__A  )
          _lowerCamelCase					: Dict		    = model(__A,training=__A  )
          self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.hidden_size)  )

          # Test with an image with different size than the one specified in config.
          _lowerCamelCase					: Dict		    = self.image_size // 2
          _lowerCamelCase					: Any		    = pixel_values[:, :, :image_size, :image_size]
          _lowerCamelCase					: int		    = model(__A,interpolate_pos_encoding=__A,training=__A  )
          _lowerCamelCase					: int		    = (image_size // self.patch_size) ** 2 + 1
          self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, seq_length, self.hidden_size)  )



    def 	lowerCamelCase_	(						self   :							Union[str, Any],__A   :							Any,__A   :							List[str],__A   :							Optional[Any]  ):
          _lowerCamelCase					: Optional[int]		    = self.type_sequence_label_size
          _lowerCamelCase					: Dict		    = TFViTForImageClassification(__A  )
          _lowerCamelCase					: Any		    = model(__A,labels=__A,training=__A  )
          self.parent.assertEqual(result.logits.shape,(self.batch_size, self.type_sequence_label_size)  )

          # Test with an image with different size than the one specified in config.
          _lowerCamelCase					: Union[str, Any]		    = self.image_size // 2
          _lowerCamelCase					: int		    = pixel_values[:, :, :image_size, :image_size]
          _lowerCamelCase					: Any		    = model(__A,interpolate_pos_encoding=__A,training=__A  )
          self.parent.assertEqual(result.logits.shape,(self.batch_size, self.type_sequence_label_size)  )

          # test greyscale images
          _lowerCamelCase					: List[Any]		    = 1
          _lowerCamelCase					: Dict		    = TFViTForImageClassification(__A  )
          _lowerCamelCase					: int		    = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
          _lowerCamelCase					: Any		    = model(__A  )
          self.parent.assertEqual(result.logits.shape,(self.batch_size, self.type_sequence_label_size)  )



    def 	lowerCamelCase_	(						self   :							str  ):
          _lowerCamelCase					: str		    = self.prepare_config_and_inputs()
          _lowerCamelCase    ,		_lowerCamelCase    ,		_lowerCamelCase					: Any		    = config_and_inputs
          _lowerCamelCase					: Any		    = {"pixel_values": pixel_values}
          return config, inputs_dict


@require_tf
class 			UpperCAmelCase__    (							A    ,						A    ,						unittest.TestCase			):
    lowerCAmelCase_					     =		(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
    lowerCAmelCase_					     =		(
        {'feature-extraction': TFViTModel, 'image-classification': TFViTForImageClassification}
        if is_tf_available()
        else {}
    )

    lowerCAmelCase_					     =		False
    lowerCAmelCase_					     =		False
    lowerCAmelCase_					     =		False



    def 	lowerCamelCase_	(						self   :							List[str]  ):
          _lowerCamelCase					: List[str]		    = TFViTModelTester(self  )
          _lowerCamelCase					: Tuple		    = ConfigTester(self,config_class=__A,has_text_modality=__A,hidden_size=3_7  )



    def 	lowerCamelCase_	(						self   :							int  ):
          self.config_tester.run_common_tests()



    @unittest.skip(reason="ViT does not use inputs_embeds"  )
    def 	lowerCamelCase_	(						self   :							List[Any]  ):
          pass



    @unittest.skip(reason="ViT does not use inputs_embeds"  )
    def 	lowerCamelCase_	(						self   :							Union[str, Any]  ):
          pass



    def 	lowerCamelCase_	(						self   :							Dict  ):
          _lowerCamelCase    ,		_lowerCamelCase					: Optional[int]		    = self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                _lowerCamelCase					: Optional[int]		    = model_class(__A  )
                self.assertIsInstance(model.get_input_embeddings(),(tf.keras.layers.Layer)  )
                _lowerCamelCase					: Optional[int]		    = model.get_output_embeddings()
                self.assertTrue(x is None or isinstance(__A,tf.keras.layers.Layer  )  )



    def 	lowerCamelCase_	(						self   :							List[str]  ):
          _lowerCamelCase    ,		_lowerCamelCase					: Optional[Any]		    = self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                _lowerCamelCase					: Dict		    = model_class(__A  )
                _lowerCamelCase					: int		    = inspect.signature(model.call  )
                # signature.parameters is an OrderedDict => so arg_names order is deterministic
                _lowerCamelCase					: Optional[int]		    = [*signature.parameters.keys()]

                _lowerCamelCase					: Optional[int]		    = ["pixel_values"]
                self.assertListEqual(arg_names[:1],__A  )



    def 	lowerCamelCase_	(						self   :							Tuple  ):
          _lowerCamelCase					: Tuple		    = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__A  )



    def 	lowerCamelCase_	(						self   :							List[Any]  ):
          _lowerCamelCase					: List[str]		    = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*__A  )



    @slow
    def 	lowerCamelCase_	(						self   :							Optional[Any]  ):
          _lowerCamelCase					: int		    = TFViTModel.from_pretrained("google/vit-base-patch16-224"  )
          self.assertIsNotNone(__A  )







def 		A_							(						):



      """simple docstring"""




      _lowerCamelCase					: Optional[Any]		    = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
      return image



@require_tf
@require_vision
class 			UpperCAmelCase__    (							unittest.TestCase			):



    @cached_property
    def 	lowerCamelCase_	(						self   :							Tuple  ):
          return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"  ) if is_vision_available() else None



    @slow
    def 	lowerCamelCase_	(						self   :							Dict  ):
          _lowerCamelCase					: Any		    = TFViTForImageClassification.from_pretrained("google/vit-base-patch16-224"  )

          _lowerCamelCase					: Any		    = self.default_image_processor
          _lowerCamelCase					: Optional[Any]		    = prepare_img()
          _lowerCamelCase					: Dict		    = image_processor(images=__A,return_tensors="tf"  )

          # forward pass
          _lowerCamelCase					: Any		    = model(**__A  )

          # verify the logits
          _lowerCamelCase					: Union[str, Any]		    = tf.TensorShape((1, 1_0_0_0)  )
          self.assertEqual(outputs.logits.shape,__A  )

          _lowerCamelCase					: Optional[int]		    = tf.constant([-0.2744, 0.8215, -0.0836]  )

          tf.debugging.assert_near(outputs.logits[0, :3],__A,atol=1e-4  )
'''simple docstring'''

import os



def snake_case_      (					__snake_case  :							List[Any])			->					str:
   lowerCAmelCase_			=      len(grid[0])
   lowerCAmelCase_			=      len(__snake_case)

   lowerCAmelCase_			=      0
   lowerCAmelCase_			=      0
   lowerCAmelCase_			=      0

   # Check vertically, horizontally, diagonally at the same time (only works
   # for nxn grid)
   for i in range(__snake_case):
      for j in range(n_rows - 3):
         lowerCAmelCase_			=      grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]
         lowerCAmelCase_			=      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_			=      (
                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_			=      (
                grid[i][j]
                * grid[i - 1][j + 1]
                * grid[i - 2][j + 2]
                * grid[i - 3][j + 3]
            )

         lowerCAmelCase_			=      max(
             __snake_case	,   __snake_case	,   __snake_case	,   __snake_case)
         if max_product > largest:
            lowerCAmelCase_			=      max_product

   return largest



def snake_case_      (					)			->					List[Any]:
   lowerCAmelCase_			=      []
   with open(os.path.dirname(__snake_case) + '''/grid.txt''') as file:
      for line in file:
         grid.append(line.strip('''\n''').split(''' '''))

   lowerCAmelCase_			=      [[int(__snake_case) for i in grid[j]] for j in range(len(__snake_case))]

   return largest_product(__snake_case)


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





