




"""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     A__      :




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




	def 							a_		(   self  :	Union[str, Any]	)					-> List[Any]:
						__snake_case          =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

						__snake_case          =	self.get_config()

						return config, pixel_values, labels




	def 							a_		(   self  :	Tuple	)					-> List[str]:
						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  :	Any      ,    lowercase  :	Union[str, Any]      ,    lowercase  :	Optional[int]      ,    lowercase  :	Union[str, Any]	)					-> Dict:
						__snake_case          =	DinatModel(config=lowercase	)
						model.to(lowercase	)
						model.eval()
						__snake_case          =	model(lowercase	)

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

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

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




	def 							a_		(   self  :	Any      ,    lowercase  :	Optional[int]      ,    lowercase  :	int      ,    lowercase  :	int	)					-> Optional[Any]:
						__snake_case          =	DinatBackbone(config=lowercase	)
						model.to(lowercase	)
						model.eval()
						__snake_case          =	model(lowercase	)

						# 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
						__snake_case          =	None
						__snake_case          =	DinatBackbone(config=lowercase	)
						model.to(lowercase	)
						model.eval()
						__snake_case          =	model(lowercase	)

						# 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  :	Tuple	)					-> Optional[Any]:
						__snake_case          =	self.prepare_config_and_inputs()
						__snake_case      ,      __snake_case      ,      __snake_case          =	config_and_inputs
						__snake_case          =	{"""pixel_values""": pixel_values}
						return config, inputs_dict

@require_natten
@require_torch
class     A__      (      lowercase_     ,	lowercase_     ,	unittest.TestCase     ):
	A_							:				Optional[Any]						     =      (
	    (
	        DinatModel,
	        DinatForImageClassification,
	        DinatBackbone,
	    )
	    if is_torch_available()
	    else ()
	)
	A_							:				Any						     =      (
	    {"""feature-extraction""": DinatModel, """image-classification""": DinatForImageClassification}
	    if is_torch_available()
	    else {}
	)
	A_							:				str						     =      False

	A_							:				Any						     =      False
	A_							:				Tuple						     =      False
	A_							:				Dict						     =      False
	A_							:				Union[str, Any]						     =      False




	def 							a_		(   self  :	Optional[int]	)					-> Optional[Any]:
						__snake_case          =	DinatModelTester(self	)
						__snake_case          =	ConfigTester(self      ,    config_class=lowercase      ,    embed_dim=37	)




	def 							a_		(   self  :	int	)					-> Optional[int]:
						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  :	Dict	)					-> Optional[Any]:
						return




	def 							a_		(   self  :	str	)					-> int:
						__snake_case          =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase	)




	def 							a_		(   self  :	int	)					-> Tuple:
						__snake_case          =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*lowercase	)




	def 							a_		(   self  :	Any	)					-> int:
						__snake_case          =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_backbone(*lowercase	)




	@unittest.skip(reason="""Dinat does not use inputs_embeds"""	)
	def 							a_		(   self  :	Optional[Any]	)					-> Dict:
						pass




	@unittest.skip(reason="""Dinat does not use feedforward chunking"""	)
	def 							a_		(   self  :	List[str]	)					-> List[Any]:
						pass




	def 							a_		(   self  :	Any	)					-> Tuple:
						__snake_case      ,      __snake_case          =	self.model_tester.prepare_config_and_inputs_for_common()

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




	def 							a_		(   self  :	Dict	)					-> Optional[int]:
						__snake_case      ,      __snake_case          =	self.model_tester.prepare_config_and_inputs_for_common()

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

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




	def 							a_		(   self  :	Tuple	)					-> Optional[int]:
						self.skipTest("""Dinat's attention operation is handled entirely by NATTEN."""	)




	def 							a_		(   self  :	str      ,    lowercase  :	Dict      ,    lowercase  :	int      ,    lowercase  :	Union[str, Any]      ,    lowercase  :	Any	)					-> Tuple:
						__snake_case          =	model_class(lowercase	)
						model.to(lowercase	)
						model.eval()

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

						__snake_case          =	outputs.hidden_states

						__snake_case          =	getattr(
						    self.model_tester      ,    """expected_num_hidden_layers"""      ,    len(self.model_tester.depths	) + 1	)
						self.assertEqual(len(lowercase	)      ,    lowercase	)

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

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

											__snake_case      ,      __snake_case      ,      __snake_case      ,      __snake_case          =	reshaped_hidden_states[0].shape
											__snake_case          =	(
											    reshaped_hidden_states[0].view(lowercase      ,    lowercase      ,    lowercase      ,    lowercase	).permute(0      ,    2      ,    3      ,    1	)
											)
											self.assertListEqual(
											    list(reshaped_hidden_states.shape[-3:]	)      ,    [height, width, self.model_tester.embed_dim]      ,    )




	def 							a_		(   self  :	Optional[Any]	)					-> int:
						__snake_case      ,      __snake_case          =	self.model_tester.prepare_config_and_inputs_for_common()

						__snake_case          =	(
						    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:
											__snake_case          =	True
											self.check_hidden_states_output(lowercase      ,    lowercase      ,    lowercase      ,    lowercase	)

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

											self.check_hidden_states_output(lowercase      ,    lowercase      ,    lowercase      ,    lowercase	)




	@slow
	def 							a_		(   self  :	Dict	)					-> Tuple:
						for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__snake_case          =	DinatModel.from_pretrained(lowercase	)
											self.assertIsNotNone(lowercase	)




	def 							a_		(   self  :	str	)					-> str:
						__snake_case      ,      __snake_case          =	self.model_tester.prepare_config_and_inputs_for_common()

						__snake_case          =	_config_zero_init(lowercase	)
						for model_class in self.all_model_classes:
											__snake_case          =	model_class(config=lowercase	)
											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     A__      (      unittest.TestCase     ):




	@cached_property
	def 							a_		(   self  :	Dict	)					-> int:
						return AutoImageProcessor.from_pretrained("""shi-labs/dinat-mini-in1k-224"""	) if is_vision_available() else None




	@slow
	def 							a_		(   self  :	Tuple	)					-> str:
						__snake_case          =	DinatForImageClassification.from_pretrained("""shi-labs/dinat-mini-in1k-224"""	).to(lowercase	)
						__snake_case          =	self.default_image_processor

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

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

						# verify the logits
						__snake_case          =	torch.Size((1, 1_000)	)
						self.assertEqual(outputs.logits.shape      ,    lowercase	)
						__snake_case          =	torch.tensor([-0.15_45, -0.76_67, 0.46_42]	).to(lowercase	)
						self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,    lowercase      ,    atol=1e-4	)	)

@require_torch
@require_natten
class     A__      (      unittest.TestCase     ,	lowercase_     ):
	A_							:				str						     =      (DinatBackbone,) if is_torch_available() else ()
	A_							:				Any						     =      DinatConfig




	def 							a_		(   self  :	List[str]	)					-> Optional[int]:
						__snake_case          =	DinatModelTester(self	)








"""simple docstring"""

import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


UpperCamelCase__ :    Optional[int] 					=							logging.get_logger(__name__)
class     A__      (      nn.Module     ):




	def __init__(   self  :	List[str]	)					-> Tuple:
						super().__init__()
						if version.parse(torch.__version__	) < version.parse("""1.12.0"""	):
											raise ImportError(
											    f'''You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use '''
											    """PytorchGELUTanh. Please upgrade torch."""	)




	def 							a_		(   self  :	Union[str, Any]      ,    lowercase  :	Tensor	)					-> Tensor:
						return nn.functional.gelu(lowercase      ,    approximate="""tanh"""	)

class     A__      (      nn.Module     ):




	def 							a_		(   self  :	Optional[int]      ,    lowercase  :	Tensor	)					-> Tensor:
						return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi	) * (input + 0.04_47_15 * torch.pow(lowercase      ,    3.0	))	))

class     A__      (      nn.Module     ):




	def __init__(   self  :	Any      ,    lowercase  :	bool = False	)					-> Optional[Any]:
						super().__init__()
						if use_gelu_python:
											__snake_case          =	self._gelu_python
						else:
											__snake_case          =	nn.functional.gelu




	def 							a_		(   self  :	Tuple      ,    lowercase  :	Tensor	)					-> Tensor:
						return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0	)	))




	def 							a_		(   self  :	str      ,    lowercase  :	Tensor	)					-> Tensor:
						return self.act(lowercase	)

class     A__      (      nn.Module     ):




	def 							a_		(   self  :	str      ,    lowercase  :	Tensor	)					-> Tensor:
						return 0.5 * input * (1.0 + torch.tanh(input * 0.79_78_84_56_08 * (1.0 + 0.04_47_15 * input * input)	))

class     A__      (      nn.Module     ):




	def 							a_		(   self  :	List[str]      ,    lowercase  :	Tensor	)					-> Tensor:
						return input * torch.sigmoid(1.7_02 * input	)

class     A__      (      nn.Module     ):




	def __init__(   self  :	Union[str, Any]      ,    lowercase  :	float      ,    lowercase  :	float	)					-> List[str]:
						if min > max:
											raise ValueError(f'''min should be < max (got min: {min}, max: {max})'''	)

						super().__init__()
						__snake_case          =	min
						__snake_case          =	max




	def 							a_		(   self  :	Any      ,    lowercase  :	Tensor	)					-> Tensor:
						return torch.clip(gelu(lowercase	)      ,    self.min      ,    self.max	)

class     A__      (      nn.Module     ):




	def __init__(   self  :	str	)					-> List[str]:
						super().__init__()
						__snake_case          =	math.sqrt(2 / math.pi	)




	def 							a_		(   self  :	Any      ,    lowercase  :	Tensor	)					-> Tensor:
						return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_47_15 * torch.pow(lowercase      ,    3	))	))

class     A__      (      nn.Module     ):




	def 							a_		(   self  :	Tuple      ,    lowercase  :	Tensor	)					-> Tensor:
						return nn.functional.silu(lowercase	)

class     A__      (      nn.Module     ):




	def __init__(   self  :	Union[str, Any]	)					-> int:
						super().__init__()
						if version.parse(torch.__version__	) < version.parse("""1.9.0"""	):
											__snake_case          =	self._mish_python
						else:
											__snake_case          =	nn.functional.mish




	def 							a_		(   self  :	Any      ,    lowercase  :	Tensor	)					-> Tensor:
						return input * torch.tanh(nn.functional.softplus(lowercase	)	)




	def 							a_		(   self  :	int      ,    lowercase  :	Tensor	)					-> Tensor:
						return self.act(lowercase	)

class     A__      (      nn.Module     ):




	def 							a_		(   self  :	Union[str, Any]      ,    lowercase  :	Tensor	)					-> Tensor:
						return input

class     A__      (      nn.Module     ):




	def 							a_		(   self  :	List[Any]      ,    lowercase  :	List[Any]      ,    lowercase  :	Optional[Any]=0.70_71_07      ,    lowercase  :	Union[str, Any]=0.28_20_95	)					-> Optional[int]:
						__snake_case          =	(input - mu).div(sigma * math.sqrt(2.0	)	)
						return 0.5 * (1.0 + torch.erf(lowercase	))

class     A__      (      nn.Module     ):




	def 							a_		(   self  :	List[str]      ,    lowercase  :	int	)					-> List[Any]:
						__snake_case          =	nn.functional.relu(lowercase	)
						__snake_case          =	torch.square(lowercase	)
						return squared

class     A__      (      lowercase_     ):




	def __getitem__(   self  :	int      ,    lowercase  :	Dict	)					-> Optional[Any]:
						__snake_case          =	super().__getitem__(lowercase	)
						__snake_case      ,      __snake_case          =	content if isinstance(lowercase      ,    lowercase	) else (content, {})
						return cls(**lowercase	)

UpperCamelCase__ :    List[Any] 					=							{
    "gelu": GELUActivation,
    "gelu_10": (ClippedGELUActivation, {"min": -10, "max": 10}),
    "gelu_fast": FastGELUActivation,
    "gelu_new": NewGELUActivation,
    "gelu_python": (GELUActivation, {"use_gelu_python": True}),
    "gelu_pytorch_tanh": PytorchGELUTanh,
    "gelu_accurate": AccurateGELUActivation,
    "laplace": LaplaceActivation,
    "linear": LinearActivation,
    "mish": MishActivation,
    "quick_gelu": QuickGELUActivation,
    "relu": nn.ReLU,
    "relu2": ReLUSquaredActivation,
    "relu6": nn.ReLUa,
    "sigmoid": nn.Sigmoid,
    "silu": SiLUActivation,
    "swish": SiLUActivation,
    "tanh": nn.Tanh,
}
UpperCamelCase__ :    Any 					=							ClassInstantier(ACTaCLS)




def       snake_case__	(       lowerCAmelCase   :		str		):
					if activation_string in ACTaFN:
										return ACTaFN[activation_string]
					else:
										raise KeyError(F'''function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()		)}'''		)


# For backwards compatibility with: from activations import gelu_python
UpperCamelCase__ :    List[Any] 					=							get_activation("gelu_python")
UpperCamelCase__ :    Optional[int] 					=							get_activation("gelu_new")
UpperCamelCase__ :    Dict 					=							get_activation("gelu")
UpperCamelCase__ :    int 					=							get_activation("gelu_fast")
UpperCamelCase__ :    Tuple 					=							get_activation("quick_gelu")
UpperCamelCase__ :    int 					=							get_activation("silu")
UpperCamelCase__ :    Union[str, Any] 					=							get_activation("mish")

UpperCamelCase__ :    Tuple 					=							get_activation("linear")


