
"""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      _UpperCAmelCase				(					_snake_case		,					unittest.TestCase):
    # TODO: is there an appropriate internal test set?
    __lowercase							: int  =					"""ssube/stable-diffusion-x4-upscaler-onnx"""
    def 					lowerCamelCase__	(    self						,    snake_case_=0     ):
      _snake_case     :						str						=   floats_tensor((1, 3, 1_28, 1_28)						,    rng=random.Random(snake_case_     )     )
      _snake_case     :						Any						=   torch.manual_seed(snake_case_     )
      _snake_case     :						List[str]						=   {
          "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 					lowerCamelCase__	(    self     ):
      _snake_case     :						Dict						=   OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,    provider="CPUExecutionProvider"     )
      pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						Optional[int]						=   self.get_dummy_inputs()
      _snake_case     :						Optional[Any]						=   pipe(**snake_case_     ).images
      _snake_case     :						List[str]						=   image[0, -3:, -3:, -1].flatten()

      # started as 128, should now be 512
      assert image.shape == (1, 5_12, 5_12, 3)
      _snake_case     :						Any						=   np.array(
          [0.6974782, 0.68902093, 0.70135885, 0.7583618, 0.7804545, 0.7854912, 0.78667426, 0.78743863, 0.78070223]     )
      assert np.abs(image_slice - expected_slice     ).max() < 1E-1
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						List[Any]						=   OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,    provider="CPUExecutionProvider"     )
      _snake_case     :						Optional[int]						=   PNDMScheduler.from_config(pipe.scheduler.config						,    skip_prk_steps=snake_case_     )
      pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						Tuple						=   self.get_dummy_inputs()
      _snake_case     :						List[Any]						=   pipe(**snake_case_     ).images
      _snake_case     :						Union[str, Any]						=   image[0, -3:, -3:, -1]

      assert image.shape == (1, 5_12, 5_12, 3)
      _snake_case     :						Optional[int]						=   np.array(
          [0.6898892, 0.59240556, 0.52499527, 0.58866215, 0.52258235, 0.52572715, 0.62414473, 0.6174387, 0.6214964]     )
      assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1E-1
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						List[str]						=   OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,    provider="CPUExecutionProvider"     )
      _snake_case     :						List[Any]						=   DPMSolverMultistepScheduler.from_config(pipe.scheduler.config     )
      pipe.set_progress_bar_config(disable=snake_case_     )

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

      assert image.shape == (1, 5_12, 5_12, 3)
      _snake_case     :						Any						=   np.array(
          [0.7659278, 0.76437664, 0.75579107, 0.7691116, 0.77666986, 0.7727672, 0.7758664, 0.7812226, 0.76942515]     )

      assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1E-1
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						Optional[int]						=   OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,    provider="CPUExecutionProvider"     )
      _snake_case     :						str						=   EulerDiscreteScheduler.from_config(pipe.scheduler.config     )
      pipe.set_progress_bar_config(disable=snake_case_     )

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

      assert image.shape == (1, 5_12, 5_12, 3)
      _snake_case     :						Any						=   np.array(
          [0.6974782, 0.68902093, 0.70135885, 0.7583618, 0.7804545, 0.7854912, 0.78667426, 0.78743863, 0.78070223]     )
      assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1E-1


    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						Optional[Any]						=   OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,    provider="CPUExecutionProvider"     )
      _snake_case     :						Union[str, Any]						=   EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config     )
      pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						Dict						=   self.get_dummy_inputs()
      _snake_case     :						Optional[Any]						=   pipe(**snake_case_     ).images
      _snake_case     :						Optional[Any]						=   image[0, -3:, -3:, -1]

      assert image.shape == (1, 5_12, 5_12, 3)
      _snake_case     :						Tuple						=   np.array(
          [0.77424496, 0.773601, 0.7645288, 0.7769598, 0.7772739, 0.7738688, 0.78187233, 0.77879584, 0.767043]     )

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






@nightly
@require_onnxruntime
@require_torch_gpu
class      _UpperCAmelCase				(					unittest.TestCase):
    @property
    def 					lowerCamelCase__	(    self     ):
      return (
          "CUDAExecutionProvider",
          {
              "gpu_mem_limit": "15000000000",  # 15GB
              "arena_extend_strategy": "kSameAsRequested",
          },
      )
    @property
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						Tuple						=   ort.SessionOptions()
      _snake_case     :						str						=   False
      return options
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						Dict						=   load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
          "/img2img/sketch-mountains-input.jpg"     )
      _snake_case     :						List[Any]						=   init_image.resize((1_28, 1_28)     )
      # using the PNDM scheduler by default
      _snake_case     :						List[str]						=   OnnxStableDiffusionUpscalePipeline.from_pretrained(
          "ssube/stable-diffusion-x4-upscaler-onnx"						,    provider=self.gpu_provider						,    sess_options=self.gpu_options						,    )
      pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						Union[str, Any]						=   "A fantasy landscape, trending on artstation"

      _snake_case     :						Any						=   torch.manual_seed(0     )
      _snake_case     :						Dict						=   pipe(
          prompt=snake_case_						,    image=snake_case_						,    guidance_scale=7.5						,    num_inference_steps=10						,    generator=snake_case_						,    output_type="np"						,    )
      _snake_case     :						Union[str, Any]						=   output.images
      _snake_case     :						List[Any]						=   images[0, 2_55:2_58, 3_83:3_86, -1]

      assert images.shape == (1, 5_12, 5_12, 3)
      _snake_case     :						int						=   np.array([0.4883, 0.4947, 0.4980, 0.4975, 0.4982, 0.4980, 0.5000, 0.5006, 0.4972]     )
      # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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


    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						Optional[Any]						=   load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
          "/img2img/sketch-mountains-input.jpg"     )
      _snake_case     :						str						=   init_image.resize((1_28, 1_28)     )
      _snake_case     :						Optional[Any]						=   LMSDiscreteScheduler.from_pretrained(
          "ssube/stable-diffusion-x4-upscaler-onnx"						,    subfolder="scheduler"     )
      _snake_case     :						Optional[Any]						=   OnnxStableDiffusionUpscalePipeline.from_pretrained(
          "ssube/stable-diffusion-x4-upscaler-onnx"						,    scheduler=snake_case_						,    provider=self.gpu_provider						,    sess_options=self.gpu_options						,    )
      pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						Optional[int]						=   "A fantasy landscape, trending on artstation"

      _snake_case     :						Tuple						=   torch.manual_seed(0     )
      _snake_case     :						List[Any]						=   pipe(
          prompt=snake_case_						,    image=snake_case_						,    guidance_scale=7.5						,    num_inference_steps=20						,    generator=snake_case_						,    output_type="np"						,    )
      _snake_case     :						str						=   output.images
      _snake_case     :						Dict						=   images[0, 2_55:2_58, 3_83:3_86, -1]

      assert images.shape == (1, 5_12, 5_12, 3)
      _snake_case     :						Optional[Any]						=   np.array(
          [0.50173753, 0.50223356, 0.502039, 0.50233036, 0.5023725, 0.5022601, 0.5018758, 0.50234085, 0.50241566]     )
      # 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 numpy import array, cos, cross, floataa, radians, sin
from numpy.typing import NDArray
def 				a__   (   a   :				float       ,     a   :				float       ,     a   :				bool = False		):

  """simple docstring"""







  if radian_mode:
    return [magnitude * cos(a		), magnitude * sin(a		)]
  return [magnitude * cos(radians(a		)		), magnitude * sin(radians(a		)		)]
def 				a__   (   a   :				NDArray[floataa]       ,     a   :				NDArray[floataa]       ,     a   :				float = 10**-1		):

  """simple docstring"""







  _snake_case     :						NDArray[floataa]						=   cross(a       ,     a		)
  _snake_case     :						float						=   sum(a		)
  return abs(a		) < eps


if __name__ == "__main__":
    # Test to check if it works
    _a      :			Tuple					=							array(
        [
            polar_force(718.4, 180 - 30),
            polar_force(879.54, 45),
            polar_force(100, -90),
        ]
    )

    _a      :			NDArray[floataa]					=							array([[0, 0], [0, 0], [0, 0]])

    assert in_static_equilibrium(forces, location)

    # Problem 1 in image_data/2D_problems.jpg
    _a      :			List[Any]					=							array(
        [
            polar_force(30 * 9.81, 15),
            polar_force(215, 180 - 45),
            polar_force(264, 90 - 30),
        ]
    )

    _a      :			List[Any]					=							array([[0, 0], [0, 0], [0, 0]])

    assert in_static_equilibrium(forces, location)

    # Problem in image_data/2D_problems_1.jpg
    _a      :			List[str]					=							array([[0, -2_000], [0, -1_200], [0, 15_600], [0, -12_400]])

    _a      :			Optional[int]					=							array([[0, 0], [6, 0], [10, 0], [12, 0]])

    assert in_static_equilibrium(forces, location)

    import doctest

    doctest.testmod()

