



# This model implementation is heavily based on:

import gc
import random
import tempfile
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    ControlNetModel,
    DDIMScheduler,
    StableDiffusionControlNetInpaintPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
    TEXT_TO_IMAGE_IMAGE_PARAMS,
)
from ..test_pipelines_common import (
    PipelineKarrasSchedulerTesterMixin,
    PipelineLatentTesterMixin,
    PipelineTesterMixin,
)


enable_full_determinism()

class    _SCREAMING_SNAKE_CASE						(_UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     unittest.TestCase			):
     lowerCAmelCase       =      StableDiffusionControlNetInpaintPipeline
     lowerCAmelCase       =      TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
     lowerCAmelCase       =      TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
     lowerCAmelCase       =      frozenset({"""control_image"""}			)  # skip `image` and `mask` for now, only test for control_image
     lowerCAmelCase       =      TEXT_TO_IMAGE_IMAGE_PARAMS





     def 						__snake_case     (							self			:				int					)->Union[str, Any]:
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     List[str]							=						UNetaDConditionModel(
          block_out_channels=(3_2, 6_4)  ,	layers_per_block=2  ,	sample_size=3_2  ,	in_channels=9  ,	out_channels=4  ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")  ,	up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")  ,	cross_attention_dim=3_2  ,	)
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						ControlNetModel(
          block_out_channels=(3_2, 6_4)  ,	layers_per_block=2  ,	in_channels=4  ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")  ,	cross_attention_dim=3_2  ,	conditioning_embedding_out_channels=(1_6, 3_2)  ,	)
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						DDIMScheduler(
          beta_start=0.0_0_0_8_5  ,	beta_end=0.0_1_2  ,	beta_schedule="scaled_linear"  ,	clip_sample=_lowerCAmelCase  ,	set_alpha_to_one=_lowerCAmelCase  ,	)
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						AutoencoderKL(
          block_out_channels=[3_2, 6_4]  ,	in_channels=3  ,	out_channels=3  ,	down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]  ,	up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]  ,	latent_channels=4  ,	)
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						CLIPTextConfig(
          bos_token_id=0  ,	eos_token_id=2  ,	hidden_size=3_2  ,	intermediate_size=3_7  ,	layer_norm_eps=1E-05  ,	num_attention_heads=4  ,	num_hidden_layers=5  ,	pad_token_id=1  ,	vocab_size=1_0_0_0  ,	)
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						CLIPTextModel(_lowerCAmelCase					)
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"					)

      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						{
          "unet": unet,
          "controlnet": controlnet,
          "scheduler": scheduler,
          "vae": vae,
          "text_encoder": text_encoder,
          "tokenizer": tokenizer,
          "safety_checker": None,
          "feature_extractor": None,
      }
      return components





     def 						__snake_case     (							self			:				Tuple  ,	UpperCamelCase			:				str  ,	UpperCamelCase			:				int=0					)->Tuple:





      if str(_lowerCAmelCase					).startswith("mps"					):
       __SCREAMING_SNAKE_CASE							:     str							=						torch.manual_seed(_lowerCAmelCase					)
      else:
       __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						torch.Generator(device=_lowerCAmelCase					).manual_seed(_lowerCAmelCase					)

      __SCREAMING_SNAKE_CASE							:     List[str]							=						2
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						randn_tensor(
          (1, 3, 3_2 * controlnet_embedder_scale_factor, 3_2 * controlnet_embedder_scale_factor)  ,	generator=_lowerCAmelCase  ,	device=torch.device(_lowerCAmelCase					)  ,	)
      __SCREAMING_SNAKE_CASE							:     str							=						floats_tensor((1, 3, 3_2, 3_2)  ,	rng=random.Random(_lowerCAmelCase					)					).to(_lowerCAmelCase					)
      __SCREAMING_SNAKE_CASE							:     Tuple							=						init_image.cpu().permute(0  ,	2  ,	3  ,	1					)[0]

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						Image.fromarray(np.uinta(_lowerCAmelCase					)					).convert("RGB"					).resize((6_4, 6_4)					)
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						Image.fromarray(np.uinta(init_image + 4					)					).convert("RGB"					).resize((6_4, 6_4)					)

      __SCREAMING_SNAKE_CASE							:     str							=						{
          "prompt": "A painting of a squirrel eating a burger",
          "generator": generator,
          "num_inference_steps": 2,
          "guidance_scale": 6.0,
          "output_type": "numpy",
          "image": image,
          "mask_image": mask_image,
          "control_image": control_image,
      }

      return inputs





     def 						__snake_case     (							self			:				int					)->str:
      return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3					)





     @unittest.skipIf(
         torch_device != "cuda" or not is_xformers_available()  ,	reason="XFormers attention is only available with CUDA and `xformers` installed"  ,	)
     def 						__snake_case     (							self			:				str					)->Optional[int]:
      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3					)






     def 						__snake_case     (							self			:				List[Any]					)->Optional[Any]:
      self._test_inference_batch_single_identical(expected_max_diff=2E-3					)






class    _SCREAMING_SNAKE_CASE						(_UpperCAmelCase			):
     lowerCAmelCase       =      StableDiffusionControlNetInpaintPipeline
     lowerCAmelCase       =      TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
     lowerCAmelCase       =      TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
     lowerCAmelCase       =      frozenset([]			)





     def 						__snake_case     (							self			:				Dict					)->Tuple:
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     Any							=						UNetaDConditionModel(
          block_out_channels=(3_2, 6_4)  ,	layers_per_block=2  ,	sample_size=3_2  ,	in_channels=4  ,	out_channels=4  ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")  ,	up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")  ,	cross_attention_dim=3_2  ,	)
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     Any							=						ControlNetModel(
          block_out_channels=(3_2, 6_4)  ,	layers_per_block=2  ,	in_channels=4  ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")  ,	cross_attention_dim=3_2  ,	conditioning_embedding_out_channels=(1_6, 3_2)  ,	)
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						DDIMScheduler(
          beta_start=0.0_0_0_8_5  ,	beta_end=0.0_1_2  ,	beta_schedule="scaled_linear"  ,	clip_sample=_lowerCAmelCase  ,	set_alpha_to_one=_lowerCAmelCase  ,	)
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						AutoencoderKL(
          block_out_channels=[3_2, 6_4]  ,	in_channels=3  ,	out_channels=3  ,	down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]  ,	up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]  ,	latent_channels=4  ,	)
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     int							=						CLIPTextConfig(
          bos_token_id=0  ,	eos_token_id=2  ,	hidden_size=3_2  ,	intermediate_size=3_7  ,	layer_norm_eps=1E-05  ,	num_attention_heads=4  ,	num_hidden_layers=5  ,	pad_token_id=1  ,	vocab_size=1_0_0_0  ,	)
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						CLIPTextModel(_lowerCAmelCase					)
      __SCREAMING_SNAKE_CASE							:     Tuple							=						CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						{
          "unet": unet,
          "controlnet": controlnet,
          "scheduler": scheduler,
          "vae": vae,
          "text_encoder": text_encoder,
          "tokenizer": tokenizer,
          "safety_checker": None,
          "feature_extractor": None,
      }
      return components






class    _SCREAMING_SNAKE_CASE						(_UpperCAmelCase      ,     _UpperCAmelCase      ,     unittest.TestCase			):
     lowerCAmelCase       =      StableDiffusionControlNetInpaintPipeline
     lowerCAmelCase       =      TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
     lowerCAmelCase       =      TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS





     def 						__snake_case     (							self			:				str					)->Dict:
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     Tuple							=						UNetaDConditionModel(
          block_out_channels=(3_2, 6_4)  ,	layers_per_block=2  ,	sample_size=3_2  ,	in_channels=9  ,	out_channels=4  ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")  ,	up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")  ,	cross_attention_dim=3_2  ,	)
      torch.manual_seed(0					)

      def init_weights(UpperCamelCase			:				List[str]					):
       if isinstance(_lowerCAmelCase  ,	torch.nn.Convad					):
        torch.nn.init.normal(m.weight					)
        m.bias.data.fill_(1.0					)

      __SCREAMING_SNAKE_CASE							:     List[str]							=						ControlNetModel(
          block_out_channels=(3_2, 6_4)  ,	layers_per_block=2  ,	in_channels=4  ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")  ,	cross_attention_dim=3_2  ,	conditioning_embedding_out_channels=(1_6, 3_2)  ,	)
      controlneta.controlnet_down_blocks.apply(_lowerCAmelCase					)

      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     Tuple							=						ControlNetModel(
          block_out_channels=(3_2, 6_4)  ,	layers_per_block=2  ,	in_channels=4  ,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")  ,	cross_attention_dim=3_2  ,	conditioning_embedding_out_channels=(1_6, 3_2)  ,	)
      controlneta.controlnet_down_blocks.apply(_lowerCAmelCase					)

      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						DDIMScheduler(
          beta_start=0.0_0_0_8_5  ,	beta_end=0.0_1_2  ,	beta_schedule="scaled_linear"  ,	clip_sample=_lowerCAmelCase  ,	set_alpha_to_one=_lowerCAmelCase  ,	)
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						AutoencoderKL(
          block_out_channels=[3_2, 6_4]  ,	in_channels=3  ,	out_channels=3  ,	down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]  ,	up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]  ,	latent_channels=4  ,	)
      torch.manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						CLIPTextConfig(
          bos_token_id=0  ,	eos_token_id=2  ,	hidden_size=3_2  ,	intermediate_size=3_7  ,	layer_norm_eps=1E-05  ,	num_attention_heads=4  ,	num_hidden_layers=5  ,	pad_token_id=1  ,	vocab_size=1_0_0_0  ,	)
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						CLIPTextModel(_lowerCAmelCase					)
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"					)

      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						MultiControlNetModel([controlneta, controlneta]					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						{
          "unet": unet,
          "controlnet": controlnet,
          "scheduler": scheduler,
          "vae": vae,
          "text_encoder": text_encoder,
          "tokenizer": tokenizer,
          "safety_checker": None,
          "feature_extractor": None,
      }
      return components





     def 						__snake_case     (							self			:				int  ,	UpperCamelCase			:				Dict  ,	UpperCamelCase			:				str=0					)->List[Any]:





      if str(_lowerCAmelCase					).startswith("mps"					):
       __SCREAMING_SNAKE_CASE							:     Any							=						torch.manual_seed(_lowerCAmelCase					)
      else:
       __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						torch.Generator(device=_lowerCAmelCase					).manual_seed(_lowerCAmelCase					)

      __SCREAMING_SNAKE_CASE							:     Dict							=						2

      __SCREAMING_SNAKE_CASE							:     Tuple							=						[
          randn_tensor(
              (1, 3, 3_2 * controlnet_embedder_scale_factor, 3_2 * controlnet_embedder_scale_factor)  ,	generator=_lowerCAmelCase  ,	device=torch.device(_lowerCAmelCase					)  ,	),
          randn_tensor(
              (1, 3, 3_2 * controlnet_embedder_scale_factor, 3_2 * controlnet_embedder_scale_factor)  ,	generator=_lowerCAmelCase  ,	device=torch.device(_lowerCAmelCase					)  ,	),
      ]
      __SCREAMING_SNAKE_CASE							:     List[str]							=						floats_tensor((1, 3, 3_2, 3_2)  ,	rng=random.Random(_lowerCAmelCase					)					).to(_lowerCAmelCase					)
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						init_image.cpu().permute(0  ,	2  ,	3  ,	1					)[0]

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						Image.fromarray(np.uinta(_lowerCAmelCase					)					).convert("RGB"					).resize((6_4, 6_4)					)
      __SCREAMING_SNAKE_CASE							:     List[str]							=						Image.fromarray(np.uinta(init_image + 4					)					).convert("RGB"					).resize((6_4, 6_4)					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						{
          "prompt": "A painting of a squirrel eating a burger",
          "generator": generator,
          "num_inference_steps": 2,
          "guidance_scale": 6.0,
          "output_type": "numpy",
          "image": image,
          "mask_image": mask_image,
          "control_image": control_image,
      }

      return inputs





     def 						__snake_case     (							self			:				List[Any]					)->Optional[Any]:
      __SCREAMING_SNAKE_CASE							:     List[str]							=						self.get_dummy_components()
      __SCREAMING_SNAKE_CASE							:     str							=						self.pipeline_class(**_lowerCAmelCase					)
      pipe.to(_lowerCAmelCase					)

      __SCREAMING_SNAKE_CASE							:     List[Any]							=						1_0.0
      __SCREAMING_SNAKE_CASE							:     Any							=						4

      __SCREAMING_SNAKE_CASE							:     int							=						self.get_dummy_inputs(_lowerCAmelCase					)
      __SCREAMING_SNAKE_CASE							:     Tuple							=						steps
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						scale
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						pipe(**_lowerCAmelCase					)[0]

      __SCREAMING_SNAKE_CASE							:     Tuple							=						self.get_dummy_inputs(_lowerCAmelCase					)
      __SCREAMING_SNAKE_CASE							:     Dict							=						steps
      __SCREAMING_SNAKE_CASE							:     Tuple							=						scale
      __SCREAMING_SNAKE_CASE							:     int							=						pipe(**_lowerCAmelCase  ,	control_guidance_start=0.1  ,	control_guidance_end=0.2					)[0]

      __SCREAMING_SNAKE_CASE							:     Any							=						self.get_dummy_inputs(_lowerCAmelCase					)
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						steps
      __SCREAMING_SNAKE_CASE							:     Dict							=						scale
      __SCREAMING_SNAKE_CASE							:     List[str]							=						pipe(**_lowerCAmelCase  ,	control_guidance_start=[0.1, 0.3]  ,	control_guidance_end=[0.2, 0.7]					)[0]

      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						self.get_dummy_inputs(_lowerCAmelCase					)
      __SCREAMING_SNAKE_CASE							:     str							=						steps
      __SCREAMING_SNAKE_CASE							:     Any							=						scale
      __SCREAMING_SNAKE_CASE							:     str							=						pipe(**_lowerCAmelCase  ,	control_guidance_start=0.4  ,	control_guidance_end=[0.5, 0.8]					)[0]

      # make sure that all outputs are different
      assert np.sum(np.abs(output_a - output_a					)					) > 1E-3
      assert np.sum(np.abs(output_a - output_a					)					) > 1E-3
      assert np.sum(np.abs(output_a - output_a					)					) > 1E-3





     def 						__snake_case     (							self			:				Dict					)->List[str]:
      return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3					)





     @unittest.skipIf(
         torch_device != "cuda" or not is_xformers_available()  ,	reason="XFormers attention is only available with CUDA and `xformers` installed"  ,	)
     def 						__snake_case     (							self			:				Any					)->Union[str, Any]:
      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3					)





     def 						__snake_case     (							self			:				List[str]					)->Optional[Any]:
      self._test_inference_batch_single_identical(expected_max_diff=2E-3					)






     def 						__snake_case     (							self			:				Union[str, Any]					)->Tuple:
      __SCREAMING_SNAKE_CASE							:     str							=						self.get_dummy_components()
      __SCREAMING_SNAKE_CASE							:     List[str]							=						self.pipeline_class(**_lowerCAmelCase					)
      pipe.to(_lowerCAmelCase					)
      pipe.set_progress_bar_config(disable=_lowerCAmelCase					)
      with tempfile.TemporaryDirectory() as tmpdir:
       try:
        # save_pretrained is not implemented for Multi-ControlNet
        pipe.save_pretrained(_lowerCAmelCase					)
       except NotImplementedError:
        pass






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





     def 						__snake_case     (							self			:				Optional[int]					)->Tuple:
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()





     def 						__snake_case     (							self			:				str					)->Dict:
      __SCREAMING_SNAKE_CASE							:     Any							=						ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny"					)

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						StableDiffusionControlNetInpaintPipeline.from_pretrained(
          "runwayml/stable-diffusion-inpainting"  ,	safety_checker=_lowerCAmelCase  ,	controlnet=_lowerCAmelCase					)
      pipe.enable_model_cpu_offload()
      pipe.set_progress_bar_config(disable=_lowerCAmelCase					)

      __SCREAMING_SNAKE_CASE							:     List[Any]							=						torch.Generator(device="cpu"					).manual_seed(0					)
      __SCREAMING_SNAKE_CASE							:     int							=						load_image(
          "https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png"					).resize((5_1_2, 5_1_2)					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						load_image(
          "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
          "/stable_diffusion_inpaint/input_bench_mask.png"					).resize((5_1_2, 5_1_2)					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						"pitch black hole"

      __SCREAMING_SNAKE_CASE							:     Dict							=						load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png"					).resize((5_1_2, 5_1_2)					)

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						pipe(
          _lowerCAmelCase  ,	image=_lowerCAmelCase  ,	mask_image=_lowerCAmelCase  ,	control_image=_lowerCAmelCase  ,	generator=_lowerCAmelCase  ,	output_type="np"  ,	num_inference_steps=3  ,	)

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

      assert image.shape == (5_1_2, 5_1_2, 3)

      __SCREAMING_SNAKE_CASE							:     int							=						load_numpy(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/inpaint.npy"					)

      assert np.abs(expected_image - image					).max() < 9E-2






     def 						__snake_case     (							self			:				Any					)->Optional[int]:
      __SCREAMING_SNAKE_CASE							:     Dict							=						ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_inpaint"					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						StableDiffusionControlNetInpaintPipeline.from_pretrained(
          "runwayml/stable-diffusion-v1-5"  ,	safety_checker=_lowerCAmelCase  ,	controlnet=_lowerCAmelCase					)
      __SCREAMING_SNAKE_CASE							:     Dict							=						DDIMScheduler.from_config(pipe.scheduler.config					)
      pipe.enable_model_cpu_offload()
      pipe.set_progress_bar_config(disable=_lowerCAmelCase					)

      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						torch.Generator(device="cpu"					).manual_seed(3_3					)

      __SCREAMING_SNAKE_CASE							:     List[str]							=						load_image(
          "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy.png"					)
      __SCREAMING_SNAKE_CASE							:     int							=						init_image.resize((5_1_2, 5_1_2)					)

      __SCREAMING_SNAKE_CASE							:     List[Any]							=						load_image(
          "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy_mask.png"					)
      __SCREAMING_SNAKE_CASE							:     int							=						mask_image.resize((5_1_2, 5_1_2)					)

      __SCREAMING_SNAKE_CASE							:     List[str]							=						"a handsome man with ray-ban sunglasses"

      def make_inpaint_condition(UpperCamelCase			:				int  ,	UpperCamelCase			:				Dict					):
       __SCREAMING_SNAKE_CASE							:     Dict							=						np.array(image.convert("RGB"					)					).astype(np.floataa					) / 2_5_5.0
       __SCREAMING_SNAKE_CASE							:     Optional[int]							=						np.array(image_mask.convert("L"					)					).astype(np.floataa					) / 2_5_5.0

       assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
       __SCREAMING_SNAKE_CASE							:     int							=						-1.0  # set as masked pixel
       __SCREAMING_SNAKE_CASE							:     List[Any]							=						np.expand_dims(_lowerCAmelCase  ,	0					).transpose(0  ,	3  ,	1  ,	2					)
       __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						torch.from_numpy(_lowerCAmelCase					)
       return image

      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						make_inpaint_condition(_lowerCAmelCase  ,	_lowerCAmelCase					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						pipe(
          _lowerCAmelCase  ,	image=_lowerCAmelCase  ,	mask_image=_lowerCAmelCase  ,	control_image=_lowerCAmelCase  ,	guidance_scale=9.0  ,	eta=1.0  ,	generator=_lowerCAmelCase  ,	num_inference_steps=2_0  ,	output_type="np"  ,	)
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						output.images[0]

      assert image.shape == (5_1_2, 5_1_2, 3)

      __SCREAMING_SNAKE_CASE							:     Any							=						load_numpy(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/boy_ray_ban.npy"					)

      assert np.abs(expected_image - image					).max() < 9E-2










import functools
import operator

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


UpperCamelCase			:					Union[str, Any]         =							logging.get_logger(__name__)

UpperCamelCase			:					List[Any]         =							{
    '''facebook/hubert-base-ls960''': '''https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json''',
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}

class      __snake_case				(   _UpperCAmelCase):
					SCREAMING_SNAKE_CASE_		:       Tuple								=			"hubert"
					def __init__(						self       :						List[Any]	,      _lowerCAmelCase       :						Any=3_2	,      _lowerCAmelCase       :						Optional[Any]=7_6_8	,      _lowerCAmelCase       :						str=1_2	,      _lowerCAmelCase       :						List[Any]=1_2	,      _lowerCAmelCase       :						Optional[int]=3_0_7_2	,      _lowerCAmelCase       :						int="gelu"	,      _lowerCAmelCase       :						List[str]=0.1	,      _lowerCAmelCase       :						Union[str, Any]=0.1	,      _lowerCAmelCase       :						Optional[Any]=0.1	,      _lowerCAmelCase       :						Optional[int]=True	,      _lowerCAmelCase       :						Any=0.0	,      _lowerCAmelCase       :						Tuple=0.1	,      _lowerCAmelCase       :						Optional[int]=0.1	,      _lowerCAmelCase       :						str=0.02	,      _lowerCAmelCase       :						str=1E-5	,      _lowerCAmelCase       :						Dict="group"	,      _lowerCAmelCase       :						Tuple="gelu"	,      _lowerCAmelCase       :						List[str]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)	,      _lowerCAmelCase       :						List[str]=(5, 2, 2, 2, 2, 2, 2)	,      _lowerCAmelCase       :						Union[str, Any]=(1_0, 3, 3, 3, 3, 2, 2)	,      _lowerCAmelCase       :						Any=False	,      _lowerCAmelCase       :						Union[str, Any]=1_2_8	,      _lowerCAmelCase       :						Any=1_6	,      _lowerCAmelCase       :						Tuple=False	,      _lowerCAmelCase       :						Dict=True	,      _lowerCAmelCase       :						Union[str, Any]=0.05	,      _lowerCAmelCase       :						Optional[int]=1_0	,      _lowerCAmelCase       :						str=2	,      _lowerCAmelCase       :						Any=0.0	,      _lowerCAmelCase       :						Tuple=1_0	,      _lowerCAmelCase       :						int=0	,      _lowerCAmelCase       :						Tuple="sum"	,      _lowerCAmelCase       :						Tuple=False	,      _lowerCAmelCase       :						str=False	,      _lowerCAmelCase       :						Tuple=2_5_6	,      _lowerCAmelCase       :						List[str]=0	,      _lowerCAmelCase       :						int=1	,      _lowerCAmelCase       :						int=2	,      **_lowerCAmelCase       :						Optional[Any]	,      )							->					Dict:



											'''simple docstring'''





											super().__init__(**_lowerCAmelCase	,      pad_token_id=_lowerCAmelCase	,      bos_token_id=_lowerCAmelCase	,      eos_token_id=_lowerCAmelCase  )
											__magic_name__					=      hidden_size
											__magic_name__					=      feat_extract_norm
											__magic_name__					=      feat_extract_activation
											__magic_name__					=      list(_lowerCAmelCase  )
											__magic_name__					=      list(_lowerCAmelCase  )
											__magic_name__					=      list(_lowerCAmelCase  )
											__magic_name__					=      conv_bias
											__magic_name__					=      num_conv_pos_embeddings
											__magic_name__					=      num_conv_pos_embedding_groups
											__magic_name__					=      len(self.conv_dim  )
											__magic_name__					=      num_hidden_layers
											__magic_name__					=      intermediate_size
											__magic_name__					=      hidden_act
											__magic_name__					=      num_attention_heads
											__magic_name__					=      hidden_dropout
											__magic_name__					=      attention_dropout
											__magic_name__					=      activation_dropout
											__magic_name__					=      feat_proj_layer_norm
											__magic_name__					=      feat_proj_dropout
											__magic_name__					=      final_dropout
											__magic_name__					=      layerdrop
											__magic_name__					=      layer_norm_eps
											__magic_name__					=      initializer_range
											__magic_name__					=      vocab_size
											__magic_name__					=      do_stable_layer_norm
											__magic_name__					=      use_weighted_layer_sum
											__magic_name__					=      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
											__magic_name__					=      apply_spec_augment
											__magic_name__					=      mask_time_prob
											__magic_name__					=      mask_time_length
											__magic_name__					=      mask_time_min_masks
											__magic_name__					=      mask_feature_prob
											__magic_name__					=      mask_feature_length
											__magic_name__					=      mask_feature_min_masks

											# ctc loss
											__magic_name__					=      ctc_loss_reduction
											__magic_name__					=      ctc_zero_infinity







					@property
					def 	a			(						self       :						Optional[int]  )							->					Any:



											'''simple docstring'''





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

