






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






      def __init__(   self      :     Optional[Any]	,	lowercase__      :     Optional[int]	,	lowercase__      :     str=13	,	lowercase__      :     Optional[Any]=64	,	lowercase__      :     str=4	,	lowercase__      :     Tuple=3	,	lowercase__      :     Optional[Any]=16	,	lowercase__      :     Tuple=[1, 2, 1]	,	lowercase__      :     List[Any]=[2, 4, 8]	,	lowercase__      :     Dict=3	,	lowercase__      :     Optional[int]=[[3], [1, 2], [1]]	,	lowercase__      :     int=2.0	,	lowercase__      :     Optional[Any]=True	,	lowercase__      :     List[str]=0.0	,	lowercase__      :     Tuple=0.0	,	lowercase__      :     Optional[Any]=0.1	,	lowercase__      :     List[str]="gelu"	,	lowercase__      :     Optional[int]=True	,	lowercase__      :     Optional[Any]=0.02	,	lowercase__      :     int=1e-5	,	lowercase__      :     List[Any]=True	,	lowercase__      :     List[str]=None	,	lowercase__      :     Any=True	,	lowercase__      :     Any=10	,	lowercase__      :     List[str]=["stage1", "stage2"]	,	lowercase__      :     Optional[Any]=[1, 2]	,	):


           '''simple docstring'''

           a_    :       int    	=     parent
           a_    :       Any    	=     batch_size
           a_    :       Optional[int]    	=     image_size
           a_    :       int    	=     patch_size
           a_    :       Dict    	=     num_channels
           a_    :       List[Any]    	=     embed_dim
           a_    :       List[str]    	=     depths
           a_    :       Any    	=     num_heads
           a_    :       List[str]    	=     kernel_size
           a_    :       int    	=     dilations
           a_    :       Optional[Any]    	=     mlp_ratio
           a_    :       Any    	=     qkv_bias
           a_    :       List[Any]    	=     hidden_dropout_prob
           a_    :       Dict    	=     attention_probs_dropout_prob
           a_    :       Tuple    	=     drop_path_rate
           a_    :       int    	=     hidden_act
           a_    :       List[Any]    	=     patch_norm
           a_    :       Any    	=     layer_norm_eps
           a_    :       Optional[Any]    	=     initializer_range
           a_    :       List[Any]    	=     is_training
           a_    :       int    	=     scope
           a_    :       Dict    	=     use_labels
           a_    :       Dict    	=     num_labels
           a_    :       List[str]    	=     out_features
           a_    :       Dict    	=     out_indices






      def        lowercase_	(   self      :     str					):


           '''simple docstring'''

           a_    :       Union[str, Any]    	=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

           a_    :       Optional[Any]    	=     None
           if self.use_labels:
                a_    :       Tuple    	=     ids_tensor([self.batch_size]	,	self.num_labels					)

           a_    :       int    	=     self.get_config()

           return config, pixel_values, labels






      def        lowercase_	(   self      :     str					):


           '''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        lowercase_	(   self      :     Dict	,	lowercase__      :     Any	,	lowercase__      :     List[str]	,	lowercase__      :     Dict					):


           '''simple docstring'''

           a_    :       Optional[int]    	=     DinatModel(config=lowercase__					)
           model.to(lowercase__					)
           model.eval()
           a_    :       Any    	=     model(lowercase__					)

           a_    :       int    	=     (config.image_size // config.patch_size) // (2 ** (len(config.depths					) - 1))
           a_    :       Tuple    	=     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        lowercase_	(   self      :     List[str]	,	lowercase__      :     Optional[int]	,	lowercase__      :     Any	,	lowercase__      :     str					):


           '''simple docstring'''

           a_    :       Any    	=     DinatForImageClassification(lowercase__					)
           model.to(lowercase__					)
           model.eval()
           a_    :       Tuple    	=     model(lowercase__	,	labels=lowercase__					)
           self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.num_labels)					)

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

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






      def        lowercase_	(   self      :     Dict	,	lowercase__      :     List[Any]	,	lowercase__      :     Optional[int]	,	lowercase__      :     Optional[Any]					):


           '''simple docstring'''

           a_    :       str    	=     DinatBackbone(config=lowercase__					)
           model.to(lowercase__					)
           model.eval()
           a_    :       Optional[int]    	=     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
           a_    :       List[Any]    	=     None
           a_    :       List[str]    	=     DinatBackbone(config=lowercase__					)
           model.to(lowercase__					)
           model.eval()
           a_    :       int    	=     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        lowercase_	(   self      :     int					):


           '''simple docstring'''

           a_    :       Dict    	=     self.prepare_config_and_inputs()
           a_    :       str    	=     config_and_inputs
           a_    :       int    	=     {"""pixel_values""": pixel_values}
           return config, inputs_dict







@require_natten
@require_torch
class     SCREAMING_SNAKE_CASE						(	snake_case_       ,			snake_case_       ,			unittest.TestCase							):
      __magic_name__     :					Tuple             =				(
          (
              DinatModel,
              DinatForImageClassification,
              DinatBackbone,
          )
          if is_torch_available()
          else ()
      )
      __magic_name__     :					Optional[int]             =				(
          {'''feature-extraction''': DinatModel, '''image-classification''': DinatForImageClassification}
          if is_torch_available()
          else {}
      )
      __magic_name__     :					Optional[Any]             =				False

      __magic_name__     :					Tuple             =				False
      __magic_name__     :					Tuple             =				False
      __magic_name__     :					List[str]             =				False
      __magic_name__     :					List[Any]             =				False






      def        lowercase_	(   self      :     Optional[Any]					):


           '''simple docstring'''

           a_    :       List[Any]    	=     DinatModelTester(self					)
           a_    :       List[Any]    	=     ConfigTester(self	,	config_class=lowercase__	,	embed_dim=37					)






      def        lowercase_	(   self      :     List[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        lowercase_	(   self      :     List[str]					):


           '''simple docstring'''

           return






      def        lowercase_	(   self      :     Any					):


           '''simple docstring'''

           a_    :       List[Any]    	=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase__					)






      def        lowercase_	(   self      :     Optional[int]					):


           '''simple docstring'''

           a_    :       Optional[int]    	=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*lowercase__					)






      def        lowercase_	(   self      :     List[Any]					):


           '''simple docstring'''

           a_    :       Union[str, Any]    	=     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        lowercase_	(   self      :     List[str]					):


           '''simple docstring'''

           pass






      @unittest.skip(reason="""Dinat does not use feedforward chunking"""					)
      def        lowercase_	(   self      :     str					):


           '''simple docstring'''

           pass






      def        lowercase_	(   self      :     Optional[Any]					):


           '''simple docstring'''

           a_    :       Optional[int]    	=     self.model_tester.prepare_config_and_inputs_for_common()

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






      def        lowercase_	(   self      :     Optional[int]					):


           '''simple docstring'''

           a_    :       Optional[int]    	=     self.model_tester.prepare_config_and_inputs_for_common()

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

                a_    :       Tuple    	=     ["""pixel_values"""]
                self.assertListEqual(arg_names[:1]	,	lowercase__					)






      def        lowercase_	(   self      :     Dict					):


           '''simple docstring'''

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






      def        lowercase_	(   self      :     Any	,	lowercase__      :     Dict	,	lowercase__      :     Any	,	lowercase__      :     Any	,	lowercase__      :     List[str]					):


           '''simple docstring'''

           a_    :       Dict    	=     model_class(lowercase__					)
           model.to(lowercase__					)
           model.eval()

           with torch.no_grad():
                a_    :       Dict    	=     model(**self._prepare_for_class(lowercase__	,	lowercase__					)					)

           a_    :       List[str]    	=     outputs.hidden_states

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

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

                a_    :       List[str]    	=     reshaped_hidden_states[0].shape
                a_    :       List[Any]    	=     (
                    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        lowercase_	(   self      :     Union[str, Any]					):


           '''simple docstring'''

           a_    :       Optional[Any]    	=     self.model_tester.prepare_config_and_inputs_for_common()

           a_    :       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:
                a_    :       List[str]    	=     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"]
                a_    :       Dict    	=     True

                self.check_hidden_states_output(lowercase__	,	lowercase__	,	lowercase__	,	lowercase__					)






      @slow
      def        lowercase_	(   self      :     Dict					):


           '''simple docstring'''

           for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                a_    :       List[str]    	=     DinatModel.from_pretrained(lowercase__					)
                self.assertIsNotNone(lowercase__					)






      def        lowercase_	(   self      :     Any					):


           '''simple docstring'''

           a_    :       List[str]    	=     self.model_tester.prepare_config_and_inputs_for_common()

           a_    :       Dict    	=     _config_zero_init(lowercase__					)
           for model_class in self.all_model_classes:
                a_    :       Optional[int]    	=     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     SCREAMING_SNAKE_CASE						(	unittest.TestCase							):






      @cached_property
      def        lowercase_	(   self      :     Optional[int]					):


           '''simple docstring'''

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






      @slow
      def        lowercase_	(   self      :     Any					):


           '''simple docstring'''

           a_    :       Any    	=     DinatForImageClassification.from_pretrained("""shi-labs/dinat-mini-in1k-224"""					).to(lowercase__					)
           a_    :       List[str]    	=     self.default_image_processor

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

           # forward pass
           with torch.no_grad():
                a_    :       Dict    	=     model(**lowercase__					)

           # verify the logits
           a_    :       Union[str, Any]    	=     torch.Size((1, 1000)					)
           self.assertEqual(outputs.logits.shape	,	lowercase__					)
           a_    :       str    	=     torch.tensor([-0.1545, -0.7667, 0.4642]					).to(lowercase__					)
           self.assertTrue(torch.allclose(outputs.logits[0, :3]	,	lowercase__	,	atol=1e-4					)					)







@require_torch
@require_natten
class     SCREAMING_SNAKE_CASE						(	unittest.TestCase       ,			snake_case_							):
      __magic_name__     :					str             =				(DinatBackbone,) if is_torch_available() else ()
      __magic_name__     :					int             =				DinatConfig






      def        lowercase_	(   self      :     Optional[int]					):


           '''simple docstring'''

           a_    :       Tuple    	=     DinatModelTester(self					)






'''simple docstring'''


from .stack import Stack



def    _SCREAMING_SNAKE_CASE    (			UpperCamelCase__						:				str							):




     """simple docstring"""


     a_    :       Stack[str]    	=     Stack()
     a_    :       Tuple    	=     {"""(""": """)""", """[""": """]""", """{""": """}"""}
     for bracket in parentheses:
          if bracket in bracket_pairs:
               stack.push(UpperCamelCase__							)
          elif bracket in (")", "]", "}"):
               if stack.is_empty() or bracket_pairs[stack.pop()] != bracket:
                    return False
     return stack.is_empty()


if __name__ == "__main__":
 from doctest import testmod

 testmod()

 lowerCAmelCase_			:     Any							    =     ['((()))', '((())', '(()))']
 print('Balanced parentheses demonstration:\n')
 for example in examples:
  lowerCAmelCase_			:     Optional[Any]							    =     '' if balanced_parentheses(example) else 'not '
  print(f"{example} is {not_str}balanced")
