



import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()







class       __lowerCAmelCase   (      unittest.TestCase						):



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



						"""simple docstring"""






						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()



			@property
			def    _lowerCamelCase	(						self       :  Tuple)       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       1
						_UpperCAmelCase			=       3
						_UpperCAmelCase			=       (32, 32)

						_UpperCAmelCase			=       floats_tensor((batch_size, num_channels) + sizes			,		rng=random.Random(0)).to(A)
						return image



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



						"""simple docstring"""






						torch.manual_seed(0)
						_UpperCAmelCase			=       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')			,		cross_attention_dim=32			,		)
						return model



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



						"""simple docstring"""






						torch.manual_seed(0)
						_UpperCAmelCase			=       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			,		)
						return model



			@property
			def    _lowerCamelCase	(						self       :  Optional[Any])       ->   Tuple:



						"""simple docstring"""






						torch.manual_seed(0)
						_UpperCAmelCase			=       RobertaSeriesConfig(
						    hidden_size=32			,		project_dim=32			,		intermediate_size=37			,		layer_norm_eps=1E-05			,		num_attention_heads=4			,		num_hidden_layers=5			,		pad_token_id=1			,		vocab_size=50_06			,		)
						return RobertaSeriesModelWithTransformation(A)



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



						"""simple docstring"""






						def extract(*A       :  Union[str, Any]			,		**A       :  Optional[int]):







									class       __lowerCAmelCase   :



												def __init__(						self       :  Optional[int])       ->   Tuple:



															"""simple docstring"""






															_UpperCAmelCase			=       torch.ones([0])



												def    _lowerCamelCase	(						self       :  int			,		A       :  Optional[Any])       ->   List[Any]:



															"""simple docstring"""






															self.pixel_values.to(A)
															return self





									return Out()

						return extract



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



						"""simple docstring"""






						_UpperCAmelCase			=       'cpu'  # ensure determinism for the device-dependent torch.Generator
						_UpperCAmelCase			=       self.dummy_cond_unet
						_UpperCAmelCase			=       PNDMScheduler(skip_prk_steps=A)
						_UpperCAmelCase			=       self.dummy_vae
						_UpperCAmelCase			=       self.dummy_text_encoder
						_UpperCAmelCase			=       XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta')
						_UpperCAmelCase			=       77

						_UpperCAmelCase			=       self.dummy_image.to(A)
						_UpperCAmelCase			=       init_image / 2 + 0.5

						# make sure here that pndm scheduler skips prk
						_UpperCAmelCase			=       AltDiffusionImgaImgPipeline(
						    unet=A			,		scheduler=A			,		vae=A			,		text_encoder=A			,		tokenizer=A			,		safety_checker=A			,		feature_extractor=self.dummy_extractor			,		)
						_UpperCAmelCase			=       VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor			,		do_normalize=A)
						_UpperCAmelCase			=       alt_pipe.to(A)
						alt_pipe.set_progress_bar_config(disable=A)

						_UpperCAmelCase			=       'A painting of a squirrel eating a burger'
						_UpperCAmelCase			=       torch.Generator(device=A).manual_seed(0)
						_UpperCAmelCase			=       alt_pipe(
						    [prompt]			,		generator=A			,		guidance_scale=6.0			,		num_inference_steps=2			,		output_type='np'			,		image=A			,		)

						_UpperCAmelCase			=       output.images

						_UpperCAmelCase			=       torch.Generator(device=A).manual_seed(0)
						_UpperCAmelCase			=       alt_pipe(
						    [prompt]			,		generator=A			,		guidance_scale=6.0			,		num_inference_steps=2			,		output_type='np'			,		image=A			,		return_dict=A			,		)[0]

						_UpperCAmelCase			=       image[0, -3:, -3:, -1]
						_UpperCAmelCase			=       image_from_tuple[0, -3:, -3:, -1]

						assert image.shape == (1, 32, 32, 3)
						_UpperCAmelCase			=       np.array([0.4_4_2_7, 0.3_7_3_1, 0.4_2_4_9, 0.4_9_4_1, 0.4_5_4_6, 0.4_1_4_8, 0.4_1_9_3, 0.4_6_6_6, 0.4_4_9_9])

						assert np.abs(image_slice.flatten() - expected_slice).max() < 5E-3
						assert np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 5E-3



			@unittest.skipIf(torch_device != 'cuda'			,		'This test requires a GPU')
			def    _lowerCamelCase	(						self       :  Optional[Any])       ->   Tuple:



						"""simple docstring"""






						_UpperCAmelCase			=       self.dummy_cond_unet
						_UpperCAmelCase			=       PNDMScheduler(skip_prk_steps=A)
						_UpperCAmelCase			=       self.dummy_vae
						_UpperCAmelCase			=       self.dummy_text_encoder
						_UpperCAmelCase			=       XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta')
						_UpperCAmelCase			=       77

						_UpperCAmelCase			=       self.dummy_image.to(A)

						# put models in fp16
						_UpperCAmelCase			=       unet.half()
						_UpperCAmelCase			=       vae.half()
						_UpperCAmelCase			=       bert.half()

						# make sure here that pndm scheduler skips prk
						_UpperCAmelCase			=       AltDiffusionImgaImgPipeline(
						    unet=A			,		scheduler=A			,		vae=A			,		text_encoder=A			,		tokenizer=A			,		safety_checker=A			,		feature_extractor=self.dummy_extractor			,		)
						_UpperCAmelCase			=       VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor			,		do_normalize=A)
						_UpperCAmelCase			=       alt_pipe.to(A)
						alt_pipe.set_progress_bar_config(disable=A)

						_UpperCAmelCase			=       'A painting of a squirrel eating a burger'
						_UpperCAmelCase			=       torch.manual_seed(0)
						_UpperCAmelCase			=       alt_pipe(
						    [prompt]			,		generator=A			,		num_inference_steps=2			,		output_type='np'			,		image=A			,		).images

						assert image.shape == (1, 32, 32, 3)



			@unittest.skipIf(torch_device != 'cuda'			,		'This test requires a GPU')
			def    _lowerCamelCase	(						self       :  str)       ->   Dict:



						"""simple docstring"""






						_UpperCAmelCase			=       load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
						    '/img2img/sketch-mountains-input.jpg')
						# resize to resolution that is divisible by 8 but not 16 or 32
						_UpperCAmelCase			=       init_image.resize((7_60, 5_04))

						_UpperCAmelCase			=       'BAAI/AltDiffusion'
						_UpperCAmelCase			=       AltDiffusionImgaImgPipeline.from_pretrained(
						    A			,		safety_checker=A			,		)
						pipe.to(A)
						pipe.set_progress_bar_config(disable=A)
						pipe.enable_attention_slicing()

						_UpperCAmelCase			=       'A fantasy landscape, trending on artstation'

						_UpperCAmelCase			=       torch.manual_seed(0)
						_UpperCAmelCase			=       pipe(
						    prompt=A			,		image=A			,		strength=0.7_5			,		guidance_scale=7.5			,		generator=A			,		output_type='np'			,		)
						_UpperCAmelCase			=       output.images[0]

						_UpperCAmelCase			=       image[2_55:2_58, 3_83:3_86, -1]

						assert image.shape == (5_04, 7_60, 3)
						_UpperCAmelCase			=       np.array([0.9_3_5_8, 0.9_3_9_7, 0.9_5_9_9, 0.9_9_0_1, 1.0_0_0_0, 1.0_0_0_0, 0.9_8_8_2, 1.0_0_0_0, 1.0_0_0_0])

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







@slow
@require_torch_gpu
class       __lowerCAmelCase   (      unittest.TestCase						):



			def    _lowerCamelCase	(						self       :  int)       ->   Tuple:



						"""simple docstring"""






						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()



			def    _lowerCamelCase	(						self       :  Optional[Any])       ->   Tuple:



						"""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((7_68, 5_12))
						_UpperCAmelCase			=       load_numpy(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy')

						_UpperCAmelCase			=       'BAAI/AltDiffusion'
						_UpperCAmelCase			=       AltDiffusionImgaImgPipeline.from_pretrained(
						    A			,		safety_checker=A			,		)
						pipe.to(A)
						pipe.set_progress_bar_config(disable=A)
						pipe.enable_attention_slicing()

						_UpperCAmelCase			=       'A fantasy landscape, trending on artstation'

						_UpperCAmelCase			=       torch.manual_seed(0)
						_UpperCAmelCase			=       pipe(
						    prompt=A			,		image=A			,		strength=0.7_5			,		guidance_scale=7.5			,		generator=A			,		output_type='np'			,		)
						_UpperCAmelCase			=       output.images[0]

						assert image.shape == (5_12, 7_68, 3)
						# img2img is flaky across GPUs even in fp32, so using MAE here
						assert np.abs(expected_image - image).max() < 1E-2





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers
from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roformer import RoFormerTokenizer
from .tokenization_utils import JiebaPreTokenizer


UpperCAmelCase__       =	logging.get_logger(__name__)

UpperCAmelCase__       =	{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

UpperCAmelCase__       =	{
    "vocab_file": {
        "junnyu/roformer_chinese_small": "https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt",
        "junnyu/roformer_chinese_base": "https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt",
        "junnyu/roformer_chinese_char_small": (
            "https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt"
        ),
        "junnyu/roformer_chinese_char_base": (
            "https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt"
        ),
        "junnyu/roformer_small_discriminator": (
            "https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt"
        ),
        "junnyu/roformer_small_generator": (
            "https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt"
        ),
    }
}

UpperCAmelCase__       =	{
    "junnyu/roformer_chinese_small": 1536,
    "junnyu/roformer_chinese_base": 1536,
    "junnyu/roformer_chinese_char_small": 512,
    "junnyu/roformer_chinese_char_base": 512,
    "junnyu/roformer_small_discriminator": 128,
    "junnyu/roformer_small_generator": 128,
}


UpperCAmelCase__       =	{
    "junnyu/roformer_chinese_small": {"do_lower_case": True},
    "junnyu/roformer_chinese_base": {"do_lower_case": True},
    "junnyu/roformer_chinese_char_small": {"do_lower_case": True},
    "junnyu/roformer_chinese_char_base": {"do_lower_case": True},
    "junnyu/roformer_small_discriminator": {"do_lower_case": True},
    "junnyu/roformer_small_generator": {"do_lower_case": True},
}







class       __lowerCAmelCase   (      A						):
			UpperCamelCase        =					VOCAB_FILES_NAMES
			UpperCamelCase        =					PRETRAINED_VOCAB_FILES_MAP
			UpperCamelCase        =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			UpperCamelCase        =					PRETRAINED_INIT_CONFIGURATION
			UpperCamelCase        =					RoFormerTokenizer



			def __init__(						self       :  Dict			,		A       :  Any=None			,		A       :  Optional[Any]=None			,		A       :  Union[str, Any]=True			,		A       :  List[str]="[UNK]"			,		A       :  List[str]="[SEP]"			,		A       :  Union[str, Any]="[PAD]"			,		A       :  Any="[CLS]"			,		A       :  str="[MASK]"			,		A       :  Optional[int]=True			,		A       :  str=None			,		**A       :  Dict			,		)       ->   Union[str, Any]:



						"""simple docstring"""






						super().__init__(
						    A			,		tokenizer_file=A			,		do_lower_case=A			,		unk_token=A			,		sep_token=A			,		pad_token=A			,		cls_token=A			,		mask_token=A			,		tokenize_chinese_chars=A			,		strip_accents=A			,		**A			,		)

						_UpperCAmelCase			=       json.loads(self.backend_tokenizer.normalizer.__getstate__())
						if (
						    pre_tok_state.get('lowercase'			,		A) != do_lower_case
						    or pre_tok_state.get('strip_accents'			,		A) != strip_accents
						):
									_UpperCAmelCase			=       getattr(A			,		pre_tok_state.pop('type'))
									_UpperCAmelCase			=       do_lower_case
									_UpperCAmelCase			=       strip_accents
									_UpperCAmelCase			=       pre_tok_class(**A)

						_UpperCAmelCase			=       do_lower_case



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



						"""simple docstring"""






						_UpperCAmelCase			=       self.__dict__.copy()
						_UpperCAmelCase			=       BertPreTokenizer()
						return state



			def __setstate__(						self       :  List[Any]			,		A       :  Optional[Any])       ->   List[str]:



						"""simple docstring"""






						_UpperCAmelCase			=       d
						_UpperCAmelCase			=       self.__dict__['_tokenizer'].get_vocab()
						_UpperCAmelCase			=       PreTokenizer.custom(JiebaPreTokenizer(A))



			def    _lowerCamelCase	(						self       :  List[Any]			,		A       :  Optional[int]			,		A       :  List[Any]=None)       ->   Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       [self.cls_token_id] + token_ids_a + [self.sep_token_id]

						if token_ids_a:
									output += token_ids_a + [self.sep_token_id]

						return output



			def    _lowerCamelCase	(						self       :  List[str]			,		A       :  List[int]			,		A       :  Optional[List[int]] = None)       ->   List[int]:



						"""simple docstring"""






						_UpperCAmelCase			=       [self.sep_token_id]
						_UpperCAmelCase			=       [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) * [0] + len(token_ids_a + sep) * [1]



			def    _lowerCamelCase	(						self       :  Union[str, Any]			,		A       :  str			,		A       :  Optional[str] = None)       ->   Tuple[str]:



						"""simple docstring"""






						_UpperCAmelCase			=       self._tokenizer.model.save(A			,		name=A)
						return tuple(A)



			def    _lowerCamelCase	(						self       :  List[Any]			,		A       :  Tuple			,		A       :  str=None			,		A       :  Union[str, Any]=None			,		A       :  Union[str, Any]=False			,		**A       :  Tuple			,		)       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       BertPreTokenizer()
						return super().save_pretrained(A			,		A			,		A			,		A			,		**A)
