




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






     def     _lowercase     (			self   )							->      Any:
          torch.manual_seed(0   )
          _snake_case							=				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") ,)
          _snake_case							=				DDIMScheduler()
          _snake_case							=				{"unet": unet, "scheduler": scheduler}
          return components






     def     _lowercase     (			self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE=0   )							->      Union[str, Any]:

          if str(lowerCamelCase_   ).startswith("mps"   ):
               _snake_case							=				torch.manual_seed(lowerCamelCase_   )
          else:
               _snake_case							=				torch.Generator(device=lowerCamelCase_   ).manual_seed(lowerCamelCase_   )
          _snake_case							=				{
              "batch_size": 1,
              "generator": generator,
              "num_inference_steps": 2,
              "output_type": "numpy",
          }
          return inputs






     def     _lowercase     (			self   )							->      int:
          _snake_case							=				"cpu"

          _snake_case							=				self.get_dummy_components()
          _snake_case							=				self.pipeline_class(**lowerCamelCase_   )
          pipe.to(lowerCamelCase_   )
          pipe.set_progress_bar_config(disable=lowerCamelCase_   )

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

          self.assertEqual(image.shape ,(1, 32, 32, 3)   )
          _snake_case							=				np.array(
              [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04]   )
          _snake_case							=				np.abs(image_slice.flatten() - expected_slice   ).max()
          self.assertLessEqual(lowerCamelCase_ ,1e-3   )






     def     _lowercase     (			self   )							->      List[Any]:
          super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3   )






     def     _lowercase     (			self   )							->      Optional[Any]:
          super().test_save_load_local(expected_max_difference=3e-3   )






     def     _lowercase     (			self   )							->      str:
          super().test_save_load_optional_components(expected_max_difference=3e-3   )






     def     _lowercase     (			self   )							->      Tuple:
          super().test_inference_batch_single_identical(expected_max_diff=3e-3   )







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






     def     _lowercase     (			self   )							->      Optional[int]:
          _snake_case							=				"google/ddpm-cifar10-32"

          _snake_case							=				UNetaDModel.from_pretrained(lowerCamelCase_   )
          _snake_case							=				DDIMScheduler()

          _snake_case							=				DDIMPipeline(unet=lowerCamelCase_ ,scheduler=lowerCamelCase_   )
          ddim.to(lowerCamelCase_   )
          ddim.set_progress_bar_config(disable=lowerCamelCase_   )

          _snake_case							=				torch.manual_seed(0   )
          _snake_case							=				ddim(generator=lowerCamelCase_ ,eta=0.0 ,output_type="numpy"   ).images

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

          assert image.shape == (1, 32, 32, 3)
          _snake_case							=				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     _lowercase     (			self   )							->      List[Any]:
          _snake_case							=				"google/ddpm-ema-bedroom-256"

          _snake_case							=				UNetaDModel.from_pretrained(lowerCamelCase_   )
          _snake_case							=				DDIMScheduler.from_pretrained(lowerCamelCase_   )

          _snake_case							=				DDIMPipeline(unet=lowerCamelCase_ ,scheduler=lowerCamelCase_   )
          ddpm.to(lowerCamelCase_   )
          ddpm.set_progress_bar_config(disable=lowerCamelCase_   )

          _snake_case							=				torch.manual_seed(0   )
          _snake_case							=				ddpm(generator=lowerCamelCase_ ,output_type="numpy"   ).images

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

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

from collections.abc import Callable





def      __a     (       _UpperCamelCase:   Callable[[int | float], int | float] ,						_UpperCamelCase:   int | float ,						_UpperCamelCase:   int | float ,						_UpperCamelCase:   int = 100 ,						)						->			float:

     """simple docstring"""




     _snake_case							=				x_start
     _snake_case							=				fnc(_UpperCamelCase   )
     _snake_case							=				0.0
     for _ in range(_UpperCamelCase   ):
          # Approximates small segments of curve as linear and solve
          # for trapezoidal area
          _snake_case							=				(x_end - x_start) / steps + xa
          _snake_case							=				fnc(_UpperCamelCase   )
          area += abs(fxa + fxa   ) * (xa - xa) / 2
          # Increment step
          _snake_case							=				xa
          _snake_case							=				fxa
     return area


if __name__ == "__main__":





   def      __a     (       _UpperCamelCase:   Any   )						->			Optional[int]:

        """simple docstring"""




        return x**3 + x**2

   print('''f(x) = x^3 + x^2''')
   print('''The area between the curve, x = -5, x = 5 and the x axis is:''')
   UpperCamelCase_						:      Optional[int]       =						10
   while i <= 100000:
      print(F'with {i} steps: {trapezoidal_area(f, -5, 5, i)}')
      i *= 10


