





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


	'''simple docstring'''





	_A													=				KandinskyInpaintPipeline
	_A													=				['''prompt''', '''image_embeds''', '''negative_image_embeds''', '''image''', '''mask_image''']
	_A													=				[
	    '''prompt''',
	    '''negative_prompt''',
	    '''image_embeds''',
	    '''negative_image_embeds''',
	    '''image''',
	    '''mask_image''',
	]
	_A													=				[
	    '''generator''',
	    '''height''',
	    '''width''',
	    '''latents''',
	    '''guidance_scale''',
	    '''negative_prompt''',
	    '''num_inference_steps''',
	    '''return_dict''',
	    '''guidance_scale''',
	    '''num_images_per_prompt''',
	    '''output_type''',
	    '''return_dict''',
	]
	_A													=				False

	@property
	def    snake_case__(						self:						Optional[Any]			):
			return 32

	@property
	def    snake_case__(						self:						Dict			):
			return 32

	@property
	def    snake_case__(						self:						Tuple			):
			return self.time_input_dim

	@property
	def    snake_case__(						self:						Union[str, Any]			):
			return self.time_input_dim * 4

	@property
	def    snake_case__(						self:						int			):
			return 100

	@property
	def    snake_case__(						self:						Optional[Any]			):
			lowercase__					:      Optional[int]      =		XLMRobertaTokenizerFast.from_pretrained('YiYiXu/tiny-random-mclip-base'			)
			return tokenizer

	@property
	def    snake_case__(						self:						Optional[Any]			):
			torch.manual_seed(0			)
			lowercase__					:      List[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,					)

			lowercase__					:      Optional[Any]      =		MultilingualCLIP(lowerCamelCase_			)
			lowercase__					:      List[str]      =		text_encoder.eval()

			return text_encoder

	@property
	def    snake_case__(						self:						int			):
			torch.manual_seed(0			)

			lowercase__					:      Any      =		{
			    '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,
			}

			lowercase__					:      str      =		UNetaDConditionModel(**lowerCamelCase_			)
			return model

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

	def    snake_case__(						self:						Dict			):
			lowercase__					:      List[str]      =		self.dummy_text_encoder
			lowercase__					:      Optional[int]      =		self.dummy_tokenizer
			lowercase__					:      Optional[int]      =		self.dummy_unet
			lowercase__					:      Optional[int]      =		self.dummy_movq

			lowercase__					:      str      =		DDIMScheduler(
			    num_train_timesteps=1000,					beta_schedule='linear',					beta_start=0.0_0_0_8_5,					beta_end=0.0_1_2,					clip_sample=lowerCamelCase_,					set_alpha_to_one=lowerCamelCase_,					steps_offset=1,					prediction_type='epsilon',					thresholding=lowerCamelCase_,					)

			lowercase__					:      List[str]      =		{
			    'text_encoder': text_encoder,
			    'tokenizer': tokenizer,
			    'unet': unet,
			    'scheduler': scheduler,
			    'movq': movq,
			}

			return components

	def    snake_case__(						self:						Union[str, Any],					lowerCamelCase_:						Dict,					lowerCamelCase_:						str=0			):
			lowercase__					:      Any      =		floats_tensor((1, self.cross_attention_dim),					rng=random.Random(lowerCamelCase_			)			).to(lowerCamelCase_			)
			lowercase__					:      Tuple      =		floats_tensor((1, self.cross_attention_dim),					rng=random.Random(seed + 1			)			).to(lowerCamelCase_			)
			# create init_image
			lowercase__					:      List[Any]      =		floats_tensor((1, 3, 64, 64),					rng=random.Random(lowerCamelCase_			)			).to(lowerCamelCase_			)
			lowercase__					:      Tuple      =		image.cpu().permute(0,					2,					3,					1			)[0]
			lowercase__					:      List[str]      =		Image.fromarray(np.uinta(lowerCamelCase_			)			).convert('RGB'			).resize((256, 256)			)
			# create mask
			lowercase__					:      Dict      =		np.ones((64, 64),					dtype=np.floataa			)
			lowercase__					:      Tuple      =		0

			if str(lowerCamelCase_			).startswith('mps'			):
					lowercase__					:      Union[str, Any]      =		torch.manual_seed(lowerCamelCase_			)
			else:
					lowercase__					:      List[Any]      =		torch.Generator(device=lowerCamelCase_			).manual_seed(lowerCamelCase_			)
			lowercase__					:      List[str]      =		{
			    '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    snake_case__(						self:						Dict			):
			lowercase__					:      Dict      =		'cpu'

			lowercase__					:      List[Any]      =		self.get_dummy_components()

			lowercase__					:      Tuple      =		self.pipeline_class(**lowerCamelCase_			)
			lowercase__					:      int      =		pipe.to(lowerCamelCase_			)

			pipe.set_progress_bar_config(disable=lowerCamelCase_			)

			lowercase__					:      Tuple      =		pipe(**self.get_dummy_inputs(lowerCamelCase_			)			)
			lowercase__					:      List[Any]      =		output.images

			lowercase__					:      Optional[int]      =		pipe(
			    **self.get_dummy_inputs(lowerCamelCase_			),					return_dict=lowerCamelCase_,					)[0]

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

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

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

			lowercase__					:      Union[str, Any]      =		np.array(
			    [0.8_3_2_6_9_1_9, 0.7_3_7_9_0_4_6_7, 0.2_0_9_1_8_5_8_1, 0.9_3_0_9_6_1_2, 0.5_5_1_1_7_9_1, 0.4_3_7_1_3_3_2_8, 0.5_5_1_3_3_2_1, 0.4_9_9_2_2_9_3_4, 0.5_9_4_9_7_7_8_6]			)

			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    snake_case__(						self:						Tuple			):
			super().test_inference_batch_single_identical(expected_max_diff=3E-3			)


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


	'''simple docstring'''





	def    snake_case__(						self:						int			):
			# clean up the VRAM after each test
			super().tearDown()
			gc.collect()
			torch.cuda.empty_cache()





	def    snake_case__(						self:						Optional[int]			):
			lowercase__					:      Union[str, Any]      =		load_numpy(
			    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
			    '/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy'			)

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

			lowercase__					:      Optional[int]      =		'a hat'

			lowercase__					:      Optional[int]      =		KandinskyPriorPipeline.from_pretrained(
			    'kandinsky-community/kandinsky-2-1-prior',					torch_dtype=torch.floataa			)
			pipe_prior.to(lowerCamelCase_			)

			lowercase__					:      Union[str, Any]      =		KandinskyInpaintPipeline.from_pretrained(
			    'kandinsky-community/kandinsky-2-1-inpaint',					torch_dtype=torch.floataa			)
			lowercase__					:      Optional[int]      =		pipeline.to(lowerCamelCase_			)
			pipeline.set_progress_bar_config(disable=lowerCamelCase_			)

			lowercase__					:      Union[str, Any]      =		torch.Generator(device='cpu'			).manual_seed(0			)
			lowercase__   ,				lowercase__					:      Optional[Any]      =		pipe_prior(
			    lowerCamelCase_,					generator=lowerCamelCase_,					num_inference_steps=5,					negative_prompt='',					).to_tuple()

			lowercase__					:      Optional[int]      =		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',					)

			lowercase__					:      Union[str, Any]      =		output.images[0]

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

			assert_mean_pixel_difference(lowerCamelCase_,					lowerCamelCase_			)







import torch
from torch import nn
class    __UpperCamelCase			(       nn.Module				):

 """simple docstring"""



 def __init__(	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=1		,				SCREAMING_SNAKE_CASE=False      ) ->			Tuple:
  super().__init__()

  a__												= n_token
  a__												= d_embed
  a__												= d_proj

  a__												= cutoffs + [n_token]
  a__												= [0] + self.cutoffs
  a__												= div_val

  a__												= self.cutoffs[0]
  a__												= len(self.cutoffs      ) - 1
  a__												= self.shortlist_size + self.n_clusters

  if self.n_clusters > 0:
   a__												= nn.Parameter(torch.zeros(self.n_clusters		,				self.d_embed      )      )
   a__												= nn.Parameter(torch.zeros(self.n_clusters      )      )

  a__												= nn.ModuleList()
  a__												= nn.ParameterList()

  if div_val == 1:
   for i in range(len(self.cutoffs      )      ):
    if d_proj != d_embed:
     self.out_projs.append(nn.Parameter(torch.FloatTensor(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )      )      )
    else:
     self.out_projs.append(SCREAMING_SNAKE_CASE      )

   self.out_layers.append(nn.Linear(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )      )
  else:
   for i in range(len(self.cutoffs      )      ):
    a__							,			a__												= self.cutoff_ends[i], self.cutoff_ends[i + 1]
    a__												= d_embed // (div_val**i)

    self.out_projs.append(nn.Parameter(torch.FloatTensor(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )      )      )

    self.out_layers.append(nn.Linear(SCREAMING_SNAKE_CASE		,				r_idx - l_idx      )      )

  a__												= keep_order

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ) ->			str:
  if proj is None:
   a__												= nn.functional.linear(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				bias=SCREAMING_SNAKE_CASE      )
  else:
   # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1:
   a__												= nn.functional.linear(SCREAMING_SNAKE_CASE		,				proj.t().contiguous()      )
   a__												= nn.functional.linear(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				bias=SCREAMING_SNAKE_CASE      )
   # else:
   #     logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t()))
   #     if bias is not None:
   #         logit = logit + bias

  return logit

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=False      ) ->			Optional[int]:

  if labels is not None:
   # Shift so that tokens < n predict n
   a__												= hidden[..., :-1, :].contiguous()
   a__												= labels[..., 1:].contiguous()
   a__												= hidden.view(-1		,				hidden.size(-1      )      )
   a__												= labels.view(-1      )
   if hidden.size(0      ) != labels.size(0      ):
    raise RuntimeError('''Input and labels should have the same size in the batch dimension.'''      )
  else:
   a__												= hidden.view(-1		,				hidden.size(-1      )      )

  if self.n_clusters == 0:
   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				self.out_layers[0].weight		,				self.out_layers[0].bias		,				self.out_projs[0]      )
   if labels is not None:
    a__												= labels != -1_0_0
    a__												= torch.zeros_like(SCREAMING_SNAKE_CASE		,				dtype=hidden.dtype		,				device=hidden.device      )
    a__												= (
        -nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=-1      )[mask].gather(1		,				labels[mask].unsqueeze(1      )      ).squeeze(1      )
    )
   else:
    a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=-1      )
  else:
   # construct weights and biases
   a__							,			a__												= [], []
   for i in range(len(self.cutoffs      )      ):
    if self.div_val == 1:
     a__							,			a__												= self.cutoff_ends[i], self.cutoff_ends[i + 1]
     a__												= self.out_layers[0].weight[l_idx:r_idx]
     a__												= self.out_layers[0].bias[l_idx:r_idx]
    else:
     a__												= self.out_layers[i].weight
     a__												= self.out_layers[i].bias

    if i == 0:
     a__												= torch.cat([weight_i, self.cluster_weight]		,				dim=0      )
     a__												= torch.cat([bias_i, self.cluster_bias]		,				dim=0      )

    weights.append(SCREAMING_SNAKE_CASE      )
    biases.append(SCREAMING_SNAKE_CASE      )

   a__							,			a__							,			a__												= weights[0], biases[0], self.out_projs[0]

   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
   a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )

   if labels is None:
    a__												= hidden.new_empty((head_logit.size(0      ), self.n_token)      )
   else:
    a__												= torch.zeros_like(SCREAMING_SNAKE_CASE		,				dtype=hidden.dtype		,				device=hidden.device      )

   a__												= 0
   a__												= [0] + self.cutoffs
   for i in range(len(SCREAMING_SNAKE_CASE      ) - 1      ):
    a__							,			a__												= cutoff_values[i], cutoff_values[i + 1]

    if labels is not None:
     a__												= (labels >= l_idx) & (labels < r_idx)
     a__												= mask_i.nonzero().squeeze()

     if indices_i.numel() == 0:
      continue

     a__												= labels.index_select(0		,				SCREAMING_SNAKE_CASE      ) - l_idx
     a__												= head_logprob.index_select(0		,				SCREAMING_SNAKE_CASE      )
     a__												= hidden.index_select(0		,				SCREAMING_SNAKE_CASE      )
    else:
     a__												= hidden

    if i == 0:
     if labels is not None:
      a__												= head_logprob_i.gather(1		,				target_i[:, None]      ).squeeze(1      )
     else:
      a__												= head_logprob[:, : self.cutoffs[0]]
    else:
     a__							,			a__							,			a__												= weights[i], biases[i], self.out_projs[i]

     a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
     a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )
     a__												= self.cutoffs[0] + i - 1  # No probability for the head cluster
     if labels is not None:
      a__												= head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather(
          1		,				target_i[:, None]      ).squeeze(1      )
     else:
      a__												= head_logprob[:, cluster_prob_idx, None] + tail_logprob_i
      a__												= logprob_i

    if labels is not None:
     if (hasattr(self		,				'''keep_order'''      ) and self.keep_order) or keep_order:
      out.index_copy_(0		,				SCREAMING_SNAKE_CASE		,				-logprob_i      )
     else:
      out[offset : offset + logprob_i.size(0      )].copy_(-logprob_i      )
     offset += logprob_i.size(0      )

  return out



 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE      ) ->			List[str]:
  if self.n_clusters == 0:
   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				self.out_layers[0].weight		,				self.out_layers[0].bias		,				self.out_projs[0]      )
   return nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=-1      )
  else:
   # construct weights and biases
   a__							,			a__												= [], []
   for i in range(len(self.cutoffs      )      ):
    if self.div_val == 1:
     a__							,			a__												= self.cutoff_ends[i], self.cutoff_ends[i + 1]
     a__												= self.out_layers[0].weight[l_idx:r_idx]
     a__												= self.out_layers[0].bias[l_idx:r_idx]
    else:
     a__												= self.out_layers[i].weight
     a__												= self.out_layers[i].bias

    if i == 0:
     a__												= torch.cat([weight_i, self.cluster_weight]		,				dim=0      )
     a__												= torch.cat([bias_i, self.cluster_bias]		,				dim=0      )

    weights.append(SCREAMING_SNAKE_CASE      )
    biases.append(SCREAMING_SNAKE_CASE      )

   a__							,			a__							,			a__												= weights[0], biases[0], self.out_projs[0]
   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )

   a__												= hidden.new_empty((head_logit.size(0      ), self.n_token)      )
   a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )

   a__												= [0] + self.cutoffs
   for i in range(len(SCREAMING_SNAKE_CASE      ) - 1      ):
    a__							,			a__												= cutoff_values[i], cutoff_values[i + 1]

    if i == 0:
     a__												= head_logprob[:, : self.cutoffs[0]]
    else:
     a__							,			a__							,			a__												= weights[i], biases[i], self.out_projs[i]

     a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
     a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )

     a__												= head_logprob[:, -i] + tail_logprob_i
     a__												= logprob_i

   return out





