



import gc
import random
import unittest

import numpy as np
import torch
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModel,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableUnCLIPImgaImgPipeline, UNetaDConditionModel
from diffusers.pipelines.pipeline_utils import DiffusionPipeline
from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    floats_tensor,
    load_image,
    load_numpy,
    require_torch_gpu,
    skip_mps,
    slow,
    torch_device,
)

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


enable_full_determinism()



class 	_lowercase							(   lowerCAmelCase,      lowerCAmelCase,      lowerCAmelCase,      unittest.TestCase	):





	"""simple docstring"""
	__A							=							StableUnCLIPImgaImgPipeline
	__A							=							TEXT_GUIDED_IMAGE_VARIATION_PARAMS
	__A							=							TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
	__A							=							frozenset(
	    []	)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
	__A							=							frozenset([]	)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   32
						a					     =   embedder_hidden_size

						# image encoding components

						a					     =   CLIPImageProcessor(crop_size=32		,       size=32		)

						torch.manual_seed(0		)
						a					     =   CLIPVisionModelWithProjection(
						    CLIPVisionConfig(
						        hidden_size=lowerCamelCase_		,       projection_dim=lowerCamelCase_		,       num_hidden_layers=5		,       num_attention_heads=4		,       image_size=32		,       intermediate_size=37		,       patch_size=1		,       )		)

						# regular denoising components

						torch.manual_seed(0		)
						a					     =   StableUnCLIPImageNormalizer(embedding_dim=lowerCamelCase_		)
						a					     =   DDPMScheduler(beta_schedule="squaredcos_cap_v2"		)

						torch.manual_seed(0		)
						a					     =   CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"		)

						torch.manual_seed(0		)
						a					     =   CLIPTextModel(
						    CLIPTextConfig(
						        bos_token_id=0		,       eos_token_id=2		,       hidden_size=lowerCamelCase_		,       projection_dim=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		,       )		)

						torch.manual_seed(0		)
						a					     =   UNetaDConditionModel(
						    sample_size=32		,       in_channels=4		,       out_channels=4		,       down_block_types=("CrossAttnDownBlock2D", "DownBlock2D")		,       up_block_types=("UpBlock2D", "CrossAttnUpBlock2D")		,       block_out_channels=(32, 64)		,       attention_head_dim=(2, 4)		,       class_embed_type="projection"		,       projection_class_embeddings_input_dim=embedder_projection_dim * 2		,       cross_attention_dim=lowerCamelCase_		,       layers_per_block=1		,       upcast_attention=lowerCamelCase_		,       use_linear_projection=lowerCamelCase_		,       )

						torch.manual_seed(0		)
						a					     =   DDIMScheduler(
						    beta_schedule="scaled_linear"		,       beta_start=0.0_0085		,       beta_end=0.012		,       prediction_type="v_prediction"		,       set_alpha_to_one=lowerCamelCase_		,       steps_offset=1		,       )

						torch.manual_seed(0		)
						a					     =   AutoencoderKL()

						a					     =   {
						    # image encoding components
						    "feature_extractor": feature_extractor,
						    "image_encoder": image_encoder.eval(),
						    # image noising components
						    "image_normalizer": image_normalizer.eval(),
						    "image_noising_scheduler": image_noising_scheduler,
						    # regular denoising components
						    "tokenizer": tokenizer,
						    "text_encoder": text_encoder.eval(),
						    "unet": unet.eval(),
						    "scheduler": scheduler,
						    "vae": vae.eval(),
						}

						return components




	def      UpperCamelCase_       (self		,       lowerCamelCase_		,       lowerCamelCase_=0		,       lowerCamelCase_=True		):
						"""simple docstring"""

						if str(lowerCamelCase_		).startswith("mps"		):
											a					     =   torch.manual_seed(lowerCamelCase_		)
						else:
											a					     =   torch.Generator(device=lowerCamelCase_		).manual_seed(lowerCamelCase_		)

						a					     =   floats_tensor((1, 3, 32, 32)		,       rng=random.Random(lowerCamelCase_		)		).to(lowerCamelCase_		)

						if pil_image:
											a					     =   input_image * 0.5 + 0.5
											a					     =   input_image.clamp(0		,       1		)
											a					     =   input_image.cpu().permute(0		,       2		,       3		,       1		).float().numpy()
											a					     =   DiffusionPipeline.numpy_to_pil(lowerCamelCase_		)[0]

						return {
						    "prompt": "An anime racoon running a marathon",
						    "image": input_image,
						    "generator": generator,
						    "num_inference_steps": 2,
						    "output_type": "np",
						}




	@skip_mps
	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   "cpu"  # ensure determinism for the device-dependent torch.Generator
						a					     =   self.get_dummy_components()
						a					     =   StableUnCLIPImgaImgPipeline(**lowerCamelCase_		)
						a					     =   sd_pipe.to(lowerCamelCase_		)
						sd_pipe.set_progress_bar_config(disable=lowerCamelCase_		)

						a					     =   self.get_dummy_inputs(lowerCamelCase_		)
						inputs.update({"image_embeds": None}		)
						a					     =   sd_pipe(**lowerCamelCase_		).images
						a					     =   image[0, -3:, -3:, -1]

						assert image.shape == (1, 32, 32, 3)
						a					     =   np.array([0.3872, 0.7224, 0.5601, 0.4741, 0.6872, 0.5814, 0.4636, 0.3867, 0.5078]		)

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




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   torch_device in ["cpu", "mps"]

						self._test_attention_slicing_forward_pass(test_max_difference=lowerCamelCase_		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   torch_device in ["cpu", "mps"]

						self._test_inference_batch_single_identical(test_max_difference=lowerCamelCase_		)




	@unittest.skipIf(
	    torch_device != "cuda" or not is_xformers_available()		,       reason="XFormers attention is only available with CUDA and `xformers` installed"		,       )
	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						self._test_xformers_attention_forwardGenerator_pass(test_max_difference=lowerCamelCase_		)




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





	"""simple docstring"""




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   load_image(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png"		)

						a					     =   load_numpy(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_img2img_anime_turtle_fp16.npy"		)

						a					     =   StableUnCLIPImgaImgPipeline.from_pretrained(
						    "fusing/stable-unclip-2-1-l-img2img"		,       torch_dtype=torch.floataa		)
						pipe.to(lowerCamelCase_		)
						pipe.set_progress_bar_config(disable=lowerCamelCase_		)
						# stable unclip will oom when integration tests are run on a V100,
						# so turn on memory savings
						pipe.enable_attention_slicing()
						pipe.enable_sequential_cpu_offload()

						a					     =   torch.Generator(device="cpu"		).manual_seed(0		)
						a					     =   pipe(lowerCamelCase_		,       "anime turle"		,       generator=lowerCamelCase_		,       output_type="np"		)

						a					     =   output.images[0]

						assert image.shape == (768, 768, 3)

						assert_mean_pixel_difference(lowerCamelCase_		,       lowerCamelCase_		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   load_image(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png"		)

						a					     =   load_numpy(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_h_img2img_anime_turtle_fp16.npy"		)

						a					     =   StableUnCLIPImgaImgPipeline.from_pretrained(
						    "fusing/stable-unclip-2-1-h-img2img"		,       torch_dtype=torch.floataa		)
						pipe.to(lowerCamelCase_		)
						pipe.set_progress_bar_config(disable=lowerCamelCase_		)
						# stable unclip will oom when integration tests are run on a V100,
						# so turn on memory savings
						pipe.enable_attention_slicing()
						pipe.enable_sequential_cpu_offload()

						a					     =   torch.Generator(device="cpu"		).manual_seed(0		)
						a					     =   pipe(lowerCamelCase_		,       "anime turle"		,       generator=lowerCamelCase_		,       output_type="np"		)

						a					     =   output.images[0]

						assert image.shape == (768, 768, 3)

						assert_mean_pixel_difference(lowerCamelCase_		,       lowerCamelCase_		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   load_image(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png"		)

						torch.cuda.empty_cache()
						torch.cuda.reset_max_memory_allocated()
						torch.cuda.reset_peak_memory_stats()

						a					     =   StableUnCLIPImgaImgPipeline.from_pretrained(
						    "fusing/stable-unclip-2-1-h-img2img"		,       torch_dtype=torch.floataa		)
						a					     =   pipe.to(lowerCamelCase_		)
						pipe.set_progress_bar_config(disable=lowerCamelCase_		)
						pipe.enable_attention_slicing()
						pipe.enable_sequential_cpu_offload()

						a					     =   pipe(
						    lowerCamelCase_		,       "anime turtle"		,       num_inference_steps=2		,       output_type="np"		,       )

						a					     =   torch.cuda.max_memory_allocated()
						# make sure that less than 7 GB is allocated
						assert mem_bytes < 7 * 10**9





import unittest

from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


_lowercase: Optional[int]      			=		get_tests_dir("fixtures/test_sentencepiece.model")



@require_sentencepiece
class 	_lowercase							(   lowerCAmelCase,      unittest.TestCase	):





	"""simple docstring"""
	__A							=							XLMProphetNetTokenizer
	__A							=							False
	__A							=							True




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						super().setUp()

						# We have a SentencePiece fixture for testing
						a					     =   XLMProphetNetTokenizer(lowerCamelCase_		,       keep_accents=lowerCamelCase_		)
						tokenizer.save_pretrained(self.tmpdirname		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   "[PAD]"
						a					     =   0

						self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase_		)		,       lowerCamelCase_		)
						self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase_		)		,       lowerCamelCase_		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   list(self.get_tokenizer().get_vocab().keys()		)

						self.assertEqual(vocab_keys[0]		,       "[PAD]"		)
						self.assertEqual(vocab_keys[1]		,       "[CLS]"		)
						self.assertEqual(vocab_keys[-1]		,       "j"		)
						self.assertEqual(len(lowerCamelCase_		)		,       1012		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						self.assertEqual(self.get_tokenizer().vocab_size		,       1012		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   XLMProphetNetTokenizer(lowerCamelCase_		,       keep_accents=lowerCamelCase_		)

						a					     =   tokenizer.tokenize("This is a test"		)
						self.assertListEqual(lowerCamelCase_		,       ["▁This", "▁is", "▁a", "▁t", "est"]		)

						self.assertListEqual(
						    tokenizer.convert_tokens_to_ids(lowerCamelCase_		)		,       [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]		,       )

						a					     =   tokenizer.tokenize("I was born in 92000, and this is falsé."		)
						self.assertListEqual(
						    lowerCamelCase_		,       [
						        SPIECE_UNDERLINE + "I",
						        SPIECE_UNDERLINE + "was",
						        SPIECE_UNDERLINE + "b",
						        "or",
						        "n",
						        SPIECE_UNDERLINE + "in",
						        SPIECE_UNDERLINE + "",
						        "9",
						        "2",
						        "0",
						        "0",
						        "0",
						        ",",
						        SPIECE_UNDERLINE + "and",
						        SPIECE_UNDERLINE + "this",
						        SPIECE_UNDERLINE + "is",
						        SPIECE_UNDERLINE + "f",
						        "al",
						        "s",
						        "é",
						        ".",
						    ]		,       )
						a					     =   tokenizer.convert_tokens_to_ids(lowerCamelCase_		)
						self.assertListEqual(
						    lowerCamelCase_		,       [
						        value + tokenizer.fairseq_offset
						        for value in [8, 21, 84, 55, 24, 19, 7, -9, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, -9, 4]
						    ]		,       )

						a					     =   tokenizer.convert_ids_to_tokens(lowerCamelCase_		)
						self.assertListEqual(
						    lowerCamelCase_		,       [
						        SPIECE_UNDERLINE + "I",
						        SPIECE_UNDERLINE + "was",
						        SPIECE_UNDERLINE + "b",
						        "or",
						        "n",
						        SPIECE_UNDERLINE + "in",
						        SPIECE_UNDERLINE + "",
						        "[UNK]",
						        "2",
						        "0",
						        "0",
						        "0",
						        ",",
						        SPIECE_UNDERLINE + "and",
						        SPIECE_UNDERLINE + "this",
						        SPIECE_UNDERLINE + "is",
						        SPIECE_UNDERLINE + "f",
						        "al",
						        "s",
						        "[UNK]",
						        ".",
						    ]		,       )




	@cached_property
	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						return XLMProphetNetTokenizer.from_pretrained("microsoft/xprophetnet-large-wiki100-cased"		)




	@slow
	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   "Hello World!"
						a					     =   [35389, 6672, 49, 2]
						self.assertListEqual(lowerCamelCase_		,       self.big_tokenizer.encode(lowerCamelCase_		)		)




	@slow
	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   {"input_ids": [[11073, 82783, 18, 26, 82783, 549, 51540, 248, 17209, 1301, 217, 20, 215186, 1325, 147, 17209, 1301, 217, 20, 56370, 53, 122020, 20, 16477, 27, 87355, 4548, 20, 4728, 78392, 17, 159969, 18, 26, 24491, 629, 15, 538, 22704, 5439, 15, 2788, 24491, 9885, 15, 43534, 605, 15, 814, 18403, 33200, 29, 15, 43534, 24458, 12410, 111, 24966, 83669, 9637, 144068, 26, 850, 22346, 27, 147, 24966, 83669, 83490, 26, 39113, 735, 27, 689, 656, 2800, 1339, 4600, 53, 122020, 115785, 34, 816, 1339, 46887, 18, 147, 53905, 1951, 42238, 41170, 17732, 834, 436, 15, 27523, 98733, 217, 147, 5542, 4981, 930, 17347, 16, 2], [20091, 629, 94, 82786, 58, 490, 20, 1528, 84, 53905, 344, 80592, 110128, 18822, 5267, 1306, 62, 152537, 308, 7997, 401, 124427, 549, 35442, 225, 109, 15055, 25748, 147, 7119, 43712, 34, 767, 135366, 18, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [592, 63784, 119466, 17, 147808, 88214, 18, 656, 81, 32, 3296, 10280, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
						# fmt: on

						self.tokenizer_integration_test_util(
						    expected_encoding=lowerCamelCase_		,       model_name="microsoft/xprophetnet-large-wiki100-cased"		,       revision="1acad1643ddd54a44df6a1b797ada8373685d90e"		,       )
