

import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor

class 						__snake_case						(							unittest.TestCase     ):
       """simple docstring"""







       def  a		(		self			,   _UpperCamelCase = False      )			->       Optional[Any]:






              """simple docstring"""


              __snake_case					  =       10

              if only_cross_attention:
                     __snake_case					  =       12
              else:
                     # when only cross attention is not set, the cross attention dim must be the same as the query dim
                     __snake_case					  =       query_dim

              return {
                  "query_dim": query_dim,
                  "cross_attention_dim": cross_attention_dim,
                  "heads": 2,
                  "dim_head": 4,
                  "added_kv_proj_dim": 6,
                  "norm_num_groups": 1,
                  "only_cross_attention": only_cross_attention,
                  "processor": AttnAddedKVProcessor(),
              }


       def  a		(		self			,   _UpperCamelCase			,   _UpperCamelCase      )			->       Any:






              """simple docstring"""


              __snake_case					  =       2

              __snake_case					  =       torch.rand(_UpperCamelCase			,   _UpperCamelCase			,   3			,   2      )
              __snake_case					  =       torch.rand(_UpperCamelCase			,   4			,   _UpperCamelCase      )
              __snake_case					  =       None

              return {
                  "hidden_states": hidden_states,
                  "encoder_hidden_states": encoder_hidden_states,
                  "attention_mask": attention_mask,
              }






       def  a		(		self      )			->       int:






              """simple docstring"""


              torch.manual_seed(0      )

              __snake_case					  =       self.get_constructor_arguments(only_cross_attention=_UpperCamelCase      )
              __snake_case					  =       Attention(**_UpperCamelCase      )

              self.assertTrue(attn.to_k is not None      )
              self.assertTrue(attn.to_v is not None      )

              __snake_case					  =       self.get_forward_arguments(
                  query_dim=constructor_args["""query_dim"""]			,   added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )

              __snake_case					  =       attn(**_UpperCamelCase      )

              # only self attention

              torch.manual_seed(0      )

              __snake_case					  =       self.get_constructor_arguments(only_cross_attention=_UpperCamelCase      )
              __snake_case					  =       Attention(**_UpperCamelCase      )

              self.assertTrue(attn.to_k is None      )
              self.assertTrue(attn.to_v is None      )

              __snake_case					  =       self.get_forward_arguments(
                  query_dim=constructor_args["""query_dim"""]			,   added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )

              __snake_case					  =       attn(**_UpperCamelCase      )

              self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )






class 						__snake_case						(							unittest.TestCase     ):
       """simple docstring"""







       def  a		(		self      )			->       Tuple:






              """simple docstring"""


              __snake_case					  =       DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""			,   safety_checker=_UpperCamelCase      )

              __snake_case					  =       pipe(
                  """foo"""			,   num_inference_steps=2			,   generator=torch.Generator("""cpu"""      ).manual_seed(0      )			,   output_type="""np"""			,   ).images

              # the initial conversion succeeds
              __snake_case					  =       DiffusionPipeline.from_pretrained(
                  """hf-internal-testing/tiny-stable-diffusion-pipe"""			,   device_map="""sequential"""			,   safety_checker=_UpperCamelCase      )

              __snake_case					  =       pipe(
                  """foo"""			,   num_inference_steps=2			,   generator=torch.Generator("""cpu"""      ).manual_seed(0      )			,   output_type="""np"""			,   ).images

              with tempfile.TemporaryDirectory() as tmpdir:
                     # save the converted model
                     pipe.save_pretrained(_UpperCamelCase      )

                     # can also load the converted weights
                     __snake_case					  =       DiffusionPipeline.from_pretrained(_UpperCamelCase			,   device_map="""sequential"""			,   safety_checker=_UpperCamelCase      )

              __snake_case					  =       pipe(
                  """foo"""			,   num_inference_steps=2			,   generator=torch.Generator("""cpu"""      ).manual_seed(0      )			,   output_type="""np"""			,   ).images

              self.assertTrue(np.allclose(_UpperCamelCase			,   _UpperCamelCase			,   atol=1E-5      )      )
              self.assertTrue(np.allclose(_UpperCamelCase			,   _UpperCamelCase			,   atol=1E-5      )      )




from collections import OrderedDict
from typing import Mapping

from packaging import version

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


UpperCamelCase__		   =    logging.get_logger(__name__)

UpperCamelCase__		   =    {
    '''apple/mobilevitv2-1.0''': '''https://huggingface.co/apple/mobilevitv2-1.0/resolve/main/config.json''',
}

class 						__snake_case						(							snake_case__     ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE													=       "mobilevitv2"


       def __init__(		self			,   _UpperCamelCase=3			,   _UpperCamelCase=2_56			,   _UpperCamelCase=2			,   _UpperCamelCase=2.0			,   _UpperCamelCase="swish"			,   _UpperCamelCase=3			,   _UpperCamelCase=32			,   _UpperCamelCase=0.1			,   _UpperCamelCase=0.02			,   _UpperCamelCase=1E-5			,   _UpperCamelCase=5_12			,   _UpperCamelCase=[6, 12, 18]			,   _UpperCamelCase=0.1			,   _UpperCamelCase=2_55			,   _UpperCamelCase=[2, 4, 3]			,   _UpperCamelCase=[1_28, 1_92, 2_56]			,   _UpperCamelCase=1.0			,   _UpperCamelCase=2			,   _UpperCamelCase=0.0			,   _UpperCamelCase=0.0			,   **_UpperCamelCase			,   )			->       List[Any]:






              """simple docstring"""


              super().__init__(**_UpperCamelCase      )

              __snake_case					  =       num_channels
              __snake_case					  =       image_size
              __snake_case					  =       patch_size
              __snake_case					  =       expand_ratio
              __snake_case					  =       hidden_act
              __snake_case					  =       conv_kernel_size
              __snake_case					  =       output_stride
              __snake_case					  =       initializer_range
              __snake_case					  =       layer_norm_eps
              __snake_case					  =       n_attn_blocks
              __snake_case					  =       base_attn_unit_dims
              __snake_case					  =       width_multiplier
              __snake_case					  =       ffn_multiplier
              __snake_case					  =       ffn_dropout
              __snake_case					  =       attn_dropout
              __snake_case					  =       classifier_dropout_prob

              # decode head attributes for semantic segmentation
              __snake_case					  =       aspp_out_channels
              __snake_case					  =       atrous_rates
              __snake_case					  =       aspp_dropout_prob
              __snake_case					  =       semantic_loss_ignore_index






class 						__snake_case						(							snake_case__     ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE													=       version.parse("1.11"     )


       @property
       def  a		(		self      )			->       Mapping[str, Mapping[int, str]]:






              """simple docstring"""


              return OrderedDict([("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""})]      )


       @property
       def  a		(		self      )			->       Mapping[str, Mapping[int, str]]:






              """simple docstring"""


              if self.task == "image-classification":
                     return OrderedDict([("""logits""", {0: """batch"""})]      )
              else:
                     return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})]      )






       @property
       def  a		(		self      )			->       float:






              """simple docstring"""


              return 1E-4

