

'''simple docstring'''




import gc
import os
import tempfile
import unittest

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, StableDiffusionPipeline, UNetaDConditionModel
from diffusers.loaders import AttnProcsLayers, LoraLoaderMixin
from diffusers.models.attention_processor import (
    Attention,
    AttnProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRAAttnProcessor2_0,
    LoRAXFormersAttnProcessor,
    XFormersAttnProcessor,
)
from diffusers.utils import TEXT_ENCODER_ATTN_MODULE, floats_tensor, torch_device


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   nn.Module						):
  UpperCAmelCase													=	{}
  for name in unet.attn_processors.keys():
    UpperCAmelCase													=	None if name.endswith('attn1.processor'						) else unet.config.cross_attention_dim
    if name.startswith('mid_block'						):
      UpperCAmelCase													=	unet.config.block_out_channels[-1]
    elif name.startswith('up_blocks'						):
      UpperCAmelCase													=	int(name[len('up_blocks.'						)]						)
      UpperCAmelCase													=	list(reversed(unet.config.block_out_channels						)						)[block_id]
    elif name.startswith('down_blocks'						):
      UpperCAmelCase													=	int(name[len('down_blocks.'						)]						)
      UpperCAmelCase													=	unet.config.block_out_channels[block_id]
    UpperCAmelCase													=	(
        LoRAAttnProcessor2_0 if hasattr(SCREAMING_SNAKE_CASE       ,   'scaled_dot_product_attention'						) else LoRAAttnProcessor
    )
    UpperCAmelCase													=	lora_attn_processor_class(
        hidden_size=SCREAMING_SNAKE_CASE       ,   cross_attention_dim=SCREAMING_SNAKE_CASE						)
  UpperCAmelCase													=	AttnProcsLayers(SCREAMING_SNAKE_CASE						)
  return lora_attn_procs, unet_lora_layers


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   nn.Module						):
  UpperCAmelCase													=	{}
  UpperCAmelCase													=	(
      LoRAAttnProcessor2_0 if hasattr(SCREAMING_SNAKE_CASE       ,   'scaled_dot_product_attention'						) else LoRAAttnProcessor
  )
  for name, module in text_encoder.named_modules():
    if name.endswith(SCREAMING_SNAKE_CASE						):
      UpperCAmelCase													=	lora_attn_processor_class(
          hidden_size=module.out_proj.out_features       ,   cross_attention_dim=SCREAMING_SNAKE_CASE						)
  return text_lora_attn_procs


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   nn.Module						):
  UpperCAmelCase													=	create_text_encoder_lora_attn_procs(SCREAMING_SNAKE_CASE						)
  UpperCAmelCase													=	AttnProcsLayers(SCREAMING_SNAKE_CASE						)
  return text_encoder_lora_layers


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   List[str]       ,   SCREAMING_SNAKE_CASE      :   Union[str, Any]=False						):
  for _, attn_proc in text_lora_attn_procs.items():
    # set up.weights
    for layer_name, layer_module in attn_proc.named_modules():
      if layer_name.endswith('_lora'						):
        UpperCAmelCase													=	(
            torch.randn_like(layer_module.up.weight						)
            if randn_weight
            else torch.zeros_like(layer_module.up.weight						)
        )
        UpperCAmelCase													=	torch.nn.Parameter(SCREAMING_SNAKE_CASE						)



class        lowercase_	(		unittest.TestCase ):

  '''simple docstring'''





  def 						snake_case_			(     self     )							->				Dict:


    """simple docstring"""
    torch.manual_seed(0     )
    UpperCAmelCase													=	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   , )
    UpperCAmelCase													=	DDIMScheduler(
        beta_start=0.0_0085   , beta_end=0.012   , beta_schedule='scaled_linear'   , clip_sample=a_   , set_alpha_to_one=a_   , steps_offset=1   , )
    torch.manual_seed(0     )
    UpperCAmelCase													=	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   , )
    UpperCAmelCase													=	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   , )
    UpperCAmelCase													=	CLIPTextModel(a_     )
    UpperCAmelCase													=	CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'     )

    UpperCAmelCase							,      UpperCAmelCase													=	create_unet_lora_layers(a_     )
    UpperCAmelCase													=	create_text_encoder_lora_layers(a_     )

    UpperCAmelCase													=	{
        'unet': unet,
        'scheduler': scheduler,
        'vae': vae,
        'text_encoder': text_encoder,
        'tokenizer': tokenizer,
        'safety_checker': None,
        'feature_extractor': None,
    }
    UpperCAmelCase													=	{
        'unet_lora_layers': unet_lora_layers,
        'text_encoder_lora_layers': text_encoder_lora_layers,
        'unet_lora_attn_procs': unet_lora_attn_procs,
    }
    return pipeline_components, lora_components
  def 						snake_case_			(     self     )							->				Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase													=	1
    UpperCAmelCase													=	1_0
    UpperCAmelCase													=	4
    UpperCAmelCase													=	(3_2, 3_2)

    UpperCAmelCase													=	torch.manual_seed(0     )
    UpperCAmelCase													=	floats_tensor((batch_size, num_channels) + sizes     )
    UpperCAmelCase													=	torch.randint(1   , a_   , size=(batch_size, sequence_length)   , generator=a_     )

    UpperCAmelCase													=	{
        'prompt': 'A painting of a squirrel eating a burger',
        'generator': generator,
        'num_inference_steps': 2,
        'guidance_scale': 6.0,
        'output_type': 'numpy',
    }

    return noise, input_ids, pipeline_inputs

    # copied from: https://colab.research.google.com/gist/sayakpaul/df2ef6e1ae6d8c10a49d859883b10860/scratchpad.ipynb
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    UpperCAmelCase													=	7_7

    UpperCAmelCase													=	torch.randint(2   , 5_6   , size=(1, max_seq_length)   , generator=torch.manual_seed(0     )     )

    UpperCAmelCase													=	{}
    UpperCAmelCase													=	inputs
    return prepared_inputs
  def 						snake_case_			(     self   , a_     )							->				int:


    """simple docstring"""
    UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_components()
    LoraLoaderMixin.save_lora_weights(
        save_directory=a_   , unet_lora_layers=lora_components['unet_lora_layers']   , text_encoder_lora_layers=lora_components['text_encoder_lora_layers']   , )
    self.assertTrue(os.path.isfile(os.path.join(a_   , 'pytorch_lora_weights.bin'     )     )     )
  def 						snake_case_			(     self     )							->				Optional[int]:


    """simple docstring"""
    UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_components()
    UpperCAmelCase													=	StableDiffusionPipeline(**a_     )
    UpperCAmelCase													=	sd_pipe.to(a_     )
    sd_pipe.set_progress_bar_config(disable=a_     )

    UpperCAmelCase							,      UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_inputs()

    UpperCAmelCase													=	sd_pipe(**a_     ).images
    UpperCAmelCase													=	original_images[0, -3:, -3:, -1]

    with tempfile.TemporaryDirectory() as tmpdirname:
      LoraLoaderMixin.save_lora_weights(
          save_directory=a_   , unet_lora_layers=lora_components['unet_lora_layers']   , text_encoder_lora_layers=lora_components['text_encoder_lora_layers']   , )
      self.assertTrue(os.path.isfile(os.path.join(a_   , 'pytorch_lora_weights.bin'     )     )     )
      sd_pipe.load_lora_weights(a_     )

    UpperCAmelCase													=	sd_pipe(**a_     ).images
    UpperCAmelCase													=	lora_images[0, -3:, -3:, -1]

    # Outputs shouldn't match.
    self.assertFalse(torch.allclose(torch.from_numpy(a_     )   , torch.from_numpy(a_     )     )     )
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_components()
    UpperCAmelCase													=	StableDiffusionPipeline(**a_     )
    UpperCAmelCase													=	sd_pipe.to(a_     )
    sd_pipe.set_progress_bar_config(disable=a_     )

    UpperCAmelCase							,      UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_inputs()

    UpperCAmelCase													=	sd_pipe(**a_     ).images
    UpperCAmelCase													=	original_images[0, -3:, -3:, -1]

    with tempfile.TemporaryDirectory() as tmpdirname:
      LoraLoaderMixin.save_lora_weights(
          save_directory=a_   , unet_lora_layers=lora_components['unet_lora_layers']   , text_encoder_lora_layers=lora_components['text_encoder_lora_layers']   , safe_serialization=a_   , )
      self.assertTrue(os.path.isfile(os.path.join(a_   , 'pytorch_lora_weights.safetensors'     )     )     )
      sd_pipe.load_lora_weights(a_     )

    UpperCAmelCase													=	sd_pipe(**a_     ).images
    UpperCAmelCase													=	lora_images[0, -3:, -3:, -1]

    # Outputs shouldn't match.
    self.assertFalse(torch.allclose(torch.from_numpy(a_     )   , torch.from_numpy(a_     )     )     )
  def 						snake_case_			(     self     )							->				Optional[Any]:


    """simple docstring"""
    UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_components()
    UpperCAmelCase													=	lora_components['unet_lora_attn_procs']
    UpperCAmelCase													=	StableDiffusionPipeline(**a_     )
    UpperCAmelCase													=	sd_pipe.to(a_     )
    sd_pipe.set_progress_bar_config(disable=a_     )

    UpperCAmelCase							,      UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_inputs()

    UpperCAmelCase													=	sd_pipe(**a_     ).images
    UpperCAmelCase													=	original_images[0, -3:, -3:, -1]

    with tempfile.TemporaryDirectory() as tmpdirname:
      UpperCAmelCase													=	sd_pipe.unet
      unet.set_attn_processor(a_     )
      unet.save_attn_procs(a_     )
      self.assertTrue(os.path.isfile(os.path.join(a_   , 'pytorch_lora_weights.bin'     )     )     )
      sd_pipe.load_lora_weights(a_     )

    UpperCAmelCase													=	sd_pipe(**a_     ).images
    UpperCAmelCase													=	lora_images[0, -3:, -3:, -1]

    # Outputs shouldn't match.
    self.assertFalse(torch.allclose(torch.from_numpy(a_     )   , torch.from_numpy(a_     )     )     )
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_components()
    UpperCAmelCase													=	StableDiffusionPipeline(**a_     )

    UpperCAmelCase													=	self.get_dummy_tokens()

    # inference without lora
    UpperCAmelCase													=	pipe.text_encoder(**a_     )[0]
    assert outputs_without_lora.shape == (1, 7_7, 3_2)

    # create lora_attn_procs with zeroed out up.weights
    UpperCAmelCase													=	create_text_encoder_lora_attn_procs(pipe.text_encoder     )
    set_lora_up_weights(a_   , randn_weight=a_     )

    # monkey patch
    pipe._modify_text_encoder(a_     )

    # verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
    del text_attn_procs
    gc.collect()

    # inference with lora
    UpperCAmelCase													=	pipe.text_encoder(**a_     )[0]
    assert outputs_with_lora.shape == (1, 7_7, 3_2)

    assert torch.allclose(
        a_   , a_     ), "lora_up_weight are all zero, so the lora outputs should be the same to without lora outputs"

    # create lora_attn_procs with randn up.weights
    UpperCAmelCase													=	create_text_encoder_lora_attn_procs(pipe.text_encoder     )
    set_lora_up_weights(a_   , randn_weight=a_     )

    # monkey patch
    pipe._modify_text_encoder(a_     )

    # verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
    del text_attn_procs
    gc.collect()

    # inference with lora
    UpperCAmelCase													=	pipe.text_encoder(**a_     )[0]
    assert outputs_with_lora.shape == (1, 7_7, 3_2)

    assert not torch.allclose(
        a_   , a_     ), "lora_up_weight are not zero, so the lora outputs should be different to without lora outputs"
  def 						snake_case_			(     self     )							->				int:


    """simple docstring"""
    UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_components()
    UpperCAmelCase													=	StableDiffusionPipeline(**a_     )

    UpperCAmelCase													=	self.get_dummy_tokens()

    # inference without lora
    UpperCAmelCase													=	pipe.text_encoder(**a_     )[0]
    assert outputs_without_lora.shape == (1, 7_7, 3_2)

    # create lora_attn_procs with randn up.weights
    UpperCAmelCase													=	create_text_encoder_lora_attn_procs(pipe.text_encoder     )
    set_lora_up_weights(a_   , randn_weight=a_     )

    # monkey patch
    pipe._modify_text_encoder(a_     )

    # verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
    del text_attn_procs
    gc.collect()

    # inference with lora
    UpperCAmelCase													=	pipe.text_encoder(**a_     )[0]
    assert outputs_with_lora.shape == (1, 7_7, 3_2)

    assert not torch.allclose(
        a_   , a_     ), "lora outputs should be different to without lora outputs"

    # remove monkey patch
    pipe._remove_text_encoder_monkey_patch()

    # inference with removed lora
    UpperCAmelCase													=	pipe.text_encoder(**a_     )[0]
    assert outputs_without_lora_removed.shape == (1, 7_7, 3_2)

    assert torch.allclose(
        a_   , a_     ), "remove lora monkey patch should restore the original outputs"
  def 						snake_case_			(     self     )							->				Tuple:


    """simple docstring"""
    UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_components()
    UpperCAmelCase													=	StableDiffusionPipeline(**a_     )
    UpperCAmelCase													=	sd_pipe.to(a_     )
    sd_pipe.set_progress_bar_config(disable=a_     )

    UpperCAmelCase							,      UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_inputs()

    with tempfile.TemporaryDirectory() as tmpdirname:
      LoraLoaderMixin.save_lora_weights(
          save_directory=a_   , unet_lora_layers=lora_components['unet_lora_layers']   , text_encoder_lora_layers=lora_components['text_encoder_lora_layers']   , )
      self.assertTrue(os.path.isfile(os.path.join(a_   , 'pytorch_lora_weights.bin'     )     )     )
      sd_pipe.load_lora_weights(a_     )

    UpperCAmelCase													=	sd_pipe(**a_     ).images
    UpperCAmelCase													=	lora_images[0, -3:, -3:, -1]

    UpperCAmelCase													=	sd_pipe(**a_   , cross_attention_kwargs={'scale': 0.5}     ).images
    UpperCAmelCase													=	lora_images_with_scale[0, -3:, -3:, -1]

    # Outputs shouldn't match.
    self.assertFalse(
        torch.allclose(torch.from_numpy(a_     )   , torch.from_numpy(a_     )     )     )
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    with tempfile.TemporaryDirectory() as tmpdirname:
      self.create_lora_weight_file(a_     )

      UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_components()
      UpperCAmelCase													=	StableDiffusionPipeline(**a_     )
      UpperCAmelCase													=	sd_pipe.to(a_     )
      sd_pipe.set_progress_bar_config(disable=a_     )

      # check if vanilla attention processors are used
      for _, module in sd_pipe.unet.named_modules():
        if isinstance(a_   , a_     ):
          self.assertIsInstance(module.processor   , (AttnProcessor, AttnProcessor2_0)     )

            # load LoRA weight file
      sd_pipe.load_lora_weights(a_     )

      # check if lora attention processors are used
      for _, module in sd_pipe.unet.named_modules():
        if isinstance(a_   , a_     ):
          UpperCAmelCase													=	(
              LoRAAttnProcessor2_0 if hasattr(a_   , 'scaled_dot_product_attention'     ) else LoRAAttnProcessor
          )
          self.assertIsInstance(module.processor   , a_     )
  @unittest.skipIf(torch_device != 'cuda'   , 'This test is supposed to run on GPU'     )
  def 						snake_case_			(     self     )							->				Optional[int]:


    """simple docstring"""
    with tempfile.TemporaryDirectory() as tmpdirname:
      self.create_lora_weight_file(a_     )

      UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_components()
      UpperCAmelCase													=	StableDiffusionPipeline(**a_     )
      UpperCAmelCase													=	sd_pipe.to(a_     )
      sd_pipe.set_progress_bar_config(disable=a_     )

      # enable XFormers
      sd_pipe.enable_xformers_memory_efficient_attention()

      # check if xFormers attention processors are used
      for _, module in sd_pipe.unet.named_modules():
        if isinstance(a_   , a_     ):
          self.assertIsInstance(module.processor   , a_     )

            # load LoRA weight file
      sd_pipe.load_lora_weights(a_     )

      # check if lora attention processors are used
      for _, module in sd_pipe.unet.named_modules():
        if isinstance(a_   , a_     ):
          self.assertIsInstance(module.processor   , a_     )





  @unittest.skipIf(torch_device != 'cuda'   , 'This test is supposed to run on GPU'     )
  def 						snake_case_			(     self     )							->				Dict:


    """simple docstring"""
    UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_components()
    UpperCAmelCase													=	StableDiffusionPipeline(**a_     )
    UpperCAmelCase													=	sd_pipe.to(a_     )
    sd_pipe.set_progress_bar_config(disable=a_     )

    UpperCAmelCase							,      UpperCAmelCase							,      UpperCAmelCase													=	self.get_dummy_inputs()

    # enable XFormers
    sd_pipe.enable_xformers_memory_efficient_attention()

    UpperCAmelCase													=	sd_pipe(**a_     ).images
    UpperCAmelCase													=	original_images[0, -3:, -3:, -1]

    with tempfile.TemporaryDirectory() as tmpdirname:
      LoraLoaderMixin.save_lora_weights(
          save_directory=a_   , unet_lora_layers=lora_components['unet_lora_layers']   , text_encoder_lora_layers=lora_components['text_encoder_lora_layers']   , )
      self.assertTrue(os.path.isfile(os.path.join(a_   , 'pytorch_lora_weights.bin'     )     )     )
      sd_pipe.load_lora_weights(a_     )

    UpperCAmelCase													=	sd_pipe(**a_     ).images
    UpperCAmelCase													=	lora_images[0, -3:, -3:, -1]

    # Outputs shouldn't match.
    self.assertFalse(torch.allclose(torch.from_numpy(a_     )   , torch.from_numpy(a_     )     )     )





'''simple docstring'''




def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   list[list[int]]						):
  if len(SCREAMING_SNAKE_CASE						) == 0 or not all(len(SCREAMING_SNAKE_CASE						) == 2 for pair in coordinates						):
    raise ValueError('The program cannot work out a fitting polynomial.'						)

  if len({tuple(SCREAMING_SNAKE_CASE						) for pair in coordinates}						) != len(SCREAMING_SNAKE_CASE						):
    raise ValueError('The program cannot work out a fitting polynomial.'						)

  UpperCAmelCase													=	{x for x, _ in coordinates}
  if len(SCREAMING_SNAKE_CASE						) == 1:
    return f'''x={coordinates[0][0]}'''

  if len(SCREAMING_SNAKE_CASE						) != len(SCREAMING_SNAKE_CASE						):
    raise ValueError('The program cannot work out a fitting polynomial.'						)

  UpperCAmelCase													=	len(SCREAMING_SNAKE_CASE						)

  UpperCAmelCase													=	0
  UpperCAmelCase													=	[]
  # put the x and x to the power values in a matrix
  while count_of_line < x:
    UpperCAmelCase													=	0
    UpperCAmelCase													=	coordinates[count_of_line][0]
    UpperCAmelCase													=	[]
    while count_in_line < x:
      count_line.append(a ** (x - (count_in_line + 1))						)
      count_in_line += 1
    matrix.append(SCREAMING_SNAKE_CASE						)
    count_of_line += 1

  UpperCAmelCase													=	0
  # put the y values into a vector
  UpperCAmelCase													=	[]
  while count_of_line < x:
    vector.append(coordinates[count_of_line][1]						)
    count_of_line += 1

  UpperCAmelCase													=	0

  while count < x:
    UpperCAmelCase													=	0
    while zahlen < x:
      if count == zahlen:
        zahlen += 1
      if zahlen == x:
        break
      UpperCAmelCase													=	matrix[zahlen][count] / matrix[count][count]
      for counting_columns, item in enumerate(matrix[count]						):
        # manipulating all the values in the matrix
        matrix[zahlen][counting_columns] -= item * bruch
      # manipulating the values in the vector
      vector[zahlen] -= vector[count] * bruch
      zahlen += 1
    count += 1

  UpperCAmelCase													=	0
  # make solutions
  UpperCAmelCase													=	[]
  while count < x:
    solution.append(str(vector[count] / matrix[count][count]						)						)
    count += 1

  UpperCAmelCase													=	0
  UpperCAmelCase													=	'f(x)='

  while count < x:
    UpperCAmelCase													=	solution[count].split('E'						)
    if len(SCREAMING_SNAKE_CASE						) > 1:
      UpperCAmelCase													=	f'''{remove_e[0]}*10^{remove_e[1]}'''
    solved += f'''x^{x - (count + 1)}*{solution[count]}'''
    if count + 1 != x:
      solved += "+"
    count += 1

  return solved


if __name__ == "__main__":
  print(points_to_polynomial([]))
  print(points_to_polynomial([[]]))
  print(points_to_polynomial([[1, 0], [2, 0], [3, 0]]))
  print(points_to_polynomial([[1, 1], [2, 1], [3, 1]]))
  print(points_to_polynomial([[1, 3], [2, 3], [3, 3]]))
  print(points_to_polynomial([[1, 1], [2, 2], [3, 3]]))
  print(points_to_polynomial([[1, 1], [2, 4], [3, 9]]))
  print(points_to_polynomial([[1, 3], [2, 6], [3, 11]]))
  print(points_to_polynomial([[1, -3], [2, -6], [3, -11]]))
  print(points_to_polynomial([[1, 5], [2, 2], [3, 9]]))


