




'''simple docstring'''




import tempfile
import unittest

import numpy as np
import torch

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






class     UpperCamelCase_     (unittest.TestCase     ):







						"""simple docstring"""


						def       _a    (     self      :       Any							, _lowerCamelCase      :       bool = False    ):





											"""simple docstring"""







											A_ :     Dict =							10

											if only_cross_attention:
																A_ :     Union[str, Any] =							12
											else:
																# when only cross attention is not set, the cross attention dim must be the same as the query dim
																A_ :     str =							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      :       List[str]							, _lowerCamelCase      :       Tuple							, _lowerCamelCase      :       List[Any]    ):





											"""simple docstring"""







											A_ :     Dict =							2

											A_ :     Dict =							torch.rand(_lowerCamelCase							, _lowerCamelCase							, 3							, 2    )
											A_ :     int =							torch.rand(_lowerCamelCase							, 4							, _lowerCamelCase    )
											A_ :     Dict =							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    )

											A_ :     str =							self.get_constructor_arguments(only_cross_attention=_lowerCamelCase    )
											A_ :     Optional[Any] =							Attention(**_lowerCamelCase    )

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

											A_ :     int =							self.get_forward_arguments(
											    query_dim=constructor_args['''query_dim''']							, added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']    )

											A_ :     Optional[Any] =							attn(**_lowerCamelCase    )

											# only self attention

											torch.manual_seed(0    )

											A_ :     List[str] =							self.get_constructor_arguments(only_cross_attention=_lowerCamelCase    )
											A_ :     Optional[Any] =							Attention(**_lowerCamelCase    )

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

											A_ :     Optional[int] =							self.get_forward_arguments(
											    query_dim=constructor_args['''query_dim''']							, added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']    )

											A_ :     Dict =							attn(**_lowerCamelCase    )

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






class     UpperCamelCase_     (unittest.TestCase     ):







						"""simple docstring"""


						def       _a    (     self      :       int    ):





											"""simple docstring"""







											A_ :     List[str] =							DiffusionPipeline.from_pretrained('''hf-internal-testing/tiny-stable-diffusion-pipe'''							, safety_checker=_lowerCamelCase    )

											A_ :     Optional[Any] =							pipe(
											    '''foo'''							, num_inference_steps=2							, generator=torch.Generator('''cpu'''    ).manual_seed(0    )							, output_type='''np'''							, ).images

											# the initial conversion succeeds
											A_ :     Dict =							DiffusionPipeline.from_pretrained(
											    '''hf-internal-testing/tiny-stable-diffusion-pipe'''							, device_map='''sequential'''							, safety_checker=_lowerCamelCase    )

											A_ :     List[Any] =							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(_lowerCamelCase    )

																# can also load the converted weights
																A_ :     Optional[int] =							DiffusionPipeline.from_pretrained(_lowerCamelCase							, device_map='''sequential'''							, safety_checker=_lowerCamelCase    )

											A_ :     Dict =							pipe(
											    '''foo'''							, num_inference_steps=2							, generator=torch.Generator('''cpu'''    ).manual_seed(0    )							, output_type='''np'''							, ).images

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









'''simple docstring'''




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


snake_case__          =						logging.get_logger(__name__)

snake_case__          =						{
    """bert-base-uncased""": """https://huggingface.co/bert-base-uncased/resolve/main/config.json""",
    # QDQBERT models can be loaded from any BERT checkpoint, available at https://huggingface.co/models?filter=bert
}






class     UpperCamelCase_     (a__     ):







						"""simple docstring"""


						_lowerCAmelCase    						=					'qdqbert'







						def __init__(     self      :       Union[str, Any]							, _lowerCamelCase      :       List[Any]=30522							, _lowerCamelCase      :       str=768							, _lowerCamelCase      :       Optional[Any]=12							, _lowerCamelCase      :       str=12							, _lowerCamelCase      :       List[Any]=3072							, _lowerCamelCase      :       Union[str, Any]="gelu"							, _lowerCamelCase      :       Dict=0.1							, _lowerCamelCase      :       Union[str, Any]=0.1							, _lowerCamelCase      :       Optional[int]=512							, _lowerCamelCase      :       Dict=2							, _lowerCamelCase      :       Optional[Any]=0.02							, _lowerCamelCase      :       List[Any]=1E-12							, _lowerCamelCase      :       Tuple=True							, _lowerCamelCase      :       Tuple=1							, _lowerCamelCase      :       Optional[Any]=0							, _lowerCamelCase      :       Optional[int]=2							, **_lowerCamelCase      :       List[str]							, ):





											"""simple docstring"""







											super().__init__(pad_token_id=_lowerCamelCase							, bos_token_id=_lowerCamelCase							, eos_token_id=_lowerCamelCase							, **_lowerCamelCase    )

											A_ :     Optional[Any] =							vocab_size
											A_ :     str =							max_position_embeddings
											A_ :     List[str] =							hidden_size
											A_ :     str =							num_hidden_layers
											A_ :     List[str] =							num_attention_heads
											A_ :     List[str] =							intermediate_size
											A_ :     int =							hidden_act
											A_ :     Tuple =							hidden_dropout_prob
											A_ :     Optional[Any] =							attention_probs_dropout_prob
											A_ :     Dict =							initializer_range
											A_ :     Optional[int] =							type_vocab_size
											A_ :     Any =							layer_norm_eps
											A_ :     Optional[int] =							use_cache



