



"""simple docstring"""





import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()




class 			lowercase__			(      A,					A,					unittest.TestCase    ):
							'''simple docstring'''







							_UpperCAmelCase      				=					VQModel
							_UpperCAmelCase      				=					'''sample'''






							@property
							def        lowerCamelCase_   (  self	,       snake_case=(32, 32)						)			->							Optional[int]:
									_UpperCAmelCase							 =		4
									_UpperCAmelCase							 =		3

									_UpperCAmelCase							 =		floats_tensor((batch_size, num_channels) + sizes						).to(snake_case						)

									return {"sample": image}






							@property
							def        lowerCamelCase_   (  self						)			->							Any:
									return (3, 32, 32)






							@property
							def        lowerCamelCase_   (  self						)			->							int:
									return (3, 32, 32)






							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		{
									    'block_out_channels': [32, 64],
									    'in_channels': 3,
									    'out_channels': 3,
									    'down_block_types': ['DownEncoderBlock2D', 'DownEncoderBlock2D'],
									    'up_block_types': ['UpDecoderBlock2D', 'UpDecoderBlock2D'],
									    'latent_channels': 3,
									}
									_UpperCAmelCase							 =		self.dummy_input
									return init_dict, inputs_dict






							def        lowerCamelCase_   (  self						)			->							Any:
									pass






							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									pass






							def        lowerCamelCase_   (  self						)			->							str:
									_UpperCAmelCase ,					_UpperCAmelCase							 =		VQModel.from_pretrained('fusing/vqgan-dummy'	,       output_loading_info=snake_case						)
									self.assertIsNotNone(snake_case						)
									self.assertEqual(len(loading_info['missing_keys']						)	,       0						)

									model.to(snake_case						)
									_UpperCAmelCase							 =		model(**self.dummy_input						)

									assert image is not None, "Make sure output is not None"






							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		VQModel.from_pretrained('fusing/vqgan-dummy'						)
									model.to(snake_case						).eval()

									torch.manual_seed(0						)
									if torch.cuda.is_available():
											torch.cuda.manual_seed_all(0						)

									_UpperCAmelCase							 =		torch.randn(1	,       model.config.in_channels	,       model.config.sample_size	,       model.config.sample_size						)
									_UpperCAmelCase							 =		image.to(snake_case						)
									with torch.no_grad():
											_UpperCAmelCase							 =		model(snake_case						).sample

									_UpperCAmelCase							 =		output[0, -1, -3:, -3:].flatten().cpu()
									# fmt: off
									_UpperCAmelCase							 =		torch.tensor([-0.0153, -0.4044, -0.1880, -0.5161, -0.2418, -0.4072, -0.1612, -0.0633, -0.0143]						)
									# fmt: on
									self.assertTrue(torch.allclose(snake_case	,       snake_case	,       atol=1E-3						)						)







"""simple docstring"""





from typing import Optional

from torch import nn

from .transformer_ad import TransformeraDModel, TransformeraDModelOutput




class 			lowercase__			(      nn.Module    ):
							'''simple docstring'''







							def __init__(  self	,       snake_case = 16	,       snake_case = 88	,       snake_case = None	,       snake_case = 1	,       snake_case = 0.0	,       snake_case = 32	,       snake_case = None	,       snake_case = False	,       snake_case = None	,       snake_case = None	,       snake_case = "geglu"	,       snake_case = None	,       )			->							str:
									super().__init__()
									_UpperCAmelCase							 =		nn.ModuleList(
									    [
									        TransformeraDModel(
									            num_attention_heads=snake_case	,       attention_head_dim=snake_case	,       in_channels=snake_case	,       num_layers=snake_case	,       dropout=snake_case	,       norm_num_groups=snake_case	,       cross_attention_dim=snake_case	,       attention_bias=snake_case	,       sample_size=snake_case	,       num_vector_embeds=snake_case	,       activation_fn=snake_case	,       num_embeds_ada_norm=snake_case	,       )
									        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, 257]

									# 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        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case=None	,       snake_case=None	,       snake_case=None	,       snake_case = True	,       )			->							Any:
									_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](
											    snake_case	,       encoder_hidden_states=snake_case	,       timestep=snake_case	,       cross_attention_kwargs=snake_case	,       return_dict=snake_case	,       )[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=snake_case						)


