




"""simple docstring"""

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 				lowercase_       (     __lowerCAmelCase			,	unittest.TestCase							):



 '''simple docstring'''
 UpperCAmelCase    :     Dict        =	LDMTextToImagePipeline
 UpperCAmelCase    :     str        =	TEXT_TO_IMAGE_PARAMS - {
     "negative_prompt",
     "negative_prompt_embeds",
     "cross_attention_kwargs",
     "prompt_embeds",
 }
 UpperCAmelCase    :     Optional[Any]        =	PipelineTesterMixin.required_optional_params - {
     "num_images_per_prompt",
     "callback",
     "callback_steps",
 }
 UpperCAmelCase    :     Union[str, Any]        =	TEXT_TO_IMAGE_BATCH_PARAMS
 UpperCAmelCase    :     List[str]        =	False

 def 			lowerCAmelCase_							( self       :       str		):
       torch.manual_seed(0		)
       _A					   =   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    ,							)
       _A					   =   DDIMScheduler(
           beta_start=0.0_0085    ,							beta_end=0.012    ,							beta_schedule='scaled_linear'    ,							clip_sample=UpperCAmelCase__    ,							set_alpha_to_one=UpperCAmelCase__    ,							)
       torch.manual_seed(0		)
       _A					   =   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		)
       _A					   =   CLIPTextConfig(
           bos_token_id=0    ,							eos_token_id=2    ,							hidden_size=32    ,							intermediate_size=37    ,							layer_norm_eps=1E-0_5    ,							num_attention_heads=4    ,							num_hidden_layers=5    ,							pad_token_id=1    ,							vocab_size=1_000    ,							)
       _A					   =   CLIPTextModel(UpperCAmelCase__		)
       _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 			lowerCAmelCase_							( self       :       Union[str, Any]    ,							_UpperCAmelCase       :       Dict    ,							_UpperCAmelCase       :       Union[str, Any]=0		):
       if str(UpperCAmelCase__		).startswith('mps'		):
             _A					   =   torch.manual_seed(UpperCAmelCase__		)
       else:
             _A					   =   torch.Generator(device=UpperCAmelCase__		).manual_seed(UpperCAmelCase__		)
       _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 			lowerCAmelCase_							( self       :       int		):
       _A					   =   'cpu'  # ensure determinism for the device-dependent torch.Generator

       _A					   =   self.get_dummy_components()
       _A					   =   LDMTextToImagePipeline(**UpperCAmelCase__		)
       pipe.to(UpperCAmelCase__		)
       pipe.set_progress_bar_config(disable=UpperCAmelCase__		)

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

       assert image.shape == (1, 16, 16, 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 				lowercase_       (     unittest.TestCase							):



 '''simple docstring'''

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

 def 			lowerCAmelCase_							( self       :       int    ,							_UpperCAmelCase       :       Optional[int]    ,							_UpperCAmelCase       :       Dict=torch.floataa    ,							_UpperCAmelCase       :       Dict=0		):
       _A					   =   torch.manual_seed(UpperCAmelCase__		)
       _A					   =   np.random.RandomState(UpperCAmelCase__		).standard_normal((1, 4, 32, 32)		)
       _A					   =   torch.from_numpy(UpperCAmelCase__		).to(device=UpperCAmelCase__    ,							dtype=UpperCAmelCase__		)
       _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 			lowerCAmelCase_							( self       :       Tuple		):
       _A					   =   LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'		).to(UpperCAmelCase__		)
       pipe.set_progress_bar_config(disable=UpperCAmelCase__		)

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

       assert image.shape == (1, 256, 256, 3)
       _A					   =   np.array([0.5_1825, 0.5_2850, 0.5_2543, 0.5_4258, 0.5_2304, 0.5_2569, 0.5_4363, 0.5_5276, 0.5_6878]		)
       _A					   =   np.abs(expected_slice - image_slice		).max()
       assert max_diff < 1E-3





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



 '''simple docstring'''

 def 			lowerCAmelCase_							( self       :       List[str]		):
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()

 def 			lowerCAmelCase_							( self       :       Union[str, Any]    ,							_UpperCAmelCase       :       Optional[Any]    ,							_UpperCAmelCase       :       List[str]=torch.floataa    ,							_UpperCAmelCase       :       List[str]=0		):
       _A					   =   torch.manual_seed(UpperCAmelCase__		)
       _A					   =   np.random.RandomState(UpperCAmelCase__		).standard_normal((1, 4, 32, 32)		)
       _A					   =   torch.from_numpy(UpperCAmelCase__		).to(device=UpperCAmelCase__    ,							dtype=UpperCAmelCase__		)
       _A					   =   {
           '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 			lowerCAmelCase_							( self       :       int		):
       _A					   =   LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'		).to(UpperCAmelCase__		)
       pipe.set_progress_bar_config(disable=UpperCAmelCase__		)

       _A					   =   self.get_inputs(UpperCAmelCase__		)
       _A					   =   pipe(**UpperCAmelCase__		).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




"""simple docstring"""





from collections.abc import Sequence




def   _A  (  UpperCamelCase_     :       Sequence[float],			UpperCamelCase_     :       float)	->						float:
   '''simple docstring'''


   return sum(c * (x**i) for i, c in enumerate(UpperCamelCase_))




def   _A  (  UpperCamelCase_     :       Sequence[float],			UpperCamelCase_     :       float)	->						float:
   '''simple docstring'''


   __lowercase								=						0.0
   for coeff in reversed(UpperCamelCase_):
      __lowercase								=						result * x + coeff
   return result


if __name__ == "__main__":
   _a       =							(0.0, 0.0, 5.0, 9.3, 7.0)
   _a       =							10.0
   print(evaluate_poly(poly, x))
   print(horner(poly, x))




