




"""simple docstring"""





from __future__ import annotations

import math

def        _UpperCamelCase     (      UpperCamelCase					,							UpperCamelCase				)      ->      list:


			"""simple docstring"""
			if len(UpperCamelCase				) != 2 or len(a[0]				) != 2 or len(UpperCamelCase				) != 2 or len(b[0]				) != 2:
						raise Exception("Matrices are not 2x2"				)
			__UpperCAmelCase						:			Optional[Any]												=       [
			    [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]],
			    [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]],
			]
			return new_matrix

def        _UpperCamelCase     (      UpperCamelCase					,							UpperCamelCase				)      ->      Dict:


			"""simple docstring"""
			return [
			    [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row]				)				)]
			    for row in range(len(UpperCamelCase				)				)
			]

def        _UpperCamelCase     (      UpperCamelCase					,							UpperCamelCase				)      ->      Optional[Any]:


			"""simple docstring"""
			return [
			    [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row]				)				)]
			    for row in range(len(UpperCamelCase				)				)
			]

def        _UpperCamelCase     (      UpperCamelCase				)      ->      tuple[list, list, list, list]:


			"""simple docstring"""
			if len(UpperCamelCase				) % 2 != 0 or len(a[0]				) % 2 != 0:
						raise Exception("Odd matrices are not supported!"				)

			__UpperCAmelCase						:			List[str]												=       len(UpperCamelCase				)
			__UpperCAmelCase						:			int												=       matrix_length // 2

			__UpperCAmelCase						:			Tuple												=       [[a[i][j] for j in range(UpperCamelCase					,							UpperCamelCase				)] for i in range(UpperCamelCase				)]
			__UpperCAmelCase						:			Dict												=       [
			    [a[i][j] for j in range(UpperCamelCase					,							UpperCamelCase				)] for i in range(UpperCamelCase					,							UpperCamelCase				)
			]

			__UpperCAmelCase						:			List[Any]												=       [[a[i][j] for j in range(UpperCamelCase				)] for i in range(UpperCamelCase				)]
			__UpperCAmelCase						:			Any												=       [[a[i][j] for j in range(UpperCamelCase				)] for i in range(UpperCamelCase					,							UpperCamelCase				)]

			return top_left, top_right, bot_left, bot_right

def        _UpperCamelCase     (      UpperCamelCase				)      ->      tuple[int, int]:


			"""simple docstring"""
			return len(UpperCamelCase				), len(matrix[0]				)

def        _UpperCamelCase     (      UpperCamelCase				)      ->      None:


			"""simple docstring"""
			print("\n".join(str(UpperCamelCase				) for line in matrix				)				)

def        _UpperCamelCase     (      UpperCamelCase					,							UpperCamelCase				)      ->      list:


			"""simple docstring"""
			if matrix_dimensions(UpperCamelCase				) == (2, 2):
						return default_matrix_multiplication(UpperCamelCase					,							UpperCamelCase				)

			__UpperCAmelCase						,  __UpperCAmelCase						,  __UpperCAmelCase						,  __UpperCAmelCase						:			Union[str, Any]												=       split_matrix(UpperCamelCase				)
			__UpperCAmelCase						,  __UpperCAmelCase						,  __UpperCAmelCase						,  __UpperCAmelCase						:			Tuple												=       split_matrix(UpperCamelCase				)

			__UpperCAmelCase						:			Dict												=       actual_strassen(UpperCamelCase					,							matrix_subtraction(UpperCamelCase					,							UpperCamelCase				)				)
			__UpperCAmelCase						:			str												=       actual_strassen(matrix_addition(UpperCamelCase					,							UpperCamelCase				)					,							UpperCamelCase				)
			__UpperCAmelCase						:			Optional[Any]												=       actual_strassen(matrix_addition(UpperCamelCase					,							UpperCamelCase				)					,							UpperCamelCase				)
			__UpperCAmelCase						:			Optional[Any]												=       actual_strassen(UpperCamelCase					,							matrix_subtraction(UpperCamelCase					,							UpperCamelCase				)				)
			__UpperCAmelCase						:			Optional[Any]												=       actual_strassen(matrix_addition(UpperCamelCase					,							UpperCamelCase				)					,							matrix_addition(UpperCamelCase					,							UpperCamelCase				)				)
			__UpperCAmelCase						:			str												=       actual_strassen(matrix_subtraction(UpperCamelCase					,							UpperCamelCase				)					,							matrix_addition(UpperCamelCase					,							UpperCamelCase				)				)
			__UpperCAmelCase						:			Dict												=       actual_strassen(matrix_subtraction(UpperCamelCase					,							UpperCamelCase				)					,							matrix_addition(UpperCamelCase					,							UpperCamelCase				)				)

			__UpperCAmelCase						:			List[Any]												=       matrix_addition(matrix_subtraction(matrix_addition(UpperCamelCase					,							UpperCamelCase				)					,							UpperCamelCase				)					,							UpperCamelCase				)
			__UpperCAmelCase						:			Dict												=       matrix_addition(UpperCamelCase					,							UpperCamelCase				)
			__UpperCAmelCase						:			List[str]												=       matrix_addition(UpperCamelCase					,							UpperCamelCase				)
			__UpperCAmelCase						:			Any												=       matrix_subtraction(matrix_subtraction(matrix_addition(UpperCamelCase					,							UpperCamelCase				)					,							UpperCamelCase				)					,							UpperCamelCase				)

			# construct the new matrix from our 4 quadrants
			__UpperCAmelCase						:			str												=       []
			for i in range(len(UpperCamelCase				)				):
						new_matrix.append(top_left[i] + top_right[i]				)
			for i in range(len(UpperCamelCase				)				):
						new_matrix.append(bot_left[i] + bot_right[i]				)
			return new_matrix

def        _UpperCamelCase     (      UpperCamelCase					,							UpperCamelCase				)      ->      list:


			"""simple docstring"""
			if matrix_dimensions(UpperCamelCase				)[1] != matrix_dimensions(UpperCamelCase				)[0]:
						__UpperCAmelCase						:			List[str]												=       (
						    "Unable to multiply these matrices, please check the dimensions.\n"
						    f"Matrix A: {matrixa}\n"
						    f"Matrix B: {matrixa}"
						)
						raise Exception(UpperCamelCase				)
			__UpperCAmelCase						:			int												=       matrix_dimensions(UpperCamelCase				)
			__UpperCAmelCase						:			Any												=       matrix_dimensions(UpperCamelCase				)

			if dimensiona[0] == dimensiona[1] and dimensiona[0] == dimensiona[1]:
						return [matrixa, matrixa]

			__UpperCAmelCase						:			Optional[int]												=       max(*UpperCamelCase					,							*UpperCamelCase				)
			__UpperCAmelCase						:			Optional[Any]												=       int(math.pow(2					,							math.ceil(math.loga(UpperCamelCase				)				)				)				)
			__UpperCAmelCase						:			List[str]												=       matrixa
			__UpperCAmelCase						:			Union[str, Any]												=       matrixa

			# Adding zeros to the matrices so that the arrays dimensions are the same and also
			# power of 2
			for i in range(0					,							UpperCamelCase				):
						if i < dimensiona[0]:
									for _ in range(dimensiona[1]					,							UpperCamelCase				):
												new_matrixa[i].append(0				)
						else:
									new_matrixa.append([0] * maxim				)
						if i < dimensiona[0]:
									for _ in range(dimensiona[1]					,							UpperCamelCase				):
												new_matrixa[i].append(0				)
						else:
									new_matrixa.append([0] * maxim				)

			__UpperCAmelCase						:			str												=       actual_strassen(UpperCamelCase					,							UpperCamelCase				)

			# Removing the additional zeros
			for i in range(0					,							UpperCamelCase				):
						if i < dimensiona[0]:
									for _ in range(dimensiona[1]					,							UpperCamelCase				):
												final_matrix[i].pop()
						else:
									final_matrix.pop()
			return final_matrix


if __name__ == "__main__":
					A				=							[
					    [2, 3, 4, 5],
					    [6, 4, 3, 1],
					    [2, 3, 6, 7],
					    [3, 1, 2, 4],
					    [2, 3, 4, 5],
					    [6, 4, 3, 1],
					    [2, 3, 6, 7],
					    [3, 1, 2, 4],
					    [2, 3, 4, 5],
					    [6, 2, 3, 1],
					]
					A				=							[[0, 2, 1, 1], [16, 2, 3, 3], [2, 2, 7, 7], [13, 11, 22, 4]]
					print(strassen(matrixa, matrixa))









"""simple docstring"""





import gc
import unittest

import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DDPMScheduler,
    PriorTransformer,
    StableUnCLIPPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer
from diffusers.utils.testing_utils import enable_full_determinism, load_numpy, require_torch_gpu, slow, torch_device

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,
    assert_mean_pixel_difference,
)


enable_full_determinism()






class    _lowerCAmelCase  (	a    ,  a    ,  a    ,  unittest.TestCase			):
       """simple docstring"""




       __magic_name__	:int						    =   StableUnCLIPPipeline
       __magic_name__	:int						    =   TEXT_TO_IMAGE_PARAMS
       __magic_name__	:List[str]						    =   TEXT_TO_IMAGE_BATCH_PARAMS
       __magic_name__	:Optional[int]						    =   TEXT_TO_IMAGE_IMAGE_PARAMS
       __magic_name__	:Union[str, Any]						    =   TEXT_TO_IMAGE_IMAGE_PARAMS

       # TODO(will) Expected attn_bias.stride(1) == 0 to be true, but got false
       __magic_name__	:List[str]						    =   False






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Dict											=				3_2
         lowerCAmelCase__     :List[Any]											=				embedder_hidden_size

         # prior components

         torch.manual_seed(0 )
         lowerCAmelCase__     :Any											=				CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' )

         torch.manual_seed(0 )
         lowerCAmelCase__     :Any											=				CLIPTextModelWithProjection(
             CLIPTextConfig(
                 bos_token_id=0       ,					eos_token_id=2       ,					hidden_size=__UpperCAmelCase       ,					projection_dim=__UpperCAmelCase       ,					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       ,					) )

         torch.manual_seed(0 )
         lowerCAmelCase__     :int											=				PriorTransformer(
             num_attention_heads=2       ,					attention_head_dim=1_2       ,					embedding_dim=__UpperCAmelCase       ,					num_layers=1       ,					)

         torch.manual_seed(0 )
         lowerCAmelCase__     :Optional[int]											=				DDPMScheduler(
             variance_type='fixed_small_log'       ,					prediction_type='sample'       ,					num_train_timesteps=1_0_0_0       ,					clip_sample=__UpperCAmelCase       ,					clip_sample_range=5.0       ,					beta_schedule='squaredcos_cap_v2'       ,					)

         # regular denoising components

         torch.manual_seed(0 )
         lowerCAmelCase__     :Dict											=				StableUnCLIPImageNormalizer(embedding_dim=__UpperCAmelCase )
         lowerCAmelCase__     :List[Any]											=				DDPMScheduler(beta_schedule='squaredcos_cap_v2' )

         torch.manual_seed(0 )
         lowerCAmelCase__     :List[Any]											=				CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' )

         torch.manual_seed(0 )
         lowerCAmelCase__     :str											=				CLIPTextModel(
             CLIPTextConfig(
                 bos_token_id=0       ,					eos_token_id=2       ,					hidden_size=__UpperCAmelCase       ,					projection_dim=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       ,					) )

         torch.manual_seed(0 )
         lowerCAmelCase__     :Optional[int]											=				UNetaDConditionModel(
             sample_size=3_2       ,					in_channels=4       ,					out_channels=4       ,					down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D')       ,					up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D')       ,					block_out_channels=(3_2, 6_4)       ,					attention_head_dim=(2, 4)       ,					class_embed_type='projection'       ,					projection_class_embeddings_input_dim=embedder_projection_dim * 2       ,					cross_attention_dim=__UpperCAmelCase       ,					layers_per_block=1       ,					upcast_attention=__UpperCAmelCase       ,					use_linear_projection=__UpperCAmelCase       ,					)

         torch.manual_seed(0 )
         lowerCAmelCase__     :Tuple											=				DDIMScheduler(
             beta_schedule='scaled_linear'       ,					beta_start=0.0_00_85       ,					beta_end=0.0_12       ,					prediction_type='v_prediction'       ,					set_alpha_to_one=__UpperCAmelCase       ,					steps_offset=1       ,					)

         torch.manual_seed(0 )
         lowerCAmelCase__     :Optional[int]											=				AutoencoderKL()

         lowerCAmelCase__     :Optional[int]											=				{
             # prior components
             'prior_tokenizer': prior_tokenizer,
             'prior_text_encoder': prior_text_encoder,
             'prior': prior,
             'prior_scheduler': prior_scheduler,
             # image noising components
             'image_normalizer': image_normalizer,
             'image_noising_scheduler': image_noising_scheduler,
             # regular denoising components
             'tokenizer': tokenizer,
             'text_encoder': text_encoder,
             'unet': unet,
             'scheduler': scheduler,
             'vae': vae,
         }

         return components






       def 	snake_case		(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase=0 ):



         '''simple docstring'''

         if str(__UpperCAmelCase ).startswith('mps' ):
           lowerCAmelCase__     :str											=				torch.manual_seed(__UpperCAmelCase )
         else:
           lowerCAmelCase__     :Optional[int]											=				torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase )
         lowerCAmelCase__     :Tuple											=				{
             'prompt': 'A painting of a squirrel eating a burger',
             'generator': generator,
             'num_inference_steps': 2,
             'prior_num_inference_steps': 2,
             'output_type': 'numpy',
         }
         return inputs






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[int]											=				torch_device == 'cpu'

         self._test_attention_slicing_forward_pass(test_max_difference=__UpperCAmelCase )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :str											=				torch_device in ['cpu', 'mps']

         self._test_inference_batch_single_identical(test_max_difference=__UpperCAmelCase )






@slow
@require_torch_gpu
class    _lowerCAmelCase  (	unittest.TestCase			):
       """simple docstring"""




       def 	snake_case		(       self ):



         '''simple docstring'''

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






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Tuple											=				load_numpy(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_anime_turtle_fp16.npy' )

         lowerCAmelCase__     :List[Any]											=				StableUnCLIPPipeline.from_pretrained('fusing/stable-unclip-2-1-l'       ,					torch_dtype=torch.floataa )
         pipe.to(__UpperCAmelCase )
         pipe.set_progress_bar_config(disable=__UpperCAmelCase )
         # stable unclip will oom when integration tests are run on a V100,
         # so turn on memory savings
         pipe.enable_attention_slicing()
         pipe.enable_sequential_cpu_offload()

         lowerCAmelCase__     :List[str]											=				torch.Generator(device='cpu' ).manual_seed(0 )
         lowerCAmelCase__     :List[str]											=				pipe('anime turle'       ,					generator=__UpperCAmelCase       ,					output_type='np' )

         lowerCAmelCase__     :Union[str, Any]											=				output.images[0]

         assert image.shape == (7_6_8, 7_6_8, 3)

         assert_mean_pixel_difference(__UpperCAmelCase       ,					__UpperCAmelCase )






       def 	snake_case		(       self ):



         '''simple docstring'''

         torch.cuda.empty_cache()
         torch.cuda.reset_max_memory_allocated()
         torch.cuda.reset_peak_memory_stats()

         lowerCAmelCase__     :int											=				StableUnCLIPPipeline.from_pretrained('fusing/stable-unclip-2-1-l'       ,					torch_dtype=torch.floataa )
         lowerCAmelCase__     :List[str]											=				pipe.to(__UpperCAmelCase )
         pipe.set_progress_bar_config(disable=__UpperCAmelCase )
         pipe.enable_attention_slicing()
         pipe.enable_sequential_cpu_offload()

         lowerCAmelCase__     :Tuple											=				pipe(
             'anime turtle'       ,					prior_num_inference_steps=2       ,					num_inference_steps=2       ,					output_type='np'       ,					)

         lowerCAmelCase__     :str											=				torch.cuda.max_memory_allocated()
         # make sure that less than 7 GB is allocated
         assert mem_bytes < 7 * 1_0**9
