


'''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__   :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__   :List[Any]												= DDIMScheduler()
							UpperCamelCase__   :str												= {'unet': unet, 'scheduler': scheduler}
							return components




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

							'''simple docstring'''


							if str(UpperCamelCase_							).startswith('''mps'''							):
												UpperCamelCase__   :str												= torch.manual_seed(UpperCamelCase_							)
							else:
												UpperCamelCase__   :Dict												= 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__   :List[str]												= 'cpu'

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

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

							self.assertEqual(image.shape    ,   (1, 32, 32, 3)							)
							UpperCamelCase__   :List[Any]												= 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__   :Optional[Any]												= 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__   :List[Any]												= 'google/ddpm-cifar10-32'

							UpperCamelCase__   :Dict												= UNetaDModel.from_pretrained(UpperCamelCase_							)
							UpperCamelCase__   :Tuple												= DDIMScheduler()

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

							UpperCamelCase__   :int												= torch.manual_seed(0							)
							UpperCamelCase__   :Union[str, Any]												= ddim(generator=UpperCamelCase_    ,   eta=0.0    ,   output_type='''numpy'''							).images

							UpperCamelCase__   :List[str]												= image[0, -3:, -3:, -1]

							assert image.shape == (1, 32, 32, 3)
							UpperCamelCase__   :Optional[int]												= 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__   :Tuple												= 'google/ddpm-ema-bedroom-256'

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

							UpperCamelCase__   :Tuple												= DDIMPipeline(unet=UpperCamelCase_    ,   scheduler=UpperCamelCase_							)
							ddpm.to(UpperCamelCase_							)
							ddpm.set_progress_bar_config(disable=UpperCamelCase_							)

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

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

							assert image.shape == (1, 256, 256, 3)
							UpperCamelCase__   :int												= 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



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_tf_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE       :      List[str]												= {
    "configuration_speech_to_text": ["SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2TextConfig"],
    "processing_speech_to_text": ["Speech2TextProcessor"],
}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE       :      Union[str, Any]												= ["Speech2TextTokenizer"]

try:
 if not is_speech_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE       :      int												= ["Speech2TextFeatureExtractor"]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE       :      Optional[Any]												= [
     "TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
     "TFSpeech2TextForConditionalGeneration",
     "TFSpeech2TextModel",
     "TFSpeech2TextPreTrainedModel",
 ]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE       :      str												= [
     "SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
     "Speech2TextForConditionalGeneration",
     "Speech2TextModel",
     "Speech2TextPreTrainedModel",
 ]


if TYPE_CHECKING:
 from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig
 from .processing_speech_to_text import SpeechaTextProcessor

 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_speech_to_text import SpeechaTextTokenizer

 try:
  if not is_speech_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_speech_to_text import (
      TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFSpeechaTextForConditionalGeneration,
      TFSpeechaTextModel,
      TFSpeechaTextPreTrainedModel,
  )

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_speech_to_text import (
      SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
      SpeechaTextForConditionalGeneration,
      SpeechaTextModel,
      SpeechaTextPreTrainedModel,
  )

else:
 import sys

 SCREAMING_SNAKE_CASE       :      Optional[Any]												= _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)


