






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







   """simple docstring"""







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



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

    __a    :	str									=						None
    if self.use_labels:
     __a    :	Union[str, Any]									=						ids_tensor([self.batch_size]   ,   self.num_labels			)

    __a    :	Optional[int]									=						self.get_config()

    return config, pixel_values, labels



   def    __magic_name__    ( self			)    ->   Union[str, 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    __magic_name__    ( self   ,   _A   ,   _A   ,   _A			)    ->   List[str]:
    __a    :	Tuple									=						DinatModel(config=_A			)
    model.to(_A			)
    model.eval()
    __a    :	int									=						model(_A			)

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

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

    __a    :	Optional[Any]									=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
    __a    :	int									=						model(_A			)
    self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.num_labels)			)



   def    __magic_name__    ( self   ,   _A   ,   _A   ,   _A			)    ->   str:
    __a    :	Union[str, Any]									=						DinatBackbone(config=_A			)
    model.to(_A			)
    model.eval()
    __a    :	Tuple									=						model(_A			)

    # 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    :	Optional[Any]									=						None
    __a    :	Optional[Any]									=						DinatBackbone(config=_A			)
    model.to(_A			)
    model.eval()
    __a    :	Tuple									=						model(_A			)

    # 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    __magic_name__    ( self			)    ->   Any:
    __a    :	Tuple									=						self.prepare_config_and_inputs()
    __a			,    __a			,    __a    :	Dict									=						config_and_inputs
    __a    :	Any									=						{'pixel_values': pixel_values}
    return config, inputs_dict




@require_natten
@require_torch
class 							lowerCAmelCase					(							lowerCamelCase__							,      lowerCamelCase__							,      unittest.TestCase   ):







   """simple docstring"""







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

   _A						=   False
   _A						=   False
   _A						=   False
   _A						=   False



   def    __magic_name__    ( self			)    ->   int:
    __a    :	Union[str, Any]									=						DinatModelTester(self			)
    __a    :	List[str]									=						ConfigTester(self   ,   config_class=_A   ,   embed_dim=37			)



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



   def    __magic_name__    ( self			)    ->   str:
    __a    :	Dict									=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_A			)



   def    __magic_name__    ( self			)    ->   List[str]:
    __a    :	int									=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*_A			)



   def    __magic_name__    ( self			)    ->   Any:
    __a    :	Any									=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_backbone(*_A			)



   @unittest.skip(reason='Dinat does not use inputs_embeds'			)
   def    __magic_name__    ( self			)    ->   str:
    pass



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



   def    __magic_name__    ( self			)    ->   int:
    __a			,    __a    :	List[Any]									=						self.model_tester.prepare_config_and_inputs_for_common()

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



   def    __magic_name__    ( self			)    ->   Tuple:
    __a			,    __a    :	Union[str, Any]									=						self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     __a    :	List[str]									=						model_class(_A			)
     __a    :	List[Any]									=						inspect.signature(model.forward			)
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     __a    :	int									=						[*signature.parameters.keys()]

     __a    :	Any									=						['pixel_values']
     self.assertListEqual(arg_names[:1]   ,   _A			)



   def    __magic_name__    ( self			)    ->   Optional[int]:
    self.skipTest('Dinat\'s attention operation is handled entirely by NATTEN.'			)



   def    __magic_name__    ( self   ,   _A   ,   _A   ,   _A   ,   _A			)    ->   str:
    __a    :	Union[str, Any]									=						model_class(_A			)
    model.to(_A			)
    model.eval()

    with torch.no_grad():
     __a    :	Optional[int]									=						model(**self._prepare_for_class(_A   ,   _A			)			)

    __a    :	str									=						outputs.hidden_states

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

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

    __a    :	str									=						image_size[0] // patch_size[0]
    __a    :	Dict									=						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[Any]									=						outputs.reshaped_hidden_states
     self.assertEqual(len(_A			)   ,   _A			)

     __a			,    __a			,    __a			,    __a    :	Any									=						reshaped_hidden_states[0].shape
     __a    :	Optional[int]									=						(
         reshaped_hidden_states[0].view(_A   ,   _A   ,   _A   ,   _A			).permute(0   ,   2   ,   3   ,   1			)
     )
     self.assertListEqual(
         list(reshaped_hidden_states.shape[-3:]			)   ,   [height, width, self.model_tester.embed_dim]   ,   )



   def    __magic_name__    ( self			)    ->   List[Any]:
    __a			,    __a    :	str									=						self.model_tester.prepare_config_and_inputs_for_common()

    __a    :	List[str]									=						(
        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    :	Union[str, Any]									=						True
     self.check_hidden_states_output(_A   ,   _A   ,   _A   ,   _A			)

     # check that output_hidden_states also work using config
     del inputs_dict["output_hidden_states"]
     __a    :	Dict									=						True

     self.check_hidden_states_output(_A   ,   _A   ,   _A   ,   _A			)



   @slow
   def    __magic_name__    ( self			)    ->   Optional[Any]:
    for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     __a    :	List[Any]									=						DinatModel.from_pretrained(_A			)
     self.assertIsNotNone(_A			)



   def    __magic_name__    ( self			)    ->   int:
    __a			,    __a    :	Any									=						self.model_tester.prepare_config_and_inputs_for_common()

    __a    :	Any									=						_config_zero_init(_A			)
    for model_class in self.all_model_classes:
     __a    :	int									=						model_class(config=_A			)
     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    __magic_name__    ( self			)    ->   Optional[Any]:
    return AutoImageProcessor.from_pretrained('shi-labs/dinat-mini-in1k-224'			) if is_vision_available() else None



   @slow
   def    __magic_name__    ( self			)    ->   List[str]:
    __a    :	List[str]									=						DinatForImageClassification.from_pretrained('shi-labs/dinat-mini-in1k-224'			).to(_A			)
    __a    :	List[Any]									=						self.default_image_processor

    __a    :	Union[str, Any]									=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
    __a    :	Any									=						image_processor(images=_A   ,   return_tensors='pt'			).to(_A			)

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

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




@require_torch
@require_natten
class 							lowerCAmelCase					(							unittest.TestCase							,      lowerCamelCase__   ):







   """simple docstring"""







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



   def    __magic_name__    ( self			)    ->   str:
    __a    :	Dict									=						DinatModelTester(self			)












'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_				      =							logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_				      =							{
    "squeezebert/squeezebert-uncased": (
        "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/config.json"
    ),
    "squeezebert/squeezebert-mnli": "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/config.json",
    "squeezebert/squeezebert-mnli-headless": (
        "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/config.json"
    ),
}




class 							lowerCAmelCase					(							lowerCamelCase__   ):







   """simple docstring"""







   _A						=   SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP
   _A						=   'squeezebert'



   def __init__( self   ,   _A=30522   ,   _A=768   ,   _A=12   ,   _A=12   ,   _A=3072   ,   _A="gelu"   ,   _A=0.1   ,   _A=0.1   ,   _A=512   ,   _A=2   ,   _A=0.02   ,   _A=1E-1_2   ,   _A=0   ,   _A=768   ,   _A=4   ,   _A=4   ,   _A=4   ,   _A=1   ,   _A=4   ,   _A=4   ,   **_A   ,   )    ->   List[Any]:
    super().__init__(pad_token_id=_A   ,   **_A			)

    __a    :	Optional[int]									=						vocab_size
    __a    :	Optional[int]									=						hidden_size
    __a    :	int									=						num_hidden_layers
    __a    :	int									=						num_attention_heads
    __a    :	Tuple									=						hidden_act
    __a    :	Optional[Any]									=						intermediate_size
    __a    :	Any									=						hidden_dropout_prob
    __a    :	List[Any]									=						attention_probs_dropout_prob
    __a    :	List[str]									=						max_position_embeddings
    __a    :	Tuple									=						type_vocab_size
    __a    :	Tuple									=						initializer_range
    __a    :	List[str]									=						layer_norm_eps
    __a    :	str									=						embedding_size
    __a    :	Any									=						q_groups
    __a    :	Any									=						k_groups
    __a    :	Union[str, Any]									=						v_groups
    __a    :	List[str]									=						post_attention_groups
    __a    :	Dict									=						intermediate_groups
    __a    :	Optional[Any]									=						output_groups




class 							lowerCAmelCase					(							lowerCamelCase__   ):







   """simple docstring"""







   @property
   def    __magic_name__    ( self			)    ->   Mapping[str, Mapping[int, str]]:
    if self.task == "multiple-choice":
     __a    :	Tuple									=						{0: 'batch', 1: 'choice', 2: 'sequence'}
    else:
     __a    :	List[Any]									=						{0: 'batch', 1: 'sequence'}
    return OrderedDict(
        [
            ('input_ids', dynamic_axis),
            ('attention_mask', dynamic_axis),
            ('token_type_ids', dynamic_axis),
        ]			)




