



"""simple docstring"""
import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


SCREAMING_SNAKE_CASE_				= random.Random()

if is_torch_available():
					import torch



def        lowerCAmelCase_						(							SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__=1.0, SCREAMING_SNAKE_CASE__=None, SCREAMING_SNAKE_CASE__=None	)			->      List[Any]:
		if rng is None:
				a_     :      Optional[int] 						=     global_rng

		a_     :      Tuple 						=     []
		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				):






							def __init__(   self			,     a_			,     a_=7			,     a_=4_0_0			,     a_=2_0_0_0			,     a_=1			,     a_=0.0			,     a_=1_6_0_0_0			,     a_=True			,     a_=True			,     ):
									a_     :      List[str] 						=     parent
									a_     :      str 						=     batch_size
									a_     :      int 						=     min_seq_length
									a_     :      int 						=     max_seq_length
									a_     :      Union[str, Any] 						=     (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
									a_     :      Optional[Any] 						=     feature_size
									a_     :      Union[str, Any] 						=     padding_value
									a_     :      Optional[int] 						=     sampling_rate
									a_     :      int 						=     return_attention_mask
									a_     :      Tuple 						=     do_normalize






							def snake_case_  (   self      ):
									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 snake_case_  (   self			,     a_=False			,     a_=False      ):
									def _flatten(a_      ):
											return list(itertools.chain(*a_      )      )

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

									return speech_inputs




@require_torch
@require_torchaudio
class 			snake_case_						(						a_     ,unittest.TestCase				):
							__lowerCAmelCase    					=					ASTFeatureExtractor






							def snake_case_  (   self      ):
									a_     :      Tuple 						=     ASTFeatureExtractionTester(self      )






							def snake_case_  (   self      ):
									# Tests that all call wrap to encode_plus and batch_encode_plus
									a_     :      int 						=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
									# create three inputs of length 800, 1000, and 1200
									a_     :      List[Any] 						=     [floats_list((1, x)      )[0] for x in range(8_0_0			,     1_4_0_0			,     2_0_0      )]
									a_     :      Optional[Any] 						=     [np.asarray(a_      ) for speech_input in speech_inputs]

									# Test not batched input
									a_     :      Optional[int] 						=     feat_extract(speech_inputs[0]			,     return_tensors="np"      ).input_values
									a_     :      Dict 						=     feat_extract(np_speech_inputs[0]			,     return_tensors="np"      ).input_values
									self.assertTrue(np.allclose(a_			,     a_			,     atol=1e-3      )      )

									# Test batched
									a_     :      Dict 						=     feat_extract(a_			,     padding=a_			,     return_tensors="np"      ).input_values
									a_     :      int 						=     feat_extract(a_			,     padding=a_			,     return_tensors="np"      ).input_values
									for enc_seq_a, enc_seq_a in zip(a_			,     a_      ):
											self.assertTrue(np.allclose(a_			,     a_			,     atol=1e-3      )      )

									# Test 2-D numpy arrays are batched.
									a_     :      Optional[int] 						=     [floats_list((1, x)      )[0] for x in (8_0_0, 8_0_0, 8_0_0)]
									a_     :      Any 						=     np.asarray(a_      )
									a_     :      List[Any] 						=     feat_extract(a_			,     return_tensors="np"      ).input_values
									a_     :      List[Any] 						=     feat_extract(a_			,     return_tensors="np"      ).input_values
									for enc_seq_a, enc_seq_a in zip(a_			,     a_      ):
											self.assertTrue(np.allclose(a_			,     a_			,     atol=1e-3      )      )






							@require_torch
							def snake_case_  (   self      ):
									import torch

									a_     :      Any 						=     self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
									a_     :      List[Any] 						=     np.random.rand(1_0_0      ).astype(np.floataa      )
									a_     :      List[str] 						=     np_speech_inputs.tolist()

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






							def snake_case_  (   self			,     a_      ):
									from datasets import load_dataset

									a_     :      str 						=     load_dataset("hf-internal-testing/librispeech_asr_dummy"			,     "clean"			,     split="validation"      )
									# automatic decoding with librispeech
									a_     :      Optional[Any] 						=     ds.sort("id"      ).select(range(a_      )      )[:num_samples]["audio"]

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






							@require_torch
							def snake_case_  (   self      ):
									# fmt: off
									a_     :      Any 						=     torch.tensor(
									    [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776,
									     -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133,
									     -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936,
									     -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869]      )
									# fmt: on

									a_     :      int 						=     self._load_datasamples(1      )
									a_     :      Any 						=     ASTFeatureExtractor()
									a_     :      str 						=     feature_extractor(a_			,     return_tensors="pt"      ).input_values
									self.assertEquals(input_values.shape			,     (1, 1_0_2_4, 1_2_8)      )
									self.assertTrue(torch.allclose(input_values[0, 0, :3_0]			,     a_			,     atol=1e-4      )      )




"""simple docstring"""
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionImageVariationPipeline
from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device


SCREAMING_SNAKE_CASE_				= False




class 			snake_case_						(						unittest.TestCase				):
							pass




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






							def snake_case_  (   self      ):
									a_     :      Tuple 						=     VersatileDiffusionImageVariationPipeline.from_pretrained("shi-labs/versatile-diffusion"      )
									pipe.to(a_      )
									pipe.set_progress_bar_config(disable=a_      )

									a_     :      Optional[Any] 						=     load_image(
									    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"      )
									a_     :      Optional[Any] 						=     torch.manual_seed(0      )
									a_     :      Dict 						=     pipe(
									    image=a_			,     generator=a_			,     guidance_scale=7.5			,     num_inference_steps=5_0			,     output_type="numpy"			,     ).images

									a_     :      List[Any] 						=     image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

									assert image.shape == (1, 5_1_2, 5_1_2, 3)
									a_     :      Any 						=     np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945]      )

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