





'''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 				lowerCAmelCase_						:




	def __init__(		self     ,       _lowerCAmelCase     ,       _lowerCAmelCase=13     ,       _lowerCAmelCase=64     ,       _lowerCAmelCase=4     ,       _lowerCAmelCase=3     ,       _lowerCAmelCase=16     ,       _lowerCAmelCase=[1, 2, 1]     ,       _lowerCAmelCase=[2, 4, 8]     ,       _lowerCAmelCase=3     ,       _lowerCAmelCase=[[3], [1, 2], [1]]     ,       _lowerCAmelCase=2.0     ,       _lowerCAmelCase=True     ,       _lowerCAmelCase=0.0     ,       _lowerCAmelCase=0.0     ,       _lowerCAmelCase=0.1     ,       _lowerCAmelCase="gelu"     ,       _lowerCAmelCase=True     ,       _lowerCAmelCase=0.02     ,       _lowerCAmelCase=1E-5     ,       _lowerCAmelCase=True     ,       _lowerCAmelCase=None     ,       _lowerCAmelCase=True     ,       _lowerCAmelCase=10     ,       _lowerCAmelCase=["stage1", "stage2"]     ,       _lowerCAmelCase=[1, 2]     ,       )      ->							Optional[int]:
					_lowerCAmelCase		     = parent
					_lowerCAmelCase		     = batch_size
					_lowerCAmelCase		     = image_size
					_lowerCAmelCase		     = patch_size
					_lowerCAmelCase		     = num_channels
					_lowerCAmelCase		     = embed_dim
					_lowerCAmelCase		     = depths
					_lowerCAmelCase		     = num_heads
					_lowerCAmelCase		     = kernel_size
					_lowerCAmelCase		     = dilations
					_lowerCAmelCase		     = mlp_ratio
					_lowerCAmelCase		     = qkv_bias
					_lowerCAmelCase		     = hidden_dropout_prob
					_lowerCAmelCase		     = attention_probs_dropout_prob
					_lowerCAmelCase		     = drop_path_rate
					_lowerCAmelCase		     = hidden_act
					_lowerCAmelCase		     = patch_norm
					_lowerCAmelCase		     = layer_norm_eps
					_lowerCAmelCase		     = initializer_range
					_lowerCAmelCase		     = is_training
					_lowerCAmelCase		     = scope
					_lowerCAmelCase		     = use_labels
					_lowerCAmelCase		     = num_labels
					_lowerCAmelCase		     = out_features
					_lowerCAmelCase		     = out_indices




	def 							_snake_case				(		self						)      ->							Optional[Any]:
					_lowerCAmelCase		     = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

					_lowerCAmelCase		     = None
					if self.use_labels:
									_lowerCAmelCase		     = ids_tensor([self.batch_size]     ,       self.num_labels						)

					_lowerCAmelCase		     = self.get_config()

					return config, pixel_values, labels




	def 							_snake_case				(		self						)      ->							int:
					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 							_snake_case				(		self     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase						)      ->							Optional[int]:
					_lowerCAmelCase		     = DinatModel(config=_lowerCAmelCase						)
					model.to(_lowerCAmelCase						)
					model.eval()
					_lowerCAmelCase		     = model(_lowerCAmelCase						)

					_lowerCAmelCase		     = _lowerCAmelCase		     = (config.image_size // config.patch_size) // (2 ** (len(config.depths						) - 1))
					_lowerCAmelCase		     = 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 							_snake_case				(		self     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase						)      ->							str:
					_lowerCAmelCase		     = DinatForImageClassification(_lowerCAmelCase						)
					model.to(_lowerCAmelCase						)
					model.eval()
					_lowerCAmelCase		     = model(_lowerCAmelCase     ,       labels=_lowerCAmelCase						)
					self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_labels)						)

					# test greyscale images
					_lowerCAmelCase		     = 1
					_lowerCAmelCase		     = DinatForImageClassification(_lowerCAmelCase						)
					model.to(_lowerCAmelCase						)
					model.eval()

					_lowerCAmelCase		     = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
					_lowerCAmelCase		     = model(_lowerCAmelCase						)
					self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_labels)						)




	def 							_snake_case				(		self     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase						)      ->							int:
					_lowerCAmelCase		     = DinatBackbone(config=_lowerCAmelCase						)
					model.to(_lowerCAmelCase						)
					model.eval()
					_lowerCAmelCase		     = 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
					_lowerCAmelCase		     = None
					_lowerCAmelCase		     = DinatBackbone(config=_lowerCAmelCase						)
					model.to(_lowerCAmelCase						)
					model.eval()
					_lowerCAmelCase		     = 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 							_snake_case				(		self						)      ->							Union[str, Any]:
					_lowerCAmelCase		     = self.prepare_config_and_inputs()
					_lowerCAmelCase     ,     _lowerCAmelCase     ,     _lowerCAmelCase		     = config_and_inputs
					_lowerCAmelCase		     = {"pixel_values": pixel_values}
					return config, inputs_dict







@require_natten
@require_torch
class 				lowerCAmelCase_						(						__magic_name__  ,__magic_name__  ,unittest.TestCase						):
	__lowerCamelCase  :							Optional[int]    					=   (
	    (
	        DinatModel,
	        DinatForImageClassification,
	        DinatBackbone,
	    )
	    if is_torch_available()
	    else ()
	)
	__lowerCamelCase  :							Optional[Any]    					=   (
	    {"feature-extraction": DinatModel, "image-classification": DinatForImageClassification}
	    if is_torch_available()
	    else {}
	)
	__lowerCamelCase  :							Any    					=   False

	__lowerCamelCase  :							Optional[Any]    					=   False
	__lowerCamelCase  :							Union[str, Any]    					=   False
	__lowerCamelCase  :							List[Any]    					=   False
	__lowerCamelCase  :							Any    					=   False




	def 							_snake_case				(		self						)      ->							List[Any]:
					_lowerCAmelCase		     = DinatModelTester(self						)
					_lowerCAmelCase		     = ConfigTester(self     ,       config_class=_lowerCAmelCase     ,       embed_dim=37						)




	def 							_snake_case				(		self						)      ->							Tuple:
					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 							_snake_case				(		self						)      ->							Dict:
					return




	def 							_snake_case				(		self						)      ->							Tuple:
					_lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_lowerCAmelCase						)




	def 							_snake_case				(		self						)      ->							Optional[int]:
					_lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase						)




	def 							_snake_case				(		self						)      ->							Tuple:
					_lowerCAmelCase		     = 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 							_snake_case				(		self						)      ->							Union[str, Any]:
					pass




	@unittest.skip(reason="Dinat does not use feedforward chunking"						)
	def 							_snake_case				(		self						)      ->							Dict:
					pass




	def 							_snake_case				(		self						)      ->							Union[str, Any]:
					_lowerCAmelCase     ,     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									_lowerCAmelCase		     = model_class(_lowerCAmelCase						)
									self.assertIsInstance(model.get_input_embeddings()     ,       (nn.Module)						)
									_lowerCAmelCase		     = model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(_lowerCAmelCase     ,       nn.Linear						)						)




	def 							_snake_case				(		self						)      ->							Any:
					_lowerCAmelCase     ,     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs_for_common()

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

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




	def 							_snake_case				(		self						)      ->							Dict:
					self.skipTest("Dinat's attention operation is handled entirely by NATTEN."						)




	def 							_snake_case				(		self     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase						)      ->							int:
					_lowerCAmelCase		     = model_class(_lowerCAmelCase						)
					model.to(_lowerCAmelCase						)
					model.eval()

					with torch.no_grad():
									_lowerCAmelCase		     = model(**self._prepare_for_class(_lowerCAmelCase     ,       _lowerCAmelCase						)						)

					_lowerCAmelCase		     = outputs.hidden_states

					_lowerCAmelCase		     = 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
					_lowerCAmelCase		     = (
					    config.patch_size
					    if isinstance(config.patch_size     ,       collections.abc.Iterable						)
					    else (config.patch_size, config.patch_size)
					)

					_lowerCAmelCase		     = image_size[0] // patch_size[0]
					_lowerCAmelCase		     = 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":
									_lowerCAmelCase		     = outputs.reshaped_hidden_states
									self.assertEqual(len(_lowerCAmelCase						)     ,       _lowerCAmelCase						)

									_lowerCAmelCase     ,     _lowerCAmelCase     ,     _lowerCAmelCase     ,     _lowerCAmelCase		     = reshaped_hidden_states[0].shape
									_lowerCAmelCase		     = (
									    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 							_snake_case				(		self						)      ->							List[Any]:
					_lowerCAmelCase     ,     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs_for_common()

					_lowerCAmelCase		     = (
					    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:
									_lowerCAmelCase		     = 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"]
									_lowerCAmelCase		     = True

									self.check_hidden_states_output(_lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase     ,       _lowerCAmelCase						)




	@slow
	def 							_snake_case				(		self						)      ->							List[Any]:
					for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_lowerCAmelCase		     = DinatModel.from_pretrained(_lowerCAmelCase						)
									self.assertIsNotNone(_lowerCAmelCase						)





	def 							_snake_case				(		self						)      ->							List[Any]:
					_lowerCAmelCase     ,     _lowerCAmelCase		     = self.model_tester.prepare_config_and_inputs_for_common()

					_lowerCAmelCase		     = _config_zero_init(_lowerCAmelCase						)
					for model_class in self.all_model_classes:
									_lowerCAmelCase		     = 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 				lowerCAmelCase_						(						unittest.TestCase						):




	@cached_property
	def 							_snake_case				(		self						)      ->							List[str]:
					return AutoImageProcessor.from_pretrained("shi-labs/dinat-mini-in1k-224"						) if is_vision_available() else None





	@slow
	def 							_snake_case				(		self						)      ->							Any:
					_lowerCAmelCase		     = DinatForImageClassification.from_pretrained("shi-labs/dinat-mini-in1k-224"						).to(_lowerCAmelCase						)
					_lowerCAmelCase		     = self.default_image_processor

					_lowerCAmelCase		     = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
					_lowerCAmelCase		     = image_processor(images=_lowerCAmelCase     ,       return_tensors="pt"						).to(_lowerCAmelCase						)

					# forward pass
					with torch.no_grad():
									_lowerCAmelCase		     = model(**_lowerCAmelCase						)

					# verify the logits
					_lowerCAmelCase		     = torch.Size((1, 1000)						)
					self.assertEqual(outputs.logits.shape     ,       _lowerCAmelCase						)
					_lowerCAmelCase		     = torch.tensor([-0.1545, -0.7667, 0.4642]						).to(_lowerCAmelCase						)
					self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,       _lowerCAmelCase     ,       atol=1E-4						)						)







@require_torch
@require_natten
class 				lowerCAmelCase_						(						unittest.TestCase  ,__magic_name__						):
	__lowerCamelCase  :							Optional[int]    					=   (DinatBackbone,) if is_torch_available() else ()
	__lowerCamelCase  :							Any    					=   DinatConfig




	def 							_snake_case				(		self						)      ->							Dict:
					_lowerCAmelCase		     = DinatModelTester(self						)









'''simple docstring'''


from __future__ import annotations

from collections.abc import Iterable, Iterator
from typing import Any, Generic, TypeVar

_SCREAMING_SNAKE_CASE			=							TypeVar("T", bound=bool)

class 				lowerCAmelCase_						(						Generic[T]						):




 def __init__(		self     ,       _lowerCAmelCase						)      ->							None:
     _lowerCAmelCase		     = value
     _lowerCAmelCase		     = None
     _lowerCAmelCase		     = None




 @property
 def 							_snake_case				(		self						)      ->							T:
     return self._value





 @staticmethod
 def 							_snake_case				(		_lowerCAmelCase     ,       _lowerCAmelCase						)      ->							SkewNode[T] | None:
     if not roota:
         return roota

     if not roota:
         return roota

     if roota.value > roota.value:
         _lowerCAmelCase     ,     _lowerCAmelCase		     = roota, roota

     _lowerCAmelCase		     = roota
     _lowerCAmelCase		     = roota.right
     _lowerCAmelCase		     = roota.left
     _lowerCAmelCase		     = SkewNode.merge(_lowerCAmelCase     ,       _lowerCAmelCase						)

     return result







class 				lowerCAmelCase_						(						Generic[T]						):




 def __init__(		self     ,       _lowerCAmelCase = ()						)      ->							None:
     _lowerCAmelCase		     = None
     if data:
         for item in data:
             self.insert(_lowerCAmelCase						)




 def __bool__(		self						)      ->							bool:
     return self._root is not None




 def __iter__(		self						)      ->							Iterator[T]:
     _lowerCAmelCase		     = []
     while self:
         result.append(self.pop()						)

     # Pushing items back to the heap not to clear it.
     for item in result:
         self.insert(_lowerCAmelCase						)

     return iter(_lowerCAmelCase						)




 def 							_snake_case				(		self     ,       _lowerCAmelCase						)      ->							None:
     _lowerCAmelCase		     = SkewNode.merge(self._root     ,       SkewNode(_lowerCAmelCase						)						)




 def 							_snake_case				(		self						)      ->							T | None:
     _lowerCAmelCase		     = self.top()
     _lowerCAmelCase		     = (
         SkewNode.merge(self._root.left     ,       self._root.right						) if self._root else None
     )

     return result




 def 							_snake_case				(		self						)      ->							T:
     if not self._root:
         raise IndexError("Can't get top element for the empty heap."						)
     return self._root.value





 def 							_snake_case				(		self						)      ->							None:
     _lowerCAmelCase		     = None


if __name__ == "__main__":
       import doctest

       doctest.testmod()


