






import tempfile
import unittest

import numpy as np
import torch

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





class __lowerCamelCase			(				unittest.TestCase):





	"""simple docstring"""






	def 	UpperCamelCase   (							self	,   UpperCAmelCase = False   ):
							"""simple docstring"""







							_UpperCAmelCase     =      10

							if only_cross_attention:
													_UpperCAmelCase     =      12
							else:
													# when only cross attention is not set, the cross attention dim must be the same as the query dim
													_UpperCAmelCase     =      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 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase   ):
							"""simple docstring"""







							_UpperCAmelCase     =      2

							_UpperCAmelCase     =      torch.rand(UpperCAmelCase	,   UpperCAmelCase	,   3	,   2   )
							_UpperCAmelCase     =      torch.rand(UpperCAmelCase	,   4	,   UpperCAmelCase   )
							_UpperCAmelCase     =      None

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

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							torch.manual_seed(0   )

							_UpperCAmelCase     =      self.get_constructor_arguments(only_cross_attention=UpperCAmelCase   )
							_UpperCAmelCase     =      Attention(**UpperCAmelCase   )

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

							_UpperCAmelCase     =      self.get_forward_arguments(
							    query_dim=constructor_args['query_dim']	,   added_kv_proj_dim=constructor_args['added_kv_proj_dim']   )

							_UpperCAmelCase     =      attn(**UpperCAmelCase   )

							# only self attention

							torch.manual_seed(0   )

							_UpperCAmelCase     =      self.get_constructor_arguments(only_cross_attention=UpperCAmelCase   )
							_UpperCAmelCase     =      Attention(**UpperCAmelCase   )

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

							_UpperCAmelCase     =      self.get_forward_arguments(
							    query_dim=constructor_args['query_dim']	,   added_kv_proj_dim=constructor_args['added_kv_proj_dim']   )

							_UpperCAmelCase     =      attn(**UpperCAmelCase   )

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





class __lowerCamelCase			(				unittest.TestCase):





	"""simple docstring"""






	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      DiffusionPipeline.from_pretrained('hf-internal-testing/tiny-stable-diffusion-pipe'	,   safety_checker=UpperCAmelCase   )

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

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

							_UpperCAmelCase     =      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
													_UpperCAmelCase     =      DiffusionPipeline.from_pretrained(UpperCAmelCase	,   device_map='sequential'	,   safety_checker=UpperCAmelCase   )

							_UpperCAmelCase     =      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 ...configuration_utils import PretrainedConfig
from ...utils import logging


_a				     =  logging.get_logger(__name__)

_a				     =  {
    '''facebook/regnet-y-040''': '''https://huggingface.co/facebook/regnet-y-040/blob/main/config.json''',
}





class __lowerCamelCase			(				snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				"regnet"
	UpperCamelCase__								=				["x", "y"]

	def __init__(							self	,   UpperCAmelCase=3	,   UpperCAmelCase=32	,   UpperCAmelCase=[128, 192, 512, 1088]	,   UpperCAmelCase=[2, 6, 12, 2]	,   UpperCAmelCase=64	,   UpperCAmelCase="y"	,   UpperCAmelCase="relu"	,   **UpperCAmelCase	,   ):
							"""simple docstring"""







							super().__init__(**UpperCAmelCase   )
							if layer_type not in self.layer_types:
													raise ValueError(F"""layer_type={layer_type} is not one of {",".join(self.layer_types   )}"""   )
							_UpperCAmelCase     =      num_channels
							_UpperCAmelCase     =      embedding_size
							_UpperCAmelCase     =      hidden_sizes
							_UpperCAmelCase     =      depths
							_UpperCAmelCase     =      groups_width
							_UpperCAmelCase     =      layer_type
							_UpperCAmelCase     =      hidden_act
							# always downsample in the first stage
							_UpperCAmelCase     =      True
