






"""simple docstring"""



import torch

from diffusers import HeunDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest


class 	_UpperCamelCase ( __UpperCamelCase							):



    '''simple docstring'''


    __lowercase    :   Tuple											=						(HeunDiscreteScheduler,)
    __lowercase    :   str											=						10


    def 		A__      (      self   ,			**__lowercase			):
     UpperCAmelCase__ 						=				{
         """num_train_timesteps""": 1100,
         """beta_start""": 0.0_001,
         """beta_end""": 0.02,
         """beta_schedule""": """linear""",
     }

     config.update(**__lowercase			)
     return config


    def 		A__      (      self			):
     for timesteps in [10, 50, 100, 1000]:
      self.check_over_configs(num_train_timesteps=__lowercase			)


    def 		A__      (      self			):
     for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001]   ,			[0.0_002, 0.002, 0.02]			):
      self.check_over_configs(beta_start=__lowercase   ,			beta_end=__lowercase			)


    def 		A__      (      self			):
     for schedule in ["linear", "scaled_linear", "exp"]:
      self.check_over_configs(beta_schedule=__lowercase			)


    def 		A__      (      self			):
     for clip_sample_range in [1.0, 2.0, 3.0]:
      self.check_over_configs(clip_sample_range=__lowercase   ,			clip_sample=__lowercase			)


    def 		A__      (      self			):
     for prediction_type in ["epsilon", "v_prediction", "sample"]:
      self.check_over_configs(prediction_type=__lowercase			)


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.scheduler_classes[0]
     UpperCAmelCase__ 						=				self.get_scheduler_config()
     UpperCAmelCase__ 						=				scheduler_class(**__lowercase			)

     scheduler.set_timesteps(self.num_inference_steps			)

     UpperCAmelCase__ 						=				self.dummy_model()
     UpperCAmelCase__ 						=				self.dummy_sample_deter * scheduler.init_noise_sigma
     UpperCAmelCase__ 						=				sample.to(__lowercase			)

     for i, t in enumerate(scheduler.timesteps			):
      UpperCAmelCase__ 						=				scheduler.scale_model_input(__lowercase   ,			__lowercase			)

      UpperCAmelCase__ 						=				model(__lowercase   ,			__lowercase			)

      UpperCAmelCase__ 						=				scheduler.step(__lowercase   ,			__lowercase   ,			__lowercase			)
      UpperCAmelCase__ 						=				output.prev_sample

     UpperCAmelCase__ 						=				torch.sum(torch.abs(__lowercase			)			)
     UpperCAmelCase__ 						=				torch.mean(torch.abs(__lowercase			)			)

     if torch_device in ["cpu", "mps"]:
      assert abs(result_sum.item() - 0.1_233			) < 1e-2
      assert abs(result_mean.item() - 0.0_002			) < 1e-3
     else:
      # CUDA
      assert abs(result_sum.item() - 0.1_233			) < 1e-2
      assert abs(result_mean.item() - 0.0_002			) < 1e-3


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.scheduler_classes[0]
     UpperCAmelCase__ 						=				self.get_scheduler_config(prediction_type="""v_prediction"""			)
     UpperCAmelCase__ 						=				scheduler_class(**__lowercase			)

     scheduler.set_timesteps(self.num_inference_steps			)

     UpperCAmelCase__ 						=				self.dummy_model()
     UpperCAmelCase__ 						=				self.dummy_sample_deter * scheduler.init_noise_sigma
     UpperCAmelCase__ 						=				sample.to(__lowercase			)

     for i, t in enumerate(scheduler.timesteps			):
      UpperCAmelCase__ 						=				scheduler.scale_model_input(__lowercase   ,			__lowercase			)

      UpperCAmelCase__ 						=				model(__lowercase   ,			__lowercase			)

      UpperCAmelCase__ 						=				scheduler.step(__lowercase   ,			__lowercase   ,			__lowercase			)
      UpperCAmelCase__ 						=				output.prev_sample

     UpperCAmelCase__ 						=				torch.sum(torch.abs(__lowercase			)			)
     UpperCAmelCase__ 						=				torch.mean(torch.abs(__lowercase			)			)

     if torch_device in ["cpu", "mps"]:
      assert abs(result_sum.item() - 4.6934e-07			) < 1e-2
      assert abs(result_mean.item() - 6.1112e-10			) < 1e-3
     else:
      # CUDA
      assert abs(result_sum.item() - 4.693_4286_5017_0972e-07			) < 1e-2
      assert abs(result_mean.item() - 0.0_002			) < 1e-3


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.scheduler_classes[0]
     UpperCAmelCase__ 						=				self.get_scheduler_config()
     UpperCAmelCase__ 						=				scheduler_class(**__lowercase			)

     scheduler.set_timesteps(self.num_inference_steps   ,			device=__lowercase			)

     UpperCAmelCase__ 						=				self.dummy_model()
     UpperCAmelCase__ 						=				self.dummy_sample_deter.to(__lowercase			) * scheduler.init_noise_sigma

     for t in scheduler.timesteps:
      UpperCAmelCase__ 						=				scheduler.scale_model_input(__lowercase   ,			__lowercase			)

      UpperCAmelCase__ 						=				model(__lowercase   ,			__lowercase			)

      UpperCAmelCase__ 						=				scheduler.step(__lowercase   ,			__lowercase   ,			__lowercase			)
      UpperCAmelCase__ 						=				output.prev_sample

     UpperCAmelCase__ 						=				torch.sum(torch.abs(__lowercase			)			)
     UpperCAmelCase__ 						=				torch.mean(torch.abs(__lowercase			)			)

     if str(__lowercase			).startswith("""cpu"""			):
      # The following sum varies between 148 and 156 on mps. Why?
      assert abs(result_sum.item() - 0.1_233			) < 1e-2
      assert abs(result_mean.item() - 0.0_002			) < 1e-3
     elif str(__lowercase			).startswith("""mps"""			):
      # Larger tolerance on mps
      assert abs(result_mean.item() - 0.0_002			) < 1e-2
     else:
      # CUDA
      assert abs(result_sum.item() - 0.1_233			) < 1e-2
      assert abs(result_mean.item() - 0.0_002			) < 1e-3






    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.scheduler_classes[0]
     UpperCAmelCase__ 						=				self.get_scheduler_config()
     UpperCAmelCase__ 						=				scheduler_class(**__lowercase   ,			use_karras_sigmas=__lowercase			)

     scheduler.set_timesteps(self.num_inference_steps   ,			device=__lowercase			)

     UpperCAmelCase__ 						=				self.dummy_model()
     UpperCAmelCase__ 						=				self.dummy_sample_deter.to(__lowercase			) * scheduler.init_noise_sigma
     UpperCAmelCase__ 						=				sample.to(__lowercase			)

     for t in scheduler.timesteps:
      UpperCAmelCase__ 						=				scheduler.scale_model_input(__lowercase   ,			__lowercase			)

      UpperCAmelCase__ 						=				model(__lowercase   ,			__lowercase			)

      UpperCAmelCase__ 						=				scheduler.step(__lowercase   ,			__lowercase   ,			__lowercase			)
      UpperCAmelCase__ 						=				output.prev_sample

     UpperCAmelCase__ 						=				torch.sum(torch.abs(__lowercase			)			)
     UpperCAmelCase__ 						=				torch.mean(torch.abs(__lowercase			)			)

     assert abs(result_sum.item() - 0.00_015			) < 1e-2
     assert abs(result_mean.item() - 1.9869_5545_3503_4695e-07			) < 1e-2











"""simple docstring"""



from __future__ import annotations

from math import pi, pow




def       snake_case__		(   _SCREAMING_SNAKE_CASE			)    ->float:
 if side_length < 0:
  raise ValueError("""vol_cube() only accepts non-negative values"""			)
 return pow(_SCREAMING_SNAKE_CASE       ,	3			)




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:
 if height < 0 or radius < 0:
  raise ValueError("""vol_spherical_cap() only accepts non-negative values"""			)
 # Volume is 1/3 pi * height squared * (3 * radius - height)
 return 1 / 3 * pi * pow(_SCREAMING_SNAKE_CASE       ,	2			) * (3 * radius - height)




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:
 if radius_a < 0 or radius_a < 0 or centers_distance < 0:
  raise ValueError("""vol_spheres_intersect() only accepts non-negative values"""			)
 if centers_distance == 0:
  return vol_sphere(min(_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)			)

 UpperCAmelCase__ 						=				(
     (radius_a - radius_a + centers_distance)
     * (radius_a + radius_a - centers_distance)
     / (2 * centers_distance)
 )
 UpperCAmelCase__ 						=				(
     (radius_a - radius_a + centers_distance)
     * (radius_a + radius_a - centers_distance)
     / (2 * centers_distance)
 )

 return vol_spherical_cap(_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			) + vol_spherical_cap(_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:

 if radius_a <= 0 or radius_a <= 0 or centers_distance < 0:
  raise ValueError(
      """vol_spheres_union() only accepts non-negative values, non-zero radius"""			)

 if centers_distance == 0:
  return vol_sphere(max(_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)			)

 return (
     vol_sphere(_SCREAMING_SNAKE_CASE			)
     + vol_sphere(_SCREAMING_SNAKE_CASE			)
     - vol_spheres_intersect(_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)
 )




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:
 if width < 0 or height < 0 or length < 0:
  raise ValueError("""vol_cuboid() only accepts non-negative values"""			)
 return float(width * height * length			)




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:
 if height < 0 or area_of_base < 0:
  raise ValueError("""vol_cone() only accepts non-negative values"""			)
 return area_of_base * height / 3.0




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:
 if height < 0 or radius < 0:
  raise ValueError("""vol_right_circ_cone() only accepts non-negative values"""			)
 return pi * pow(_SCREAMING_SNAKE_CASE       ,	2			) * height / 3.0




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:
 if height < 0 or area_of_base < 0:
  raise ValueError("""vol_prism() only accepts non-negative values"""			)
 return float(area_of_base * height			)




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:
 if height < 0 or area_of_base < 0:
  raise ValueError("""vol_pyramid() only accepts non-negative values"""			)
 return area_of_base * height / 3.0




def       snake_case__		(   _SCREAMING_SNAKE_CASE			)    ->float:
 if radius < 0:
  raise ValueError("""vol_sphere() only accepts non-negative values"""			)
 # Volume is 4/3 * pi * radius cubed
 return 4 / 3 * pi * pow(_SCREAMING_SNAKE_CASE       ,	3			)




def       snake_case__		(   _SCREAMING_SNAKE_CASE			)    ->float:
 if radius < 0:
  raise ValueError("""vol_hemisphere() only accepts non-negative values"""			)
 # Volume is radius cubed * pi * 2/3
 return pow(_SCREAMING_SNAKE_CASE       ,	3			) * pi * 2 / 3




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:
 if height < 0 or radius < 0:
  raise ValueError("""vol_circular_cylinder() only accepts non-negative values"""			)
 # Volume is radius squared * height * pi
 return pow(_SCREAMING_SNAKE_CASE       ,	2			) * height * pi




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:
 # Volume - (outer_radius squared - inner_radius squared) * pi * height
 if inner_radius < 0 or outer_radius < 0 or height < 0:
  raise ValueError(
      """vol_hollow_circular_cylinder() only accepts non-negative values"""			)
 if outer_radius <= inner_radius:
  raise ValueError("""outer_radius must be greater than inner_radius"""			)
 return pi * (pow(_SCREAMING_SNAKE_CASE       ,	2			) - pow(_SCREAMING_SNAKE_CASE       ,	2			)) * height




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:
 # Volume is 1/3 * pi * height *
 #           (radius_1 squared + radius_2 squared + radius_1 * radius_2)
 if radius_a < 0 or radius_a < 0 or height < 0:
  raise ValueError("""vol_conical_frustum() only accepts non-negative values"""			)
 return (
     1
     / 3
     * pi
     * height
     * (pow(_SCREAMING_SNAKE_CASE       ,	2			) + pow(_SCREAMING_SNAKE_CASE       ,	2			) + radius_a * radius_a)
 )




def       snake_case__		(   _SCREAMING_SNAKE_CASE       ,	_SCREAMING_SNAKE_CASE			)    ->float:
 if torus_radius < 0 or tube_radius < 0:
  raise ValueError("""vol_torus() only accepts non-negative values"""			)
 return 2 * pow(_SCREAMING_SNAKE_CASE       ,	2			) * torus_radius * pow(_SCREAMING_SNAKE_CASE       ,	2			)




def       snake_case__		(   )    ->str:
 print("""Volumes:"""			)
 print(F'''Cube: {vol_cube(2			) = }'''			)  # = 8
 print(F'''Cuboid: {vol_cuboid(2       ,	2       ,	2			) = }'''			)  # = 8
 print(F'''Cone: {vol_cone(2       ,	2			) = }'''			)  # ~= 1.33
 print(F'''Right Circular Cone: {vol_right_circ_cone(2       ,	2			) = }'''			)  # ~= 8.38
 print(F'''Prism: {vol_prism(2       ,	2			) = }'''			)  # = 4
 print(F'''Pyramid: {vol_pyramid(2       ,	2			) = }'''			)  # ~= 1.33
 print(F'''Sphere: {vol_sphere(2			) = }'''			)  # ~= 33.5
 print(F'''Hemisphere: {vol_hemisphere(2			) = }'''			)  # ~= 16.75
 print(F'''Circular Cylinder: {vol_circular_cylinder(2       ,	2			) = }'''			)  # ~= 25.1
 print(F'''Torus: {vol_torus(2       ,	2			) = }'''			)  # ~= 157.9
 print(F'''Conical Frustum: {vol_conical_frustum(2       ,	2       ,	4			) = }'''			)  # ~= 58.6
 print(F'''Spherical cap: {vol_spherical_cap(1       ,	2			) = }'''			)  # ~= 5.24
 print(F'''Spheres intersetion: {vol_spheres_intersect(2       ,	2       ,	1			) = }'''			)  # ~= 21.21
 print(F'''Spheres union: {vol_spheres_union(2       ,	2       ,	1			) = }'''			)  # ~= 45.81
 print(
     F'''Hollow Circular Cylinder: {vol_hollow_circular_cylinder(1       ,	2       ,	3			) = }'''			)  # ~= 28.3


if __name__ == "__main__":
 main()



