




import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
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


enable_full_determinism()
class     a__					(      unittest.TestCase   ):






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



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






						@property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = 1
											__lowerCAmelCase            = 3
											__lowerCAmelCase            = (3_2, 3_2)

											__lowerCAmelCase            = floats_tensor((batch_size, num_channels) + sizes	,    rng=random.Random(0    )    ).to(_A    )
											return image






						@property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											torch.manual_seed(0    )
											__lowerCAmelCase            = UNetaDConditionModel(
											    block_out_channels=(3_2, 6_4)	,    layers_per_block=2	,    sample_size=3_2	,    in_channels=4	,    out_channels=4	,    down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")	,    up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")	,    cross_attention_dim=3_2	,    )
											return model






						@property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											torch.manual_seed(0    )
											__lowerCAmelCase            = AutoencoderKL(
											    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=4	,    )
											return model






						@property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											torch.manual_seed(0    )
											__lowerCAmelCase            = RobertaSeriesConfig(
											    hidden_size=3_2	,    project_dim=3_2	,    intermediate_size=3_7	,    layer_norm_eps=1E-0_5	,    num_attention_heads=4	,    num_hidden_layers=5	,    pad_token_id=1	,    vocab_size=5_0_0_6	,    )
											return RobertaSeriesModelWithTransformation(_A    )






						@property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											def extract(*_A	,    **_A    ):
																class     a__					:






																						def __init__(     self    ):




																											"""simple docstring"""



																											__lowerCAmelCase            = torch.ones([0]    )






																						def        __SCREAMING_SNAKE_CASE(     self	,    _A    ):




																											"""simple docstring"""



																											self.pixel_values.to(_A    )
																											return self






																return Out()

											return extract






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = "cpu"  # ensure determinism for the device-dependent torch.Generator
											__lowerCAmelCase            = self.dummy_cond_unet
											__lowerCAmelCase            = PNDMScheduler(skip_prk_steps=_A    )
											__lowerCAmelCase            = self.dummy_vae
											__lowerCAmelCase            = self.dummy_text_encoder
											__lowerCAmelCase            = XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"    )
											__lowerCAmelCase            = 7_7

											__lowerCAmelCase            = self.dummy_image.to(_A    )
											__lowerCAmelCase            = init_image / 2 + 0.5

											# make sure here that pndm scheduler skips prk
											__lowerCAmelCase            = AltDiffusionImgaImgPipeline(
											    unet=_A	,    scheduler=_A	,    vae=_A	,    text_encoder=_A	,    tokenizer=_A	,    safety_checker=_A	,    feature_extractor=self.dummy_extractor	,    )
											__lowerCAmelCase            = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor	,    do_normalize=_A    )
											__lowerCAmelCase            = alt_pipe.to(_A    )
											alt_pipe.set_progress_bar_config(disable=_A    )

											__lowerCAmelCase            = "A painting of a squirrel eating a burger"
											__lowerCAmelCase            = torch.Generator(device=_A    ).manual_seed(0    )
											__lowerCAmelCase            = alt_pipe(
											    [prompt]	,    generator=_A	,    guidance_scale=6.0	,    num_inference_steps=2	,    output_type="np"	,    image=_A	,    )

											__lowerCAmelCase            = output.images

											__lowerCAmelCase            = torch.Generator(device=_A    ).manual_seed(0    )
											__lowerCAmelCase            = alt_pipe(
											    [prompt]	,    generator=_A	,    guidance_scale=6.0	,    num_inference_steps=2	,    output_type="np"	,    image=_A	,    return_dict=_A	,    )[0]

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

											assert image.shape == (1, 3_2, 3_2, 3)
											__lowerCAmelCase            = np.array([0.44_27, 0.37_31, 0.42_49, 0.49_41, 0.45_46, 0.41_48, 0.41_93, 0.46_66, 0.44_99]    )

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






						@unittest.skipIf(torch_device != "cuda"	,    "This test requires a GPU"    )
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.dummy_cond_unet
											__lowerCAmelCase            = PNDMScheduler(skip_prk_steps=_A    )
											__lowerCAmelCase            = self.dummy_vae
											__lowerCAmelCase            = self.dummy_text_encoder
											__lowerCAmelCase            = XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"    )
											__lowerCAmelCase            = 7_7

											__lowerCAmelCase            = self.dummy_image.to(_A    )

											# put models in fp16
											__lowerCAmelCase            = unet.half()
											__lowerCAmelCase            = vae.half()
											__lowerCAmelCase            = bert.half()

											# make sure here that pndm scheduler skips prk
											__lowerCAmelCase            = AltDiffusionImgaImgPipeline(
											    unet=_A	,    scheduler=_A	,    vae=_A	,    text_encoder=_A	,    tokenizer=_A	,    safety_checker=_A	,    feature_extractor=self.dummy_extractor	,    )
											__lowerCAmelCase            = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor	,    do_normalize=_A    )
											__lowerCAmelCase            = alt_pipe.to(_A    )
											alt_pipe.set_progress_bar_config(disable=_A    )

											__lowerCAmelCase            = "A painting of a squirrel eating a burger"
											__lowerCAmelCase            = torch.manual_seed(0    )
											__lowerCAmelCase            = alt_pipe(
											    [prompt]	,    generator=_A	,    num_inference_steps=2	,    output_type="np"	,    image=_A	,    ).images

											assert image.shape == (1, 3_2, 3_2, 3)






						@unittest.skipIf(torch_device != "cuda"	,    "This test requires a GPU"    )
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = load_image(
											    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
											    "/img2img/sketch-mountains-input.jpg"    )
											# resize to resolution that is divisible by 8 but not 16 or 32
											__lowerCAmelCase            = init_image.resize((7_6_0, 5_0_4)    )

											__lowerCAmelCase            = "BAAI/AltDiffusion"
											__lowerCAmelCase            = AltDiffusionImgaImgPipeline.from_pretrained(
											    _A	,    safety_checker=_A	,    )
											pipe.to(_A    )
											pipe.set_progress_bar_config(disable=_A    )
											pipe.enable_attention_slicing()

											__lowerCAmelCase            = "A fantasy landscape, trending on artstation"

											__lowerCAmelCase            = torch.manual_seed(0    )
											__lowerCAmelCase            = pipe(
											    prompt=_A	,    image=_A	,    strength=0.75	,    guidance_scale=7.5	,    generator=_A	,    output_type="np"	,    )
											__lowerCAmelCase            = output.images[0]

											__lowerCAmelCase            = image[2_5_5:2_5_8, 3_8_3:3_8_6, -1]

											assert image.shape == (5_0_4, 7_6_0, 3)
											__lowerCAmelCase            = np.array([0.93_58, 0.93_97, 0.95_99, 0.99_01, 1.00_00, 1.00_00, 0.98_82, 1.00_00, 1.00_00]    )

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






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






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



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






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = load_image(
											    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
											    "/img2img/sketch-mountains-input.jpg"    )
											__lowerCAmelCase            = init_image.resize((7_6_8, 5_1_2)    )
											__lowerCAmelCase            = load_numpy(
											    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy"    )

											__lowerCAmelCase            = "BAAI/AltDiffusion"
											__lowerCAmelCase            = AltDiffusionImgaImgPipeline.from_pretrained(
											    _A	,    safety_checker=_A	,    )
											pipe.to(_A    )
											pipe.set_progress_bar_config(disable=_A    )
											pipe.enable_attention_slicing()

											__lowerCAmelCase            = "A fantasy landscape, trending on artstation"

											__lowerCAmelCase            = torch.manual_seed(0    )
											__lowerCAmelCase            = pipe(
											    prompt=_A	,    image=_A	,    strength=0.75	,    guidance_scale=7.5	,    generator=_A	,    output_type="np"	,    )
											__lowerCAmelCase            = output.images[0]

											assert image.shape == (5_1_2, 7_6_8, 3)
											# img2img is flaky across GPUs even in fp32, so using MAE here
											assert np.abs(expected_image - image    ).max() < 1E-2










import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
					from PIL import Image
else:
					class     a__					:






											@staticmethod
											def        __SCREAMING_SNAKE_CASE(     *_A	,    **_A    ):




																"""simple docstring"""



																pass
@is_pipeline_test
@require_torch
@require_vision
class     a__					(      unittest.TestCase   ):
						_a							:					Any      =					MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = pipeline("visual-question-answering"	,    model="hf-internal-testing/tiny-vilt-random-vqa"    )
											__lowerCAmelCase            = [
											    {
											        "image": Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    ),
											        "question": "How many cats are there?",
											    },
											    {
											        "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
											        "question": "How many cats are there?",
											    },
											]
											return vqa_pipeline, examples






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = vqa_pipeline(_A	,    top_k=1    )
											self.assertEqual(
											    _A	,    [
											        [{"score": ANY(_A    ), "answer": ANY(_A    )}],
											        [{"score": ANY(_A    ), "answer": ANY(_A    )}],
											    ]	,    )






						@require_torch
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = pipeline("visual-question-answering"	,    model="hf-internal-testing/tiny-vilt-random-vqa"    )
											__lowerCAmelCase            = "./tests/fixtures/tests_samples/COCO/000000039769.png"
											__lowerCAmelCase            = "How many cats are there?"

											__lowerCAmelCase            = vqa_pipeline(image=_A	,    question="How many cats are there?"	,    top_k=2    )
											self.assertEqual(
											    _A	,    [{"score": ANY(_A    ), "answer": ANY(_A    )}, {"score": ANY(_A    ), "answer": ANY(_A    )}]    )

											__lowerCAmelCase            = vqa_pipeline({"image": image, "question": question}	,    top_k=2    )
											self.assertEqual(
											    _A	,    [{"score": ANY(_A    ), "answer": ANY(_A    )}, {"score": ANY(_A    ), "answer": ANY(_A    )}]    )






						@slow
						@require_torch
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = pipeline("visual-question-answering"	,    model="dandelin/vilt-b32-finetuned-vqa"    )
											__lowerCAmelCase            = "./tests/fixtures/tests_samples/COCO/000000039769.png"
											__lowerCAmelCase            = "How many cats are there?"

											__lowerCAmelCase            = vqa_pipeline(image=_A	,    question=_A	,    top_k=2    )
											self.assertEqual(
											    nested_simplify(_A	,    decimals=4    )	,    [{"score": 0.87_99, "answer": "2"}, {"score": 0.2_96, "answer": "1"}]    )

											__lowerCAmelCase            = vqa_pipeline({"image": image, "question": question}	,    top_k=2    )
											self.assertEqual(
											    nested_simplify(_A	,    decimals=4    )	,    [{"score": 0.87_99, "answer": "2"}, {"score": 0.2_96, "answer": "1"}]    )

											__lowerCAmelCase            = vqa_pipeline(
											    [{"image": image, "question": question}, {"image": image, "question": question}]	,    top_k=2    )
											self.assertEqual(
											    nested_simplify(_A	,    decimals=4    )	,    [[{"score": 0.87_99, "answer": "2"}, {"score": 0.2_96, "answer": "1"}]] * 2	,    )






						@require_tf
						@unittest.skip("Visual question answering not implemented in TF"    )
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											pass




