



"""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  lowercase__  (       SCREAMING_SNAKE_CASE							,   unittest.TestCase ):







       '''simple docstring'''


       UpperCamelCase		=	'''ssube/stable-diffusion-x4-upscaler-onnx'''



       def 	lowercase__							(				self     :    str						,							_UpperCAmelCase     :    int=0       )					->		int:


             '''simple docstring'''




             UpperCAmelCase_     					=				floats_tensor((1, 3, 128, 128)						,							rng=random.Random(_UpperCAmelCase       )       )
             UpperCAmelCase_     					=				torch.manual_seed(_UpperCAmelCase       )
             UpperCAmelCase_     					=				{
                 "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]       )					->		Union[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_     					=				OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,							provider="CPUExecutionProvider"       )
             pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

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

             # started as 128, should now be 512
             assert image.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.697_4782, 0.6890_2093, 0.7013_5885, 0.758_3618, 0.780_4545, 0.785_4912, 0.7866_7426, 0.7874_3863, 0.7807_0223]       )
             assert np.abs(image_slice - expected_slice       ).max() < 1e-1



       def 	lowercase__							(				self     :    Optional[int]       )					->		int:


             '''simple docstring'''




             UpperCAmelCase_     					=				OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,							provider="CPUExecutionProvider"       )
             UpperCAmelCase_     					=				PNDMScheduler.from_config(pipe.scheduler.config						,							skip_prk_steps=_UpperCAmelCase       )
             pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

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

             assert image.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.689_8892, 0.5924_0556, 0.5249_9527, 0.5886_6215, 0.5225_8235, 0.5257_2715, 0.6241_4473, 0.617_4387, 0.621_4964]       )
             assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-1



       def 	lowercase__							(				self     :    Any       )					->		Dict:


             '''simple docstring'''




             UpperCAmelCase_     					=				OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,							provider="CPUExecutionProvider"       )
             UpperCAmelCase_     					=				DPMSolverMultistepScheduler.from_config(pipe.scheduler.config       )
             pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

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

             assert image.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.765_9278, 0.7643_7664, 0.7557_9107, 0.769_1116, 0.7766_6986, 0.772_7672, 0.775_8664, 0.781_2226, 0.7694_2515]       )

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



       def 	lowercase__							(				self     :    int       )					->		Dict:


             '''simple docstring'''




             UpperCAmelCase_     					=				OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,							provider="CPUExecutionProvider"       )
             UpperCAmelCase_     					=				EulerDiscreteScheduler.from_config(pipe.scheduler.config       )
             pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

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

             assert image.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.697_4782, 0.6890_2093, 0.7013_5885, 0.758_3618, 0.780_4545, 0.785_4912, 0.7866_7426, 0.7874_3863, 0.7807_0223]       )
             assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-1







       def 	lowercase__							(				self     :    Dict       )					->		Any:


             '''simple docstring'''




             UpperCAmelCase_     					=				OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,							provider="CPUExecutionProvider"       )
             UpperCAmelCase_     					=				EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config       )
             pipe.set_progress_bar_config(disable=_UpperCAmelCase       )

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

             assert image.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.7742_4496, 0.77_3601, 0.764_5288, 0.776_9598, 0.777_2739, 0.773_8688, 0.7818_7233, 0.7787_9584, 0.76_7043]       )

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



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







       '''simple docstring'''


       @property
       def 	lowercase__							(				self     :    List[str]       )					->		Dict:


             '''simple docstring'''




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



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


             '''simple docstring'''




             UpperCAmelCase_     					=				ort.SessionOptions()
             UpperCAmelCase_     					=				False
             return options



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


             '''simple docstring'''




             UpperCAmelCase_     					=				load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/img2img/sketch-mountains-input.jpg"       )
             UpperCAmelCase_     					=				init_image.resize((128, 128)       )
             # using the PNDM scheduler by default
             UpperCAmelCase_     					=				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       )

             UpperCAmelCase_     					=				"A fantasy landscape, trending on artstation"

             UpperCAmelCase_     					=				torch.manual_seed(0       )
             UpperCAmelCase_     					=				pipe(
                 prompt=_UpperCAmelCase						,							image=_UpperCAmelCase						,							guidance_scale=7.5						,							num_inference_steps=10						,							generator=_UpperCAmelCase						,							output_type="np"						,							)
             UpperCAmelCase_     					=				output.images
             UpperCAmelCase_     					=				images[0, 255:258, 383:386, -1]

             assert images.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array([0.4883, 0.4947, 0.4980, 0.4975, 0.4982, 0.4980, 0.5000, 0.5006, 0.4972]       )
             # 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     :    Any       )					->		Optional[Any]:


             '''simple docstring'''




             UpperCAmelCase_     					=				load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/img2img/sketch-mountains-input.jpg"       )
             UpperCAmelCase_     					=				init_image.resize((128, 128)       )
             UpperCAmelCase_     					=				LMSDiscreteScheduler.from_pretrained(
                 "ssube/stable-diffusion-x4-upscaler-onnx"						,							subfolder="scheduler"       )
             UpperCAmelCase_     					=				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       )

             UpperCAmelCase_     					=				"A fantasy landscape, trending on artstation"

             UpperCAmelCase_     					=				torch.manual_seed(0       )
             UpperCAmelCase_     					=				pipe(
                 prompt=_UpperCAmelCase						,							image=_UpperCAmelCase						,							guidance_scale=7.5						,							num_inference_steps=20						,							generator=_UpperCAmelCase						,							output_type="np"						,							)
             UpperCAmelCase_     					=				output.images
             UpperCAmelCase_     					=				images[0, 255:258, 383:386, -1]

             assert images.shape == (1, 512, 512, 3)
             UpperCAmelCase_     					=				np.array(
                 [0.5017_3753, 0.5022_3356, 0.50_2039, 0.5023_3036, 0.502_3725, 0.502_2601, 0.501_8758, 0.5023_4085, 0.5024_1566]       )
             # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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








import json
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roberta import RobertaTokenizer


SCREAMING_SNAKE_CASE						:str			=  logging.get_logger(__name__)

SCREAMING_SNAKE_CASE						:Dict			=  {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

SCREAMING_SNAKE_CASE						:str			=  {
    '''vocab_file''': {
        '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/vocab.json''',
        '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/vocab.json''',
        '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json''',
        '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/vocab.json''',
        '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json''',
        '''roberta-large-openai-detector''': (
            '''https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json'''
        ),
    },
    '''merges_file''': {
        '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/merges.txt''',
        '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/merges.txt''',
        '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt''',
        '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/merges.txt''',
        '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt''',
        '''roberta-large-openai-detector''': (
            '''https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/tokenizer.json''',
        '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/tokenizer.json''',
        '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json''',
        '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json''',
        '''roberta-base-openai-detector''': (
            '''https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json'''
        ),
        '''roberta-large-openai-detector''': (
            '''https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json'''
        ),
    },
}

SCREAMING_SNAKE_CASE						:Tuple			=  {
    '''roberta-base''': 5_12,
    '''roberta-large''': 5_12,
    '''roberta-large-mnli''': 5_12,
    '''distilroberta-base''': 5_12,
    '''roberta-base-openai-detector''': 5_12,
    '''roberta-large-openai-detector''': 5_12,
}


class      __lowerCAmelCase  (			a       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							VOCAB_FILES_NAMES
  _SCREAMING_SNAKE_CASE		     =							PRETRAINED_VOCAB_FILES_MAP
  _SCREAMING_SNAKE_CASE		     =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  _SCREAMING_SNAKE_CASE		     =							['input_ids', 'attention_mask']
  _SCREAMING_SNAKE_CASE		     =							RobertaTokenizer



  def __init__(  self							:							Tuple     ,					_lowerCAmelCase							:							Union[str, Any]=None     ,					_lowerCAmelCase							:							Tuple=None     ,					_lowerCAmelCase							:							Any=None     ,					_lowerCAmelCase							:							Optional[Any]="replace"     ,					_lowerCAmelCase							:							str="<s>"     ,					_lowerCAmelCase							:							Tuple="</s>"     ,					_lowerCAmelCase							:							Any="</s>"     ,					_lowerCAmelCase							:							Tuple="<s>"     ,					_lowerCAmelCase							:							Any="<unk>"     ,					_lowerCAmelCase							:							Tuple="<pad>"     ,					_lowerCAmelCase							:							List[str]="<mask>"     ,					_lowerCAmelCase							:							str=False     ,					_lowerCAmelCase							:							Union[str, Any]=True     ,					**_lowerCAmelCase							:							int     ,					) ->			str:




     """simple docstring"""
     super().__init__(
         _lowerCAmelCase     ,					_lowerCAmelCase     ,					tokenizer_file=_lowerCAmelCase     ,					errors=_lowerCAmelCase     ,					bos_token=_lowerCAmelCase     ,					eos_token=_lowerCAmelCase     ,					sep_token=_lowerCAmelCase     ,					cls_token=_lowerCAmelCase     ,					unk_token=_lowerCAmelCase     ,					pad_token=_lowerCAmelCase     ,					mask_token=_lowerCAmelCase     ,					add_prefix_space=_lowerCAmelCase     ,					trim_offsets=_lowerCAmelCase     ,					**_lowerCAmelCase     ,					)

     snake_case_            =       json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()	)
     if pre_tok_state.get("add_prefix_space"     ,					_lowerCAmelCase	) != add_prefix_space:
        snake_case_            =       getattr(_lowerCAmelCase     ,					pre_tok_state.pop("type"	)	)
        snake_case_            =       add_prefix_space
        snake_case_            =       pre_tok_class(**_lowerCAmelCase	)

     snake_case_            =       add_prefix_space

     snake_case_            =       "post_processor"
     snake_case_            =       getattr(self.backend_tokenizer     ,					_lowerCAmelCase     ,					_lowerCAmelCase	)
     if tokenizer_component_instance:
        snake_case_            =       json.loads(tokenizer_component_instance.__getstate__()	)

        # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
        if "sep" in state:
           snake_case_            =       tuple(state["sep"]	)
        if "cls" in state:
           snake_case_            =       tuple(state["cls"]	)

        snake_case_            =       False

        if state.get("add_prefix_space"     ,					_lowerCAmelCase	) != add_prefix_space:
           snake_case_            =       add_prefix_space
           snake_case_            =       True

        if state.get("trim_offsets"     ,					_lowerCAmelCase	) != trim_offsets:
           snake_case_            =       trim_offsets
           snake_case_            =       True

        if changes_to_apply:
           snake_case_            =       getattr(_lowerCAmelCase     ,					state.pop("type"	)	)
           snake_case_            =       component_class(**_lowerCAmelCase	)
           setattr(self.backend_tokenizer     ,					_lowerCAmelCase     ,					_lowerCAmelCase	)



  @property
  def        lowerCAmelCase__	(  self							:							Dict	) ->			str:




     """simple docstring"""
     if self._mask_token is None:
        if self.verbose:
           logger.error("Using mask_token, but it is not set yet."	)
        return None
     return str(self._mask_token	)



  @mask_token.setter
  def        lowerCAmelCase__	(  self							:							Tuple     ,					_lowerCAmelCase							:							Tuple	) ->			Optional[int]:




     """simple docstring"""
     snake_case_            =       AddedToken(_lowerCAmelCase     ,					lstrip=_lowerCAmelCase     ,					rstrip=_lowerCAmelCase	) if isinstance(_lowerCAmelCase     ,					_lowerCAmelCase	) else value
     snake_case_            =       value



  def        lowerCAmelCase__	(  self							:							Dict     ,					*_lowerCAmelCase							:							Optional[int]     ,					**_lowerCAmelCase							:							Optional[int]	) ->			BatchEncoding:




     """simple docstring"""
     snake_case_            =       kwargs.get("is_split_into_words"     ,					_lowerCAmelCase	)
     assert self.add_prefix_space or not is_split_into_words, (
         F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
         "to use it with pretokenized inputs."
     )

     return super()._batch_encode_plus(*_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Optional[Any]     ,					*_lowerCAmelCase							:							str     ,					**_lowerCAmelCase							:							Any	) ->			BatchEncoding:




     """simple docstring"""
     snake_case_            =       kwargs.get("is_split_into_words"     ,					_lowerCAmelCase	)

     assert self.add_prefix_space or not is_split_into_words, (
         F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
         "to use it with pretokenized inputs."
     )

     return super()._encode_plus(*_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							int     ,					_lowerCAmelCase							:							str     ,					_lowerCAmelCase							:							Optional[str] = None	) ->			Tuple[str]:




     """simple docstring"""
     snake_case_            =       self._tokenizer.model.save(_lowerCAmelCase     ,					name=_lowerCAmelCase	)
     return tuple(_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Any     ,					_lowerCAmelCase							:							int     ,					_lowerCAmelCase							:							List[Any]=None	) ->			Union[str, Any]:




     """simple docstring"""
     snake_case_            =       [self.bos_token_id] + token_ids_a + [self.eos_token_id]
     if token_ids_a is None:
        return output

     return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]



  def        lowerCAmelCase__	(  self							:							Union[str, Any]     ,					_lowerCAmelCase							:							List[int]     ,					_lowerCAmelCase							:							Optional[List[int]] = None	) ->			List[int]:




     """simple docstring"""
     snake_case_            =       [self.sep_token_id]
     snake_case_            =       [self.cls_token_id]

     if token_ids_a is None:
        return len(cls + token_ids_a + sep	) * [0]
     return len(cls + token_ids_a + sep + sep + token_ids_a + sep	) * [0]




