



"""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 SCREAMING_SNAKE_CASE__					(		__snake_case     ,					unittest.TestCase      ):
						_lowerCAmelCase							=					KandinskyImgaImgPipeline
						_lowerCAmelCase							=					["prompt", "image_embeds", "negative_image_embeds", "image"]
						_lowerCAmelCase							=					[
						    "prompt",
						    "negative_prompt",
						    "image_embeds",
						    "negative_image_embeds",
						    "image",
						]
						_lowerCAmelCase							=					[
						    "generator",
						    "height",
						    "width",
						    "strength",
						    "guidance_scale",
						    "negative_prompt",
						    "num_inference_steps",
						    "return_dict",
						    "guidance_scale",
						    "num_images_per_prompt",
						    "output_type",
						    "return_dict",
						]
						_lowerCAmelCase							=					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'''

													__a		:		List[Any]									=       XLMRobertaTokenizerFast.from_pretrained("""YiYiXu/tiny-random-mclip-base"""							)
													return tokenizer



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													torch.manual_seed(0							)
													__a		:		str									=       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		:		int									=       MultilingualCLIP(_lowercase							)
													__a		:		Union[str, Any]									=       text_encoder.eval()

													return text_encoder



						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													torch.manual_seed(0							)

													__a		:		str									=       {
													    """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		:		Dict									=       UNetaDConditionModel(**_lowercase							)
													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							)
													__a		:		Union[str, Any]									=       VQModel(**self.dummy_movq_kwargs							)
													return model



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Tuple									=       self.dummy_text_encoder
													__a		:		str									=       self.dummy_tokenizer
													__a		:		Union[str, Any]									=       self.dummy_unet
													__a		:		Union[str, Any]									=       self.dummy_movq

													__a		:		int									=       {
													    """num_train_timesteps""": 1000,
													    """beta_schedule""": """linear""",
													    """beta_start""": 0.0_0085,
													    """beta_end""": 0.012,
													    """clip_sample""": False,
													    """set_alpha_to_one""": False,
													    """steps_offset""": 0,
													    """prediction_type""": """epsilon""",
													    """thresholding""": False,
													}

													__a		:		Tuple									=       DDIMScheduler(**_lowercase							)

													__a		:		Optional[int]									=       {
													    """text_encoder""": text_encoder,
													    """tokenizer""": tokenizer,
													    """unet""": unet,
													    """scheduler""": scheduler,
													    """movq""": movq,
													}

													return components



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase=0							):





													'''simple docstring'''

													__a		:		Tuple									=       floats_tensor((1, self.cross_attention_dim)						,     rng=random.Random(_lowercase							)							).to(_lowercase							)
													__a		:		int									=       floats_tensor((1, self.cross_attention_dim)						,     rng=random.Random(seed + 1							)							).to(_lowercase							)
													# create init_image
													__a		:		Dict									=       floats_tensor((1, 3, 64, 64)						,     rng=random.Random(_lowercase							)							).to(_lowercase							)
													__a		:		Union[str, Any]									=       image.cpu().permute(0						,     2						,     3						,     1							)[0]
													__a		:		Tuple									=       Image.fromarray(np.uinta(_lowercase							)							).convert("""RGB"""							).resize((256, 256)							)

													if str(_lowercase							).startswith("""mps"""							):
																				__a		:		Union[str, Any]									=       torch.manual_seed(_lowercase							)
													else:
																				__a		:		str									=       torch.Generator(device=_lowercase							).manual_seed(_lowercase							)
													__a		:		str									=       {
													    """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'''

													__a		:		Dict									=       """cpu"""

													__a		:		Any									=       self.get_dummy_components()

													__a		:		Optional[Any]									=       self.pipeline_class(**_lowercase							)
													__a		:		Any									=       pipe.to(_lowercase							)

													pipe.set_progress_bar_config(disable=_lowercase							)

													__a		:		Tuple									=       pipe(**self.get_dummy_inputs(_lowercase							)							)
													__a		:		Optional[Any]									=       output.images

													__a		:		Union[str, Any]									=       pipe(
													    **self.get_dummy_inputs(_lowercase							)						,     return_dict=_lowercase						,     )[0]

													__a		:		List[str]									=       image[0, -3:, -3:, -1]
													__a		:		Tuple									=       image_from_tuple[0, -3:, -3:, -1]

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

													__a		:		List[str]									=       np.array(
													    [0.6147_4943, 0.607_3539, 0.4330_8544, 0.592_8269, 0.4749_3595, 0.4675_5973, 0.461_3838, 0.4536_8797, 0.5011_9233]							)
													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 SCREAMING_SNAKE_CASE__					(		unittest.TestCase      ):



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

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





						def     lowerCAmelCase__(self							):





													'''simple docstring'''

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

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

													__a		:		Optional[Any]									=       KandinskyPriorPipeline.from_pretrained(
													    """kandinsky-community/kandinsky-2-1-prior"""						,     torch_dtype=torch.floataa							)
													pipe_prior.to(_lowercase							)

													__a		:		Any									=       KandinskyImgaImgPipeline.from_pretrained(
													    """kandinsky-community/kandinsky-2-1"""						,     torch_dtype=torch.floataa							)
													__a		:		str									=       pipeline.to(_lowercase							)

													pipeline.set_progress_bar_config(disable=_lowercase							)

													__a		:		Optional[Any]									=       torch.Generator(device="""cpu"""							).manual_seed(0							)
													__a							,     __a		:		str									=       pipe_prior(
													    _lowercase						,     generator=_lowercase						,     num_inference_steps=5						,     negative_prompt=""""""						,     ).to_tuple()

													__a		:		Union[str, Any]									=       pipeline(
													    _lowercase						,     image=_lowercase						,     image_embeds=_lowercase						,     negative_image_embeds=_lowercase						,     generator=_lowercase						,     num_inference_steps=100						,     height=768						,     width=768						,     strength=0.2						,     output_type="""np"""						,     )

													__a		:		List[Any]									=       output.images[0]

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

													assert_mean_pixel_difference(_lowercase						,     _lowercase							)






"""simple docstring"""

import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase__			   =  logging.get_logger(__name__)

lowercase__			   =  {
    "microsoft/unispeech-large-1500h-cv": (
        "https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json"
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}




class SCREAMING_SNAKE_CASE__					(		__snake_case      ):
						_lowerCAmelCase							=					"unispeech"



						def __init__(self						,     _lowercase=32						,     _lowercase=768						,     _lowercase=12						,     _lowercase=12						,     _lowercase=3072						,     _lowercase="gelu"						,     _lowercase=0.1						,     _lowercase=0.1						,     _lowercase=0.1						,     _lowercase=0.0						,     _lowercase=0.0						,     _lowercase=0.1						,     _lowercase=0.1						,     _lowercase=0.02						,     _lowercase=1e-5						,     _lowercase="group"						,     _lowercase="gelu"						,     _lowercase=(512, 512, 512, 512, 512, 512, 512)						,     _lowercase=(5, 2, 2, 2, 2, 2, 2)						,     _lowercase=(10, 3, 3, 3, 3, 2, 2)						,     _lowercase=False						,     _lowercase=128						,     _lowercase=16						,     _lowercase=False						,     _lowercase=True						,     _lowercase=0.05						,     _lowercase=10						,     _lowercase=2						,     _lowercase=0.0						,     _lowercase=10						,     _lowercase=0						,     _lowercase=320						,     _lowercase=2						,     _lowercase=0.1						,     _lowercase=100						,     _lowercase=256						,     _lowercase=256						,     _lowercase=0.1						,     _lowercase="mean"						,     _lowercase=False						,     _lowercase=False						,     _lowercase=256						,     _lowercase=80						,     _lowercase=0						,     _lowercase=1						,     _lowercase=2						,     _lowercase=0.5						,     **_lowercase						,     ):





													'''simple docstring'''

													super().__init__(**_lowercase						,     pad_token_id=_lowercase						,     bos_token_id=_lowercase						,     eos_token_id=_lowercase							)
													__a		:		Union[str, Any]									=       hidden_size
													__a		:		Any									=       feat_extract_norm
													__a		:		Union[str, Any]									=       feat_extract_activation
													__a		:		Tuple									=       list(_lowercase							)
													__a		:		Dict									=       list(_lowercase							)
													__a		:		List[Any]									=       list(_lowercase							)
													__a		:		List[Any]									=       conv_bias
													__a		:		Optional[Any]									=       num_conv_pos_embeddings
													__a		:		Union[str, Any]									=       num_conv_pos_embedding_groups
													__a		:		Dict									=       len(self.conv_dim							)
													__a		:		Dict									=       num_hidden_layers
													__a		:		Union[str, Any]									=       intermediate_size
													__a		:		List[str]									=       hidden_act
													__a		:		int									=       num_attention_heads
													__a		:		int									=       hidden_dropout
													__a		:		Any									=       attention_dropout
													__a		:		List[Any]									=       activation_dropout
													__a		:		List[Any]									=       feat_proj_dropout
													__a		:		Union[str, Any]									=       final_dropout
													__a		:		str									=       layerdrop
													__a		:		Dict									=       layer_norm_eps
													__a		:		Dict									=       initializer_range
													__a		:		Union[str, Any]									=       num_ctc_classes
													__a		:		List[Any]									=       vocab_size
													__a		:		Any									=       do_stable_layer_norm
													__a		:		List[str]									=       use_weighted_layer_sum
													__a		:		List[str]									=       classifier_proj_size

													if (
													    (len(self.conv_stride							) != self.num_feat_extract_layers)
													    or (len(self.conv_kernel							) != self.num_feat_extract_layers)
													    or (len(self.conv_dim							) != self.num_feat_extract_layers)
													):
																				raise ValueError(
																				    """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
																				    """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
																				    F''' {len(self.conv_dim							)}`, `len(config.conv_stride) = {len(self.conv_stride							)}`,'''
																				    F''' `len(config.conv_kernel) = {len(self.conv_kernel							)}`.'''							)

													# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
													__a		:		Dict									=       apply_spec_augment
													__a		:		Union[str, Any]									=       mask_time_prob
													__a		:		List[str]									=       mask_time_length
													__a		:		Dict									=       mask_time_min_masks
													__a		:		List[Any]									=       mask_feature_prob
													__a		:		Tuple									=       mask_feature_length
													__a		:		int									=       mask_feature_min_masks

													# parameters for pretraining with codevector quantized representations
													__a		:		List[Any]									=       num_codevectors_per_group
													__a		:		Union[str, Any]									=       num_codevector_groups
													__a		:		List[Any]									=       contrastive_logits_temperature
													__a		:		Any									=       feat_quantizer_dropout
													__a		:		Optional[int]									=       num_negatives
													__a		:		List[str]									=       codevector_dim
													__a		:		List[Any]									=       proj_codevector_dim
													__a		:		Tuple									=       diversity_loss_weight

													# ctc loss
													__a		:		Any									=       ctc_loss_reduction
													__a		:		List[str]									=       ctc_zero_infinity

													# pretraining loss
													__a		:		Tuple									=       replace_prob





						@property
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													return functools.reduce(operator.mul						,     self.conv_stride						,     1							)

