

'''simple docstring'''

import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


SCREAMING_SNAKE_CASE							:				int     	=			random.Random()




def 							_UpperCamelCase				(   lowerCAmelCase__:					Optional[int]		,lowerCAmelCase__:					Optional[Any]=1.0		,lowerCAmelCase__:					Any=None		,lowerCAmelCase__:					Any=None   )      -> Any:
							if rng is None:
														SCREAMING_SNAKE_CASE_				=				global_rng

							SCREAMING_SNAKE_CASE_				=				[]
							for batch_idx in range(shape[0]   ):
														values.append([]   )
														for _ in range(shape[1]   ):
																					values[-1].append(rng.random() * scale   )

							return values






class    snake_case     (      unittest.TestCase		):






				"""simple docstring"""



				def __init__(    self,     _lowercase,     _lowercase=7,     _lowercase=400,     _lowercase=2000,     _lowercase=1,     _lowercase=0.0,     _lowercase=16000,     _lowercase=True,     _lowercase=True,     )     ->							Optional[Any]:
											SCREAMING_SNAKE_CASE_				=				parent
											SCREAMING_SNAKE_CASE_				=				batch_size
											SCREAMING_SNAKE_CASE_				=				min_seq_length
											SCREAMING_SNAKE_CASE_				=				max_seq_length
											SCREAMING_SNAKE_CASE_				=				(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
											SCREAMING_SNAKE_CASE_				=				feature_size
											SCREAMING_SNAKE_CASE_				=				padding_value
											SCREAMING_SNAKE_CASE_				=				sampling_rate
											SCREAMING_SNAKE_CASE_				=				return_attention_mask
											SCREAMING_SNAKE_CASE_				=				do_normalize



				def   a__					(    self       )     ->							List[str]:
											return {
											    "feature_size": self.feature_size,
											    "padding_value": self.padding_value,
											    "sampling_rate": self.sampling_rate,
											    "return_attention_mask": self.return_attention_mask,
											    "do_normalize": self.do_normalize,
											}





				def   a__					(    self,     _lowercase=False,     _lowercase=False       )     ->							List[str]:
											def _flatten(_lowercase       ):
																		return list(itertools.chain(*_lowercase       )       )

											if equal_length:
																		SCREAMING_SNAKE_CASE_				=				floats_list((self.batch_size, self.max_seq_length)       )
											else:
																		# make sure that inputs increase in size
																		SCREAMING_SNAKE_CASE_				=				[
																		    _flatten(floats_list((x, self.feature_size)       )       )
																		    for x in range(self.min_seq_length,     self.max_seq_length,     self.seq_length_diff       )
																		]

											if numpify:
																		SCREAMING_SNAKE_CASE_				=				[np.asarray(_lowercase       ) for x in speech_inputs]

											return speech_inputs






class    snake_case     (      lowercase_,						unittest.TestCase		):






				"""simple docstring"""
				_a    	=     WavaVecaFeatureExtractor



				def   a__					(    self       )     ->							Any:
											SCREAMING_SNAKE_CASE_				=				WavaVecaFeatureExtractionTester(self       )



				def   a__					(    self,     _lowercase       )     ->							Optional[Any]:
											self.assertTrue(np.all(np.mean(_lowercase,     axis=0       ) < 1E-3       )       )
											self.assertTrue(np.all(np.abs(np.var(_lowercase,     axis=0       ) - 1       ) < 1E-3       )       )



				def   a__					(    self       )     ->							Any:
											# Tests that all call wrap to encode_plus and batch_encode_plus
											SCREAMING_SNAKE_CASE_				=				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
											# create three inputs of length 800, 1000, and 1200
											SCREAMING_SNAKE_CASE_				=				[floats_list((1, x)       )[0] for x in range(800,     1400,     200       )]
											SCREAMING_SNAKE_CASE_				=				[np.asarray(_lowercase       ) for speech_input in speech_inputs]

											# Test not batched input
											SCREAMING_SNAKE_CASE_				=				feat_extract(speech_inputs[0],     return_tensors='np'       ).input_values
											SCREAMING_SNAKE_CASE_				=				feat_extract(np_speech_inputs[0],     return_tensors='np'       ).input_values
											self.assertTrue(np.allclose(_lowercase,     _lowercase,     atol=1E-3       )       )

											# Test batched
											SCREAMING_SNAKE_CASE_				=				feat_extract(_lowercase,     return_tensors='np'       ).input_values
											SCREAMING_SNAKE_CASE_				=				feat_extract(_lowercase,     return_tensors='np'       ).input_values
											for enc_seq_a, enc_seq_a in zip(_lowercase,     _lowercase       ):
																		self.assertTrue(np.allclose(_lowercase,     _lowercase,     atol=1E-3       )       )

											# Test 2-D numpy arrays are batched.
											SCREAMING_SNAKE_CASE_				=				[floats_list((1, x)       )[0] for x in (800, 800, 800)]
											SCREAMING_SNAKE_CASE_				=				np.asarray(_lowercase       )
											SCREAMING_SNAKE_CASE_				=				feat_extract(_lowercase,     return_tensors='np'       ).input_values
											SCREAMING_SNAKE_CASE_				=				feat_extract(_lowercase,     return_tensors='np'       ).input_values
											for enc_seq_a, enc_seq_a in zip(_lowercase,     _lowercase       ):
																		self.assertTrue(np.allclose(_lowercase,     _lowercase,     atol=1E-3       )       )



				def   a__					(    self       )     ->							Union[str, Any]:
											SCREAMING_SNAKE_CASE_				=				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
											SCREAMING_SNAKE_CASE_				=				[floats_list((1, x)       )[0] for x in range(800,     1400,     200       )]

											SCREAMING_SNAKE_CASE_				=				['longest', 'max_length', 'do_not_pad']
											SCREAMING_SNAKE_CASE_				=				[None, 1600, None]
											for max_length, padding in zip(_lowercase,     _lowercase       ):
																		SCREAMING_SNAKE_CASE_				=				feat_extract(_lowercase,     padding=_lowercase,     max_length=_lowercase,     return_tensors='np'       )
																		SCREAMING_SNAKE_CASE_				=				processed.input_values

																		self._check_zero_mean_unit_variance(input_values[0][:800]       )
																		self.assertTrue(input_values[0][800:].sum() < 1E-6       )
																		self._check_zero_mean_unit_variance(input_values[1][:1000]       )
																		self.assertTrue(input_values[0][1000:].sum() < 1E-6       )
																		self._check_zero_mean_unit_variance(input_values[2][:1200]       )



				def   a__					(    self       )     ->							Union[str, Any]:
											SCREAMING_SNAKE_CASE_				=				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
											SCREAMING_SNAKE_CASE_				=				range(800,     1400,     200       )
											SCREAMING_SNAKE_CASE_				=				[floats_list((1, x)       )[0] for x in lengths]

											SCREAMING_SNAKE_CASE_				=				['longest', 'max_length', 'do_not_pad']
											SCREAMING_SNAKE_CASE_				=				[None, 1600, None]

											for max_length, padding in zip(_lowercase,     _lowercase       ):
																		SCREAMING_SNAKE_CASE_				=				feat_extract(_lowercase,     max_length=_lowercase,     padding=_lowercase       )
																		SCREAMING_SNAKE_CASE_				=				processed.input_values

																		self._check_zero_mean_unit_variance(input_values[0][:800]       )
																		self._check_zero_mean_unit_variance(input_values[1][:1000]       )
																		self._check_zero_mean_unit_variance(input_values[2][:1200]       )



				def   a__					(    self       )     ->							List[str]:
											SCREAMING_SNAKE_CASE_				=				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
											SCREAMING_SNAKE_CASE_				=				[floats_list((1, x)       )[0] for x in range(800,     1400,     200       )]
											SCREAMING_SNAKE_CASE_				=				feat_extract(
											    _lowercase,     truncation=_lowercase,     max_length=1000,     padding='max_length',     return_tensors='np'       )
											SCREAMING_SNAKE_CASE_				=				processed.input_values

											self._check_zero_mean_unit_variance(input_values[0, :800]       )
											self._check_zero_mean_unit_variance(input_values[1]       )
											self._check_zero_mean_unit_variance(input_values[2]       )



				def   a__					(    self       )     ->							Union[str, Any]:
											SCREAMING_SNAKE_CASE_				=				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
											SCREAMING_SNAKE_CASE_				=				[floats_list((1, x)       )[0] for x in range(800,     1400,     200       )]
											SCREAMING_SNAKE_CASE_				=				feat_extract(
											    _lowercase,     truncation=_lowercase,     max_length=1000,     padding='longest',     return_tensors='np'       )
											SCREAMING_SNAKE_CASE_				=				processed.input_values

											self._check_zero_mean_unit_variance(input_values[0, :800]       )
											self._check_zero_mean_unit_variance(input_values[1, :1000]       )
											self._check_zero_mean_unit_variance(input_values[2]       )

											# make sure that if max_length < longest -> then pad to max_length
											self.assertTrue(input_values.shape == (3, 1000)       )

											SCREAMING_SNAKE_CASE_				=				[floats_list((1, x)       )[0] for x in range(800,     1400,     200       )]
											SCREAMING_SNAKE_CASE_				=				feat_extract(
											    _lowercase,     truncation=_lowercase,     max_length=2000,     padding='longest',     return_tensors='np'       )
											SCREAMING_SNAKE_CASE_				=				processed.input_values

											self._check_zero_mean_unit_variance(input_values[0, :800]       )
											self._check_zero_mean_unit_variance(input_values[1, :1000]       )
											self._check_zero_mean_unit_variance(input_values[2]       )

											# make sure that if max_length > longest -> then pad to longest
											self.assertTrue(input_values.shape == (3, 1200)       )



				@require_torch
				def   a__					(    self       )     ->							Tuple:
											import torch

											SCREAMING_SNAKE_CASE_				=				self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
											SCREAMING_SNAKE_CASE_				=				np.random.rand(100       ).astype(np.floataa       )
											SCREAMING_SNAKE_CASE_				=				np_speech_inputs.tolist()

											for inputs in [py_speech_inputs, np_speech_inputs]:
																		SCREAMING_SNAKE_CASE_				=				feature_extractor.pad([{'input_values': inputs}],     return_tensors='np'       )
																		self.assertTrue(np_processed.input_values.dtype == np.floataa       )
																		SCREAMING_SNAKE_CASE_				=				feature_extractor.pad([{'input_values': inputs}],     return_tensors='pt'       )
																		self.assertTrue(pt_processed.input_values.dtype == torch.floataa       )





				@slow
				@require_torch
				def   a__					(    self       )     ->							Optional[int]:
											# this test makes sure that models that are using
											# group norm don't have their feature extractor return the
											# attention_mask
											for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
																		SCREAMING_SNAKE_CASE_				=				WavaVecaConfig.from_pretrained(_lowercase       )
																		SCREAMING_SNAKE_CASE_				=				WavaVecaFeatureExtractor.from_pretrained(_lowercase       )

																		# only "layer" feature extraction norm should make use of
																		# attention_mask
																		self.assertEqual(feat_extract.return_attention_mask,     config.feat_extract_norm == 'layer'       )








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






				"""simple docstring"""



				def   a__					(    self       )     ->							int:
											# clean up the VRAM after each test
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()



				@property
				def   a__					(    self       )     ->							Optional[int]:
											SCREAMING_SNAKE_CASE_				=				1
											SCREAMING_SNAKE_CASE_				=				3
											SCREAMING_SNAKE_CASE_				=				(32, 32)

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



				@property
				def   a__					(    self       )     ->							Any:
											torch.manual_seed(0       )
											SCREAMING_SNAKE_CASE_				=				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       )     ->							Tuple:
											torch.manual_seed(0       )
											SCREAMING_SNAKE_CASE_				=				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       )     ->							Tuple:
											torch.manual_seed(0       )
											SCREAMING_SNAKE_CASE_				=				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(_lowercase       )



				@property
				def   a__					(    self       )     ->							List[str]:
											def extract(*_lowercase,     **_lowercase       ):






																		class    snake_case     :






																						"""simple docstring"""



																						def __init__(    self       )     ->							Dict:
																													SCREAMING_SNAKE_CASE_				=				torch.ones([0]       )





																						def   a__					(    self,     _lowercase       )     ->							List[str]:
																													self.pixel_values.to(_lowercase       )
																													return self


																		return Out()

											return extract



				def   a__					(    self       )     ->							Tuple:
											SCREAMING_SNAKE_CASE_				=				'cpu'  # ensure determinism for the device-dependent torch.Generator
											SCREAMING_SNAKE_CASE_				=				self.dummy_cond_unet
											SCREAMING_SNAKE_CASE_				=				PNDMScheduler(skip_prk_steps=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				self.dummy_vae
											SCREAMING_SNAKE_CASE_				=				self.dummy_text_encoder
											SCREAMING_SNAKE_CASE_				=				XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'       )
											SCREAMING_SNAKE_CASE_				=				77

											SCREAMING_SNAKE_CASE_				=				self.dummy_image.to(_lowercase       )
											SCREAMING_SNAKE_CASE_				=				init_image / 2 + 0.5

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

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

											SCREAMING_SNAKE_CASE_				=				output.images

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

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

											assert image.shape == (1, 32, 32, 3)
											SCREAMING_SNAKE_CASE_				=				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   a__					(    self       )     ->							str:
											SCREAMING_SNAKE_CASE_				=				self.dummy_cond_unet
											SCREAMING_SNAKE_CASE_				=				PNDMScheduler(skip_prk_steps=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				self.dummy_vae
											SCREAMING_SNAKE_CASE_				=				self.dummy_text_encoder
											SCREAMING_SNAKE_CASE_				=				XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'       )
											SCREAMING_SNAKE_CASE_				=				77

											SCREAMING_SNAKE_CASE_				=				self.dummy_image.to(_lowercase       )

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

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

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

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





				@unittest.skipIf(torch_device != 'cuda',     'This test requires a GPU'       )
				def   a__					(    self       )     ->							Optional[Any]:
											SCREAMING_SNAKE_CASE_				=				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
											SCREAMING_SNAKE_CASE_				=				init_image.resize((760, 504)       )

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

											SCREAMING_SNAKE_CASE_				=				'A fantasy landscape, trending on artstation'

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

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

											assert image.shape == (504, 760, 3)
											SCREAMING_SNAKE_CASE_				=				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    snake_case     (      unittest.TestCase		):






				"""simple docstring"""



				def   a__					(    self       )     ->							List[Any]:
											# clean up the VRAM after each test
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()





				def   a__					(    self       )     ->							Optional[int]:
											SCREAMING_SNAKE_CASE_				=				load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
											    '/img2img/sketch-mountains-input.jpg'       )
											SCREAMING_SNAKE_CASE_				=				init_image.resize((768, 512)       )
											SCREAMING_SNAKE_CASE_				=				load_numpy(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy'       )

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

											SCREAMING_SNAKE_CASE_				=				'A fantasy landscape, trending on artstation'

											SCREAMING_SNAKE_CASE_				=				torch.manual_seed(0       )
											SCREAMING_SNAKE_CASE_				=				pipe(
											    prompt=_lowercase,     image=_lowercase,     strength=0.75,     guidance_scale=7.5,     generator=_lowercase,     output_type='np',     )
											SCREAMING_SNAKE_CASE_				=				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





