





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       __A     (						a    ,					unittest.TestCase    ):
				__A      =				LDMTextToImagePipeline
				__A      =				TEXT_TO_IMAGE_PARAMS - {
				    """negative_prompt""",
				    """negative_prompt_embeds""",
				    """cross_attention_kwargs""",
				    """prompt_embeds""",
				}
				__A      =				PipelineTesterMixin.required_optional_params - {
				    """num_images_per_prompt""",
				    """callback""",
				    """callback_steps""",
				}
				__A      =				TEXT_TO_IMAGE_BATCH_PARAMS
				__A      =				False


				def _snake_case       (		self							):
								torch.manual_seed(0							)
								lowerCamelCase	 =UNetaDConditionModel(
								    block_out_channels=(32, 64)   , layers_per_block=2   , sample_size=32   , in_channels=4   , out_channels=4   , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")   , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")   , cross_attention_dim=32   , )
								lowerCamelCase	 =DDIMScheduler(
								    beta_start=0.0_0_0_8_5   , beta_end=0.0_1_2   , beta_schedule="""scaled_linear"""   , clip_sample=UpperCAmelCase_   , set_alpha_to_one=UpperCAmelCase_   , )
								torch.manual_seed(0							)
								lowerCamelCase	 =AutoencoderKL(
								    block_out_channels=(32, 64)   , in_channels=3   , out_channels=3   , down_block_types=("""DownEncoderBlock2D""", """DownEncoderBlock2D""")   , up_block_types=("""UpDecoderBlock2D""", """UpDecoderBlock2D""")   , latent_channels=4   , )
								torch.manual_seed(0							)
								lowerCamelCase	 =CLIPTextConfig(
								    bos_token_id=0   , eos_token_id=2   , hidden_size=32   , intermediate_size=37   , layer_norm_eps=1E-05   , num_attention_heads=4   , num_hidden_layers=5   , pad_token_id=1   , vocab_size=1000   , )
								lowerCamelCase	 =CLIPTextModel(UpperCAmelCase_							)
								lowerCamelCase	 =CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""							)

								lowerCamelCase	 ={
								    """unet""": unet,
								    """scheduler""": scheduler,
								    """vqvae""": vae,
								    """bert""": text_encoder,
								    """tokenizer""": tokenizer,
								}
								return components


				def _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_=0							):
								if str(UpperCAmelCase_							).startswith("""mps"""							):
												lowerCamelCase	 =torch.manual_seed(UpperCAmelCase_							)
								else:
												lowerCamelCase	 =torch.Generator(device=UpperCAmelCase_							).manual_seed(UpperCAmelCase_							)
								lowerCamelCase	 ={
								    """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 _snake_case       (		self							):
								lowerCamelCase	 ="""cpu"""  # ensure determinism for the device-dependent torch.Generator

								lowerCamelCase	 =self.get_dummy_components()
								lowerCamelCase	 =LDMTextToImagePipeline(**UpperCAmelCase_							)
								pipe.to(UpperCAmelCase_							)
								pipe.set_progress_bar_config(disable=UpperCAmelCase_							)

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

								assert image.shape == (1, 16, 16, 3)
								lowerCamelCase	 =np.array([0.6_1_0_1, 0.6_1_5_6, 0.5_6_2_2, 0.4_8_9_5, 0.6_6_6_1, 0.3_8_0_4, 0.5_7_4_8, 0.6_1_3_6, 0.5_0_1_4]							)

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




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


				def _snake_case       (		self							):
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()


				def _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_=torch.floataa   , UpperCAmelCase_=0							):
								lowerCamelCase	 =torch.manual_seed(UpperCAmelCase_							)
								lowerCamelCase	 =np.random.RandomState(UpperCAmelCase_							).standard_normal((1, 4, 32, 32)							)
								lowerCamelCase	 =torch.from_numpy(UpperCAmelCase_							).to(device=UpperCAmelCase_   , dtype=UpperCAmelCase_							)
								lowerCamelCase	 ={
								    """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 _snake_case       (		self							):
								lowerCamelCase	 =LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256"""							).to(UpperCAmelCase_							)
								pipe.set_progress_bar_config(disable=UpperCAmelCase_							)

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

								assert image.shape == (1, 256, 256, 3)
								lowerCamelCase	 =np.array([0.5_1_8_2_5, 0.5_2_8_5_0, 0.5_2_5_4_3, 0.5_4_2_5_8, 0.5_2_3_0_4, 0.5_2_5_6_9, 0.5_4_3_6_3, 0.5_5_2_7_6, 0.5_6_8_7_8]							)
								lowerCamelCase	 =np.abs(expected_slice - image_slice							).max()
								assert max_diff < 1E-3




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


				def _snake_case       (		self							):
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()


				def _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_=torch.floataa   , UpperCAmelCase_=0							):
								lowerCamelCase	 =torch.manual_seed(UpperCAmelCase_							)
								lowerCamelCase	 =np.random.RandomState(UpperCAmelCase_							).standard_normal((1, 4, 32, 32)							)
								lowerCamelCase	 =torch.from_numpy(UpperCAmelCase_							).to(device=UpperCAmelCase_   , dtype=UpperCAmelCase_							)
								lowerCamelCase	 ={
								    """prompt""": """A painting of a squirrel eating a burger""",
								    """latents""": latents,
								    """generator""": generator,
								    """num_inference_steps""": 50,
								    """guidance_scale""": 6.0,
								    """output_type""": """numpy""",
								}
								return inputs


				def _snake_case       (		self							):
								lowerCamelCase	 =LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256"""							).to(UpperCAmelCase_							)
								pipe.set_progress_bar_config(disable=UpperCAmelCase_							)

								lowerCamelCase	 =self.get_inputs(UpperCAmelCase_							)
								lowerCamelCase	 =pipe(**UpperCAmelCase_							).images[0]

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












import math
from collections.abc import Iterator
from itertools import takewhile





def      _lowercase					(						_UpperCAmelCase				)						->  bool:

				if 1 < number < 4:
								# 2 and 3 are primes
								return True
				elif number < 2 or number % 2 == 0 or number % 3 == 0:
								# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
								return False

				# All primes number are in format of 6k +/- 1
				for i in range(5			,      int(math.sqrt(_UpperCAmelCase				) + 1				)			,      6				):
								if number % i == 0 or number % (i + 2) == 0:
												return False
				return True





def      _lowercase					(						)						->  Iterator[int]:
				lowerCamelCase	 =2
				while True:
								if is_prime(_UpperCAmelCase				):
												yield num
								num += 1





def      _lowercase					(						_UpperCAmelCase = 2_00_00_00				)						->  int:
				return sum(takewhile(lambda _UpperCAmelCase				: x < n			,      prime_generator()				)				)


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





