





"""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 						__snake_case   (						SCREAMING_SNAKE_CASE__				,				SCREAMING_SNAKE_CASE__				,				SCREAMING_SNAKE_CASE__				,				unittest.TestCase	):





 """simple docstring"""


 _lowerCamelCase								=	AltDiffusionPipeline
 _lowerCamelCase								=	TEXT_TO_IMAGE_PARAMS
 _lowerCamelCase								=	TEXT_TO_IMAGE_BATCH_PARAMS
 _lowerCamelCase								=	TEXT_TO_IMAGE_IMAGE_PARAMS
 _lowerCamelCase								=	TEXT_TO_IMAGE_IMAGE_PARAMS

 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     torch.manual_seed(0    )
     __A       :		Tuple							=	UNetaDConditionModel(
         block_out_channels=(32, 64)   ,		layers_per_block=2   ,		sample_size=32   ,		in_channels=4   ,		out_channels=4   ,		down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')   ,		up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')   ,		cross_attention_dim=32   ,		)
     __A       :		int							=	DDIMScheduler(
         beta_start=0.0_0_0_8_5   ,		beta_end=0.0_1_2   ,		beta_schedule='''scaled_linear'''   ,		clip_sample=snake_case__   ,		set_alpha_to_one=snake_case__   ,		)
     torch.manual_seed(0    )
     __A       :		Union[str, Any]							=	AutoencoderKL(
         block_out_channels=[32, 64]   ,		in_channels=3   ,		out_channels=3   ,		down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']   ,		up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']   ,		latent_channels=4   ,		)

     # 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    )
     __A       :		int							=	CLIPTextConfig(
         bos_token_id=0   ,		eos_token_id=2   ,		hidden_size=32   ,		projection_dim=32   ,		intermediate_size=37   ,		layer_norm_eps=1e-0_5   ,		num_attention_heads=4   ,		num_hidden_layers=5   ,		pad_token_id=1   ,		vocab_size=5002   ,		)
     __A       :		List[Any]							=	CLIPTextModel(snake_case__    )

     __A       :		List[Any]							=	XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''    )
     __A       :		str							=	77

     __A       :		Any							=	{
         "unet": unet,
         "scheduler": scheduler,
         "vae": vae,
         "text_encoder": text_encoder,
         "tokenizer": tokenizer,
         "safety_checker": None,
         "feature_extractor": None,
     }
     return components

 def    UpperCamelCase__(       self   ,		__lowerCamelCase   ,		__lowerCamelCase=0    ):



     '''simple docstring'''




     if str(snake_case__    ).startswith('''mps'''    ):
         __A       :		Any							=	torch.manual_seed(snake_case__    )
     else:
         __A       :		Any							=	torch.Generator(device=snake_case__    ).manual_seed(snake_case__    )
     __A       :		int							=	{
         "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    UpperCamelCase__(       self    ):



     '''simple docstring'''




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

 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




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

 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		Dict							=	"cpu"  # ensure determinism for the device-dependent torch.Generator

     __A       :		Dict							=	self.get_dummy_components()
     torch.manual_seed(0    )
     __A       :		Optional[Any]							=	RobertaSeriesConfig(
         hidden_size=32   ,		project_dim=32   ,		intermediate_size=37   ,		layer_norm_eps=1e-0_5   ,		num_attention_heads=4   ,		num_hidden_layers=5   ,		vocab_size=5002   ,		)
     # TODO: remove after fixing the non-deterministic text encoder
     __A       :		Dict							=	RobertaSeriesModelWithTransformation(snake_case__    )
     __A       :		Any							=	text_encoder

     __A       :		Optional[int]							=	AltDiffusionPipeline(**snake_case__    )
     __A       :		List[str]							=	alt_pipe.to(snake_case__    )
     alt_pipe.set_progress_bar_config(disable=snake_case__    )

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

     assert image.shape == (1, 64, 64, 3)
     __A       :		Tuple							=	np.array(
         [0.5_7_4_8_1_6_2, 0.6_0_4_4_7_1_4_5, 0.4_8_8_2_1_2_1_7, 0.5_0_1_0_0_6_3_6, 0.5_4_3_1_1_8_5, 0.4_5_7_6_3_6_8_3, 0.4_9_6_5_7_6_9_6, 0.4_8_1_3_2_7_3_3, 0.4_7_5_7_3_0_9_3]    )

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







 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		Optional[int]							=	"cpu"  # ensure determinism for the device-dependent torch.Generator

     __A       :		Tuple							=	self.get_dummy_components()
     __A       :		Dict							=	PNDMScheduler(skip_prk_steps=snake_case__    )
     torch.manual_seed(0    )
     __A       :		Optional[int]							=	RobertaSeriesConfig(
         hidden_size=32   ,		project_dim=32   ,		intermediate_size=37   ,		layer_norm_eps=1e-0_5   ,		num_attention_heads=4   ,		num_hidden_layers=5   ,		vocab_size=5002   ,		)
     # TODO: remove after fixing the non-deterministic text encoder
     __A       :		Optional[int]							=	RobertaSeriesModelWithTransformation(snake_case__    )
     __A       :		str							=	text_encoder
     __A       :		Tuple							=	AltDiffusionPipeline(**snake_case__    )
     __A       :		Tuple							=	alt_pipe.to(snake_case__    )
     alt_pipe.set_progress_bar_config(disable=snake_case__    )

     __A       :		Dict							=	self.get_dummy_inputs(snake_case__    )
     __A       :		Tuple							=	alt_pipe(**snake_case__    )
     __A       :		Optional[Any]							=	output.images
     __A       :		List[str]							=	image[0, -3:, -3:, -1]

     assert image.shape == (1, 64, 64, 3)
     __A       :		Any							=	np.array(
         [0.5_1_6_0_5_0_9_3, 0.5_7_0_7_2_4_1, 0.4_7_3_6_5_5_0_7, 0.5_0_5_7_8_8_8_6, 0.5_6_3_3_8_7_7, 0.4_6_4_2_5_0_3, 0.5_1_8_2_0_8_1, 0.4_8_7_6_3_4_8_4, 0.4_9_0_8_4_2_3_7]    )

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






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





 """simple docstring"""


 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




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

 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		Optional[int]							=	AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''   ,		safety_checker=snake_case__    )
     __A       :		List[Any]							=	alt_pipe.to(snake_case__    )
     alt_pipe.set_progress_bar_config(disable=snake_case__    )

     __A       :		Optional[Any]							=	"A painting of a squirrel eating a burger"
     __A       :		Union[str, Any]							=	torch.manual_seed(0    )
     __A       :		Union[str, Any]							=	alt_pipe([prompt]   ,		generator=snake_case__   ,		guidance_scale=6.0   ,		num_inference_steps=20   ,		output_type='''np'''    )

     __A       :		List[str]							=	output.images

     __A       :		List[Any]							=	image[0, -3:, -3:, -1]

     assert image.shape == (1, 512, 512, 3)
     __A       :		Optional[int]							=	np.array([0.1_0_1_0, 0.0_8_0_0, 0.0_7_9_4, 0.0_8_8_5, 0.0_8_4_3, 0.0_7_6_2, 0.0_7_6_9, 0.0_7_2_9, 0.0_5_8_6]    )

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







 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		List[Any]							=	DDIMScheduler.from_pretrained('''BAAI/AltDiffusion'''   ,		subfolder='''scheduler'''    )

     __A       :		Union[str, Any]							=	AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''   ,		scheduler=snake_case__   ,		safety_checker=snake_case__    )
     __A       :		Any							=	alt_pipe.to(snake_case__    )
     alt_pipe.set_progress_bar_config(disable=snake_case__    )

     __A       :		str							=	"A painting of a squirrel eating a burger"
     __A       :		Any							=	torch.manual_seed(0    )

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

     __A       :		Any							=	image[0, -3:, -3:, -1]

     assert image.shape == (1, 512, 512, 3)
     __A       :		Union[str, Any]							=	np.array([0.4_0_1_9, 0.4_0_5_2, 0.3_8_1_0, 0.4_1_1_9, 0.3_9_1_6, 0.3_9_8_2, 0.4_6_5_1, 0.4_1_9_5, 0.5_3_2_3]    )

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












"""simple docstring"""



import numpy as np


def 	SCREAMING_SNAKE_CASE__       (  snake_case       : np.ndarray			,					snake_case       : np.ndarray			,					snake_case       : float = 1E-1_2			,					snake_case       : int = 100			,					)->		tuple[float, np.ndarray]:






     '''simple docstring'''



     assert np.shape(snake_case				)[0] == np.shape(snake_case				)[1]
     # Ensure proper dimensionality.
     assert np.shape(snake_case				)[0] == np.shape(snake_case				)[0]
     # Ensure inputs are either both complex or both real
     assert np.iscomplexobj(snake_case				) == np.iscomplexobj(snake_case				)
     UpperCAmelCase__						:    str							=							np.iscomplexobj(snake_case				)
     if is_complex:
          # Ensure complex input_matrix is Hermitian
          assert np.array_equal(snake_case			,					input_matrix.conj().T				)

     # Set convergence to False. Will define convergence when we exceed max_iterations
     # or when we have small changes from one iteration to next.

     UpperCAmelCase__						:    Union[str, Any]							=							False
     UpperCAmelCase__						:    List[Any]							=							0
     UpperCAmelCase__						:    Any							=							0
     UpperCAmelCase__						:    List[str]							=							1E1_2

     while not convergence:
          # Multiple matrix by the vector.
          UpperCAmelCase__						:    Optional[Any]							=							np.dot(snake_case			,					snake_case				)
          # Normalize the resulting output vector.
          UpperCAmelCase__						:    Tuple							=							w / np.linalg.norm(snake_case				)
          # Find rayleigh quotient
          # (faster than usual b/c we know vector is normalized already)
          UpperCAmelCase__						:    Any							=							vector.conj().T if is_complex else vector.T
          UpperCAmelCase__						:    List[str]							=							np.dot(snake_case			,					np.dot(snake_case			,					snake_case				)				)

          # Check convergence.
          UpperCAmelCase__						:    Optional[Any]							=							np.abs(lambda_ - lambda_previous				) / lambda_
          iterations += 1

          if error <= error_tol or iterations >= max_iterations:
               UpperCAmelCase__						:    Tuple							=							True

          UpperCAmelCase__						:    Tuple							=							lambda_

     if is_complex:
          UpperCAmelCase__						:    str							=							np.real(lambda_				)

     return lambda_, vector


def 	SCREAMING_SNAKE_CASE__       (  )->		None:






     '''simple docstring'''



     UpperCAmelCase__						:    List[Any]							=							np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]				)
     UpperCAmelCase__						:    Dict							=							np.array([41, 4, 20]				)
     UpperCAmelCase__						:    Union[str, Any]							=							real_input_matrix.astype(np.complexaaa				)
     UpperCAmelCase__						:    Optional[int]							=							np.triu(1J * complex_input_matrix			,					1				)
     complex_input_matrix += imag_matrix
     complex_input_matrix += -1 * imag_matrix.T
     UpperCAmelCase__						:    int							=							np.array([41, 4, 20]				).astype(np.complexaaa				)

     for problem_type in ["real", "complex"]:
          if problem_type == "real":
               UpperCAmelCase__						:    List[str]							=							real_input_matrix
               UpperCAmelCase__						:    Any							=							real_vector
          elif problem_type == "complex":
               UpperCAmelCase__						:    str							=							complex_input_matrix
               UpperCAmelCase__						:    List[Any]							=							complex_vector

          # Our implementation.
          UpperCAmelCase__							,				UpperCAmelCase__						:    Optional[int]							=							power_iteration(snake_case			,					snake_case				)

          # Numpy implementation.

          # Get eigenvalues and eigenvectors using built-in numpy
          # eigh (eigh used for symmetric or hermetian matrices).
          UpperCAmelCase__							,				UpperCAmelCase__						:    List[Any]							=							np.linalg.eigh(snake_case				)
          # Last eigenvalue is the maximum one.
          UpperCAmelCase__						:    str							=							eigen_values[-1]
          # Last column in this matrix is eigenvector corresponding to largest eigenvalue.
          UpperCAmelCase__						:    Optional[int]							=							eigen_vectors[:, -1]

          # Check our implementation and numpy gives close answers.
          assert np.abs(eigen_value - eigen_value_max				) <= 1E-6
          # Take absolute values element wise of each eigenvector.
          # as they are only unique to a minus sign.
          assert np.linalg.norm(np.abs(snake_case				) - np.abs(snake_case				)				) <= 1E-6


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 test_power_iteration()


