





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      lowerCAmelCase							(      unittest.TestCase    ):
 def   A_			(   self						:   Optional[int]			)    ->			str:
     # clean up the VRAM after each test
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()
 @property
 def   A_			(   self						:   List[str]			)    ->			Optional[Any]:
     lowerCamelCase__					:   int      				=	1
     lowerCamelCase__					:   List[Any]      				=	3
     lowerCamelCase__					:   Any      				=	(32, 32)

     lowerCamelCase__					:   Dict      				=	floats_tensor((batch_size, num_channels) + sizes    ,  rng=random.Random(0			)			).to(UpperCAmelCase			)
     return image
 @property
 def   A_			(   self						:   Any			)    ->			Optional[int]:
     torch.manual_seed(0			)
     lowerCamelCase__					:   Any      				=	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   A_			(   self						:   Union[str, Any]			)    ->			Tuple:
     torch.manual_seed(0			)
     lowerCamelCase__					:   Optional[int]      				=	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   A_			(   self						:   Union[str, Any]			)    ->			Tuple:
     torch.manual_seed(0			)
     lowerCamelCase__					:   Optional[Any]      				=	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   A_			(   self						:   Union[str, Any]			)    ->			List[str]:
     def extract(*UpperCAmelCase						:   Union[str, Any]    ,  **UpperCAmelCase						:   List[Any]			):

         class      lowerCAmelCase							:
          def __init__(   self						:   List[Any]			)    ->			List[str]:
              lowerCamelCase__					:   Dict      				=	torch.ones([0]			)







          def   A_			(   self						:   Union[str, Any]    ,  UpperCAmelCase						:   Dict			)    ->			Optional[int]:
              self.pixel_values.to(UpperCAmelCase			)
              return self


         return Out()

     return extract
 def   A_			(   self						:   Optional[int]			)    ->			List[Any]:
     lowerCamelCase__					:   Tuple      				=	'cpu'  # ensure determinism for the device-dependent torch.Generator
     lowerCamelCase__					:   Optional[int]      				=	self.dummy_cond_unet
     lowerCamelCase__					:   List[Any]      				=	PNDMScheduler(skip_prk_steps=UpperCAmelCase			)
     lowerCamelCase__					:   List[Any]      				=	self.dummy_vae
     lowerCamelCase__					:   Any      				=	self.dummy_text_encoder
     lowerCamelCase__					:   str      				=	XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'			)
     lowerCamelCase__					:   List[str]      				=	77

     lowerCamelCase__					:   Optional[Any]      				=	self.dummy_image.to(UpperCAmelCase			)
     lowerCamelCase__					:   Tuple      				=	init_image / 2 + 0.5

     # make sure here that pndm scheduler skips prk
     lowerCamelCase__					:   Optional[Any]      				=	AltDiffusionImgaImgPipeline(
         unet=UpperCAmelCase    ,  scheduler=UpperCAmelCase    ,  vae=UpperCAmelCase    ,  text_encoder=UpperCAmelCase    ,  tokenizer=UpperCAmelCase    ,  safety_checker=UpperCAmelCase    ,  feature_extractor=self.dummy_extractor    ,  )
     lowerCamelCase__					:   List[Any]      				=	VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor    ,  do_normalize=UpperCAmelCase			)
     lowerCamelCase__					:   Optional[int]      				=	alt_pipe.to(UpperCAmelCase			)
     alt_pipe.set_progress_bar_config(disable=UpperCAmelCase			)

     lowerCamelCase__					:   str      				=	'A painting of a squirrel eating a burger'
     lowerCamelCase__					:   Tuple      				=	torch.Generator(device=UpperCAmelCase			).manual_seed(0			)
     lowerCamelCase__					:   Any      				=	alt_pipe(
         [prompt]    ,  generator=UpperCAmelCase    ,  guidance_scale=6.0    ,  num_inference_steps=2    ,  output_type='np'    ,  image=UpperCAmelCase    ,  )

     lowerCamelCase__					:   Optional[Any]      				=	output.images

     lowerCamelCase__					:   str      				=	torch.Generator(device=UpperCAmelCase			).manual_seed(0			)
     lowerCamelCase__					:   List[str]      				=	alt_pipe(
         [prompt]    ,  generator=UpperCAmelCase    ,  guidance_scale=6.0    ,  num_inference_steps=2    ,  output_type='np'    ,  image=UpperCAmelCase    ,  return_dict=UpperCAmelCase    ,  )[0]

     lowerCamelCase__					:   List[Any]      				=	image[0, -3:, -3:, -1]
     lowerCamelCase__					:   Optional[Any]      				=	image_from_tuple[0, -3:, -3:, -1]

     assert image.shape == (1, 32, 32, 3)
     lowerCamelCase__					:   Optional[Any]      				=	np.array([0.4_4_2_7, 0.3_7_3_1, 0.4_2_4_9, 0.4_9_4_1, 0.4_5_4_6, 0.4_1_4_8, 0.4_1_9_3, 0.4_6_6_6, 0.4_4_9_9]			)

     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   A_			(   self						:   Optional[int]			)    ->			str:
     lowerCamelCase__					:   Optional[Any]      				=	self.dummy_cond_unet
     lowerCamelCase__					:   Any      				=	PNDMScheduler(skip_prk_steps=UpperCAmelCase			)
     lowerCamelCase__					:   Any      				=	self.dummy_vae
     lowerCamelCase__					:   Optional[int]      				=	self.dummy_text_encoder
     lowerCamelCase__					:   List[Any]      				=	XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'			)
     lowerCamelCase__					:   int      				=	77

     lowerCamelCase__					:   Dict      				=	self.dummy_image.to(UpperCAmelCase			)

     # put models in fp16
     lowerCamelCase__					:   str      				=	unet.half()
     lowerCamelCase__					:   Optional[int]      				=	vae.half()
     lowerCamelCase__					:   Dict      				=	bert.half()

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

     lowerCamelCase__					:   List[str]      				=	'A painting of a squirrel eating a burger'
     lowerCamelCase__					:   List[Any]      				=	torch.manual_seed(0			)
     lowerCamelCase__					:   str      				=	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   A_			(   self						:   int			)    ->			int:
     lowerCamelCase__					:   Any      				=	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__					:   Optional[int]      				=	init_image.resize((760, 504)			)

     lowerCamelCase__					:   int      				=	'BAAI/AltDiffusion'
     lowerCamelCase__					:   Tuple      				=	AltDiffusionImgaImgPipeline.from_pretrained(
         UpperCAmelCase    ,  safety_checker=UpperCAmelCase    ,  )
     pipe.to(UpperCAmelCase			)
     pipe.set_progress_bar_config(disable=UpperCAmelCase			)
     pipe.enable_attention_slicing()

     lowerCamelCase__					:   Union[str, Any]      				=	'A fantasy landscape, trending on artstation'

     lowerCamelCase__					:   Tuple      				=	torch.manual_seed(0			)
     lowerCamelCase__					:   Optional[int]      				=	pipe(
         prompt=UpperCAmelCase    ,  image=UpperCAmelCase    ,  strength=0.7_5    ,  guidance_scale=7.5    ,  generator=UpperCAmelCase    ,  output_type='np'    ,  )
     lowerCamelCase__					:   Any      				=	output.images[0]

     lowerCamelCase__					:   Optional[Any]      				=	image[255:258, 383:386, -1]

     assert image.shape == (504, 760, 3)
     lowerCamelCase__					:   Optional[int]      				=	np.array([0.9_3_5_8, 0.9_3_9_7, 0.9_5_9_9, 0.9_9_0_1, 1.0_0_0_0, 1.0_0_0_0, 0.9_8_8_2, 1.0_0_0_0, 1.0_0_0_0]			)

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


@slow
@require_torch_gpu
class      lowerCAmelCase							(      unittest.TestCase    ):
 def   A_			(   self						:   List[str]			)    ->			int:
     # clean up the VRAM after each test
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()







 def   A_			(   self						:   str			)    ->			Dict:
     lowerCamelCase__					:   Dict      				=	load_image(
         'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
         '/img2img/sketch-mountains-input.jpg'			)
     lowerCamelCase__					:   List[Any]      				=	init_image.resize((768, 512)			)
     lowerCamelCase__					:   Dict      				=	load_numpy(
         'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy'			)

     lowerCamelCase__					:   Union[str, Any]      				=	'BAAI/AltDiffusion'
     lowerCamelCase__					:   List[Any]      				=	AltDiffusionImgaImgPipeline.from_pretrained(
         UpperCAmelCase    ,  safety_checker=UpperCAmelCase    ,  )
     pipe.to(UpperCAmelCase			)
     pipe.set_progress_bar_config(disable=UpperCAmelCase			)
     pipe.enable_attention_slicing()

     lowerCamelCase__					:   Tuple      				=	'A fantasy landscape, trending on artstation'

     lowerCamelCase__					:   Dict      				=	torch.manual_seed(0			)
     lowerCamelCase__					:   Tuple      				=	pipe(
         prompt=UpperCAmelCase    ,  image=UpperCAmelCase    ,  strength=0.7_5    ,  guidance_scale=7.5    ,  generator=UpperCAmelCase    ,  output_type='np'    ,  )
     lowerCamelCase__					:   int      				=	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












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      lowerCAmelCase							(      unittest.TestCase    ):
 def   A_			(   self						:   str			)    ->			Union[str, Any]:
     lowerCamelCase__					:   Optional[int]      				=	'laion/clap-htsat-unfused'
     lowerCamelCase__					:   List[Any]      				=	tempfile.mkdtemp()
 def   A_			(   self						:   Optional[int]    ,  **UpperCAmelCase						:   int			)    ->			Optional[Any]:
     return RobertaTokenizer.from_pretrained(self.checkpoint    ,  **UpperCAmelCase			)
 def   A_			(   self						:   Union[str, Any]    ,  **UpperCAmelCase						:   Any			)    ->			List[str]:
     return ClapFeatureExtractor.from_pretrained(self.checkpoint    ,  **UpperCAmelCase			)
 def   A_			(   self						:   List[str]			)    ->			Dict:
     shutil.rmtree(self.tmpdirname			)
 def   A_			(   self						:   Optional[Any]			)    ->			Optional[Any]:
     lowerCamelCase__					:   Tuple      				=	self.get_tokenizer()
     lowerCamelCase__					:   Dict      				=	self.get_feature_extractor()

     lowerCamelCase__					:   Dict      				=	ClapProcessor(tokenizer=UpperCAmelCase    ,  feature_extractor=UpperCAmelCase			)

     processor.save_pretrained(self.tmpdirname			)
     lowerCamelCase__					:   List[Any]      				=	ClapProcessor.from_pretrained(self.tmpdirname			)

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

     self.assertEqual(processor.feature_extractor.to_json_string()    ,  feature_extractor.to_json_string()			)
     self.assertIsInstance(processor.feature_extractor    ,  UpperCAmelCase			)
 def   A_			(   self						:   str			)    ->			int:
     lowerCamelCase__					:   Optional[Any]      				=	ClapProcessor(tokenizer=self.get_tokenizer()    ,  feature_extractor=self.get_feature_extractor()			)
     processor.save_pretrained(self.tmpdirname			)

     lowerCamelCase__					:   Dict      				=	self.get_tokenizer(bos_token='(BOS)'    ,  eos_token='(EOS)'			)
     lowerCamelCase__					:   Dict      				=	self.get_feature_extractor(do_normalize=UpperCAmelCase    ,  padding_value=1.0			)

     lowerCamelCase__					:   str      				=	ClapProcessor.from_pretrained(
         self.tmpdirname    ,  bos_token='(BOS)'    ,  eos_token='(EOS)'    ,  do_normalize=UpperCAmelCase    ,  padding_value=1.0			)

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

     self.assertEqual(processor.feature_extractor.to_json_string()    ,  feature_extractor_add_kwargs.to_json_string()			)
     self.assertIsInstance(processor.feature_extractor    ,  UpperCAmelCase			)
 def   A_			(   self						:   List[Any]			)    ->			int:
     lowerCamelCase__					:   Any      				=	self.get_feature_extractor()
     lowerCamelCase__					:   str      				=	self.get_tokenizer()

     lowerCamelCase__					:   Optional[Any]      				=	ClapProcessor(tokenizer=UpperCAmelCase    ,  feature_extractor=UpperCAmelCase			)

     lowerCamelCase__					:   Optional[Any]      				=	floats_list((3, 1000)			)

     lowerCamelCase__					:   Tuple      				=	feature_extractor(UpperCAmelCase    ,  return_tensors='np'			)
     lowerCamelCase__					:   Tuple      				=	processor(audios=UpperCAmelCase    ,  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   A_			(   self						:   List[str]			)    ->			Tuple:
     lowerCamelCase__					:   Dict      				=	self.get_feature_extractor()
     lowerCamelCase__					:   List[Any]      				=	self.get_tokenizer()

     lowerCamelCase__					:   Any      				=	ClapProcessor(tokenizer=UpperCAmelCase    ,  feature_extractor=UpperCAmelCase			)

     lowerCamelCase__					:   Union[str, Any]      				=	'This is a test string'

     lowerCamelCase__					:   Union[str, Any]      				=	processor(text=UpperCAmelCase			)

     lowerCamelCase__					:   Any      				=	tokenizer(UpperCAmelCase			)

     for key in encoded_tok.keys():
         self.assertListEqual(encoded_tok[key]    ,  encoded_processor[key]			)
 def   A_			(   self						:   Optional[int]			)    ->			Tuple:
     lowerCamelCase__					:   Tuple      				=	self.get_feature_extractor()
     lowerCamelCase__					:   List[str]      				=	self.get_tokenizer()

     lowerCamelCase__					:   Optional[int]      				=	ClapProcessor(tokenizer=UpperCAmelCase    ,  feature_extractor=UpperCAmelCase			)

     lowerCamelCase__					:   int      				=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     lowerCamelCase__					:   List[Any]      				=	processor.batch_decode(UpperCAmelCase			)
     lowerCamelCase__					:   Tuple      				=	tokenizer.batch_decode(UpperCAmelCase			)

     self.assertListEqual(UpperCAmelCase    ,  UpperCAmelCase			)







 def   A_			(   self						:   Optional[Any]			)    ->			int:
     lowerCamelCase__					:   str      				=	self.get_feature_extractor()
     lowerCamelCase__					:   str      				=	self.get_tokenizer()

     lowerCamelCase__					:   Dict      				=	ClapProcessor(tokenizer=UpperCAmelCase    ,  feature_extractor=UpperCAmelCase			)

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





