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



						'''simple docstring'''


						_lowerCamelCase:    Optional[int]      =    KandinskyImgaImgPipeline
						_lowerCamelCase:    Dict      =    ['''prompt''', '''image_embeds''', '''negative_image_embeds''', '''image''']
						_lowerCamelCase:    Any      =    [
						    '''prompt''',
						    '''negative_prompt''',
						    '''image_embeds''',
						    '''negative_image_embeds''',
						    '''image''',
						]
						_lowerCamelCase:    Dict      =    [
						    '''generator''',
						    '''height''',
						    '''width''',
						    '''strength''',
						    '''guidance_scale''',
						    '''negative_prompt''',
						    '''num_inference_steps''',
						    '''return_dict''',
						    '''guidance_scale''',
						    '''num_images_per_prompt''',
						    '''output_type''',
						    '''return_dict''',
						]
						_lowerCamelCase:    Tuple      =    False


						@property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[int]      )     ->   List[Any]:
							return 32


						@property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      Dict      )     ->   List[Any]:
							return 32


						@property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      str      )     ->   int:
							return self.time_input_dim


						@property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      Union[str, Any]      )     ->   Dict:
							return self.time_input_dim * 4


						@property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      str      )     ->   str:
							return 100


						@property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      Union[str, Any]      )     ->   int:
							A						   =   XLMRobertaTokenizerFast.from_pretrained('YiYiXu/tiny-random-mclip-base'      )
							return tokenizer


						@property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      str      )     ->   Any:
							torch.manual_seed(0      )
							A						   =   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=1005  ,)

							A						   =   MultilingualCLIP(A_      )
							A						   =   text_encoder.eval()

							return text_encoder


						@property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      Tuple      )     ->   Optional[Any]:
							torch.manual_seed(0      )

							A						   =   {
							    '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,
							}

							A						   =   UNetaDConditionModel(**A_      )
							return model


						@property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      Union[str, Any]      )     ->   str:
							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 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[int]      )     ->   Optional[Any]:
							torch.manual_seed(0      )
							A						   =   VQModel(**self.dummy_movq_kwargs      )
							return model


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Tuple      )     ->   str:
							A						   =   self.dummy_text_encoder
							A						   =   self.dummy_tokenizer
							A						   =   self.dummy_unet
							A						   =   self.dummy_movq

							A						   =   {
							    'num_train_timesteps': 1000,
							    'beta_schedule': 'linear',
							    'beta_start': 0.0_00_85,
							    'beta_end': 0.0_12,
							    'clip_sample': False,
							    'set_alpha_to_one': False,
							    'steps_offset': 0,
							    'prediction_type': 'epsilon',
							    'thresholding': False,
							}

							A						   =   DDIMScheduler(**A_      )

							A						   =   {
							    'text_encoder': text_encoder,
							    'tokenizer': tokenizer,
							    'unet': unet,
							    'scheduler': scheduler,
							    'movq': movq,
							}

							return components


						def 						_SCREAMING_SNAKE_CASE      ( self     :      int  ,A_     :      Any  ,A_     :      Tuple=0      )     ->   int:
							A						   =   floats_tensor((1, self.cross_attention_dim)  ,rng=random.Random(A_      )      ).to(A_      )
							A						   =   floats_tensor((1, self.cross_attention_dim)  ,rng=random.Random(seed + 1      )      ).to(A_      )
							# create init_image
							A						   =   floats_tensor((1, 3, 64, 64)  ,rng=random.Random(A_      )      ).to(A_      )
							A						   =   image.cpu().permute(0  ,2  ,3  ,1      )[0]
							A						   =   Image.fromarray(np.uinta(A_      )      ).convert('RGB'      ).resize((256, 256)      )

							if str(A_      ).startswith('mps'      ):
								A						   =   torch.manual_seed(A_      )
							else:
								A						   =   torch.Generator(device=A_      ).manual_seed(A_      )
							A						   =   {
							    '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 						_SCREAMING_SNAKE_CASE      ( self     :      Dict      )     ->   List[Any]:
							A						   =   'cpu'

							A						   =   self.get_dummy_components()

							A						   =   self.pipeline_class(**A_      )
							A						   =   pipe.to(A_      )

							pipe.set_progress_bar_config(disable=A_      )

							A						   =   pipe(**self.get_dummy_inputs(A_      )      )
							A						   =   output.images

							A						   =   pipe(
							    **self.get_dummy_inputs(A_      )  ,return_dict=A_  ,)[0]

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

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

							A						   =   np.array(
							    [0.61_47_49_43, 0.6_07_35_39, 0.43_30_85_44, 0.5_92_82_69, 0.47_49_35_95, 0.46_75_59_73, 0.4_61_38_38, 0.45_36_87_97, 0.50_11_92_33]      )
							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 				lowerCAmelCase_				(      unittest.TestCase    ):



						'''simple docstring'''


						def 						_SCREAMING_SNAKE_CASE      ( self     :      int      )     ->   Union[str, Any]:
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()




						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[int]      )     ->   Any:
							A						   =   load_numpy(
							    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
							    '/kandinsky/kandinsky_img2img_frog.npy'      )

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

							A						   =   KandinskyPriorPipeline.from_pretrained(
							    'kandinsky-community/kandinsky-2-1-prior'  ,torch_dtype=torch.floataa      )
							pipe_prior.to(A_      )

							A						   =   KandinskyImgaImgPipeline.from_pretrained(
							    'kandinsky-community/kandinsky-2-1'  ,torch_dtype=torch.floataa      )
							A						   =   pipeline.to(A_      )

							pipeline.set_progress_bar_config(disable=A_      )

							A						   =   torch.Generator(device='cpu'      ).manual_seed(0      )
							A   ,      A						   =   pipe_prior(
							    A_  ,generator=A_  ,num_inference_steps=5  ,negative_prompt=''  ,).to_tuple()

							A						   =   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'  ,)

							A						   =   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 Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


_lowercase							   =				logging.get_logger(__name__)

_lowercase							   =				{'''vocab_file''': '''spm_char.model'''}

_lowercase							   =				{
    '''vocab_file''': {
        '''microsoft/speecht5_asr''': '''https://huggingface.co/microsoft/speecht5_asr/resolve/main/spm_char.model''',
        '''microsoft/speecht5_tts''': '''https://huggingface.co/microsoft/speecht5_tts/resolve/main/spm_char.model''',
        '''microsoft/speecht5_vc''': '''https://huggingface.co/microsoft/speecht5_vc/resolve/main/spm_char.model''',
    }
}

_lowercase							   =				{
    '''microsoft/speecht5_asr''': 10_24,
    '''microsoft/speecht5_tts''': 10_24,
    '''microsoft/speecht5_vc''': 10_24,
}







class 				lowerCAmelCase_				(      _lowercase    ):



						'''simple docstring'''


						_lowerCamelCase:    Optional[Any]      =    VOCAB_FILES_NAMES
						_lowerCamelCase:    List[Any]      =    PRETRAINED_VOCAB_FILES_MAP
						_lowerCamelCase:    str      =    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						_lowerCamelCase:    Tuple      =    ['''input_ids''', '''attention_mask''']


						def __init__( self     :      List[str]  ,A_     :      int  ,A_     :      List[str]="<s>"  ,A_     :      Optional[Any]="</s>"  ,A_     :      Optional[Any]="<unk>"  ,A_     :      str="<pad>"  ,A_     :      Optional[Dict[str, Any]] = None  ,**A_     :      List[str]  ,)     ->   None:
							A						   =   {} if sp_model_kwargs is None else sp_model_kwargs

							super().__init__(
							    bos_token=A_  ,eos_token=A_  ,unk_token=A_  ,pad_token=A_  ,sp_model_kwargs=self.sp_model_kwargs  ,**A_  ,)

							A						   =   vocab_file

							A						   =   spm.SentencePieceProcessor(**self.sp_model_kwargs      )
							self.sp_model.Load(A_      )


						@property
						def 						_SCREAMING_SNAKE_CASE      ( self     :      Any      )     ->   str:
							return self.sp_model.get_piece_size()


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[int]      )     ->   Optional[Any]:
							A						   =   {self.convert_ids_to_tokens(A_      ): i for i in range(self.vocab_size      )}
							vocab.update(self.added_tokens_encoder      )
							return vocab


						def __getstate__( self     :      str      )     ->   Any:
							A						   =   self.__dict__.copy()
							A						   =   None
							return state


						def __setstate__( self     :      Optional[int]  ,A_     :      str      )     ->   Tuple:
							A						   =   d

							# for backward compatibility
							if not hasattr(self  ,'sp_model_kwargs'      ):
								A						   =   {}

							A						   =   spm.SentencePieceProcessor(**self.sp_model_kwargs      )
							self.sp_model.Load(self.vocab_file      )


						def 						_SCREAMING_SNAKE_CASE      ( self     :      str  ,A_     :      str      )     ->   List[str]:
							return self.sp_model.encode(A_  ,out_type=A_      )


						def 						_SCREAMING_SNAKE_CASE      ( self     :      List[str]  ,A_     :      Union[str, Any]      )     ->   Union[str, Any]:
							return self.sp_model.piece_to_id(A_      )


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[Any]  ,A_     :      Dict      )     ->   List[Any]:
							A						   =   self.sp_model.IdToPiece(A_      )
							return token


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Tuple  ,A_     :      Optional[Any]      )     ->   List[str]:
							A						   =   []
							A						   =   ''
							for token in tokens:
								# make sure that special tokens are not decoded using sentencepiece model
								if token in self.all_special_tokens:
									out_string += self.sp_model.decode(A_      ) + token
									A						   =   []
								else:
									current_sub_tokens.append(A_      )
							out_string += self.sp_model.decode(A_      )
							return out_string.strip()


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Dict  ,A_     :      Dict  ,A_     :      Optional[int]=None      )     ->   List[int]:
							if token_ids_a is None:
								return token_ids_a + [self.eos_token_id]
							# We don't expect to process pairs, but leave the pair logic for API consistency
							return token_ids_a + token_ids_a + [self.eos_token_id]


						def 						_SCREAMING_SNAKE_CASE      ( self     :      Optional[Any]  ,A_     :      List[int]  ,A_     :      Optional[List[int]] = None  ,A_     :      bool = False      )     ->   List[int]:
							if already_has_special_tokens:
								return super().get_special_tokens_mask(
								    token_ids_a=A_  ,token_ids_a=A_  ,already_has_special_tokens=A_      )

							A						   =   [1]
							if token_ids_a is None:
								return ([0] * len(A_      )) + suffix_ones
							return ([0] * len(A_      )) + ([0] * len(A_      )) + suffix_ones




						def 						_SCREAMING_SNAKE_CASE      ( self     :      str  ,A_     :      str  ,A_     :      Optional[str] = None      )     ->   Tuple[str]:
							if not os.path.isdir(A_      ):
								logger.error(F'Vocabulary path ({save_directory}) should be a directory'      )
								return
							A						   =   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_      ) and os.path.isfile(self.vocab_file      ):
								copyfile(self.vocab_file  ,A_      )
							elif not os.path.isfile(self.vocab_file      ):
								with open(A_  ,'wb'      ) as fi:
									A						   =   self.sp_model.serialized_model_proto()
									fi.write(A_      )

							return (out_vocab_file,)