




import numpy as np
import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel

from ...utils import logging


lowerCamelCase__				:     Any			       =							logging.get_logger(__name__)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			List[str]    ,    __UpperCAmelCase		:			Dict							)      ->				Tuple:
      SCREAMING_SNAKE_CASE_					=    nn.functional.normalize(__UpperCAmelCase							)
      SCREAMING_SNAKE_CASE_					=    nn.functional.normalize(__UpperCAmelCase							)
      return torch.mm(__UpperCAmelCase    ,    normalized_text_embeds.t()							)





class  lowerCamelCase_							(   _SCREAMING_SNAKE_CASE      ):


 '''simple docstring'''







 lowercase_       					=					CLIPConfig

 lowercase_       					=					["CLIPEncoderLayer"]



 def __init__(							self						:	int       ,       _lowerCAmelCase						:	CLIPConfig		):
       super().__init__(_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    CLIPVisionModel(config.vision_config		)
       SCREAMING_SNAKE_CASE_					=    nn.Linear(config.vision_config.hidden_size       ,       config.projection_dim       ,       bias=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    nn.Parameter(torch.ones(17       ,       config.projection_dim		)       ,       requires_grad=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_					=    nn.Parameter(torch.ones(3       ,       config.projection_dim		)       ,       requires_grad=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    nn.Parameter(torch.ones(17		)       ,       requires_grad=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_					=    nn.Parameter(torch.ones(3		)       ,       requires_grad=_lowerCAmelCase		)



 @torch.no_grad()
 def    lowerCAmelCase_						(							self						:	Optional[int]       ,       _lowerCAmelCase						:	Tuple       ,       _lowerCAmelCase						:	Union[str, Any]		):
       SCREAMING_SNAKE_CASE_					=    self.vision_model(_lowerCAmelCase		)[1]  # pooled_output
       SCREAMING_SNAKE_CASE_					=    self.visual_projection(_lowerCAmelCase		)

       # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
       SCREAMING_SNAKE_CASE_					=    cosine_distance(_lowerCAmelCase       ,       self.special_care_embeds		).cpu().float().numpy()
       SCREAMING_SNAKE_CASE_					=    cosine_distance(_lowerCAmelCase       ,       self.concept_embeds		).cpu().float().numpy()

       SCREAMING_SNAKE_CASE_					=    []
       SCREAMING_SNAKE_CASE_					=    image_embeds.shape[0]
       for i in range(_lowerCAmelCase		):
             SCREAMING_SNAKE_CASE_					=    {'special_scores': {}, 'special_care': [], 'concept_scores': {}, 'bad_concepts': []}

             # increase this value to create a stronger `nfsw` filter
             # at the cost of increasing the possibility of filtering benign images
             SCREAMING_SNAKE_CASE_					=    0.0

             for concept_idx in range(len(special_cos_dist[0]		)		):
                   SCREAMING_SNAKE_CASE_					=    special_cos_dist[i][concept_idx]
                   SCREAMING_SNAKE_CASE_					=    self.special_care_embeds_weights[concept_idx].item()
                   SCREAMING_SNAKE_CASE_					=    round(concept_cos - concept_threshold + adjustment       ,       3		)
                   if result_img["special_scores"][concept_idx] > 0:
                         result_img["special_care"].append({concept_idx, result_img['special_scores'][concept_idx]}		)
                         SCREAMING_SNAKE_CASE_					=    0.01

             for concept_idx in range(len(cos_dist[0]		)		):
                   SCREAMING_SNAKE_CASE_					=    cos_dist[i][concept_idx]
                   SCREAMING_SNAKE_CASE_					=    self.concept_embeds_weights[concept_idx].item()
                   SCREAMING_SNAKE_CASE_					=    round(concept_cos - concept_threshold + adjustment       ,       3		)
                   if result_img["concept_scores"][concept_idx] > 0:
                         result_img["bad_concepts"].append(_lowerCAmelCase		)

             result.append(_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    [len(res['bad_concepts']		) > 0 for res in result]

       for idx, has_nsfw_concept in enumerate(_lowerCAmelCase		):
             if has_nsfw_concept:
                   if torch.is_tensor(_lowerCAmelCase		) or torch.is_tensor(images[0]		):
                         SCREAMING_SNAKE_CASE_					=    torch.zeros_like(images[idx]		)  # black image
                   else:
                         SCREAMING_SNAKE_CASE_					=    np.zeros(images[idx].shape		)  # black image

       if any(_lowerCAmelCase		):
             logger.warning(
                 'Potential NSFW content was detected in one or more images. A black image will be returned instead.'
                 ' Try again with a different prompt and/or seed.'		)

       return images, has_nsfw_concepts




 @torch.no_grad()
 def    lowerCAmelCase_						(							self						:	Dict       ,       _lowerCAmelCase						:	torch.FloatTensor       ,       _lowerCAmelCase						:	torch.FloatTensor		):
       SCREAMING_SNAKE_CASE_					=    self.vision_model(_lowerCAmelCase		)[1]  # pooled_output
       SCREAMING_SNAKE_CASE_					=    self.visual_projection(_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    cosine_distance(_lowerCAmelCase       ,       self.special_care_embeds		)
       SCREAMING_SNAKE_CASE_					=    cosine_distance(_lowerCAmelCase       ,       self.concept_embeds		)

       # increase this value to create a stronger `nsfw` filter
       # at the cost of increasing the possibility of filtering benign images
       SCREAMING_SNAKE_CASE_					=    0.0

       SCREAMING_SNAKE_CASE_					=    special_cos_dist - self.special_care_embeds_weights + adjustment
       # special_scores = special_scores.round(decimals=3)
       SCREAMING_SNAKE_CASE_					=    torch.any(special_scores > 0       ,       dim=1		)
       SCREAMING_SNAKE_CASE_					=    special_care * 0.01
       SCREAMING_SNAKE_CASE_					=    special_adjustment.unsqueeze(1		).expand(-1       ,       cos_dist.shape[1]		)

       SCREAMING_SNAKE_CASE_					=    (cos_dist - self.concept_embeds_weights) + special_adjustment
       # concept_scores = concept_scores.round(decimals=3)
       SCREAMING_SNAKE_CASE_					=    torch.any(concept_scores > 0       ,       dim=1		)

       SCREAMING_SNAKE_CASE_					=    0.0  # black image

       return images, has_nsfw_concepts





import gc
import unittest

import numpy as np
import torch

from diffusers import RePaintPipeline, RePaintScheduler, UNetaDModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_image,
    load_numpy,
    nightly,
    require_torch_gpu,
    skip_mps,
    torch_device,
)

from ..pipeline_params import IMAGE_INPAINTING_BATCH_PARAMS, IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()





class  lowerCamelCase_							(   _SCREAMING_SNAKE_CASE ,     unittest.TestCase      ):


 '''simple docstring'''







 lowercase_       					=					RePaintPipeline
 lowercase_       					=					IMAGE_INPAINTING_PARAMS - {"width", "height", "guidance_scale"}
 lowercase_       					=					PipelineTesterMixin.required_optional_params - {
     "latents",
     "num_images_per_prompt",
     "callback",
     "callback_steps",
 }
 lowercase_       					=					IMAGE_INPAINTING_BATCH_PARAMS
 lowercase_       					=					False



 def    lowerCAmelCase_						(							self						:	int		):
       torch.manual_seed(0		)
       torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_					=    UNetaDModel(
           block_out_channels=(32, 64)       ,       layers_per_block=2       ,       sample_size=32       ,       in_channels=3       ,       out_channels=3       ,       down_block_types=('DownBlock2D', 'AttnDownBlock2D')       ,       up_block_types=('AttnUpBlock2D', 'UpBlock2D')       ,       )
       SCREAMING_SNAKE_CASE_					=    RePaintScheduler()
       SCREAMING_SNAKE_CASE_					=    {'unet': unet, 'scheduler': scheduler}
       return components



 def    lowerCAmelCase_						(							self						:	Optional[int]       ,       _lowerCAmelCase						:	Tuple       ,       _lowerCAmelCase						:	str=0		):
       if str(_lowerCAmelCase		).startswith('mps'		):
             SCREAMING_SNAKE_CASE_					=    torch.manual_seed(_lowerCAmelCase		)
       else:
             SCREAMING_SNAKE_CASE_					=    torch.Generator(device=_lowerCAmelCase		).manual_seed(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_					=    np.random.RandomState(_lowerCAmelCase		).standard_normal((1, 3, 32, 32)		)
       SCREAMING_SNAKE_CASE_					=    torch.from_numpy(_lowerCAmelCase		).to(device=_lowerCAmelCase       ,       dtype=torch.floataa		)
       SCREAMING_SNAKE_CASE_					=    (image > 0).to(device=_lowerCAmelCase       ,       dtype=torch.floataa		)
       SCREAMING_SNAKE_CASE_					=    {
           'image': image,
           'mask_image': mask,
           'generator': generator,
           'num_inference_steps': 5,
           'eta': 0.0,
           'jump_length': 2,
           'jump_n_sample': 2,
           'output_type': 'numpy',
       }
       return inputs



 def    lowerCAmelCase_						(							self						:	Optional[Any]		):
       SCREAMING_SNAKE_CASE_					=    'cpu'  # ensure determinism for the device-dependent torch.Generator
       SCREAMING_SNAKE_CASE_					=    self.get_dummy_components()
       SCREAMING_SNAKE_CASE_					=    RePaintPipeline(**_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_					=    sd_pipe.to(_lowerCAmelCase		)
       sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    self.get_dummy_inputs(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_					=    sd_pipe(**_lowerCAmelCase		).images
       SCREAMING_SNAKE_CASE_					=    image[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       SCREAMING_SNAKE_CASE_					=    np.array([1.0000, 0.5426, 0.5497, 0.2200, 1.0000, 1.0000, 0.5623, 1.0000, 0.6274]		)

       assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-3



 @skip_mps
 def    lowerCAmelCase_						(							self						:	str		):
       return super().test_save_load_local()



 @unittest.skip('non-deterministic pipeline'		)
 def    lowerCAmelCase_						(							self						:	List[Any]		):
       return super().test_inference_batch_single_identical()



 @skip_mps
 def    lowerCAmelCase_						(							self						:	List[Any]		):
       return super().test_dict_tuple_outputs_equivalent()



 @skip_mps
 def    lowerCAmelCase_						(							self						:	str		):
       return super().test_save_load_optional_components()




 @skip_mps
 def    lowerCAmelCase_						(							self						:	Tuple		):
       return super().test_attention_slicing_forward_pass()





@nightly
@require_torch_gpu
class  lowerCamelCase_							(   unittest.TestCase      ):


 '''simple docstring'''







 def    lowerCAmelCase_						(							self						:	List[str]		):
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()




 def    lowerCAmelCase_						(							self						:	Dict		):
       SCREAMING_SNAKE_CASE_					=    load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/'
           'repaint/celeba_hq_256.png'		)
       SCREAMING_SNAKE_CASE_					=    load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/mask_256.png'		)
       SCREAMING_SNAKE_CASE_					=    load_numpy(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/'
           'repaint/celeba_hq_256_result.npy'		)

       SCREAMING_SNAKE_CASE_					=    'google/ddpm-ema-celebahq-256'
       SCREAMING_SNAKE_CASE_					=    UNetaDModel.from_pretrained(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_					=    RePaintScheduler.from_pretrained(_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    RePaintPipeline(unet=_lowerCAmelCase       ,       scheduler=_lowerCAmelCase		).to(_lowerCAmelCase		)
       repaint.set_progress_bar_config(disable=_lowerCAmelCase		)
       repaint.enable_attention_slicing()

       SCREAMING_SNAKE_CASE_					=    torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_					=    repaint(
           _lowerCAmelCase       ,       _lowerCAmelCase       ,       num_inference_steps=250       ,       eta=0.0       ,       jump_length=10       ,       jump_n_sample=10       ,       generator=_lowerCAmelCase       ,       output_type='np'       ,       )
       SCREAMING_SNAKE_CASE_					=    output.images[0]

       assert image.shape == (256, 256, 3)
       assert np.abs(expected_image - image		).mean() < 1E-2