




'''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       :  Tuple							,			lowerCAmelCase_       :  Union[str, Any]							,			lowerCAmelCase_       :  List[Any]=13							,			lowerCAmelCase_       :  Optional[Any]=64							,			lowerCAmelCase_       :  Optional[Any]=4							,			lowerCAmelCase_       :  List[Any]=3							,			lowerCAmelCase_       :  str=16							,			lowerCAmelCase_       :  str=[1, 2, 1]							,			lowerCAmelCase_       :  Dict=[2, 4, 8]							,			lowerCAmelCase_       :  int=3							,			lowerCAmelCase_       :  Tuple=[[3], [1, 2], [1]]							,			lowerCAmelCase_       :  Tuple=2.0							,			lowerCAmelCase_       :  Optional[int]=True							,			lowerCAmelCase_       :  Optional[Any]=0.0							,			lowerCAmelCase_       :  Optional[Any]=0.0							,			lowerCAmelCase_       :  Optional[int]=0.1							,			lowerCAmelCase_       :  Optional[int]="gelu"							,			lowerCAmelCase_       :  Optional[int]=True							,			lowerCAmelCase_       :  Optional[Any]=0.0_2							,			lowerCAmelCase_       :  str=1e-5							,			lowerCAmelCase_       :  Union[str, Any]=True							,			lowerCAmelCase_       :  Optional[int]=None							,			lowerCAmelCase_       :  Union[str, Any]=True							,			lowerCAmelCase_       :  Optional[int]=10							,			lowerCAmelCase_       :  Optional[Any]=["stage1", "stage2"]							,			lowerCAmelCase_       :  int=[1, 2]							,			)			->							Optional[Any]:


														"""simple docstring"""


														_a	     =				parent
														_a	     =				batch_size
														_a	     =				image_size
														_a	     =				patch_size
														_a	     =				num_channels
														_a	     =				embed_dim
														_a	     =				depths
														_a	     =				num_heads
														_a	     =				kernel_size
														_a	     =				dilations
														_a	     =				mlp_ratio
														_a	     =				qkv_bias
														_a	     =				hidden_dropout_prob
														_a	     =				attention_probs_dropout_prob
														_a	     =				drop_path_rate
														_a	     =				hidden_act
														_a	     =				patch_norm
														_a	     =				layer_norm_eps
														_a	     =				initializer_range
														_a	     =				is_training
														_a	     =				scope
														_a	     =				use_labels
														_a	     =				num_labels
														_a	     =				out_features
														_a	     =				out_indices







							def    __lowerCAmelCase			(  self       :  Dict					)			->							Optional[Any]:


														"""simple docstring"""


														_a	     =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

														_a	     =				self.get_config()

														return config, pixel_values, labels







							def    __lowerCAmelCase			(  self       :  List[str]					)			->							int:


														"""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    __lowerCAmelCase			(  self       :  Optional[Any]							,			lowerCAmelCase_       :  Any							,			lowerCAmelCase_       :  List[str]							,			lowerCAmelCase_       :  int					)			->							str:


														"""simple docstring"""


														_a	     =				DinatModel(config=lowerCAmelCase_					)
														model.to(lowerCAmelCase_					)
														model.eval()
														_a	     =				model(lowerCAmelCase_					)

														_a	     =				_a	     =				(config.image_size // config.patch_size) // (2 ** (len(config.depths					) - 1))
														_a	     =				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    __lowerCAmelCase			(  self       :  Dict							,			lowerCAmelCase_       :  Any							,			lowerCAmelCase_       :  Tuple							,			lowerCAmelCase_       :  Optional[Any]					)			->							List[Any]:


														"""simple docstring"""


														_a	     =				DinatForImageClassification(lowerCAmelCase_					)
														model.to(lowerCAmelCase_					)
														model.eval()
														_a	     =				model(lowerCAmelCase_							,			labels=lowerCAmelCase_					)
														self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_labels)					)

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

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







							def    __lowerCAmelCase			(  self       :  int							,			lowerCAmelCase_       :  Any							,			lowerCAmelCase_       :  Union[str, Any]							,			lowerCAmelCase_       :  Tuple					)			->							Any:


														"""simple docstring"""


														_a	     =				DinatBackbone(config=lowerCAmelCase_					)
														model.to(lowerCAmelCase_					)
														model.eval()
														_a	     =				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	     =				None
														_a	     =				DinatBackbone(config=lowerCAmelCase_					)
														model.to(lowerCAmelCase_					)
														model.eval()
														_a	     =				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    __lowerCAmelCase			(  self       :  List[Any]					)			->							Tuple:


														"""simple docstring"""


														_a	     =				self.prepare_config_and_inputs()
														_a     ,							_a     ,							_a	     =				config_and_inputs
														_a	     =				{'''pixel_values''': pixel_values}
														return config, inputs_dict



@require_natten
@require_torch
class 			A    (		_a     ,_a     ,unittest.TestCase    ):
							lowercase_							=				(
							    (
							        DinatModel,
							        DinatForImageClassification,
							        DinatBackbone,
							    )
							    if is_torch_available()
							    else ()
							)
							lowercase_							=				(
							    {'feature-extraction': DinatModel, 'image-classification': DinatForImageClassification}
							    if is_torch_available()
							    else {}
							)
							lowercase_							=				False

							lowercase_							=				False
							lowercase_							=				False
							lowercase_							=				False
							lowercase_							=				False







							def    __lowerCAmelCase			(  self       :  Dict					)			->							Optional[Any]:


														"""simple docstring"""


														_a	     =				DinatModelTester(self					)
														_a	     =				ConfigTester(self							,			config_class=lowerCAmelCase_							,			embed_dim=37					)







							def    __lowerCAmelCase			(  self       :  Optional[int]					)			->							str:


														"""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    __lowerCAmelCase			(  self       :  Any					)			->							int:


														"""simple docstring"""


														return







							def    __lowerCAmelCase			(  self       :  int					)			->							str:


														"""simple docstring"""


														_a	     =				self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCAmelCase_					)







							def    __lowerCAmelCase			(  self       :  Any					)			->							List[str]:


														"""simple docstring"""


														_a	     =				self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_					)







							def    __lowerCAmelCase			(  self       :  Tuple					)			->							Dict:


														"""simple docstring"""


														_a	     =				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    __lowerCAmelCase			(  self       :  Any					)			->							str:


														"""simple docstring"""


														pass







							@unittest.skip(reason='''Dinat does not use feedforward chunking'''					)
							def    __lowerCAmelCase			(  self       :  Optional[Any]					)			->							Optional[Any]:


														"""simple docstring"""


														pass







							def    __lowerCAmelCase			(  self       :  Dict					)			->							List[Any]:


														"""simple docstring"""


														_a     ,							_a	     =				self.model_tester.prepare_config_and_inputs_for_common()

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







							def    __lowerCAmelCase			(  self       :  Union[str, Any]					)			->							List[Any]:


														"""simple docstring"""


														_a     ,							_a	     =				self.model_tester.prepare_config_and_inputs_for_common()

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

																					_a	     =				['''pixel_values''']
																					self.assertListEqual(arg_names[:1]							,			lowerCAmelCase_					)







							def    __lowerCAmelCase			(  self       :  int					)			->							Any:


														"""simple docstring"""


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







							def    __lowerCAmelCase			(  self       :  Dict							,			lowerCAmelCase_       :  Optional[Any]							,			lowerCAmelCase_       :  str							,			lowerCAmelCase_       :  List[Any]							,			lowerCAmelCase_       :  Tuple					)			->							Tuple:


														"""simple docstring"""


														_a	     =				model_class(lowerCAmelCase_					)
														model.to(lowerCAmelCase_					)
														model.eval()

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

														_a	     =				outputs.hidden_states

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

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

																					_a     ,							_a     ,							_a     ,							_a	     =				reshaped_hidden_states[0].shape
																					_a	     =				(
																					    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    __lowerCAmelCase			(  self       :  Tuple					)			->							List[Any]:


														"""simple docstring"""


														_a     ,							_a	     =				self.model_tester.prepare_config_and_inputs_for_common()

														_a	     =				(
														    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	     =				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	     =				True

																					self.check_hidden_states_output(lowerCAmelCase_							,			lowerCAmelCase_							,			lowerCAmelCase_							,			lowerCAmelCase_					)







							@slow
							def    __lowerCAmelCase			(  self       :  Dict					)			->							int:


														"""simple docstring"""


														for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					_a	     =				DinatModel.from_pretrained(lowerCAmelCase_					)
																					self.assertIsNotNone(lowerCAmelCase_					)







							def    __lowerCAmelCase			(  self       :  Tuple					)			->							Optional[int]:


														"""simple docstring"""


														_a     ,							_a	     =				self.model_tester.prepare_config_and_inputs_for_common()

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







							@cached_property
							def    __lowerCAmelCase			(  self       :  Optional[int]					)			->							List[str]:


														"""simple docstring"""


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







							@slow
							def    __lowerCAmelCase			(  self       :  List[Any]					)			->							Tuple:


														"""simple docstring"""


														_a	     =				DinatForImageClassification.from_pretrained('''shi-labs/dinat-mini-in1k-224'''					).to(lowerCAmelCase_					)
														_a	     =				self.default_image_processor

														_a	     =				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
														_a	     =				image_processor(images=lowerCAmelCase_							,			return_tensors='''pt'''					).to(lowerCAmelCase_					)

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

														# verify the logits
														_a	     =				torch.Size((1, 10_00)					)
														self.assertEqual(outputs.logits.shape							,			lowerCAmelCase_					)
														_a	     =				torch.tensor([-0.1_5_4_5, -0.7_6_6_7, 0.4_6_4_2]					).to(lowerCAmelCase_					)
														self.assertTrue(torch.allclose(outputs.logits[0, :3]							,			lowerCAmelCase_							,			atol=1e-4					)					)



@require_torch
@require_natten
class 			A    (		unittest.TestCase     ,_a    ):
							lowercase_							=				(DinatBackbone,) if is_torch_available() else ()
							lowercase_							=				DinatConfig







							def    __lowerCAmelCase			(  self       :  Optional[Any]					)			->							Optional[int]:


														"""simple docstring"""


														_a	     =				DinatModelTester(self					)







'''simple docstring'''


import os
from shutil import copyfile
from typing import Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
				from .tokenization_reformer import ReformerTokenizer
else:
				_snake_case      :      int   							=		None


_snake_case      :      Union[str, Any]   							=		logging.get_logger(__name__)


_snake_case      :      Union[str, Any]   							=		'▁'

_snake_case      :      Any   							=		{'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'}

_snake_case      :      List[str]   							=		{
    'vocab_file': {
        'google/reformer-crime-and-punishment': (
            'https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model'
        )
    },
    'tokenizer_file': {
        'google/reformer-crime-and-punishment': (
            'https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/tokenizer.json'
        )
    },
}

_snake_case      :      Union[str, Any]   							=		{
    'google/reformer-crime-and-punishment': 524288,
}

class 			A    (		_a    ):
							lowercase_							=				VOCAB_FILES_NAMES
							lowercase_							=				PRETRAINED_VOCAB_FILES_MAP
							lowercase_							=				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							lowercase_							=				['input_ids', 'attention_mask']
							lowercase_							=				ReformerTokenizer







							def __init__(  self       :  Any							,			lowerCAmelCase_       :  int=None							,			lowerCAmelCase_       :  Optional[Any]=None							,			lowerCAmelCase_       :  List[str]="</s>"							,			lowerCAmelCase_       :  Any="<unk>"							,			lowerCAmelCase_       :  List[str]=[]							,			**lowerCAmelCase_       :  List[Any]							,			)			->							Dict:


														"""simple docstring"""


														super().__init__(
														    lowerCAmelCase_							,			tokenizer_file=lowerCAmelCase_							,			eos_token=lowerCAmelCase_							,			unk_token=lowerCAmelCase_							,			additional_special_tokens=lowerCAmelCase_							,			**lowerCAmelCase_							,			)

														_a	     =				vocab_file
														_a	     =				False if not self.vocab_file else True







							def    __lowerCAmelCase			(  self       :  List[Any]							,			lowerCAmelCase_       :  str							,			lowerCAmelCase_       :  Optional[str] = None					)			->							Tuple[str]:


														"""simple docstring"""


														if not self.can_save_slow_tokenizer:
																					raise ValueError(
																					    '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '''
																					    '''tokenizer.'''					)

														if not os.path.isdir(lowerCAmelCase_					):
																					logger.error(F'Vocabulary path ({save_directory}) should be a directory'					)
																					return
														_a	     =				os.path.join(
														    lowerCAmelCase_							,			(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']					)

														if os.path.abspath(self.vocab_file					) != os.path.abspath(lowerCAmelCase_					):
																					copyfile(self.vocab_file							,			lowerCAmelCase_					)

														return (out_vocab_file,)

