





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 SCREAMING_SNAKE_CASE_						(					__lowerCamelCase       ,				unittest.TestCase					):
						# TODO: is there an appropriate internal test set?
						__magic_name__:			Tuple       =       'ssube/stable-diffusion-x4-upscaler-onnx'






						def UpperCAmelCase_				(      self      :      Tuple		,   _A      :      Optional[int]=0	)							->    Union[str, Any]:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	floats_tensor((1, 3, 128, 128)		,   rng=random.Random(UpperCamelCase_	)	)
							snake_case_		:			Union[str, Any]											=	torch.manual_seed(UpperCamelCase_	)
							snake_case_		:			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      :      Optional[int]	)							->    Dict:







							"""simple docstring"""


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

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

							# started as 128, should now be 512
							assert image.shape == (1, 512, 512, 3)
							snake_case_		:			str											=	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      :      List[Any]	)							->    str:







							"""simple docstring"""


							snake_case_		:			Tuple											=	OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint		,   provider='CPUExecutionProvider'	)
							snake_case_		:			Union[str, Any]											=	PNDMScheduler.from_config(pipe.scheduler.config		,   skip_prk_steps=UpperCamelCase_	)
							pipe.set_progress_bar_config(disable=UpperCamelCase_	)

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

							assert image.shape == (1, 512, 512, 3)
							snake_case_		:			Dict											=	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"""


							snake_case_		:			Dict											=	OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint		,   provider='CPUExecutionProvider'	)
							snake_case_		:			Union[str, Any]											=	DPMSolverMultistepScheduler.from_config(pipe.scheduler.config	)
							pipe.set_progress_bar_config(disable=UpperCamelCase_	)

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

							assert image.shape == (1, 512, 512, 3)
							snake_case_		:			str											=	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[Any]	)							->    Optional[int]:







							"""simple docstring"""


							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_		:			Dict											=	self.get_dummy_inputs()
							snake_case_		:			Optional[int]											=	pipe(**UpperCamelCase_	).images
							snake_case_		:			int											=	image[0, -3:, -3:, -1]

							assert image.shape == (1, 512, 512, 3)
							snake_case_		:			List[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      :      Any	)							->    Optional[Any]:







							"""simple docstring"""


							snake_case_		:			List[Any]											=	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_		:			int											=	self.get_dummy_inputs()
							snake_case_		:			Tuple											=	pipe(**UpperCamelCase_	).images
							snake_case_		:			Any											=	image[0, -3:, -3:, -1]

							assert image.shape == (1, 512, 512, 3)
							snake_case_		:			Dict											=	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 SCREAMING_SNAKE_CASE_						(					unittest.TestCase					):






						@property
						def UpperCAmelCase_				(      self      :      Dict	)							->    Optional[Any]:







							"""simple docstring"""


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






						@property
						def UpperCAmelCase_				(      self      :      List[Any]	)							->    List[Any]:







							"""simple docstring"""


							snake_case_		:			Union[str, Any]											=	ort.SessionOptions()
							snake_case_		:			List[str]											=	False
							return options






						def UpperCAmelCase_				(      self      :      Any	)							->    int:







							"""simple docstring"""


							snake_case_		:			Union[str, Any]											=	load_image(
							    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
							    '/img2img/sketch-mountains-input.jpg'	)
							snake_case_		:			Optional[int]											=	init_image.resize((128, 128)	)
							# using the PNDM scheduler by default
							snake_case_		:			Optional[int]											=	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_		:			List[str]											=	'A fantasy landscape, trending on artstation'

							snake_case_		:			List[Any]											=	torch.manual_seed(0	)
							snake_case_		:			Union[str, Any]											=	pipe(
							    prompt=UpperCamelCase_		,   image=UpperCamelCase_		,   guidance_scale=7.5		,   num_inference_steps=10		,   generator=UpperCamelCase_		,   output_type='np'		,   )
							snake_case_		:			List[str]											=	output.images
							snake_case_		:			List[str]											=	images[0, 255:258, 383:386, -1]

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







							"""simple docstring"""


							snake_case_		:			str											=	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((128, 128)	)
							snake_case_		:			Union[str, Any]											=	LMSDiscreteScheduler.from_pretrained(
							    'ssube/stable-diffusion-x4-upscaler-onnx'		,   subfolder='scheduler'	)
							snake_case_		:			List[str]											=	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_		:			Tuple											=	'A fantasy landscape, trending on artstation'

							snake_case_		:			Tuple											=	torch.manual_seed(0	)
							snake_case_		:			Any											=	pipe(
							    prompt=UpperCamelCase_		,   image=UpperCamelCase_		,   guidance_scale=7.5		,   num_inference_steps=20		,   generator=UpperCamelCase_		,   output_type='np'		,   )
							snake_case_		:			Tuple											=	output.images
							snake_case_		:			str											=	images[0, 255:258, 383:386, -1]

							assert images.shape == (1, 512, 512, 3)
							snake_case_		:			Tuple											=	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








import unittest

from transformers import CamembertTokenizer, CamembertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow
from transformers.utils import is_torch_available

from ...test_tokenization_common import TokenizerTesterMixin


_SCREAMING_SNAKE_CASE      	=		get_tests_dir("""fixtures/test_sentencepiece.model""")
_SCREAMING_SNAKE_CASE      	=		get_tests_dir("""fixtures/test_sentencepiece_bpe.model""")

_SCREAMING_SNAKE_CASE      	=		"""pt""" if is_torch_available() else """tf"""
@require_sentencepiece
@require_tokenizers
class SCREAMING_SNAKE_CASE_						(					snake_case_       ,				unittest.TestCase					):
						__magic_name__:			str       =       CamembertTokenizer
						__magic_name__:			Optional[Any]       =       CamembertTokenizerFast
						__magic_name__:			Optional[int]       =       True
						__magic_name__:			str       =       True






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







							"""simple docstring"""


							super().setUp()

							# We have a SentencePiece fixture for testing
							snake_case_		:			Union[str, Any]											=	CamembertTokenizer(_A	)
							tokenizer.save_pretrained(self.tmpdirname	)






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







							"""simple docstring"""


							snake_case_		:			int											=	'<pad>'
							snake_case_		:			int											=	1

							self.assertEqual(self.get_tokenizer()._convert_token_to_id(_A	)		,   _A	)
							self.assertEqual(self.get_tokenizer()._convert_id_to_token(_A	)		,   _A	)






						def UpperCAmelCase_				(      self      :      str	)							->    Any:







							"""simple docstring"""


							snake_case_		:			Optional[Any]											=	list(self.get_tokenizer().get_vocab().keys()	)

							self.assertEqual(vocab_keys[0]		,   '<s>NOTUSED'	)
							self.assertEqual(vocab_keys[1]		,   '<pad>'	)
							self.assertEqual(vocab_keys[-1]		,   '<mask>'	)
							self.assertEqual(len(_A	)		,   1004	)






						def UpperCAmelCase_				(      self      :      Optional[int]	)							->    Dict:







							"""simple docstring"""


							self.assertEqual(self.get_tokenizer().vocab_size		,   1005	)






						def UpperCAmelCase_				(      self      :      Dict	)							->    List[str]:







							"""simple docstring"""


							snake_case_		:			Any											=	CamembertTokenizer(_A	)
							tokenizer.save_pretrained(self.tmpdirname	)
							snake_case_		:			List[Any]											=	CamembertTokenizerFast.from_pretrained(self.tmpdirname	)

							snake_case_		:			str											=	'I was born in 92000, and this is falsé.'

							snake_case_		:			Dict											=	tokenizer.encode(_A	)
							snake_case_		:			Tuple											=	rust_tokenizer.encode(_A	)
							self.assertListEqual(_A		,   _A	)

							snake_case_		:			List[Any]											=	tokenizer.encode(_A		,   add_special_tokens=_A	)
							snake_case_		:			Any											=	rust_tokenizer.encode(_A		,   add_special_tokens=_A	)
							self.assertListEqual(_A		,   _A	)

							# <unk> tokens are not the same for `rust` than for `slow`.
							# Because spm gives back raw token instead of `unk` in EncodeAsPieces
							# tokens = tokenizer.tokenize(sequence)
							snake_case_		:			Union[str, Any]											=	tokenizer.convert_ids_to_tokens(_A	)
							snake_case_		:			Optional[Any]											=	rust_tokenizer.tokenize(_A	)
							self.assertListEqual(_A		,   _A	)






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







							"""simple docstring"""


							if not self.test_rust_tokenizer:
								return

							snake_case_		:			Union[str, Any]											=	self.get_tokenizer()
							snake_case_		:			Any											=	self.get_rust_tokenizer()

							snake_case_		:			Union[str, Any]											=	'I was born in 92000, and this is falsé.'

							snake_case_		:			Optional[int]											=	tokenizer.tokenize(_A	)
							snake_case_		:			Dict											=	rust_tokenizer.tokenize(_A	)
							self.assertListEqual(_A		,   _A	)

							snake_case_		:			Union[str, Any]											=	tokenizer.encode(_A		,   add_special_tokens=_A	)
							snake_case_		:			str											=	rust_tokenizer.encode(_A		,   add_special_tokens=_A	)
							self.assertListEqual(_A		,   _A	)

							snake_case_		:			Optional[int]											=	self.get_rust_tokenizer()
							snake_case_		:			int											=	tokenizer.encode(_A	)
							snake_case_		:			int											=	rust_tokenizer.encode(_A	)
							self.assertListEqual(_A		,   _A	)






						@slow
						def UpperCAmelCase_				(      self      :      Optional[int]	)							->    Optional[int]:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	{'input_ids': [[5, 54, 7196, 297, 30, 23, 776, 18, 11, 3215, 3705, 8252, 22, 3164, 1181, 2116, 29, 16, 813, 25, 791, 3314, 20, 3446, 38, 27575, 120, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 468, 17, 11, 9088, 20, 1517, 8, 22804, 18818, 10, 38, 629, 607, 607, 142, 19, 7196, 867, 56, 10326, 24, 2267, 20, 416, 5072, 15612, 233, 734, 7, 2399, 27, 16, 3015, 1649, 7, 24, 20, 4338, 2399, 27, 13, 3400, 14, 13, 6189, 8, 930, 9, 6]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
							# fmt: on

							# camembert is a french model. So we also use french texts.
							snake_case_		:			Dict											=	[
							    'Le transformeur est un modèle d\'apprentissage profond introduit en 2017, '
							    'utilisé principalement dans le domaine du traitement automatique des langues (TAL).',
							    'À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus '
							    'pour gérer des données séquentielles, telles que le langage naturel, pour des tâches '
							    'telles que la traduction et la synthèse de texte.',
							]

							self.tokenizer_integration_test_util(
							    expected_encoding=_A		,   model_name='camembert-base'		,   revision='3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf'		,   sequences=_A		,   )

