





from __future__ import annotations

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




class 			_a						:






				def __init__(			self		:   int,			lowerCAmelCase__		:   Collection[float] | None = None							)     ->				None:




										'''simple docstring'''



										if components is None:
																_UpperCamelCase      : Tuple         =   []
										_UpperCamelCase      : Optional[int]         =   list(lowerCAmelCase__							)






				def __len__(			self		:   List[Any]							)     ->				int:




										'''simple docstring'''



										return len(self.__components							)






				def __str__(			self		:   Dict							)     ->				str:




										'''simple docstring'''



										return "(" + ",".join(map(lowerCAmelCase__,			self.__components							)							) + ")"






				def __add__(			self		:   int,			lowerCAmelCase__		:   Vector							)     ->				Vector:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   len(self							)
										if size == len(lowerCAmelCase__							):
																_UpperCamelCase      : str         =   [self.__components[i] + other.component(lowerCAmelCase__							) for i in range(lowerCAmelCase__							)]
																return Vector(lowerCAmelCase__							)
										else:
																raise Exception('''must have the same size'''							)






				def __sub__(			self		:   Optional[Any],			lowerCAmelCase__		:   Vector							)     ->				Vector:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   len(self							)
										if size == len(lowerCAmelCase__							):
																_UpperCamelCase      : List[Any]         =   [self.__components[i] - other.component(lowerCAmelCase__							) for i in range(lowerCAmelCase__							)]
																return Vector(lowerCAmelCase__							)
										else:  # error case
																raise Exception('''must have the same size'''							)






				@overload
				def __mul__(			self		:   Optional[int],			lowerCAmelCase__		:   float							)     ->				Vector:




										'''simple docstring'''



										...






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




										'''simple docstring'''



										...






				def __mul__(			self		:   int,			lowerCAmelCase__		:   float | Vector							)     ->				float | Vector:




										'''simple docstring'''



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






				def     snake_case     (			self		:   List[str]							)     ->				Vector:




										'''simple docstring'''



										return Vector(self.__components							)






				def     snake_case     (			self		:   Any,			lowerCAmelCase__		:   int							)     ->				float:




										'''simple docstring'''



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






				def     snake_case     (			self		:   Optional[Any],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   float							)     ->				None:




										'''simple docstring'''



										assert -len(self.__components							) <= pos < len(self.__components							)
										_UpperCamelCase      : List[str]         =   value






				def     snake_case     (			self		:   str							)     ->				float:




										'''simple docstring'''



										if len(self.__components							) == 0:
																raise Exception('''Vector is empty'''							)
										_UpperCamelCase      : Optional[int]         =   [c**2 for c in self.__components]
										return math.sqrt(sum(lowerCAmelCase__							)							)






				def     snake_case     (			self		:   Optional[Any],			lowerCAmelCase__		:   Vector,			lowerCAmelCase__		:   bool = False							)     ->				float:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   self * other
										_UpperCamelCase      : Union[str, Any]         =   self.euclidean_length() * other.euclidean_length()
										if deg:
																return math.degrees(math.acos(num / den							)							)
										else:
																return math.acos(num / den							)





def 	a_    (							_lowercase       ):
						assert isinstance(_lowercase	,						_lowercase       )
						return Vector([0] * dimension       )





def 	a_    (							_lowercase	,						_lowercase       ):
						assert isinstance(_lowercase	,						_lowercase       ) and (isinstance(_lowercase	,						_lowercase       ))
						_UpperCamelCase      : Tuple         =   [0] * dimension
						_UpperCamelCase      : List[Any]         =   1
						return Vector(_lowercase       )





def 	a_    (							_lowercase	,						_lowercase	,						_lowercase       ):
						assert (
						    isinstance(_lowercase	,						_lowercase       )
						    and isinstance(_lowercase	,						_lowercase       )
						    and (isinstance(_lowercase	,						(int, float)       ))
						)
						return x * scalar + y





def 	a_    (							_lowercase	,						_lowercase	,						_lowercase       ):
						random.seed(_lowercase       )
						_UpperCamelCase      : int         =   [random.randint(_lowercase	,						_lowercase       ) for _ in range(_lowercase       )]
						return Vector(_lowercase       )




class 			_a						:






				def __init__(			self		:   Dict,			lowerCAmelCase__		:   list[list[float]],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   int							)     ->				None:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   matrix
										_UpperCamelCase      : List[str]         =   w
										_UpperCamelCase      : Dict         =   h






				def __str__(			self		:   Optional[int]							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : Optional[Any]         =   ''''''
										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		:   Any,			lowerCAmelCase__		:   Matrix							)     ->				Matrix:




										'''simple docstring'''



										if self.__width == other.width() and self.__height == other.height():
																_UpperCamelCase      : List[str]         =   []
																for i in range(self.__height							):
																						_UpperCamelCase      : int         =   [
																						    self.__matrix[i][j] + other.component(lowerCAmelCase__,			lowerCAmelCase__							)
																						    for j in range(self.__width							)
																						]
																						matrix.append(lowerCAmelCase__							)
																return Matrix(lowerCAmelCase__,			self.__width,			self.__height							)
										else:
																raise Exception('''matrix must have the same dimension!'''							)






				def __sub__(			self		:   Union[str, Any],			lowerCAmelCase__		:   Matrix							)     ->				Matrix:




										'''simple docstring'''



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






				@overload
				def __mul__(			self		:   Any,			lowerCAmelCase__		:   float							)     ->				Matrix:




										'''simple docstring'''



										...






				@overload
				def __mul__(			self		:   str,			lowerCAmelCase__		:   Vector							)     ->				Vector:




										'''simple docstring'''



										...






				def __mul__(			self		:   List[Any],			lowerCAmelCase__		:   float | Vector							)     ->				Vector | Matrix:




										'''simple docstring'''



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






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




										'''simple docstring'''



										return self.__height






				def     snake_case     (			self		:   List[str]							)     ->				int:




										'''simple docstring'''



										return self.__width






				def     snake_case     (			self		:   Union[str, Any],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   int							)     ->				float:




										'''simple docstring'''



										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     snake_case     (			self		:   Any,			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   float							)     ->				None:




										'''simple docstring'''



										if 0 <= x < self.__height and 0 <= y < self.__width:
																_UpperCamelCase      : str         =   value
										else:
																raise Exception('''change_component: indices out of bounds'''							)






				def     snake_case     (			self		:   List[Any],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   int							)     ->				float:




										'''simple docstring'''



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






				def     snake_case     (			self		:   Union[str, Any],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   int							)     ->				float:




										'''simple docstring'''



										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(lowerCAmelCase__,			lowerCAmelCase__							)
										else:
																raise Exception('''Indices out of bounds'''							)






				def     snake_case     (			self		:   Optional[Any]							)     ->				float:




										'''simple docstring'''



										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      : Any         =   [
																    self.__matrix[0][y] * self.cofactor(0,			lowerCAmelCase__							) for y in range(self.__width							)
																]
																return sum(lowerCAmelCase__							)





def 	a_    (							_lowercase       ):
						_UpperCamelCase      : list[list[float]]         =   [[0] * n for _ in range(_lowercase       )]
						return Matrix(_lowercase	,						_lowercase	,						_lowercase       )






def 	a_    (							_lowercase	,						_lowercase	,						_lowercase	,						_lowercase       ):
						random.seed(_lowercase       )
						_UpperCamelCase      : list[list[float]]         =   [
						    [random.randint(_lowercase	,						_lowercase       ) for _ in range(_lowercase       )] for _ in range(_lowercase       )
						]
						return Matrix(_lowercase	,						_lowercase	,						_lowercase       )










"""simple docstring"""




import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer

from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
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 PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




class 			_a						(				_lowerCAmelCase     ,    _lowerCAmelCase     ,    _lowerCAmelCase     ,    unittest.TestCase    ):
				UpperCamelCase								=   AltDiffusionPipeline
				UpperCamelCase								=   TEXT_TO_IMAGE_PARAMS
				UpperCamelCase								=   TEXT_TO_IMAGE_BATCH_PARAMS
				UpperCamelCase								=   TEXT_TO_IMAGE_IMAGE_PARAMS
				UpperCamelCase								=   TEXT_TO_IMAGE_IMAGE_PARAMS






				def     snake_case     (			self		:   int							)     ->				int:




										'''simple docstring'''



										torch.manual_seed(0							)
										_UpperCamelCase      : Dict         =   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      : Union[str, Any]         =   DDIMScheduler(
										    beta_start=0.00_085,			beta_end=0.012,			beta_schedule='''scaled_linear''',			clip_sample=lowerCAmelCase__,			set_alpha_to_one=lowerCAmelCase__,			)
										torch.manual_seed(0							)
										_UpperCamelCase      : List[str]         =   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,			)

										# TODO: address the non-deterministic text encoder (fails for save-load tests)
										# torch.manual_seed(0)
										# text_encoder_config = RobertaSeriesConfig(
										#     hidden_size=32,
										#     project_dim=32,
										#     intermediate_size=37,
										#     layer_norm_eps=1e-05,
										#     num_attention_heads=4,
										#     num_hidden_layers=5,
										#     vocab_size=5002,
										# )
										# text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config)

										torch.manual_seed(0							)
										_UpperCamelCase      : str         =   CLIPTextConfig(
										    bos_token_id=0,			eos_token_id=2,			hidden_size=3_2,			projection_dim=3_2,			intermediate_size=3_7,			layer_norm_eps=1e-0_5,			num_attention_heads=4,			num_hidden_layers=5,			pad_token_id=1,			vocab_size=5_0_0_2,			)
										_UpperCamelCase      : List[Any]         =   CLIPTextModel(lowerCAmelCase__							)

										_UpperCamelCase      : Dict         =   XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''							)
										_UpperCamelCase      : str         =   7_7

										_UpperCamelCase      : int         =   {
										    '''unet''': unet,
										    '''scheduler''': scheduler,
										    '''vae''': vae,
										    '''text_encoder''': text_encoder,
										    '''tokenizer''': tokenizer,
										    '''safety_checker''': None,
										    '''feature_extractor''': None,
										}
										return components






				def     snake_case     (			self		:   Dict,			lowerCAmelCase__		:   Any,			lowerCAmelCase__		:   int=0							)     ->				Optional[int]:




										'''simple docstring'''



										if str(lowerCAmelCase__							).startswith('''mps'''							):
																_UpperCamelCase      : Any         =   torch.manual_seed(lowerCAmelCase__							)
										else:
																_UpperCamelCase      : Dict         =   torch.Generator(device=lowerCAmelCase__							).manual_seed(lowerCAmelCase__							)
										_UpperCamelCase      : str         =   {
										    '''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     snake_case     (			self		:   List[Any]							)     ->				List[str]:




										'''simple docstring'''



										super().test_attention_slicing_forward_pass(expected_max_diff=3e-3							)






				def     snake_case     (			self		:   List[Any]							)     ->				Tuple:




										'''simple docstring'''



										super().test_inference_batch_single_identical(expected_max_diff=3e-3							)






				def     snake_case     (			self		:   List[str]							)     ->				List[Any]:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   '''cpu'''  # ensure determinism for the device-dependent torch.Generator

										_UpperCamelCase      : int         =   self.get_dummy_components()
										torch.manual_seed(0							)
										_UpperCamelCase      : Any         =   RobertaSeriesConfig(
										    hidden_size=3_2,			project_dim=3_2,			intermediate_size=3_7,			layer_norm_eps=1e-0_5,			num_attention_heads=4,			num_hidden_layers=5,			vocab_size=5_0_0_2,			)
										# TODO: remove after fixing the non-deterministic text encoder
										_UpperCamelCase      : Tuple         =   RobertaSeriesModelWithTransformation(lowerCAmelCase__							)
										_UpperCamelCase      : str         =   text_encoder

										_UpperCamelCase      : List[Any]         =   AltDiffusionPipeline(**lowerCAmelCase__							)
										_UpperCamelCase      : List[Any]         =   alt_pipe.to(lowerCAmelCase__							)
										alt_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

										_UpperCamelCase      : Optional[int]         =   self.get_dummy_inputs(lowerCAmelCase__							)
										_UpperCamelCase      : Optional[Any]         =   '''A photo of an astronaut'''
										_UpperCamelCase      : Any         =   alt_pipe(**lowerCAmelCase__							)
										_UpperCamelCase      : Any         =   output.images
										_UpperCamelCase      : Union[str, Any]         =   image[0, -3:, -3:, -1]

										assert image.shape == (1, 6_4, 6_4, 3)
										_UpperCamelCase      : List[Any]         =   np.array(
										    [0.5_748_162, 0.60_447_145, 0.48_821_217, 0.50_100_636, 0.5_431_185, 0.45_763_683, 0.49_657_696, 0.48_132_733, 0.47_573_093]							)

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






				def     snake_case     (			self		:   int							)     ->				Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   '''cpu'''  # ensure determinism for the device-dependent torch.Generator

										_UpperCamelCase      : Optional[Any]         =   self.get_dummy_components()
										_UpperCamelCase      : str         =   PNDMScheduler(skip_prk_steps=lowerCAmelCase__							)
										torch.manual_seed(0							)
										_UpperCamelCase      : int         =   RobertaSeriesConfig(
										    hidden_size=3_2,			project_dim=3_2,			intermediate_size=3_7,			layer_norm_eps=1e-0_5,			num_attention_heads=4,			num_hidden_layers=5,			vocab_size=5_0_0_2,			)
										# TODO: remove after fixing the non-deterministic text encoder
										_UpperCamelCase      : Tuple         =   RobertaSeriesModelWithTransformation(lowerCAmelCase__							)
										_UpperCamelCase      : int         =   text_encoder
										_UpperCamelCase      : str         =   AltDiffusionPipeline(**lowerCAmelCase__							)
										_UpperCamelCase      : List[str]         =   alt_pipe.to(lowerCAmelCase__							)
										alt_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

										_UpperCamelCase      : Union[str, Any]         =   self.get_dummy_inputs(lowerCAmelCase__							)
										_UpperCamelCase      : Optional[int]         =   alt_pipe(**lowerCAmelCase__							)
										_UpperCamelCase      : List[str]         =   output.images
										_UpperCamelCase      : Optional[int]         =   image[0, -3:, -3:, -1]

										assert image.shape == (1, 6_4, 6_4, 3)
										_UpperCamelCase      : List[Any]         =   np.array(
										    [0.51_605_093, 0.5_707_241, 0.47_365_507, 0.50_578_886, 0.5_633_877, 0.4_642_503, 0.5_182_081, 0.48_763_484, 0.49_084_237]							)

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




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






				def     snake_case     (			self		:   List[str]							)     ->				Any:




										'''simple docstring'''



										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()






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




										'''simple docstring'''



										_UpperCamelCase      : str         =   AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''',			safety_checker=lowerCAmelCase__							)
										_UpperCamelCase      : List[Any]         =   alt_pipe.to(lowerCAmelCase__							)
										alt_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

										_UpperCamelCase      : int         =   '''A painting of a squirrel eating a burger'''
										_UpperCamelCase      : int         =   torch.manual_seed(0							)
										_UpperCamelCase      : Dict         =   alt_pipe([prompt],			generator=lowerCAmelCase__,			guidance_scale=6.0,			num_inference_steps=2_0,			output_type='''np'''							)

										_UpperCamelCase      : Optional[int]         =   output.images

										_UpperCamelCase      : Optional[int]         =   image[0, -3:, -3:, -1]

										assert image.shape == (1, 5_1_2, 5_1_2, 3)
										_UpperCamelCase      : List[str]         =   np.array([0.1_010, 0.0_800, 0.0_794, 0.0_885, 0.0_843, 0.0_762, 0.0_769, 0.0_729, 0.0_586]							)

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






				def     snake_case     (			self		:   str							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   DDIMScheduler.from_pretrained('''BAAI/AltDiffusion''',			subfolder='''scheduler'''							)

										_UpperCamelCase      : Dict         =   AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''',			scheduler=lowerCAmelCase__,			safety_checker=lowerCAmelCase__							)
										_UpperCamelCase      : Dict         =   alt_pipe.to(lowerCAmelCase__							)
										alt_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

										_UpperCamelCase      : Optional[Any]         =   '''A painting of a squirrel eating a burger'''
										_UpperCamelCase      : Optional[Any]         =   torch.manual_seed(0							)

										_UpperCamelCase      : Union[str, Any]         =   alt_pipe([prompt],			generator=lowerCAmelCase__,			num_inference_steps=2,			output_type='''numpy'''							)
										_UpperCamelCase      : Tuple         =   output.images

										_UpperCamelCase      : Dict         =   image[0, -3:, -3:, -1]

										assert image.shape == (1, 5_1_2, 5_1_2, 3)
										_UpperCamelCase      : str         =   np.array([0.4_019, 0.4_052, 0.3_810, 0.4_119, 0.3_916, 0.3_982, 0.4_651, 0.4_195, 0.5_323]							)

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



