





"""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, KandinskyInpaintPipeline, 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 			__UpperCamelCase							(		a__						,			unittest.TestCase		):
							lowerCamelCase  :   List[Any]    =KandinskyInpaintPipeline
							lowerCamelCase  :   int    =["""prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image"""]
							lowerCamelCase  :   List[Any]    =[
							    """prompt""",
							    """negative_prompt""",
							    """image_embeds""",
							    """negative_image_embeds""",
							    """image""",
							    """mask_image""",
							]
							lowerCamelCase  :   Union[str, Any]    =[
							    """generator""",
							    """height""",
							    """width""",
							    """latents""",
							    """guidance_scale""",
							    """negative_prompt""",
							    """num_inference_steps""",
							    """return_dict""",
							    """guidance_scale""",
							    """num_images_per_prompt""",
							    """output_type""",
							    """return_dict""",
							]
							lowerCamelCase  :   Dict    =False






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






							@property
							def        __a  (  self )      ->					int:
								return 32






							@property
							def        __a  (  self )      ->					Union[str, Any]:
								return self.time_input_dim






							@property
							def        __a  (  self )      ->					Dict:
								return self.time_input_dim * 4






							@property
							def        __a  (  self )      ->					Any:
								return 100






							@property
							def        __a  (  self )      ->					Any:
								a :						List[str]      =							XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base" )
								return tokenizer






							@property
							def        __a  (  self )      ->					Any:
								torch.manual_seed(0 )
								a :						Dict      =							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 :						List[str]      =							MultilingualCLIP(lowerCAmelCase__ )
								a :						Optional[int]      =							text_encoder.eval()

								return text_encoder






							@property
							def        __a  (  self )      ->					Dict:
								torch.manual_seed(0 )

								a :						str      =							{
								    "in_channels": 9,
								    # 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 :						Optional[Any]      =							UNetaDConditionModel(**lowerCAmelCase__ )
								return model






							@property
							def        __a  (  self )      ->					List[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        __a  (  self )      ->					Optional[int]:
								torch.manual_seed(0 )
								a :						List[Any]      =							VQModel(**self.dummy_movq_kwargs )
								return model






							def        __a  (  self )      ->					Optional[Any]:
								a :						List[Any]      =							self.dummy_text_encoder
								a :						Any      =							self.dummy_tokenizer
								a :						Union[str, Any]      =							self.dummy_unet
								a :						Dict      =							self.dummy_movq

								a :						List[Any]      =							DDIMScheduler(
								    num_train_timesteps=1000 ,   beta_schedule="linear" ,   beta_start=0.00_085 ,   beta_end=0.012 ,   clip_sample=lowerCAmelCase__ ,   set_alpha_to_one=lowerCAmelCase__ ,   steps_offset=1 ,   prediction_type="epsilon" ,   thresholding=lowerCAmelCase__ ,   )

								a :						Optional[Any]      =							{
								    "text_encoder": text_encoder,
								    "tokenizer": tokenizer,
								    "unet": unet,
								    "scheduler": scheduler,
								    "movq": movq,
								}

								return components






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__=0 )      ->					Any:
								a :						Any      =							floats_tensor((1, self.cross_attention_dim) ,   rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ )
								a :						Dict      =							floats_tensor((1, self.cross_attention_dim) ,   rng=random.Random(seed + 1 ) ).to(lowerCAmelCase__ )
								# create init_image
								a :						List[str]      =							floats_tensor((1, 3, 64, 64) ,   rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ )
								a :						Optional[int]      =							image.cpu().permute(0 ,   2 ,   3 ,   1 )[0]
								a :						str      =							Image.fromarray(np.uinta(lowerCAmelCase__ ) ).convert("RGB" ).resize((256, 256) )
								# create mask
								a :						int      =							np.ones((64, 64) ,   dtype=np.floataa )
								a :						Optional[int]      =							0

								if str(lowerCAmelCase__ ).startswith("mps" ):
									a :						Union[str, Any]      =							torch.manual_seed(lowerCAmelCase__ )
								else:
									a :						int      =							torch.Generator(device=lowerCAmelCase__ ).manual_seed(lowerCAmelCase__ )
								a :						Tuple      =							{
								    "prompt": "horse",
								    "image": init_image,
								    "mask_image": mask,
								    "image_embeds": image_embeds,
								    "negative_image_embeds": negative_image_embeds,
								    "generator": generator,
								    "height": 64,
								    "width": 64,
								    "num_inference_steps": 2,
								    "guidance_scale": 4.0,
								    "output_type": "np",
								}
								return inputs






							def        __a  (  self )      ->					str:
								a :						Any      =							"cpu"

								a :						Any      =							self.get_dummy_components()

								a :						Optional[int]      =							self.pipeline_class(**lowerCAmelCase__ )
								a :						Optional[int]      =							pipe.to(lowerCAmelCase__ )

								pipe.set_progress_bar_config(disable=lowerCAmelCase__ )

								a :						Optional[int]      =							pipe(**self.get_dummy_inputs(lowerCAmelCase__ ) )
								a :						List[str]      =							output.images

								a :						Dict      =							pipe(
								    **self.get_dummy_inputs(lowerCAmelCase__ ) ,   return_dict=lowerCAmelCase__ ,   )[0]

								a :						str      =							image[0, -3:, -3:, -1]
								a :						Any      =							image_from_tuple[0, -3:, -3:, -1]

								print(f"""image.shape {image.shape}""" )

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

								a :						Tuple      =							np.array(
								    [0.8_326_919, 0.73_790_467, 0.20_918_581, 0.9_309_612, 0.5_511_791, 0.43_713_328, 0.5_513_321, 0.49_922_934, 0.59_497_786] )

								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()}"""






							def        __a  (  self )      ->					Optional[Any]:
								super().test_inference_batch_single_identical(expected_max_diff=3E-3 )


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






							def        __a  (  self )      ->					Any:
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()






							def        __a  (  self )      ->					List[Any]:
								a :						Optional[int]      =							load_numpy(
								    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
								    "/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy" )

								a :						Union[str, Any]      =							load_image(
								    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" )
								a :						Union[str, Any]      =							np.ones((768, 768) ,   dtype=np.floataa )
								a :						Optional[int]      =							0

								a :						Any      =							"a hat"

								a :						Tuple      =							KandinskyPriorPipeline.from_pretrained(
								    "kandinsky-community/kandinsky-2-1-prior" ,   torch_dtype=torch.floataa )
								pipe_prior.to(lowerCAmelCase__ )

								a :						List[str]      =							KandinskyInpaintPipeline.from_pretrained(
								    "kandinsky-community/kandinsky-2-1-inpaint" ,   torch_dtype=torch.floataa )
								a :						Dict      =							pipeline.to(lowerCAmelCase__ )
								pipeline.set_progress_bar_config(disable=lowerCAmelCase__ )

								a :						Any      =							torch.Generator(device="cpu" ).manual_seed(0 )
								a,			a :						int      =							pipe_prior(
								    lowerCAmelCase__ ,   generator=lowerCAmelCase__ ,   num_inference_steps=5 ,   negative_prompt="" ,   ).to_tuple()

								a :						Tuple      =							pipeline(
								    lowerCAmelCase__ ,   image=lowerCAmelCase__ ,   mask_image=lowerCAmelCase__ ,   image_embeds=lowerCAmelCase__ ,   negative_image_embeds=lowerCAmelCase__ ,   generator=lowerCAmelCase__ ,   num_inference_steps=100 ,   height=768 ,   width=768 ,   output_type="np" ,   )

								a :						Any      =							output.images[0]

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

								assert_mean_pixel_difference(lowerCAmelCase__ ,   lowerCAmelCase__ )









"""simple docstring"""




import functools
import operator

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


a		:					List[Any]	      =     logging.get_logger(__name__)

a		:					List[Any]	      =     {
    '''microsoft/unispeech-sat-base-100h-libri-ft''': (
        '''https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'''
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}


class 			__UpperCamelCase							(		a__		):
							lowerCamelCase  :   Tuple    ="""unispeech-sat"""






							def __init__(  self ,   lowerCAmelCase__=32 ,   lowerCAmelCase__=768 ,   lowerCAmelCase__=12 ,   lowerCAmelCase__=12 ,   lowerCAmelCase__=3072 ,   lowerCAmelCase__="gelu" ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.0 ,   lowerCAmelCase__=0.0 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.02 ,   lowerCAmelCase__=1E-5 ,   lowerCAmelCase__="group" ,   lowerCAmelCase__="gelu" ,   lowerCAmelCase__=(512, 512, 512, 512, 512, 512, 512) ,   lowerCAmelCase__=(5, 2, 2, 2, 2, 2, 2) ,   lowerCAmelCase__=(10, 3, 3, 3, 3, 2, 2) ,   lowerCAmelCase__=False ,   lowerCAmelCase__=128 ,   lowerCAmelCase__=16 ,   lowerCAmelCase__=False ,   lowerCAmelCase__=True ,   lowerCAmelCase__=0.05 ,   lowerCAmelCase__=10 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=0.0 ,   lowerCAmelCase__=10 ,   lowerCAmelCase__=0 ,   lowerCAmelCase__=320 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=100 ,   lowerCAmelCase__=256 ,   lowerCAmelCase__=256 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__="mean" ,   lowerCAmelCase__=False ,   lowerCAmelCase__=False ,   lowerCAmelCase__=256 ,   lowerCAmelCase__=(512, 512, 512, 512, 1500) ,   lowerCAmelCase__=(5, 3, 3, 1, 1) ,   lowerCAmelCase__=(1, 2, 3, 1, 1) ,   lowerCAmelCase__=512 ,   lowerCAmelCase__=0 ,   lowerCAmelCase__=1 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=504 ,   **lowerCAmelCase__ ,   )      ->					List[str]:
								super().__init__(**lowerCAmelCase__ ,   pad_token_id=lowerCAmelCase__ ,   bos_token_id=lowerCAmelCase__ ,   eos_token_id=lowerCAmelCase__ )
								a :						Any      =							hidden_size
								a :						Tuple      =							feat_extract_norm
								a :						Tuple      =							feat_extract_activation
								a :						Dict      =							list(lowerCAmelCase__ )
								a :						int      =							list(lowerCAmelCase__ )
								a :						Optional[Any]      =							list(lowerCAmelCase__ )
								a :						int      =							conv_bias
								a :						str      =							num_conv_pos_embeddings
								a :						Dict      =							num_conv_pos_embedding_groups
								a :						Optional[int]      =							len(self.conv_dim )
								a :						int      =							num_hidden_layers
								a :						Any      =							intermediate_size
								a :						Any      =							hidden_act
								a :						List[Any]      =							num_attention_heads
								a :						Any      =							hidden_dropout
								a :						Union[str, Any]      =							attention_dropout
								a :						Tuple      =							activation_dropout
								a :						Dict      =							feat_proj_dropout
								a :						Optional[Any]      =							final_dropout
								a :						Union[str, Any]      =							layerdrop
								a :						str      =							layer_norm_eps
								a :						Optional[int]      =							initializer_range
								a :						Optional[int]      =							vocab_size
								a :						str      =							num_clusters
								a :						Any      =							do_stable_layer_norm
								a :						str      =							use_weighted_layer_sum

								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 :						List[Any]      =							apply_spec_augment
								a :						int      =							mask_time_prob
								a :						Optional[int]      =							mask_time_length
								a :						Dict      =							mask_time_min_masks
								a :						Optional[int]      =							mask_feature_prob
								a :						List[str]      =							mask_feature_length
								a :						Dict      =							mask_feature_min_masks

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

								# ctc loss
								a :						Dict      =							ctc_loss_reduction
								a :						Optional[Any]      =							ctc_zero_infinity

								# SequenceClassification-specific parameter. Feel free to ignore for other classes.
								a :						Dict      =							classifier_proj_size

								# XVector-specific parameters. Feel free to ignore for other classes.
								a :						Union[str, Any]      =							list(lowerCAmelCase__ )
								a :						List[str]      =							list(lowerCAmelCase__ )
								a :						Tuple      =							list(lowerCAmelCase__ )
								a :						Optional[int]      =							xvector_output_dim






							@property
							def        __a  (  self )      ->					Any:
								return functools.reduce(operator.mul ,   self.conv_stride ,   1 )


