


'''simple docstring'''







import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


class  __lowerCamelCase (       a_				,					a_				,					unittest.TestCase       ):
    """simple docstring"""
    a													=    StableDiffusionXLPipeline
    a													=    TEXT_TO_IMAGE_PARAMS
    a													=    TEXT_TO_IMAGE_BATCH_PARAMS
    a													=    TEXT_TO_IMAGE_IMAGE_PARAMS
    a													=    TEXT_TO_IMAGE_IMAGE_PARAMS


    def    A    (  self			:							int):
           torch.manual_seed(0)
           _A     :	Optional[Any]      =					UNetaDConditionModel(
               block_out_channels=(32, 64)      ,							layers_per_block=2      ,							sample_size=32      ,							in_channels=4      ,							out_channels=4      ,							down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')      ,							up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')      ,							attention_head_dim=(2, 4)      ,							use_linear_projection=SCREAMING_SNAKE_CASE      ,							addition_embed_type='text_time'      ,							addition_time_embed_dim=8      ,							transformer_layers_per_block=(1, 2)      ,							projection_class_embeddings_input_dim=80      ,							cross_attention_dim=64      ,							)
           _A     :	List[str]      =					EulerDiscreteScheduler(
               beta_start=0.0_0085      ,							beta_end=0.012      ,							steps_offset=1      ,							beta_schedule='scaled_linear'      ,							timestep_spacing='leading'      ,							)
           torch.manual_seed(0)
           _A     :	List[Any]      =					AutoencoderKL(
               block_out_channels=[32, 64]      ,							in_channels=3      ,							out_channels=3      ,							down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']      ,							up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']      ,							latent_channels=4      ,							sample_size=128      ,							)
           torch.manual_seed(0)
           _A     :	Any      =					CLIPTextConfig(
               bos_token_id=0      ,							eos_token_id=2      ,							hidden_size=32      ,							intermediate_size=37      ,							layer_norm_eps=1e-05      ,							num_attention_heads=4      ,							num_hidden_layers=5      ,							pad_token_id=1      ,							vocab_size=1000      ,							hidden_act='gelu'      ,							projection_dim=32      ,							)
           _A     :	Any      =					CLIPTextModel(SCREAMING_SNAKE_CASE)
           _A     :	Union[str, Any]      =					CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'      ,							local_files_only=SCREAMING_SNAKE_CASE)

           _A     :	List[Any]      =					CLIPTextModelWithProjection(SCREAMING_SNAKE_CASE)
           _A     :	Optional[Any]      =					CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'      ,							local_files_only=SCREAMING_SNAKE_CASE)

           _A     :	Optional[int]      =					{
               'unet': unet,
               'scheduler': scheduler,
               'vae': vae,
               'text_encoder': text_encoder,
               'tokenizer': tokenizer,
               'text_encoder_2': text_encoder_a,
               'tokenizer_2': tokenizer_a,
               # "safety_checker": None,
               # "feature_extractor": None,
           }
           return components


    def    A    (  self			:							Union[str, Any]      ,							SCREAMING_SNAKE_CASE			:							List[str]      ,							SCREAMING_SNAKE_CASE			:							Tuple=0):
           if str(SCREAMING_SNAKE_CASE).startswith('mps'):
                  _A     :	List[Any]      =					torch.manual_seed(SCREAMING_SNAKE_CASE)
           else:
                  _A     :	Optional[Any]      =					torch.Generator(device=SCREAMING_SNAKE_CASE).manual_seed(SCREAMING_SNAKE_CASE)
           _A     :	List[Any]      =					{
               'prompt': 'A painting of a squirrel eating a burger',
               'generator': generator,
               'num_inference_steps': 2,
               'guidance_scale': 5.0,
               'output_type': 'numpy',
           }
           return inputs


    def    A    (  self			:							List[Any]):
           _A     :	Any      =					'cpu'  # ensure determinism for the device-dependent torch.Generator
           _A     :	int      =					self.get_dummy_components()
           _A     :	List[Any]      =					StableDiffusionXLPipeline(**SCREAMING_SNAKE_CASE)
           _A     :	List[str]      =					sd_pipe.to(SCREAMING_SNAKE_CASE)
           sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE)

           _A     :	int      =					self.get_dummy_inputs(SCREAMING_SNAKE_CASE)
           _A     :	Union[str, Any]      =					sd_pipe(**SCREAMING_SNAKE_CASE).images
           _A     :	str      =					image[0, -3:, -3:, -1]

           assert image.shape == (1, 64, 64, 3)
           _A     :	List[Any]      =					np.array([0.5873, 0.6128, 0.4797, 0.5122, 0.5674, 0.4639, 0.5227, 0.5149, 0.4747])

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


    def    A    (  self			:							str):
           _A     :	Union[str, Any]      =					self.get_dummy_components()
           _A     :	Union[str, Any]      =					StableDiffusionXLPipeline(**SCREAMING_SNAKE_CASE)
           _A     :	Optional[int]      =					sd_pipe.to(SCREAMING_SNAKE_CASE)
           _A     :	Dict      =					sd_pipe.to(SCREAMING_SNAKE_CASE)
           sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE)

           # forward without prompt embeds
           _A     :	Optional[int]      =					self.get_dummy_inputs(SCREAMING_SNAKE_CASE)
           _A     :	str      =					3 * ['this is a negative prompt']
           _A     :	int      =					negative_prompt
           _A     :	Any      =					3 * [inputs['prompt']]

           _A     :	Union[str, Any]      =					sd_pipe(**SCREAMING_SNAKE_CASE)
           _A     :	List[Any]      =					output.images[0, -3:, -3:, -1]

           # forward with prompt embeds
           _A     :	str      =					self.get_dummy_inputs(SCREAMING_SNAKE_CASE)
           _A     :	int      =					3 * ['this is a negative prompt']
           _A     :	Optional[Any]      =					3 * [inputs.pop('prompt')]

           (
               (
               _A
           )  , (
               _A
           )  , (
               _A
           )  , (
               _A
           )  , 
           )     :	Tuple      =					sd_pipe.encode_prompt(SCREAMING_SNAKE_CASE      ,							negative_prompt=SCREAMING_SNAKE_CASE)

           _A     :	Tuple      =					sd_pipe(
               **SCREAMING_SNAKE_CASE      ,							prompt_embeds=SCREAMING_SNAKE_CASE      ,							negative_prompt_embeds=SCREAMING_SNAKE_CASE      ,							pooled_prompt_embeds=SCREAMING_SNAKE_CASE      ,							negative_pooled_prompt_embeds=SCREAMING_SNAKE_CASE      ,							)
           _A     :	Dict      =					output.images[0, -3:, -3:, -1]

           # make sure that it's equal
           assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()).max() < 1e-4


    def    A    (  self			:							Optional[int]):
           super().test_attention_slicing_forward_pass(expected_max_diff=3e-3)


    def    A    (  self			:							List[str]):
           super().test_inference_batch_single_identical(expected_max_diff=3e-3)




@slow
@require_torch_gpu
class  __lowerCamelCase (       unittest.TestCase       ):
    """simple docstring"""


    def    A    (  self			:							List[str]):
           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()


    def    A    (  self			:							int      ,							SCREAMING_SNAKE_CASE			:							Union[str, Any]      ,							SCREAMING_SNAKE_CASE			:							Any="cpu"      ,							SCREAMING_SNAKE_CASE			:							Optional[int]=torch.floataa      ,							SCREAMING_SNAKE_CASE			:							Optional[Any]=0):
           _A     :	Optional[Any]      =					torch.Generator(device=SCREAMING_SNAKE_CASE).manual_seed(SCREAMING_SNAKE_CASE)
           _A     :	List[str]      =					np.random.RandomState(SCREAMING_SNAKE_CASE).standard_normal((1, 4, 64, 64))
           _A     :	Tuple      =					torch.from_numpy(SCREAMING_SNAKE_CASE).to(device=SCREAMING_SNAKE_CASE      ,							dtype=SCREAMING_SNAKE_CASE)
           _A     :	List[str]      =					{
               'prompt': 'a photograph of an astronaut riding a horse',
               'latents': latents,
               'generator': generator,
               'num_inference_steps': 3,
               'guidance_scale': 7.5,
               'output_type': 'numpy',
           }
           return inputs


    def    A    (  self			:							Union[str, Any]):
           _A     :	Optional[Any]      =					DiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-base')
           pipe.to(SCREAMING_SNAKE_CASE)
           pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE)

           _A     :	Optional[int]      =					self.get_inputs(SCREAMING_SNAKE_CASE)
           _A     :	List[str]      =					pipe(**SCREAMING_SNAKE_CASE).images
           _A     :	str      =					image[0, -3:, -3:, -1].flatten()

           assert image.shape == (1, 512, 512, 3)
           _A     :	Optional[int]      =					np.array([0.4_9493, 0.4_7896, 0.4_0798, 0.5_4214, 0.5_3212, 0.4_8202, 0.4_7656, 0.4_6329, 0.4_8506])
           assert np.abs(image_slice - expected_slice).max() < 7e-3










'''simple docstring'''







import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
   from .tokenization_xlm_roberta import XLMRobertaTokenizer
else:
   A      :     Optional[int]										= None


A      :     Optional[Any]										= logging.get_logger(__name__)

A      :     Dict										= {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''}

A      :     Optional[int]										= {
    '''vocab_file''': {
        '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/sentencepiece.bpe.model''',
        '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/sentencepiece.bpe.model''',
        '''xlm-roberta-large-finetuned-conll02-dutch''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/sentencepiece.bpe.model'''
        ),
        '''xlm-roberta-large-finetuned-conll02-spanish''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/sentencepiece.bpe.model'''
        ),
        '''xlm-roberta-large-finetuned-conll03-english''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/sentencepiece.bpe.model'''
        ),
        '''xlm-roberta-large-finetuned-conll03-german''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/sentencepiece.bpe.model'''
        ),
    },
    '''tokenizer_file''': {
        '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/tokenizer.json''',
        '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/tokenizer.json''',
        '''xlm-roberta-large-finetuned-conll02-dutch''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/tokenizer.json'''
        ),
        '''xlm-roberta-large-finetuned-conll02-spanish''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/tokenizer.json'''
        ),
        '''xlm-roberta-large-finetuned-conll03-english''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/tokenizer.json'''
        ),
        '''xlm-roberta-large-finetuned-conll03-german''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/tokenizer.json'''
        ),
    },
}

A      :     List[str]										= {
    '''xlm-roberta-base''': 512,
    '''xlm-roberta-large''': 512,
    '''xlm-roberta-large-finetuned-conll02-dutch''': 512,
    '''xlm-roberta-large-finetuned-conll02-spanish''': 512,
    '''xlm-roberta-large-finetuned-conll03-english''': 512,
    '''xlm-roberta-large-finetuned-conll03-german''': 512,
}


class  __lowerCamelCase (       a_       ):
    """simple docstring"""
    a													=    VOCAB_FILES_NAMES
    a													=    PRETRAINED_VOCAB_FILES_MAP
    a													=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    a													=    ["input_ids", "attention_mask"]
    a													=    XLMRobertaTokenizer


    def __init__(  self			:							Union[str, Any]      ,							SCREAMING_SNAKE_CASE			:							Dict=None      ,							SCREAMING_SNAKE_CASE			:							List[Any]=None      ,							SCREAMING_SNAKE_CASE			:							Any="<s>"      ,							SCREAMING_SNAKE_CASE			:							Tuple="</s>"      ,							SCREAMING_SNAKE_CASE			:							str="</s>"      ,							SCREAMING_SNAKE_CASE			:							Optional[Any]="<s>"      ,							SCREAMING_SNAKE_CASE			:							Any="<unk>"      ,							SCREAMING_SNAKE_CASE			:							Union[str, Any]="<pad>"      ,							SCREAMING_SNAKE_CASE			:							List[Any]="<mask>"      ,							**SCREAMING_SNAKE_CASE			:							int      ,							):
           # Mask token behave like a normal word, i.e. include the space before it
           _A     :	str      =					AddedToken(SCREAMING_SNAKE_CASE      ,							lstrip=SCREAMING_SNAKE_CASE      ,							rstrip=SCREAMING_SNAKE_CASE) if isinstance(SCREAMING_SNAKE_CASE      ,							SCREAMING_SNAKE_CASE) else mask_token

           super().__init__(
               SCREAMING_SNAKE_CASE      ,							tokenizer_file=SCREAMING_SNAKE_CASE      ,							bos_token=SCREAMING_SNAKE_CASE      ,							eos_token=SCREAMING_SNAKE_CASE      ,							sep_token=SCREAMING_SNAKE_CASE      ,							cls_token=SCREAMING_SNAKE_CASE      ,							unk_token=SCREAMING_SNAKE_CASE      ,							pad_token=SCREAMING_SNAKE_CASE      ,							mask_token=SCREAMING_SNAKE_CASE      ,							**SCREAMING_SNAKE_CASE      ,							)

           _A     :	Dict      =					vocab_file
           _A     :	Optional[Any]      =					False if not self.vocab_file else True


    def    A    (  self			:							int      ,							SCREAMING_SNAKE_CASE			:							List[int]      ,							SCREAMING_SNAKE_CASE			:							Optional[List[int]] = None):

           if token_ids_a is None:
                  return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
           _A     :	Optional[Any]      =					[self.cls_token_id]
           _A     :	Optional[int]      =					[self.sep_token_id]
           return cls + token_ids_a + sep + sep + token_ids_a + sep


    def    A    (  self			:							List[str]      ,							SCREAMING_SNAKE_CASE			:							List[int]      ,							SCREAMING_SNAKE_CASE			:							Optional[List[int]] = None):
           _A     :	Any      =					[self.sep_token_id]
           _A     :	Optional[Any]      =					[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]


    def    A    (  self			:							Dict      ,							SCREAMING_SNAKE_CASE			:							str      ,							SCREAMING_SNAKE_CASE			:							Optional[str] = None):
           if not self.can_save_slow_tokenizer:
                  raise ValueError(
                      'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '
                      'tokenizer.')

           if not os.path.isdir(SCREAMING_SNAKE_CASE):
                  logger.error(F'Vocabulary path ({save_directory}) should be a directory.')
                  return
           _A     :	Any      =					os.path.join(
               SCREAMING_SNAKE_CASE      ,							(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'])

           if os.path.abspath(self.vocab_file) != os.path.abspath(SCREAMING_SNAKE_CASE):
                  copyfile(self.vocab_file      ,							SCREAMING_SNAKE_CASE)

           return (out_vocab_file,)






