






import os
import tempfile
import unittest

import numpy as np
import torch

from diffusers.models import ModelMixin, UNetaDConditionModel
from diffusers.models.attention_processor import AttnProcessor, LoRAAttnProcessor
from diffusers.utils import (
    floats_tensor,
    logging,
    skip_mps,
    torch_device,
)
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()

UpperCamelCase			=      logging.get_logger(__name__)






def     A(  snake_case_						,       snake_case_ = True		):
       """simple docstring"""





       lowercase__:    Optional[Any]        =      {}
       for name in model.attn_processors.keys():
              lowercase__:    Optional[Any]        =      name.endswith("attn2.processor"		) and not (
                  name.startswith("transformer_in"		) or "temp_attentions" in name.split("."		)
              )
              lowercase__:    Dict        =      model.config.cross_attention_dim if has_cross_attention else None
              if name.startswith("mid_block"		):
                     lowercase__:    int        =      model.config.block_out_channels[-1]
              elif name.startswith("up_blocks"		):
                     lowercase__:    List[str]        =      int(name[len("up_blocks."		)]		)
                     lowercase__:    str        =      list(reversed(model.config.block_out_channels		)		)[block_id]
              elif name.startswith("down_blocks"		):
                     lowercase__:    Union[str, Any]        =      int(name[len("down_blocks."		)]		)
                     lowercase__:    Union[str, Any]        =      model.config.block_out_channels[block_id]
              elif name.startswith("transformer_in"		):
                     # Note that the `8 * ...` comes from: https://github.com/huggingface/diffusers/blob/7139f0e874f10b2463caa8cbd585762a309d12d6/src/diffusers/models/unet_3d_condition.py#L148
                     lowercase__:    Optional[Any]        =      8 * model.config.attention_head_dim

              lowercase__:    List[str]        =      LoRAAttnProcessor(hidden_size=_lowerCamelCase						,       cross_attention_dim=_lowerCamelCase		)
              lowercase__:    int        =      lora_attn_procs[name].to(model.device		)

              if mock_weights:
                     # add 1 to weights to mock trained weights
                     with torch.no_grad():
                            lora_attn_procs[name].to_q_lora.up.weight += 1
                            lora_attn_procs[name].to_k_lora.up.weight += 1
                            lora_attn_procs[name].to_v_lora.up.weight += 1
                            lora_attn_procs[name].to_out_lora.up.weight += 1

       return lora_attn_procs







@skip_mps
class 		_a       (						__lowerCAmelCase    ,					__lowerCAmelCase    ,					unittest.TestCase   ):





   '''simple docstring'''






   UpperCamelCase__      					=    UNetaDConditionModel
   UpperCamelCase__      					=    '''sample'''





   @property
   def __lowercase    (   self)	-> List[Any]:




          '''simple docstring'''




          lowercase__:    str        =      4
          lowercase__:    Dict        =      4
          lowercase__:    Tuple        =      4
          lowercase__:    Optional[Any]        =      (32, 32)

          lowercase__:    Optional[int]        =      floats_tensor((batch_size, num_channels, num_frames) + sizes).to(__SCREAMING_SNAKE_CASE)
          lowercase__:    List[Any]        =      torch.tensor([10]).to(__SCREAMING_SNAKE_CASE)
          lowercase__:    Union[str, Any]        =      floats_tensor((batch_size, 4, 32)).to(__SCREAMING_SNAKE_CASE)

          return {"sample": noise, "timestep": time_step, "encoder_hidden_states": encoder_hidden_states}





   @property
   def __lowercase    (   self)	-> List[str]:




          '''simple docstring'''




          return (4, 4, 32, 32)





   @property
   def __lowercase    (   self)	-> str:




          '''simple docstring'''




          return (4, 4, 32, 32)





   def __lowercase    (   self)	-> List[Any]:




          '''simple docstring'''




          lowercase__:    Any        =      {
              "block_out_channels": (32, 64),
              "down_block_types": (
                  "CrossAttnDownBlock3D",
                  "DownBlock3D",
              ),
              "up_block_types": ("UpBlock3D", "CrossAttnUpBlock3D"),
              "cross_attention_dim": 32,
              "attention_head_dim": 8,
              "out_channels": 4,
              "in_channels": 4,
              "layers_per_block": 1,
              "sample_size": 32,
          }
          lowercase__:    str        =      self.dummy_input
          return init_dict, inputs_dict





   @unittest.skipIf(
       torch_device != "cuda" or not is_xformers_available()			,				reason="XFormers attention is only available with CUDA and `xformers` installed"			,				)
   def __lowercase    (   self)	-> Tuple:




          '''simple docstring'''




          lowercase__  ,     lowercase__:    Any        =      self.prepare_init_args_and_inputs_for_common()
          lowercase__:    int        =      self.model_class(**__SCREAMING_SNAKE_CASE)

          model.enable_xformers_memory_efficient_attention()

          assert (
              model.mid_block.attentions[0].transformer_blocks[0].attna.processor.__class__.__name__
              == "XFormersAttnProcessor"
          ), "xformers is not enabled"





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




          '''simple docstring'''




          lowercase__  ,     lowercase__:    Union[str, Any]        =      self.prepare_init_args_and_inputs_for_common()

          lowercase__:    int        =      32

          lowercase__:    str        =      self.model_class(**__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)
          model.eval()

          with torch.no_grad():
                 lowercase__:    Optional[int]        =      model(**__SCREAMING_SNAKE_CASE)

                 if isinstance(__SCREAMING_SNAKE_CASE			,				__SCREAMING_SNAKE_CASE):
                        lowercase__:    Any        =      output.sample

          self.assertIsNotNone(__SCREAMING_SNAKE_CASE)
          lowercase__:    int        =      inputs_dict["sample"].shape
          self.assertEqual(output.shape			,				__SCREAMING_SNAKE_CASE			,				"Input and output shapes do not match")





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




          '''simple docstring'''




          lowercase__  ,     lowercase__:    Optional[int]        =      self.prepare_init_args_and_inputs_for_common()
          lowercase__:    int        =      self.model_class(**__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)
          model.eval()

          with torch.no_grad():
                 # Warmup pass when using mps (see #372)
                 if torch_device == "mps" and isinstance(__SCREAMING_SNAKE_CASE			,				__SCREAMING_SNAKE_CASE):
                        model(**self.dummy_input)

                 lowercase__:    Dict        =      model(**__SCREAMING_SNAKE_CASE)
                 if isinstance(__SCREAMING_SNAKE_CASE			,				__SCREAMING_SNAKE_CASE):
                        lowercase__:    Optional[Any]        =      first.sample

                 lowercase__:    List[Any]        =      model(**__SCREAMING_SNAKE_CASE)
                 if isinstance(__SCREAMING_SNAKE_CASE			,				__SCREAMING_SNAKE_CASE):
                        lowercase__:    Union[str, Any]        =      second.sample

          lowercase__:    Tuple        =      first.cpu().numpy()
          lowercase__:    Any        =      second.cpu().numpy()
          lowercase__:    Optional[int]        =      out_a[~np.isnan(__SCREAMING_SNAKE_CASE)]
          lowercase__:    str        =      out_a[~np.isnan(__SCREAMING_SNAKE_CASE)]
          lowercase__:    int        =      np.amax(np.abs(out_a - out_a))
          self.assertLessEqual(__SCREAMING_SNAKE_CASE			,				1E-5)





   def __lowercase    (   self)	-> Dict:




          '''simple docstring'''




          lowercase__  ,     lowercase__:    Any        =      self.prepare_init_args_and_inputs_for_common()

          lowercase__:    Union[str, Any]        =      8

          lowercase__:    List[Any]        =      self.model_class(**__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)
          model.eval()

          model.set_attention_slice("auto")
          with torch.no_grad():
                 lowercase__:    Dict        =      model(**__SCREAMING_SNAKE_CASE)
          assert output is not None

          model.set_attention_slice("max")
          with torch.no_grad():
                 lowercase__:    Optional[Any]        =      model(**__SCREAMING_SNAKE_CASE)
          assert output is not None

          model.set_attention_slice(2)
          with torch.no_grad():
                 lowercase__:    int        =      model(**__SCREAMING_SNAKE_CASE)
          assert output is not None





   def __lowercase    (   self)	-> List[Any]:




          '''simple docstring'''




          lowercase__  ,     lowercase__:    Optional[Any]        =      self.prepare_init_args_and_inputs_for_common()

          lowercase__:    Optional[Any]        =      8

          lowercase__:    str        =      self.model_class(**__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)

          with torch.no_grad():
                 lowercase__:    int        =      model(**__SCREAMING_SNAKE_CASE).sample

          lowercase__:    Any        =      create_lora_layers(__SCREAMING_SNAKE_CASE)

          # make sure we can set a list of attention processors
          model.set_attn_processor(__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)

          # test that attn processors can be set to itself
          model.set_attn_processor(model.attn_processors)

          with torch.no_grad():
                 lowercase__:    List[str]        =      model(**__SCREAMING_SNAKE_CASE			,				cross_attention_kwargs={"scale": 0.0}).sample
                 lowercase__:    str        =      model(**__SCREAMING_SNAKE_CASE			,				cross_attention_kwargs={"scale": 0.5}).sample
                 lowercase__:    Optional[Any]        =      model(**__SCREAMING_SNAKE_CASE			,				cross_attention_kwargs={"scale": 0.5}).sample

          assert (samplea - samplea).abs().max() < 3E-3
          assert (samplea - samplea).abs().max() < 3E-3

          # sample 2 and sample 3 should be different
          assert (samplea - samplea).abs().max() > 3E-3





   def __lowercase    (   self)	-> Tuple:




          '''simple docstring'''




          lowercase__  ,     lowercase__:    Dict        =      self.prepare_init_args_and_inputs_for_common()

          lowercase__:    Tuple        =      8

          torch.manual_seed(0)
          lowercase__:    Tuple        =      self.model_class(**__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)

          with torch.no_grad():
                 lowercase__:    Any        =      model(**__SCREAMING_SNAKE_CASE).sample

          lowercase__:    List[Any]        =      create_lora_layers(__SCREAMING_SNAKE_CASE)
          model.set_attn_processor(__SCREAMING_SNAKE_CASE)

          with torch.no_grad():
                 lowercase__:    Tuple        =      model(**__SCREAMING_SNAKE_CASE			,				cross_attention_kwargs={"scale": 0.5}).sample

          with tempfile.TemporaryDirectory() as tmpdirname:
                 model.save_attn_procs(__SCREAMING_SNAKE_CASE)
                 self.assertTrue(os.path.isfile(os.path.join(__SCREAMING_SNAKE_CASE			,				"pytorch_lora_weights.bin")))
                 torch.manual_seed(0)
                 lowercase__:    Optional[Any]        =      self.model_class(**__SCREAMING_SNAKE_CASE)
                 new_model.to(__SCREAMING_SNAKE_CASE)
                 new_model.load_attn_procs(__SCREAMING_SNAKE_CASE)

          with torch.no_grad():
                 lowercase__:    List[Any]        =      new_model(**__SCREAMING_SNAKE_CASE			,				cross_attention_kwargs={"scale": 0.5}).sample

          assert (sample - new_sample).abs().max() < 1E-3

          # LoRA and no LoRA should NOT be the same
          assert (sample - old_sample).abs().max() > 1E-4





   def __lowercase    (   self)	-> List[Any]:




          '''simple docstring'''




          lowercase__  ,     lowercase__:    Union[str, Any]        =      self.prepare_init_args_and_inputs_for_common()

          lowercase__:    Dict        =      8

          torch.manual_seed(0)
          lowercase__:    Tuple        =      self.model_class(**__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)

          with torch.no_grad():
                 lowercase__:    str        =      model(**__SCREAMING_SNAKE_CASE).sample

          lowercase__:    str        =      create_lora_layers(__SCREAMING_SNAKE_CASE)
          model.set_attn_processor(__SCREAMING_SNAKE_CASE)

          with torch.no_grad():
                 lowercase__:    Union[str, Any]        =      model(**__SCREAMING_SNAKE_CASE			,				cross_attention_kwargs={"scale": 0.5}).sample

          with tempfile.TemporaryDirectory() as tmpdirname:
                 model.save_attn_procs(__SCREAMING_SNAKE_CASE			,				safe_serialization=__SCREAMING_SNAKE_CASE)
                 self.assertTrue(os.path.isfile(os.path.join(__SCREAMING_SNAKE_CASE			,				"pytorch_lora_weights.safetensors")))
                 torch.manual_seed(0)
                 lowercase__:    int        =      self.model_class(**__SCREAMING_SNAKE_CASE)
                 new_model.to(__SCREAMING_SNAKE_CASE)
                 new_model.load_attn_procs(__SCREAMING_SNAKE_CASE)

          with torch.no_grad():
                 lowercase__:    List[Any]        =      new_model(**__SCREAMING_SNAKE_CASE			,				cross_attention_kwargs={"scale": 0.5}).sample

          assert (sample - new_sample).abs().max() < 3E-3

          # LoRA and no LoRA should NOT be the same
          assert (sample - old_sample).abs().max() > 1E-4





   def __lowercase    (   self)	-> Union[str, Any]:




          '''simple docstring'''




          lowercase__  ,     lowercase__:    Dict        =      self.prepare_init_args_and_inputs_for_common()

          lowercase__:    Union[str, Any]        =      8

          torch.manual_seed(0)
          lowercase__:    Optional[int]        =      self.model_class(**__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)

          lowercase__:    List[Any]        =      create_lora_layers(__SCREAMING_SNAKE_CASE			,				mock_weights=__SCREAMING_SNAKE_CASE)
          model.set_attn_processor(__SCREAMING_SNAKE_CASE)
          # Saving as torch, properly reloads with directly filename
          with tempfile.TemporaryDirectory() as tmpdirname:
                 model.save_attn_procs(__SCREAMING_SNAKE_CASE)
                 self.assertTrue(os.path.isfile(os.path.join(__SCREAMING_SNAKE_CASE			,				"pytorch_lora_weights.bin")))
                 torch.manual_seed(0)
                 lowercase__:    Dict        =      self.model_class(**__SCREAMING_SNAKE_CASE)
                 new_model.to(__SCREAMING_SNAKE_CASE)
                 new_model.load_attn_procs(__SCREAMING_SNAKE_CASE			,				weight_name="pytorch_lora_weights.bin")





   def __lowercase    (   self)	-> str:




          '''simple docstring'''




          lowercase__  ,     lowercase__:    List[Any]        =      self.prepare_init_args_and_inputs_for_common()

          lowercase__:    str        =      8

          torch.manual_seed(0)
          lowercase__:    Union[str, Any]        =      self.model_class(**__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)

          lowercase__:    str        =      create_lora_layers(__SCREAMING_SNAKE_CASE			,				mock_weights=__SCREAMING_SNAKE_CASE)
          model.set_attn_processor(__SCREAMING_SNAKE_CASE)
          # Saving as torch, properly reloads with directly filename
          with tempfile.TemporaryDirectory() as tmpdirname:
                 model.save_attn_procs(__SCREAMING_SNAKE_CASE)
                 self.assertTrue(os.path.isfile(os.path.join(__SCREAMING_SNAKE_CASE			,				"pytorch_lora_weights.bin")))
                 torch.manual_seed(0)
                 lowercase__:    Dict        =      self.model_class(**__SCREAMING_SNAKE_CASE)
                 new_model.to(__SCREAMING_SNAKE_CASE)
                 with self.assertRaises(__SCREAMING_SNAKE_CASE) as e:
                        new_model.load_attn_procs(__SCREAMING_SNAKE_CASE			,				use_safetensors=__SCREAMING_SNAKE_CASE)
                 self.assertIn("Error no file named pytorch_lora_weights.safetensors"			,				str(e.exception))





   def __lowercase    (   self)	-> Any:




          '''simple docstring'''




          lowercase__  ,     lowercase__:    int        =      self.prepare_init_args_and_inputs_for_common()

          lowercase__:    Union[str, Any]        =      8

          torch.manual_seed(0)
          lowercase__:    int        =      self.model_class(**__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)

          with torch.no_grad():
                 lowercase__:    str        =      model(**__SCREAMING_SNAKE_CASE).sample

          lowercase__:    Tuple        =      create_lora_layers(__SCREAMING_SNAKE_CASE)
          model.set_attn_processor(__SCREAMING_SNAKE_CASE)

          with torch.no_grad():
                 lowercase__:    Any        =      model(**__SCREAMING_SNAKE_CASE			,				cross_attention_kwargs={"scale": 0.0}).sample

          model.set_attn_processor(AttnProcessor())

          with torch.no_grad():
                 lowercase__:    int        =      model(**__SCREAMING_SNAKE_CASE).sample

          assert (sample - new_sample).abs().max() < 1E-4
          assert (sample - old_sample).abs().max() < 3E-3





   @unittest.skipIf(
       torch_device != "cuda" or not is_xformers_available()			,				reason="XFormers attention is only available with CUDA and `xformers` installed"			,				)
   def __lowercase    (   self)	-> Any:




          '''simple docstring'''




          lowercase__  ,     lowercase__:    Dict        =      self.prepare_init_args_and_inputs_for_common()

          lowercase__:    Union[str, Any]        =      4

          torch.manual_seed(0)
          lowercase__:    Any        =      self.model_class(**__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)
          lowercase__:    Optional[int]        =      create_lora_layers(__SCREAMING_SNAKE_CASE)
          model.set_attn_processor(__SCREAMING_SNAKE_CASE)

          # default
          with torch.no_grad():
                 lowercase__:    Any        =      model(**__SCREAMING_SNAKE_CASE).sample

                 model.enable_xformers_memory_efficient_attention()
                 lowercase__:    str        =      model(**__SCREAMING_SNAKE_CASE).sample

                 model.disable_xformers_memory_efficient_attention()
                 lowercase__:    Optional[Any]        =      model(**__SCREAMING_SNAKE_CASE).sample

          assert (sample - on_sample).abs().max() < 1E-4
          assert (sample - off_sample).abs().max() < 1E-4





   def __lowercase    (   self)	-> str:




          '''simple docstring'''




          lowercase__  ,     lowercase__:    Optional[int]        =      self.prepare_init_args_and_inputs_for_common()
          lowercase__:    Tuple        =      32

          lowercase__:    List[Any]        =      self.model_class(**__SCREAMING_SNAKE_CASE)
          model.to(__SCREAMING_SNAKE_CASE)
          model.eval()

          with torch.no_grad():
                 lowercase__:    List[str]        =      model(**__SCREAMING_SNAKE_CASE)[0]

          model.enable_forward_chunking()
          with torch.no_grad():
                 lowercase__:    str        =      model(**__SCREAMING_SNAKE_CASE)[0]

          self.assertEqual(output.shape			,				output_a.shape			,				"Shape doesn\'t match")
          assert np.abs(output.cpu() - output_a.cpu()).max() < 1E-2


# (todo: sayakpaul) implement SLOW tests.














"""simple docstring"""





import warnings
from collections.abc import Callable
from typing import Any

import numpy
from matplotlib import pyplot

UpperCamelCase			=      0.25 + 0.0j
UpperCamelCase			=      -0.4 + 0.6j
UpperCamelCase			=      -0.1 + 0.651j
UpperCamelCase			=      -2.0
UpperCamelCase			=      56
UpperCamelCase			=      2.0
UpperCamelCase			=      6_66






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       return numpy.exp(snake_case_		) + c_parameter






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       return z_values * z_values + c_parameter






def     A(  snake_case_						,       snake_case_		):
       """simple docstring"""





       lowercase__:    str        =      numpy.linspace(-window_size						,       snake_case_						,       snake_case_		)
       lowercase__:    Union[str, Any]        =      x.reshape((nb_pixels, 1)		)
       lowercase__:    Any        =      numpy.linspace(-window_size						,       snake_case_						,       snake_case_		)
       lowercase__:    List[Any]        =      y.reshape((1, nb_pixels)		)
       return x + 1.0J * y






def     A(  snake_case_						,       snake_case_						,       snake_case_						,       snake_case_						,       snake_case_ = None						,       ):
       """simple docstring"""





       lowercase__:    List[Any]        =      z_a.astype("complex64"		)
       for _ in range(snake_case_		):
              lowercase__:    Any        =      eval_function(snake_case_						,       snake_case_		)
              if infinity is not None:
                     numpy.nan_to_num(snake_case_						,       copy=snake_case_						,       nan=snake_case_		)
                     lowercase__:    Union[str, Any]        =      infinity
       return z_n






def     A(  snake_case_						,       snake_case_						,       snake_case_						,       snake_case_						,       ):
       """simple docstring"""





       lowercase__:    Union[str, Any]        =      (abs(snake_case_		)).transpose()
       lowercase__:    List[Any]        =      abs_z_final[::-1, :]
       pyplot.matshow(abs_z_final < escape_radius		)
       pyplot.title(F"""Julia set of ${function_label}$, $c={function_params}$"""		)
       pyplot.show()






def     A(  ):
       """simple docstring"""





       warnings.filterwarnings(
           "ignore"						,       category=snake_case_						,       message="overflow encountered in multiply"		)
       warnings.filterwarnings(
           "ignore"						,       category=snake_case_						,       message="invalid value encountered in multiply"						,       )
       warnings.filterwarnings(
           "ignore"						,       category=snake_case_						,       message="overflow encountered in absolute"		)
       warnings.filterwarnings(
           "ignore"						,       category=snake_case_						,       message="overflow encountered in exp"		)


if __name__ == "__main__":
  UpperCamelCase			=      prepare_grid(window_size, nb_pixels)

  ignore_overflow_warnings()  # See file header for explanations

  UpperCamelCase			=      24
  UpperCamelCase			=      2 * abs(c_cauliflower) + 1
  UpperCamelCase			=      iterate_function(
      eval_quadratic_polynomial,
      c_cauliflower,
      nb_iterations,
      z_a,
      infinity=1.1 * escape_radius,
  )
  show_results("""z^2+c""", c_cauliflower, escape_radius, z_final)

  UpperCamelCase			=      64
  UpperCamelCase			=      2 * abs(c_polynomial_a) + 1
  UpperCamelCase			=      iterate_function(
      eval_quadratic_polynomial,
      c_polynomial_a,
      nb_iterations,
      z_a,
      infinity=1.1 * escape_radius,
  )
  show_results("""z^2+c""", c_polynomial_a, escape_radius, z_final)

  UpperCamelCase			=      1_61
  UpperCamelCase			=      2 * abs(c_polynomial_a) + 1
  UpperCamelCase			=      iterate_function(
      eval_quadratic_polynomial,
      c_polynomial_a,
      nb_iterations,
      z_a,
      infinity=1.1 * escape_radius,
  )
  show_results("""z^2+c""", c_polynomial_a, escape_radius, z_final)

  UpperCamelCase			=      12
  UpperCamelCase			=      10_000.0
  UpperCamelCase			=      iterate_function(
      eval_exponential,
      c_exponential,
      nb_iterations,
      z_a + 2,
      infinity=1.0e10,
  )
  show_results("""e^z+c""", c_exponential, escape_radius, z_final)






