



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





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





     def 						__snake_case     (							self			:				List[str]					)->Tuple:
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

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

      return config, pixel_values, labels





     def 						__snake_case     (							self			:				Tuple					)->Tuple:
      return DinatConfig(
          num_labels=self.num_labels  ,	image_size=self.image_size  ,	patch_size=self.patch_size  ,	num_channels=self.num_channels  ,	embed_dim=self.embed_dim  ,	depths=self.depths  ,	num_heads=self.num_heads  ,	kernel_size=self.kernel_size  ,	dilations=self.dilations  ,	mlp_ratio=self.mlp_ratio  ,	qkv_bias=self.qkv_bias  ,	hidden_dropout_prob=self.hidden_dropout_prob  ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,	drop_path_rate=self.drop_path_rate  ,	hidden_act=self.hidden_act  ,	patch_norm=self.patch_norm  ,	layer_norm_eps=self.layer_norm_eps  ,	initializer_range=self.initializer_range  ,	out_features=self.out_features  ,	out_indices=self.out_indices  ,	)





     def 						__snake_case     (							self			:				Dict  ,	UpperCamelCase			:				Dict  ,	UpperCamelCase			:				Tuple  ,	UpperCamelCase			:				List[str]					)->List[Any]:
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						DinatModel(config=UpperCamelCase					)
      model.to(UpperCamelCase					)
      model.eval()
      __SCREAMING_SNAKE_CASE							:     Dict							=						model(UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     str							=						(config.image_size // config.patch_size) // (2 ** (len(config.depths					) - 1))
      __SCREAMING_SNAKE_CASE							:     int							=						int(config.embed_dim * 2 ** (len(config.depths					) - 1)					)

      self.parent.assertEqual(
          result.last_hidden_state.shape  ,	(self.batch_size, expected_height, expected_width, expected_dim)					)





     def 						__snake_case     (							self			:				Tuple  ,	UpperCamelCase			:				Optional[Any]  ,	UpperCamelCase			:				Optional[int]  ,	UpperCamelCase			:				Union[str, Any]					)->str:
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						DinatForImageClassification(UpperCamelCase					)
      model.to(UpperCamelCase					)
      model.eval()
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						model(UpperCamelCase  ,	labels=UpperCamelCase					)
      self.parent.assertEqual(result.logits.shape  ,	(self.batch_size, self.num_labels)					)

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

      __SCREAMING_SNAKE_CASE							:     List[str]							=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
      __SCREAMING_SNAKE_CASE							:     Tuple							=						model(UpperCamelCase					)
      self.parent.assertEqual(result.logits.shape  ,	(self.batch_size, self.num_labels)					)





     def 						__snake_case     (							self			:				List[Any]  ,	UpperCamelCase			:				int  ,	UpperCamelCase			:				Optional[Any]  ,	UpperCamelCase			:				Optional[Any]					)->Tuple:
      __SCREAMING_SNAKE_CASE							:     List[str]							=						DinatBackbone(config=UpperCamelCase					)
      model.to(UpperCamelCase					)
      model.eval()
      __SCREAMING_SNAKE_CASE							:     Dict							=						model(UpperCamelCase					)

      # 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							:     List[str]							=						None
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						DinatBackbone(config=UpperCamelCase					)
      model.to(UpperCamelCase					)
      model.eval()
      __SCREAMING_SNAKE_CASE							:     str							=						model(UpperCamelCase					)

      # verify feature maps
      self.parent.assertEqual(len(result.feature_maps					)  ,	1					)
      self.parent.assertListEqual(list(result.feature_maps[0].shape					)  ,	[self.batch_size, model.channels[-1], 4, 4]					)

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






     def 						__snake_case     (							self			:				Any					)->Optional[int]:
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						self.prepare_config_and_inputs()
      __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							:     Dict							=						config_and_inputs
      __SCREAMING_SNAKE_CASE							:     str							=						{"pixel_values": pixel_values}
      return config, inputs_dict






@require_natten
@require_torch
class    _SCREAMING_SNAKE_CASE						(UpperCamelCase      ,     UpperCamelCase      ,     unittest.TestCase			):
     lowerCAmelCase       =      (
         (
             DinatModel,
             DinatForImageClassification,
             DinatBackbone,
         )
         if is_torch_available()
         else ()
     )
     lowerCAmelCase       =      (
         {"""feature-extraction""": DinatModel, """image-classification""": DinatForImageClassification}
         if is_torch_available()
         else {}
     )
     lowerCAmelCase       =      False

     lowerCAmelCase       =      False
     lowerCAmelCase       =      False
     lowerCAmelCase       =      False
     lowerCAmelCase       =      False





     def 						__snake_case     (							self			:				Optional[int]					)->Union[str, Any]:
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						DinatModelTester(self					)
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						ConfigTester(self  ,	config_class=UpperCamelCase  ,	embed_dim=3_7					)





     def 						__snake_case     (							self			:				Tuple					)->Any:
      self.create_and_test_config_common_properties()
      self.config_tester.create_and_test_config_to_json_string()
      self.config_tester.create_and_test_config_to_json_file()
      self.config_tester.create_and_test_config_from_and_save_pretrained()
      self.config_tester.create_and_test_config_with_num_labels()
      self.config_tester.check_config_can_be_init_without_params()
      self.config_tester.check_config_arguments_init()





     def 						__snake_case     (							self			:				Dict					)->str:
      return





     def 						__snake_case     (							self			:				Optional[int]					)->Union[str, Any]:
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*UpperCamelCase					)





     def 						__snake_case     (							self			:				Union[str, Any]					)->str:
      __SCREAMING_SNAKE_CASE							:     Any							=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*UpperCamelCase					)





     def 						__snake_case     (							self			:				List[Any]					)->List[str]:
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_backbone(*UpperCamelCase					)





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





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





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

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





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

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

       __SCREAMING_SNAKE_CASE							:     Tuple							=						["pixel_values"]
       self.assertListEqual(arg_names[:1]  ,	UpperCamelCase					)





     def 						__snake_case     (							self			:				List[Any]					)->Optional[Any]:
      self.skipTest("Dinat's attention operation is handled entirely by NATTEN."					)





     def 						__snake_case     (							self			:				Tuple  ,	UpperCamelCase			:				List[Any]  ,	UpperCamelCase			:				List[Any]  ,	UpperCamelCase			:				Dict  ,	UpperCamelCase			:				List[str]					)->int:
      __SCREAMING_SNAKE_CASE							:     List[str]							=						model_class(UpperCamelCase					)
      model.to(UpperCamelCase					)
      model.eval()

      with torch.no_grad():
       __SCREAMING_SNAKE_CASE							:     List[Any]							=						model(**self._prepare_for_class(UpperCamelCase  ,	UpperCamelCase					)					)

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						outputs.hidden_states

      __SCREAMING_SNAKE_CASE							:     List[Any]							=						getattr(
          self.model_tester  ,	"expected_num_hidden_layers"  ,	len(self.model_tester.depths					) + 1					)
      self.assertEqual(len(UpperCamelCase					)  ,	UpperCamelCase					)

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

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

       __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							:     Tuple							=						reshaped_hidden_states[0].shape
       __SCREAMING_SNAKE_CASE							:     List[Any]							=						(
           reshaped_hidden_states[0].view(UpperCamelCase  ,	UpperCamelCase  ,	UpperCamelCase  ,	UpperCamelCase					).permute(0  ,	2  ,	3  ,	1					)
       )
       self.assertListEqual(
           list(reshaped_hidden_states.shape[-3:]					)  ,	[height, width, self.model_tester.embed_dim]  ,	)





     def 						__snake_case     (							self			:				Dict					)->Any:
      __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							:     str							=						self.model_tester.prepare_config_and_inputs_for_common()

      __SCREAMING_SNAKE_CASE							:     int							=						(
          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							:     Optional[Any]							=						True
       self.check_hidden_states_output(UpperCamelCase  ,	UpperCamelCase  ,	UpperCamelCase  ,	UpperCamelCase					)

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       __SCREAMING_SNAKE_CASE							:     Optional[int]							=						True

       self.check_hidden_states_output(UpperCamelCase  ,	UpperCamelCase  ,	UpperCamelCase  ,	UpperCamelCase					)





     @slow
     def 						__snake_case     (							self			:				Dict					)->Optional[int]:
      for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __SCREAMING_SNAKE_CASE							:     Any							=						DinatModel.from_pretrained(UpperCamelCase					)
       self.assertIsNotNone(UpperCamelCase					)






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

      __SCREAMING_SNAKE_CASE							:     List[Any]							=						_config_zero_init(UpperCamelCase					)
      for model_class in self.all_model_classes:
       __SCREAMING_SNAKE_CASE							:     Dict							=						model_class(config=UpperCamelCase					)
       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    _SCREAMING_SNAKE_CASE						(unittest.TestCase			):





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






     @slow
     def 						__snake_case     (							self			:				List[str]					)->Dict:
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						DinatForImageClassification.from_pretrained("shi-labs/dinat-mini-in1k-224"					).to(UpperCamelCase					)
      __SCREAMING_SNAKE_CASE							:     Dict							=						self.default_image_processor

      __SCREAMING_SNAKE_CASE							:     Dict							=						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
      __SCREAMING_SNAKE_CASE							:     str							=						image_processor(images=UpperCamelCase  ,	return_tensors="pt"					).to(UpperCamelCase					)

      # forward pass
      with torch.no_grad():
       __SCREAMING_SNAKE_CASE							:     str							=						model(**UpperCamelCase					)

      # verify the logits
      __SCREAMING_SNAKE_CASE							:     Tuple							=						torch.Size((1, 1_0_0_0)					)
      self.assertEqual(outputs.logits.shape  ,	UpperCamelCase					)
      __SCREAMING_SNAKE_CASE							:     Dict							=						torch.tensor([-0.1_5_4_5, -0.7_6_6_7, 0.4_6_4_2]					).to(UpperCamelCase					)
      self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,	UpperCamelCase  ,	atol=1E-4					)					)






@require_torch
@require_natten
class    _SCREAMING_SNAKE_CASE						(unittest.TestCase      ,     UpperCamelCase			):
     lowerCAmelCase       =      (DinatBackbone,) if is_torch_available() else ()
     lowerCAmelCase       =      DinatConfig





     def 						__snake_case     (							self			:				Optional[Any]					)->Dict:
      __SCREAMING_SNAKE_CASE							:     Any							=						DinatModelTester(self					)











def 						_lowerCAmelCase    (					__lowerCamelCase :  int  ):



 """simple docstring"""





 if not isinstance(__lowerCamelCase		,						__lowerCamelCase  ):
  __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						(
      "operation can not be conducted on a object of type "
      F"""{type(__lowerCamelCase  ).__name__}"""
  )
  raise TypeError(__lowerCamelCase  )
 __SCREAMING_SNAKE_CASE							:     List[str]							=						""
 for _ in range(0		,						32  ):
  bit_string += str(number % 2  )
  __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						number >> 1
 return bit_string






def 						_lowerCAmelCase    (					__lowerCamelCase :  int  ):



 """simple docstring"""





 if number < 0:
  raise ValueError("the value of input must be positive"  )
 elif isinstance(__lowerCamelCase		,						__lowerCamelCase  ):
  raise TypeError("Input value must be a 'int' type"  )
 elif isinstance(__lowerCamelCase		,						__lowerCamelCase  ):
  raise TypeError("'<' not supported between instances of 'str' and 'int'"  )
 __SCREAMING_SNAKE_CASE							:     Any							=						0
 # iterator over [1 to 32],since we are dealing with 32 bit integer
 for _ in range(1		,						33  ):
  # left shift the bits by unity
  __SCREAMING_SNAKE_CASE							:     Optional[int]							=						result << 1
  # get the end bit
  __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						number % 2
  # right shift the bits by unity
  __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						number >> 1
  # add that bit to our ans
  __SCREAMING_SNAKE_CASE							:     Optional[int]							=						result | end_bit
 return get_reverse_bit_string(__lowerCamelCase  )


if __name__ == "__main__":
      import doctest

      doctest.testmod()






