




from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
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 TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
		from PIL import Image

		from transformers import ConvNextImageProcessor




class        snake_case__				:
				"""simple docstring"""



				def __init__( self							:	Dict   , __lowerCamelCase							:	str   , __lowerCamelCase							:	Tuple=13   , __lowerCamelCase							:	Union[str, Any]=32   , __lowerCamelCase							:	str=3   , __lowerCamelCase							:	Dict=4   , __lowerCamelCase							:	Dict=[10, 20, 30, 40]   , __lowerCamelCase							:	List[Any]=[2, 2, 3, 2]   , __lowerCamelCase							:	Dict=True   , __lowerCamelCase							:	Tuple=True   , __lowerCamelCase							:	Union[str, Any]=37   , __lowerCamelCase							:	List[str]="gelu"   , __lowerCamelCase							:	int=10   , __lowerCamelCase							:	Optional[int]=0.02   , __lowerCamelCase							:	str=3   , __lowerCamelCase							:	Any=None   , )   ->	Optional[int]:
							a       =     parent
							a       =     batch_size
							a       =     image_size
							a       =     num_channels
							a       =     num_stages
							a       =     hidden_sizes
							a       =     depths
							a       =     is_training
							a       =     use_labels
							a       =     intermediate_size
							a       =     hidden_act
							a       =     type_sequence_label_size
							a       =     initializer_range
							a       =     scope





				def 				__UpperCAmelCase				( self							:	Tuple				)   ->	Optional[int]:
							a       =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

							a       =     None
							if self.use_labels:
										a       =     ids_tensor([self.batch_size]   , self.type_sequence_label_size				)

							a       =     self.get_config()

							return config, pixel_values, labels





				def 				__UpperCAmelCase				( self							:	List[str]				)   ->	str:
							return ConvNextConfig(
							    num_channels=self.num_channels   , hidden_sizes=self.hidden_sizes   , depths=self.depths   , num_stages=self.num_stages   , hidden_act=self.hidden_act   , is_decoder=_a   , initializer_range=self.initializer_range   , )





				def 				__UpperCAmelCase				( self							:	Any   , __lowerCamelCase							:	int   , __lowerCamelCase							:	Optional[Any]   , __lowerCamelCase							:	Dict				)   ->	str:
							a       =     TFConvNextModel(config=_a				)
							a       =     model(_a   , training=_a				)
							# expected last hidden states: B, C, H // 32, W // 32
							self.parent.assertEqual(
							    result.last_hidden_state.shape   , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)   , )





				def 				__UpperCAmelCase				( self							:	List[Any]   , __lowerCamelCase							:	Any   , __lowerCamelCase							:	List[str]   , __lowerCamelCase							:	Union[str, Any]				)   ->	Optional[int]:
							a       =     self.type_sequence_label_size
							a       =     TFConvNextForImageClassification(_a				)
							a       =     model(_a   , labels=_a   , training=_a				)
							self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.type_sequence_label_size)				)





				def 				__UpperCAmelCase				( self							:	Dict				)   ->	Tuple:
							a       =     self.prepare_config_and_inputs()
							a       =     config_and_inputs
							a       =     {"pixel_values": pixel_values}
							return config, inputs_dict




@require_tf
class        snake_case__				(a_       ,			a_       ,			unittest.TestCase  ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			:				List[Any]									=   (TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
				SCREAMING_SNAKE_CASE_			:				List[Any]									=   (
				    {'''feature-extraction''': TFConvNextModel, '''image-classification''': TFConvNextForImageClassification}
				    if is_tf_available()
				    else {}
				)

				SCREAMING_SNAKE_CASE_			:				Any									=   False
				SCREAMING_SNAKE_CASE_			:				Optional[Any]									=   False
				SCREAMING_SNAKE_CASE_			:				List[str]									=   False
				SCREAMING_SNAKE_CASE_			:				Tuple									=   False
				SCREAMING_SNAKE_CASE_			:				List[str]									=   False





				def 				__UpperCAmelCase				( self							:	int				)   ->	Any:
							a       =     TFConvNextModelTester(self				)
							a       =     ConfigTester(
							    self   , config_class=_a   , has_text_modality=_a   , hidden_size=37   , )





				@unittest.skip(reason="ConvNext does not use inputs_embeds"				)
				def 				__UpperCAmelCase				( self							:	Union[str, Any]				)   ->	int:
							pass





				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices("GPU"				)				) == 0   , reason="TF does not support backprop for grouped convolutions on CPU."   , )
				@slow
				def 				__UpperCAmelCase				( self							:	Optional[int]				)   ->	Tuple:
							super().test_keras_fit()





				@unittest.skip(reason="ConvNext does not support input and output embeddings"				)
				def 				__UpperCAmelCase				( self							:	Any				)   ->	Any:
							pass





				def 				__UpperCAmelCase				( self							:	Optional[int]				)   ->	int:
							a       =     self.model_tester.prepare_config_and_inputs_for_common()

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

										a       =     ["pixel_values"]
										self.assertListEqual(arg_names[:1]   , _a				)





				def 				__UpperCAmelCase				( self							:	Optional[Any]				)   ->	Tuple:
							a       =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_a				)





				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices("GPU"				)				) == 0   , reason="TF does not support backprop for grouped convolutions on CPU."   , )
				def 				__UpperCAmelCase				( self							:	List[str]				)   ->	Optional[int]:
							super().test_dataset_conversion()





				def 				__UpperCAmelCase				( self							:	int				)   ->	Union[str, Any]:
							def check_hidden_states_output(__lowerCamelCase							:	int   , __lowerCamelCase							:	Union[str, Any]   , __lowerCamelCase							:	Union[str, Any]				):
										a       =     model_class(_a				)

										a       =     model(**self._prepare_for_class(_a   , _a				)				)
										a       =     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

										a       =     self.model_tester.num_stages
										self.assertEqual(len(_a				)   , expected_num_stages + 1				)

										# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
										self.assertListEqual(
										    list(hidden_states[0].shape[-2:]				)   , [self.model_tester.image_size // 4, self.model_tester.image_size // 4]   , )

							a       =     self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										a       =     True
										check_hidden_states_output(_a   , _a   , _a				)

										# check that output_hidden_states also work using config
										del inputs_dict["output_hidden_states"]
										a       =     True

										check_hidden_states_output(_a   , _a   , _a				)





				def 				__UpperCAmelCase				( self							:	List[Any]				)   ->	Union[str, Any]:
							a       =     self.model_tester.prepare_config_and_inputs_for_common()

							def check_equivalence(__lowerCamelCase							:	Any   , __lowerCamelCase							:	Any   , __lowerCamelCase							:	Optional[int]   , __lowerCamelCase							:	Union[str, Any]={}				):
										a       =     model(_a   , return_dict=_a   , **_a				)
										a       =     model(_a   , return_dict=_a   , **_a				).to_tuple()

										def recursive_check(__lowerCamelCase							:	Optional[int]   , __lowerCamelCase							:	Tuple				):
													if isinstance(_a   , (List, Tuple)				):
																for tuple_iterable_value, dict_iterable_value in zip(_a   , _a				):
																			recursive_check(_a   , _a				)
													elif tuple_object is None:
																return
													else:
																self.assertTrue(
																    all(tf.equal(_a   , _a				)				)   , msg=(
																        "Tuple and dict output are not equal. Difference:"
																        f""" {tf.math.reduce_max(tf.abs(tuple_object - dict_object				)				)}"""
																    )   , )

													recursive_check(_a   , _a				)

							for model_class in self.all_model_classes:
										a       =     model_class(_a				)

										a       =     self._prepare_for_class(_a   , _a				)
										a       =     self._prepare_for_class(_a   , _a				)
										check_equivalence(_a   , _a   , _a				)

										a       =     self._prepare_for_class(_a   , _a   , return_labels=_a				)
										a       =     self._prepare_for_class(_a   , _a   , return_labels=_a				)
										check_equivalence(_a   , _a   , _a				)

										a       =     self._prepare_for_class(_a   , _a				)
										a       =     self._prepare_for_class(_a   , _a				)
										check_equivalence(_a   , _a   , _a   , {"output_hidden_states": True}				)

										a       =     self._prepare_for_class(_a   , _a   , return_labels=_a				)
										a       =     self._prepare_for_class(_a   , _a   , return_labels=_a				)
										check_equivalence(_a   , _a   , _a   , {"output_hidden_states": True}				)





				def 				__UpperCAmelCase				( self							:	str				)   ->	List[Any]:
							a       =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_a				)





				@slow
				def 				__UpperCAmelCase				( self							:	int				)   ->	Union[str, Any]:
							a       =     TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"				)
							self.assertIsNotNone(_a				)




def      __magic_name__							(					):






			'''simple docstring'''







			a       =     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  )
			return image






@require_tf
@require_vision
class        snake_case__				(unittest.TestCase  ):
				"""simple docstring"""



				@cached_property
				def 				__UpperCAmelCase				( self							:	List[str]				)   ->	Dict:
							return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224"				) if is_vision_available() else None





				@slow
				def 				__UpperCAmelCase				( self							:	List[Any]				)   ->	Tuple:
							a       =     TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"				)

							a       =     self.default_image_processor
							a       =     prepare_img()
							a       =     image_processor(images=_a   , return_tensors="tf"				)

							# forward pass
							a       =     model(**_a				)

							# verify the logits
							a       =     tf.TensorShape((1, 10_00)				)
							self.assertEqual(outputs.logits.shape   , _a				)

							a       =     tf.constant([-0.0_260, -0.4_739, 0.1_911]				)

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










import os







def 	A (		):
     UpperCAmelCase    :					Any			=      os.path.dirname(os.path.realpath(A__			)			)
     UpperCAmelCase    :					Optional[int]			=      os.path.join(A__		,		"triangle.txt"			)

     with open(A__			) as f:
          UpperCAmelCase    :					Union[str, Any]			=      f.readlines()

     UpperCAmelCase    :					List[str]			=      [[int(A__			) for y in x.rstrip("\r\n"			).split(" "			)] for x in triangle]

     for i in range(1		,		len(A__			)			):
          for j in range(len(a[i]			)			):
               UpperCAmelCase    :					List[Any]			=      a[i - 1][j] if j != len(a[i - 1]			) else 0
               UpperCAmelCase    :					Optional[int]			=      a[i - 1][j - 1] if j > 0 else 0
               a[i][j] += max(A__		,		A__			)
     return max(a[-1]			)


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



