


import warnings
from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType






class _lowerCamelCase(						_a   ):
       lowercase_ :	Dict     =			["""image_processor""", """tokenizer"""]
       lowercase_ :	Optional[Any]     =			"""FlavaImageProcessor"""
       lowercase_ :	List[str]     =			("""BertTokenizer""", """BertTokenizerFast""")
       def __init__(					self,      lowerCamelCase=None,      lowerCamelCase=None,      **lowerCamelCase)       ->   List[str]:




           """simple docstring"""

           _lowercase       :							Dict      		=		None
           if "feature_extractor" in kwargs:
               warnings.warn(
                   'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                   ' instead.',      lowerCamelCase,      )
               _lowercase       :							List[Any]      		=		kwargs.pop('feature_extractor')

           _lowercase       :							List[Any]      		=		image_processor if image_processor is not None else feature_extractor
           if image_processor is None:
               raise ValueError('You need to specify an `image_processor`.')
           if tokenizer is None:
               raise ValueError('You need to specify a `tokenizer`.')

           super().__init__(lowerCamelCase,      lowerCamelCase)
           _lowercase       :							int      		=		self.image_processor
       def __call__(					self,      lowerCamelCase = None,      lowerCamelCase = None,      lowerCamelCase = True,      lowerCamelCase = False,      lowerCamelCase = False,      lowerCamelCase = None,      lowerCamelCase = 0,      lowerCamelCase = None,      lowerCamelCase = None,      lowerCamelCase = None,      lowerCamelCase = None,      lowerCamelCase = None,      lowerCamelCase = False,      lowerCamelCase = False,      lowerCamelCase = False,      lowerCamelCase = False,      lowerCamelCase = True,      lowerCamelCase = None,      **lowerCamelCase,      )       ->   Dict:




           """simple docstring"""

           if text is None and images is None:
               raise ValueError('You have to specify either text or images. Both cannot be none.')

           if text is not None:
               _lowercase       :							Optional[int]      		=		self.tokenizer(
                   text=lowerCamelCase,      add_special_tokens=lowerCamelCase,      padding=lowerCamelCase,      truncation=lowerCamelCase,      max_length=lowerCamelCase,      stride=lowerCamelCase,      pad_to_multiple_of=lowerCamelCase,      return_token_type_ids=lowerCamelCase,      return_attention_mask=lowerCamelCase,      return_overflowing_tokens=lowerCamelCase,      return_special_tokens_mask=lowerCamelCase,      return_offsets_mapping=lowerCamelCase,      return_length=lowerCamelCase,      verbose=lowerCamelCase,      return_tensors=lowerCamelCase,      **lowerCamelCase,      )
           if images is not None:
               _lowercase       :							str      		=		self.image_processor(
                   lowerCamelCase,      return_image_mask=lowerCamelCase,      return_codebook_pixels=lowerCamelCase,      return_tensors=lowerCamelCase,      **lowerCamelCase,      )

           if text is not None and images is not None:
               encoding.update(lowerCamelCase)
               return encoding
           elif text is not None:
               return encoding
           else:
               return BatchEncoding(data=dict(**lowerCamelCase),      tensor_type=lowerCamelCase)
       def 			UpperCamelCase     (					self,      *lowerCamelCase,      **lowerCamelCase)       ->   str:




           """simple docstring"""

           return self.tokenizer.batch_decode(*lowerCamelCase,      **lowerCamelCase)
       def 			UpperCamelCase     (					self,      *lowerCamelCase,      **lowerCamelCase)       ->   List[str]:




           """simple docstring"""

           return self.tokenizer.decode(*lowerCamelCase,      **lowerCamelCase)
       @property
       def 			UpperCamelCase     (					self)       ->   Optional[Any]:




           """simple docstring"""

           _lowercase       :							Dict      		=		self.tokenizer.model_input_names
           _lowercase       :							List[str]      		=		self.image_processor.model_input_names
           return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))
       @property
       def 			UpperCamelCase     (					self)       ->   Optional[int]:




           """simple docstring"""

           warnings.warn(
               '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.',      lowerCamelCase,      )
           return self.image_processor_class




       @property
       def 			UpperCamelCase     (					self)       ->   int:




           """simple docstring"""

           warnings.warn(
               '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.',      lowerCamelCase,      )
           return self.image_processor






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 _lowerCamelCase(						_a, unittest.TestCase   ):
       # TODO: is there an appropriate internal test set?
       lowercase_ :	int     =			"""ssube/stable-diffusion-x4-upscaler-onnx"""
       def 			UpperCamelCase     (					self,      lowerCamelCase=0)       ->   Union[str, Any]:




           """simple docstring"""

           _lowercase       :							Dict      		=		floats_tensor((1, 3, 1_28, 1_28),      rng=random.Random(lowerCamelCase))
           _lowercase       :							Union[str, Any]      		=		torch.manual_seed(lowerCamelCase)
           _lowercase       :							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 			UpperCamelCase     (					self)       ->   Tuple:




           """simple docstring"""

           _lowercase       :							Tuple      		=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint,      provider='CPUExecutionProvider')
           pipe.set_progress_bar_config(disable=lowerCamelCase)

           _lowercase       :							Dict      		=		self.get_dummy_inputs()
           _lowercase       :							Optional[int]      		=		pipe(**lowerCamelCase).images
           _lowercase       :							Optional[int]      		=		image[0, -3:, -3:, -1].flatten()

           # started as 128, should now be 512
           assert image.shape == (1, 5_12, 5_12, 3)
           _lowercase       :							Union[str, 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 - expected_slice).max() < 1E-1
       def 			UpperCamelCase     (					self)       ->   Union[str, Any]:




           """simple docstring"""

           _lowercase       :							int      		=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint,      provider='CPUExecutionProvider')
           _lowercase       :							str      		=		PNDMScheduler.from_config(pipe.scheduler.config,      skip_prk_steps=lowerCamelCase)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

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

           assert image.shape == (1, 5_12, 5_12, 3)
           _lowercase       :							int      		=		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 			UpperCamelCase     (					self)       ->   Optional[Any]:




           """simple docstring"""

           _lowercase       :							Dict      		=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint,      provider='CPUExecutionProvider')
           _lowercase       :							Optional[int]      		=		DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

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

           assert image.shape == (1, 5_12, 5_12, 3)
           _lowercase       :							Optional[int]      		=		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 			UpperCamelCase     (					self)       ->   Optional[int]:




           """simple docstring"""

           _lowercase       :							List[str]      		=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint,      provider='CPUExecutionProvider')
           _lowercase       :							List[str]      		=		EulerDiscreteScheduler.from_config(pipe.scheduler.config)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

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

           assert image.shape == (1, 5_12, 5_12, 3)
           _lowercase       :							Union[str, 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 			UpperCamelCase     (					self)       ->   Dict:




           """simple docstring"""

           _lowercase       :							Tuple      		=		OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint,      provider='CPUExecutionProvider')
           _lowercase       :							Any      		=		EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

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

           assert image.shape == (1, 5_12, 5_12, 3)
           _lowercase       :							Union[str, Any]      		=		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 _lowerCamelCase(						unittest.TestCase   ):
       @property
       def 			UpperCamelCase     (					self)       ->   List[Any]:




           """simple docstring"""

           return (
               "CUDAExecutionProvider",
               {
                   "gpu_mem_limit": "15000000000",  # 15GB
                   "arena_extend_strategy": "kSameAsRequested",
               },
           )
       @property
       def 			UpperCamelCase     (					self)       ->   Any:




           """simple docstring"""

           _lowercase       :							Union[str, Any]      		=		ort.SessionOptions()
           _lowercase       :							str      		=		False
           return options
       def 			UpperCamelCase     (					self)       ->   Any:




           """simple docstring"""

           _lowercase       :							Optional[int]      		=		load_image(
               'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
               '/img2img/sketch-mountains-input.jpg')
           _lowercase       :							int      		=		init_image.resize((1_28, 1_28))
           # using the PNDM scheduler by default
           _lowercase       :							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=lowerCamelCase)

           _lowercase       :							Optional[int]      		=		'A fantasy landscape, trending on artstation'

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

           assert images.shape == (1, 5_12, 5_12, 3)
           _lowercase       :							List[Any]      		=		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 			UpperCamelCase     (					self)       ->   Any:




           """simple docstring"""

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

           _lowercase       :							Optional[int]      		=		'A fantasy landscape, trending on artstation'

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

           assert images.shape == (1, 5_12, 5_12, 3)
           _lowercase       :							Union[str, Any]      		=		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


