"""simple docstring"""


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 	snake_case__	(     self  ):
        '''simple docstring'''



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


   @property
   def 	snake_case__	(     self  ):
        '''simple docstring'''



        lowercase__				 =							1
        lowercase__				 =							3
        lowercase__				 =							(32, 32)

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


   @property
   def 	snake_case__	(     self  ):
        '''simple docstring'''



        torch.manual_seed(0  )
        lowercase__				 =							UNetaDConditionModel(
            block_out_channels=(32, 64),				layers_per_block=2,				sample_size=32,				in_channels=4,				out_channels=4,				down_block_types=("DownBlock2D", "CrossAttnDownBlock2D"),				up_block_types=("CrossAttnUpBlock2D", "UpBlock2D"),				cross_attention_dim=32,				)
        return model


   @property
   def 	snake_case__	(     self  ):
        '''simple docstring'''



        torch.manual_seed(0  )
        lowercase__				 =							AutoencoderKL(
            block_out_channels=[32, 64],				in_channels=3,				out_channels=3,				down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"],				up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"],				latent_channels=4,				)
        return model


   @property
   def 	snake_case__	(     self  ):
        '''simple docstring'''



        torch.manual_seed(0  )
        lowercase__				 =							RobertaSeriesConfig(
            hidden_size=32,				project_dim=32,				intermediate_size=37,				layer_norm_eps=1E-05,				num_attention_heads=4,				num_hidden_layers=5,				pad_token_id=1,				vocab_size=5006,				)
        return RobertaSeriesModelWithTransformation(_UpperCAmelCase  )


   @property
   def 	snake_case__	(     self  ):
        '''simple docstring'''



        def extract(*_UpperCAmelCase,				**_UpperCAmelCase  ):






             class    a__    :


                def __init__(     self  ):
                     '''simple docstring'''



                     lowercase__				 =							torch.ones([0]  )


                def 	snake_case__	(     self,				_UpperCAmelCase  ):
                     '''simple docstring'''



                     self.pixel_values.to(_UpperCAmelCase  )
                     return self



             return Out()

        return extract


   def 	snake_case__	(     self  ):
        '''simple docstring'''



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

        lowercase__				 =							self.dummy_image.to(_UpperCAmelCase  )
        lowercase__				 =							init_image / 2 + 0.5

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

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

        lowercase__				 =							output.images

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

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

        assert image.shape == (1, 32, 32, 3)
        lowercase__				 =							np.array([0.4_427, 0.3_731, 0.4_249, 0.4_941, 0.4_546, 0.4_148, 0.4_193, 0.4_666, 0.4_499]  )

        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 	snake_case__	(     self  ):
        '''simple docstring'''



        lowercase__				 =							self.dummy_cond_unet
        lowercase__				 =							PNDMScheduler(skip_prk_steps=_UpperCAmelCase  )
        lowercase__				 =							self.dummy_vae
        lowercase__				 =							self.dummy_text_encoder
        lowercase__				 =							XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"  )
        lowercase__				 =							77

        lowercase__				 =							self.dummy_image.to(_UpperCAmelCase  )

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

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

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

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


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



        lowercase__				 =							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
        lowercase__				 =							init_image.resize((760, 504)  )

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

        lowercase__				 =							"A fantasy landscape, trending on artstation"

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

        lowercase__				 =							image[255:258, 383:386, -1]

        assert image.shape == (504, 760, 3)
        lowercase__				 =							np.array([0.9_358, 0.9_397, 0.9_599, 0.9_901, 1.0_000, 1.0_000, 0.9_882, 1.0_000, 1.0_000]  )

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






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


   def 	snake_case__	(     self  ):
        '''simple docstring'''



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


   def 	snake_case__	(     self  ):
        '''simple docstring'''



        lowercase__				 =							load_image(
            "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
            "/img2img/sketch-mountains-input.jpg"  )
        lowercase__				 =							init_image.resize((768, 512)  )
        lowercase__				 =							load_numpy(
            "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy"  )

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

        lowercase__				 =							"A fantasy landscape, trending on artstation"

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

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





"""simple docstring"""


from ...utils import is_note_seq_available, is_transformers_available, is_torch_available
from ...utils import OptionalDependencyNotAvailable


try:
 if not (is_transformers_available() and is_torch_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
 from .notes_encoder import SpectrogramNotesEncoder
 from .continous_encoder import SpectrogramContEncoder
 from .pipeline_spectrogram_diffusion import (
     SpectrogramContEncoder,
     SpectrogramDiffusionPipeline,
     TaFilmDecoder,
 )

try:
 if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from ...utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
 from .midi_utils import MidiProcessor




