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  UpperCamelCase				( __lowercase   ,  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 		UpperCamelCase_						(					self						) ->				str:






           """simple docstring"""
           torch.manual_seed(0						)
           _lowerCamelCase			    =				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''')				,				)
           _lowerCamelCase			    =				DDIMScheduler()
           _lowerCamelCase			    =				{'''unet''': unet, '''scheduler''': scheduler}
           return components



     def 		UpperCamelCase_						(					self				,				A_				,				A_=0						) ->				int:






           """simple docstring"""
           if str(A_						).startswith('''mps'''						):
                 _lowerCamelCase			    =				torch.manual_seed(A_						)
           else:
                 _lowerCamelCase			    =				torch.Generator(device=A_						).manual_seed(A_						)
           _lowerCamelCase			    =				{
               '''batch_size''': 1,
               '''generator''': generator,
               '''num_inference_steps''': 2,
               '''output_type''': '''numpy''',
           }
           return inputs



     def 		UpperCamelCase_						(					self						) ->				str:






           """simple docstring"""
           _lowerCamelCase			    =				'''cpu'''

           _lowerCamelCase			    =				self.get_dummy_components()
           _lowerCamelCase			    =				self.pipeline_class(**A_						)
           pipe.to(A_						)
           pipe.set_progress_bar_config(disable=A_						)

           _lowerCamelCase			    =				self.get_dummy_inputs(A_						)
           _lowerCamelCase			    =				pipe(**A_						).images
           _lowerCamelCase			    =				image[0, -3:, -3:, -1]

           self.assertEqual(image.shape				,				(1, 32, 32, 3)						)
           _lowerCamelCase			    =				np.array(
               [1.0_0_0E0_0, 5.7_1_7E-0_1, 4.7_1_7E-0_1, 1.0_0_0E0_0, 0.0_0_0E0_0, 1.0_0_0E0_0, 3.0_0_0E-0_4, 0.0_0_0E0_0, 9.0_0_0E-0_4]						)
           _lowerCamelCase			    =				np.abs(image_slice.flatten() - expected_slice						).max()
           self.assertLessEqual(A_				,				1E-3						)



     def 		UpperCamelCase_						(					self						) ->				str:






           """simple docstring"""
           super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3						)



     def 		UpperCamelCase_						(					self						) ->				int:






           """simple docstring"""
           super().test_save_load_local(expected_max_difference=3E-3						)



     def 		UpperCamelCase_						(					self						) ->				Any:






           """simple docstring"""
           super().test_save_load_optional_components(expected_max_difference=3E-3						)





     def 		UpperCamelCase_						(					self						) ->				List[Any]:






           """simple docstring"""
           super().test_inference_batch_single_identical(expected_max_diff=3E-3						)





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

     '''simple docstring'''

     def 		UpperCamelCase_						(					self						) ->				Union[str, Any]:






           """simple docstring"""
           _lowerCamelCase			    =				'''google/ddpm-cifar10-32'''

           _lowerCamelCase			    =				UNetaDModel.from_pretrained(A_						)
           _lowerCamelCase			    =				DDIMScheduler()

           _lowerCamelCase			    =				DDIMPipeline(unet=A_				,				scheduler=A_						)
           ddim.to(A_						)
           ddim.set_progress_bar_config(disable=A_						)

           _lowerCamelCase			    =				torch.manual_seed(0						)
           _lowerCamelCase			    =				ddim(generator=A_				,				eta=0.0				,				output_type='''numpy'''						).images

           _lowerCamelCase			    =				image[0, -3:, -3:, -1]

           assert image.shape == (1, 32, 32, 3)
           _lowerCamelCase			    =				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 		UpperCamelCase_						(					self						) ->				List[Any]:






           """simple docstring"""
           _lowerCamelCase			    =				'''google/ddpm-ema-bedroom-256'''

           _lowerCamelCase			    =				UNetaDModel.from_pretrained(A_						)
           _lowerCamelCase			    =				DDIMScheduler.from_pretrained(A_						)

           _lowerCamelCase			    =				DDIMPipeline(unet=A_				,				scheduler=A_						)
           ddpm.to(A_						)
           ddpm.set_progress_bar_config(disable=A_						)

           _lowerCamelCase			    =				torch.manual_seed(0						)
           _lowerCamelCase			    =				ddpm(generator=A_				,				output_type='''numpy'''						).images

           _lowerCamelCase			    =				image[0, -3:, -3:, -1]

           assert image.shape == (1, 2_56, 2_56, 3)
           _lowerCamelCase			    =				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_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


snake_case__				= {
    'configuration_whisper': ['WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WhisperConfig', 'WhisperOnnxConfig'],
    'feature_extraction_whisper': ['WhisperFeatureExtractor'],
    'processing_whisper': ['WhisperProcessor'],
    'tokenization_whisper': ['WhisperTokenizer'],
}

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case__				= ['WhisperTokenizerFast']

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case__				= [
         'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST',
         'WhisperForConditionalGeneration',
         'WhisperModel',
         'WhisperPreTrainedModel',
         'WhisperForAudioClassification',
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case__				= [
         'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST',
         'TFWhisperForConditionalGeneration',
         'TFWhisperModel',
         'TFWhisperPreTrainedModel',
     ]

try:
     if not is_flax_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case__				= [
         'FlaxWhisperForConditionalGeneration',
         'FlaxWhisperModel',
         'FlaxWhisperPreTrainedModel',
         'FlaxWhisperForAudioClassification',
     ]


if TYPE_CHECKING:
     from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig
     from .feature_extraction_whisper import WhisperFeatureExtractor
     from .processing_whisper import WhisperProcessor
     from .tokenization_whisper import WhisperTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_whisper_fast import WhisperTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_whisper import (
              WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
              WhisperForAudioClassification,
              WhisperForConditionalGeneration,
              WhisperModel,
              WhisperPreTrainedModel,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_whisper import (
              TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFWhisperForConditionalGeneration,
              TFWhisperModel,
              TFWhisperPreTrainedModel,
          )

     try:
          if not is_flax_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_flax_whisper import (
              FlaxWhisperForAudioClassification,
              FlaxWhisperForConditionalGeneration,
              FlaxWhisperModel,
              FlaxWhisperPreTrainedModel,
          )

else:
     import sys

     snake_case__				= _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)