





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__:






       '''simple docstring'''


       def __init__(					self			,  __lowerCamelCase			,  __lowerCamelCase=1_3			,  __lowerCamelCase=6_4			,  __lowerCamelCase=4			,  __lowerCamelCase=3			,  __lowerCamelCase=1_6			,  __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=1_0			,  __lowerCamelCase=["stage1", "stage2"]			,  __lowerCamelCase=[1, 2]			,  )	->   Tuple:
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							parent
            _SCREAMING_SNAKE_CASE				:		Dict  			=							batch_size
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							image_size
            _SCREAMING_SNAKE_CASE				:		List[Any]  			=							patch_size
            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							num_channels
            _SCREAMING_SNAKE_CASE				:		str  			=							embed_dim
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							depths
            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							num_heads
            _SCREAMING_SNAKE_CASE				:		Any  			=							kernel_size
            _SCREAMING_SNAKE_CASE				:		Any  			=							dilations
            _SCREAMING_SNAKE_CASE				:		Any  			=							mlp_ratio
            _SCREAMING_SNAKE_CASE				:		int  			=							qkv_bias
            _SCREAMING_SNAKE_CASE				:		Tuple  			=							hidden_dropout_prob
            _SCREAMING_SNAKE_CASE				:		str  			=							attention_probs_dropout_prob
            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							drop_path_rate
            _SCREAMING_SNAKE_CASE				:		Any  			=							hidden_act
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							patch_norm
            _SCREAMING_SNAKE_CASE				:		Dict  			=							layer_norm_eps
            _SCREAMING_SNAKE_CASE				:		Tuple  			=							initializer_range
            _SCREAMING_SNAKE_CASE				:		List[Any]  			=							is_training
            _SCREAMING_SNAKE_CASE				:		Dict  			=							scope
            _SCREAMING_SNAKE_CASE				:		str  			=							use_labels
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							num_labels
            _SCREAMING_SNAKE_CASE				:		int  			=							out_features
            _SCREAMING_SNAKE_CASE				:		Any  			=							out_indices




       def 			UpperCamelCase_	(					self							)	->   Tuple:
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							self.get_config()

            return config, pixel_values, labels




       def 			UpperCamelCase_	(					self							)	->   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 			UpperCamelCase_	(					self			,  __lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase							)	->   Any:
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							DinatModel(config=__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()
            _SCREAMING_SNAKE_CASE				:		str  			=							model(__lowerCamelCase							)

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

            # test greyscale images
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							1
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							DinatForImageClassification(__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()

            _SCREAMING_SNAKE_CASE				:		List[str]  			=							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							model(__lowerCamelCase							)
            self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_labels)							)




       def 			UpperCamelCase_	(					self			,  __lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase							)	->   List[str]:
            _SCREAMING_SNAKE_CASE				:		str  			=							DinatBackbone(config=__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()
            _SCREAMING_SNAKE_CASE				:		List[Any]  			=							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], 1_6, 1_6]							)

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

            # verify backbone works with out_features=None
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							None
            _SCREAMING_SNAKE_CASE				:		Tuple  			=							DinatBackbone(config=__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()
            _SCREAMING_SNAKE_CASE				:		Dict  			=							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 			UpperCamelCase_	(					self							)	->   Any:
            _SCREAMING_SNAKE_CASE				:		int  			=							self.prepare_config_and_inputs()
            _SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							config_and_inputs
            _SCREAMING_SNAKE_CASE				:		int  			=							{"pixel_values": pixel_values}
            return config, inputs_dict





@require_natten
@require_torch
class 	lowerCAmelCase__(       __lowercase     ,   __lowercase     ,   unittest.TestCase			):






       '''simple docstring'''


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

       __snake_case       	=			False
       __snake_case       	=			False
       __snake_case       	=			False
       __snake_case       	=			False




       def 			UpperCamelCase_	(					self							)	->   Optional[Any]:
            _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							DinatModelTester(self							)
            _SCREAMING_SNAKE_CASE				:		int  			=							ConfigTester(self			,  config_class=__lowerCamelCase			,  embed_dim=3_7							)




       def 			UpperCamelCase_	(					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 			UpperCamelCase_	(					self							)	->   str:
            return




       def 			UpperCamelCase_	(					self							)	->   str:
            _SCREAMING_SNAKE_CASE				:		List[Any]  			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__lowerCamelCase							)




       def 			UpperCamelCase_	(					self							)	->   Optional[Any]:
            _SCREAMING_SNAKE_CASE				:		Tuple  			=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase							)




       def 			UpperCamelCase_	(					self							)	->   List[Any]:
            _SCREAMING_SNAKE_CASE				:		Any  			=							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 			UpperCamelCase_	(					self							)	->   Dict:
            pass




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




       def 			UpperCamelCase_	(					self							)	->   Dict:
            _SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE				:		List[Any]  			=							self.model_tester.prepare_config_and_inputs_for_common()

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




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

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

                 _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							["pixel_values"]
                 self.assertListEqual(arg_names[:1]			,  __lowerCamelCase							)




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




       def 			UpperCamelCase_	(					self			,  __lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase							)	->   Optional[int]:
            _SCREAMING_SNAKE_CASE				:		int  			=							model_class(__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()

            with torch.no_grad():
                 _SCREAMING_SNAKE_CASE				:		List[str]  			=							model(**self._prepare_for_class(__lowerCamelCase			,  __lowerCamelCase							)							)

            _SCREAMING_SNAKE_CASE				:		str  			=							outputs.hidden_states

            _SCREAMING_SNAKE_CASE				:		Optional[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
            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							(
                config.patch_size
                if isinstance(config.patch_size			,  collections.abc.Iterable							)
                else (config.patch_size, config.patch_size)
            )

            _SCREAMING_SNAKE_CASE				:		Tuple  			=							image_size[0] // patch_size[0]
            _SCREAMING_SNAKE_CASE				:		Optional[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":
                 _SCREAMING_SNAKE_CASE				:		List[str]  			=							outputs.reshaped_hidden_states
                 self.assertEqual(len(__lowerCamelCase							)			,  __lowerCamelCase							)

                 _SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE				:		Optional[int]  			=							reshaped_hidden_states[0].shape
                 _SCREAMING_SNAKE_CASE				:		List[Any]  			=							(
                     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 			UpperCamelCase_	(					self							)	->   List[Any]:
            _SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE				:		str  			=							self.model_tester.prepare_config_and_inputs_for_common()

            _SCREAMING_SNAKE_CASE				:		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:
                 _SCREAMING_SNAKE_CASE				:		int  			=							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"]
                 _SCREAMING_SNAKE_CASE				:		Tuple  			=							True

                 self.check_hidden_states_output(__lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase							)




       @slow
       def 			UpperCamelCase_	(					self							)	->   Any:
            for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _SCREAMING_SNAKE_CASE				:		Dict  			=							DinatModel.from_pretrained(__lowerCamelCase							)
                 self.assertIsNotNone(__lowerCamelCase							)







       def 			UpperCamelCase_	(					self							)	->   Union[str, Any]:
            _SCREAMING_SNAKE_CASE			,	_SCREAMING_SNAKE_CASE				:		Dict  			=							self.model_tester.prepare_config_and_inputs_for_common()

            _SCREAMING_SNAKE_CASE				:		str  			=							_config_zero_init(__lowerCamelCase							)
            for model_class in self.all_model_classes:
                 _SCREAMING_SNAKE_CASE				:		Any  			=							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			):






       '''simple docstring'''


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







       @slow
       def 			UpperCamelCase_	(					self							)	->   Any:
            _SCREAMING_SNAKE_CASE				:		List[str]  			=							DinatForImageClassification.from_pretrained("shi-labs/dinat-mini-in1k-224"							).to(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE				:		List[Any]  			=							self.default_image_processor

            _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
            _SCREAMING_SNAKE_CASE				:		int  			=							image_processor(images=__lowerCamelCase			,  return_tensors="pt"							).to(__lowerCamelCase							)

            # forward pass
            with torch.no_grad():
                 _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							model(**__lowerCamelCase							)

            # verify the logits
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							torch.Size((1, 1_0_0_0)							)
            self.assertEqual(outputs.logits.shape			,  __lowerCamelCase							)
            _SCREAMING_SNAKE_CASE				:		Any  			=							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     ,   __lowercase			):






       '''simple docstring'''


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




       def 			UpperCamelCase_	(					self							)	->   List[Any]:
            _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							DinatModelTester(self							)






import sys

sys.set_int_max_str_digits(0)  # type: ignore






def 			lowerCamelCase__	(__lowerCamelCase		):
     _SCREAMING_SNAKE_CASE				:		int  			=							[0] * 11
     _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							[0] * 11

     # mark last 9 numbers
     for _ in range(9		):
          _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							1
          _SCREAMING_SNAKE_CASE				:		str  			=							number // 10
     # flag
     _SCREAMING_SNAKE_CASE				:		int  			=							True

     # check last 9 numbers for pandigitality

     for x in range(9		):
          if not check_last[x + 1]:
               _SCREAMING_SNAKE_CASE				:		Any  			=							False
     if not f:
          return f

     # mark first 9 numbers
     _SCREAMING_SNAKE_CASE				:		str  			=							int(str(__lowerCamelCase		)[:9]		)

     for _ in range(9		):
          _SCREAMING_SNAKE_CASE				:		str  			=							1
          _SCREAMING_SNAKE_CASE				:		List[Any]  			=							number // 10

     # check first 9 numbers for pandigitality

     for x in range(9		):
          if not check_front[x + 1]:
               _SCREAMING_SNAKE_CASE				:		Any  			=							False
     return f






def 			lowerCamelCase__	(__lowerCamelCase		):
     _SCREAMING_SNAKE_CASE				:		List[str]  			=							[0] * 11

     # mark last 9 numbers
     for _ in range(9		):
          _SCREAMING_SNAKE_CASE				:		Dict  			=							1
          _SCREAMING_SNAKE_CASE				:		List[str]  			=							number // 10
     # flag
     _SCREAMING_SNAKE_CASE				:		List[Any]  			=							True

     # check last 9 numbers for pandigitality

     for x in range(9		):
          if not check_last[x + 1]:
               _SCREAMING_SNAKE_CASE				:		str  			=							False
     return f






def 			lowerCamelCase__	():
     _SCREAMING_SNAKE_CASE				:		Dict  			=							1
     _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							1
     _SCREAMING_SNAKE_CASE				:		List[str]  			=							2
     # temporary Fibonacci numbers

     _SCREAMING_SNAKE_CASE				:		Any  			=							1
     _SCREAMING_SNAKE_CASE				:		List[str]  			=							1
     _SCREAMING_SNAKE_CASE				:		str  			=							2
     # temporary Fibonacci numbers mod 1e9

     # mod m=1e9, done for fast optimisation
     _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							[0] * 1000000
     _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							1000000000

     for x in range(1000000		):
          _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							(aa + ba) % m
          _SCREAMING_SNAKE_CASE				:		int  			=							ba % m
          _SCREAMING_SNAKE_CASE				:		List[str]  			=							ca % m
          if checka(__lowerCamelCase		):
               _SCREAMING_SNAKE_CASE				:		List[Any]  			=							1

     for x in range(1000000		):
          _SCREAMING_SNAKE_CASE				:		Any  			=							a + b
          _SCREAMING_SNAKE_CASE				:		Dict  			=							b
          _SCREAMING_SNAKE_CASE				:		int  			=							c
          # perform check only if in tocheck
          if tocheck[x + 3] and check(__lowerCamelCase		):
               return x + 3  # first 2 already done
     return -1


if __name__ == "__main__":
       print(f"{solution() = }")