





'''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 UpperCAmelCase							(		UpperCAmelCase_					,			unittest.TestCase							):
    _A :			Tuple						   =       DDIMPipeline
    _A :			List[str]						   =       UNCONDITIONAL_IMAGE_GENERATION_PARAMS
    _A :			Optional[int]						   =       PipelineTesterMixin.required_optional_params - {
        """num_images_per_prompt""",
        """latents""",
        """callback""",
        """callback_steps""",
    }
    _A :			Tuple						   =       UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
    _A :			Union[str, Any]						   =       False



    def 					__lowerCamelCase						(  self					):
     torch.manual_seed(0					)
     __UpperCAmelCase					=   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					=   DDIMScheduler()
     __UpperCAmelCase					=   {'unet': unet, 'scheduler': scheduler}
     return components



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A=0					):
     if str(__A					).startswith('mps'					):
      __UpperCAmelCase					=   torch.manual_seed(__A					)
     else:
      __UpperCAmelCase					=   torch.Generator(device=__A					).manual_seed(__A					)
     __UpperCAmelCase					=   {
         'batch_size': 1,
         'generator': generator,
         'num_inference_steps': 2,
         'output_type': 'numpy',
     }
     return inputs



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   'cpu'

     __UpperCAmelCase					=   self.get_dummy_components()
     __UpperCAmelCase					=   self.pipeline_class(**__A					)
     pipe.to(__A					)
     pipe.set_progress_bar_config(disable=__A					)

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

     self.assertEqual(image.shape     ,			(1, 32, 32, 3)					)
     __UpperCAmelCase					=   np.array(
         [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04]					)
     __UpperCAmelCase					=   np.abs(image_slice.flatten() - expected_slice					).max()
     self.assertLessEqual(__A     ,			1E-3					)



    def 					__lowerCamelCase						(  self					):
     super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3					)



    def 					__lowerCamelCase						(  self					):
     super().test_save_load_local(expected_max_difference=3E-3					)



    def 					__lowerCamelCase						(  self					):
     super().test_save_load_optional_components(expected_max_difference=3E-3					)




    def 					__lowerCamelCase						(  self					):
     super().test_inference_batch_single_identical(expected_max_diff=3E-3					)






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



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   'google/ddpm-cifar10-32'

     __UpperCAmelCase					=   UNetaDModel.from_pretrained(__A					)
     __UpperCAmelCase					=   DDIMScheduler()

     __UpperCAmelCase					=   DDIMPipeline(unet=__A     ,			scheduler=__A					)
     ddim.to(__A					)
     ddim.set_progress_bar_config(disable=__A					)

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

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

     assert image.shape == (1, 32, 32, 3)
     __UpperCAmelCase					=   np.array([0.1_7_2_3, 0.1_6_1_7, 0.1_6_0_0, 0.1_6_2_6, 0.1_4_9_7, 0.1_5_1_3, 0.1_5_0_5, 0.1_4_4_2, 0.1_4_5_3]					)

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




    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   'google/ddpm-ema-bedroom-256'

     __UpperCAmelCase					=   UNetaDModel.from_pretrained(__A					)
     __UpperCAmelCase					=   DDIMScheduler.from_pretrained(__A					)

     __UpperCAmelCase					=   DDIMPipeline(unet=__A     ,			scheduler=__A					)
     ddpm.to(__A					)
     ddpm.set_progress_bar_config(disable=__A					)

     __UpperCAmelCase					=   torch.manual_seed(0					)
     __UpperCAmelCase					=   ddpm(generator=__A     ,			output_type='numpy'					).images

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

     assert image.shape == (1, 256, 256, 3)
     __UpperCAmelCase					=   np.array([0.0_0_6_0, 0.0_2_0_1, 0.0_3_4_4, 0.0_0_2_4, 0.0_0_1_8, 0.0_0_0_2, 0.0_0_2_2, 0.0_0_0_0, 0.0_0_6_9]					)

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












'''simple docstring'''




from __future__ import annotations







def _lowerCAmelCase     (      _lowerCAmelCase    )->     int:
 if not nums:
  return 0
 __UpperCAmelCase					=   nums[0]
 __UpperCAmelCase					=   0
 for num in nums[1:]:
  __UpperCAmelCase					,    __UpperCAmelCase					=   (
      max_excluding + num,
      max(_lowerCAmelCase       ,    _lowerCAmelCase    ),
  )
 return max(_lowerCAmelCase       ,    _lowerCAmelCase    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()





