






from __future__ import annotations




class       snake_case__ :

       '''simple docstring'''



       def __init__(				self      ,    a__     )			->   str:






            '''simple docstring'''





            __snake_case      :Tuple 							=    TypeError(
                """Matrices must be formed from a list of zero or more lists containing at """
                """least one and the same number of values, each of which must be of type """
                """int or float."""     )
            if len(a__     ) != 0:
                 __snake_case      :Optional[Any] 							=    len(rows[0]     )
                 if cols == 0:
                      raise error
                 for row in rows:
                      if len(a__     ) != cols:
                           raise error
                      for value in row:
                           if not isinstance(a__      ,    (int, float)     ):
                                raise error
                 __snake_case      :int 							=    rows
            else:
                 __snake_case      :Any 							=    []



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






            '''simple docstring'''





            return [[row[i] for row in self.rows] for i in range(len(self.rows[0]     )     )]



       @property
       def        __lowercase    (				self     )			->   int:






            '''simple docstring'''





            return len(self.rows     )



       @property
       def        __lowercase    (				self     )			->   int:






            '''simple docstring'''





            return len(self.rows[0]     )



       @property
       def        __lowercase    (				self     )			->   tuple[int, int]:






            '''simple docstring'''





            return (self.num_rows, self.num_columns)



       @property
       def        __lowercase    (				self     )			->   bool:






            '''simple docstring'''





            return self.order[0] == self.order[1]



       def        __lowercase    (				self     )			->   Matrix:






            '''simple docstring'''





            __snake_case      :Dict 							=    [
                [0 if column_num != row_num else 1 for column_num in range(self.num_rows     )]
                for row_num in range(self.num_rows     )
            ]
            return Matrix(a__     )



       def        __lowercase    (				self     )			->   int:






            '''simple docstring'''





            if not self.is_square:
                 return 0
            if self.order == (0, 0):
                 return 1
            if self.order == (1, 1):
                 return int(self.rows[0][0]     )
            if self.order == (2, 2):
                 return int(
                     (self.rows[0][0] * self.rows[1][1])
                     - (self.rows[0][1] * self.rows[1][0])     )
            else:
                 return sum(
                     self.rows[0][column] * self.cofactors().rows[0][column]
                     for column in range(self.num_columns     )     )



       def        __lowercase    (				self     )			->   bool:






            '''simple docstring'''





            return bool(self.determinant()     )



       def        __lowercase    (				self      ,    a__      ,    a__     )			->   int:






            '''simple docstring'''





            __snake_case      :List[str] 							=    [
                [
                    self.rows[other_row][other_column]
                    for other_column in range(self.num_columns     )
                    if other_column != column
                ]
                for other_row in range(self.num_rows     )
                if other_row != row
            ]
            return Matrix(a__     ).determinant()



       def        __lowercase    (				self      ,    a__      ,    a__     )			->   int:






            '''simple docstring'''





            if (row + column) % 2 == 0:
                 return self.get_minor(a__      ,    a__     )
            return -1 * self.get_minor(a__      ,    a__     )



       def        __lowercase    (				self     )			->   Matrix:






            '''simple docstring'''





            return Matrix(
                [
                    [self.get_minor(a__      ,    a__     ) for column in range(self.num_columns     )]
                    for row in range(self.num_rows     )
                ]     )



       def        __lowercase    (				self     )			->   Matrix:






            '''simple docstring'''





            return Matrix(
                [
                    [
                        self.minors().rows[row][column]
                        if (row + column) % 2 == 0
                        else self.minors().rows[row][column] * -1
                        for column in range(self.minors().num_columns     )
                    ]
                    for row in range(self.minors().num_rows     )
                ]     )



       def        __lowercase    (				self     )			->   Matrix:






            '''simple docstring'''





            __snake_case      :int 							=    [
                [self.cofactors().rows[column][row] for column in range(self.num_columns     )]
                for row in range(self.num_rows     )
            ]
            return Matrix(a__     )



       def        __lowercase    (				self     )			->   Matrix:






            '''simple docstring'''





            __snake_case      :Optional[int] 							=    self.determinant()
            if not determinant:
                 raise TypeError("""Only matrices with a non-zero determinant have an inverse"""     )
            return self.adjugate() * (1 / determinant)



       def __repr__(				self     )			->   str:






            '''simple docstring'''





            return str(self.rows     )



       def __str__(				self     )			->   str:






            '''simple docstring'''





            if self.num_rows == 0:
                 return "[]"
            if self.num_rows == 1:
                 return "[[" + ". ".join(str(self.rows[0]     )     ) + "]]"
            return (
                "["
                + "\n ".join(
                    [
                        """[""" + """. """.join([str(a__     ) for value in row]     ) + """.]"""
                        for row in self.rows
                    ]     )
                + "]"
            )



       def        __lowercase    (				self      ,    a__      ,    a__ = None     )			->   None:






            '''simple docstring'''





            __snake_case      :int 							=    TypeError("""Row must be a list containing all ints and/or floats"""     )
            if not isinstance(a__      ,    a__     ):
                 raise type_error
            for value in row:
                 if not isinstance(a__      ,    (int, float)     ):
                      raise type_error
            if len(a__     ) != self.num_columns:
                 raise ValueError(
                     """Row must be equal in length to the other rows in the matrix"""     )
            if position is None:
                 self.rows.append(a__     )
            else:
                 __snake_case      :int 							=    self.rows[0:position] + [row] + self.rows[position:]



       def        __lowercase    (				self      ,    a__      ,    a__ = None     )			->   None:






            '''simple docstring'''





            __snake_case      :Optional[Any] 							=    TypeError(
                """Column must be a list containing all ints and/or floats"""     )
            if not isinstance(a__      ,    a__     ):
                 raise type_error
            for value in column:
                 if not isinstance(a__      ,    (int, float)     ):
                      raise type_error
            if len(a__     ) != self.num_rows:
                 raise ValueError(
                     """Column must be equal in length to the other columns in the matrix"""     )
            if position is None:
                 __snake_case      :int 							=    [self.rows[i] + [column[i]] for i in range(self.num_rows     )]
            else:
                 __snake_case      :List[str] 							=    [
                     self.rows[i][0:position] + [column[i]] + self.rows[i][position:]
                     for i in range(self.num_rows     )
                 ]



       def __eq__(				self      ,    a__     )			->   bool:






            '''simple docstring'''





            if not isinstance(a__      ,    a__     ):
                 return NotImplemented
            return self.rows == other.rows



       def __ne__(				self      ,    a__     )			->   bool:






            '''simple docstring'''





            return not self == other



       def __neg__(				self     )			->   Matrix:






            '''simple docstring'''





            return self * -1



       def __add__(				self      ,    a__     )			->   Matrix:






            '''simple docstring'''





            if self.order != other.order:
                 raise ValueError("""Addition requires matrices of the same order"""     )
            return Matrix(
                [
                    [self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns     )]
                    for i in range(self.num_rows     )
                ]     )



       def __sub__(				self      ,    a__     )			->   Matrix:






            '''simple docstring'''





            if self.order != other.order:
                 raise ValueError("""Subtraction requires matrices of the same order"""     )
            return Matrix(
                [
                    [self.rows[i][j] - other.rows[i][j] for j in range(self.num_columns     )]
                    for i in range(self.num_rows     )
                ]     )



       def __mul__(				self      ,    a__     )			->   Matrix:






            '''simple docstring'''





            if isinstance(a__      ,    (int, float)     ):
                 return Matrix(
                     [[int(element * other     ) for element in row] for row in self.rows]     )
            elif isinstance(a__      ,    a__     ):
                 if self.num_columns != other.num_rows:
                      raise ValueError(
                          """The number of columns in the first matrix must """
                          """be equal to the number of rows in the second"""     )
                 return Matrix(
                     [
                         [Matrix.dot_product(a__      ,    a__     ) for column in other.columns()]
                         for row in self.rows
                     ]     )
            else:
                 raise TypeError(
                     """A Matrix can only be multiplied by an int, float, or another matrix"""     )



       def __pow__(				self      ,    a__     )			->   Matrix:






            '''simple docstring'''





            if not isinstance(a__      ,    a__     ):
                 raise TypeError("""A Matrix can only be raised to the power of an int"""     )
            if not self.is_square:
                 raise ValueError("""Only square matrices can be raised to a power"""     )
            if other == 0:
                 return self.identity()
            if other < 0:
                 if self.is_invertable():
                      return self.inverse() ** (-other)
                 raise ValueError(
                     """Only invertable matrices can be raised to a negative power"""     )
            __snake_case      :int 							=    self
            for _ in range(other - 1     ):
                 result *= self
            return result



       @classmethod
       def        __lowercase    (				cls      ,    a__      ,    a__     )			->   int:






            '''simple docstring'''





            return sum(row[i] * column[i] for i in range(len(a__     )     )     )


if __name__ == "__main__":
     import doctest

     doctest.testmod()










import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()




class       snake_case__ (				lowercase_						,     unittest.TestCase):

       '''simple docstring'''
       lowerCamelCase				:					Tuple								=			KandinskyVaaPriorPipeline
       lowerCamelCase				:					Optional[Any]								=			["prompt"]
       lowerCamelCase				:					Any								=			["prompt", "negative_prompt"]
       lowerCamelCase				:					Tuple								=			[
           "num_images_per_prompt",
           "generator",
           "num_inference_steps",
           "latents",
           "negative_prompt",
           "guidance_scale",
           "output_type",
           "return_dict",
       ]
       lowerCamelCase				:					Any								=			False



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






            '''simple docstring'''





            return 32



       @property
       def        __lowercase    (				self     )			->   Any:






            '''simple docstring'''





            return 32



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






            '''simple docstring'''





            return self.time_input_dim



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






            '''simple docstring'''





            return self.time_input_dim * 4



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






            '''simple docstring'''





            return 1_00



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






            '''simple docstring'''





            __snake_case      :Tuple 							=    CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""     )
            return tokenizer



       @property
       def        __lowercase    (				self     )			->   str:






            '''simple docstring'''





            torch.manual_seed(0     )
            __snake_case      :Dict 							=    CLIPTextConfig(
                bos_token_id=0      ,    eos_token_id=2      ,    hidden_size=self.text_embedder_hidden_size      ,    projection_dim=self.text_embedder_hidden_size      ,    intermediate_size=37      ,    layer_norm_eps=1e-05      ,    num_attention_heads=4      ,    num_hidden_layers=5      ,    pad_token_id=1      ,    vocab_size=10_00      ,    )
            return CLIPTextModelWithProjection(a__     )



       @property
       def        __lowercase    (				self     )			->   str:






            '''simple docstring'''





            torch.manual_seed(0     )

            __snake_case      :List[Any] 							=    {
                """num_attention_heads""": 2,
                """attention_head_dim""": 12,
                """embedding_dim""": self.text_embedder_hidden_size,
                """num_layers""": 1,
            }

            __snake_case      :Union[str, Any] 							=    PriorTransformer(**a__     )
            # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
            __snake_case      :int 							=    nn.Parameter(torch.ones(model.clip_std.shape     )     )
            return model



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






            '''simple docstring'''





            torch.manual_seed(0     )
            __snake_case      :List[Any] 							=    CLIPVisionConfig(
                hidden_size=self.text_embedder_hidden_size      ,    image_size=2_24      ,    projection_dim=self.text_embedder_hidden_size      ,    intermediate_size=37      ,    num_attention_heads=4      ,    num_channels=3      ,    num_hidden_layers=5      ,    patch_size=14      ,    )

            __snake_case      :Tuple 							=    CLIPVisionModelWithProjection(a__     )
            return model



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






            '''simple docstring'''





            __snake_case      :Optional[int] 							=    CLIPImageProcessor(
                crop_size=2_24      ,    do_center_crop=a__      ,    do_normalize=a__      ,    do_resize=a__      ,    image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]      ,    image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]      ,    resample=3      ,    size=2_24      ,    )

            return image_processor



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






            '''simple docstring'''





            __snake_case      :Optional[Any] 							=    self.dummy_prior
            __snake_case      :Tuple 							=    self.dummy_image_encoder
            __snake_case      :List[Any] 							=    self.dummy_text_encoder
            __snake_case      :List[str] 							=    self.dummy_tokenizer
            __snake_case      :int 							=    self.dummy_image_processor

            __snake_case      :Union[str, Any] 							=    UnCLIPScheduler(
                variance_type="""fixed_small_log"""      ,    prediction_type="""sample"""      ,    num_train_timesteps=10_00      ,    clip_sample=a__      ,    clip_sample_range=10.0      ,    )

            __snake_case      :Union[str, Any] 							=    {
                """prior""": prior,
                """image_encoder""": image_encoder,
                """text_encoder""": text_encoder,
                """tokenizer""": tokenizer,
                """scheduler""": scheduler,
                """image_processor""": image_processor,
            }

            return components



       def        __lowercase    (				self      ,    a__      ,    a__=0     )			->   Optional[Any]:






            '''simple docstring'''





            if str(a__     ).startswith("""mps"""     ):
                 __snake_case      :Any 							=    torch.manual_seed(a__     )
            else:
                 __snake_case      :Any 							=    torch.Generator(device=a__     ).manual_seed(a__     )
            __snake_case      :Union[str, Any] 							=    {
                """prompt""": """horse""",
                """generator""": generator,
                """guidance_scale""": 4.0,
                """num_inference_steps""": 2,
                """output_type""": """np""",
            }
            return inputs



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






            '''simple docstring'''





            __snake_case      :str 							=    """cpu"""

            __snake_case      :Optional[Any] 							=    self.get_dummy_components()

            __snake_case      :List[Any] 							=    self.pipeline_class(**a__     )
            __snake_case      :Union[str, Any] 							=    pipe.to(a__     )

            pipe.set_progress_bar_config(disable=a__     )

            __snake_case      :List[Any] 							=    pipe(**self.get_dummy_inputs(a__     )     )
            __snake_case      :Any 							=    output.image_embeds

            __snake_case      :Any 							=    pipe(
                **self.get_dummy_inputs(a__     )      ,    return_dict=a__      ,    )[0]

            __snake_case      :Any 							=    image[0, -10:]
            __snake_case      :Any 							=    image_from_tuple[0, -10:]

            assert image.shape == (1, 32)

            __snake_case      :List[str] 							=    np.array(
                [-0.05_32, 1.71_20, 0.36_56, -1.08_52, -0.89_46, -1.17_56, 0.43_48, 0.24_82, 0.51_46, -0.11_56]     )

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



       @skip_mps
       def        __lowercase    (				self     )			->   str:






            '''simple docstring'''





            __snake_case      :List[Any] 							=    torch_device == """cpu"""
            __snake_case      :List[str] 							=    True
            __snake_case      :Optional[Any] 							=    False

            self._test_inference_batch_single_identical(
                test_max_difference=a__      ,    relax_max_difference=a__      ,    test_mean_pixel_difference=a__      ,    )



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






            '''simple docstring'''





            __snake_case      :Any 							=    torch_device == """cpu"""
            __snake_case      :str 							=    False

            self._test_attention_slicing_forward_pass(
                test_max_difference=a__      ,    test_mean_pixel_difference=a__      ,    )


