


'''simple docstring'''
import unittest

import numpy as np
import torch

from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class      lowercase			(							A__			,	unittest.TestCase      ):

		"""simple docstring"""

		_a									=       DDIMPipeline
		_a									=       UNCONDITIONAL_IMAGE_GENERATION_PARAMS
		_a									=       PipelineTesterMixin.required_optional_params - {
		    'num_images_per_prompt',
		    'latents',
		    'callback',
		    'callback_steps',
		}
		_a									=       UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
		_a									=       False




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							torch.manual_seed(0							)
							UpperCamelCase__     :List[Any]							      =				UNetaDModel(
							    block_out_channels=(32, 64)					,     layers_per_block=2					,     sample_size=32					,     in_channels=3					,     out_channels=3					,     down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')					,     up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')					,     )
							UpperCamelCase__     :Union[str, Any]							      =				DDIMScheduler()
							UpperCamelCase__     :List[str]							      =				{'''unet''': unet, '''scheduler''': scheduler}
							return components




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_=0							):

							'''simple docstring'''


							if str(UpperCamelCase_							).startswith('''mps'''							):
												UpperCamelCase__     :Optional[Any]							      =				torch.manual_seed(UpperCamelCase_							)
							else:
												UpperCamelCase__     :List[Any]							      =				torch.Generator(device=UpperCamelCase_							).manual_seed(UpperCamelCase_							)
							UpperCamelCase__     :List[Any]							      =				{
							    '''batch_size''': 1,
							    '''generator''': generator,
							    '''num_inference_steps''': 2,
							    '''output_type''': '''numpy''',
							}
							return inputs




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Optional[int]							      =				'''cpu'''

							UpperCamelCase__     :Optional[int]							      =				self.get_dummy_components()
							UpperCamelCase__     :Dict							      =				self.pipeline_class(**UpperCamelCase_							)
							pipe.to(UpperCamelCase_							)
							pipe.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__     :str							      =				self.get_dummy_inputs(UpperCamelCase_							)
							UpperCamelCase__     :Dict							      =				pipe(**UpperCamelCase_							).images
							UpperCamelCase__     :Dict							      =				image[0, -3:, -3:, -1]

							self.assertEqual(image.shape					,     (1, 32, 32, 3)							)
							UpperCamelCase__     :int							      =				np.array(
							    [1.0_00e00, 5.7_17e-01, 4.7_17e-01, 1.0_00e00, 0.0_00e00, 1.0_00e00, 3.0_00e-04, 0.0_00e00, 9.0_00e-04]							)
							UpperCamelCase__     :int							      =				np.abs(image_slice.flatten() - expected_slice							).max()
							self.assertLessEqual(UpperCamelCase_					,     1e-3							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							super().test_save_load_local(expected_max_difference=3e-3							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							super().test_save_load_optional_components(expected_max_difference=3e-3							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							super().test_inference_batch_single_identical(expected_max_diff=3e-3							)



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

		"""simple docstring"""

		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Tuple							      =				'''google/ddpm-cifar10-32'''

							UpperCamelCase__     :Union[str, Any]							      =				UNetaDModel.from_pretrained(UpperCamelCase_							)
							UpperCamelCase__     :Union[str, Any]							      =				DDIMScheduler()

							UpperCamelCase__     :Optional[int]							      =				DDIMPipeline(unet=UpperCamelCase_					,     scheduler=UpperCamelCase_							)
							ddim.to(UpperCamelCase_							)
							ddim.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__     :Dict							      =				torch.manual_seed(0							)
							UpperCamelCase__     :Tuple							      =				ddim(generator=UpperCamelCase_					,     eta=0.0					,     output_type='''numpy'''							).images

							UpperCamelCase__     :Tuple							      =				image[0, -3:, -3:, -1]

							assert image.shape == (1, 32, 32, 3)
							UpperCamelCase__     :Optional[Any]							      =				np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453]							)

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




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Any							      =				'''google/ddpm-ema-bedroom-256'''

							UpperCamelCase__     :List[str]							      =				UNetaDModel.from_pretrained(UpperCamelCase_							)
							UpperCamelCase__     :List[Any]							      =				DDIMScheduler.from_pretrained(UpperCamelCase_							)

							UpperCamelCase__     :Optional[int]							      =				DDIMPipeline(unet=UpperCamelCase_					,     scheduler=UpperCamelCase_							)
							ddpm.to(UpperCamelCase_							)
							ddpm.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__     :Union[str, Any]							      =				torch.manual_seed(0							)
							UpperCamelCase__     :Optional[Any]							      =				ddpm(generator=UpperCamelCase_					,     output_type='''numpy'''							).images

							UpperCamelCase__     :Optional[int]							      =				image[0, -3:, -3:, -1]

							assert image.shape == (1, 256, 256, 3)
							UpperCamelCase__     :List[Any]							      =				np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069]							)

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



'''simple docstring'''
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       'Speech2TextFeatureExtractor'
		_a									=       'Speech2TextTokenizer'




		def __init__(	self					,     UpperCamelCase_					,     UpperCamelCase_							):

							'''simple docstring'''


							super().__init__(UpperCamelCase_					,     UpperCamelCase_							)
							UpperCamelCase__     :List[str]							      =				self.feature_extractor
							UpperCamelCase__     :Optional[Any]							      =				False




		def __call__(	self					,     *UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							if self._in_target_context_manager:
												return self.current_processor(*UpperCamelCase_					,     **UpperCamelCase_							)

							if "raw_speech" in kwargs:
												warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''							)
												UpperCamelCase__     :Optional[Any]							      =				kwargs.pop('''raw_speech'''							)
							else:
												UpperCamelCase__     :List[Any]							      =				kwargs.pop('''audio'''					,     UpperCamelCase_							)
							UpperCamelCase__     :List[Any]							      =				kwargs.pop('''sampling_rate'''					,     UpperCamelCase_							)
							UpperCamelCase__     :Dict							      =				kwargs.pop('''text'''					,     UpperCamelCase_							)
							if len(UpperCamelCase_							) > 0:
												UpperCamelCase__     :Tuple							      =				args[0]
												UpperCamelCase__     :Optional[int]							      =				args[1:]

							if audio is None and text is None:
												raise ValueError('''You need to specify either an `audio` or `text` input to process.'''							)

							if audio is not None:
												UpperCamelCase__     :Any							      =				self.feature_extractor(UpperCamelCase_					,     *UpperCamelCase_					,     sampling_rate=UpperCamelCase_					,     **UpperCamelCase_							)
							if text is not None:
												UpperCamelCase__     :Dict							      =				self.tokenizer(UpperCamelCase_					,     **UpperCamelCase_							)

							if text is None:
												return inputs
							elif audio is None:
												return encodings
							else:
												UpperCamelCase__     :Dict							      =				encodings['''input_ids''']
												return inputs




		def 					lowerCAmelCase__       (	self					,     *UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return self.tokenizer.batch_decode(*UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     *UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return self.tokenizer.decode(*UpperCamelCase_					,     **UpperCamelCase_							)




		@contextmanager
		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							warnings.warn(
							    '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
							    '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
							    '''your audio inputs, or in a separate call.'''							)
							UpperCamelCase__     :Optional[Any]							      =				True
							UpperCamelCase__     :Any							      =				self.tokenizer
							yield
							UpperCamelCase__     :Any							      =				self.feature_extractor
							UpperCamelCase__     :Any							      =				False