


"""simple docstring"""






from typing import List, Optional, Union

import torch
from transformers import (
    XLMRobertaTokenizer,
)

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


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

_lowerCamelCase     =  '\n    Examples:\n        ```py\n        >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline\n        >>> import torch\n\n        >>> pipe_prior = KandinskyPriorPipeline.from_pretrained(\"kandinsky-community/Kandinsky-2-1-prior\")\n        >>> pipe_prior.to(\"cuda\")\n\n        >>> prompt = \"red cat, 4k photo\"\n        >>> out = pipe_prior(prompt)\n        >>> image_emb = out.image_embeds\n        >>> negative_image_emb = out.negative_image_embeds\n\n        >>> pipe = KandinskyPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-1\")\n        >>> pipe.to(\"cuda\")\n\n        >>> image = pipe(\n        ...     prompt,\n        ...     image_embeds=image_emb,\n        ...     negative_image_embeds=negative_image_emb,\n        ...     height=768,\n        ...     width=768,\n        ...     num_inference_steps=100,\n        ... ).images\n\n        >>> image[0].save(\"cat.png\")\n        ```\n'


def __lowercase     (      lowerCamelCase_  :      Dict						,					lowerCamelCase_  :      Dict						,					lowerCamelCase_  :      int=8  ):
				SCREAMING_SNAKE_CASE__     							=				h // scale_factor**2
				if h % scale_factor**2 != 0:
								new_h += 1
				SCREAMING_SNAKE_CASE__     							=				w // scale_factor**2
				if w % scale_factor**2 != 0:
								new_w += 1
				return new_h * scale_factor, new_w * scale_factor


class 			lowerCamelCase_		(			lowercase__   ):





				"""simple docstring"""







				def __init__(						self       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		):
								super().__init__()

								self.register_modules(
								    text_encoder=__lowercase       ,		tokenizer=__lowercase       ,		unet=__lowercase       ,		scheduler=__lowercase       ,		movq=__lowercase       ,		)
								SCREAMING_SNAKE_CASE__     							=				2 ** (len(self.movq.config.block_out_channels     ) - 1)



				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__     ):
								if latents is None:
												SCREAMING_SNAKE_CASE__     							=				randn_tensor(__lowercase       ,		generator=__lowercase       ,		device=__lowercase       ,		dtype=__lowercase     )
								else:
												if latents.shape != shape:
																raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {shape}'''     )
												SCREAMING_SNAKE_CASE__     							=				latents.to(__lowercase     )

								SCREAMING_SNAKE_CASE__     							=				latents * scheduler.init_noise_sigma
								return latents



				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__=None       ,		):
								SCREAMING_SNAKE_CASE__     							=				len(__lowercase     ) if isinstance(__lowercase       ,		__lowercase     ) else 1
								# get prompt text embeddings
								SCREAMING_SNAKE_CASE__     							=				self.tokenizer(
								    __lowercase       ,		padding="max_length"       ,		truncation=__lowercase       ,		max_length=77       ,		return_attention_mask=__lowercase       ,		add_special_tokens=__lowercase       ,		return_tensors="pt"       ,		)

								SCREAMING_SNAKE_CASE__     							=				text_inputs.input_ids
								SCREAMING_SNAKE_CASE__     							=				self.tokenizer(__lowercase       ,		padding="longest"       ,		return_tensors="pt"     ).input_ids

								if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(__lowercase       ,		__lowercase     ):
												SCREAMING_SNAKE_CASE__     							=				self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]     )
												logger.warning(
												    "The following part of your input was truncated because CLIP can only handle sequences up to"
												    f''' {self.tokenizer.model_max_length} tokens: {removed_text}'''     )

								SCREAMING_SNAKE_CASE__     							=				text_input_ids.to(__lowercase     )
								SCREAMING_SNAKE_CASE__     							=				text_inputs.attention_mask.to(__lowercase     )

								SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     							=				self.text_encoder(
								    input_ids=__lowercase       ,		attention_mask=__lowercase     )

								SCREAMING_SNAKE_CASE__     							=				prompt_embeds.repeat_interleave(__lowercase       ,		dim=0     )
								SCREAMING_SNAKE_CASE__     							=				text_encoder_hidden_states.repeat_interleave(__lowercase       ,		dim=0     )
								SCREAMING_SNAKE_CASE__     							=				text_mask.repeat_interleave(__lowercase       ,		dim=0     )

								if do_classifier_free_guidance:
												SCREAMING_SNAKE_CASE__     							=				42
												if negative_prompt is None:
																SCREAMING_SNAKE_CASE__     							=				[""] * batch_size
												elif type(__lowercase     ) is not type(__lowercase     ):
																raise TypeError(
																    f'''`negative_prompt` should be the same type to `prompt`, but got {type(__lowercase     )} !='''
																    f''' {type(__lowercase     )}.'''     )
												elif isinstance(__lowercase       ,		__lowercase     ):
																SCREAMING_SNAKE_CASE__     							=				[negative_prompt]
												elif batch_size != len(__lowercase     ):
																raise ValueError(
																    f'''`negative_prompt`: {negative_prompt} has batch size {len(__lowercase     )}, but `prompt`:'''
																    f''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches'''
																    " the batch size of `prompt`."     )
												else:
																SCREAMING_SNAKE_CASE__     							=				negative_prompt

												SCREAMING_SNAKE_CASE__     							=				self.tokenizer(
												    __lowercase       ,		padding="max_length"       ,		max_length=77       ,		truncation=__lowercase       ,		return_attention_mask=__lowercase       ,		add_special_tokens=__lowercase       ,		return_tensors="pt"       ,		)
												SCREAMING_SNAKE_CASE__     							=				uncond_input.input_ids.to(__lowercase     )
												SCREAMING_SNAKE_CASE__     							=				uncond_input.attention_mask.to(__lowercase     )

												SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     							=				self.text_encoder(
												    input_ids=__lowercase       ,		attention_mask=__lowercase     )

												# duplicate unconditional embeddings for each generation per prompt, using mps friendly method

												SCREAMING_SNAKE_CASE__     							=				negative_prompt_embeds.shape[1]
												SCREAMING_SNAKE_CASE__     							=				negative_prompt_embeds.repeat(1       ,		__lowercase     )
												SCREAMING_SNAKE_CASE__     							=				negative_prompt_embeds.view(batch_size * num_images_per_prompt       ,		__lowercase     )

												SCREAMING_SNAKE_CASE__     							=				uncond_text_encoder_hidden_states.shape[1]
												SCREAMING_SNAKE_CASE__     							=				uncond_text_encoder_hidden_states.repeat(1       ,		__lowercase       ,		1     )
												SCREAMING_SNAKE_CASE__     							=				uncond_text_encoder_hidden_states.view(
												    batch_size * num_images_per_prompt       ,		__lowercase       ,		-1     )
												SCREAMING_SNAKE_CASE__     							=				uncond_text_mask.repeat_interleave(__lowercase       ,		dim=0     )

												# done duplicates

												# For classifier free guidance, we need to do two forward passes.
												# Here we concatenate the unconditional and text embeddings into a single batch
												# to avoid doing two forward passes
												SCREAMING_SNAKE_CASE__     							=				torch.cat([negative_prompt_embeds, prompt_embeds]     )
												SCREAMING_SNAKE_CASE__     							=				torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states]     )

												SCREAMING_SNAKE_CASE__     							=				torch.cat([uncond_text_mask, text_mask]     )

								return prompt_embeds, text_encoder_hidden_states, text_mask



				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__=0     ):
								if is_accelerate_available():
												from accelerate import cpu_offload
								else:
												raise ImportError("Please install accelerate via `pip install accelerate`"     )

								SCREAMING_SNAKE_CASE__     							=				torch.device(f'''cuda:{gpu_id}'''     )

								SCREAMING_SNAKE_CASE__     							=				[
								    self.unet,
								    self.text_encoder,
								    self.movq,
								]
								for cpu_offloaded_model in models:
												if cpu_offloaded_model is not None:
																cpu_offload(__lowercase       ,		__lowercase     )



				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__=0     ):
								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."     )

								SCREAMING_SNAKE_CASE__     							=				torch.device(f'''cuda:{gpu_id}'''     )

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

								SCREAMING_SNAKE_CASE__     							=				None
								for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]:
												SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     							=				cpu_offload_with_hook(__lowercase       ,		__lowercase       ,		prev_module_hook=__lowercase     )

								if self.safety_checker is not None:
												SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     							=				cpu_offload_with_hook(self.safety_checker       ,		__lowercase       ,		prev_module_hook=__lowercase     )

								# We'll offload the last model manually.
								SCREAMING_SNAKE_CASE__     							=				hook



				@property
				# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
				def   lowerCAmelCase__		(						self     ):
								if not hasattr(self.unet       ,		"_hf_hook"     ):
												return self.device
								for module in self.unet.modules():
												if (
												    hasattr(__lowercase       ,		"_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(__lowercase     )
				def __call__(						self       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__ = None       ,		UpperCAmelCase__ = 512       ,		UpperCAmelCase__ = 512       ,		UpperCAmelCase__ = 100       ,		UpperCAmelCase__ = 4.0       ,		UpperCAmelCase__ = 1       ,		UpperCAmelCase__ = None       ,		UpperCAmelCase__ = None       ,		UpperCAmelCase__ = "pil"       ,		UpperCAmelCase__ = True       ,		):

								if isinstance(__lowercase       ,		__lowercase     ):
												SCREAMING_SNAKE_CASE__     							=				1
								elif isinstance(__lowercase       ,		__lowercase     ):
												SCREAMING_SNAKE_CASE__     							=				len(__lowercase     )
								else:
												raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(__lowercase     )}'''     )

								SCREAMING_SNAKE_CASE__     							=				self._execution_device

								SCREAMING_SNAKE_CASE__     							=				batch_size * num_images_per_prompt
								SCREAMING_SNAKE_CASE__     							=				guidance_scale > 1.0

								SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     							=				self._encode_prompt(
								    __lowercase       ,		__lowercase       ,		__lowercase       ,		__lowercase       ,		__lowercase     )

								if isinstance(__lowercase       ,		__lowercase     ):
												SCREAMING_SNAKE_CASE__     							=				torch.cat(__lowercase       ,		dim=0     )
								if isinstance(__lowercase       ,		__lowercase     ):
												SCREAMING_SNAKE_CASE__     							=				torch.cat(__lowercase       ,		dim=0     )

								if do_classifier_free_guidance:
												SCREAMING_SNAKE_CASE__     							=				image_embeds.repeat_interleave(__lowercase       ,		dim=0     )
												SCREAMING_SNAKE_CASE__     							=				negative_image_embeds.repeat_interleave(__lowercase       ,		dim=0     )

								SCREAMING_SNAKE_CASE__     							=				torch.cat([negative_image_embeds, image_embeds]       ,		dim=0     ).to(
								    dtype=prompt_embeds.dtype       ,		device=__lowercase     )

								self.scheduler.set_timesteps(__lowercase       ,		device=__lowercase     )
								SCREAMING_SNAKE_CASE__     							=				self.scheduler.timesteps

								SCREAMING_SNAKE_CASE__     							=				self.unet.config.in_channels

								SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     							=				get_new_h_w(__lowercase       ,		__lowercase       ,		self.movq_scale_factor     )

								# create initial latent
								SCREAMING_SNAKE_CASE__     							=				self.prepare_latents(
								    (batch_size, num_channels_latents, height, width)       ,		text_encoder_hidden_states.dtype       ,		__lowercase       ,		__lowercase       ,		__lowercase       ,		self.scheduler       ,		)

								for i, t in enumerate(self.progress_bar(__lowercase     )     ):
												# expand the latents if we are doing classifier free guidance
												SCREAMING_SNAKE_CASE__     							=				torch.cat([latents] * 2     ) if do_classifier_free_guidance else latents

												SCREAMING_SNAKE_CASE__     							=				{"text_embeds": prompt_embeds, "image_embeds": image_embeds}
												SCREAMING_SNAKE_CASE__     							=				self.unet(
												    sample=__lowercase       ,		timestep=__lowercase       ,		encoder_hidden_states=__lowercase       ,		added_cond_kwargs=__lowercase       ,		return_dict=__lowercase       ,		)[0]

												if do_classifier_free_guidance:
																SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     							=				noise_pred.split(latents.shape[1]       ,		dim=1     )
																SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     							=				noise_pred.chunk(2     )
																SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     							=				variance_pred.chunk(2     )
																SCREAMING_SNAKE_CASE__     							=				noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
																SCREAMING_SNAKE_CASE__     							=				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"]
												):
																SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     							=				noise_pred.split(latents.shape[1]       ,		dim=1     )

												# compute the previous noisy sample x_t -> x_t-1
												SCREAMING_SNAKE_CASE__     							=				self.scheduler.step(
												    __lowercase       ,		__lowercase       ,		__lowercase       ,		generator=__lowercase       ,		).prev_sample
								# post-processing
								SCREAMING_SNAKE_CASE__     							=				self.movq.decode(__lowercase       ,		force_not_quantize=__lowercase     )["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"]:
												SCREAMING_SNAKE_CASE__     							=				image * 0.5 + 0.5
												SCREAMING_SNAKE_CASE__     							=				image.clamp(0       ,		1     )
												SCREAMING_SNAKE_CASE__     							=				image.cpu().permute(0       ,		2       ,		3       ,		1     ).float().numpy()

								if output_type == "pil":
												SCREAMING_SNAKE_CASE__     							=				self.numpy_to_pil(__lowercase     )

								if not return_dict:
												return (image,)

								return ImagePipelineOutput(images=__lowercase     )




"""simple docstring"""






import json
import os
import subprocess
import unittest
from ast import literal_eval

import pytest
from parameterized import parameterized_class

from . import is_sagemaker_available


if is_sagemaker_available():
	from sagemaker import Session, TrainingJobAnalytics
	from sagemaker.huggingface import HuggingFace


@pytest.mark.skipif(
    literal_eval(os.getenv("TEST_SAGEMAKER"       ,  "False"   )   ) is not True       ,  reason="Skipping test because should only be run when releasing minor transformers version"       ,  )
@pytest.mark.usefixtures("sm_env"   )
@parameterized_class(
    [
        {
            "framework": "pytorch",
            "script": "run_glue.py",
            "model_name_or_path": "distilbert-base-cased",
            "instance_type": "ml.g4dn.xlarge",
            "results": {"train_runtime": 650, "eval_accuracy": 0.6, "eval_loss": 0.9},
        },
        {
            "framework": "tensorflow",
            "script": "run_tf.py",
            "model_name_or_path": "distilbert-base-cased",
            "instance_type": "ml.g4dn.xlarge",
            "results": {"train_runtime": 600, "eval_accuracy": 0.3, "eval_loss": 0.9},
        },
    ]   )
class 			lowerCamelCase_		(			unittest.TestCase   ):





				"""simple docstring"""







				def   lowerCAmelCase__		(						self     ):
								if self.framework == "pytorch":
												subprocess.run(
												    f'''cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py'''.split()       ,		encoding="utf-8"       ,		check=UpperCAmelCase__       ,		)
								assert hasattr(self       ,		"env"     )



				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__=1     ):
								# creates estimator
								return HuggingFace(
								    entry_point=self.script       ,		source_dir=self.env.test_path       ,		role=self.env.role       ,		image_uri=self.env.image_uri       ,		base_job_name=f'''{self.env.base_job_name}-single'''       ,		instance_count=UpperCAmelCase__       ,		instance_type=self.instance_type       ,		debugger_hook_config=UpperCAmelCase__       ,		hyperparameters={**self.env.hyperparameters, "model_name_or_path": self.model_name_or_path}       ,		metric_definitions=self.env.metric_definitions       ,		py_version="py36"       ,		)



				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__     ):
								TrainingJobAnalytics(UpperCAmelCase__     ).export_csv(f'''{self.env.test_path}/{job_name}_metrics.csv'''     )






				def   lowerCAmelCase__		(						self     ):
								# create estimator
								SCREAMING_SNAKE_CASE__     							=				self.create_estimator()

								# run training
								estimator.fit()

								# result dataframe
								SCREAMING_SNAKE_CASE__     							=				TrainingJobAnalytics(estimator.latest_training_job.name     ).dataframe()

								# extract kpis
								SCREAMING_SNAKE_CASE__     							=				list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"]     )
								SCREAMING_SNAKE_CASE__     							=				list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"]     )
								# get train time from SageMaker job, this includes starting, preprocessing, stopping
								SCREAMING_SNAKE_CASE__     							=				(
								    Session().describe_training_job(estimator.latest_training_job.name     ).get("TrainingTimeInSeconds"       ,		99_9999     )
								)

								# assert kpis
								assert train_runtime <= self.results["train_runtime"]
								assert all(t >= self.results["eval_accuracy"] for t in eval_accuracy     )
								assert all(t <= self.results["eval_loss"] for t in eval_loss     )

								# dump tests result into json file to share in PR
								with open(f'''{estimator.latest_training_job.name}.json'''       ,		"w"     ) as outfile:
												json.dump({"train_time": train_runtime, "eval_accuracy": eval_accuracy, "eval_loss": eval_loss}       ,		UpperCAmelCase__     )
