


"""simple docstring"""







import gc
import unittest

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

from diffusers import TransformeraDModel, VQDiffusionPipeline, VQDiffusionScheduler, VQModel
from diffusers.pipelines.vq_diffusion.pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings
from diffusers.utils import load_numpy, slow, torch_device
from diffusers.utils.testing_utils import require_torch_gpu


a__    :							str            =					False




class 			UpperCamelCase_     (			unittest.TestCase):

	"""simple docstring"""





	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     int:
						# clean up the VRAM after each test
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()




	@property
	def   UpperCAmelCase_				(			self      :    str       )	->     Any:
						return 1_2




	@property
	def   UpperCAmelCase_				(			self      :    str       )	->     int:
						return 1_2




	@property
	def   UpperCAmelCase_				(			self      :    str       )	->     Tuple:
						return 3_2




	@property
	def   UpperCAmelCase_				(			self      :    Union[str, Any]       )	->     Union[str, Any]:
						torch.manual_seed(0       )
						__SCREAMING_SNAKE_CASE						       =    VQModel(
						    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=3      ,      num_vq_embeddings=self.num_embed      ,      vq_embed_dim=3      ,      )
						return model




	@property
	def   UpperCAmelCase_				(			self      :    Optional[int]       )	->     Optional[Any]:
						__SCREAMING_SNAKE_CASE						       =    CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"       )
						return tokenizer




	@property
	def   UpperCAmelCase_				(			self      :    Tuple       )	->     Dict:
						torch.manual_seed(0       )
						__SCREAMING_SNAKE_CASE						       =    CLIPTextConfig(
						    bos_token_id=0      ,      eos_token_id=2      ,      hidden_size=self.text_embedder_hidden_size      ,      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      ,      )
						return CLIPTextModel(UpperCAmelCase__       )




	@property
	def   UpperCAmelCase_				(			self      :    Union[str, Any]       )	->     Optional[Any]:
						torch.manual_seed(0       )

						__SCREAMING_SNAKE_CASE						       =    1_2
						__SCREAMING_SNAKE_CASE						       =    1_2

						__SCREAMING_SNAKE_CASE						       =    {
						    "attention_bias": True,
						    "cross_attention_dim": 3_2,
						    "attention_head_dim": height * width,
						    "num_attention_heads": 1,
						    "num_vector_embeds": self.num_embed,
						    "num_embeds_ada_norm": self.num_embeds_ada_norm,
						    "norm_num_groups": 3_2,
						    "sample_size": width,
						    "activation_fn": "geglu-approximate",
						}

						__SCREAMING_SNAKE_CASE						       =    TransformeraDModel(**UpperCAmelCase__       )
						return model




	def   UpperCAmelCase_				(			self      :    str       )	->     Optional[int]:
						__SCREAMING_SNAKE_CASE						       =    "cpu"

						__SCREAMING_SNAKE_CASE						       =    self.dummy_vqvae
						__SCREAMING_SNAKE_CASE						       =    self.dummy_text_encoder
						__SCREAMING_SNAKE_CASE						       =    self.dummy_tokenizer
						__SCREAMING_SNAKE_CASE						       =    self.dummy_transformer
						__SCREAMING_SNAKE_CASE						       =    VQDiffusionScheduler(self.num_embed       )
						__SCREAMING_SNAKE_CASE						       =    LearnedClassifierFreeSamplingEmbeddings(learnable=UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    VQDiffusionPipeline(
						    vqvae=UpperCAmelCase__      ,      text_encoder=UpperCAmelCase__      ,      tokenizer=UpperCAmelCase__      ,      transformer=UpperCAmelCase__      ,      scheduler=UpperCAmelCase__      ,      learned_classifier_free_sampling_embeddings=UpperCAmelCase__      ,      )
						__SCREAMING_SNAKE_CASE						       =    pipe.to(UpperCAmelCase__       )
						pipe.set_progress_bar_config(disable=UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    "teddy bear playing in the pool"

						__SCREAMING_SNAKE_CASE						       =    torch.Generator(device=UpperCAmelCase__       ).manual_seed(0       )
						__SCREAMING_SNAKE_CASE						       =    pipe([prompt]      ,      generator=UpperCAmelCase__      ,      num_inference_steps=2      ,      output_type="np"       )
						__SCREAMING_SNAKE_CASE						       =    output.images

						__SCREAMING_SNAKE_CASE						       =    torch.Generator(device=UpperCAmelCase__       ).manual_seed(0       )
						__SCREAMING_SNAKE_CASE						       =    pipe(
						    [prompt]      ,      generator=UpperCAmelCase__      ,      output_type="np"      ,      return_dict=UpperCAmelCase__      ,      num_inference_steps=2       )[0]

						__SCREAMING_SNAKE_CASE						       =    image[0, -3:, -3:, -1]
						__SCREAMING_SNAKE_CASE						       =    image_from_tuple[0, -3:, -3:, -1]

						assert image.shape == (1, 2_4, 2_4, 3)

						__SCREAMING_SNAKE_CASE						       =    np.array([0.6_551, 0.6_168, 0.5_008, 0.5_676, 0.5_659, 0.4_295, 0.6_073, 0.5_599, 0.4_992]       )

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




	def   UpperCAmelCase_				(			self      :    Optional[Any]       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    "cpu"

						__SCREAMING_SNAKE_CASE						       =    self.dummy_vqvae
						__SCREAMING_SNAKE_CASE						       =    self.dummy_text_encoder
						__SCREAMING_SNAKE_CASE						       =    self.dummy_tokenizer
						__SCREAMING_SNAKE_CASE						       =    self.dummy_transformer
						__SCREAMING_SNAKE_CASE						       =    VQDiffusionScheduler(self.num_embed       )
						__SCREAMING_SNAKE_CASE						       =    LearnedClassifierFreeSamplingEmbeddings(
						    learnable=UpperCAmelCase__      ,      hidden_size=self.text_embedder_hidden_size      ,      length=tokenizer.model_max_length       )

						__SCREAMING_SNAKE_CASE						       =    VQDiffusionPipeline(
						    vqvae=UpperCAmelCase__      ,      text_encoder=UpperCAmelCase__      ,      tokenizer=UpperCAmelCase__      ,      transformer=UpperCAmelCase__      ,      scheduler=UpperCAmelCase__      ,      learned_classifier_free_sampling_embeddings=UpperCAmelCase__      ,      )
						__SCREAMING_SNAKE_CASE						       =    pipe.to(UpperCAmelCase__       )
						pipe.set_progress_bar_config(disable=UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    "teddy bear playing in the pool"

						__SCREAMING_SNAKE_CASE						       =    torch.Generator(device=UpperCAmelCase__       ).manual_seed(0       )
						__SCREAMING_SNAKE_CASE						       =    pipe([prompt]      ,      generator=UpperCAmelCase__      ,      num_inference_steps=2      ,      output_type="np"       )
						__SCREAMING_SNAKE_CASE						       =    output.images

						__SCREAMING_SNAKE_CASE						       =    torch.Generator(device=UpperCAmelCase__       ).manual_seed(0       )
						__SCREAMING_SNAKE_CASE						       =    pipe(
						    [prompt]      ,      generator=UpperCAmelCase__      ,      output_type="np"      ,      return_dict=UpperCAmelCase__      ,      num_inference_steps=2       )[0]

						__SCREAMING_SNAKE_CASE						       =    image[0, -3:, -3:, -1]
						__SCREAMING_SNAKE_CASE						       =    image_from_tuple[0, -3:, -3:, -1]

						assert image.shape == (1, 2_4, 2_4, 3)

						__SCREAMING_SNAKE_CASE						       =    np.array([0.6_693, 0.6_075, 0.4_959, 0.5_701, 0.5_583, 0.4_333, 0.6_171, 0.5_684, 0.4_988]       )

						assert np.abs(image_slice.flatten() - expected_slice       ).max() < 2.0
						assert np.abs(image_from_tuple_slice.flatten() - expected_slice       ).max() < 1E-2




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

	"""simple docstring"""





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




	def   UpperCAmelCase_				(			self      :    Any       )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    load_numpy(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
						    "/vq_diffusion/teddy_bear_pool_classifier_free_sampling.npy"       )

						__SCREAMING_SNAKE_CASE						       =    VQDiffusionPipeline.from_pretrained("microsoft/vq-diffusion-ithq"       )
						__SCREAMING_SNAKE_CASE						       =    pipeline.to(UpperCAmelCase__       )
						pipeline.set_progress_bar_config(disable=UpperCAmelCase__       )

						# requires GPU generator for gumbel softmax
						# don't use GPU generator in tests though
						__SCREAMING_SNAKE_CASE						       =    torch.Generator(device=UpperCAmelCase__       ).manual_seed(0       )
						__SCREAMING_SNAKE_CASE						       =    pipeline(
						    "teddy bear playing in the pool"      ,      num_images_per_prompt=1      ,      generator=UpperCAmelCase__      ,      output_type="np"      ,      )

						__SCREAMING_SNAKE_CASE						       =    output.images[0]

						assert image.shape == (2_5_6, 2_5_6, 3)
						assert np.abs(expected_image - image       ).max() < 2.0




"""simple docstring"""







from typing import Dict, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    flip_channel_order,
    get_resize_output_image_size,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging


if is_vision_available():
		import PIL

if is_torch_available():
		import torch


a__    :							Optional[int]            =					logging.get_logger(__name__)




class 			UpperCamelCase_     (			UpperCamelCase):

	"""simple docstring"""





	snake_case__    :  Optional[Any]    =      ["pixel_values"]




	def __init__(			self      :    Dict      ,      UpperCAmelCase__      :    bool = True      ,      UpperCAmelCase__      :    Dict[str, int] = None      ,      UpperCAmelCase__      :    PILImageResampling = PILImageResampling.BILINEAR      ,      UpperCAmelCase__      :    bool = True      ,      UpperCAmelCase__      :    Union[int, float] = 1 / 2_5_5      ,      UpperCAmelCase__      :    bool = True      ,      UpperCAmelCase__      :    Dict[str, int] = None      ,      UpperCAmelCase__      :    bool = True      ,      **UpperCAmelCase__      :    Dict      ,      )	->     None:
						super().__init__(**UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    size if size is not None else {"shortest_edge": 2_2_4}
						__SCREAMING_SNAKE_CASE						       =    get_size_dict(UpperCAmelCase__      ,      default_to_square=UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    crop_size if crop_size is not None else {"height": 2_5_6, "width": 2_5_6}
						__SCREAMING_SNAKE_CASE						       =    get_size_dict(UpperCAmelCase__      ,      param_name="crop_size"       )

						__SCREAMING_SNAKE_CASE						       =    do_resize
						__SCREAMING_SNAKE_CASE						       =    size
						__SCREAMING_SNAKE_CASE						       =    resample
						__SCREAMING_SNAKE_CASE						       =    do_rescale
						__SCREAMING_SNAKE_CASE						       =    rescale_factor
						__SCREAMING_SNAKE_CASE						       =    do_center_crop
						__SCREAMING_SNAKE_CASE						       =    crop_size
						__SCREAMING_SNAKE_CASE						       =    do_flip_channel_order




	def   UpperCAmelCase_				(			self      :    Any      ,      UpperCAmelCase__      :    np.ndarray      ,      UpperCAmelCase__      :    Dict[str, int]      ,      UpperCAmelCase__      :    PILImageResampling = PIL.Image.BILINEAR      ,      UpperCAmelCase__      :    Optional[Union[str, ChannelDimension]] = None      ,      **UpperCAmelCase__      :    Optional[Any]      ,      )	->     np.ndarray:
						__SCREAMING_SNAKE_CASE						       =    get_size_dict(UpperCAmelCase__      ,      default_to_square=UpperCAmelCase__       )
						if "shortest_edge" not in size:
											raise ValueError(F"""The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}"""       )
						__SCREAMING_SNAKE_CASE						       =    get_resize_output_image_size(UpperCAmelCase__      ,      size=size["shortest_edge"]      ,      default_to_square=UpperCAmelCase__       )
						return resize(UpperCAmelCase__      ,      size=UpperCAmelCase__      ,      resample=UpperCAmelCase__      ,      data_format=UpperCAmelCase__      ,      **UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    str      ,      UpperCAmelCase__      :    np.ndarray      ,      UpperCAmelCase__      :    Dict[str, int]      ,      UpperCAmelCase__      :    Optional[Union[str, ChannelDimension]] = None      ,      **UpperCAmelCase__      :    Any      ,      )	->     np.ndarray:
						__SCREAMING_SNAKE_CASE						       =    get_size_dict(UpperCAmelCase__       )
						if "height" not in size or "width" not in size:
											raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}"""       )
						return center_crop(UpperCAmelCase__      ,      size=(size["height"], size["width"])      ,      data_format=UpperCAmelCase__      ,      **UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Optional[int]      ,      UpperCAmelCase__      :    np.ndarray      ,      UpperCAmelCase__      :    Union[int, float]      ,      UpperCAmelCase__      :    Optional[Union[str, ChannelDimension]] = None      ,      **UpperCAmelCase__      :    Dict      ,      )	->     Dict:
						return rescale(UpperCAmelCase__      ,      scale=UpperCAmelCase__      ,      data_format=UpperCAmelCase__      ,      **UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Optional[int]      ,      UpperCAmelCase__      :    np.ndarray      ,      UpperCAmelCase__      :    Optional[Union[str, ChannelDimension]] = None       )	->     np.ndarray:
						return flip_channel_order(UpperCAmelCase__      ,      data_format=UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Optional[int]      ,      UpperCAmelCase__      :    ImageInput      ,      UpperCAmelCase__      :    bool = None      ,      UpperCAmelCase__      :    Dict[str, int] = None      ,      UpperCAmelCase__      :    PILImageResampling = None      ,      UpperCAmelCase__      :    bool = None      ,      UpperCAmelCase__      :    float = None      ,      UpperCAmelCase__      :    bool = None      ,      UpperCAmelCase__      :    Dict[str, int] = None      ,      UpperCAmelCase__      :    bool = None      ,      UpperCAmelCase__      :    Optional[Union[str, TensorType]] = None      ,      UpperCAmelCase__      :    ChannelDimension = ChannelDimension.FIRST      ,      **UpperCAmelCase__      :    str      ,      )	->     PIL.Image.Image:
						__SCREAMING_SNAKE_CASE						       =    do_resize if do_resize is not None else self.do_resize
						__SCREAMING_SNAKE_CASE						       =    resample if resample is not None else self.resample
						__SCREAMING_SNAKE_CASE						       =    do_rescale if do_rescale is not None else self.do_rescale
						__SCREAMING_SNAKE_CASE						       =    rescale_factor if rescale_factor is not None else self.rescale_factor
						__SCREAMING_SNAKE_CASE						       =    do_center_crop if do_center_crop is not None else self.do_center_crop
						__SCREAMING_SNAKE_CASE						       =    (
						    do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order
						)

						__SCREAMING_SNAKE_CASE						       =    size if size is not None else self.size
						__SCREAMING_SNAKE_CASE						       =    get_size_dict(UpperCAmelCase__      ,      default_to_square=UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    crop_size if crop_size is not None else self.crop_size
						__SCREAMING_SNAKE_CASE						       =    get_size_dict(UpperCAmelCase__      ,      param_name="crop_size"       )

						__SCREAMING_SNAKE_CASE						       =    make_list_of_images(UpperCAmelCase__       )

						if not valid_images(UpperCAmelCase__       ):
											raise ValueError(
											    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
											    "torch.Tensor, tf.Tensor or jax.ndarray."       )

						if do_resize and size is None:
											raise ValueError("Size must be specified if do_resize is True."       )

						if do_rescale and rescale_factor is None:
											raise ValueError("Rescale factor must be specified if do_rescale is True."       )

						if do_center_crop and crop_size is None:
											raise ValueError("Crop size must be specified if do_center_crop is True."       )

						# All transformations expect numpy arrays.
						__SCREAMING_SNAKE_CASE						       =    [to_numpy_array(UpperCAmelCase__       ) for image in images]

						if do_resize:
											__SCREAMING_SNAKE_CASE						       =    [self.resize(image=UpperCAmelCase__      ,      size=UpperCAmelCase__      ,      resample=UpperCAmelCase__       ) for image in images]

						if do_center_crop:
											__SCREAMING_SNAKE_CASE						       =    [self.center_crop(image=UpperCAmelCase__      ,      size=UpperCAmelCase__       ) for image in images]

						if do_rescale:
											__SCREAMING_SNAKE_CASE						       =    [self.rescale(image=UpperCAmelCase__      ,      scale=UpperCAmelCase__       ) for image in images]

						# the pretrained checkpoints assume images are BGR, not RGB
						if do_flip_channel_order:
											__SCREAMING_SNAKE_CASE						       =    [self.flip_channel_order(image=UpperCAmelCase__       ) for image in images]

						__SCREAMING_SNAKE_CASE						       =    [to_channel_dimension_format(UpperCAmelCase__      ,      UpperCAmelCase__       ) for image in images]

						__SCREAMING_SNAKE_CASE						       =    {"pixel_values": images}
						return BatchFeature(data=UpperCAmelCase__      ,      tensor_type=UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Optional[Any]      ,      UpperCAmelCase__      :    List[Any]      ,      UpperCAmelCase__      :    List[Tuple] = None       )	->     Optional[int]:
						__SCREAMING_SNAKE_CASE						       =    outputs.logits

						# Resize logits and compute semantic segmentation maps
						if target_sizes is not None:
											if len(UpperCAmelCase__       ) != len(UpperCAmelCase__       ):
																raise ValueError(
																    "Make sure that you pass in as many target sizes as the batch dimension of the logits"       )

											if is_torch_tensor(UpperCAmelCase__       ):
																__SCREAMING_SNAKE_CASE						       =    target_sizes.numpy()

											__SCREAMING_SNAKE_CASE						       =    []

											for idx in range(len(UpperCAmelCase__       )       ):
																__SCREAMING_SNAKE_CASE						       =    torch.nn.functional.interpolate(
																    logits[idx].unsqueeze(dim=0       )      ,      size=target_sizes[idx]      ,      mode="bilinear"      ,      align_corners=UpperCAmelCase__       )
																__SCREAMING_SNAKE_CASE						       =    resized_logits[0].argmax(dim=0       )
																semantic_segmentation.append(UpperCAmelCase__       )
						else:
											__SCREAMING_SNAKE_CASE						       =    logits.argmax(dim=1       )
											__SCREAMING_SNAKE_CASE						       =    [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]       )]

						return semantic_segmentation
