




"""simple docstring"""




import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import XLMRobertaTokenizerFast

from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP
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

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()






class     __magic_name__							(       UpperCAmelCase__						,					unittest.TestCase				):






				'''simple docstring'''
				__UpperCamelCase			  =						KandinskyImgaImgPipeline
				__UpperCamelCase			  =						["prompt", "image_embeds", "negative_image_embeds", "image"]
				__UpperCamelCase			  =						[
				    "prompt",
				    "negative_prompt",
				    "image_embeds",
				    "negative_image_embeds",
				    "image",
				]
				__UpperCamelCase			  =						[
				    "generator",
				    "height",
				    "width",
				    "strength",
				    "guidance_scale",
				    "negative_prompt",
				    "num_inference_steps",
				    "return_dict",
				    "guidance_scale",
				    "num_images_per_prompt",
				    "output_type",
				    "return_dict",
				]
				__UpperCamelCase			  =						False





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return 32





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return 32





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return self.time_input_dim





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return self.time_input_dim * 4





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return 100





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					XLMRobertaTokenizerFast.from_pretrained("""YiYiXu/tiny-random-mclip-base""" )
							return tokenizer





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							torch.manual_seed(0 )
							lowerCamelCase			      =					MCLIPConfig(
							    numDims=self.cross_attention_dim		, transformerDimensions=self.text_embedder_hidden_size		, hidden_size=self.text_embedder_hidden_size		, intermediate_size=37		, num_attention_heads=4		, num_hidden_layers=5		, vocab_size=1_005		, )

							lowerCamelCase			      =					MultilingualCLIP(_a )
							lowerCamelCase			      =					text_encoder.eval()

							return text_encoder





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							torch.manual_seed(0 )

							lowerCamelCase			      =					{
							    """in_channels""": 4,
							    # Out channels is double in channels because predicts mean and variance
							    """out_channels""": 8,
							    """addition_embed_type""": """text_image""",
							    """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""),
							    """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""),
							    """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""",
							    """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2),
							    """layers_per_block""": 1,
							    """encoder_hid_dim""": self.text_embedder_hidden_size,
							    """encoder_hid_dim_type""": """text_image_proj""",
							    """cross_attention_dim""": self.cross_attention_dim,
							    """attention_head_dim""": 4,
							    """resnet_time_scale_shift""": """scale_shift""",
							    """class_embed_type""": None,
							}

							lowerCamelCase			      =					UNetaDConditionModel(**_a )
							return model





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return {
							    "block_out_channels": [32, 64],
							    "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
							    "in_channels": 3,
							    "latent_channels": 4,
							    "layers_per_block": 1,
							    "norm_num_groups": 8,
							    "norm_type": "spatial",
							    "num_vq_embeddings": 12,
							    "out_channels": 3,
							    "up_block_types": [
							        "AttnUpDecoderBlock2D",
							        "UpDecoderBlock2D",
							    ],
							    "vq_embed_dim": 4,
							}





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							torch.manual_seed(0 )
							lowerCamelCase			      =					VQModel(**self.dummy_movq_kwargs )
							return model





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.dummy_text_encoder
							lowerCamelCase			      =					self.dummy_tokenizer
							lowerCamelCase			      =					self.dummy_unet
							lowerCamelCase			      =					self.dummy_movq

							lowerCamelCase			      =					{
							    """num_train_timesteps""": 1_000,
							    """beta_schedule""": """linear""",
							    """beta_start""": 0.00_085,
							    """beta_end""": 0.012,
							    """clip_sample""": False,
							    """set_alpha_to_one""": False,
							    """steps_offset""": 0,
							    """prediction_type""": """epsilon""",
							    """thresholding""": False,
							}

							lowerCamelCase			      =					DDIMScheduler(**_a )

							lowerCamelCase			      =					{
							    """text_encoder""": text_encoder,
							    """tokenizer""": tokenizer,
							    """unet""": unet,
							    """scheduler""": scheduler,
							    """movq""": movq,
							}

							return components





				def    _lowerCAmelCase				(       self		, _a		, _a=0 ):





							"""simple docstring"""
							lowerCamelCase			      =					floats_tensor((1, self.cross_attention_dim)		, rng=random.Random(_a ) ).to(_a )
							lowerCamelCase			      =					floats_tensor((1, self.cross_attention_dim)		, rng=random.Random(seed + 1 ) ).to(_a )
							# create init_image
							lowerCamelCase			      =					floats_tensor((1, 3, 64, 64)		, rng=random.Random(_a ) ).to(_a )
							lowerCamelCase			      =					image.cpu().permute(0		, 2		, 3		, 1 )[0]
							lowerCamelCase			      =					Image.fromarray(np.uinta(_a ) ).convert("""RGB""" ).resize((256, 256) )

							if str(_a ).startswith("""mps""" ):
										lowerCamelCase			      =					torch.manual_seed(_a )
							else:
										lowerCamelCase			      =					torch.Generator(device=_a ).manual_seed(_a )
							lowerCamelCase			      =					{
							    """prompt""": """horse""",
							    """image""": init_image,
							    """image_embeds""": image_embeds,
							    """negative_image_embeds""": negative_image_embeds,
							    """generator""": generator,
							    """height""": 64,
							    """width""": 64,
							    """num_inference_steps""": 10,
							    """guidance_scale""": 7.0,
							    """strength""": 0.2,
							    """output_type""": """np""",
							}
							return inputs





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					"""cpu"""

							lowerCamelCase			      =					self.get_dummy_components()

							lowerCamelCase			      =					self.pipeline_class(**_a )
							lowerCamelCase			      =					pipe.to(_a )

							pipe.set_progress_bar_config(disable=_a )

							lowerCamelCase			      =					pipe(**self.get_dummy_inputs(_a ) )
							lowerCamelCase			      =					output.images

							lowerCamelCase			      =					pipe(
							    **self.get_dummy_inputs(_a )		, return_dict=_a		, )[0]

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

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

							lowerCamelCase			      =					np.array(
							    [0.61_474_943, 0.6_073_539, 0.43_308_544, 0.5_928_269, 0.47_493_595, 0.46_755_973, 0.4_613_838, 0.45_368_797, 0.50_119_233] )
							assert (
							    np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
							), f' expected_slice {expected_slice}, but got {image_slice.flatten()}'
							assert (
							    np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2
							), f' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}'






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






				'''simple docstring'''





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					load_numpy(
							    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
							    """/kandinsky/kandinsky_img2img_frog.npy""" )

							lowerCamelCase			      =					load_image(
							    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" )
							lowerCamelCase			      =					"""A red cartoon frog, 4k"""

							lowerCamelCase			      =					KandinskyPriorPipeline.from_pretrained(
							    """kandinsky-community/kandinsky-2-1-prior"""		, torch_dtype=torch.floataa )
							pipe_prior.to(_a )

							lowerCamelCase			      =					KandinskyImgaImgPipeline.from_pretrained(
							    """kandinsky-community/kandinsky-2-1"""		, torch_dtype=torch.floataa )
							lowerCamelCase			      =					pipeline.to(_a )

							pipeline.set_progress_bar_config(disable=_a )

							lowerCamelCase			      =					torch.Generator(device="""cpu""" ).manual_seed(0 )
							lowerCamelCase      ,   lowerCamelCase			      =					pipe_prior(
							    _a		, generator=_a		, num_inference_steps=5		, negative_prompt=""""""		, ).to_tuple()

							lowerCamelCase			      =					pipeline(
							    _a		, image=_a		, image_embeds=_a		, negative_image_embeds=_a		, generator=_a		, num_inference_steps=100		, height=768		, width=768		, strength=0.2		, output_type="""np"""		, )

							lowerCamelCase			      =					output.images[0]

							assert image.shape == (768, 768, 3)

							assert_mean_pixel_difference(_a		, _a )









"""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_xlnet import XLNetTokenizer
else:
					lowerCAmelCase							:			int     							=							None


lowerCAmelCase							:			Tuple     							=							logging.get_logger(__name__)

lowerCAmelCase							:			Tuple     							=							{"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""}

lowerCAmelCase							:			Union[str, Any]     							=							{
    """vocab_file""": {
        """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model""",
        """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model""",
    },
    """tokenizer_file""": {
        """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json""",
        """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json""",
    },
}

lowerCAmelCase							:			Optional[int]     							=							{
    """xlnet-base-cased""": None,
    """xlnet-large-cased""": None,
}

lowerCAmelCase							:			Union[str, Any]     							=							"""▁"""

# Segments (not really needed)
lowerCAmelCase							:			str     							=							0
lowerCAmelCase							:			Optional[int]     							=							1
lowerCAmelCase							:			Tuple     							=							2
lowerCAmelCase							:			Optional[Any]     							=							3
lowerCAmelCase							:			List[Any]     							=							4






class     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''
				__UpperCamelCase			  =						VOCAB_FILES_NAMES
				__UpperCamelCase			  =						PRETRAINED_VOCAB_FILES_MAP
				__UpperCamelCase			  =						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				__UpperCamelCase			  =						"left"
				__UpperCamelCase			  =						XLNetTokenizer





				def __init__(       self		, _a=None		, _a=None		, _a=False		, _a=True		, _a=False		, _a="<s>"		, _a="</s>"		, _a="<unk>"		, _a="<sep>"		, _a="<pad>"		, _a="<cls>"		, _a="<mask>"		, _a=["<eop>", "<eod>"]		, **_a		, ):





							"""simple docstring"""
							# Mask token behave like a normal word, i.e. include the space before it
							lowerCamelCase			      =					AddedToken(_a		, lstrip=_a		, rstrip=_a ) if isinstance(_a		, _a ) else mask_token

							super().__init__(
							    vocab_file=_a		, tokenizer_file=_a		, do_lower_case=_a		, remove_space=_a		, keep_accents=_a		, bos_token=_a		, eos_token=_a		, unk_token=_a		, sep_token=_a		, pad_token=_a		, cls_token=_a		, mask_token=_a		, additional_special_tokens=_a		, **_a		, )

							lowerCamelCase			      =					3
							lowerCamelCase			      =					do_lower_case
							lowerCamelCase			      =					remove_space
							lowerCamelCase			      =					keep_accents
							lowerCamelCase			      =					vocab_file
							lowerCamelCase			      =					False if not self.vocab_file else True





				def    _lowerCAmelCase				(       self		, _a		, _a = None ):





							"""simple docstring"""
							lowerCamelCase			      =					[self.sep_token_id]
							lowerCamelCase			      =					[self.cls_token_id]
							if token_ids_a is None:
										return token_ids_a + sep + cls
							return token_ids_a + sep + token_ids_a + sep + cls





				def    _lowerCAmelCase				(       self		, _a		, _a = None ):





							"""simple docstring"""
							lowerCamelCase			      =					[self.sep_token_id]
							lowerCamelCase			      =					[2]

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





				def    _lowerCAmelCase				(       self		, _a		, _a = None ):





							"""simple docstring"""
							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(_a ):
										logger.error(f'Vocabulary path ({save_directory}) should be a directory' )
										return
							lowerCamelCase			      =					os.path.join(
							    _a		, (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] )

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

							return (out_vocab_file,)



