




"""simple docstring"""







import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

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


enable_full_determinism()






class  SCREAMING_SNAKE_CASE__   (   _a  ,			_a  ,			unittest.TestCase   ):
						_a        =  StableDiffusionXLPipeline
						_a        =  TEXT_TO_IMAGE_PARAMS
						_a        =  TEXT_TO_IMAGE_BATCH_PARAMS
						_a        =  TEXT_TO_IMAGE_IMAGE_PARAMS
						_a        =  TEXT_TO_IMAGE_IMAGE_PARAMS


						def 					__lowercase	(							self			:     Tuple   ):
										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""")  ,  attention_head_dim=(2, 4)  ,  use_linear_projection=lowerCAmelCase  ,  addition_embed_type="""text_time"""  ,  addition_time_embed_dim=8  ,  transformer_layers_per_block=(1, 2)  ,  projection_class_embeddings_input_dim=80  ,  cross_attention_dim=64  ,  )
										lowerCAmelCase		=			EulerDiscreteScheduler(
										    beta_start=0.0_0085  ,  beta_end=0.012  ,  steps_offset=1  ,  beta_schedule="""scaled_linear"""  ,  timestep_spacing="""leading"""  ,  )
										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  ,  sample_size=128  ,  )
										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  ,  hidden_act="""gelu"""  ,  projection_dim=32  ,  )
										lowerCAmelCase		=			CLIPTextModel(lowerCAmelCase   )
										lowerCAmelCase		=			CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""  ,  local_files_only=lowerCAmelCase   )

										lowerCAmelCase		=			CLIPTextModelWithProjection(lowerCAmelCase   )
										lowerCAmelCase		=			CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""  ,  local_files_only=lowerCAmelCase   )

										lowerCAmelCase		=			{
										    """unet""": unet,
										    """scheduler""": scheduler,
										    """vae""": vae,
										    """text_encoder""": text_encoder,
										    """tokenizer""": tokenizer,
										    """text_encoder_2""": text_encoder_a,
										    """tokenizer_2""": tokenizer_a,
										    # "safety_checker": None,
										    # "feature_extractor": None,
										}
										return components


						def 					__lowercase	(							self			:     int  ,  lowerCAmelCase			:     List[str]  ,  lowerCAmelCase			:     Union[str, Any]=0   ):
										if str(lowerCAmelCase   ).startswith("""mps"""   ):
														lowerCAmelCase		=			torch.manual_seed(lowerCAmelCase   )
										else:
														lowerCAmelCase		=			torch.Generator(device=lowerCAmelCase   ).manual_seed(lowerCAmelCase   )
										lowerCAmelCase		=			{
										    """prompt""": """A painting of a squirrel eating a burger""",
										    """generator""": generator,
										    """num_inference_steps""": 2,
										    """guidance_scale""": 5.0,
										    """output_type""": """numpy""",
										}
										return inputs


						def 					__lowercase	(							self			:     Dict   ):
										lowerCAmelCase		=			"""cpu"""  # ensure determinism for the device-dependent torch.Generator
										lowerCAmelCase		=			self.get_dummy_components()
										lowerCAmelCase		=			StableDiffusionXLPipeline(**lowerCAmelCase   )
										lowerCAmelCase		=			sd_pipe.to(lowerCAmelCase   )
										sd_pipe.set_progress_bar_config(disable=lowerCAmelCase   )

										lowerCAmelCase		=			self.get_dummy_inputs(lowerCAmelCase   )
										lowerCAmelCase		=			sd_pipe(**lowerCAmelCase   ).images
										lowerCAmelCase		=			image[0, -3:, -3:, -1]

										assert image.shape == (1, 64, 64, 3)
										lowerCAmelCase		=			np.array([0.5873, 0.6128, 0.4797, 0.5122, 0.5674, 0.4639, 0.5227, 0.5149, 0.4747]   )

										assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1e-2


						def 					__lowercase	(							self			:     Optional[Any]   ):
										lowerCAmelCase		=			self.get_dummy_components()
										lowerCAmelCase		=			StableDiffusionXLPipeline(**lowerCAmelCase   )
										lowerCAmelCase		=			sd_pipe.to(lowerCAmelCase   )
										lowerCAmelCase		=			sd_pipe.to(lowerCAmelCase   )
										sd_pipe.set_progress_bar_config(disable=lowerCAmelCase   )

										# forward without prompt embeds
										lowerCAmelCase		=			self.get_dummy_inputs(lowerCAmelCase   )
										lowerCAmelCase		=			3 * ["""this is a negative prompt"""]
										lowerCAmelCase		=			negative_prompt
										lowerCAmelCase		=			3 * [inputs["""prompt"""]]

										lowerCAmelCase		=			sd_pipe(**lowerCAmelCase   )
										lowerCAmelCase		=			output.images[0, -3:, -3:, -1]

										# forward with prompt embeds
										lowerCAmelCase		=			self.get_dummy_inputs(lowerCAmelCase   )
										lowerCAmelCase		=			3 * ["""this is a negative prompt"""]
										lowerCAmelCase		=			3 * [inputs.pop("""prompt"""   )]

										(
										    (
										    lowerCAmelCase
										)	,  (
										    lowerCAmelCase
										)	,  (
										    lowerCAmelCase
										)	,  (
										    lowerCAmelCase
										)	,  
										)		=			sd_pipe.encode_prompt(lowerCAmelCase  ,  negative_prompt=lowerCAmelCase   )

										lowerCAmelCase		=			sd_pipe(
										    **lowerCAmelCase  ,  prompt_embeds=lowerCAmelCase  ,  negative_prompt_embeds=lowerCAmelCase  ,  pooled_prompt_embeds=lowerCAmelCase  ,  negative_pooled_prompt_embeds=lowerCAmelCase  ,  )
										lowerCAmelCase		=			output.images[0, -3:, -3:, -1]

										# make sure that it's equal
										assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()   ).max() < 1e-4


						def 					__lowercase	(							self			:     List[Any]   ):
										super().test_attention_slicing_forward_pass(expected_max_diff=3e-3   )




						def 					__lowercase	(							self			:     int   ):
										super().test_inference_batch_single_identical(expected_max_diff=3e-3   )






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


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


						def 					__lowercase	(							self			:     str  ,  lowerCAmelCase			:     Dict  ,  lowerCAmelCase			:     Union[str, Any]="cpu"  ,  lowerCAmelCase			:     Any=torch.floataa  ,  lowerCAmelCase			:     str=0   ):
										lowerCAmelCase		=			torch.Generator(device=lowerCAmelCase   ).manual_seed(lowerCAmelCase   )
										lowerCAmelCase		=			np.random.RandomState(lowerCAmelCase   ).standard_normal((1, 4, 64, 64)   )
										lowerCAmelCase		=			torch.from_numpy(lowerCAmelCase   ).to(device=lowerCAmelCase  ,  dtype=lowerCAmelCase   )
										lowerCAmelCase		=			{
										    """prompt""": """a photograph of an astronaut riding a horse""",
										    """latents""": latents,
										    """generator""": generator,
										    """num_inference_steps""": 3,
										    """guidance_scale""": 7.5,
										    """output_type""": """numpy""",
										}
										return inputs




						def 					__lowercase	(							self			:     List[Any]   ):
										lowerCAmelCase		=			DiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-base"""   )
										pipe.to(lowerCAmelCase   )
										pipe.set_progress_bar_config(disable=lowerCAmelCase   )

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

										assert image.shape == (1, 512, 512, 3)
										lowerCAmelCase		=			np.array([0.4_9493, 0.4_7896, 0.4_0798, 0.5_4214, 0.5_3212, 0.4_8202, 0.4_7656, 0.4_6329, 0.4_8506]   )
										assert np.abs(image_slice - expected_slice   ).max() < 7e-3











"""simple docstring"""







import numpy as np
import requests





def 							lowercase   ()       ->					Optional[int]:


				'''simple docstring'''






				lowerCAmelCase		=			requests.get(
				    """https://raw.githubusercontent.com/yashLadha/The_Math_of_Intelligence/"""
				    """master/Week1/ADRvsRating.csv"""	)
				lowerCAmelCase		=			response.text.splitlines()
				lowerCAmelCase		=			[]
				for item in lines:
								lowerCAmelCase		=			item.split(""","""	)
								data.append(snake_case__	)
				data.pop(0	)  # This is for removing the labels from the list
				lowerCAmelCase		=			np.matrix(snake_case__	)
				return dataset





def 							lowercase   (snake_case__				:	List[str] ,    snake_case__				:	int ,    snake_case__				:	List[str] ,    snake_case__				:	Tuple ,    snake_case__				:	Union[str, Any]	)       ->					str:


				'''simple docstring'''






				lowerCAmelCase		=			len_data

				lowerCAmelCase		=			np.dot(snake_case__ ,    data_x.transpose()	)
				prod -= data_y.transpose()
				lowerCAmelCase		=			np.dot(snake_case__ ,    snake_case__	)
				lowerCAmelCase		=			theta - (alpha / n) * sum_grad
				return theta





def 							lowercase   (snake_case__				:	Any ,    snake_case__				:	int ,    snake_case__				:	Any ,    snake_case__				:	Dict	)       ->					List[str]:


				'''simple docstring'''






				lowerCAmelCase		=			np.dot(snake_case__ ,    data_x.transpose()	)
				prod -= data_y.transpose()
				lowerCAmelCase		=			np.sum(np.square(snake_case__	)	)
				lowerCAmelCase		=			sum_elem / (2 * len_data)
				return error





def 							lowercase   (snake_case__				:	Optional[Any] ,    snake_case__				:	List[Any]	)       ->					str:


				'''simple docstring'''






				lowerCAmelCase		=			100_000
				lowerCAmelCase		=			0.0_001_550

				lowerCAmelCase		=			data_x.shape[1]
				lowerCAmelCase		=			data_x.shape[0] - 1

				lowerCAmelCase		=			np.zeros((1, no_features)	)

				for i in range(0 ,    snake_case__	):
								lowerCAmelCase		=			run_steep_gradient_descent(snake_case__ ,    snake_case__ ,    snake_case__ ,    snake_case__ ,    snake_case__	)
								lowerCAmelCase		=			sum_of_square_error(snake_case__ ,    snake_case__ ,    snake_case__ ,    snake_case__	)
								print(f'''At Iteration {i + 1} - Error is {error:.5f}'''	)

				return theta





def 							lowercase   (snake_case__				:	List[str] ,    snake_case__				:	Optional[int]	)       ->					List[str]:


				'''simple docstring'''






				lowerCAmelCase		=			sum(abs(y - predicted_y[i]	) for i, y in enumerate(snake_case__	)	)
				return total / len(snake_case__	)





def 							lowercase   ()       ->					Optional[int]:


				'''simple docstring'''






				lowerCAmelCase		=			collect_dataset()

				lowerCAmelCase		=			data.shape[0]
				lowerCAmelCase		=			np.c_[np.ones(snake_case__	), data[:, :-1]].astype(snake_case__	)
				lowerCAmelCase		=			data[:, -1].astype(snake_case__	)

				lowerCAmelCase		=			run_linear_regression(snake_case__ ,    snake_case__	)
				lowerCAmelCase		=			theta.shape[1]
				print("""Resultant Feature vector : """	)
				for i in range(0 ,    snake_case__	):
								print(f'''{theta[0, i]:.5f}'''	)


if __name__ == "__main__":
		main()





