





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 UpperCAmelCase_				( unittest.TestCase ):


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





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


  @property
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       1
       A_          =       3
       A_          =       (3_2, 3_2)

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


  @property
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       torch.manual_seed(0 )
       A_          =       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 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       torch.manual_seed(0 )
       A_          =       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 						__UpperCAmelCase     (							self ):



       """simple docstring"""





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


  @property
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       def extract(*__snake_case ,**__snake_case ):





            class UpperCAmelCase_				:


              def __init__(							self ):



                   """simple docstring"""





                   A_          =       torch.ones([0] )




              def 						__UpperCAmelCase     (							self ,__snake_case ):



                   """simple docstring"""





                   self.pixel_values.to(__snake_case )
                   return self




            return Out()

       return extract


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





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

       A_          =       self.dummy_image.to(__snake_case )
       A_          =       init_image / 2 + 0.5

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

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

       A_          =       output.images

       A_          =       torch.Generator(device=__snake_case ).manual_seed(0 )
       A_          =       alt_pipe(
           [prompt] ,generator=__snake_case ,guidance_scale=6.0 ,num_inference_steps=2 ,output_type='''np''' ,image=__snake_case ,return_dict=__snake_case ,)[0]

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

       assert image.shape == (1, 3_2, 3_2, 3)
       A_          =       np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] )

       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 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.dummy_cond_unet
       A_          =       PNDMScheduler(skip_prk_steps=__snake_case )
       A_          =       self.dummy_vae
       A_          =       self.dummy_text_encoder
       A_          =       XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta''' )
       A_          =       7_7

       A_          =       self.dummy_image.to(__snake_case )

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

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

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

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




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



       """simple docstring"""





       A_          =       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
       A_          =       init_image.resize((7_6_0, 5_0_4) )

       A_          =       '''BAAI/AltDiffusion'''
       A_          =       AltDiffusionImgaImgPipeline.from_pretrained(
           __snake_case ,safety_checker=__snake_case ,)
       pipe.to(__snake_case )
       pipe.set_progress_bar_config(disable=__snake_case )
       pipe.enable_attention_slicing()

       A_          =       '''A fantasy landscape, trending on artstation'''

       A_          =       torch.manual_seed(0 )
       A_          =       pipe(
           prompt=__snake_case ,image=__snake_case ,strength=0.75 ,guidance_scale=7.5 ,generator=__snake_case ,output_type='''np''' ,)
       A_          =       output.images[0]

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

       assert image.shape == (5_0_4, 7_6_0, 3)
       A_          =       np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] )

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





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


  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'''
           '''/img2img/sketch-mountains-input.jpg''' )
       A_          =       init_image.resize((7_6_8, 5_1_2) )
       A_          =       load_numpy(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy''' )

       A_          =       '''BAAI/AltDiffusion'''
       A_          =       AltDiffusionImgaImgPipeline.from_pretrained(
           __snake_case ,safety_checker=__snake_case ,)
       pipe.to(__snake_case )
       pipe.set_progress_bar_config(disable=__snake_case )
       pipe.enable_attention_slicing()

       A_          =       '''A fantasy landscape, trending on artstation'''

       A_          =       torch.manual_seed(0 )
       A_          =       pipe(
           prompt=__snake_case ,image=__snake_case ,strength=0.75 ,guidance_scale=7.5 ,generator=__snake_case ,output_type='''np''' ,)
       A_          =       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 shutil
import tempfile
import unittest

from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torchaudio

from .test_feature_extraction_clap import floats_list





@require_torchaudio
@require_sentencepiece
class UpperCAmelCase_				( unittest.TestCase ):


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       '''laion/clap-htsat-unfused'''
       A_          =       tempfile.mkdtemp()


  def 						__UpperCAmelCase     (							self ,**__snake_case ):



       """simple docstring"""





       return RobertaTokenizer.from_pretrained(self.checkpoint ,**__snake_case )


  def 						__UpperCAmelCase     (							self ,**__snake_case ):



       """simple docstring"""





       return ClapFeatureExtractor.from_pretrained(self.checkpoint ,**__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       shutil.rmtree(self.tmpdirname )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_tokenizer()
       A_          =       self.get_feature_extractor()

       A_          =       ClapProcessor(tokenizer=__snake_case ,feature_extractor=__snake_case )

       processor.save_pretrained(self.tmpdirname )
       A_          =       ClapProcessor.from_pretrained(self.tmpdirname )

       self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer.get_vocab() )
       self.assertIsInstance(processor.tokenizer ,__snake_case )

       self.assertEqual(processor.feature_extractor.to_json_string() ,feature_extractor.to_json_string() )
       self.assertIsInstance(processor.feature_extractor ,__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       ClapProcessor(tokenizer=self.get_tokenizer() ,feature_extractor=self.get_feature_extractor() )
       processor.save_pretrained(self.tmpdirname )

       A_          =       self.get_tokenizer(bos_token='''(BOS)''' ,eos_token='''(EOS)''' )
       A_          =       self.get_feature_extractor(do_normalize=__snake_case ,padding_value=1.0 )

       A_          =       ClapProcessor.from_pretrained(
           self.tmpdirname ,bos_token='''(BOS)''' ,eos_token='''(EOS)''' ,do_normalize=__snake_case ,padding_value=1.0 )

       self.assertEqual(processor.tokenizer.get_vocab() ,tokenizer_add_kwargs.get_vocab() )
       self.assertIsInstance(processor.tokenizer ,__snake_case )

       self.assertEqual(processor.feature_extractor.to_json_string() ,feature_extractor_add_kwargs.to_json_string() )
       self.assertIsInstance(processor.feature_extractor ,__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_feature_extractor()
       A_          =       self.get_tokenizer()

       A_          =       ClapProcessor(tokenizer=__snake_case ,feature_extractor=__snake_case )

       A_          =       floats_list((3, 1_0_0_0) )

       A_          =       feature_extractor(__snake_case ,return_tensors='''np''' )
       A_          =       processor(audios=__snake_case ,return_tensors='''np''' )

       for key in input_feat_extract.keys():
            self.assertAlmostEqual(input_feat_extract[key].sum() ,input_processor[key].sum() ,delta=1E-2 )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_feature_extractor()
       A_          =       self.get_tokenizer()

       A_          =       ClapProcessor(tokenizer=__snake_case ,feature_extractor=__snake_case )

       A_          =       '''This is a test string'''

       A_          =       processor(text=__snake_case )

       A_          =       tokenizer(__snake_case )

       for key in encoded_tok.keys():
            self.assertListEqual(encoded_tok[key] ,encoded_processor[key] )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_feature_extractor()
       A_          =       self.get_tokenizer()

       A_          =       ClapProcessor(tokenizer=__snake_case ,feature_extractor=__snake_case )

       A_          =       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       A_          =       processor.batch_decode(__snake_case )
       A_          =       tokenizer.batch_decode(__snake_case )

       self.assertListEqual(__snake_case ,__snake_case )




  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_feature_extractor()
       A_          =       self.get_tokenizer()

       A_          =       ClapProcessor(tokenizer=__snake_case ,feature_extractor=__snake_case )

       self.assertListEqual(
           processor.model_input_names[2:] ,feature_extractor.model_input_names ,msg='''`processor` and `feature_extractor` model input names do not match''' ,)



