

"""simple docstring"""





import inspect
import unittest

from transformers import DPTConfig
from transformers.file_utils import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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 MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel
							from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import DPTImageProcessor







class        SCREAMING_SNAKE_CASE      :






					"""simple docstring"""



					def __init__( self    :       List[str]     ,lowercase_    :       Tuple     ,lowercase_    :       int=2     ,lowercase_    :       Dict=3_2     ,lowercase_    :       List[str]=1_6     ,lowercase_    :       Optional[int]=3     ,lowercase_    :       Any=True     ,lowercase_    :       List[Any]=True     ,lowercase_    :       List[Any]=3_2     ,lowercase_    :       Optional[int]=4     ,lowercase_    :       Optional[int]=[0, 1, 2, 3]     ,lowercase_    :       Dict=4     ,lowercase_    :       int=3_7     ,lowercase_    :       Any="gelu"     ,lowercase_    :       Optional[int]=0.1     ,lowercase_    :       Union[str, Any]=0.1     ,lowercase_    :       str=0.02     ,lowercase_    :       Tuple=3     ,lowercase_    :       Any=False     ,lowercase_    :       Optional[Any]=None     ,):
						lowerCAmelCase__			:		Any						       =			parent
						lowerCAmelCase__			:		str						       =			batch_size
						lowerCAmelCase__			:		Dict						       =			image_size
						lowerCAmelCase__			:		List[str]						       =			patch_size
						lowerCAmelCase__			:		Union[str, Any]						       =			num_channels
						lowerCAmelCase__			:		List[str]						       =			is_training
						lowerCAmelCase__			:		Dict						       =			use_labels
						lowerCAmelCase__			:		Optional[Any]						       =			hidden_size
						lowerCAmelCase__			:		Tuple						       =			num_hidden_layers
						lowerCAmelCase__			:		Tuple						       =			backbone_out_indices
						lowerCAmelCase__			:		Union[str, Any]						       =			num_attention_heads
						lowerCAmelCase__			:		Union[str, Any]						       =			intermediate_size
						lowerCAmelCase__			:		Optional[Any]						       =			hidden_act
						lowerCAmelCase__			:		Union[str, Any]						       =			hidden_dropout_prob
						lowerCAmelCase__			:		List[str]						       =			attention_probs_dropout_prob
						lowerCAmelCase__			:		Tuple						       =			initializer_range
						lowerCAmelCase__			:		List[Any]						       =			num_labels
						lowerCAmelCase__			:		Dict						       =			scope
						lowerCAmelCase__			:		int						       =			is_hybrid
						# sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
						lowerCAmelCase__			:		Union[str, Any]						       =			(image_size // patch_size) ** 2
						lowerCAmelCase__			:		Tuple						       =			num_patches + 1







					def  __lowerCAmelCase				( self    :       Optional[int]	):
						lowerCAmelCase__			:		Optional[int]						       =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

						lowerCAmelCase__			:		Optional[Any]						       =			None
						if self.use_labels:
							lowerCAmelCase__			:		str						       =			ids_tensor([self.batch_size, self.image_size, self.image_size]     ,self.num_labels	)

						lowerCAmelCase__			:		List[Any]						       =			self.get_config()

						return config, pixel_values, labels







					def  __lowerCAmelCase				( self    :       Any	):
						return DPTConfig(
						    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     ,backbone_out_indices=self.backbone_out_indices     ,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=lowercase_     ,initializer_range=self.initializer_range     ,is_hybrid=self.is_hybrid     ,)







					def  __lowerCAmelCase				( self    :       Optional[Any]     ,lowercase_    :       Optional[Any]     ,lowercase_    :       List[str]     ,lowercase_    :       str	):
						lowerCAmelCase__			:		Optional[Any]						       =			DPTModel(config=lowercase_	)
						model.to(lowercase_	)
						model.eval()
						lowerCAmelCase__			:		int						       =			model(lowercase_	)
						self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size)	)







					def  __lowerCAmelCase				( self    :       List[Any]     ,lowercase_    :       int     ,lowercase_    :       Optional[int]     ,lowercase_    :       List[str]	):
						lowerCAmelCase__			:		int						       =			self.num_labels
						lowerCAmelCase__			:		Optional[int]						       =			DPTForDepthEstimation(lowercase_	)
						model.to(lowercase_	)
						model.eval()
						lowerCAmelCase__			:		List[str]						       =			model(lowercase_	)
						self.parent.assertEqual(result.predicted_depth.shape     ,(self.batch_size, self.image_size, self.image_size)	)







					def  __lowerCAmelCase				( self    :       Tuple     ,lowercase_    :       Union[str, Any]     ,lowercase_    :       Any     ,lowercase_    :       Optional[Any]	):
						lowerCAmelCase__			:		Union[str, Any]						       =			self.num_labels
						lowerCAmelCase__			:		Tuple						       =			DPTForSemanticSegmentation(lowercase_	)
						model.to(lowercase_	)
						model.eval()
						lowerCAmelCase__			:		List[Any]						       =			model(lowercase_     ,labels=lowercase_	)
						self.parent.assertEqual(
						    result.logits.shape     ,(self.batch_size, self.num_labels, self.image_size, self.image_size)	)







					def  __lowerCAmelCase				( self    :       str	):
						lowerCAmelCase__			:		Any						       =			self.prepare_config_and_inputs()
						lowerCAmelCase__			:		Tuple						       =			config_and_inputs
						lowerCAmelCase__			:		Tuple						       =			{'''pixel_values''': pixel_values}
						return config, inputs_dict







@require_torch
class        SCREAMING_SNAKE_CASE      (	a_					,							a_					,							unittest.TestCase ):






					"""simple docstring"""



					lowercase__       				= (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
					lowercase__       				= (
					    {
					        "depth-estimation": DPTForDepthEstimation,
					        "feature-extraction": DPTModel,
					        "image-segmentation": DPTForSemanticSegmentation,
					    }
					    if is_torch_available()
					    else {}
					)

					lowercase__       				= False
					lowercase__       				= False
					lowercase__       				= False







					def  __lowerCAmelCase				( self    :       int	):
						lowerCAmelCase__			:		int						       =			DPTModelTester(self	)
						lowerCAmelCase__			:		Dict						       =			ConfigTester(self     ,config_class=lowercase_     ,has_text_modality=lowercase_     ,hidden_size=3_7	)







					def  __lowerCAmelCase				( self    :       str	):
						self.config_tester.run_common_tests()







					@unittest.skip(reason='''DPT does not use inputs_embeds'''	)
					def  __lowerCAmelCase				( self    :       Dict	):
						pass







					def  __lowerCAmelCase				( self    :       int	):
						lowerCAmelCase__			:		str						       =			self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							lowerCAmelCase__			:		Union[str, Any]						       =			model_class(lowercase_	)
							self.assertIsInstance(model.get_input_embeddings()     ,(nn.Module)	)
							lowerCAmelCase__			:		Tuple						       =			model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(lowercase_     ,nn.Linear	)	)







					def  __lowerCAmelCase				( self    :       List[str]	):
						lowerCAmelCase__			:		int						       =			self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							lowerCAmelCase__			:		int						       =			model_class(lowercase_	)
							lowerCAmelCase__			:		Any						       =			inspect.signature(model.forward	)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							lowerCAmelCase__			:		int						       =			[*signature.parameters.keys()]

							lowerCAmelCase__			:		Tuple						       =			['''pixel_values''']
							self.assertListEqual(arg_names[:1]     ,lowercase_	)







					def  __lowerCAmelCase				( self    :       Union[str, Any]	):
						lowerCAmelCase__			:		List[str]						       =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase_	)







					def  __lowerCAmelCase				( self    :       Any	):
						lowerCAmelCase__			:		Optional[int]						       =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_depth_estimation(*lowercase_	)







					def  __lowerCAmelCase				( self    :       Union[str, Any]	):
						lowerCAmelCase__			:		Tuple						       =			self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_semantic_segmentation(*lowercase_	)







					def  __lowerCAmelCase				( self    :       Optional[int]	):
						for model_class in self.all_model_classes:
							if model_class.__name__ == "DPTForDepthEstimation":
								continue

							lowerCAmelCase__			:		int						       =			self.model_tester.prepare_config_and_inputs_for_common()
							lowerCAmelCase__			:		Optional[Any]						       =			True

							if model_class in get_values(lowercase_	):
								continue

							lowerCAmelCase__			:		Dict						       =			model_class(lowercase_	)
							model.to(lowercase_	)
							model.train()
							lowerCAmelCase__			:		str						       =			self._prepare_for_class(lowercase_     ,lowercase_     ,return_labels=lowercase_	)
							lowerCAmelCase__			:		Tuple						       =			model(**lowercase_	).loss
							loss.backward()







					def  __lowerCAmelCase				( self    :       str	):
						for model_class in self.all_model_classes:
							if model_class.__name__ == "DPTForDepthEstimation":
								continue

							lowerCAmelCase__			:		Dict						       =			self.model_tester.prepare_config_and_inputs_for_common()
							lowerCAmelCase__			:		Optional[int]						       =			False
							lowerCAmelCase__			:		Optional[Any]						       =			True

							if model_class in get_values(lowercase_	) or not model_class.supports_gradient_checkpointing:
								continue
							lowerCAmelCase__			:		List[str]						       =			model_class(lowercase_	)
							model.to(lowercase_	)
							model.gradient_checkpointing_enable()
							model.train()
							lowerCAmelCase__			:		Optional[Any]						       =			self._prepare_for_class(lowercase_     ,lowercase_     ,return_labels=lowercase_	)
							lowerCAmelCase__			:		int						       =			model(**lowercase_	).loss
							loss.backward()







					def  __lowerCAmelCase				( self    :       str	):
						lowerCAmelCase__			:		int						       =			self.model_tester.prepare_config_and_inputs_for_common()

						lowerCAmelCase__			:		Optional[Any]						       =			_config_zero_init(lowercase_	)
						for model_class in self.all_model_classes:
							lowerCAmelCase__			:		Dict						       =			model_class(config=lowercase_	)
							# Skip the check for the backbone
							lowerCAmelCase__			:		Dict						       =			[]
							for name, module in model.named_modules():
								if module.__class__.__name__ == "DPTViTHybridEmbeddings":
									lowerCAmelCase__			:		Tuple						       =			[F'{name}.{key}' for key in module.state_dict().keys()]
									break

							for name, param in model.named_parameters():
								if param.requires_grad:
									if name in backbone_params:
										continue
									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'     ,)







					@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''	)
					def  __lowerCAmelCase				( self    :       str	):
						pass







					@slow
					def  __lowerCAmelCase				( self    :       List[str]	):
						for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCAmelCase__			:		Tuple						       =			DPTModel.from_pretrained(lowercase_	)
							self.assertIsNotNone(lowercase_	)

def 						__SCREAMING_SNAKE_CASE						(   ):
	lowerCAmelCase__			:		int						       =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
	return image







@require_torch
@require_vision
@slow
class        SCREAMING_SNAKE_CASE      (	unittest.TestCase ):






					"""simple docstring"""



					def  __lowerCAmelCase				( self    :       Dict	):
						lowerCAmelCase__			:		str						       =			DPTImageProcessor.from_pretrained('''Intel/dpt-large'''	)
						lowerCAmelCase__			:		str						       =			DPTForDepthEstimation.from_pretrained('''Intel/dpt-large'''	).to(lowercase_	)

						lowerCAmelCase__			:		Any						       =			prepare_img()
						lowerCAmelCase__			:		Optional[Any]						       =			image_processor(images=lowercase_     ,return_tensors='''pt'''	).to(lowercase_	)

						# forward pass
						with torch.no_grad():
							lowerCAmelCase__			:		List[str]						       =			model(**lowercase_	)
							lowerCAmelCase__			:		Union[str, Any]						       =			outputs.predicted_depth

						# verify the predicted depth
						lowerCAmelCase__			:		List[str]						       =			torch.Size((1, 3_8_4, 3_8_4)	)
						self.assertEqual(predicted_depth.shape     ,lowercase_	)

						lowerCAmelCase__			:		Any						       =			torch.tensor(
						    [[6.3199, 6.3629, 6.4148], [6.3850, 6.3615, 6.4166], [6.3519, 6.3176, 6.3575]]	).to(lowercase_	)

						self.assertTrue(torch.allclose(outputs.predicted_depth[0, :3, :3]     ,lowercase_     ,atol=1E-4	)	)







					def  __lowerCAmelCase				( self    :       List[Any]	):
						lowerCAmelCase__			:		Dict						       =			DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''	)
						lowerCAmelCase__			:		List[str]						       =			DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''	).to(lowercase_	)

						lowerCAmelCase__			:		Tuple						       =			prepare_img()
						lowerCAmelCase__			:		int						       =			image_processor(images=lowercase_     ,return_tensors='''pt'''	).to(lowercase_	)

						# forward pass
						with torch.no_grad():
							lowerCAmelCase__			:		int						       =			model(**lowercase_	)

						# verify the logits
						lowerCAmelCase__			:		Optional[Any]						       =			torch.Size((1, 1_5_0, 4_8_0, 4_8_0)	)
						self.assertEqual(outputs.logits.shape     ,lowercase_	)

						lowerCAmelCase__			:		Dict						       =			torch.tensor(
						    [[4.0480, 4.2420, 4.4360], [4.3124, 4.5693, 4.8261], [4.5768, 4.8965, 5.2163]]	).to(lowercase_	)

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







					def  __lowerCAmelCase				( self    :       int	):
						lowerCAmelCase__			:		Tuple						       =			DPTImageProcessor.from_pretrained('''Intel/dpt-large-ade'''	)
						lowerCAmelCase__			:		Optional[int]						       =			DPTForSemanticSegmentation.from_pretrained('''Intel/dpt-large-ade'''	).to(lowercase_	)

						lowerCAmelCase__			:		Optional[Any]						       =			prepare_img()
						lowerCAmelCase__			:		Any						       =			image_processor(images=lowercase_     ,return_tensors='''pt'''	).to(lowercase_	)

						# forward pass
						with torch.no_grad():
							lowerCAmelCase__			:		List[str]						       =			model(**lowercase_	)

						lowerCAmelCase__			:		Any						       =			outputs.logits.detach().cpu()

						lowerCAmelCase__			:		Optional[Any]						       =			image_processor.post_process_semantic_segmentation(outputs=lowercase_     ,target_sizes=[(5_0_0, 3_0_0)]	)
						lowerCAmelCase__			:		Dict						       =			torch.Size((5_0_0, 3_0_0)	)
						self.assertEqual(segmentation[0].shape     ,lowercase_	)

						lowerCAmelCase__			:		int						       =			image_processor.post_process_semantic_segmentation(outputs=lowercase_	)
						lowerCAmelCase__			:		Optional[int]						       =			torch.Size((4_8_0, 4_8_0)	)
						self.assertEqual(segmentation[0].shape     ,lowercase_	)







"""simple docstring"""





from itertools import permutations

def 						__SCREAMING_SNAKE_CASE						(   A_ = 5				):
	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
	lowerCAmelCase__			:		str						       =			list(range(gon_side + 1		,  0		,  -1				)				)
	lowerCAmelCase__			:		int						       =			list(range(gon_side + 2		,  gon_side * 2 + 1				)				)

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

	lowerCAmelCase__			:		Tuple						       =			f'Magic {gon_side}-gon ring is impossible'
	raise ValueError(A_				)

def 						__SCREAMING_SNAKE_CASE						(   A_		,  A_				):
	lowerCAmelCase__			:		Dict						       =			[0] * (gon_side * 3)
	lowerCAmelCase__			:		Optional[int]						       =			perm[0:3]
	perm.append(perm[1]				)

	lowerCAmelCase__			:		int						       =			1 if gon_side < 5 else 2

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

	return result

def 						__SCREAMING_SNAKE_CASE						(   A_				):
	if len(A_				) % 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

	lowerCAmelCase__			:		int						       =			sum(numbers[:3]				)

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


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




