
"""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 		a_						:







    def __init__(			self			:       Optional[Any]     ,					__UpperCamelCase			:       Optional[int]     ,					__UpperCamelCase			:       Tuple=13     ,					__UpperCamelCase			:       List[str]=64     ,					__UpperCamelCase			:       int=2     ,					__UpperCamelCase			:       Dict=3     ,					__UpperCamelCase			:       Any=True     ,					__UpperCamelCase			:       Dict=True     ,					__UpperCamelCase			:       Any=32     ,					__UpperCamelCase			:       Tuple=5     ,					__UpperCamelCase			:       List[Any]=4     ,					__UpperCamelCase			:       List[str]=37     ,					__UpperCamelCase			:       List[Any]="gelu"     ,					__UpperCamelCase			:       int=0.1     ,					__UpperCamelCase			:       int=0.1     ,					__UpperCamelCase			:       Optional[int]=10     ,					__UpperCamelCase			:       List[str]=0.0_2     ,					__UpperCamelCase			:       str=[1, 16, 4, 4]     ,					__UpperCamelCase			:       str=None     ,					)		->List[Any]:






        '''simple docstring'''






        _UpperCAmelCase          =   parent
        _UpperCAmelCase          =   batch_size
        _UpperCAmelCase          =   image_size
        _UpperCAmelCase          =   patch_size
        _UpperCAmelCase          =   num_channels
        _UpperCAmelCase          =   is_training
        _UpperCAmelCase          =   use_labels
        _UpperCAmelCase          =   hidden_size
        _UpperCAmelCase          =   num_hidden_layers
        _UpperCAmelCase          =   num_attention_heads
        _UpperCAmelCase          =   intermediate_size
        _UpperCAmelCase          =   hidden_act
        _UpperCAmelCase          =   hidden_dropout_prob
        _UpperCAmelCase          =   attention_probs_dropout_prob
        _UpperCAmelCase          =   type_sequence_label_size
        _UpperCAmelCase          =   initializer_range
        _UpperCAmelCase          =   scope
        _UpperCAmelCase          =   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          =   (self.image_size // 32) ** 2
        _UpperCAmelCase          =   num_patches + 1







    def      _snake_case					(			self			:       List[Any]     )		->Optional[int]:






        '''simple docstring'''






        _UpperCAmelCase          =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

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

        _UpperCAmelCase          =   self.get_config()

        return config, pixel_values, labels







    def      _snake_case					(			self			:       List[str]     )		->Optional[Any]:






        '''simple docstring'''






        _UpperCAmelCase          =   {
            """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=__UpperCamelCase     ,					initializer_range=self.initializer_range     ,					backbone_featmap_shape=self.backbone_featmap_shape     ,					backbone_config=__UpperCamelCase     ,					)







    def      _snake_case					(			self			:       Optional[Any]     ,					__UpperCamelCase			:       Dict     ,					__UpperCamelCase			:       Tuple     ,					__UpperCamelCase			:       Tuple     )		->List[str]:






        '''simple docstring'''






        _UpperCAmelCase          =   ViTHybridModel(config=__UpperCamelCase     )
        model.to(__UpperCamelCase     )
        model.eval()
        _UpperCAmelCase          =   model(__UpperCamelCase     )
        self.parent.assertEqual(result.last_hidden_state.shape     ,					(self.batch_size, self.seq_length, self.hidden_size)     )







    def      _snake_case					(			self			:       Any     ,					__UpperCamelCase			:       Union[str, Any]     ,					__UpperCamelCase			:       Union[str, Any]     ,					__UpperCamelCase			:       Dict     )		->Dict:






        '''simple docstring'''






        _UpperCAmelCase          =   self.type_sequence_label_size
        _UpperCAmelCase          =   ViTHybridForImageClassification(__UpperCamelCase     )
        model.to(__UpperCamelCase     )
        model.eval()
        _UpperCAmelCase          =   model(__UpperCamelCase     ,					labels=__UpperCamelCase     )
        self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.type_sequence_label_size)     )







    def      _snake_case					(			self			:       int     )		->Tuple:






        '''simple docstring'''






        _UpperCAmelCase          =   self.prepare_config_and_inputs()
        _UpperCAmelCase    ,_UpperCAmelCase    ,_UpperCAmelCase          =   config_and_inputs
        _UpperCAmelCase          =   {"""pixel_values""": pixel_values}
        return config, inputs_dict







@require_torch
class 		a_						( _UpperCAmelCase     ,						_UpperCAmelCase     ,						unittest.TestCase	):
    a      :				Union[str, Any]     =				(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
    a      :				Dict     =				(
        {'feature-extraction': ViTHybridModel, 'image-classification': ViTHybridForImageClassification}
        if is_torch_available()
        else {}
    )
    a      :				str     =				False
    a      :				Dict     =				False
    a      :				Union[str, Any]     =				False







    def      _snake_case					(			self			:       str     )		->Optional[int]:






        '''simple docstring'''






        _UpperCAmelCase          =   ViTHybridModelTester(self     )
        _UpperCAmelCase          =   ConfigTester(self     ,					config_class=__UpperCamelCase     ,					has_text_modality=__UpperCamelCase     ,					hidden_size=37     )







    def      _snake_case					(			self			:       Any     )		->Optional[int]:






        '''simple docstring'''






        self.config_tester.run_common_tests()







    @unittest.skip(reason="""ViT does not use inputs_embeds"""     )
    def      _snake_case					(			self			:       List[Any]     )		->Optional[Any]:






        '''simple docstring'''






        pass







    def      _snake_case					(			self			:       Union[str, Any]     )		->int:






        '''simple docstring'''






        _UpperCAmelCase    ,_UpperCAmelCase          =   self.model_tester.prepare_config_and_inputs_for_common()

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







    def      _snake_case					(			self			:       Union[str, Any]     )		->Optional[int]:






        '''simple docstring'''






        _UpperCAmelCase    ,_UpperCAmelCase          =   self.model_tester.prepare_config_and_inputs_for_common()

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

            _UpperCAmelCase          =   ["""pixel_values"""]
            self.assertListEqual(arg_names[:1]     ,					__UpperCamelCase     )







    def      _snake_case					(			self			:       int     )		->Any:






        '''simple docstring'''






        _UpperCAmelCase          =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__UpperCamelCase     )







    def      _snake_case					(			self			:       Union[str, Any]     )		->Optional[int]:






        '''simple docstring'''






        _UpperCAmelCase          =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase     )







    def      _snake_case					(			self			:       Any     )		->Tuple:






        '''simple docstring'''






        _UpperCAmelCase    ,_UpperCAmelCase          =   self.model_tester.prepare_config_and_inputs_for_common()

        _UpperCAmelCase          =   _config_zero_init(__UpperCamelCase     )
        for model_class in self.all_model_classes:
            _UpperCAmelCase          =   model_class(config=__UpperCamelCase     )
            # Skip the check for the backbone
            for name, module in model.named_modules():
                if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
                    _UpperCAmelCase          =   [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			:       Tuple     )		->Optional[Any]:






        '''simple docstring'''






        for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            _UpperCAmelCase          =   ViTHybridModel.from_pretrained(__UpperCamelCase     )
            self.assertIsNotNone(__UpperCamelCase     )







def  _UpperCamelCase	(						)    ->  Optional[int]:





    """simple docstring"""

    _UpperCAmelCase          =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
    return image







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







    @cached_property
    def      _snake_case					(			self			:       str     )		->List[Any]:






        '''simple docstring'''






        return (
            ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]     )
            if is_vision_available()
            else None
        )







    @slow
    def      _snake_case					(			self			:       List[Any]     )		->int:






        '''simple docstring'''






        _UpperCAmelCase          =   ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]     ).to(
            __UpperCamelCase     )

        _UpperCAmelCase          =   self.default_image_processor
        _UpperCAmelCase          =   prepare_img()
        _UpperCAmelCase          =   image_processor(images=__UpperCamelCase     ,					return_tensors="""pt"""     ).to(__UpperCamelCase     )

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

        # verify the logits
        _UpperCAmelCase          =   torch.Size((1, 10_00)     )
        self.assertEqual(outputs.logits.shape     ,					__UpperCamelCase     )

        _UpperCAmelCase          =   torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9]     ).to(__UpperCamelCase     )

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







    @slow
    @require_accelerate
    def      _snake_case					(			self			:       List[Any]     )		->List[str]:






        '''simple docstring'''






        _UpperCAmelCase          =   ViTHybridImageProcessor.from_pretrained("""google/vit-hybrid-base-bit-384"""     )
        _UpperCAmelCase          =   ViTHybridForImageClassification.from_pretrained("""google/vit-hybrid-base-bit-384"""     ,					device_map="""auto"""     )

        _UpperCAmelCase          =   prepare_img()

        _UpperCAmelCase          =   image_processor(images=__UpperCamelCase     ,					return_tensors="""pt"""     )
        _UpperCAmelCase          =   model(**__UpperCamelCase     )
        _UpperCAmelCase          =   outputs.logits
        # model predicts one of the 1000 ImageNet classes
        _UpperCAmelCase          =   logits.argmax(-1     ).item()

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

"""simple docstring"""



from typing import Optional

from torch import nn

from .transformer_ad import TransformeraDModel, TransformeraDModelOutput




class 		a_						( nn.Module	):







    def __init__(			self			:       List[str]     ,					__UpperCamelCase			:       int = 16     ,					__UpperCamelCase			:       int = 88     ,					__UpperCamelCase			:       Optional[int] = None     ,					__UpperCamelCase			:       int = 1     ,					__UpperCamelCase			:       float = 0.0     ,					__UpperCamelCase			:       int = 32     ,					__UpperCamelCase			:       Optional[int] = None     ,					__UpperCamelCase			:       bool = False     ,					__UpperCamelCase			:       Optional[int] = None     ,					__UpperCamelCase			:       Optional[int] = None     ,					__UpperCamelCase			:       str = "geglu"     ,					__UpperCamelCase			:       Optional[int] = None     ,					)		->Dict:






        '''simple docstring'''






        super().__init__()
        _UpperCAmelCase          =   nn.ModuleList(
            [
                TransformeraDModel(
                    num_attention_heads=__UpperCamelCase     ,					attention_head_dim=__UpperCamelCase     ,					in_channels=__UpperCamelCase     ,					num_layers=__UpperCamelCase     ,					dropout=__UpperCamelCase     ,					norm_num_groups=__UpperCamelCase     ,					cross_attention_dim=__UpperCamelCase     ,					attention_bias=__UpperCamelCase     ,					sample_size=__UpperCamelCase     ,					num_vector_embeds=__UpperCamelCase     ,					activation_fn=__UpperCamelCase     ,					num_embeds_ada_norm=__UpperCamelCase     ,					)
                for _ in range(2     )
            ]     )

        # Variables that can be set by a pipeline:

        # The ratio of transformer1 to transformer2's output states to be combined during inference
        _UpperCAmelCase          =   0.5

        # The shape of `encoder_hidden_states` is expected to be
        # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)`
        _UpperCAmelCase          =   [77, 2_57]

        # Which transformer to use to encode which condition.
        # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])`
        _UpperCAmelCase          =   [1, 0]







    def      _snake_case					(			self			:       Optional[int]     ,					__UpperCamelCase			:       Any     ,					__UpperCamelCase			:       List[Any]     ,					__UpperCamelCase			:       Union[str, Any]=None     ,					__UpperCamelCase			:       Union[str, Any]=None     ,					__UpperCamelCase			:       Tuple=None     ,					__UpperCamelCase			:       bool = True     ,					)		->Tuple:






        '''simple docstring'''






        _UpperCAmelCase          =   hidden_states

        _UpperCAmelCase          =   []
        _UpperCAmelCase          =   0
        # attention_mask is not used yet
        for i in range(2     ):
            # for each of the two transformers, pass the corresponding condition tokens
            _UpperCAmelCase          =   encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]]
            _UpperCAmelCase          =   self.transformer_index_for_condition[i]
            _UpperCAmelCase          =   self.transformers[transformer_index](
                __UpperCamelCase     ,					encoder_hidden_states=__UpperCamelCase     ,					timestep=__UpperCamelCase     ,					cross_attention_kwargs=__UpperCamelCase     ,					return_dict=__UpperCamelCase     ,					)[0]
            encoded_states.append(encoded_state - input_states     )
            tokens_start += self.condition_lengths[i]

        _UpperCAmelCase          =   encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio)
        _UpperCAmelCase          =   output_states + input_states

        if not return_dict:
            return (output_states,)

        return TransformeraDModelOutput(sample=__UpperCamelCase     )