

import inspect
import unittest

from transformers import ConvNextConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel
		from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor






class _UpperCamelCase							:
			'''simple docstring'''







			def __init__(			self							: Union[str, Any]      ,      a							: str      ,      a							: str=13      ,      a							: Union[str, Any]=32      ,      a							: Tuple=3      ,      a							: List[str]=4      ,      a							: Optional[Any]=[10, 20, 30, 40]      ,      a							: List[str]=[2, 2, 3, 2]      ,      a							: Any=True      ,      a							: Optional[Any]=True      ,      a							: Union[str, Any]=37      ,      a							: List[str]="gelu"      ,      a							: str=10      ,      a							: int=0.02      ,      a							: Optional[Any]=["stage2", "stage3", "stage4"]      ,      a							: Optional[int]=[2, 3, 4]      ,      a							: int=None      ,      )							->       List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      int				     =       parent
				SCREAMING_SNAKE_CASE   :      List[str]				     =       batch_size
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       image_size
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       num_channels
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       num_stages
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       hidden_sizes
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       depths
				SCREAMING_SNAKE_CASE   :      Dict				     =       is_training
				SCREAMING_SNAKE_CASE   :      int				     =       use_labels
				SCREAMING_SNAKE_CASE   :      int				     =       intermediate_size
				SCREAMING_SNAKE_CASE   :      str				     =       hidden_act
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       num_labels
				SCREAMING_SNAKE_CASE   :      List[str]				     =       initializer_range
				SCREAMING_SNAKE_CASE   :      int				     =       out_features
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       out_indices
				SCREAMING_SNAKE_CASE   :      str				     =       scope
			def 							__UpperCamelCase (			self							: List[Any]	)							->       List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

				SCREAMING_SNAKE_CASE   :      Tuple				     =       None
				if self.use_labels:
					SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       ids_tensor([self.batch_size]      ,      self.num_labels	)

				SCREAMING_SNAKE_CASE   :      str				     =       self.get_config()
				return config, pixel_values, labels
			def 							__UpperCamelCase (			self							: Any	)							->       Tuple:

				"""simple docstring"""

				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=_snake_case      ,      initializer_range=self.initializer_range      ,      out_features=self.out_features      ,      out_indices=self.out_indices      ,      num_labels=self.num_labels      ,      )
			def 							__UpperCamelCase (			self							: Tuple      ,      a							: Any      ,      a							: Union[str, Any]      ,      a							: Union[str, Any]	)							->       int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       ConvNextModel(config=_snake_case	)
				model.to(_snake_case	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      Dict				     =       model(_snake_case	)
				# 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							: str      ,      a							: Optional[int]      ,      a							: int      ,      a							: List[str]	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       ConvNextForImageClassification(_snake_case	)
				model.to(_snake_case	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       model(_snake_case      ,      labels=_snake_case	)
				self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_labels)	)
			def 							__UpperCamelCase (			self							: List[str]      ,      a							: Union[str, Any]      ,      a							: str      ,      a							: Dict	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       ConvNextBackbone(config=_snake_case	)
				model.to(_snake_case	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      int				     =       model(_snake_case	)

				# verify hidden states
				self.parent.assertEqual(len(result.feature_maps	)      ,      len(config.out_features	)	)
				self.parent.assertListEqual(list(result.feature_maps[0].shape	)      ,      [self.batch_size, self.hidden_sizes[1], 4, 4]	)

				# verify channels
				self.parent.assertEqual(len(model.channels	)      ,      len(config.out_features	)	)
				self.parent.assertListEqual(model.channels      ,      config.hidden_sizes[1:]	)

				# verify backbone works with out_features=None
				SCREAMING_SNAKE_CASE   :      Any				     =       None
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       ConvNextBackbone(config=_snake_case	)
				model.to(_snake_case	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      str				     =       model(_snake_case	)

				# verify feature maps
				self.parent.assertEqual(len(result.feature_maps	)      ,      1	)
				self.parent.assertListEqual(list(result.feature_maps[0].shape	)      ,      [self.batch_size, self.hidden_sizes[-1], 1, 1]	)

				# verify channels
				self.parent.assertEqual(len(model.channels	)      ,      1	)
				self.parent.assertListEqual(model.channels      ,      [config.hidden_sizes[-1]]	)






			def 							__UpperCamelCase (			self							: List[Any]	)							->       List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.prepare_config_and_inputs()
				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       config_and_inputs
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       {"pixel_values": pixel_values}
				return config, inputs_dict






@require_torch
class _UpperCamelCase							(							UpperCAmelCase_							,	UpperCAmelCase_							,	unittest.TestCase   ):
			'''simple docstring'''







			lowerCamelCase__           =(
			    (
			        ConvNextModel,
			        ConvNextForImageClassification,
			        ConvNextBackbone,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase__           =(
			    {"feature-extraction": ConvNextModel, "image-classification": ConvNextForImageClassification}
			    if is_torch_available()
			    else {}
			)

			lowerCamelCase__           =True
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			def 							__UpperCamelCase (			self							: Optional[Any]	)							->       List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[str]				     =       ConvNextModelTester(self	)
				SCREAMING_SNAKE_CASE   :      str				     =       ConfigTester(self      ,      config_class=_snake_case      ,      has_text_modality=_snake_case      ,      hidden_size=37	)
			def 							__UpperCamelCase (			self							: List[Any]	)							->       Union[str, Any]:

				"""simple docstring"""

				self.create_and_test_config_common_properties()
				self.config_tester.create_and_test_config_to_json_string()
				self.config_tester.create_and_test_config_to_json_file()
				self.config_tester.create_and_test_config_from_and_save_pretrained()
				self.config_tester.create_and_test_config_with_num_labels()
				self.config_tester.check_config_can_be_init_without_params()
				self.config_tester.check_config_arguments_init()
			def 							__UpperCamelCase (			self							: Dict	)							->       List[Any]:

				"""simple docstring"""

				return
			@unittest.skip(reason="ConvNext does not use inputs_embeds"	)
			def 							__UpperCamelCase (			self							: int	)							->       List[Any]:

				"""simple docstring"""

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

				"""simple docstring"""

				pass
			@unittest.skip(reason="ConvNext does not use feedforward chunking"	)
			def 							__UpperCamelCase (			self							: str	)							->       Tuple:

				"""simple docstring"""

				pass
			def 							__UpperCamelCase (			self							: Optional[Any]	)							->       str:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE   :      Dict				     =       model_class(_snake_case	)
					SCREAMING_SNAKE_CASE   :      str				     =       inspect.signature(model.forward	)
					# signature.parameters is an OrderedDict => so arg_names order is deterministic
					SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       [*signature.parameters.keys()]

					SCREAMING_SNAKE_CASE   :      List[str]				     =       ["pixel_values"]
					self.assertListEqual(arg_names[:1]      ,      _snake_case	)
			def 							__UpperCamelCase (			self							: str	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      str				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_snake_case	)
			def 							__UpperCamelCase (			self							: int	)							->       Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_backbone(*_snake_case	)
			def 							__UpperCamelCase (			self							: Tuple	)							->       List[Any]:

				"""simple docstring"""

				def check_hidden_states_output(a							: int      ,      a							: int      ,      a							: Union[str, Any]	):
					SCREAMING_SNAKE_CASE   :      Any				     =       model_class(_snake_case	)
					model.to(_snake_case	)
					model.eval()

					with torch.no_grad():
						SCREAMING_SNAKE_CASE   :      Any				     =       model(**self._prepare_for_class(_snake_case      ,      _snake_case	)	)

					SCREAMING_SNAKE_CASE   :      List[Any]				     =       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

					SCREAMING_SNAKE_CASE   :      int				     =       self.model_tester.num_stages
					self.assertEqual(len(_snake_case	)      ,      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]      ,      )

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       True
					check_hidden_states_output(_snake_case      ,      _snake_case      ,      _snake_case	)

					# check that output_hidden_states also work using config
					del inputs_dict["output_hidden_states"]
					SCREAMING_SNAKE_CASE   :      List[str]				     =       True

					check_hidden_states_output(_snake_case      ,      _snake_case      ,      _snake_case	)
			def 							__UpperCamelCase (			self							: Optional[Any]	)							->       Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_snake_case	)






			@slow
			def 							__UpperCamelCase (			self							: str	)							->       Optional[int]:

				"""simple docstring"""

				for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       ConvNextModel.from_pretrained(_snake_case	)
					self.assertIsNotNone(_snake_case	)

def  lowerCamelCase__ (					):
	SCREAMING_SNAKE_CASE   :      Dict				     =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
	return image






@require_torch
@require_vision
class _UpperCamelCase							(							unittest.TestCase   ):
			'''simple docstring'''







			@cached_property
			def 							__UpperCamelCase (			self							: Optional[Any]	)							->       str:

				"""simple docstring"""

				return AutoImageProcessor.from_pretrained("facebook/convnext-tiny-224"	) if is_vision_available() else None






			@slow
			def 							__UpperCamelCase (			self							: List[str]	)							->       List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       ConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"	).to(_snake_case	)

				SCREAMING_SNAKE_CASE   :      List[str]				     =       self.default_image_processor
				SCREAMING_SNAKE_CASE   :      Dict				     =       prepare_img()
				SCREAMING_SNAKE_CASE   :      Any				     =       image_processor(images=_snake_case      ,      return_tensors="pt"	).to(_snake_case	)

				# forward pass
				with torch.no_grad():
					SCREAMING_SNAKE_CASE   :      Optional[int]				     =       model(**_snake_case	)

				# verify the logits
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       torch.Size((1, 1000)	)
				self.assertEqual(outputs.logits.shape      ,      _snake_case	)

				SCREAMING_SNAKE_CASE   :      str				     =       torch.tensor([-0.0260, -0.4739, 0.1911]	).to(_snake_case	)

				self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,      _snake_case      ,      atol=1e-4	)	)






@require_torch
class _UpperCamelCase							(							unittest.TestCase							,	UpperCAmelCase_   ):
			'''simple docstring'''







			lowerCamelCase__           =(ConvNextBackbone,) if is_torch_available() else ()
			lowerCamelCase__           =ConvNextConfig

			lowerCamelCase__           =False
			def 							__UpperCamelCase (			self							: Union[str, Any]	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       ConvNextModelTester(self	)


def  lowerCamelCase__ (					_a = 50):
	SCREAMING_SNAKE_CASE   :      int				     =       [1] * (length + 1)

	for row_length in range(length + 1):
		for tile_length in range(2							,			5):
			for tile_start in range(row_length - tile_length + 1):
				ways_number[row_length] += ways_number[
				    row_length - tile_start - tile_length
				]

	return ways_number[length]


if __name__ == "__main__":
			print(F'''{solution() = }''')