





from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image

from ...models import UNetaDConditionModel, VQModel
from ...pipelines import DiffusionPipeline
from ...pipelines.pipeline_utils import ImagePipelineOutput
from ...schedulers import DDPMScheduler
from ...utils import (
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)


_SCREAMING_SNAKE_CASE 		=						logging.get_logger(__name__)  # pylint: disable=invalid-name

_SCREAMING_SNAKE_CASE 		=						"""
    Examples:
        ```py
        >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline
        >>> from diffusers.utils import load_image
        >>> import torch

        >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
        ...     \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16
        ... )
        >>> pipe_prior.to(\"cuda\")

        >>> prompt = \"A red cartoon frog, 4k\"
        >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)

        >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained(
        ...     \"kandinsky-community/kandinsky-2-2-decoder\", torch_dtype=torch.float16
        ... )
        >>> pipe.to(\"cuda\")

        >>> init_image = load_image(
        ...     \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\"
        ...     \"/kandinsky/frog.png\"
        ... )

        >>> image = pipe(
        ...     image=init_image,
        ...     image_embeds=image_emb,
        ...     negative_image_embeds=zero_image_emb,
        ...     height=768,
        ...     width=768,
        ...     num_inference_steps=100,
        ...     strength=0.2,
        ... ).images

        >>> image[0].save(\"red_frog.png\")
        ```
"""


def 							SCREAMING_SNAKE_CASE__  (      __a				,	__a				,	__a=8				):
	snake_case_						:     List[Any]     =		height // scale_factor**2
	if height % scale_factor**2 != 0:
		new_height += 1
	snake_case_						:     Tuple     =		width // scale_factor**2
	if width % scale_factor**2 != 0:
		new_width += 1
	return new_height * scale_factor, new_width * scale_factor


def 							SCREAMING_SNAKE_CASE__  (      __a				,	__a=5_12				,	__a=5_12				):
	snake_case_						:     List[str]     =		pil_image.resize((w, h)				,	resample=Image.BICUBIC				,	reducing_gap=1				)
	snake_case_						:     Dict     =		np.array(pil_image.convert('RGB'				)				)
	snake_case_						:     Optional[int]     =		arr.astype(np.floataa				) / 127.5 - 1
	snake_case_						:     str     =		np.transpose(__a				,	[2, 0, 1]				)
	snake_case_						:     Tuple     =		torch.from_numpy(__a				).unsqueeze(0				)
	return image


class SCREAMING_SNAKE_CASE_  (   snake_case_   ):






						def __init__( self : List[str]						,				_A : UNetaDConditionModel						,				_A : DDPMScheduler						,				_A : VQModel						,				)							->      List[Any]:







							"""simple docstring"""


							super().__init__()

							self.register_modules(
							    unet=_A						,				scheduler=_A						,				movq=_A						,				)
							snake_case_						:     List[Any]     =		2 ** (len(self.movq.config.block_out_channels		) - 1)






						def UpperCAmelCase_			( self : Optional[int]						,				_A : Any						,				_A : Tuple						,				_A : Union[str, Any]		)							->      Dict:







							"""simple docstring"""


							snake_case_						:     Tuple     =		min(int(num_inference_steps * strength		)						,				_A		)

							snake_case_						:     Union[str, Any]     =		max(num_inference_steps - init_timestep						,				0		)
							snake_case_						:     List[Any]     =		self.scheduler.timesteps[t_start:]

							return timesteps, num_inference_steps - t_start






						def UpperCAmelCase_			( self : Optional[Any]						,				_A : List[Any]						,				_A : Optional[int]						,				_A : Optional[int]						,				_A : int						,				_A : Optional[Any]						,				_A : Optional[int]						,				_A : Dict=None		)							->      Optional[Any]:







							"""simple docstring"""


							if not isinstance(_A						,				(torch.Tensor, PIL.Image.Image, list)		):
								raise ValueError(
								    F"""`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(_A		)}"""		)

							snake_case_						:     Tuple     =		image.to(device=_A						,				dtype=_A		)

							snake_case_						:     int     =		batch_size * num_images_per_prompt

							if image.shape[1] == 4:
								snake_case_						:     List[str]     =		image

							else:
								if isinstance(_A						,				_A		) and len(_A		) != batch_size:
									raise ValueError(
									    F"""You have passed a list of generators of length {len(_A		)}, but requested an effective batch"""
									    F""" size of {batch_size}. Make sure the batch size matches the length of the generators."""		)

								elif isinstance(_A						,				_A		):
									snake_case_						:     str     =		[
									    self.movq.encode(image[i : i + 1]		).latent_dist.sample(generator[i]		) for i in range(_A		)
									]
									snake_case_						:     Any     =		torch.cat(_A						,				dim=0		)
								else:
									snake_case_						:     List[str]     =		self.movq.encode(_A		).latent_dist.sample(_A		)

								snake_case_						:     Optional[Any]     =		self.movq.config.scaling_factor * init_latents

							snake_case_						:     Tuple     =		torch.cat([init_latents]						,				dim=0		)

							snake_case_						:     List[Any]     =		init_latents.shape
							snake_case_						:     List[str]     =		randn_tensor(_A						,				generator=_A						,				device=_A						,				dtype=_A		)

							# get latents
							snake_case_						:     Tuple     =		self.scheduler.add_noise(_A						,				_A						,				_A		)

							snake_case_						:     Optional[Any]     =		init_latents

							return latents






						def UpperCAmelCase_			( self : List[Any]						,				_A : Optional[Any]=0		)							->      Union[str, Any]:







							"""simple docstring"""


							if is_accelerate_available():
								from accelerate import cpu_offload
							else:
								raise ImportError('Please install accelerate via `pip install accelerate`'		)

							snake_case_						:     List[Any]     =		torch.device(F"""cuda:{gpu_id}"""		)

							snake_case_						:     List[Any]     =		[
							    self.unet,
							    self.movq,
							]
							for cpu_offloaded_model in models:
								if cpu_offloaded_model is not None:
									cpu_offload(_A						,				_A		)






						def UpperCAmelCase_			( self : Optional[Any]						,				_A : List[str]=0		)							->      int:







							"""simple docstring"""


							if is_accelerate_available() and is_accelerate_version('>='						,				'0.17.0.dev0'		):
								from accelerate import cpu_offload_with_hook
							else:
								raise ImportError('`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.'		)

							snake_case_						:     List[Any]     =		torch.device(F"""cuda:{gpu_id}"""		)

							if self.device.type != "cpu":
								self.to('cpu'						,				silence_dtype_warnings=_A		)
								torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

							snake_case_						:     Tuple     =		None
							for cpu_offloaded_model in [self.unet, self.movq]:
								snake_case_  ,snake_case_						:     List[str]     =		cpu_offload_with_hook(_A						,				_A						,				prev_module_hook=_A		)

							# We'll offload the last model manually.
							snake_case_						:     str     =		hook






						@property
						# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
						def UpperCAmelCase_			( self : str		)							->      List[str]:







							"""simple docstring"""


							if not hasattr(self.unet						,				'_hf_hook'		):
								return self.device
							for module in self.unet.modules():
								if (
								    hasattr(_A						,				'_hf_hook'		)
								    and hasattr(module._hf_hook						,				'execution_device'		)
								    and module._hf_hook.execution_device is not None
								):
									return torch.device(module._hf_hook.execution_device		)
							return self.device






						@torch.no_grad()
						@replace_example_docstring(_A		)
						def __call__( self : Union[str, Any]						,				_A : Union[torch.FloatTensor, List[torch.FloatTensor]]						,				_A : Union[torch.FloatTensor, PIL.Image.Image, List[torch.FloatTensor], List[PIL.Image.Image]]						,				_A : Union[torch.FloatTensor, List[torch.FloatTensor]]						,				_A : int = 512						,				_A : int = 512						,				_A : int = 100						,				_A : float = 4.0						,				_A : float = 0.3						,				_A : int = 1						,				_A : Optional[Union[torch.Generator, List[torch.Generator]]] = None						,				_A : Optional[str] = "pil"						,				_A : bool = True						,				)							->      List[Any]:







							"""simple docstring"""


							snake_case_						:     Tuple     =		self._execution_device

							snake_case_						:     Dict     =		guidance_scale > 1.0

							if isinstance(_A						,				_A		):
								snake_case_						:     List[Any]     =		torch.cat(_A						,				dim=0		)
							snake_case_						:     str     =		image_embeds.shape[0]
							if isinstance(_A						,				_A		):
								snake_case_						:     Dict     =		torch.cat(_A						,				dim=0		)

							if do_classifier_free_guidance:
								snake_case_						:     Dict     =		image_embeds.repeat_interleave(_A						,				dim=0		)
								snake_case_						:     Union[str, Any]     =		negative_image_embeds.repeat_interleave(_A						,				dim=0		)

							snake_case_						:     int     =		torch.cat([negative_image_embeds, image_embeds]						,				dim=0		).to(dtype=self.unet.dtype						,				device=_A		)

							if not isinstance(_A						,				_A		):
								snake_case_						:     Optional[Any]     =		[image]
							if not all(isinstance(_A						,				(PIL.Image.Image, torch.Tensor)		) for i in image		):
								raise ValueError(
								    F"""Input is in incorrect format: {[type(_A		) for i in image]}. Currently, we only support  PIL image and pytorch tensor"""		)

							snake_case_						:     Dict     =		torch.cat([prepare_image(_A						,				_A						,				_A		) for i in image]						,				dim=0		)
							snake_case_						:     str     =		image.to(dtype=image_embeds.dtype						,				device=_A		)

							snake_case_						:     List[str]     =		self.movq.encode(_A		)['latents']
							snake_case_						:     List[Any]     =		latents.repeat_interleave(_A						,				dim=0		)
							self.scheduler.set_timesteps(_A						,				device=_A		)
							snake_case_  ,snake_case_						:     int     =		self.get_timesteps(_A						,				_A						,				_A		)
							snake_case_						:     Dict     =		timesteps[:1].repeat(batch_size * num_images_per_prompt		)
							snake_case_  ,snake_case_						:     Tuple     =		downscale_height_and_width(_A						,				_A						,				self.movq_scale_factor		)
							snake_case_						:     str     =		self.prepare_latents(
							    _A						,				_A						,				_A						,				_A						,				image_embeds.dtype						,				_A						,				_A		)
							for i, t in enumerate(self.progress_bar(_A		)		):
								# expand the latents if we are doing classifier free guidance
								snake_case_						:     Union[str, Any]     =		torch.cat([latents] * 2		) if do_classifier_free_guidance else latents

								snake_case_						:     Any     =		{'image_embeds': image_embeds}
								snake_case_						:     str     =		self.unet(
								    sample=_A						,				timestep=_A						,				encoder_hidden_states=_A						,				added_cond_kwargs=_A						,				return_dict=_A						,				)[0]

								if do_classifier_free_guidance:
									snake_case_  ,snake_case_						:     Tuple     =		noise_pred.split(latents.shape[1]						,				dim=1		)
									snake_case_  ,snake_case_						:     int     =		noise_pred.chunk(2		)
									snake_case_  ,snake_case_						:     str     =		variance_pred.chunk(2		)
									snake_case_						:     Dict     =		noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
									snake_case_						:     Any     =		torch.cat([noise_pred, variance_pred_text]						,				dim=1		)

								if not (
								    hasattr(self.scheduler.config						,				'variance_type'		)
								    and self.scheduler.config.variance_type in ["learned", "learned_range"]
								):
									snake_case_  ,snake_case_						:     int     =		noise_pred.split(latents.shape[1]						,				dim=1		)

								# compute the previous noisy sample x_t -> x_t-1
								snake_case_						:     str     =		self.scheduler.step(
								    _A						,				_A						,				_A						,				generator=_A						,				)[0]

							# post-processing
							snake_case_						:     List[str]     =		self.movq.decode(_A						,				force_not_quantize=_A		)['sample']

							if output_type not in ["pt", "np", "pil"]:
								raise ValueError(F"""Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}"""		)

							if output_type in ["np", "pil"]:
								snake_case_						:     Tuple     =		image * 0.5 + 0.5
								snake_case_						:     List[Any]     =		image.clamp(0						,				1		)
								snake_case_						:     List[str]     =		image.cpu().permute(0						,				2						,				3						,				1		).float().numpy()

							if output_type == "pil":
								snake_case_						:     Optional[int]     =		self.numpy_to_pil(_A		)

							if not return_dict:
								return (image,)

							return ImagePipelineOutput(images=_A		)








from __future__ import annotations

import unittest

from transformers import AutoTokenizer, MBartConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
						import tensorflow as tf

						from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel
@require_tf
class SCREAMING_SNAKE_CASE_  :
						__magic_name__:     int	 =    MBartConfig
						__magic_name__:     str	 =    {}
						__magic_name__:     Union[str, Any]	 =    "gelu"






						def __init__( self : List[str]						,				_A : Optional[int]						,				_A : List[Any]=13						,				_A : List[Any]=7						,				_A : Dict=True						,				_A : Tuple=False						,				_A : Optional[Any]=99						,				_A : Dict=32						,				_A : str=2						,				_A : str=4						,				_A : Tuple=37						,				_A : Tuple=0.1						,				_A : Union[str, Any]=0.1						,				_A : Optional[int]=20						,				_A : Dict=2						,				_A : List[str]=1						,				_A : Union[str, Any]=0						,				)							->      List[Any]:







							"""simple docstring"""


							snake_case_						:     str     =		parent
							snake_case_						:     List[str]     =		batch_size
							snake_case_						:     List[str]     =		seq_length
							snake_case_						:     Union[str, Any]     =		is_training
							snake_case_						:     Optional[int]     =		use_labels
							snake_case_						:     Dict     =		vocab_size
							snake_case_						:     Union[str, Any]     =		hidden_size
							snake_case_						:     str     =		num_hidden_layers
							snake_case_						:     Optional[Any]     =		num_attention_heads
							snake_case_						:     Union[str, Any]     =		intermediate_size
							snake_case_						:     Any     =		hidden_dropout_prob
							snake_case_						:     Any     =		attention_probs_dropout_prob
							snake_case_						:     Optional[Any]     =		max_position_embeddings
							snake_case_						:     Optional[Any]     =		eos_token_id
							snake_case_						:     Tuple     =		pad_token_id
							snake_case_						:     int     =		bos_token_id






						def UpperCAmelCase_			( self : List[str]		)							->      Tuple:







							"""simple docstring"""


							snake_case_						:     List[Any]     =		ids_tensor([self.batch_size, self.seq_length - 1]						,				self.vocab_size		)
							snake_case_						:     int     =		tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size		)						,				1		)
							snake_case_						:     Dict     =		tf.concat([input_ids, eos_tensor]						,				axis=1		)

							snake_case_						:     Any     =		ids_tensor([self.batch_size, self.seq_length]						,				self.vocab_size		)

							snake_case_						:     Optional[Any]     =		self.config_cls(
							    vocab_size=self.vocab_size						,				d_model=self.hidden_size						,				encoder_layers=self.num_hidden_layers						,				decoder_layers=self.num_hidden_layers						,				encoder_attention_heads=self.num_attention_heads						,				decoder_attention_heads=self.num_attention_heads						,				encoder_ffn_dim=self.intermediate_size						,				decoder_ffn_dim=self.intermediate_size						,				dropout=self.hidden_dropout_prob						,				attention_dropout=self.attention_probs_dropout_prob						,				max_position_embeddings=self.max_position_embeddings						,				eos_token_ids=[2]						,				bos_token_id=self.bos_token_id						,				pad_token_id=self.pad_token_id						,				decoder_start_token_id=self.pad_token_id						,				**self.config_updates						,				)
							snake_case_						:     Union[str, Any]     =		prepare_mbart_inputs_dict(_A						,				_A						,				_A		)
							return config, inputs_dict






						def UpperCAmelCase_			( self : Optional[Any]						,				_A : Optional[Any]						,				_A : int		)							->      str:







							"""simple docstring"""


							snake_case_						:     Dict     =		TFMBartModel(config=_A		).get_decoder()
							snake_case_						:     Any     =		inputs_dict['input_ids']

							snake_case_						:     List[Any]     =		input_ids[:1, :]
							snake_case_						:     Dict     =		inputs_dict['attention_mask'][:1, :]
							snake_case_						:     Tuple     =		inputs_dict['head_mask']
							snake_case_						:     List[Any]     =		1

							# first forward pass
							snake_case_						:     Any     =		model(_A						,				attention_mask=_A						,				head_mask=_A						,				use_cache=_A		)

							snake_case_  ,snake_case_						:     str     =		outputs.to_tuple()
							snake_case_						:     int     =		past_key_values[1]


def 							SCREAMING_SNAKE_CASE__  (      __a				,	__a				,	__a				,	__a=None				,	__a=None				,	__a=None				,	__a=None				,	__a=None				,	):
	if attention_mask is None:
		snake_case_						:     Optional[int]     =		tf.cast(tf.math.not_equal(__a				,	config.pad_token_id				)				,	tf.inta				)
	if decoder_attention_mask is None:
		snake_case_						:     str     =		tf.concat(
		    [
		        tf.ones(decoder_input_ids[:, :1].shape				,	dtype=tf.inta				),
		        tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]				,	config.pad_token_id				)				,	tf.inta				),
		    ]				,	axis=-1				,	)
	if head_mask is None:
		snake_case_						:     str     =		tf.ones((config.encoder_layers, config.encoder_attention_heads)				)
	if decoder_head_mask is None:
		snake_case_						:     int     =		tf.ones((config.decoder_layers, config.decoder_attention_heads)				)
	if cross_attn_head_mask is None:
		snake_case_						:     Dict     =		tf.ones((config.decoder_layers, config.decoder_attention_heads)				)
	return {
	    "input_ids": input_ids,
	    "decoder_input_ids": decoder_input_ids,
	    "attention_mask": attention_mask,
	    "decoder_attention_mask": decoder_attention_mask,
	    "head_mask": head_mask,
	    "decoder_head_mask": decoder_head_mask,
	    "cross_attn_head_mask": cross_attn_head_mask,
	}
@require_tf
class SCREAMING_SNAKE_CASE_  (   snake_case_    ,			snake_case_    ,			unittest.TestCase   ):
						__magic_name__:     Tuple	 =    (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
						__magic_name__:     int	 =    (TFMBartForConditionalGeneration,) if is_tf_available() else ()
						__magic_name__:     Union[str, Any]	 =    (
						    {
						        "conversational": TFMBartForConditionalGeneration,
						        "feature-extraction": TFMBartModel,
						        "summarization": TFMBartForConditionalGeneration,
						        "text2text-generation": TFMBartForConditionalGeneration,
						        "translation": TFMBartForConditionalGeneration,
						    }
						    if is_tf_available()
						    else {}
						)
						__magic_name__:     Tuple	 =    True
						__magic_name__:     Tuple	 =    False
						__magic_name__:     Any	 =    False






						def UpperCAmelCase_			( self : Any						,				_A : Union[str, Any]						,				_A : List[Any]						,				_A : str						,				_A : int						,				_A : Dict		)							->      Union[str, Any]:







							"""simple docstring"""


							if pipeline_test_casse_name != "FeatureExtractionPipelineTests":
								# Exception encountered when calling layer '...'
								return True

							return False






						def UpperCAmelCase_			( self : Dict		)							->      Tuple:







							"""simple docstring"""


							snake_case_						:     Optional[Any]     =		TFMBartModelTester(self		)
							snake_case_						:     List[Any]     =		ConfigTester(self						,				config_class=_A		)






						def UpperCAmelCase_			( self : Optional[Any]		)							->      str:







							"""simple docstring"""


							self.config_tester.run_common_tests()






						def UpperCAmelCase_			( self : Union[str, Any]		)							->      Tuple:







							"""simple docstring"""


							snake_case_						:     Dict     =		self.model_tester.prepare_config_and_inputs_for_common()
							self.model_tester.check_decoder_model_past_large_inputs(*_A		)
@require_sentencepiece
@require_tokenizers
@require_tf
class SCREAMING_SNAKE_CASE_  (   unittest.TestCase   ):
						__magic_name__:     Optional[int]	 =    [
						    " UN Chief Says There Is No Military Solution in Syria",
						]
						__magic_name__:     Union[str, Any]	 =    [
						    "Şeful ONU declară că nu există o soluţie militară în Siria",
						]
						__magic_name__:     List[Any]	 =    "facebook/mbart-large-en-ro"






						@cached_property
						def UpperCAmelCase_			( self : str		)							->      List[Any]:







							"""simple docstring"""


							return AutoTokenizer.from_pretrained(self.model_name		)






						@cached_property
						def UpperCAmelCase_			( self : List[Any]		)							->      Any:







							"""simple docstring"""


							snake_case_						:     List[str]     =		TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name		)
							return model






						def UpperCAmelCase_			( self : Optional[int]						,				**_A : str		)							->      int:







							"""simple docstring"""


							snake_case_						:     List[str]     =		self.translate_src_text(**_A		)
							self.assertListEqual(self.expected_text						,				_A		)






						def UpperCAmelCase_			( self : Union[str, Any]						,				**_A : Dict		)							->      int:







							"""simple docstring"""


							snake_case_						:     Optional[Any]     =		self.tokenizer(self.src_text						,				**_A						,				return_tensors='tf'		)
							snake_case_						:     int     =		self.model.generate(
							    model_inputs.input_ids						,				attention_mask=model_inputs.attention_mask						,				num_beams=2		)
							snake_case_						:     Any     =		self.tokenizer.batch_decode(_A						,				skip_special_tokens=_A		)
							return generated_words






						@slow
						def UpperCAmelCase_			( self : str		)							->      List[str]:







							"""simple docstring"""


							self._assert_generated_batch_equal_expected()

