






import gc
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNetaDConditionModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_numpy,
    nightly,
    require_torch_gpu,
    slow,
    torch_device,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class      UpperCamelCase (				snake_case__			,       unittest.TestCase ):







		"""simple docstring"""



		snake_case    =   LDMTextToImagePipeline
		snake_case    =   TEXT_TO_IMAGE_PARAMS - {
		    "negative_prompt",
		    "negative_prompt_embeds",
		    "cross_attention_kwargs",
		    "prompt_embeds",
		}
		snake_case    =   PipelineTesterMixin.required_optional_params - {
		    "num_images_per_prompt",
		    "callback",
		    "callback_steps",
		}
		snake_case    =   TEXT_TO_IMAGE_BATCH_PARAMS
		snake_case    =   False
		def       A(  self		:       List[Any]		)    ->   Dict:


						'''simple docstring'''
						torch.manual_seed(0		)
						A	 =      UNetaDConditionModel(
						    block_out_channels=(3_2, 6_4)						,layers_per_block=2						,sample_size=3_2						,in_channels=4						,out_channels=4						,down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')						,up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')						,cross_attention_dim=3_2						,)
						A	 =      DDIMScheduler(
						    beta_start=0.00085						,beta_end=0.012						,beta_schedule='scaled_linear'						,clip_sample=_SCREAMING_SNAKE_CASE						,set_alpha_to_one=_SCREAMING_SNAKE_CASE						,)
						torch.manual_seed(0		)
						A	 =      AutoencoderKL(
						    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=4						,)
						torch.manual_seed(0		)
						A	 =      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						,)
						A	 =      CLIPTextModel(_SCREAMING_SNAKE_CASE		)
						A	 =      CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'		)

						A	 =      {
						    'unet': unet,
						    'scheduler': scheduler,
						    'vqvae': vae,
						    'bert': text_encoder,
						    'tokenizer': tokenizer,
						}
						return components
		def       A(  self		:       Dict						,_SCREAMING_SNAKE_CASE		:       int						,_SCREAMING_SNAKE_CASE		:       str=0		)    ->   int:


						'''simple docstring'''
						if str(_SCREAMING_SNAKE_CASE		).startswith('mps'		):
										A	 =      torch.manual_seed(_SCREAMING_SNAKE_CASE		)
						else:
										A	 =      torch.Generator(device=_SCREAMING_SNAKE_CASE		).manual_seed(_SCREAMING_SNAKE_CASE		)
						A	 =      {
						    'prompt': 'A painting of a squirrel eating a burger',
						    'generator': generator,
						    'num_inference_steps': 2,
						    'guidance_scale': 6.0,
						    'output_type': 'numpy',
						}
						return inputs







		def       A(  self		:       Optional[int]		)    ->   Optional[int]:


						'''simple docstring'''
						A	 =      'cpu'  # ensure determinism for the device-dependent torch.Generator

						A	 =      self.get_dummy_components()
						A	 =      LDMTextToImagePipeline(**_SCREAMING_SNAKE_CASE		)
						pipe.to(_SCREAMING_SNAKE_CASE		)
						pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

						A	 =      self.get_dummy_inputs(_SCREAMING_SNAKE_CASE		)
						A	 =      pipe(**_SCREAMING_SNAKE_CASE		).images
						A	 =      image[0, -3:, -3:, -1]

						assert image.shape == (1, 1_6, 1_6, 3)
						A	 =      np.array([0.6101, 0.6156, 0.5622, 0.4895, 0.6661, 0.3804, 0.5748, 0.6136, 0.5014]		)

						assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-3


@slow
@require_torch_gpu
class      UpperCamelCase (				unittest.TestCase ):







		"""simple docstring"""



		def       A(  self		:       List[str]		)    ->   Tuple:


						'''simple docstring'''
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()
		def       A(  self		:       Any						,_SCREAMING_SNAKE_CASE		:       Tuple						,_SCREAMING_SNAKE_CASE		:       List[Any]=torch.floataa						,_SCREAMING_SNAKE_CASE		:       Any=0		)    ->   Tuple:


						'''simple docstring'''
						A	 =      torch.manual_seed(_SCREAMING_SNAKE_CASE		)
						A	 =      np.random.RandomState(_SCREAMING_SNAKE_CASE		).standard_normal((1, 4, 3_2, 3_2)		)
						A	 =      torch.from_numpy(_SCREAMING_SNAKE_CASE		).to(device=_SCREAMING_SNAKE_CASE						,dtype=_SCREAMING_SNAKE_CASE		)
						A	 =      {
						    'prompt': 'A painting of a squirrel eating a burger',
						    'latents': latents,
						    'generator': generator,
						    'num_inference_steps': 3,
						    'guidance_scale': 6.0,
						    'output_type': 'numpy',
						}
						return inputs







		def       A(  self		:       str		)    ->   Any:


						'''simple docstring'''
						A	 =      LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'		).to(_SCREAMING_SNAKE_CASE		)
						pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

						A	 =      self.get_inputs(_SCREAMING_SNAKE_CASE		)
						A	 =      pipe(**_SCREAMING_SNAKE_CASE		).images
						A	 =      image[0, -3:, -3:, -1].flatten()

						assert image.shape == (1, 2_5_6, 2_5_6, 3)
						A	 =      np.array([0.51825, 0.52850, 0.52543, 0.54258, 0.52304, 0.52569, 0.54363, 0.55276, 0.56878]		)
						A	 =      np.abs(expected_slice - image_slice		).max()
						assert max_diff < 1E-3


@nightly
@require_torch_gpu
class      UpperCamelCase (				unittest.TestCase ):







		"""simple docstring"""



		def       A(  self		:       Optional[Any]		)    ->   Optional[Any]:


						'''simple docstring'''
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()
		def       A(  self		:       Union[str, Any]						,_SCREAMING_SNAKE_CASE		:       Optional[Any]						,_SCREAMING_SNAKE_CASE		:       List[str]=torch.floataa						,_SCREAMING_SNAKE_CASE		:       List[Any]=0		)    ->   Optional[Any]:


						'''simple docstring'''
						A	 =      torch.manual_seed(_SCREAMING_SNAKE_CASE		)
						A	 =      np.random.RandomState(_SCREAMING_SNAKE_CASE		).standard_normal((1, 4, 3_2, 3_2)		)
						A	 =      torch.from_numpy(_SCREAMING_SNAKE_CASE		).to(device=_SCREAMING_SNAKE_CASE						,dtype=_SCREAMING_SNAKE_CASE		)
						A	 =      {
						    'prompt': 'A painting of a squirrel eating a burger',
						    'latents': latents,
						    'generator': generator,
						    'num_inference_steps': 5_0,
						    'guidance_scale': 6.0,
						    'output_type': 'numpy',
						}
						return inputs







		def       A(  self		:       Any		)    ->   Dict:


						'''simple docstring'''
						A	 =      LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'		).to(_SCREAMING_SNAKE_CASE		)
						pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

						A	 =      self.get_inputs(_SCREAMING_SNAKE_CASE		)
						A	 =      pipe(**_SCREAMING_SNAKE_CASE		).images[0]

						A	 =      load_numpy(
						    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy'		)
						A	 =      np.abs(expected_image - image		).max()
						assert max_diff < 1E-3













from itertools import permutations







def      snake_case		(							UpperCAmelCase	:       tuple   ):
				if num[3] % 2 != 0:
								return False

				if (num[2] + num[3] + num[4]) % 3 != 0:
								return False

				if num[5] % 5 != 0:
								return False

				A	 =      [7, 11, 13, 17]
				for i, test in enumerate(UpperCAmelCase   ):
								if (num[i + 4] * 1_00 + num[i + 5] * 10 + num[i + 6]) % test != 0:
												return False
				return True







def      snake_case		(							UpperCAmelCase	:       int = 10   ):
				return sum(
				    int(''.join(map(UpperCAmelCase,						UpperCAmelCase   )   )   )
				    for num in permutations(range(UpperCAmelCase   )   )
				    if is_substring_divisible(UpperCAmelCase   )   )


if __name__ == "__main__":
					print(f'''{solution() = }''')





