






import gc
import random
import unittest

import torch

from diffusers import (
    IFImgaImgPipeline,
    IFImgaImgSuperResolutionPipeline,
    IFInpaintingPipeline,
    IFInpaintingSuperResolutionPipeline,
    IFPipeline,
    IFSuperResolutionPipeline,
)
from diffusers.models.attention_processor import AttnAddedKVProcessor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference
from . import IFPipelineTesterMixin





@skip_mps
class       lowercase__		(     UpperCamelCase_			,							UpperCamelCase_			,							unittest.TestCase):
						UpperCamelCase_  =	IFPipeline
						UpperCamelCase_  =	TEXT_TO_IMAGE_PARAMS - {"""width""", """height""", """latents"""}
						UpperCamelCase_  =	TEXT_TO_IMAGE_BATCH_PARAMS
						UpperCamelCase_  =	PipelineTesterMixin.required_optional_params - {"""latents"""}


						def  __A ( self			:       Tuple		):





												'''simple docstring'''





												return self._get_dummy_components()


						def  __A ( self			:       int					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       int=0		):





												'''simple docstring'''





												if str(UpperCamelCase__		).startswith('''mps'''		):
																		SCREAMING_SNAKE_CASE							:					Tuple           =							torch.manual_seed(UpperCamelCase__		)
												else:
																		SCREAMING_SNAKE_CASE							:					Dict           =							torch.Generator(device=UpperCamelCase__		).manual_seed(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					int           =							{
												    '''prompt''': '''A painting of a squirrel eating a burger''',
												    '''generator''': generator,
												    '''num_inference_steps''': 2,
												    '''output_type''': '''numpy''',
												}

												return inputs


						def  __A ( self			:       List[str]		):





												'''simple docstring'''





												self._test_save_load_optional_components()


						@unittest.skipIf(torch_device != '''cuda'''					,	reason='''float16 requires CUDA'''		)
						def  __A ( self			:       Any		):





												'''simple docstring'''





												super().test_save_load_floataa(expected_max_diff=1E-1		)


						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												self._test_attention_slicing_forward_pass(expected_max_diff=1E-2		)


						def  __A ( self			:       List[Any]		):





												'''simple docstring'''





												self._test_save_load_local()


						def  __A ( self			:       List[str]		):





												'''simple docstring'''





												self._test_inference_batch_single_identical(
												    expected_max_diff=1E-2					,	)



						@unittest.skipIf(
						    torch_device != '''cuda''' or not is_xformers_available()					,	reason='''XFormers attention is only available with CUDA and `xformers` installed'''					,	)
						def  __A ( self			:       Tuple		):





												'''simple docstring'''





												self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3		)






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


						def  __A ( self			:       Optional[Any]		):





												'''simple docstring'''





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


						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0'''					,	variant='''fp16'''					,	torch_dtype=torch.floataa		)

												SCREAMING_SNAKE_CASE							:					str           =							IFSuperResolutionPipeline.from_pretrained(
												    '''DeepFloyd/IF-II-L-v1.0'''					,	variant='''fp16'''					,	torch_dtype=torch.floataa					,	text_encoder=UpperCamelCase__					,	tokenizer=UpperCamelCase__		)

												# pre compute text embeddings and remove T5 to save memory

												pipe_a.text_encoder.to('''cuda'''		)

												SCREAMING_SNAKE_CASE							:					Dict           =							pipe_a.encode_prompt('''anime turtle'''					,	device='''cuda'''		)

												del pipe_a.tokenizer
												del pipe_a.text_encoder
												gc.collect()

												SCREAMING_SNAKE_CASE							:					Tuple           =							None
												SCREAMING_SNAKE_CASE							:					str           =							None

												pipe_a.enable_model_cpu_offload()
												pipe_a.enable_model_cpu_offload()

												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()		)
												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()		)

												self._test_if(UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__		)

												pipe_a.remove_all_hooks()
												pipe_a.remove_all_hooks()

												# img2img

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							IFImgaImgPipeline(**pipe_a.components		)
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							IFImgaImgSuperResolutionPipeline(**pipe_a.components		)

												pipe_a.enable_model_cpu_offload()
												pipe_a.enable_model_cpu_offload()

												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()		)
												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()		)

												self._test_if_imgaimg(UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__		)

												pipe_a.remove_all_hooks()
												pipe_a.remove_all_hooks()

												# inpainting

												SCREAMING_SNAKE_CASE							:					Tuple           =							IFInpaintingPipeline(**pipe_a.components		)
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							IFInpaintingSuperResolutionPipeline(**pipe_a.components		)

												pipe_a.enable_model_cpu_offload()
												pipe_a.enable_model_cpu_offload()

												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()		)
												pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()		)

												self._test_if_inpainting(UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__		)


						def  __A ( self			:       Tuple					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Dict		):





												'''simple docstring'''





												_start_torch_memory_measurement()

												SCREAMING_SNAKE_CASE							:					List[str]           =							torch.Generator(device='''cpu'''		).manual_seed(0		)
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							pipe_a(
												    prompt_embeds=UpperCamelCase__					,	negative_prompt_embeds=UpperCamelCase__					,	num_inference_steps=2					,	generator=UpperCamelCase__					,	output_type='''np'''					,	)

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							output.images[0]

												assert image.shape == (64, 64, 3)

												SCREAMING_SNAKE_CASE							:					Tuple           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 13 * 10**9

												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy'''		)
												assert_mean_pixel_difference(UpperCamelCase__					,	UpperCamelCase__		)

												# pipeline 2

												_start_torch_memory_measurement()

												SCREAMING_SNAKE_CASE							:					Tuple           =							torch.Generator(device='''cpu'''		).manual_seed(0		)

												SCREAMING_SNAKE_CASE							:					int           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Any           =							pipe_a(
												    prompt_embeds=UpperCamelCase__					,	negative_prompt_embeds=UpperCamelCase__					,	image=UpperCamelCase__					,	generator=UpperCamelCase__					,	num_inference_steps=2					,	output_type='''np'''					,	)

												SCREAMING_SNAKE_CASE							:					Tuple           =							output.images[0]

												assert image.shape == (256, 256, 3)

												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 10**9

												SCREAMING_SNAKE_CASE							:					int           =							load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy'''		)
												assert_mean_pixel_difference(UpperCamelCase__					,	UpperCamelCase__		)


						def  __A ( self			:       Optional[int]					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       str		):





												'''simple docstring'''





												_start_torch_memory_measurement()

												SCREAMING_SNAKE_CASE							:					Tuple           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Any           =							torch.Generator(device='''cpu'''		).manual_seed(0		)

												SCREAMING_SNAKE_CASE							:					List[Any]           =							pipe_a(
												    prompt_embeds=UpperCamelCase__					,	negative_prompt_embeds=UpperCamelCase__					,	image=UpperCamelCase__					,	num_inference_steps=2					,	generator=UpperCamelCase__					,	output_type='''np'''					,	)

												SCREAMING_SNAKE_CASE							:					int           =							output.images[0]

												assert image.shape == (64, 64, 3)

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 10 * 10**9

												SCREAMING_SNAKE_CASE							:					Tuple           =							load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy'''		)
												assert_mean_pixel_difference(UpperCamelCase__					,	UpperCamelCase__		)

												# pipeline 2

												_start_torch_memory_measurement()

												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							torch.Generator(device='''cpu'''		).manual_seed(0		)

												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							floats_tensor((1, 3, 256, 256)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					List[Any]           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					str           =							pipe_a(
												    prompt_embeds=UpperCamelCase__					,	negative_prompt_embeds=UpperCamelCase__					,	image=UpperCamelCase__					,	original_image=UpperCamelCase__					,	generator=UpperCamelCase__					,	num_inference_steps=2					,	output_type='''np'''					,	)

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							output.images[0]

												assert image.shape == (256, 256, 3)

												SCREAMING_SNAKE_CASE							:					List[Any]           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 10**9

												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy'''		)
												assert_mean_pixel_difference(UpperCamelCase__					,	UpperCamelCase__		)



						def  __A ( self			:       List[str]					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Optional[Any]		):





												'''simple docstring'''





												_start_torch_memory_measurement()

												SCREAMING_SNAKE_CASE							:					str           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(1		)		).to(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							torch.Generator(device='''cpu'''		).manual_seed(0		)
												SCREAMING_SNAKE_CASE							:					int           =							pipe_a(
												    prompt_embeds=UpperCamelCase__					,	negative_prompt_embeds=UpperCamelCase__					,	image=UpperCamelCase__					,	mask_image=UpperCamelCase__					,	num_inference_steps=2					,	generator=UpperCamelCase__					,	output_type='''np'''					,	)

												SCREAMING_SNAKE_CASE							:					Any           =							output.images[0]

												assert image.shape == (64, 64, 3)

												SCREAMING_SNAKE_CASE							:					Any           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 10 * 10**9

												SCREAMING_SNAKE_CASE							:					List[Any]           =							load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy'''		)
												assert_mean_pixel_difference(UpperCamelCase__					,	UpperCamelCase__		)

												# pipeline 2

												_start_torch_memory_measurement()

												SCREAMING_SNAKE_CASE							:					str           =							torch.Generator(device='''cpu'''		).manual_seed(0		)

												SCREAMING_SNAKE_CASE							:					List[str]           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					int           =							floats_tensor((1, 3, 256, 256)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							floats_tensor((1, 3, 256, 256)					,	rng=random.Random(1		)		).to(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Dict           =							pipe_a(
												    prompt_embeds=UpperCamelCase__					,	negative_prompt_embeds=UpperCamelCase__					,	image=UpperCamelCase__					,	mask_image=UpperCamelCase__					,	original_image=UpperCamelCase__					,	generator=UpperCamelCase__					,	num_inference_steps=2					,	output_type='''np'''					,	)

												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							output.images[0]

												assert image.shape == (256, 256, 3)

												SCREAMING_SNAKE_CASE							:					Any           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 10**9

												SCREAMING_SNAKE_CASE							:					Tuple           =							load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy'''		)
												assert_mean_pixel_difference(UpperCamelCase__					,	UpperCamelCase__		)






def    A			(					):
						torch.cuda.empty_cache()
						torch.cuda.reset_max_memory_allocated()
						torch.cuda.reset_peak_memory_stats()






import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
					import torch

if is_datasets_available():
					from datasets import load_dataset

__UpperCamelCase	: Dict    		=  random.Random()
def    A			(					_lowercase	,						_lowercase=1.0	,						_lowercase=None	,						_lowercase=None		):
						if rng is None:
												SCREAMING_SNAKE_CASE							:					Any           =							global_rng

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

						return values





class       lowercase__		(     unittest.TestCase):


						def __init__( self			:       List[Any]					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       str=7					,	UpperCamelCase__			:       Any=400					,	UpperCamelCase__			:       List[str]=2000					,	UpperCamelCase__			:       List[Any]=2048					,	UpperCamelCase__			:       Any=128					,	UpperCamelCase__			:       Union[str, Any]=1					,	UpperCamelCase__			:       List[Any]=512					,	UpperCamelCase__			:       str=30					,	UpperCamelCase__			:       Tuple=4_4100					,	):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							parent
												SCREAMING_SNAKE_CASE							:					str           =							batch_size
												SCREAMING_SNAKE_CASE							:					str           =							min_seq_length
												SCREAMING_SNAKE_CASE							:					Dict           =							max_seq_length
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							spectrogram_length
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							feature_size
												SCREAMING_SNAKE_CASE							:					Tuple           =							num_audio_channels
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							hop_length
												SCREAMING_SNAKE_CASE							:					List[Any]           =							chunk_length
												SCREAMING_SNAKE_CASE							:					str           =							sampling_rate


						def  __A ( self			:       Optional[Any]		):





												'''simple docstring'''





												return {
												    "spectrogram_length": self.spectrogram_length,
												    "feature_size": self.feature_size,
												    "num_audio_channels": self.num_audio_channels,
												    "hop_length": self.hop_length,
												    "chunk_length": self.chunk_length,
												    "sampling_rate": self.sampling_rate,
												}



						def  __A ( self			:       Tuple					,	UpperCamelCase__			:       List[str]=False					,	UpperCamelCase__			:       Optional[int]=False		):





												'''simple docstring'''





												def _flatten(UpperCamelCase__			:       str		):
																		return list(itertools.chain(*UpperCamelCase__		)		)

												if equal_length:
																		SCREAMING_SNAKE_CASE							:					List[str]           =							[floats_list((self.max_seq_length, self.feature_size)		) for _ in range(self.batch_size		)]
												else:
																		# make sure that inputs increase in size
																		SCREAMING_SNAKE_CASE							:					int           =							[
																		    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							:					Optional[Any]           =							[np.asarray(UpperCamelCase__		) for x in speech_inputs]
												return speech_inputs






@require_torch
@require_torchaudio
class       lowercase__		(     UpperCamelCase_			,							unittest.TestCase):
						UpperCamelCase_  =	TvltFeatureExtractor


						def  __A ( self			:       List[str]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Dict           =							TvltFeatureExtractionTester(self		)


						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Any           =							self.feature_extraction_class(**self.feat_extract_dict		)
												self.assertTrue(hasattr(UpperCamelCase__					,	'''spectrogram_length'''		)		)
												self.assertTrue(hasattr(UpperCamelCase__					,	'''feature_size'''		)		)
												self.assertTrue(hasattr(UpperCamelCase__					,	'''num_audio_channels'''		)		)
												self.assertTrue(hasattr(UpperCamelCase__					,	'''hop_length'''		)		)
												self.assertTrue(hasattr(UpperCamelCase__					,	'''chunk_length'''		)		)
												self.assertTrue(hasattr(UpperCamelCase__					,	'''sampling_rate'''		)		)


						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							self.feature_extraction_class(**self.feat_extract_dict		)

												with tempfile.TemporaryDirectory() as tmpdirname:
																		SCREAMING_SNAKE_CASE							:					Optional[Any]           =							feat_extract_first.save_pretrained(UpperCamelCase__		)[0]
																		check_json_file_has_correct_format(UpperCamelCase__		)
																		SCREAMING_SNAKE_CASE							:					List[Any]           =							self.feature_extraction_class.from_pretrained(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							feat_extract_first.to_dict()
												SCREAMING_SNAKE_CASE							:					str           =							feat_extract_second.to_dict()
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							dict_first.pop('''mel_filters'''		)
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							dict_second.pop('''mel_filters'''		)
												self.assertTrue(np.allclose(UpperCamelCase__					,	UpperCamelCase__		)		)
												self.assertEqual(UpperCamelCase__					,	UpperCamelCase__		)


						def  __A ( self			:       int		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Dict           =							self.feature_extraction_class(**self.feat_extract_dict		)

												with tempfile.TemporaryDirectory() as tmpdirname:
																		SCREAMING_SNAKE_CASE							:					Tuple           =							os.path.join(UpperCamelCase__					,	'''feat_extract.json'''		)
																		feat_extract_first.to_json_file(UpperCamelCase__		)
																		SCREAMING_SNAKE_CASE							:					List[str]           =							self.feature_extraction_class.from_json_file(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Any           =							feat_extract_first.to_dict()
												SCREAMING_SNAKE_CASE							:					str           =							feat_extract_second.to_dict()
												SCREAMING_SNAKE_CASE							:					int           =							dict_first.pop('''mel_filters'''		)
												SCREAMING_SNAKE_CASE							:					Any           =							dict_second.pop('''mel_filters'''		)
												self.assertTrue(np.allclose(UpperCamelCase__					,	UpperCamelCase__		)		)
												self.assertEqual(UpperCamelCase__					,	UpperCamelCase__		)


						def  __A ( self			:       Any		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					int           =							self.feature_extraction_class(**self.feat_extract_dict		)

												# create three inputs of length 800, 1000, and 1200
												SCREAMING_SNAKE_CASE							:					List[Any]           =							[floats_list((1, x)		)[0] for x in range(800					,	1400					,	200		)]
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							[np.asarray(UpperCamelCase__		) for speech_input in speech_inputs]

												# Test not batched input
												SCREAMING_SNAKE_CASE							:					List[Any]           =							feature_extractor(np_speech_inputs[0]					,	return_tensors='''np'''					,	sampling_rate=4_4100		).audio_values

												self.assertTrue(encoded_audios.ndim == 4		)
												self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size		)
												self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length		)
												self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels		)

												# Test batched
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							feature_extractor(UpperCamelCase__					,	return_tensors='''np'''					,	sampling_rate=4_4100		).audio_values

												self.assertTrue(encoded_audios.ndim == 4		)
												self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size		)
												self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length		)
												self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels		)

												# Test audio masking
												SCREAMING_SNAKE_CASE							:					List[str]           =							feature_extractor(
												    UpperCamelCase__					,	return_tensors='''np'''					,	sampling_rate=4_4100					,	mask_audio=UpperCamelCase__		).audio_values

												self.assertTrue(encoded_audios.ndim == 4		)
												self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size		)
												self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length		)
												self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels		)

												# Test 2-D numpy arrays are batched.
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							[floats_list((1, x)		)[0] for x in (800, 800, 800)]
												SCREAMING_SNAKE_CASE							:					Dict           =							np.asarray(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					str           =							feature_extractor(UpperCamelCase__					,	return_tensors='''np'''					,	sampling_rate=4_4100		).audio_values
												self.assertTrue(encoded_audios.ndim == 4		)
												self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size		)
												self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length		)
												self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels		)


						def  __A ( self			:       Optional[int]					,	UpperCamelCase__			:       int		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[str]           =							load_dataset('''hf-internal-testing/librispeech_asr_dummy'''					,	'''clean'''					,	split='''validation'''		)
												# automatic decoding with librispeech
												SCREAMING_SNAKE_CASE							:					Dict           =							ds.sort('''id'''		).select(range(UpperCamelCase__		)		)[:num_samples]['''audio''']

												return [x["array"] for x in speech_samples]



						def  __A ( self			:       List[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							self._load_datasamples(1		)
												SCREAMING_SNAKE_CASE							:					int           =							TvltFeatureExtractor()
												SCREAMING_SNAKE_CASE							:					List[Any]           =							feature_extractor(UpperCamelCase__					,	return_tensors='''pt'''		).audio_values

												self.assertEquals(audio_values.shape					,	(1, 1, 192, 128)		)

												SCREAMING_SNAKE_CASE							:					str           =							torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]]		)
												self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]					,	UpperCamelCase__					,	atol=1E-4		)		)





