

"""simple docstring"""




import random
import unittest

import numpy as np
import torch

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionUpscalePipeline,
    PNDMScheduler,
)
from diffusers.utils import floats_tensor
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
      import onnxruntime as ort







class      __lowercase			(    __lowerCamelCase	,			unittest.TestCase					):
  # TODO: is there an appropriate internal test set?
  snake_case_				       =       """ssube/stable-diffusion-x4-upscaler-onnx"""

  def    __lowercase       (  self		:			Union[str, Any]					,A		:			Optional[Any]=0    ):






   '''simple docstring'''
   UpperCAmelCase__					:  int					   =					floats_tensor((1, 3, 128, 128)					,rng=random.Random(A    )    )
   UpperCAmelCase__					:  int					   =					torch.manual_seed(A    )
   UpperCAmelCase__					:  List[Any]					   =					{
       """prompt""": """A painting of a squirrel eating a burger""",
       """image""": image,
       """generator""": generator,
       """num_inference_steps""": 3,
       """guidance_scale""": 7.5,
       """output_type""": """numpy""",
   }
   return inputs

  def    __lowercase       (  self		:			Any    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint					,provider="""CPUExecutionProvider"""    )
   pipe.set_progress_bar_config(disable=A    )

   UpperCAmelCase__					:  List[Any]					   =					self.get_dummy_inputs()
   UpperCAmelCase__					:  List[str]					   =					pipe(**A    ).images
   UpperCAmelCase__					:  Any					   =					image[0, -3:, -3:, -1].flatten()

   # started as 128, should now be 512
   assert image.shape == (1, 512, 512, 3)
   UpperCAmelCase__					:  Optional[int]					   =					np.array(
       [0.6_9_7_4_7_8_2, 0.6_8_9_0_2_0_9_3, 0.7_0_1_3_5_8_8_5, 0.7_5_8_3_6_1_8, 0.7_8_0_4_5_4_5, 0.7_8_5_4_9_1_2, 0.7_8_6_6_7_4_2_6, 0.7_8_7_4_3_8_6_3, 0.7_8_0_7_0_2_2_3]    )
   assert np.abs(image_slice - expected_slice    ).max() < 1e-1

  def    __lowercase       (  self		:			Union[str, Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint					,provider="""CPUExecutionProvider"""    )
   UpperCAmelCase__					:  Dict					   =					PNDMScheduler.from_config(pipe.scheduler.config					,skip_prk_steps=A    )
   pipe.set_progress_bar_config(disable=A    )

   UpperCAmelCase__					:  Optional[Any]					   =					self.get_dummy_inputs()
   UpperCAmelCase__					:  Optional[int]					   =					pipe(**A    ).images
   UpperCAmelCase__					:  Optional[Any]					   =					image[0, -3:, -3:, -1]

   assert image.shape == (1, 512, 512, 3)
   UpperCAmelCase__					:  str					   =					np.array(
       [0.6_8_9_8_8_9_2, 0.5_9_2_4_0_5_5_6, 0.5_2_4_9_9_5_2_7, 0.5_8_8_6_6_2_1_5, 0.5_2_2_5_8_2_3_5, 0.5_2_5_7_2_7_1_5, 0.6_2_4_1_4_4_7_3, 0.6_1_7_4_3_8_7, 0.6_2_1_4_9_6_4]    )
   assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-1

  def    __lowercase       (  self		:			List[str]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Tuple					   =					OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint					,provider="""CPUExecutionProvider"""    )
   UpperCAmelCase__					:  Any					   =					DPMSolverMultistepScheduler.from_config(pipe.scheduler.config    )
   pipe.set_progress_bar_config(disable=A    )

   UpperCAmelCase__					:  Union[str, Any]					   =					self.get_dummy_inputs()
   UpperCAmelCase__					:  Optional[int]					   =					pipe(**A    ).images
   UpperCAmelCase__					:  Any					   =					image[0, -3:, -3:, -1]

   assert image.shape == (1, 512, 512, 3)
   UpperCAmelCase__					:  Optional[Any]					   =					np.array(
       [0.7_6_5_9_2_7_8, 0.7_6_4_3_7_6_6_4, 0.7_5_5_7_9_1_0_7, 0.7_6_9_1_1_1_6, 0.7_7_6_6_6_9_8_6, 0.7_7_2_7_6_7_2, 0.7_7_5_8_6_6_4, 0.7_8_1_2_2_2_6, 0.7_6_9_4_2_5_1_5]    )

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

  def    __lowercase       (  self		:			Union[str, Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint					,provider="""CPUExecutionProvider"""    )
   UpperCAmelCase__					:  Dict					   =					EulerDiscreteScheduler.from_config(pipe.scheduler.config    )
   pipe.set_progress_bar_config(disable=A    )

   UpperCAmelCase__					:  Optional[int]					   =					self.get_dummy_inputs()
   UpperCAmelCase__					:  Any					   =					pipe(**A    ).images
   UpperCAmelCase__					:  str					   =					image[0, -3:, -3:, -1]

   assert image.shape == (1, 512, 512, 3)
   UpperCAmelCase__					:  Optional[Any]					   =					np.array(
       [0.6_9_7_4_7_8_2, 0.6_8_9_0_2_0_9_3, 0.7_0_1_3_5_8_8_5, 0.7_5_8_3_6_1_8, 0.7_8_0_4_5_4_5, 0.7_8_5_4_9_1_2, 0.7_8_6_6_7_4_2_6, 0.7_8_7_4_3_8_6_3, 0.7_8_0_7_0_2_2_3]    )
   assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-1




  def    __lowercase       (  self		:			Tuple    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Union[str, Any]					   =					OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint					,provider="""CPUExecutionProvider"""    )
   UpperCAmelCase__					:  str					   =					EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config    )
   pipe.set_progress_bar_config(disable=A    )

   UpperCAmelCase__					:  str					   =					self.get_dummy_inputs()
   UpperCAmelCase__					:  List[str]					   =					pipe(**A    ).images
   UpperCAmelCase__					:  int					   =					image[0, -3:, -3:, -1]

   assert image.shape == (1, 512, 512, 3)
   UpperCAmelCase__					:  Optional[int]					   =					np.array(
       [0.7_7_4_2_4_4_9_6, 0.7_7_3_6_0_1, 0.7_6_4_5_2_8_8, 0.7_7_6_9_5_9_8, 0.7_7_7_2_7_3_9, 0.7_7_3_8_6_8_8, 0.7_8_1_8_7_2_3_3, 0.7_7_8_7_9_5_8_4, 0.7_6_7_0_4_3]    )

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







@nightly
@require_onnxruntime
@require_torch_gpu
class      __lowercase			(    unittest.TestCase					):

  @property
  def    __lowercase       (  self		:			Tuple    ):






   '''simple docstring'''
   return (
       "CUDAExecutionProvider",
       {
           "gpu_mem_limit": "15000000000",  # 15GB
           "arena_extend_strategy": "kSameAsRequested",
       },
   )

  @property
  def    __lowercase       (  self		:			Dict    ):






   '''simple docstring'''
   UpperCAmelCase__					:  int					   =					ort.SessionOptions()
   UpperCAmelCase__					:  List[Any]					   =					False
   return options

  def    __lowercase       (  self		:			Optional[int]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  int					   =					load_image(
       """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
       """/img2img/sketch-mountains-input.jpg"""    )
   UpperCAmelCase__					:  Optional[int]					   =					init_image.resize((128, 128)    )
   # using the PNDM scheduler by default
   UpperCAmelCase__					:  Optional[Any]					   =					OnnxStableDiffusionUpscalePipeline.from_pretrained(
       """ssube/stable-diffusion-x4-upscaler-onnx"""					,provider=self.gpu_provider					,sess_options=self.gpu_options					,)
   pipe.set_progress_bar_config(disable=A    )

   UpperCAmelCase__					:  Optional[Any]					   =					"""A fantasy landscape, trending on artstation"""

   UpperCAmelCase__					:  str					   =					torch.manual_seed(0    )
   UpperCAmelCase__					:  Dict					   =					pipe(
       prompt=A					,image=A					,guidance_scale=7.5					,num_inference_steps=10					,generator=A					,output_type="""np"""					,)
   UpperCAmelCase__					:  List[Any]					   =					output.images
   UpperCAmelCase__					:  Optional[int]					   =					images[0, 255:258, 383:386, -1]

   assert images.shape == (1, 512, 512, 3)
   UpperCAmelCase__					:  Tuple					   =					np.array([0.4_8_8_3, 0.4_9_4_7, 0.4_9_8_0, 0.4_9_7_5, 0.4_9_8_2, 0.4_9_8_0, 0.5_0_0_0, 0.5_0_0_6, 0.4_9_7_2]    )
   # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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




  def    __lowercase       (  self		:			int    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Dict					   =					load_image(
       """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
       """/img2img/sketch-mountains-input.jpg"""    )
   UpperCAmelCase__					:  Optional[int]					   =					init_image.resize((128, 128)    )
   UpperCAmelCase__					:  List[str]					   =					LMSDiscreteScheduler.from_pretrained(
       """ssube/stable-diffusion-x4-upscaler-onnx"""					,subfolder="""scheduler"""    )
   UpperCAmelCase__					:  int					   =					OnnxStableDiffusionUpscalePipeline.from_pretrained(
       """ssube/stable-diffusion-x4-upscaler-onnx"""					,scheduler=A					,provider=self.gpu_provider					,sess_options=self.gpu_options					,)
   pipe.set_progress_bar_config(disable=A    )

   UpperCAmelCase__					:  Any					   =					"""A fantasy landscape, trending on artstation"""

   UpperCAmelCase__					:  int					   =					torch.manual_seed(0    )
   UpperCAmelCase__					:  Tuple					   =					pipe(
       prompt=A					,image=A					,guidance_scale=7.5					,num_inference_steps=20					,generator=A					,output_type="""np"""					,)
   UpperCAmelCase__					:  List[str]					   =					output.images
   UpperCAmelCase__					:  int					   =					images[0, 255:258, 383:386, -1]

   assert images.shape == (1, 512, 512, 3)
   UpperCAmelCase__					:  int					   =					np.array(
       [0.5_0_1_7_3_7_5_3, 0.5_0_2_2_3_3_5_6, 0.5_0_2_0_3_9, 0.5_0_2_3_3_0_3_6, 0.5_0_2_3_7_2_5, 0.5_0_2_2_6_0_1, 0.5_0_1_8_7_5_8, 0.5_0_2_3_4_0_8_5, 0.5_0_2_4_1_5_6_6]    )
   # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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





"""simple docstring"""




from __future__ import annotations

from typing import Any







class      __lowercase			:

  def __init__(  self		:			Optional[int]					,A		:			int					,A		:			int					,A		:			float = 0    ):






   '''simple docstring'''
   UpperCAmelCase__   ,      UpperCAmelCase__					:  str					   =					row, column
   UpperCAmelCase__					:  int					   =					[[default_value for c in range(A    )] for r in range(A    )]

  def __str__(  self		:			Union[str, Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Union[str, Any]					   =					f"Matrix consist of {self.row} rows and {self.column} columns\n"

   # Make string identifier
   UpperCAmelCase__					:  Optional[Any]					   =					0
   for row_vector in self.array:
    for obj in row_vector:
     UpperCAmelCase__					:  Optional[int]					   =					max(A					,len(str(A    )    )    )
   UpperCAmelCase__					:  int					   =					f"%{max_element_length}s"

   # Make string and return
   def single_line(A		:			list[float]    ) -> str:
    nonlocal string_format_identifier
    UpperCAmelCase__					:  List[str]					   =					"""["""
    line += ", ".join(string_format_identifier % (obj,) for obj in row_vector    )
    line += "]"
    return line

   s += "\n".join(single_line(A    ) for row_vector in self.array    )
   return s

  def __repr__(  self		:			Dict    ):






   '''simple docstring'''
   return str(self    )

  def    __lowercase       (  self		:			int					,A		:			tuple[int, int]    ):






   '''simple docstring'''
   if not (isinstance(A					,(list, tuple)    ) and len(A    ) == 2):
    return False
   elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column):
    return False
   else:
    return True

  def __getitem__(  self		:			Optional[int]					,A		:			tuple[int, int]    ):






   '''simple docstring'''
   assert self.validate_indicies(A    )
   return self.array[loc[0]][loc[1]]

  def __setitem__(  self		:			List[Any]					,A		:			tuple[int, int]					,A		:			float    ):






   '''simple docstring'''
   assert self.validate_indicies(A    )
   UpperCAmelCase__					:  str					   =					value

  def __add__(  self		:			Any					,A		:			Matrix    ):






   '''simple docstring'''
   assert isinstance(A					,A    )
   assert self.row == another.row and self.column == another.column

   # Add
   UpperCAmelCase__					:  Optional[int]					   =					Matrix(self.row					,self.column    )
   for r in range(self.row    ):
    for c in range(self.column    ):
     UpperCAmelCase__					:  Union[str, Any]					   =					self[r, c] + another[r, c]
   return result

  def __neg__(  self		:			Union[str, Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Tuple					   =					Matrix(self.row					,self.column    )
   for r in range(self.row    ):
    for c in range(self.column    ):
     UpperCAmelCase__					:  int					   =					-self[r, c]
   return result

  def __sub__(  self		:			str					,A		:			Matrix    ):






   '''simple docstring'''
   return self + (-another)

  def __mul__(  self		:			Union[str, Any]					,A		:			int | float | Matrix    ):






   '''simple docstring'''

   if isinstance(A					,(int, float)    ):  # Scalar multiplication
    UpperCAmelCase__					:  Dict					   =					Matrix(self.row					,self.column    )
    for r in range(self.row    ):
     for c in range(self.column    ):
      UpperCAmelCase__					:  Any					   =					self[r, c] * another
    return result
   elif isinstance(A					,A    ):  # Matrix multiplication
    assert self.column == another.row
    UpperCAmelCase__					:  Optional[int]					   =					Matrix(self.row					,another.column    )
    for r in range(self.row    ):
     for c in range(another.column    ):
      for i in range(self.column    ):
       result[r, c] += self[r, i] * another[i, c]
    return result
   else:
    UpperCAmelCase__					:  Union[str, Any]					   =					f"Unsupported type given for another ({type(A    )})"
    raise TypeError(A    )

  def    __lowercase       (  self		:			str    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					Matrix(self.column					,self.row    )
   for r in range(self.row    ):
    for c in range(self.column    ):
     UpperCAmelCase__					:  Optional[int]					   =					self[r, c]
   return result




  def    __lowercase       (  self		:			Any					,A		:			Matrix					,A		:			Matrix    ):






   '''simple docstring'''
   assert isinstance(A					,A    ) and isinstance(A					,A    )
   assert self.row == self.column == u.row == v.row  # u, v should be column vector
   assert u.column == v.column == 1  # u, v should be column vector

   # Calculate
   UpperCAmelCase__					:  Optional[Any]					   =					v.transpose()
   UpperCAmelCase__					:  Union[str, Any]					   =					(v_t * self * u)[0, 0] + 1
   if numerator_factor == 0:
    return None  # It's not invertable
   return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor))


# Testing
if __name__ == "__main__":
      def   lowerCAmelCase						(					):
       '''simple docstring'''





       UpperCAmelCase__					:  List[str]					   =					Matrix(3							,    3							,    0      )
       for i in range(3      ):
        UpperCAmelCase__					:  List[str]					   =					1
       print(F"a^(-1) is {ainv}"      )
       # u, v
       UpperCAmelCase__					:  List[Any]					   =					Matrix(3							,    1							,    0      )
       UpperCAmelCase__   ,      UpperCAmelCase__   ,      UpperCAmelCase__					:  Optional[int]					   =					1, 2, -3
       UpperCAmelCase__					:  List[str]					   =					Matrix(3							,    1							,    0      )
       UpperCAmelCase__   ,      UpperCAmelCase__   ,      UpperCAmelCase__					:  Tuple					   =					4, -2, 5
       print(F"u is {u}"      )
       print(F"v is {v}"      )
       print(F"uv^T is {u * v.transpose()}"      )
       # Sherman Morrison
       print(F"(a + uv^T)^(-1) is {ainv.sherman_morrison(__UpperCamelCase							,    __UpperCamelCase      )}"      )
      def   lowerCAmelCase						(					):
       '''simple docstring'''





       import doctest

       doctest.testmod()




      testa()





