


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


     """simple docstring"""






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

     def 					lowerCamelCase__					(							self	:						Optional[int]     )		->				Dict:
          __UpperCAmelCase      :					Union[str, Any]    				=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

          __UpperCAmelCase      :					List[str]    				=     None
          if self.use_labels:
               __UpperCAmelCase      :					Dict    				=     ids_tensor([self.batch_size]				,					self.num_labels     )

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

          return config, pixel_values, labels

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

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

          # test greyscale images
          __UpperCAmelCase      :					Optional[int]    				=     1
          __UpperCAmelCase      :					Optional[Any]    				=     DinatForImageClassification(snake_case     )
          model.to(snake_case     )
          model.eval()

          __UpperCAmelCase      :					Tuple    				=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
          __UpperCAmelCase      :					Tuple    				=     model(snake_case     )
          self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.num_labels)     )

     def 					lowerCamelCase__					(							self	:						List[str]				,					snake_case	:						Optional[int]				,					snake_case	:						Optional[Any]				,					snake_case	:						Any     )		->				List[Any]:
          __UpperCAmelCase      :					Tuple    				=     DinatBackbone(config=snake_case     )
          model.to(snake_case     )
          model.eval()
          __UpperCAmelCase      :					Tuple    				=     model(snake_case     )

          # 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
          __UpperCAmelCase      :					Dict    				=     None
          __UpperCAmelCase      :					Optional[int]    				=     DinatBackbone(config=snake_case     )
          model.to(snake_case     )
          model.eval()
          __UpperCAmelCase      :					str    				=     model(snake_case     )

          # 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	:						Union[str, Any]     )		->				int:
          __UpperCAmelCase      :					Optional[Any]    				=     self.prepare_config_and_inputs()
          __UpperCAmelCase				,  __UpperCAmelCase				,  __UpperCAmelCase      :					Optional[int]    				=     config_and_inputs
          __UpperCAmelCase      :					List[Any]    				=     {'''pixel_values''': pixel_values}
          return config, inputs_dict



@require_natten
@require_torch
class       a							(						_a      ,    _a      ,    unittest.TestCase			):


     """simple docstring"""






     SCREAMING_SNAKE_CASE			:					int				  =  (
         (
             DinatModel,
             DinatForImageClassification,
             DinatBackbone,
         )
         if is_torch_available()
         else ()
     )
     SCREAMING_SNAKE_CASE			:					Tuple				  =  (
         {"feature-extraction": DinatModel, "image-classification": DinatForImageClassification}
         if is_torch_available()
         else {}
     )
     SCREAMING_SNAKE_CASE			:					Dict				  =  False

     SCREAMING_SNAKE_CASE			:					Dict				  =  False
     SCREAMING_SNAKE_CASE			:					Dict				  =  False
     SCREAMING_SNAKE_CASE			:					str				  =  False
     SCREAMING_SNAKE_CASE			:					Optional[Any]				  =  False

     def 					lowerCamelCase__					(							self	:						Optional[int]     )		->				str:
          __UpperCAmelCase      :					Optional[Any]    				=     DinatModelTester(self     )
          __UpperCAmelCase      :					Optional[int]    				=     ConfigTester(self				,					config_class=snake_case				,					embed_dim=37     )

     def 					lowerCamelCase__					(							self	:						Optional[int]     )		->				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 					lowerCamelCase__					(							self	:						str     )		->				Tuple:
          return

     def 					lowerCamelCase__					(							self	:						Dict     )		->				Optional[Any]:
          __UpperCAmelCase      :					Any    				=     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case     )

     def 					lowerCamelCase__					(							self	:						Optional[int]     )		->				Dict:
          __UpperCAmelCase      :					List[str]    				=     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*snake_case     )

     def 					lowerCamelCase__					(							self	:						Optional[int]     )		->				str:
          __UpperCAmelCase      :					Optional[Any]    				=     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_backbone(*snake_case     )

     @unittest.skip(reason='''Dinat does not use inputs_embeds'''     )
     def 					lowerCamelCase__					(							self	:						Optional[Any]     )		->				List[Any]:
          pass

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

     def 					lowerCamelCase__					(							self	:						int     )		->				Dict:
          __UpperCAmelCase				,  __UpperCAmelCase      :					Any    				=     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               __UpperCAmelCase      :					int    				=     model_class(snake_case     )
               self.assertIsInstance(model.get_input_embeddings()				,					(nn.Module)     )
               __UpperCAmelCase      :					Optional[Any]    				=     model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(snake_case				,					nn.Linear     )     )

     def 					lowerCamelCase__					(							self	:						Optional[int]     )		->				Optional[int]:
          __UpperCAmelCase				,  __UpperCAmelCase      :					int    				=     self.model_tester.prepare_config_and_inputs_for_common()

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

               __UpperCAmelCase      :					Any    				=     ['''pixel_values''']
               self.assertListEqual(arg_names[:1]				,					snake_case     )

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

     def 					lowerCamelCase__					(							self	:						Dict				,					snake_case	:						int				,					snake_case	:						str				,					snake_case	:						Any				,					snake_case	:						str     )		->				int:
          __UpperCAmelCase      :					Any    				=     model_class(snake_case     )
          model.to(snake_case     )
          model.eval()

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

          __UpperCAmelCase      :					str    				=     outputs.hidden_states

          __UpperCAmelCase      :					Union[str, Any]    				=     getattr(
              self.model_tester				,					'''expected_num_hidden_layers'''				,					len(self.model_tester.depths     ) + 1     )
          self.assertEqual(len(snake_case     )				,					snake_case     )

          # Dinat has a different seq_length
          __UpperCAmelCase      :					Optional[int]    				=     (
              config.patch_size
              if isinstance(config.patch_size				,					collections.abc.Iterable     )
              else (config.patch_size, config.patch_size)
          )

          __UpperCAmelCase      :					List[Any]    				=     image_size[0] // patch_size[0]
          __UpperCAmelCase      :					Optional[int]    				=     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":
               __UpperCAmelCase      :					int    				=     outputs.reshaped_hidden_states
               self.assertEqual(len(snake_case     )				,					snake_case     )

               __UpperCAmelCase				,  __UpperCAmelCase				,  __UpperCAmelCase				,  __UpperCAmelCase      :					List[Any]    				=     reshaped_hidden_states[0].shape
               __UpperCAmelCase      :					Union[str, Any]    				=     (
                   reshaped_hidden_states[0].view(snake_case				,					snake_case				,					snake_case				,					snake_case     ).permute(0				,					2				,					3				,					1     )
               )
               self.assertListEqual(
                   list(reshaped_hidden_states.shape[-3:]     )				,					[height, width, self.model_tester.embed_dim]				,					)

     def 					lowerCamelCase__					(							self	:						Optional[Any]     )		->				Tuple:
          __UpperCAmelCase				,  __UpperCAmelCase      :					List[str]    				=     self.model_tester.prepare_config_and_inputs_for_common()

          __UpperCAmelCase      :					List[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:
               __UpperCAmelCase      :					Any    				=     True
               self.check_hidden_states_output(snake_case				,					snake_case				,					snake_case				,					snake_case     )

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               __UpperCAmelCase      :					List[str]    				=     True

               self.check_hidden_states_output(snake_case				,					snake_case				,					snake_case				,					snake_case     )

     @slow
     def 					lowerCamelCase__					(							self	:						Optional[int]     )		->				Optional[Any]:
          for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __UpperCAmelCase      :					int    				=     DinatModel.from_pretrained(snake_case     )
               self.assertIsNotNone(snake_case     )






     def 					lowerCamelCase__					(							self	:						List[str]     )		->				int:
          __UpperCAmelCase				,  __UpperCAmelCase      :					Tuple    				=     self.model_tester.prepare_config_and_inputs_for_common()

          __UpperCAmelCase      :					List[Any]    				=     _config_zero_init(snake_case     )
          for model_class in self.all_model_classes:
               __UpperCAmelCase      :					str    				=     model_class(config=snake_case     )
               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			):


     """simple docstring"""






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






     @slow
     def 					lowerCamelCase__					(							self	:						int     )		->				Tuple:
          __UpperCAmelCase      :					Dict    				=     DinatForImageClassification.from_pretrained('''shi-labs/dinat-mini-in1k-224'''     ).to(snake_case     )
          __UpperCAmelCase      :					Optional[Any]    				=     self.default_image_processor

          __UpperCAmelCase      :					Optional[Any]    				=     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
          __UpperCAmelCase      :					List[str]    				=     image_processor(images=snake_case				,					return_tensors='''pt'''     ).to(snake_case     )

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

          # verify the logits
          __UpperCAmelCase      :					int    				=     torch.Size((1, 1000)     )
          self.assertEqual(outputs.logits.shape				,					snake_case     )
          __UpperCAmelCase      :					Dict    				=     torch.tensor([-0.1_545, -0.7_667, 0.4_642]     ).to(snake_case     )
          self.assertTrue(torch.allclose(outputs.logits[0, :3]				,					snake_case				,					atol=1E-4     )     )



@require_torch
@require_natten
class       a							(						unittest.TestCase      ,    _a			):


     """simple docstring"""






     SCREAMING_SNAKE_CASE			:					Optional[Any]				  =  (DinatBackbone,) if is_torch_available() else ()
     SCREAMING_SNAKE_CASE			:					int				  =  DinatConfig

     def 					lowerCamelCase__					(							self	:						int     )		->				Optional[Any]:
          __UpperCAmelCase      :					int    				=     DinatModelTester(self     )



'''simple docstring'''



import os
import unittest

import pytest
from parameterized import parameterized

from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device


# skipping in unittest tests

__UpperCAmelCase     :Tuple												=  [(1,)]




def       _a      (		):





     '''simple docstring'''






     __UpperCAmelCase      :					Any    				=     bool(os.getenv('''RUN_SLOW'''     ,   0    )    )
     if run_slow:
          assert True
     else:
          assert False, "should have been skipped"




def       _a      (		):





     '''simple docstring'''






     __UpperCAmelCase      :					str    				=     bool(os.getenv('''RUN_SLOW'''     ,   0    )    )
     if run_slow and torch_device == "cuda":
          assert True
     else:
          assert False, "should have been skipped"



@require_torch
class       a							(						unittest.TestCase			):


     """simple docstring"""






     @slow
     @require_torch_gpu
     def 					lowerCamelCase__					(							self	:						Tuple     )		->				str:
          check_slow_torch_cuda()

     @require_torch_gpu
     @slow
     def 					lowerCamelCase__					(							self	:						Any     )		->				List[Any]:
          check_slow_torch_cuda()






     @parameterized.expand(snake_case     )
     @slow
     def 					lowerCamelCase__					(							self	:						Union[str, Any]				,					snake_case	:						int=None     )		->				Union[str, Any]:
          check_slow()




@slow
@require_torch_gpu
def       _a      (		):





     '''simple docstring'''






     check_slow_torch_cuda()




@require_torch_gpu
@slow
def       _a      (		):





     '''simple docstring'''






     check_slow_torch_cuda()




@slow
@pytest.mark.parametrize('''param'''     ,   [1]    )
def       _a      (		_lowercase   :      Union[str, Any]    ):





     '''simple docstring'''






     check_slow()




@pytest.mark.parametrize('''param'''     ,   [1]    )
@slow
def       _a      (		_lowercase   :      Dict    ):





     '''simple docstring'''






     check_slow()