






import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel

from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()



class   lowerCamelCase				(unittest.TestCase      ):



		@property
		def  __UpperCAmelCase      (							self       )     ->							Optional[Any]:
							torch.manual_seed(0       )
							UpperCAmelCase_   :				Union[str, Any]						=						UNetaDModel(
							    block_out_channels=(3_2, 6_4)    ,		layers_per_block=2    ,		sample_size=3_2    ,		in_channels=3    ,		out_channels=3    ,		down_block_types=('DownBlock2D', 'AttnDownBlock2D')    ,		up_block_types=('AttnUpBlock2D', 'UpBlock2D')    ,		)
							return model



		@property
		def  __UpperCAmelCase      (							self       )     ->							List[Any]:
							torch.manual_seed(0       )
							UpperCAmelCase_   :				str						=						VQModel(
							    block_out_channels=[3_2, 6_4]    ,		in_channels=3    ,		out_channels=3    ,		down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']    ,		up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']    ,		latent_channels=3    ,		)
							return model



		@property
		def  __UpperCAmelCase      (							self       )     ->							Dict:
							torch.manual_seed(0       )
							UpperCAmelCase_   :				Tuple						=						CLIPTextConfig(
							    bos_token_id=0    ,		eos_token_id=2    ,		hidden_size=3_2    ,		intermediate_size=3_7    ,		layer_norm_eps=1E-05    ,		num_attention_heads=4    ,		num_hidden_layers=5    ,		pad_token_id=1    ,		vocab_size=1_0_0_0    ,		)
							return CLIPTextModel(_UpperCamelCase       )



		def  __UpperCAmelCase      (							self       )     ->							Any:
							UpperCAmelCase_   :				Any						=						self.dummy_uncond_unet
							UpperCAmelCase_   :				str						=						DDIMScheduler()
							UpperCAmelCase_   :				int						=						self.dummy_vq_model

							UpperCAmelCase_   :				Tuple						=						LDMPipeline(unet=_UpperCamelCase    ,		vqvae=_UpperCamelCase    ,		scheduler=_UpperCamelCase       )
							ldm.to(_UpperCamelCase       )
							ldm.set_progress_bar_config(disable=_UpperCamelCase       )

							UpperCAmelCase_   :				List[str]						=						torch.manual_seed(0       )
							UpperCAmelCase_   :				List[Any]						=						ldm(generator=_UpperCamelCase    ,		num_inference_steps=2    ,		output_type='numpy'       ).images

							UpperCAmelCase_   :				List[str]						=						torch.manual_seed(0       )
							UpperCAmelCase_   :				Optional[Any]						=						ldm(generator=_UpperCamelCase    ,		num_inference_steps=2    ,		output_type='numpy'    ,		return_dict=_UpperCamelCase       )[0]

							UpperCAmelCase_   :				Tuple						=						image[0, -3:, -3:, -1]
							UpperCAmelCase_   :				str						=						image_from_tuple[0, -3:, -3:, -1]

							assert image.shape == (1, 6_4, 6_4, 3)
							UpperCAmelCase_   :				Optional[int]						=						np.array([0.85_12, 0.8_18, 0.64_11, 0.68_08, 0.44_65, 0.56_18, 0.46, 0.62_31, 0.51_72]       )
							UpperCAmelCase_   :				Dict						=						1E-2 if torch_device != 'mps' else 3E-2

							assert np.abs(image_slice.flatten() - expected_slice       ).max() < tolerance
							assert np.abs(image_from_tuple_slice.flatten() - expected_slice       ).max() < tolerance





@slow
@require_torch
class   lowerCamelCase				(unittest.TestCase      ):



		def  __UpperCAmelCase      (							self       )     ->							List[str]:
							UpperCAmelCase_   :				str						=						LDMPipeline.from_pretrained('CompVis/ldm-celebahq-256'       )
							ldm.to(_UpperCamelCase       )
							ldm.set_progress_bar_config(disable=_UpperCamelCase       )

							UpperCAmelCase_   :				List[str]						=						torch.manual_seed(0       )
							UpperCAmelCase_   :				Optional[Any]						=						ldm(generator=_UpperCamelCase    ,		num_inference_steps=5    ,		output_type='numpy'       ).images

							UpperCAmelCase_   :				Any						=						image[0, -3:, -3:, -1]

							assert image.shape == (1, 2_5_6, 2_5_6, 3)
							UpperCAmelCase_   :				Optional[int]						=						np.array([0.43_99, 0.4_49_75, 0.4_68_25, 0.4_74, 0.43_59, 0.45_81, 0.4_50_95, 0.43_41, 0.44_47]       )
							UpperCAmelCase_   :				str						=						1E-2 if torch_device != 'mps' else 3E-2

							assert np.abs(image_slice.flatten() - expected_slice       ).max() < tolerance














def 					lowercase__				(					__snake_case   :							List[str]					,	__snake_case   :							List[str]					,	__snake_case   :							Union[str, Any]					,	__snake_case   :							Optional[int]					,	__snake_case   :							str					,	__snake_case   :							Optional[Any]  ):


					'''simple docstring'''


					if index == r:
										for j in range(__snake_case  ):
															print(data[j]					,	end=' '  )
										print(' '  )
										return
					#  When no more elements are there to put in data[]
					if i >= n:
										return
					# current is included, put next at next location
					UpperCAmelCase_   :				int						=						arr[i]
					combination_util(__snake_case					,	__snake_case					,	__snake_case					,	index + 1					,	__snake_case					,	i + 1  )
					# current is excluded, replace it with
					# next (Note that i+1 is passed, but
					# index is not changed)
					combination_util(__snake_case					,	__snake_case					,	__snake_case					,	__snake_case					,	__snake_case					,	i + 1  )
					# The main function that prints all combinations
					# of size r in arr[] of size n. This function
					# mainly uses combinationUtil()




def 					lowercase__				(					__snake_case   :							List[Any]					,	__snake_case   :							List[Any]					,	__snake_case   :							str  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Union[str, Any]						=						[0] * r
					# Print all combination using temporary array 'data[]'
					combination_util(__snake_case					,	__snake_case					,	__snake_case					,	0					,	__snake_case					,	0  )


if __name__ == "__main__":
		# Driver code to check the function above
		__UpperCAmelCase     							=						[10, 20, 30, 40, 50]
		print_combination(arr, len(arr), 3)
		# This code is contributed by Ambuj sahu






