




'''simple docstring'''


import unittest

import numpy as np
import torch

from diffusers import DDPMPipeline, DDPMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device


enable_full_determinism()






class 						lowerCAmelCase_	(       unittest.TestCase							):







  '''simple docstring'''

  @property
  def 					_snake_case     ( self				:							str	)			->		Any:







       '''simple docstring'''



       torch.manual_seed(0	)
       A:		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''')					,  )
       return model



  def 					_snake_case     ( self				:							int	)			->		List[str]:







       '''simple docstring'''



       A:		str          =		'''cpu'''
       A:		Optional[int]          =		self.dummy_uncond_unet
       A:		Dict          =		DDPMScheduler()

       A:		Optional[int]          =		DDPMPipeline(unet=SCREAMING_SNAKE_CASE_					,  scheduler=SCREAMING_SNAKE_CASE_	)
       ddpm.to(SCREAMING_SNAKE_CASE_	)
       ddpm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

       A:		int          =		torch.Generator(device=SCREAMING_SNAKE_CASE_	).manual_seed(0	)
       A:		Optional[Any]          =		ddpm(generator=SCREAMING_SNAKE_CASE_					,  num_inference_steps=2					,  output_type='''numpy'''	).images

       A:		str          =		torch.Generator(device=SCREAMING_SNAKE_CASE_	).manual_seed(0	)
       A:		Tuple          =		ddpm(generator=SCREAMING_SNAKE_CASE_					,  num_inference_steps=2					,  output_type='''numpy'''					,  return_dict=SCREAMING_SNAKE_CASE_	)[0]

       A:		Optional[int]          =		image[0, -3:, -3:, -1]
       A:		List[str]          =		image_from_tuple[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       A:		Optional[Any]          =		np.array(
           [9.956E-01, 5.785E-01, 4.675E-01, 9.930E-01, 0.0, 1.000, 1.199E-03, 2.648E-04, 5.101E-04]	)

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




  def 					_snake_case     ( self				:							Dict	)			->		int:







       '''simple docstring'''



       A:		Any          =		self.dummy_uncond_unet
       A:		List[Any]          =		DDPMScheduler(prediction_type='''sample'''	)

       A:		str          =		DDPMPipeline(unet=SCREAMING_SNAKE_CASE_					,  scheduler=SCREAMING_SNAKE_CASE_	)
       ddpm.to(SCREAMING_SNAKE_CASE_	)
       ddpm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

       A:		Any          =		torch.manual_seed(0	)
       A:		List[str]          =		ddpm(generator=SCREAMING_SNAKE_CASE_					,  num_inference_steps=2					,  output_type='''numpy'''	).images

       A:		str          =		torch.manual_seed(0	)
       A:		int          =		ddpm(generator=SCREAMING_SNAKE_CASE_					,  num_inference_steps=2					,  output_type='''numpy'''	)[0]

       A:		Optional[int]          =		image[0, -3:, -3:, -1]
       A:		Tuple          =		image_eps[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       A:		Tuple          =		1E-2 if torch_device != '''mps''' else 3E-2
       assert np.abs(image_slice.flatten() - image_eps_slice.flatten()	).max() < tolerance






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







  '''simple docstring'''

  def 					_snake_case     ( self				:							Any	)			->		Optional[int]:







       '''simple docstring'''



       A:		int          =		'''google/ddpm-cifar10-32'''

       A:		str          =		UNetaDModel.from_pretrained(SCREAMING_SNAKE_CASE_	)
       A:		str          =		DDPMScheduler.from_pretrained(SCREAMING_SNAKE_CASE_	)

       A:		Optional[int]          =		DDPMPipeline(unet=SCREAMING_SNAKE_CASE_					,  scheduler=SCREAMING_SNAKE_CASE_	)
       ddpm.to(SCREAMING_SNAKE_CASE_	)
       ddpm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

       A:		List[Any]          =		torch.manual_seed(0	)
       A:		Union[str, Any]          =		ddpm(generator=SCREAMING_SNAKE_CASE_					,  output_type='''numpy'''	).images

       A:		Dict          =		image[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       A:		List[str]          =		np.array([0.4200, 0.3588, 0.1939, 0.3847, 0.3382, 0.2647, 0.4155, 0.3582, 0.3385]	)
       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 						lowerCAmelCase_	(       UpperCAmelCase_							):







  '''simple docstring'''

  UpperCamelCase_			:      Optional[int]										=				"""AutoFeatureExtractor"""
  UpperCamelCase_			:      List[Any]										=				"""Speech2Text2Tokenizer"""



  def __init__( self				:							str					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							int	)			->		Any:







       '''simple docstring'''



       super().__init__(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)
       A:		Union[str, Any]          =		self.feature_extractor
       A:		Dict          =		False



  def __call__( self				:							Any					,  *SCREAMING_SNAKE_CASE_				:							Any					,  **SCREAMING_SNAKE_CASE_				:							Optional[int]	)			->		Optional[Any]:







       '''simple docstring'''



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

       if "raw_speech" in kwargs:
            warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''	)
            A:		List[str]          =		kwargs.pop('''raw_speech'''	)
       else:
            A:		Any          =		kwargs.pop('''audio'''					,  SCREAMING_SNAKE_CASE_	)
       A:		List[Any]          =		kwargs.pop('''sampling_rate'''					,  SCREAMING_SNAKE_CASE_	)
       A:		Optional[int]          =		kwargs.pop('''text'''					,  SCREAMING_SNAKE_CASE_	)
       if len(SCREAMING_SNAKE_CASE_	) > 0:
            A:		str          =		args[0]
            A:		Tuple          =		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:
            A:		Tuple          =		self.feature_extractor(SCREAMING_SNAKE_CASE_					,  *SCREAMING_SNAKE_CASE_					,  sampling_rate=SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)
       if text is not None:
            A:		List[Any]          =		self.tokenizer(SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_	)

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



  def 					_snake_case     ( self				:							Tuple					,  *SCREAMING_SNAKE_CASE_				:							int					,  **SCREAMING_SNAKE_CASE_				:							Union[str, Any]	)			->		str:







       '''simple docstring'''



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



  def 					_snake_case     ( self				:							Optional[int]					,  *SCREAMING_SNAKE_CASE_				:							str					,  **SCREAMING_SNAKE_CASE_				:							int	)			->		Union[str, Any]:







       '''simple docstring'''



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




  @contextmanager
  def 					_snake_case     ( self				:							str	)			->		int:







       '''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.'''	)
       A:		List[Any]          =		True
       A:		List[str]          =		self.tokenizer
       yield
       A:		List[Any]          =		self.feature_extractor
       A:		int          =		False






