"""simple docstring"""






import gc
import random
import unittest

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

from diffusers import AutoencoderKL, CycleDiffusionPipeline, DDIMScheduler, UNetaDConditionModel
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, skip_mps

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


class     UpperCAmelCase						(_UpperCAmelCase    ,_UpperCAmelCase    ,unittest.TestCase  ):



    """simple docstring"""

    _UpperCAmelCase	:Union[str, Any]						    = CycleDiffusionPipeline
    _UpperCAmelCase	:Optional[int]						    = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
        "negative_prompt",
        "height",
        "width",
        "negative_prompt_embeds",
    }
    _UpperCAmelCase	:Any						    = PipelineTesterMixin.required_optional_params - {"latents"}
    _UpperCAmelCase	:Any						    = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"source_prompt"}  )
    _UpperCAmelCase	:List[Any]						    = IMAGE_TO_IMAGE_IMAGE_PARAMS
    _UpperCAmelCase	:Dict						    = IMAGE_TO_IMAGE_IMAGE_PARAMS






    def _snake_case			(   self							):
       torch.manual_seed(0							)
       lowercase__:   Optional[Any]					=      UNetaDConditionModel(
           block_out_channels=(32, 64)    ,			layers_per_block=2    ,			sample_size=32    ,			in_channels=4    ,			out_channels=4    ,			down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')    ,			up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')    ,			cross_attention_dim=32    ,			)
       lowercase__:   Tuple					=      DDIMScheduler(
           beta_start=0.00_085    ,			beta_end=0.012    ,			beta_schedule='''scaled_linear'''    ,			num_train_timesteps=1000    ,			clip_sample=_UpperCAmelCase    ,			set_alpha_to_one=_UpperCAmelCase    ,			)
       torch.manual_seed(0							)
       lowercase__:   Union[str, Any]					=      AutoencoderKL(
           block_out_channels=[32, 64]    ,			in_channels=3    ,			out_channels=3    ,			down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']    ,			up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']    ,			latent_channels=4    ,			)
       torch.manual_seed(0							)
       lowercase__:   Tuple					=      CLIPTextConfig(
           bos_token_id=0    ,			eos_token_id=2    ,			hidden_size=32    ,			intermediate_size=37    ,			layer_norm_eps=1e-0_5    ,			num_attention_heads=4    ,			num_hidden_layers=5    ,			pad_token_id=1    ,			vocab_size=1000    ,			)
       lowercase__:   int					=      CLIPTextModel(_UpperCAmelCase							)
       lowercase__:   Optional[Any]					=      CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''							)

       lowercase__:   int					=      {
           '''unet''': unet,
           '''scheduler''': scheduler,
           '''vae''': vae,
           '''text_encoder''': text_encoder,
           '''tokenizer''': tokenizer,
           '''safety_checker''': None,
           '''feature_extractor''': None,
       }
       return components






    def _snake_case			(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase=0							):
       lowercase__:   Tuple					=      floats_tensor((1, 3, 32, 32)    ,			rng=random.Random(_UpperCAmelCase							)							).to(_UpperCAmelCase							)
       lowercase__:   str					=      image / 2 + 0.5
       if str(_UpperCAmelCase							).startswith('''mps'''							):
          lowercase__:   int					=      torch.manual_seed(_UpperCAmelCase							)
       else:
          lowercase__:   Any					=      torch.Generator(device=_UpperCAmelCase							).manual_seed(_UpperCAmelCase							)
       lowercase__:   str					=      {
           '''prompt''': '''An astronaut riding an elephant''',
           '''source_prompt''': '''An astronaut riding a horse''',
           '''image''': image,
           '''generator''': generator,
           '''num_inference_steps''': 2,
           '''eta''': 0.1,
           '''strength''': 0.8,
           '''guidance_scale''': 3,
           '''source_guidance_scale''': 1,
           '''output_type''': '''numpy''',
       }
       return inputs






    def _snake_case			(   self							):
       lowercase__:   Tuple					=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator

       lowercase__:   Union[str, Any]					=      self.get_dummy_components()
       lowercase__:   str					=      CycleDiffusionPipeline(**_UpperCAmelCase							)
       lowercase__:   Tuple					=      pipe.to(_UpperCAmelCase							)
       pipe.set_progress_bar_config(disable=_UpperCAmelCase							)

       lowercase__:   Optional[int]					=      self.get_dummy_inputs(_UpperCAmelCase							)
       lowercase__:   Tuple					=      pipe(**_UpperCAmelCase							)
       lowercase__:   Any					=      output.images

       lowercase__:   int					=      images[0, -3:, -3:, -1]

       assert images.shape == (1, 32, 32, 3)
       lowercase__:   Any					=      np.array([0.4_459, 0.4_943, 0.4_544, 0.6_643, 0.5_474, 0.4_327, 0.5_701, 0.5_959, 0.5_179]							)

       assert np.abs(image_slice.flatten() - expected_slice							).max() < 1e-2






    @unittest.skipIf(torch_device != '''cuda'''    ,			'''This test requires a GPU'''							)
    def _snake_case			(   self							):
       lowercase__:   Optional[Any]					=      self.get_dummy_components()
       for name, module in components.items():
          if hasattr(_UpperCAmelCase    ,			'''half'''							):
             lowercase__:   str					=      module.half()
       lowercase__:   List[Any]					=      CycleDiffusionPipeline(**_UpperCAmelCase							)
       lowercase__:   List[str]					=      pipe.to(_UpperCAmelCase							)
       pipe.set_progress_bar_config(disable=_UpperCAmelCase							)

       lowercase__:   Dict					=      self.get_dummy_inputs(_UpperCAmelCase							)
       lowercase__:   Union[str, Any]					=      pipe(**_UpperCAmelCase							)
       lowercase__:   List[Any]					=      output.images

       lowercase__:   int					=      images[0, -3:, -3:, -1]

       assert images.shape == (1, 32, 32, 3)
       lowercase__:   int					=      np.array([0.3_506, 0.4_543, 0.446, 0.4_575, 0.5_195, 0.4_155, 0.5_273, 0.518, 0.4_116]							)

       assert np.abs(image_slice.flatten() - expected_slice							).max() < 1e-2






    @skip_mps
    def _snake_case			(   self							):
       return super().test_save_load_local()






    @unittest.skip('''non-deterministic pipeline'''							)
    def _snake_case			(   self							):
       return super().test_inference_batch_single_identical()






    @skip_mps
    def _snake_case			(   self							):
       return super().test_dict_tuple_outputs_equivalent()






    @skip_mps
    def _snake_case			(   self							):
       return super().test_save_load_optional_components()






    @skip_mps
    def _snake_case			(   self							):
       return super().test_attention_slicing_forward_pass()



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



    """simple docstring"""

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






    def _snake_case			(   self							):
       lowercase__:   List[Any]					=      load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
           '''/cycle-diffusion/black_colored_car.png'''							)
       lowercase__:   Optional[int]					=      load_numpy(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car_fp16.npy'''							)
       lowercase__:   List[str]					=      init_image.resize((512, 512)							)

       lowercase__:   Dict					=      '''CompVis/stable-diffusion-v1-4'''
       lowercase__:   Any					=      DDIMScheduler.from_pretrained(_UpperCAmelCase    ,			subfolder='''scheduler'''							)
       lowercase__:   Optional[int]					=      CycleDiffusionPipeline.from_pretrained(
           _UpperCAmelCase    ,			scheduler=_UpperCAmelCase    ,			safety_checker=_UpperCAmelCase    ,			torch_dtype=torch.floataa    ,			revision='''fp16'''							)

       pipe.to(_UpperCAmelCase							)
       pipe.set_progress_bar_config(disable=_UpperCAmelCase							)
       pipe.enable_attention_slicing()

       lowercase__:   int					=      '''A black colored car'''
       lowercase__:   List[str]					=      '''A blue colored car'''

       lowercase__:   Optional[int]					=      torch.manual_seed(0							)
       lowercase__:   Dict					=      pipe(
           prompt=_UpperCAmelCase    ,			source_prompt=_UpperCAmelCase    ,			image=_UpperCAmelCase    ,			num_inference_steps=100    ,			eta=0.1    ,			strength=0.85    ,			guidance_scale=3    ,			source_guidance_scale=1    ,			generator=_UpperCAmelCase    ,			output_type='''np'''    ,			)
       lowercase__:   Optional[int]					=      output.images

       # the values aren't exactly equal, but the images look the same visually
       assert np.abs(image - expected_image							).max() < 5e-1






    def _snake_case			(   self							):
       lowercase__:   Any					=      load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
           '''/cycle-diffusion/black_colored_car.png'''							)
       lowercase__:   List[str]					=      load_numpy(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car.npy'''							)
       lowercase__:   Tuple					=      init_image.resize((512, 512)							)

       lowercase__:   Any					=      '''CompVis/stable-diffusion-v1-4'''
       lowercase__:   Tuple					=      DDIMScheduler.from_pretrained(_UpperCAmelCase    ,			subfolder='''scheduler'''							)
       lowercase__:   Optional[int]					=      CycleDiffusionPipeline.from_pretrained(_UpperCAmelCase    ,			scheduler=_UpperCAmelCase    ,			safety_checker=_UpperCAmelCase							)

       pipe.to(_UpperCAmelCase							)
       pipe.set_progress_bar_config(disable=_UpperCAmelCase							)
       pipe.enable_attention_slicing()

       lowercase__:   List[str]					=      '''A black colored car'''
       lowercase__:   str					=      '''A blue colored car'''

       lowercase__:   Optional[Any]					=      torch.manual_seed(0							)
       lowercase__:   Tuple					=      pipe(
           prompt=_UpperCAmelCase    ,			source_prompt=_UpperCAmelCase    ,			image=_UpperCAmelCase    ,			num_inference_steps=100    ,			eta=0.1    ,			strength=0.85    ,			guidance_scale=3    ,			source_guidance_scale=1    ,			generator=_UpperCAmelCase    ,			output_type='''np'''    ,			)
       lowercase__:   List[Any]					=      output.images

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

"""simple docstring"""






from collections import defaultdict
from typing import Optional

from ..image_utils import load_image
from ..utils import (
    add_end_docstrings,
    is_torch_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_torch_available():
       import torch

       from ..models.auto.modeling_auto import MODEL_FOR_MASK_GENERATION_MAPPING

__A			   = logging.get_logger(__name__)


@add_end_docstrings(_UpperCAmelCase  )
class     UpperCAmelCase						(_UpperCAmelCase  ):



    """simple docstring"""

    def __init__(   self    ,			**_UpperCAmelCase							):
       super().__init__(**_UpperCAmelCase							)
       requires_backends(self    ,			'''vision'''							)
       requires_backends(self    ,			'''torch'''							)

       if self.framework != "pt":
          raise ValueError(F"""The {self.__class__} is only available in PyTorch."""							)

       self.check_model_type(_UpperCAmelCase							)






    def _snake_case			(   self    ,			**_UpperCAmelCase							):
       lowercase__:   List[Any]					=      {}
       lowercase__:   List[Any]					=      {}
       lowercase__:   Dict					=      {}
       # preprocess args
       if "points_per_batch" in kwargs:
          lowercase__:   Dict					=      kwargs['''points_per_batch''']
       if "points_per_crop" in kwargs:
          lowercase__:   Any					=      kwargs['''points_per_crop''']
       if "crops_n_layers" in kwargs:
          lowercase__:   Union[str, Any]					=      kwargs['''crops_n_layers''']
       if "crop_overlap_ratio" in kwargs:
          lowercase__:   Optional[Any]					=      kwargs['''crop_overlap_ratio''']
       if "crop_n_points_downscale_factor" in kwargs:
          lowercase__:   Union[str, Any]					=      kwargs['''crop_n_points_downscale_factor''']
       # postprocess args
       if "pred_iou_thresh" in kwargs:
          lowercase__:   Any					=      kwargs['''pred_iou_thresh''']
       if "stability_score_offset" in kwargs:
          lowercase__:   Tuple					=      kwargs['''stability_score_offset''']
       if "mask_threshold" in kwargs:
          lowercase__:   List[str]					=      kwargs['''mask_threshold''']
       if "stability_score_thresh" in kwargs:
          lowercase__:   str					=      kwargs['''stability_score_thresh''']
       if "crops_nms_thresh" in kwargs:
          lowercase__:   List[str]					=      kwargs['''crops_nms_thresh''']
       if "output_rle_mask" in kwargs:
          lowercase__:   Dict					=      kwargs['''output_rle_mask''']
       if "output_bboxes_mask" in kwargs:
          lowercase__:   int					=      kwargs['''output_bboxes_mask''']
       return preprocess_kwargs, forward_params, postprocess_kwargs






    def __call__(   self    ,			_UpperCAmelCase    ,			*_UpperCAmelCase    ,			_UpperCAmelCase=None    ,			_UpperCAmelCase=None    ,			**_UpperCAmelCase							):
       return super().__call__(_UpperCAmelCase    ,			*_UpperCAmelCase    ,			num_workers=_UpperCAmelCase    ,			batch_size=_UpperCAmelCase    ,			**_UpperCAmelCase							)






    def _snake_case			(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase=64    ,			_UpperCAmelCase = 0    ,			_UpperCAmelCase = 512 / 1500    ,			_UpperCAmelCase = 32    ,			_UpperCAmelCase = 1    ,			):
       lowercase__:   Union[str, Any]					=      load_image(_UpperCAmelCase							)
       lowercase__:   Dict					=      self.image_processor.size['''longest_edge''']
       lowercase__, lowercase__, lowercase__, lowercase__:   Optional[Any]					=      self.image_processor.generate_crop_boxes(
           _UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase							)
       lowercase__:   List[Any]					=      self.image_processor(images=_UpperCAmelCase    ,			return_tensors='''pt'''							)

       with self.device_placement():
          if self.framework == "pt":
             lowercase__:   Tuple					=      self.get_inference_context()
             with inference_context():
                lowercase__:   Optional[Any]					=      self._ensure_tensor_on_device(_UpperCAmelCase    ,			device=self.device							)
                lowercase__:   Any					=      self.model.get_image_embeddings(model_inputs.pop('''pixel_values'''							)							)
                lowercase__:   Tuple					=      image_embeddings

       lowercase__:   Optional[Any]					=      grid_points.shape[1]
       lowercase__:   Tuple					=      points_per_batch if points_per_batch is not None else n_points

       if points_per_batch <= 0:
          raise ValueError(
              '''Cannot have points_per_batch<=0. Must be >=1 to returned batched outputs. '''
              '''To return all points at once, set points_per_batch to None'''							)

       for i in range(0    ,			_UpperCAmelCase    ,			_UpperCAmelCase							):
          lowercase__:   Dict					=      grid_points[:, i : i + points_per_batch, :, :]
          lowercase__:   int					=      input_labels[:, i : i + points_per_batch]
          lowercase__:   Any					=      i == n_points - points_per_batch
          yield {
              "input_points": batched_points,
              "input_labels": labels,
              "input_boxes": crop_boxes,
              "is_last": is_last,
              **model_inputs,
          }






    def _snake_case			(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase=0.88    ,			_UpperCAmelCase=0.95    ,			_UpperCAmelCase=0    ,			_UpperCAmelCase=1    ,			):
       lowercase__:   List[Any]					=      model_inputs.pop('''input_boxes'''							)
       lowercase__:   List[Any]					=      model_inputs.pop('''is_last'''							)
       lowercase__:   Any					=      model_inputs.pop('''original_sizes'''							).tolist()
       lowercase__:   Union[str, Any]					=      model_inputs.pop('''reshaped_input_sizes'''							).tolist()

       lowercase__:   List[Any]					=      self.model(**_UpperCAmelCase							)

       # post processing happens here in order to avoid CPU GPU copies of ALL the masks
       lowercase__:   int					=      model_outputs['''pred_masks''']
       lowercase__:   str					=      self.image_processor.post_process_masks(
           _UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			binarize=_UpperCAmelCase							)
       lowercase__:   str					=      model_outputs['''iou_scores''']
       lowercase__, lowercase__, lowercase__:   Optional[int]					=      self.image_processor.filter_masks(
           masks[0]    ,			iou_scores[0]    ,			original_sizes[0]    ,			input_boxes[0]    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			)
       return {
           "masks": masks,
           "is_last": is_last,
           "boxes": boxes,
           "iou_scores": iou_scores,
       }






    def _snake_case			(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase=False    ,			_UpperCAmelCase=False    ,			_UpperCAmelCase=0.7    ,			):
       lowercase__:   int					=      []
       lowercase__:   str					=      []
       lowercase__:   List[Any]					=      []
       for model_output in model_outputs:
          all_scores.append(model_output.pop('''iou_scores'''							)							)
          all_masks.extend(model_output.pop('''masks'''							)							)
          all_boxes.append(model_output.pop('''boxes'''							)							)

       lowercase__:   Any					=      torch.cat(_UpperCAmelCase							)
       lowercase__:   Dict					=      torch.cat(_UpperCAmelCase							)
       lowercase__, lowercase__, lowercase__, lowercase__:   Any					=      self.image_processor.post_process_for_mask_generation(
           _UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase							)

       lowercase__:   Union[str, Any]					=      defaultdict(_UpperCAmelCase							)
       for output in model_outputs:
          for k, v in output.items():
             extra[k].append(_UpperCAmelCase							)

       lowercase__:   Any					=      {}
       if output_rle_mask:
          lowercase__:   Optional[Any]					=      rle_mask

       if output_bboxes_mask:
          lowercase__:   Optional[int]					=      bounding_boxes

       return {"masks": output_masks, "scores": iou_scores, **optional, **extra}
