"""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						):






				"""simple docstring"""




				UpperCAmelCase    :				List[str]   =		"""ssube/stable-diffusion-x4-upscaler-onnx"""



				def  _lowercase					(self    :				Dict     ,  _A    :				Union[str, Any]=0)   ->     Optional[Any]:
					__snake_case      :							str						=			floats_tensor((1, 3, 1_28, 1_28)     ,  rng=random.Random(UpperCAmelCase_))
					__snake_case      :							Any						=			torch.manual_seed(UpperCAmelCase_)
					__snake_case      :							Optional[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    :				List[str])   ->     Tuple:
					__snake_case      :							Any						=			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint     ,  provider='CPUExecutionProvider')
					pipe.set_progress_bar_config(disable=UpperCAmelCase_)

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

					# started as 128, should now be 512
					assert image.shape == (1, 5_12, 5_12, 3)
					__snake_case      :							List[Any]						=			np.array(
					    [0.6_974_782, 0.68_902_093, 0.70_135_885, 0.7_583_618, 0.7_804_545, 0.7_854_912, 0.78_667_426, 0.78_743_863, 0.78_070_223])
					assert np.abs(image_slice - expected_slice).max() < 1E-1



				def  _lowercase					(self    :				str)   ->     Any:
					__snake_case      :							Tuple						=			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint     ,  provider='CPUExecutionProvider')
					__snake_case      :							Optional[int]						=			PNDMScheduler.from_config(pipe.scheduler.config     ,  skip_prk_steps=UpperCAmelCase_)
					pipe.set_progress_bar_config(disable=UpperCAmelCase_)

					__snake_case      :							Tuple						=			self.get_dummy_inputs()
					__snake_case      :							str						=			pipe(**UpperCAmelCase_).images
					__snake_case      :							Tuple						=			image[0, -3:, -3:, -1]

					assert image.shape == (1, 5_12, 5_12, 3)
					__snake_case      :							Optional[int]						=			np.array(
					    [0.6_898_892, 0.59_240_556, 0.52_499_527, 0.58_866_215, 0.52_258_235, 0.52_572_715, 0.62_414_473, 0.6_174_387, 0.6_214_964])
					assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-1



				def  _lowercase					(self    :				List[str])   ->     List[Any]:
					__snake_case      :							Optional[int]						=			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint     ,  provider='CPUExecutionProvider')
					__snake_case      :							int						=			DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
					pipe.set_progress_bar_config(disable=UpperCAmelCase_)

					__snake_case      :							List[str]						=			self.get_dummy_inputs()
					__snake_case      :							Dict						=			pipe(**UpperCAmelCase_).images
					__snake_case      :							str						=			image[0, -3:, -3:, -1]

					assert image.shape == (1, 5_12, 5_12, 3)
					__snake_case      :							Union[str, Any]						=			np.array(
					    [0.7_659_278, 0.76_437_664, 0.75_579_107, 0.7_691_116, 0.77_666_986, 0.7_727_672, 0.7_758_664, 0.7_812_226, 0.76_942_515])

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



				def  _lowercase					(self    :				List[Any])   ->     Dict:
					__snake_case      :							Optional[int]						=			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint     ,  provider='CPUExecutionProvider')
					__snake_case      :							List[str]						=			EulerDiscreteScheduler.from_config(pipe.scheduler.config)
					pipe.set_progress_bar_config(disable=UpperCAmelCase_)

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

					assert image.shape == (1, 5_12, 5_12, 3)
					__snake_case      :							Dict						=			np.array(
					    [0.6_974_782, 0.68_902_093, 0.70_135_885, 0.7_583_618, 0.7_804_545, 0.7_854_912, 0.78_667_426, 0.78_743_863, 0.78_070_223])
					assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-1






				def  _lowercase					(self    :				Dict)   ->     Any:
					__snake_case      :							List[str]						=			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint     ,  provider='CPUExecutionProvider')
					__snake_case      :							Optional[int]						=			EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
					pipe.set_progress_bar_config(disable=UpperCAmelCase_)

					__snake_case      :							List[Any]						=			self.get_dummy_inputs()
					__snake_case      :							Optional[int]						=			pipe(**UpperCAmelCase_).images
					__snake_case      :							Dict						=			image[0, -3:, -3:, -1]

					assert image.shape == (1, 5_12, 5_12, 3)
					__snake_case      :							str						=			np.array(
					    [0.77_424_496, 0.773_601, 0.7_645_288, 0.7_769_598, 0.7_772_739, 0.7_738_688, 0.78_187_233, 0.77_879_584, 0.767_043])

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







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






				"""simple docstring"""




				@property
				def  _lowercase					(self    :				List[Any])   ->     Tuple:
					return (
					    "CUDAExecutionProvider",
					    {
					        "gpu_mem_limit": "15000000000",  # 15GB
					        "arena_extend_strategy": "kSameAsRequested",
					    },
					)



				@property
				def  _lowercase					(self    :				List[str])   ->     int:
					__snake_case      :							List[str]						=			ort.SessionOptions()
					__snake_case      :							List[Any]						=			False
					return options



				def  _lowercase					(self    :				List[str])   ->     Optional[int]:
					__snake_case      :							Tuple						=			load_image(
					    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
					    '/img2img/sketch-mountains-input.jpg')
					__snake_case      :							int						=			init_image.resize((1_28, 1_28))
					# using the PNDM scheduler by default
					__snake_case      :							Tuple						=			OnnxStableDiffusionUpscalePipeline.from_pretrained(
					    'ssube/stable-diffusion-x4-upscaler-onnx'     ,  provider=self.gpu_provider     ,  sess_options=self.gpu_options     ,  )
					pipe.set_progress_bar_config(disable=UpperCAmelCase_)

					__snake_case      :							str						=			"A fantasy landscape, trending on artstation"

					__snake_case      :							Any						=			torch.manual_seed(0)
					__snake_case      :							int						=			pipe(
					    prompt=UpperCAmelCase_     ,  image=UpperCAmelCase_     ,  guidance_scale=7.5     ,  num_inference_steps=10     ,  generator=UpperCAmelCase_     ,  output_type='np'     ,  )
					__snake_case      :							Dict						=			output.images
					__snake_case      :							Tuple						=			images[0, 2_55:2_58, 3_83:3_86, -1]

					assert images.shape == (1, 5_12, 5_12, 3)
					__snake_case      :							Union[str, Any]						=			np.array([0.4_883, 0.4_947, 0.4_980, 0.4_975, 0.4_982, 0.4_980, 0.5_000, 0.5_006, 0.4_972])
					# 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    :				List[str])   ->     int:
					__snake_case      :							Tuple						=			load_image(
					    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
					    '/img2img/sketch-mountains-input.jpg')
					__snake_case      :							int						=			init_image.resize((1_28, 1_28))
					__snake_case      :							Optional[int]						=			LMSDiscreteScheduler.from_pretrained(
					    'ssube/stable-diffusion-x4-upscaler-onnx'     ,  subfolder='scheduler')
					__snake_case      :							Any						=			OnnxStableDiffusionUpscalePipeline.from_pretrained(
					    'ssube/stable-diffusion-x4-upscaler-onnx'     ,  scheduler=UpperCAmelCase_     ,  provider=self.gpu_provider     ,  sess_options=self.gpu_options     ,  )
					pipe.set_progress_bar_config(disable=UpperCAmelCase_)

					__snake_case      :							int						=			"A fantasy landscape, trending on artstation"

					__snake_case      :							List[Any]						=			torch.manual_seed(0)
					__snake_case      :							Optional[Any]						=			pipe(
					    prompt=UpperCAmelCase_     ,  image=UpperCAmelCase_     ,  guidance_scale=7.5     ,  num_inference_steps=20     ,  generator=UpperCAmelCase_     ,  output_type='np'     ,  )
					__snake_case      :							List[str]						=			output.images
					__snake_case      :							int						=			images[0, 2_55:2_58, 3_83:3_86, -1]

					assert images.shape == (1, 5_12, 5_12, 3)
					__snake_case      :							str						=			np.array(
					    [0.50_173_753, 0.50_223_356, 0.502_039, 0.50_233_036, 0.5_023_725, 0.5_022_601, 0.5_018_758, 0.50_234_085, 0.50_241_566])
					# 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"""







import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils"))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402

class 		UpperCamelCase      (   unittest.TestCase						):



				def  _lowercase					(self    :				Union[str, Any])   ->     Optional[int]:
					__snake_case      :							Optional[Any]						=			0



				def  _lowercase					(self    :				Tuple)   ->     int:
					__snake_case      :							Optional[Any]						=			AutoImageProcessor.from_pretrained('openai/clip-vit-base-patch32')
					self.assertIsInstance(_A     ,  _A)



				def  _lowercase					(self    :				str)   ->     List[str]:
					with tempfile.TemporaryDirectory() as tmpdirname:
						__snake_case      :							List[str]						=			Path(_A) / 'preprocessor_config.json'
						__snake_case      :							Optional[Any]						=			Path(_A) / 'config.json'
						json.dump(
						    {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}     ,  open(_A     ,  'w')     ,  )
						json.dump({'model_type': 'clip'}     ,  open(_A     ,  'w'))

						__snake_case      :							Optional[int]						=			AutoImageProcessor.from_pretrained(_A)
						self.assertIsInstance(_A     ,  _A)



				def  _lowercase					(self    :				Any)   ->     Optional[int]:
					# Ensure we can load the image processor from the feature extractor config
					with tempfile.TemporaryDirectory() as tmpdirname:
						__snake_case      :							Any						=			Path(_A) / 'preprocessor_config.json'
						__snake_case      :							List[Any]						=			Path(_A) / 'config.json'
						json.dump(
						    {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}     ,  open(_A     ,  'w')     ,  )
						json.dump({'model_type': 'clip'}     ,  open(_A     ,  'w'))

						__snake_case      :							Tuple						=			AutoImageProcessor.from_pretrained(_A)
						self.assertIsInstance(_A     ,  _A)



				def  _lowercase					(self    :				List[Any])   ->     Optional[Any]:
					with tempfile.TemporaryDirectory() as tmpdirname:
						__snake_case      :							str						=			CLIPConfig()

						# Create a dummy config file with image_proceesor_type
						__snake_case      :							List[Any]						=			Path(_A) / 'preprocessor_config.json'
						__snake_case      :							Optional[Any]						=			Path(_A) / 'config.json'
						json.dump(
						    {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}     ,  open(_A     ,  'w')     ,  )
						json.dump({'model_type': 'clip'}     ,  open(_A     ,  'w'))

						# remove image_processor_type to make sure config.json alone is enough to load image processor locally
						__snake_case      :							List[str]						=			AutoImageProcessor.from_pretrained(_A).to_dict()

						config_dict.pop('image_processor_type')
						__snake_case      :							Optional[int]						=			CLIPImageProcessor(**_A)

						# save in new folder
						model_config.save_pretrained(_A)
						config.save_pretrained(_A)

						__snake_case      :							Optional[int]						=			AutoImageProcessor.from_pretrained(_A)

						# make sure private variable is not incorrectly saved
						__snake_case      :							int						=			json.loads(config.to_json_string())
						self.assertTrue('_processor_class' not in dict_as_saved)

					self.assertIsInstance(_A     ,  _A)



				def  _lowercase					(self    :				Union[str, Any])   ->     Optional[Any]:
					with tempfile.TemporaryDirectory() as tmpdirname:
						__snake_case      :							int						=			Path(_A) / 'preprocessor_config.json'
						json.dump(
						    {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}     ,  open(_A     ,  'w')     ,  )

						__snake_case      :							List[str]						=			AutoImageProcessor.from_pretrained(_A)
						self.assertIsInstance(_A     ,  _A)



				def  _lowercase					(self    :				Optional[int])   ->     Dict:
					with self.assertRaisesRegex(
					    _A     ,  'clip-base is not a local folder and is not a valid model identifier'):
						__snake_case      :							Tuple						=			AutoImageProcessor.from_pretrained('clip-base')



				def  _lowercase					(self    :				str)   ->     int:
					with self.assertRaisesRegex(
					    _A     ,  r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'):
						__snake_case      :							str						=			AutoImageProcessor.from_pretrained(_A     ,  revision='aaaaaa')



				def  _lowercase					(self    :				List[Any])   ->     str:
					with self.assertRaisesRegex(
					    _A     ,  'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'     ,  ):
						__snake_case      :							List[Any]						=			AutoImageProcessor.from_pretrained('hf-internal-testing/config-no-model')



				def  _lowercase					(self    :				Optional[int])   ->     List[str]:
					# If remote code is not set, we will time out when asking whether to load the model.
					with self.assertRaises(_A):
						__snake_case      :							Any						=			AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor')
					# If remote code is disabled, we can't load this config.
					with self.assertRaises(_A):
						__snake_case      :							Tuple						=			AutoImageProcessor.from_pretrained(
						    'hf-internal-testing/test_dynamic_image_processor'     ,  trust_remote_code=_A)

					__snake_case      :							Union[str, Any]						=			AutoImageProcessor.from_pretrained(
					    'hf-internal-testing/test_dynamic_image_processor'     ,  trust_remote_code=_A)
					self.assertEqual(image_processor.__class__.__name__     ,  'NewImageProcessor')

					# Test image processor can be reloaded.
					with tempfile.TemporaryDirectory() as tmp_dir:
						image_processor.save_pretrained(_A)
						__snake_case      :							Optional[int]						=			AutoImageProcessor.from_pretrained(_A     ,  trust_remote_code=_A)
					self.assertEqual(reloaded_image_processor.__class__.__name__     ,  'NewImageProcessor')



				def  _lowercase					(self    :				int)   ->     Optional[int]:
					try:
						AutoConfig.register('custom'     ,  _A)
						AutoImageProcessor.register(_A     ,  _A)
						# Trying to register something existing in the Transformers library will raise an error
						with self.assertRaises(_A):
							AutoImageProcessor.register(_A     ,  _A)

						with tempfile.TemporaryDirectory() as tmpdirname:
							__snake_case      :							Tuple						=			Path(_A) / 'preprocessor_config.json'
							__snake_case      :							Dict						=			Path(_A) / 'config.json'
							json.dump(
							    {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}     ,  open(_A     ,  'w')     ,  )
							json.dump({'model_type': 'clip'}     ,  open(_A     ,  'w'))

							__snake_case      :							Tuple						=			CustomImageProcessor.from_pretrained(_A)

						# Now that the config is registered, it can be used as any other config with the auto-API
						with tempfile.TemporaryDirectory() as tmp_dir:
							image_processor.save_pretrained(_A)
							__snake_case      :							Tuple						=			AutoImageProcessor.from_pretrained(_A)
							self.assertIsInstance(_A     ,  _A)

					finally:
						if "custom" in CONFIG_MAPPING._extra_content:
							del CONFIG_MAPPING._extra_content["custom"]
						if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
							del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]






				def  _lowercase					(self    :				List[Any])   ->     Tuple:

					class 		UpperCamelCase      (   lowercase						):
									UpperCAmelCase    :				str   =		True

					try:
						AutoConfig.register('custom'     ,  _A)
						AutoImageProcessor.register(_A     ,  _A)
						# If remote code is not set, the default is to use local
						__snake_case      :							Tuple						=			AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor')
						self.assertEqual(image_processor.__class__.__name__     ,  'NewImageProcessor')
						self.assertTrue(image_processor.is_local)

						# If remote code is disabled, we load the local one.
						__snake_case      :							Optional[int]						=			AutoImageProcessor.from_pretrained(
						    'hf-internal-testing/test_dynamic_image_processor'     ,  trust_remote_code=_A)
						self.assertEqual(image_processor.__class__.__name__     ,  'NewImageProcessor')
						self.assertTrue(image_processor.is_local)

						# If remote is enabled, we load from the Hub
						__snake_case      :							List[Any]						=			AutoImageProcessor.from_pretrained(
						    'hf-internal-testing/test_dynamic_image_processor'     ,  trust_remote_code=_A)
						self.assertEqual(image_processor.__class__.__name__     ,  'NewImageProcessor')
						self.assertTrue(not hasattr(_A     ,  'is_local'))

					finally:
						if "custom" in CONFIG_MAPPING._extra_content:
							del CONFIG_MAPPING._extra_content["custom"]
						if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
							del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]





