





'''simple docstring'''






from __future__ import annotations

import math
import random
from collections.abc import Collection
from typing import overload







class 		UpperCAmelCase   :


  """simple docstring"""







  def __init__(				self    :					List[Any]							,	UpperCamelCase__    :					Collection[float] | None = None  )     ->						None:
    if components is None:
      _UpperCamelCase       			=[]
    _UpperCamelCase       			=list(UpperCamelCase__  )


  def __len__(				self    :					Dict  )     ->						int:
    return len(self.__components  )


  def __str__(				self    :					Union[str, Any]  )     ->						str:
    return "(" + ",".join(map(UpperCamelCase__							,	self.__components  )  ) + ")"


  def __add__(				self    :					int							,	UpperCamelCase__    :					Vector  )     ->						Vector:
    _UpperCamelCase       			=len(self  )
    if size == len(UpperCamelCase__  ):
      _UpperCamelCase       			=[self.__components[i] + other.component(UpperCamelCase__  ) for i in range(UpperCamelCase__  )]
      return Vector(UpperCamelCase__  )
    else:
      raise Exception('''must have the same size'''  )


  def __sub__(				self    :					Dict							,	UpperCamelCase__    :					Vector  )     ->						Vector:
    _UpperCamelCase       			=len(self  )
    if size == len(UpperCamelCase__  ):
      _UpperCamelCase       			=[self.__components[i] - other.component(UpperCamelCase__  ) for i in range(UpperCamelCase__  )]
      return Vector(UpperCamelCase__  )
    else:  # error case
      raise Exception('''must have the same size'''  )


  @overload
  def __mul__(				self    :					Optional[Any]							,	UpperCamelCase__    :					float  )     ->						Vector:
    ...


  @overload
  def __mul__(				self    :					int							,	UpperCamelCase__    :					Vector  )     ->						float:
    ...


  def __mul__(				self    :					Union[str, Any]							,	UpperCamelCase__    :					float | Vector  )     ->						float | Vector:
    if isinstance(UpperCamelCase__							,	(float, int)  ):
      _UpperCamelCase       			=[c * other for c in self.__components]
      return Vector(UpperCamelCase__  )
    elif isinstance(UpperCamelCase__							,	UpperCamelCase__  ) and len(self  ) == len(UpperCamelCase__  ):
      _UpperCamelCase       			=len(self  )
      _UpperCamelCase       			=[self.__components[i] * other.component(UpperCamelCase__  ) for i in range(UpperCamelCase__  )]
      return sum(UpperCamelCase__  )
    else:  # error case
      raise Exception('''invalid operand!'''  )


  def   UpperCamelCase__      (				self    :					List[Any]  )     ->						Vector:
    return Vector(self.__components  )


  def   UpperCamelCase__      (				self    :					int							,	UpperCamelCase__    :					int  )     ->						float:
    if isinstance(UpperCamelCase__							,	UpperCamelCase__  ) and -len(self.__components  ) <= i < len(self.__components  ):
      return self.__components[i]
    else:
      raise Exception('''index out of range'''  )


  def   UpperCamelCase__      (				self    :					Union[str, Any]							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					float  )     ->						None:
    assert -len(self.__components  ) <= pos < len(self.__components  )
    _UpperCamelCase       			=value


  def   UpperCamelCase__      (				self    :					List[str]  )     ->						float:
    if len(self.__components  ) == 0:
      raise Exception('''Vector is empty'''  )
    _UpperCamelCase       			=[c**2 for c in self.__components]
    return math.sqrt(sum(UpperCamelCase__  )  )




  def   UpperCamelCase__      (				self    :					Any							,	UpperCamelCase__    :					Vector							,	UpperCamelCase__    :					bool = False  )     ->						float:
    _UpperCamelCase       			=self * other
    _UpperCamelCase       			=self.euclidean_length() * other.euclidean_length()
    if deg:
      return math.degrees(math.acos(num / den  )  )
    else:
      return math.acos(num / den  )




def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  assert isinstance(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
  return Vector([0] * dimension   )


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  assert isinstance(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ) and (isinstance(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ))
  _UpperCamelCase       			=[0] * dimension
  _UpperCamelCase       			=1
  return Vector(__SCREAMING_SNAKE_CASE   )


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  assert (
      isinstance(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
      and isinstance(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
      and (isinstance(__SCREAMING_SNAKE_CASE  ,      (int, float)   ))
  )
  return x * scalar + y


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  random.seed(__SCREAMING_SNAKE_CASE   )
  _UpperCamelCase       			=[random.randint(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ) for _ in range(__SCREAMING_SNAKE_CASE   )]
  return Vector(__SCREAMING_SNAKE_CASE   )







class 		UpperCAmelCase   :


  """simple docstring"""







  def __init__(				self    :					Optional[int]							,	UpperCamelCase__    :					list[list[float]]							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					int  )     ->						None:
    _UpperCamelCase       			=matrix
    _UpperCamelCase       			=w
    _UpperCamelCase       			=h


  def __str__(				self    :					Optional[Any]  )     ->						str:
    _UpperCamelCase       			=''''''
    for i in range(self.__height  ):
      ans += "|"
      for j in range(self.__width  ):
        if j < self.__width - 1:
          ans += str(self.__matrix[i][j]  ) + ","
        else:
          ans += str(self.__matrix[i][j]  ) + "|\n"
    return ans


  def __add__(				self    :					Optional[Any]							,	UpperCamelCase__    :					Matrix  )     ->						Matrix:
    if self.__width == other.width() and self.__height == other.height():
      _UpperCamelCase       			=[]
      for i in range(self.__height  ):
        _UpperCamelCase       			=[
            self.__matrix[i][j] + other.component(UpperCamelCase__							,	UpperCamelCase__  )
            for j in range(self.__width  )
        ]
        matrix.append(UpperCamelCase__  )
      return Matrix(UpperCamelCase__							,	self.__width							,	self.__height  )
    else:
      raise Exception('''matrix must have the same dimension!'''  )


  def __sub__(				self    :					List[Any]							,	UpperCamelCase__    :					Matrix  )     ->						Matrix:
    if self.__width == other.width() and self.__height == other.height():
      _UpperCamelCase       			=[]
      for i in range(self.__height  ):
        _UpperCamelCase       			=[
            self.__matrix[i][j] - other.component(UpperCamelCase__							,	UpperCamelCase__  )
            for j in range(self.__width  )
        ]
        matrix.append(UpperCamelCase__  )
      return Matrix(UpperCamelCase__							,	self.__width							,	self.__height  )
    else:
      raise Exception('''matrices must have the same dimension!'''  )


  @overload
  def __mul__(				self    :					List[Any]							,	UpperCamelCase__    :					float  )     ->						Matrix:
    ...


  @overload
  def __mul__(				self    :					List[Any]							,	UpperCamelCase__    :					Vector  )     ->						Vector:
    ...


  def __mul__(				self    :					int							,	UpperCamelCase__    :					float | Vector  )     ->						Vector | Matrix:
    if isinstance(UpperCamelCase__							,	UpperCamelCase__  ):  # matrix-vector
      if len(UpperCamelCase__  ) == self.__width:
        _UpperCamelCase       			=zero_vector(self.__height  )
        for i in range(self.__height  ):
          _UpperCamelCase       			=[
              self.__matrix[i][j] * other.component(UpperCamelCase__  )
              for j in range(self.__width  )
          ]
          ans.change_component(UpperCamelCase__							,	sum(UpperCamelCase__  )  )
        return ans
      else:
        raise Exception(
            '''vector must have the same size as the '''
            '''number of columns of the matrix!'''  )
    elif isinstance(UpperCamelCase__							,	(int, float)  ):  # matrix-scalar
      _UpperCamelCase       			=[
          [self.__matrix[i][j] * other for j in range(self.__width  )]
          for i in range(self.__height  )
      ]
      return Matrix(UpperCamelCase__							,	self.__width							,	self.__height  )
    return None


  def   UpperCamelCase__      (				self    :					Optional[Any]  )     ->						int:
    return self.__height


  def   UpperCamelCase__      (				self    :					int  )     ->						int:
    return self.__width


  def   UpperCamelCase__      (				self    :					int							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					int  )     ->						float:
    if 0 <= x < self.__height and 0 <= y < self.__width:
      return self.__matrix[x][y]
    else:
      raise Exception('''change_component: indices out of bounds'''  )


  def   UpperCamelCase__      (				self    :					List[Any]							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					float  )     ->						None:
    if 0 <= x < self.__height and 0 <= y < self.__width:
      _UpperCamelCase       			=value
    else:
      raise Exception('''change_component: indices out of bounds'''  )


  def   UpperCamelCase__      (				self    :					List[str]							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					int  )     ->						float:
    if self.__height != self.__width:
      raise Exception('''Matrix is not square'''  )
    _UpperCamelCase       			=self.__matrix[:x] + self.__matrix[x + 1 :]
    for i in range(len(UpperCamelCase__  )  ):
      _UpperCamelCase       			=minor[i][:y] + minor[i][y + 1 :]
    return Matrix(UpperCamelCase__							,	self.__width - 1							,	self.__height - 1  ).determinant()


  def   UpperCamelCase__      (				self    :					Optional[int]							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					int  )     ->						float:
    if self.__height != self.__width:
      raise Exception('''Matrix is not square'''  )
    if 0 <= x < self.__height and 0 <= y < self.__width:
      return (-1) ** (x + y) * self.minor(UpperCamelCase__							,	UpperCamelCase__  )
    else:
      raise Exception('''Indices out of bounds'''  )




  def   UpperCamelCase__      (				self    :					int  )     ->						float:
    if self.__height != self.__width:
      raise Exception('''Matrix is not square'''  )
    if self.__height < 1:
      raise Exception('''Matrix has no element'''  )
    elif self.__height == 1:
      return self.__matrix[0][0]
    elif self.__height == 2:
      return (
          self.__matrix[0][0] * self.__matrix[1][1]
          - self.__matrix[0][1] * self.__matrix[1][0]
      )
    else:
      _UpperCamelCase       			=[
          self.__matrix[0][y] * self.cofactor(0							,	UpperCamelCase__  ) for y in range(self.__width  )
      ]
      return sum(UpperCamelCase__  )




def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=[[0] * n for _ in range(__SCREAMING_SNAKE_CASE   )]
  return Matrix(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  random.seed(__SCREAMING_SNAKE_CASE   )
  _UpperCamelCase       			=[
      [random.randint(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ) for _ in range(__SCREAMING_SNAKE_CASE   )] for _ in range(__SCREAMING_SNAKE_CASE   )
  ]
  return Matrix(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )













'''simple docstring'''






import tempfile

import numpy as np
import torch
from transformers import AutoTokenizer, TaEncoderModel

from diffusers import DDPMScheduler, UNetaDConditionModel
from diffusers.models.attention_processor import AttnAddedKVProcessor
from diffusers.pipelines.deepfloyd_if import IFWatermarker
from diffusers.utils.testing_utils import torch_device

from ..test_pipelines_common import to_np







class 		UpperCAmelCase   :


  """simple docstring"""







  def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						Union[str, Any]:
    torch.manual_seed(0  )
    _UpperCamelCase       			=TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5'''  )

    torch.manual_seed(0  )
    _UpperCamelCase       			=AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5'''  )

    torch.manual_seed(0  )
    _UpperCamelCase       			=UNetaDConditionModel(
        sample_size=32							,	layers_per_block=1							,	block_out_channels=[32, 64]							,	down_block_types=[
            '''ResnetDownsampleBlock2D''',
            '''SimpleCrossAttnDownBlock2D''',
        ]							,	mid_block_type='''UNetMidBlock2DSimpleCrossAttn'''							,	up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D''']							,	in_channels=3							,	out_channels=6							,	cross_attention_dim=32							,	encoder_hid_dim=32							,	attention_head_dim=8							,	addition_embed_type='''text'''							,	addition_embed_type_num_heads=2							,	cross_attention_norm='''group_norm'''							,	resnet_time_scale_shift='''scale_shift'''							,	act_fn='''gelu'''							,	)
    unet.set_attn_processor(AttnAddedKVProcessor()  )  # For reproducibility tests

    torch.manual_seed(0  )
    _UpperCamelCase       			=DDPMScheduler(
        num_train_timesteps=1000							,	beta_schedule='''squaredcos_cap_v2'''							,	beta_start=0.0001							,	beta_end=0.02							,	thresholding=UpperCamelCase__							,	dynamic_thresholding_ratio=0.95							,	sample_max_value=1.0							,	prediction_type='''epsilon'''							,	variance_type='''learned_range'''							,	)

    torch.manual_seed(0  )
    _UpperCamelCase       			=IFWatermarker()

    return {
        "text_encoder": text_encoder,
        "tokenizer": tokenizer,
        "unet": unet,
        "scheduler": scheduler,
        "watermarker": watermarker,
        "safety_checker": None,
        "feature_extractor": None,
    }


  def   UpperCamelCase__      (				self    :					List[Any]  )     ->						Optional[int]:
    torch.manual_seed(0  )
    _UpperCamelCase       			=TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5'''  )

    torch.manual_seed(0  )
    _UpperCamelCase       			=AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5'''  )

    torch.manual_seed(0  )
    _UpperCamelCase       			=UNetaDConditionModel(
        sample_size=32							,	layers_per_block=[1, 2]							,	block_out_channels=[32, 64]							,	down_block_types=[
            '''ResnetDownsampleBlock2D''',
            '''SimpleCrossAttnDownBlock2D''',
        ]							,	mid_block_type='''UNetMidBlock2DSimpleCrossAttn'''							,	up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D''']							,	in_channels=6							,	out_channels=6							,	cross_attention_dim=32							,	encoder_hid_dim=32							,	attention_head_dim=8							,	addition_embed_type='''text'''							,	addition_embed_type_num_heads=2							,	cross_attention_norm='''group_norm'''							,	resnet_time_scale_shift='''scale_shift'''							,	act_fn='''gelu'''							,	class_embed_type='''timestep'''							,	mid_block_scale_factor=1.414							,	time_embedding_act_fn='''gelu'''							,	time_embedding_dim=32							,	)
    unet.set_attn_processor(AttnAddedKVProcessor()  )  # For reproducibility tests

    torch.manual_seed(0  )
    _UpperCamelCase       			=DDPMScheduler(
        num_train_timesteps=1000							,	beta_schedule='''squaredcos_cap_v2'''							,	beta_start=0.0001							,	beta_end=0.02							,	thresholding=UpperCamelCase__							,	dynamic_thresholding_ratio=0.95							,	sample_max_value=1.0							,	prediction_type='''epsilon'''							,	variance_type='''learned_range'''							,	)

    torch.manual_seed(0  )
    _UpperCamelCase       			=DDPMScheduler(
        num_train_timesteps=1000							,	beta_schedule='''squaredcos_cap_v2'''							,	beta_start=0.0001							,	beta_end=0.02							,	)

    torch.manual_seed(0  )
    _UpperCamelCase       			=IFWatermarker()

    return {
        "text_encoder": text_encoder,
        "tokenizer": tokenizer,
        "unet": unet,
        "scheduler": scheduler,
        "image_noising_scheduler": image_noising_scheduler,
        "watermarker": watermarker,
        "safety_checker": None,
        "feature_extractor": None,
    }


  def   UpperCamelCase__      (				self    :					Any  )     ->						Any:
    _UpperCamelCase       			=self.get_dummy_components()
    _UpperCamelCase       			=self.pipeline_class(**UpperCamelCase__  )
    pipe.to(UpperCamelCase__  )
    pipe.set_progress_bar_config(disable=UpperCamelCase__  )

    _UpperCamelCase       			=self.get_dummy_inputs(UpperCamelCase__  )

    _UpperCamelCase       			=inputs['''prompt''']
    _UpperCamelCase       			=inputs['''generator''']
    _UpperCamelCase       			=inputs['''num_inference_steps''']
    _UpperCamelCase       			=inputs['''output_type''']

    if "image" in inputs:
      _UpperCamelCase       			=inputs['''image''']
    else:
      _UpperCamelCase       			=None

    if "mask_image" in inputs:
      _UpperCamelCase       			=inputs['''mask_image''']
    else:
      _UpperCamelCase       			=None

    if "original_image" in inputs:
      _UpperCamelCase       			=inputs['''original_image''']
    else:
      _UpperCamelCase       			=None

    _UpperCamelCase			,     _UpperCamelCase       			=pipe.encode_prompt(UpperCamelCase__  )

    # inputs with prompt converted to embeddings
    _UpperCamelCase       			={
        '''prompt_embeds''': prompt_embeds,
        '''negative_prompt_embeds''': negative_prompt_embeds,
        '''generator''': generator,
        '''num_inference_steps''': num_inference_steps,
        '''output_type''': output_type,
    }

    if image is not None:
      _UpperCamelCase       			=image

    if mask_image is not None:
      _UpperCamelCase       			=mask_image

    if original_image is not None:
      _UpperCamelCase       			=original_image

    # set all optional components to None
    for optional_component in pipe._optional_components:
      setattr(UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__  )

    _UpperCamelCase       			=pipe(**UpperCamelCase__  )[0]

    with tempfile.TemporaryDirectory() as tmpdir:
      pipe.save_pretrained(UpperCamelCase__  )
      _UpperCamelCase       			=self.pipeline_class.from_pretrained(UpperCamelCase__  )
      pipe_loaded.to(UpperCamelCase__  )
      pipe_loaded.set_progress_bar_config(disable=UpperCamelCase__  )

    pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor()  )  # For reproducibility tests

    for optional_component in pipe._optional_components:
      self.assertTrue(
          getattr(UpperCamelCase__							,	UpperCamelCase__  ) is None							,	F'''`{optional_component}` did not stay set to None after loading.'''							,	)

    _UpperCamelCase       			=self.get_dummy_inputs(UpperCamelCase__  )

    _UpperCamelCase       			=inputs['''generator''']
    _UpperCamelCase       			=inputs['''num_inference_steps''']
    _UpperCamelCase       			=inputs['''output_type''']

    # inputs with prompt converted to embeddings
    _UpperCamelCase       			={
        '''prompt_embeds''': prompt_embeds,
        '''negative_prompt_embeds''': negative_prompt_embeds,
        '''generator''': generator,
        '''num_inference_steps''': num_inference_steps,
        '''output_type''': output_type,
    }

    if image is not None:
      _UpperCamelCase       			=image

    if mask_image is not None:
      _UpperCamelCase       			=mask_image

    if original_image is not None:
      _UpperCamelCase       			=original_image

    _UpperCamelCase       			=pipe_loaded(**UpperCamelCase__  )[0]

    _UpperCamelCase       			=np.abs(to_np(UpperCamelCase__  ) - to_np(UpperCamelCase__  )  ).max()
    self.assertLess(UpperCamelCase__							,	1E-4  )




  def   UpperCamelCase__      (				self    :					int  )     ->						str:
    _UpperCamelCase       			=self.get_dummy_components()
    _UpperCamelCase       			=self.pipeline_class(**UpperCamelCase__  )
    pipe.to(UpperCamelCase__  )
    pipe.set_progress_bar_config(disable=UpperCamelCase__  )

    _UpperCamelCase       			=self.get_dummy_inputs(UpperCamelCase__  )
    _UpperCamelCase       			=pipe(**UpperCamelCase__  )[0]

    with tempfile.TemporaryDirectory() as tmpdir:
      pipe.save_pretrained(UpperCamelCase__  )
      _UpperCamelCase       			=self.pipeline_class.from_pretrained(UpperCamelCase__  )
      pipe_loaded.to(UpperCamelCase__  )
      pipe_loaded.set_progress_bar_config(disable=UpperCamelCase__  )

    pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor()  )  # For reproducibility tests

    _UpperCamelCase       			=self.get_dummy_inputs(UpperCamelCase__  )
    _UpperCamelCase       			=pipe_loaded(**UpperCamelCase__  )[0]

    _UpperCamelCase       			=np.abs(to_np(UpperCamelCase__  ) - to_np(UpperCamelCase__  )  ).max()
    self.assertLess(UpperCamelCase__							,	1E-4  )






