




'''simple docstring'''




import collections
import inspect
import unittest

from transformers import DinatConfig
from transformers.testing_utils import require_natten, 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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch
							from torch import nn

							from transformers import DinatBackbone, DinatForImageClassification, DinatModel
							from transformers.models.dinat.modeling_dinat import DINAT_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor






class      UpperCamelCase_  :







						"""simple docstring"""


						def __init__(  self   :							str				,			_lowerCamelCase   :							Optional[Any]				,			_lowerCamelCase   :							Tuple=13				,			_lowerCamelCase   :							str=64				,			_lowerCamelCase   :							int=4				,			_lowerCamelCase   :							Tuple=3				,			_lowerCamelCase   :							List[str]=16				,			_lowerCamelCase   :							Any=[1, 2, 1]				,			_lowerCamelCase   :							Tuple=[2, 4, 8]				,			_lowerCamelCase   :							int=3				,			_lowerCamelCase   :							Any=[[3], [1, 2], [1]]				,			_lowerCamelCase   :							Tuple=2.0				,			_lowerCamelCase   :							Optional[Any]=True				,			_lowerCamelCase   :							Optional[Any]=0.0				,			_lowerCamelCase   :							str=0.0				,			_lowerCamelCase   :							str=0.1				,			_lowerCamelCase   :							str="gelu"				,			_lowerCamelCase   :							List[str]=True				,			_lowerCamelCase   :							List[Any]=0.02				,			_lowerCamelCase   :							Optional[Any]=1E-5				,			_lowerCamelCase   :							str=True				,			_lowerCamelCase   :							List[str]=None				,			_lowerCamelCase   :							str=True				,			_lowerCamelCase   :							Optional[int]=10				,			_lowerCamelCase   :							Any=["stage1", "stage2"]				,			_lowerCamelCase   :							int=[1, 2]				,			):





											"""simple docstring"""







											A_			:  List[str]			=				parent
											A_			:  Any			=				batch_size
											A_			:  Optional[Any]			=				image_size
											A_			:  List[Any]			=				patch_size
											A_			:  Tuple			=				num_channels
											A_			:  Optional[Any]			=				embed_dim
											A_			:  str			=				depths
											A_			:  Optional[Any]			=				num_heads
											A_			:  int			=				kernel_size
											A_			:  List[Any]			=				dilations
											A_			:  Optional[Any]			=				mlp_ratio
											A_			:  Dict			=				qkv_bias
											A_			:  Tuple			=				hidden_dropout_prob
											A_			:  Any			=				attention_probs_dropout_prob
											A_			:  Union[str, Any]			=				drop_path_rate
											A_			:  Union[str, Any]			=				hidden_act
											A_			:  Tuple			=				patch_norm
											A_			:  Optional[Any]			=				layer_norm_eps
											A_			:  int			=				initializer_range
											A_			:  Any			=				is_training
											A_			:  Any			=				scope
											A_			:  List[Any]			=				use_labels
											A_			:  Dict			=				num_labels
											A_			:  Union[str, Any]			=				out_features
											A_			:  Tuple			=				out_indices







						def    _a     (  self   :							str     ):





											"""simple docstring"""







											A_			:  Dict			=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

											A_			:  str			=				None
											if self.use_labels:
																A_			:  List[str]			=				ids_tensor([self.batch_size]				,			self.num_labels     )

											A_			:  Any			=				self.get_config()

											return config, pixel_values, labels







						def    _a     (  self   :							str     ):





											"""simple docstring"""







											return DinatConfig(
											    num_labels=self.num_labels				,			image_size=self.image_size				,			patch_size=self.patch_size				,			num_channels=self.num_channels				,			embed_dim=self.embed_dim				,			depths=self.depths				,			num_heads=self.num_heads				,			kernel_size=self.kernel_size				,			dilations=self.dilations				,			mlp_ratio=self.mlp_ratio				,			qkv_bias=self.qkv_bias				,			hidden_dropout_prob=self.hidden_dropout_prob				,			attention_probs_dropout_prob=self.attention_probs_dropout_prob				,			drop_path_rate=self.drop_path_rate				,			hidden_act=self.hidden_act				,			patch_norm=self.patch_norm				,			layer_norm_eps=self.layer_norm_eps				,			initializer_range=self.initializer_range				,			out_features=self.out_features				,			out_indices=self.out_indices				,			)







						def    _a     (  self   :							Optional[Any]				,			_lowerCamelCase   :							Tuple				,			_lowerCamelCase   :							Optional[Any]				,			_lowerCamelCase   :							Optional[Any]     ):





											"""simple docstring"""







											A_			:  Union[str, Any]			=				DinatModel(config=_lowerCamelCase     )
											model.to(_lowerCamelCase     )
											model.eval()
											A_			:  Any			=				model(_lowerCamelCase     )

											A_			:  int			=				(config.image_size // config.patch_size) // (2 ** (len(config.depths     ) - 1))
											A_			:  List[str]			=				int(config.embed_dim * 2 ** (len(config.depths     ) - 1)     )

											self.parent.assertEqual(
											    result.last_hidden_state.shape				,			(self.batch_size, expected_height, expected_width, expected_dim)     )







						def    _a     (  self   :							Optional[Any]				,			_lowerCamelCase   :							Union[str, Any]				,			_lowerCamelCase   :							Optional[Any]				,			_lowerCamelCase   :							Tuple     ):





											"""simple docstring"""







											A_			:  Tuple			=				DinatForImageClassification(_lowerCamelCase     )
											model.to(_lowerCamelCase     )
											model.eval()
											A_			:  Optional[Any]			=				model(_lowerCamelCase				,			labels=_lowerCamelCase     )
											self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.num_labels)     )

											# test greyscale images
											A_			:  Dict			=				1
											A_			:  List[str]			=				DinatForImageClassification(_lowerCamelCase     )
											model.to(_lowerCamelCase     )
											model.eval()

											A_			:  Optional[int]			=				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
											A_			:  Dict			=				model(_lowerCamelCase     )
											self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.num_labels)     )







						def    _a     (  self   :							Tuple				,			_lowerCamelCase   :							Tuple				,			_lowerCamelCase   :							Tuple				,			_lowerCamelCase   :							Tuple     ):





											"""simple docstring"""







											A_			:  Tuple			=				DinatBackbone(config=_lowerCamelCase     )
											model.to(_lowerCamelCase     )
											model.eval()
											A_			:  Dict			=				model(_lowerCamelCase     )

											# 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, model.channels[0], 16, 16]     )

											# verify channels
											self.parent.assertEqual(len(model.channels     )				,			len(config.out_features     )     )

											# verify backbone works with out_features=None
											A_			:  List[str]			=				None
											A_			:  str			=				DinatBackbone(config=_lowerCamelCase     )
											model.to(_lowerCamelCase     )
											model.eval()
											A_			:  Optional[Any]			=				model(_lowerCamelCase     )

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

											# verify channels
											self.parent.assertEqual(len(model.channels     )				,			1     )







						def    _a     (  self   :							str     ):





											"""simple docstring"""







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






@require_natten
@require_torch
class      UpperCamelCase_  (a__,   a__,   unittest.TestCase  ):







						"""simple docstring"""


						_lowerCAmelCase         =       (
						    (
						        DinatModel,
						        DinatForImageClassification,
						        DinatBackbone,
						    )
						    if is_torch_available()
						    else ()
						)
						_lowerCAmelCase         =       (
						    {'feature-extraction': DinatModel, 'image-classification': DinatForImageClassification}
						    if is_torch_available()
						    else {}
						)
						_lowerCAmelCase         =       False

						_lowerCAmelCase         =       False
						_lowerCAmelCase         =       False
						_lowerCAmelCase         =       False
						_lowerCAmelCase         =       False







						def    _a     (  self   :							Optional[Any]     ):





											"""simple docstring"""







											A_			:  Dict			=				DinatModelTester(self     )
											A_			:  Dict			=				ConfigTester(self				,			config_class=_lowerCamelCase				,			embed_dim=37     )







						def    _a     (  self   :							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    _a     (  self   :							Optional[Any]     ):





											"""simple docstring"""







											return







						def    _a     (  self   :							List[Any]     ):





											"""simple docstring"""







											A_			:  Optional[int]			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_lowerCamelCase     )







						def    _a     (  self   :							List[str]     ):





											"""simple docstring"""







											A_			:  Dict			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase     )







						def    _a     (  self   :							Optional[Any]     ):





											"""simple docstring"""







											A_			:  List[str]			=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_backbone(*_lowerCamelCase     )







						@unittest.skip(reason='''Dinat does not use inputs_embeds'''     )
						def    _a     (  self   :							Tuple     ):





											"""simple docstring"""







											pass







						@unittest.skip(reason='''Dinat does not use feedforward chunking'''     )
						def    _a     (  self   :							List[Any]     ):





											"""simple docstring"""







											pass







						def    _a     (  self   :							Optional[int]     ):





											"""simple docstring"""







											A_			,A_			:  Any			=				self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																A_			:  Dict			=				model_class(_lowerCamelCase     )
																self.assertIsInstance(model.get_input_embeddings()				,			(nn.Module)     )
																A_			:  List[Any]			=				model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(_lowerCamelCase				,			nn.Linear     )     )







						def    _a     (  self   :							Union[str, Any]     ):





											"""simple docstring"""







											A_			,A_			:  Optional[int]			=				self.model_tester.prepare_config_and_inputs_for_common()

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

																A_			:  Union[str, Any]			=				['''pixel_values''']
																self.assertListEqual(arg_names[:1]				,			_lowerCamelCase     )







						def    _a     (  self   :							Optional[int]     ):





											"""simple docstring"""







											self.skipTest('''Dinat\'s attention operation is handled entirely by NATTEN.'''     )







						def    _a     (  self   :							Optional[Any]				,			_lowerCamelCase   :							Optional[Any]				,			_lowerCamelCase   :							List[Any]				,			_lowerCamelCase   :							List[Any]				,			_lowerCamelCase   :							str     ):





											"""simple docstring"""







											A_			:  Optional[int]			=				model_class(_lowerCamelCase     )
											model.to(_lowerCamelCase     )
											model.eval()

											with torch.no_grad():
																A_			:  str			=				model(**self._prepare_for_class(_lowerCamelCase				,			_lowerCamelCase     )     )

											A_			:  Optional[int]			=				outputs.hidden_states

											A_			:  int			=				getattr(
											    self.model_tester				,			'''expected_num_hidden_layers'''				,			len(self.model_tester.depths     ) + 1     )
											self.assertEqual(len(_lowerCamelCase     )				,			_lowerCamelCase     )

											# Dinat has a different seq_length
											A_			:  Tuple			=				(
											    config.patch_size
											    if isinstance(config.patch_size				,			collections.abc.Iterable     )
											    else (config.patch_size, config.patch_size)
											)

											A_			:  Optional[int]			=				image_size[0] // patch_size[0]
											A_			:  List[Any]			=				image_size[1] // patch_size[1]

											self.assertListEqual(
											    list(hidden_states[0].shape[-3:]     )				,			[height, width, self.model_tester.embed_dim]				,			)

											if model_class.__name__ != "DinatBackbone":
																A_			:  Union[str, Any]			=				outputs.reshaped_hidden_states
																self.assertEqual(len(_lowerCamelCase     )				,			_lowerCamelCase     )

																A_			,A_			,A_			,A_			:  Any			=				reshaped_hidden_states[0].shape
																A_			:  Optional[int]			=				(
																    reshaped_hidden_states[0].view(_lowerCamelCase				,			_lowerCamelCase				,			_lowerCamelCase				,			_lowerCamelCase     ).permute(0				,			2				,			3				,			1     )
																)
																self.assertListEqual(
																    list(reshaped_hidden_states.shape[-3:]     )				,			[height, width, self.model_tester.embed_dim]				,			)







						def    _a     (  self   :							int     ):





											"""simple docstring"""







											A_			,A_			:  Optional[int]			=				self.model_tester.prepare_config_and_inputs_for_common()

											A_			:  Optional[Any]			=				(
											    self.model_tester.image_size
											    if isinstance(self.model_tester.image_size				,			collections.abc.Iterable     )
											    else (self.model_tester.image_size, self.model_tester.image_size)
											)

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

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																A_			:  int			=				True

																self.check_hidden_states_output(_lowerCamelCase				,			_lowerCamelCase				,			_lowerCamelCase				,			_lowerCamelCase     )







						@slow
						def    _a     (  self   :							List[Any]     ):





											"""simple docstring"""







											for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																A_			:  List[str]			=				DinatModel.from_pretrained(_lowerCamelCase     )
																self.assertIsNotNone(_lowerCamelCase     )







						def    _a     (  self   :							str     ):





											"""simple docstring"""







											A_			,A_			:  List[str]			=				self.model_tester.prepare_config_and_inputs_for_common()

											A_			:  int			=				_config_zero_init(_lowerCamelCase     )
											for model_class in self.all_model_classes:
																A_			:  Optional[int]			=				model_class(config=_lowerCamelCase     )
																for name, param in model.named_parameters():
																					if "embeddings" not in name and param.requires_grad:
																										self.assertIn(
																										    ((param.data.mean() * 1E9).round() / 1E9).item()				,			[0.0, 1.0]				,			msg=f'Parameter {name} of model {model_class} seems not properly initialized'				,			)






@require_natten
@require_vision
@require_torch
class      UpperCamelCase_  (unittest.TestCase  ):







						"""simple docstring"""


						@cached_property
						def    _a     (  self   :							List[str]     ):





											"""simple docstring"""







											return AutoImageProcessor.from_pretrained('''shi-labs/dinat-mini-in1k-224'''     ) if is_vision_available() else None







						@slow
						def    _a     (  self   :							List[str]     ):





											"""simple docstring"""







											A_			:  Union[str, Any]			=				DinatForImageClassification.from_pretrained('''shi-labs/dinat-mini-in1k-224'''     ).to(_lowerCamelCase     )
											A_			:  int			=				self.default_image_processor

											A_			:  Optional[Any]			=				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
											A_			:  List[Any]			=				image_processor(images=_lowerCamelCase				,			return_tensors='''pt'''     ).to(_lowerCamelCase     )

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

											# verify the logits
											A_			:  str			=				torch.Size((1, 1000)     )
											self.assertEqual(outputs.logits.shape				,			_lowerCamelCase     )
											A_			:  Dict			=				torch.tensor([-0.15_45, -0.76_67, 0.46_42]     ).to(_lowerCamelCase     )
											self.assertTrue(torch.allclose(outputs.logits[0, :3]				,			_lowerCamelCase				,			atol=1E-4     )     )






@require_torch
@require_natten
class      UpperCamelCase_  (unittest.TestCase,   a__  ):







						"""simple docstring"""


						_lowerCAmelCase         =       (DinatBackbone,) if is_torch_available() else ()
						_lowerCAmelCase         =       DinatConfig







						def    _a     (  self   :							Optional[Any]     ):





											"""simple docstring"""







											A_			:  str			=				DinatModelTester(self     )









'''simple docstring'''




from itertools import permutations




def     snake_case__     (			lowerCamelCase__					: int = 5      ) -> int:
					if gon_side < 3 or gon_side > 5:
										raise ValueError('''gon_side must be in the range [3, 5]'''      )

					# Since it's 16, we know 10 is on the outer ring
					# Put the big numbers at the end so that they are never the first number
					A_			:  int			=				list(range(gon_side + 1      ,		0      ,		-1      )      )
					A_			:  List[Any]			=				list(range(gon_side + 2      ,		gon_side * 2 + 1      )      )

					for perm in permutations(small_numbers + big_numbers      ):
										A_			:  Optional[int]			=				generate_gon_ring(lowerCamelCase__      ,		list(lowerCamelCase__      )      )
										if is_magic_gon(lowerCamelCase__      ):
															return int(''''''.join(str(lowerCamelCase__      ) for n in numbers      )      )

					A_			:  Optional[Any]			=				f'Magic {gon_side}-gon ring is impossible'
					raise ValueError(lowerCamelCase__      )




def     snake_case__     (			lowerCamelCase__					: int      ,		lowerCamelCase__					: list[int]      ) -> list[int]:
					A_			:  List[Any]			=				[0] * (gon_side * 3)
					A_			:  Optional[Any]			=				perm[0:3]
					perm.append(perm[1]      )

					A_			:  Optional[Any]			=				1 if gon_side < 5 else 2

					for i in range(1      ,		len(lowerCamelCase__      ) // 3 + magic_number      ):
										A_			:  List[str]			=				perm[2 * i + 1]
										A_			:  str			=				result[3 * i - 1]
										A_			:  Union[str, Any]			=				perm[2 * i + 2]

					return result




def     snake_case__     (			lowerCamelCase__					: list[int]      ) -> bool:
					if len(lowerCamelCase__      ) % 3 != 0:
										raise ValueError('''a gon ring should have a length that is a multiple of 3'''      )

					if min(numbers[::3]      ) != numbers[0]:
										return False

					A_			:  Union[str, Any]			=				sum(numbers[:3]      )

					return all(sum(numbers[i : i + 3]      ) == total for i in range(3      ,		len(lowerCamelCase__      )      ,		3      )      )


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



