



'''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 							a__( lowerCamelCase__		,			unittest.TestCase     ):
    lowercase__			=   KandinskyInpaintPipeline
    lowercase__			=   ["""prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image"""]
    lowercase__			=   [
        """prompt""",
        """negative_prompt""",
        """image_embeds""",
        """negative_image_embeds""",
        """image""",
        """mask_image""",
    ]
    lowercase__			=   [
        """generator""",
        """height""",
        """width""",
        """latents""",
        """guidance_scale""",
        """negative_prompt""",
        """num_inference_steps""",
        """return_dict""",
        """guidance_scale""",
        """num_images_per_prompt""",
        """output_type""",
        """return_dict""",
    ]
    lowercase__			=   False







    @property
    def       lowercase_	(				self			:  List[Any]     ):
      return 32







    @property
    def       lowercase_	(				self			:  Optional[int]     ):
      return 32







    @property
    def       lowercase_	(				self			:  Optional[int]     ):
      return self.time_input_dim







    @property
    def       lowercase_	(				self			:  Optional[Any]     ):
      return self.time_input_dim * 4







    @property
    def       lowercase_	(				self			:  List[str]     ):
      return 1_00







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







    @property
    def       lowercase_	(				self			:  Optional[int]     ):
      torch.manual_seed(0     )
      a						: 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=10_05						,      )

      a						: Union[str, Any]					       =					MultilingualCLIP(__snake_case     )
      a						: Tuple					       =					text_encoder.eval()

      return text_encoder







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

      a						: Tuple					       =					{
          '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						: str					       =					UNetaDConditionModel(**__snake_case     )
      return model







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







    def       lowercase_	(				self			:  Tuple     ):
      a						: int					       =					self.dummy_text_encoder
      a						: Optional[int]					       =					self.dummy_tokenizer
      a						: str					       =					self.dummy_unet
      a						: Union[str, Any]					       =					self.dummy_movq

      a						: Tuple					       =					DDIMScheduler(
          num_train_timesteps=10_00						,      beta_schedule='linear'						,      beta_start=0.00085						,      beta_end=0.012						,      clip_sample=__snake_case						,      set_alpha_to_one=__snake_case						,      steps_offset=1						,      prediction_type='epsilon'						,      thresholding=__snake_case						,      )

      a						: List[Any]					       =					{
          'text_encoder': text_encoder,
          'tokenizer': tokenizer,
          'unet': unet,
          'scheduler': scheduler,
          'movq': movq,
      }

      return components







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

      if str(__snake_case     ).startswith('mps'     ):
        a						: Optional[Any]					       =					torch.manual_seed(__snake_case     )
      else:
        a						: Optional[Any]					       =					torch.Generator(device=__snake_case     ).manual_seed(__snake_case     )
      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       lowercase_	(				self			:  Any     ):
      a						: List[str]					       =					'cpu'

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

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

      pipe.set_progress_bar_config(disable=__snake_case     )

      a						: Dict					       =					pipe(**self.get_dummy_inputs(__snake_case     )     )
      a						: List[Any]					       =					output.images

      a						: Any					       =					pipe(
          **self.get_dummy_inputs(__snake_case     )						,      return_dict=__snake_case						,      )[0]

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

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

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

      a						: Any					       =					np.array(
          [0.8326919, 0.73790467, 0.20918581, 0.9309612, 0.5511791, 0.43713328, 0.5513321, 0.49922934, 0.59497786]     )

      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       lowercase_	(				self			:  Any     ):
      super().test_inference_batch_single_identical(expected_max_diff=3e-3     )

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







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







    def       lowercase_	(				self			:  Optional[Any]     ):
      a						: Dict					       =					load_numpy(
          'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
          '/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy'     )

      a						: Tuple					       =					load_image(
          'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png'     )
      a						: str					       =					np.ones((7_68, 7_68)						,      dtype=np.floataa     )
      a						: str					       =					0

      a						: Any					       =					'a hat'

      a						: Union[str, Any]					       =					KandinskyPriorPipeline.from_pretrained(
          'kandinsky-community/kandinsky-2-1-prior'						,      torch_dtype=torch.floataa     )
      pipe_prior.to(__snake_case     )

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

      a						: str					       =					torch.Generator(device='cpu'     ).manual_seed(0     )
      a       ,     a						: List[Any]					       =					pipe_prior(
          __snake_case						,      generator=__snake_case						,      num_inference_steps=5						,      negative_prompt=''						,      ).to_tuple()

      a						: Union[str, Any]					       =					pipeline(
          __snake_case						,      image=__snake_case						,      mask_image=__snake_case						,      image_embeds=__snake_case						,      negative_image_embeds=__snake_case						,      generator=__snake_case						,      num_inference_steps=1_00						,      height=7_68						,      width=7_68						,      output_type='np'						,      )

      a						: List[str]					       =					output.images[0]

      assert image.shape == (7_68, 7_68, 3)

      assert_mean_pixel_difference(__snake_case						,      __snake_case     )




'''simple docstring'''






import re
from typing import Callable, List, Optional, Union

import tensorflow as tf


try:
     from tensorflow.keras.optimizers.legacy import Adam
except ImportError:
     from tensorflow.keras.optimizers import Adam

class 							a__( tf.keras.optimizers.schedules.LearningRateSchedule     ):







    def __init__(				self			:  Optional[Any]						,      __snake_case			:  float						,      __snake_case			:  Callable						,      __snake_case			:  int						,      __snake_case			:  float = 1.0						,      __snake_case			:  str = None						,      ):
      super().__init__()
      a						: Dict					       =					initial_learning_rate
      a						: Optional[int]					       =					warmup_steps
      a						: Tuple					       =					power
      a						: str					       =					decay_schedule_fn
      a						: Optional[int]					       =					name







    def __call__(				self			:  List[Any]						,      __snake_case			:  int     ):
      with tf.name_scope(self.name or 'WarmUp'     ) as name:
        # Implements polynomial warmup. i.e., if global_step < warmup_steps, the
        # learning rate will be `global_step/num_warmup_steps * init_lr`.
        a						: Optional[int]					       =					tf.cast(__snake_case						,      tf.floataa     )
        a						: Tuple					       =					tf.cast(self.warmup_steps						,      tf.floataa     )
        a						: str					       =					global_step_float / warmup_steps_float
        a						: Union[str, Any]					       =					self.initial_learning_rate * tf.math.pow(__snake_case						,      self.power     )
        return tf.cond(
            global_step_float < warmup_steps_float						,      lambda: warmup_learning_rate						,      lambda: self.decay_schedule_fn(step - self.warmup_steps     )						,      name=__snake_case						,      )







    def       lowercase_	(				self			:  Dict     ):
      return {
          "initial_learning_rate": self.initial_learning_rate,
          "decay_schedule_fn": self.decay_schedule_fn,
          "warmup_steps": self.warmup_steps,
          "power": self.power,
          "name": self.name,
      }
def 				lowerCamelCase__	(						_A			,      _A			,      _A			,      _A = 0.0			,      _A = 0.9			,      _A = 0.999			,      _A = 1E-8			,      _A = None			,      _A = None			,      _A = 0.0			,      _A = 1.0			,      _A = None			,      ):
  a						: Optional[int]					       =					tf.keras.optimizers.schedules.PolynomialDecay(
      initial_learning_rate=_A			,      decay_steps=num_train_steps - num_warmup_steps			,      end_learning_rate=init_lr * min_lr_ratio			,      power=_A			,      )
  if num_warmup_steps:
    a						: Tuple					       =					WarmUp(
        initial_learning_rate=_A			,      decay_schedule_fn=_A			,      warmup_steps=_A			,      )
  if weight_decay_rate > 0.0:
    a						: List[Any]					       =					AdamWeightDecay(
        learning_rate=_A			,      weight_decay_rate=_A			,      beta_a=_A			,      beta_a=_A			,      epsilon=_A			,      clipnorm=_A			,      global_clipnorm=_A			,      exclude_from_weight_decay=['LayerNorm', 'layer_norm', 'bias']			,      include_in_weight_decay=_A			,      )
  else:
    a						: Tuple					       =					tf.keras.optimizers.Adam(
        learning_rate=_A			,      beta_a=_A			,      beta_a=_A			,      epsilon=_A			,      clipnorm=_A			,      global_clipnorm=_A			,      )
  # We return the optimizer and the LR scheduler in order to better track the
  # evolution of the LR independently of the optimizer.
  return optimizer, lr_schedule

class 							a__( lowerCamelCase__     ):







    def __init__(				self			:  Union[str, Any]						,      __snake_case			:  Union[float, tf.keras.optimizers.schedules.LearningRateSchedule] = 0.001						,      __snake_case			:  float = 0.9						,      __snake_case			:  float = 0.999						,      __snake_case			:  float = 1e-7						,      __snake_case			:  bool = False						,      __snake_case			:  float = 0.0						,      __snake_case			:  Optional[List[str]] = None						,      __snake_case			:  Optional[List[str]] = None						,      __snake_case			:  str = "AdamWeightDecay"						,      **__snake_case			:  Optional[int]						,      ):
      super().__init__(__snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      **__snake_case     )
      a						: Tuple					       =					weight_decay_rate
      a						: Optional[Any]					       =					include_in_weight_decay
      a						: Any					       =					exclude_from_weight_decay







    @classmethod
    def       lowercase_	(				cls			:  Any						,      __snake_case			:  Optional[Any]     ):
      a						: Any					       =					{'WarmUp': WarmUp}
      return super(__snake_case						,      cls     ).from_config(__snake_case						,      custom_objects=__snake_case     )







    def       lowercase_	(				self			:  str						,      __snake_case			:  Optional[int]						,      __snake_case			:  Dict						,      __snake_case			:  Optional[int]     ):
      super(__snake_case						,      self     )._prepare_local(__snake_case						,      __snake_case						,      __snake_case     )
      a						: List[str]					       =					tf.constant(
          self.weight_decay_rate						,      name='adam_weight_decay_rate'     )







    def       lowercase_	(				self			:  int						,      __snake_case			:  Dict						,      __snake_case			:  List[Any]						,      __snake_case			:  str     ):
      a						: Optional[Any]					       =					self._do_use_weight_decay(var.name     )
      if do_decay:
        return var.assign_sub(
            learning_rate * var * apply_state[(var.device, var.dtype.base_dtype)]['weight_decay_rate']						,      use_locking=self._use_locking						,      )
      return tf.no_op()







    def       lowercase_	(				self			:  List[str]						,      __snake_case			:  List[str]						,      __snake_case			:  Any=None						,      **__snake_case			:  Tuple     ):
      a       ,     a						: Dict					       =					list(zip(*__snake_case     )     )
      return super(__snake_case						,      self     ).apply_gradients(zip(__snake_case						,      __snake_case     )						,      name=__snake_case						,      **__snake_case     )







    def       lowercase_	(				self			:  List[Any]						,      __snake_case			:  List[str]						,      __snake_case			:  Union[str, Any]						,      __snake_case			:  Optional[int]     ):
      if apply_state is None:
        return self._decayed_lr_t[var_dtype], {}

      a						: List[Any]					       =					apply_state or {}
      a						: Optional[Any]					       =					apply_state.get((var_device, var_dtype)     )
      if coefficients is None:
        a						: int					       =					self._fallback_apply_state(__snake_case						,      __snake_case     )
        a						: Any					       =					coefficients

      return coefficients["lr_t"], {"apply_state": apply_state}







    def       lowercase_	(				self			:  Dict						,      __snake_case			:  Union[str, Any]						,      __snake_case			:  Dict						,      __snake_case			:  Tuple=None     ):
      a       ,     a						: List[Any]					       =					self._get_lr(var.device						,      var.dtype.base_dtype						,      __snake_case     )
      a						: List[Any]					       =					self._decay_weights_op(__snake_case						,      __snake_case						,      __snake_case     )
      with tf.control_dependencies([decay]     ):
        return super(__snake_case						,      self     )._resource_apply_dense(__snake_case						,      __snake_case						,      **__snake_case     )







    def       lowercase_	(				self			:  Any						,      __snake_case			:  Optional[Any]						,      __snake_case			:  Optional[Any]						,      __snake_case			:  List[str]						,      __snake_case			:  int=None     ):
      a       ,     a						: Tuple					       =					self._get_lr(var.device						,      var.dtype.base_dtype						,      __snake_case     )
      a						: int					       =					self._decay_weights_op(__snake_case						,      __snake_case						,      __snake_case     )
      with tf.control_dependencies([decay]     ):
        return super(__snake_case						,      self     )._resource_apply_sparse(__snake_case						,      __snake_case						,      __snake_case						,      **__snake_case     )







    def       lowercase_	(				self			:  Optional[Any]     ):
      a						: Union[str, Any]					       =					super().get_config()
      config.update({'weight_decay_rate': self.weight_decay_rate}     )
      return config







    def       lowercase_	(				self			:  List[str]						,      __snake_case			:  Dict     ):
      if self.weight_decay_rate == 0:
        return False

      if self._include_in_weight_decay:
        for r in self._include_in_weight_decay:
          if re.search(__snake_case						,      __snake_case     ) is not None:
            return True

      if self._exclude_from_weight_decay:
        for r in self._exclude_from_weight_decay:
          if re.search(__snake_case						,      __snake_case     ) is not None:
            return False
      return True

class 							a__( lowerCamelCase__     ):







    def __init__(				self			:  List[str]     ):
      a						: List[Any]					       =					[]
      a						: Optional[int]					       =					None







    @property
    def       lowercase_	(				self			:  Any     ):
      if self._accum_steps is None:
        a						: List[Any]					       =					tf.Variable(
            tf.constant(0						,      dtype=tf.intaa     )						,      trainable=__snake_case						,      synchronization=tf.VariableSynchronization.ON_READ						,      aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA						,      )

      return self._accum_steps.value()







    @property
    def       lowercase_	(				self			:  Optional[int]     ):
      if not self._gradients:
        raise ValueError('The accumulator should be called first to initialize the gradients'     )
      return [gradient.value() if gradient is not None else gradient for gradient in self._gradients]







    def __call__(				self			:  List[Any]						,      __snake_case			:  Any     ):
      if not self._gradients:
        a						: Union[str, Any]					       =					self.step  # Create the step variable.
        self._gradients.extend(
            [
                tf.Variable(
                    tf.zeros_like(__snake_case     )						,      trainable=__snake_case						,      synchronization=tf.VariableSynchronization.ON_READ						,      aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA						,      )
                if gradient is not None
                else gradient
                for gradient in gradients
            ]     )
      if len(__snake_case     ) != len(self._gradients     ):
        raise ValueError(F"""Expected {len(self._gradients     )} gradients, but got {len(__snake_case     )}"""     )

      for accum_gradient, gradient in zip(self._gradients						,      __snake_case     ):
        if accum_gradient is not None and gradient is not None:
          accum_gradient.assign_add(__snake_case     )

      self._accum_steps.assign_add(1     )







    def       lowercase_	(				self			:  Tuple     ):
      if not self._gradients:
        return
      self._accum_steps.assign(0     )
      for gradient in self._gradients:
        if gradient is not None:
          gradient.assign(tf.zeros_like(__snake_case     )     )