



'''simple docstring'''






import inspect
import unittest

from transformers import ViTHybridConfig
from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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 ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
   from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image



class 							lowercase       :


 """simple docstring"""


 def __init__(							self  ,a_  ,a_=13  ,a_=64  ,a_=2  ,a_=3  ,a_=True  ,a_=True  ,a_=32  ,a_=5  ,a_=4  ,a_=37  ,a_="gelu"  ,a_=0.1  ,a_=0.1  ,a_=10  ,a_=0.02  ,a_=[1, 16, 4, 4]  ,a_=None  ,)		->   Tuple:
        _UpperCAmelCase  :						Dict			     = parent
        _UpperCAmelCase  :						List[str]			     = batch_size
        _UpperCAmelCase  :						Union[str, Any]			     = image_size
        _UpperCAmelCase  :						int			     = patch_size
        _UpperCAmelCase  :						Tuple			     = num_channels
        _UpperCAmelCase  :						int			     = is_training
        _UpperCAmelCase  :						Optional[Any]			     = use_labels
        _UpperCAmelCase  :						Optional[Any]			     = hidden_size
        _UpperCAmelCase  :						List[Any]			     = num_hidden_layers
        _UpperCAmelCase  :						Tuple			     = num_attention_heads
        _UpperCAmelCase  :						Union[str, Any]			     = intermediate_size
        _UpperCAmelCase  :						Any			     = hidden_act
        _UpperCAmelCase  :						Optional[Any]			     = hidden_dropout_prob
        _UpperCAmelCase  :						Optional[Any]			     = attention_probs_dropout_prob
        _UpperCAmelCase  :						Tuple			     = type_sequence_label_size
        _UpperCAmelCase  :						List[Any]			     = initializer_range
        _UpperCAmelCase  :						str			     = scope
        _UpperCAmelCase  :						str			     = backbone_featmap_shape

        # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
        # the number of patches is based on the feature map of the backbone, which by default uses an output stride
        # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
        _UpperCAmelCase  :						Dict			     = (self.image_size // 32) ** 2
        _UpperCAmelCase  :						Tuple			     = num_patches + 1







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

        _UpperCAmelCase  :						str			     = None
        if self.use_labels:
               _UpperCAmelCase  :						int			     = ids_tensor([self.batch_size]  ,self.type_sequence_label_size							)

        _UpperCAmelCase  :						Any			     = self.get_config()

        return config, pixel_values, labels







 def _snake_case (							self							)		->   Any:
        _UpperCAmelCase  :						Optional[int]			     = {
            """global_padding""": """same""",
            """layer_type""": """bottleneck""",
            """depths""": [3, 4, 9],
            """out_features""": ["""stage1""", """stage2""", """stage3"""],
            """embedding_dynamic_padding""": True,
            """hidden_sizes""": [4, 8, 16, 32],
            """num_groups""": 2,
        }

        return ViTHybridConfig(
            image_size=self.image_size  ,patch_size=self.patch_size  ,num_channels=self.num_channels  ,hidden_size=self.hidden_size  ,num_hidden_layers=self.num_hidden_layers  ,num_attention_heads=self.num_attention_heads  ,intermediate_size=self.intermediate_size  ,hidden_act=self.hidden_act  ,hidden_dropout_prob=self.hidden_dropout_prob  ,attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,is_decoder=a_  ,initializer_range=self.initializer_range  ,backbone_featmap_shape=self.backbone_featmap_shape  ,backbone_config=a_  ,)







 def _snake_case (							self  ,a_  ,a_  ,a_							)		->   Optional[int]:
        _UpperCAmelCase  :						str			     = ViTHybridModel(config=a_							)
        model.to(a_							)
        model.eval()
        _UpperCAmelCase  :						Union[str, Any]			     = model(a_							)
        self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, self.seq_length, self.hidden_size)							)







 def _snake_case (							self  ,a_  ,a_  ,a_							)		->   Tuple:
        _UpperCAmelCase  :						Tuple			     = self.type_sequence_label_size
        _UpperCAmelCase  :						int			     = ViTHybridForImageClassification(a_							)
        model.to(a_							)
        model.eval()
        _UpperCAmelCase  :						Dict			     = model(a_  ,labels=a_							)
        self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.type_sequence_label_size)							)







 def _snake_case (							self							)		->   Dict:
        _UpperCAmelCase  :						Tuple			     = self.prepare_config_and_inputs()
        _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase  :						Union[str, Any]			     = config_and_inputs
        _UpperCAmelCase  :						Any			     = {"""pixel_values""": pixel_values}
        return config, inputs_dict







@require_torch
class 							lowercase       (  _lowerCamelCase					,					_lowerCamelCase					,					unittest.TestCase				):


 """simple docstring"""


 UpperCAmelCase							=  (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
 UpperCAmelCase							=  (
     {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification}
     if is_torch_available()
     else {}
 )
 UpperCAmelCase							=  False
 UpperCAmelCase							=  False
 UpperCAmelCase							=  False







 def _snake_case (							self							)		->   Dict:
        _UpperCAmelCase  :						Optional[int]			     = ViTHybridModelTester(self							)
        _UpperCAmelCase  :						Optional[int]			     = ConfigTester(self  ,config_class=a_  ,has_text_modality=a_  ,hidden_size=37							)







 def _snake_case (							self							)		->   List[Any]:
        self.config_tester.run_common_tests()







 @unittest.skip(reason="""ViT does not use inputs_embeds"""							)
 def _snake_case (							self							)		->   Union[str, Any]:
        pass







 def _snake_case (							self							)		->   Any:
        _UpperCAmelCase     ,_UpperCAmelCase  :						Optional[Any]			     = self.model_tester.prepare_config_and_inputs_for_common()

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







 def _snake_case (							self							)		->   Optional[int]:
        _UpperCAmelCase     ,_UpperCAmelCase  :						Union[str, Any]			     = self.model_tester.prepare_config_and_inputs_for_common()

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

               _UpperCAmelCase  :						List[Any]			     = ["""pixel_values"""]
               self.assertListEqual(arg_names[:1]  ,a_							)







 def _snake_case (							self							)		->   List[Any]:
        _UpperCAmelCase  :						Any			     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*a_							)







 def _snake_case (							self							)		->   List[str]:
        _UpperCAmelCase  :						Dict			     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*a_							)







 def _snake_case (							self							)		->   Tuple:
        _UpperCAmelCase     ,_UpperCAmelCase  :						List[str]			     = self.model_tester.prepare_config_and_inputs_for_common()

        _UpperCAmelCase  :						str			     = _config_zero_init(a_							)
        for model_class in self.all_model_classes:
               _UpperCAmelCase  :						Union[str, Any]			     = model_class(config=a_							)
               # Skip the check for the backbone
               for name, module in model.named_modules():
                      if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
                             _UpperCAmelCase  :						Dict			     = [f'''{name}.{key}''' for key in module.state_dict().keys()]
                             break

               for name, param in model.named_parameters():
                      if param.requires_grad:
                             if name in backbone_params:
                                    continue
                             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'''  ,)







 @slow
 def _snake_case (							self							)		->   Union[str, Any]:
        for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _UpperCAmelCase  :						Any			     = ViTHybridModel.from_pretrained(a_							)
               self.assertIsNotNone(a_							)







def     snake_case_					(     )->		Any:





       '''simple docstring'''


       _UpperCAmelCase  :						List[Any]			     = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
       return image



@require_torch
@require_vision
class 							lowercase       (  unittest.TestCase				):


 """simple docstring"""


 @cached_property
 def _snake_case (							self							)		->   Optional[int]:
        return (
            ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)
            if is_vision_available()
            else None
        )







 @slow
 def _snake_case (							self							)		->   List[str]:
        _UpperCAmelCase  :						int			     = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]							).to(
            a_							)

        _UpperCAmelCase  :						List[Any]			     = self.default_image_processor
        _UpperCAmelCase  :						Dict			     = prepare_img()
        _UpperCAmelCase  :						List[Any]			     = image_processor(images=a_  ,return_tensors="""pt"""							).to(a_							)

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

        # verify the logits
        _UpperCAmelCase  :						Tuple			     = torch.Size((1, 1_000)							)
        self.assertEqual(outputs.logits.shape  ,a_							)

        _UpperCAmelCase  :						Dict			     = torch.tensor([-1.9090, -0.4993, -0.2389]							).to(a_							)

        self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,a_  ,atol=1E-4							)							)







 @slow
 @require_accelerate
 def _snake_case (							self							)		->   Dict:
        _UpperCAmelCase  :						Optional[Any]			     = ViTHybridImageProcessor.from_pretrained("""google/vit-hybrid-base-bit-384"""							)
        _UpperCAmelCase  :						List[str]			     = ViTHybridForImageClassification.from_pretrained("""google/vit-hybrid-base-bit-384"""  ,device_map="""auto"""							)

        _UpperCAmelCase  :						List[str]			     = prepare_img()

        _UpperCAmelCase  :						str			     = image_processor(images=a_  ,return_tensors="""pt"""							)
        _UpperCAmelCase  :						Any			     = model(**a_							)
        _UpperCAmelCase  :						Dict			     = outputs.logits
        # model predicts one of the 1000 ImageNet classes
        _UpperCAmelCase  :						List[Any]			     = logits.argmax(-1							).item()

        self.assertTrue(model.config.idalabel[predicted_class_idx]  ,"""tabby, tabby cat"""							)








'''simple docstring'''






from ...configuration_utils import PretrainedConfig
from ...utils import logging


A_							: Any						 =	logging.get_logger(__name__)

A_							: Any						 =	{
    """microsoft/markuplm-base""": """https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json""",
    """microsoft/markuplm-large""": """https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json""",
}



class 							lowercase       (  _lowerCamelCase				):


 """simple docstring"""


 UpperCAmelCase							=  """markuplm"""







 def __init__(							self  ,a_=30_522  ,a_=768  ,a_=12  ,a_=12  ,a_=3_072  ,a_="gelu"  ,a_=0.1  ,a_=0.1  ,a_=512  ,a_=2  ,a_=0.02  ,a_=1E-1_2  ,a_=0  ,a_=0  ,a_=2  ,a_=256  ,a_=1_024  ,a_=216  ,a_=1_001  ,a_=32  ,a_=50  ,a_="absolute"  ,a_=True  ,a_=None  ,**a_  ,)		->   Union[str, Any]:
        super().__init__(
            pad_token_id=a_  ,bos_token_id=a_  ,eos_token_id=a_  ,**a_  ,)
        _UpperCAmelCase  :						Optional[int]			     = vocab_size
        _UpperCAmelCase  :						Tuple			     = hidden_size
        _UpperCAmelCase  :						str			     = num_hidden_layers
        _UpperCAmelCase  :						Dict			     = num_attention_heads
        _UpperCAmelCase  :						int			     = hidden_act
        _UpperCAmelCase  :						Optional[Any]			     = intermediate_size
        _UpperCAmelCase  :						Tuple			     = hidden_dropout_prob
        _UpperCAmelCase  :						List[str]			     = attention_probs_dropout_prob
        _UpperCAmelCase  :						Optional[int]			     = max_position_embeddings
        _UpperCAmelCase  :						Tuple			     = type_vocab_size
        _UpperCAmelCase  :						Dict			     = initializer_range
        _UpperCAmelCase  :						List[Any]			     = layer_norm_eps
        _UpperCAmelCase  :						Optional[Any]			     = position_embedding_type
        _UpperCAmelCase  :						Any			     = use_cache
        _UpperCAmelCase  :						List[Any]			     = classifier_dropout
        # additional properties
        _UpperCAmelCase  :						Dict			     = max_depth
        _UpperCAmelCase  :						Union[str, Any]			     = max_xpath_tag_unit_embeddings
        _UpperCAmelCase  :						Optional[int]			     = max_xpath_subs_unit_embeddings
        _UpperCAmelCase  :						List[Any]			     = tag_pad_id
        _UpperCAmelCase  :						Tuple			     = subs_pad_id
        _UpperCAmelCase  :						List[str]			     = xpath_unit_hidden_size



