





'''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 					__a      (lowerCamelCase       ,		unittest.TestCase     ):
							# TODO: is there an appropriate internal test set?
							__a					:   Optional[Any]									=       "ssube/stable-diffusion-x4-upscaler-onnx"

							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Union[str, Any]=0   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  floats_tensor((1, 3, 1_28, 1_28)       ,     rng=random.Random(__magic_name__   )   )
													UpperCAmelCase_			:      List[str]     						=  torch.manual_seed(__magic_name__   )
													UpperCAmelCase_			:      int     						=  {
													    '''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							:     Dict   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint       ,     provider='''CPUExecutionProvider'''   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

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

													# started as 128, should now be 512
													assert image.shape == (1, 5_12, 5_12, 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 - expected_slice   ).max() < 1E-1

							def     UpperCAmelCase__	(							self							:     Optional[int]   )					->       Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint       ,     provider='''CPUExecutionProvider'''   )
													UpperCAmelCase_			:      Any     						=  PNDMScheduler.from_config(pipe.scheduler.config       ,     skip_prk_steps=__magic_name__   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

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

													assert image.shape == (1, 5_12, 5_12, 3)
													UpperCAmelCase_			:      Any     						=  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							:     Dict   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint       ,     provider='''CPUExecutionProvider'''   )
													UpperCAmelCase_			:      int     						=  DPMSolverMultistepScheduler.from_config(pipe.scheduler.config   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

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

													assert image.shape == (1, 5_12, 5_12, 3)
													UpperCAmelCase_			:      Dict     						=  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							:     int   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint       ,     provider='''CPUExecutionProvider'''   )
													UpperCAmelCase_			:      Optional[Any]     						=  EulerDiscreteScheduler.from_config(pipe.scheduler.config   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

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

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







													"""simple docstring"""
													UpperCAmelCase_			:      Tuple     						=  OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint       ,     provider='''CPUExecutionProvider'''   )
													UpperCAmelCase_			:      Dict     						=  EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config   )
													pipe.set_progress_bar_config(disable=__magic_name__   )

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

													assert image.shape == (1, 5_12, 5_12, 3)
													UpperCAmelCase_			:      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 					__a      (unittest.TestCase     ):

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







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

							@property
							def     UpperCAmelCase__	(							self							:     str   )					->       str:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  ort.SessionOptions()
													UpperCAmelCase_			:      Optional[int]     						=  False
													return options

							def     UpperCAmelCase__	(							self							:     Any   )					->       Optional[Any]:







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

													UpperCAmelCase_			:      List[Any]     						=  '''A fantasy landscape, trending on artstation'''

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

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







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

													UpperCAmelCase_			:      List[Any]     						=  '''A fantasy landscape, trending on artstation'''

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

													assert images.shape == (1, 5_12, 5_12, 3)
													UpperCAmelCase_			:      Optional[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












'''simple docstring'''







import json
import os
import unittest

from transformers.models.blenderbot_small.tokenization_blenderbot_small import (
    VOCAB_FILES_NAMES,
    BlenderbotSmallTokenizer,
)

from ...test_tokenization_common import TokenizerTesterMixin


class 					__a      (lowerCamelCase       ,		unittest.TestCase     ):
							__a					:   List[str]									=       BlenderbotSmallTokenizer
							__a					:   List[Any]									=       False

							def     UpperCAmelCase__	(							self							:     str   )					->       str:







													"""simple docstring"""
													super().setUp()

													UpperCAmelCase_			:      Tuple     						=  ['''__start__''', '''adapt''', '''act''', '''ap@@''', '''te''', '''__end__''', '''__unk__''']
													UpperCAmelCase_			:      Optional[Any]     						=  dict(zip(__magic_name__       ,     range(len(__magic_name__   )   )   )   )

													UpperCAmelCase_			:      int     						=  ['''#version: 0.2''', '''a p''', '''t e</w>''', '''ap t</w>''', '''a d''', '''ad apt</w>''', '''a c''', '''ac t</w>''', '''''']
													UpperCAmelCase_			:      Optional[Any]     						=  {'''unk_token''': '''__unk__''', '''bos_token''': '''__start__''', '''eos_token''': '''__end__'''}

													UpperCAmelCase_			:      Tuple     						=  os.path.join(self.tmpdirname       ,     VOCAB_FILES_NAMES['''vocab_file''']   )
													UpperCAmelCase_			:      Dict     						=  os.path.join(self.tmpdirname       ,     VOCAB_FILES_NAMES['''merges_file''']   )
													with open(self.vocab_file       ,     '''w'''       ,     encoding='''utf-8'''   ) as fp:
																			fp.write(json.dumps(__magic_name__   ) + '''\n'''   )
													with open(self.merges_file       ,     '''w'''       ,     encoding='''utf-8'''   ) as fp:
																			fp.write('''\n'''.join(__magic_name__   )   )

							def     UpperCAmelCase__	(							self							:     List[Any]       ,     **__magic_name__							:     Dict   )					->       Tuple:







													"""simple docstring"""
													kwargs.update(self.special_tokens_map   )
													return BlenderbotSmallTokenizer.from_pretrained(self.tmpdirname       ,     **__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     List[str]   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  '''adapt act apte'''
													UpperCAmelCase_			:      Tuple     						=  '''adapt act apte'''
													return input_text, output_text

							def     UpperCAmelCase__	(							self							:     str   )					->       Any:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  BlenderbotSmallTokenizer(self.vocab_file       ,     self.merges_file       ,     **self.special_tokens_map   )
													UpperCAmelCase_			:      List[Any]     						=  '''adapt act apte'''
													UpperCAmelCase_			:      Dict     						=  ['''adapt''', '''act''', '''ap@@''', '''te''']
													UpperCAmelCase_			:      Dict     						=  tokenizer.tokenize(__magic_name__   )
													self.assertListEqual(__magic_name__       ,     __magic_name__   )

													UpperCAmelCase_			:      Tuple     						=  [tokenizer.bos_token] + tokens + [tokenizer.eos_token]

													UpperCAmelCase_			:      Dict     						=  [0, 1, 2, 3, 4, 5]
													self.assertListEqual(tokenizer.convert_tokens_to_ids(__magic_name__   )       ,     __magic_name__   )

							def     UpperCAmelCase__	(							self							:     int   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  BlenderbotSmallTokenizer.from_pretrained('''facebook/blenderbot-90M'''   )
													assert tok('''sam'''   ).input_ids == [13_84]
													UpperCAmelCase_			:      Optional[int]     						=  '''I am a small frog.'''
													UpperCAmelCase_			:      List[str]     						=  tok([src_text]       ,     padding=__magic_name__       ,     truncation=__magic_name__   )['''input_ids''']
													UpperCAmelCase_			:      Dict     						=  tok.batch_decode(__magic_name__       ,     skip_special_tokens=__magic_name__       ,     clean_up_tokenization_spaces=__magic_name__   )[0]
													assert src_text != decoded  # I wish it did!
													assert decoded == "i am a small frog ."



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







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  BlenderbotSmallTokenizer.from_pretrained('''facebook/blenderbot-90M'''   )
													UpperCAmelCase_			:      List[Any]     						=  '''I am a small frog .'''
													UpperCAmelCase_			:      Any     						=  '''.'''
													UpperCAmelCase_			:      List[Any]     						=  tok(__magic_name__   )['''input_ids''']
													UpperCAmelCase_			:      Optional[int]     						=  tok(__magic_name__   )['''input_ids''']

													assert encoded[-1] == encoded_dot[0]





