





"""simple docstring"""




import unittest

import numpy as np

from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline
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 			_a						(				_lowerCAmelCase     ,    unittest.TestCase    ):
				# FIXME: add fast tests
				pass




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






				@property
				def     a     (			self		:   int							)     ->				Any:




										'''simple docstring'''



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






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




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   ort.SessionOptions()
										_UpperCamelCase      : Optional[Any]         =   False
										return options






				def     a     (			self		:   List[Any]							)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : Optional[Any]         =   load_image(
										    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
										    '''/in_paint/overture-creations-5sI6fQgYIuo.png'''							)
										_UpperCamelCase      : Optional[Any]         =   load_image(
										    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
										    '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png'''							)
										_UpperCamelCase      : int         =   OnnxStableDiffusionInpaintPipeline.from_pretrained(
										    '''runwayml/stable-diffusion-inpainting''',			revision='''onnx''',			safety_checker=lowerCAmelCase__,			feature_extractor=lowerCAmelCase__,			provider=self.gpu_provider,			sess_options=self.gpu_options,			)
										pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

										_UpperCamelCase      : List[Any]         =   '''A red cat sitting on a park bench'''

										_UpperCamelCase      : Dict         =   np.random.RandomState(0							)
										_UpperCamelCase      : List[Any]         =   pipe(
										    prompt=lowerCAmelCase__,			image=lowerCAmelCase__,			mask_image=lowerCAmelCase__,			guidance_scale=7.5,			num_inference_steps=1_0,			generator=lowerCAmelCase__,			output_type='''np''',			)
										_UpperCamelCase      : Any         =   output.images
										_UpperCamelCase      : List[str]         =   images[0, 2_5_5:2_5_8, 2_5_5:2_5_8, -1]

										assert images.shape == (1, 5_1_2, 5_1_2, 3)
										_UpperCamelCase      : int         =   np.array([0.2_514, 0.3_007, 0.3_517, 0.1_790, 0.2_382, 0.3_167, 0.1_944, 0.2_273, 0.2_464]							)

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






				def     a     (			self		:   Tuple							)     ->				Union[str, Any]:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   load_image(
										    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
										    '''/in_paint/overture-creations-5sI6fQgYIuo.png'''							)
										_UpperCamelCase      : Tuple         =   load_image(
										    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
										    '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png'''							)
										_UpperCamelCase      : Tuple         =   LMSDiscreteScheduler.from_pretrained(
										    '''runwayml/stable-diffusion-inpainting''',			subfolder='''scheduler''',			revision='''onnx'''							)
										_UpperCamelCase      : Union[str, Any]         =   OnnxStableDiffusionInpaintPipeline.from_pretrained(
										    '''runwayml/stable-diffusion-inpainting''',			revision='''onnx''',			scheduler=lowerCAmelCase__,			safety_checker=lowerCAmelCase__,			feature_extractor=lowerCAmelCase__,			provider=self.gpu_provider,			sess_options=self.gpu_options,			)
										pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

										_UpperCamelCase      : Optional[Any]         =   '''A red cat sitting on a park bench'''

										_UpperCamelCase      : List[Any]         =   np.random.RandomState(0							)
										_UpperCamelCase      : Union[str, Any]         =   pipe(
										    prompt=lowerCAmelCase__,			image=lowerCAmelCase__,			mask_image=lowerCAmelCase__,			guidance_scale=7.5,			num_inference_steps=2_0,			generator=lowerCAmelCase__,			output_type='''np''',			)
										_UpperCamelCase      : Optional[Any]         =   output.images
										_UpperCamelCase      : Tuple         =   images[0, 2_5_5:2_5_8, 2_5_5:2_5_8, -1]

										assert images.shape == (1, 5_1_2, 5_1_2, 3)
										_UpperCamelCase      : Dict         =   np.array([0.0_086, 0.0_077, 0.0_083, 0.0_093, 0.0_107, 0.0_139, 0.0_094, 0.0_097, 0.0_125]							)

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










"""simple docstring"""




import numpy as np
import torch
from imwatermark import WatermarkEncoder


# Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66
UpperCamelCase_									=0b1_0_1_1_0_0_1_1_1_1_1_0_1_1_0_0_1_0_0_1_0_0_0_0_0_1_1_1_1_0_1_1_1_0_1_1_0_0_0_1_1_0_0_1_1_1_1_0
# bin(x)[2:] gives bits of x as str, use int to convert them to 0/1
UpperCamelCase_									=[int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]]




class 			_a						:






				def __init__(			self		:   str							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : str         =   WATERMARK_BITS
										_UpperCamelCase      : Optional[int]         =   WatermarkEncoder()

										self.encoder.set_watermark('''bits''',			self.watermark							)






				def     snake_case     (			self		:   Dict,			lowerCAmelCase__		:   torch.FloatTensor							)     ->				int:




										'''simple docstring'''



										if images.shape[-1] < 2_5_6:
																return images

										_UpperCamelCase      : Union[str, Any]         =   (2_5_5 * (images / 2 + 0.5)).cpu().permute(0,			2,			3,			1							).float().numpy()

										_UpperCamelCase      : List[str]         =   [self.encoder.encode(lowerCAmelCase__,			'''dwtDct'''							) for image in images]

										_UpperCamelCase      : Dict         =   torch.from_numpy(np.array(lowerCAmelCase__							)							).permute(0,			3,			1,			2							)

										_UpperCamelCase      : Optional[int]         =   torch.clamp(2 * (images / 2_5_5 - 0.5),			min=-1.0,			max=1.0							)
										return images



