


"""simple docstring"""



from manim import *







class _lowerCAmelCase							( snake_case_      ):







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

       '''simple docstring'''




       snake_case       : Union[str, Any]    =       Rectangle(height=0.5		,      width=0.5		)
       snake_case       : Any    =       Rectangle(height=0.46		,      width=0.46		).set_stroke(width=0		)

       snake_case       : Union[str, Any]    =       [mem.copy() for i in range(6		)]
       snake_case       : str    =       [mem.copy() for i in range(6		)]
       snake_case       : List[str]    =       VGroup(*UpperCamelCase__		).arrange(UpperCamelCase__		,      buff=0		)
       snake_case       : List[Any]    =       VGroup(*UpperCamelCase__		).arrange(UpperCamelCase__		,      buff=0		)
       snake_case       : int    =       VGroup(UpperCamelCase__		,      UpperCamelCase__		).arrange(UpperCamelCase__		,      buff=0		)
       snake_case       : List[str]    =       Text("CPU"		,      font_size=24		)
       snake_case       : str    =       Group(UpperCamelCase__		,      UpperCamelCase__		).arrange(UpperCamelCase__		,      buff=0.5		,      aligned_edge=UpperCamelCase__		)
       cpu.move_to([-2.5, -0.5, 0]		)
       self.add(UpperCamelCase__		)

       snake_case       : List[Any]    =       [mem.copy() for i in range(4		)]
       snake_case       : Tuple    =       VGroup(*UpperCamelCase__		).arrange(UpperCamelCase__		,      buff=0		)
       snake_case       : Optional[int]    =       Text("GPU"		,      font_size=24		)
       snake_case       : List[str]    =       Group(UpperCamelCase__		,      UpperCamelCase__		).arrange(UpperCamelCase__		,      buff=0.5		,      aligned_edge=UpperCamelCase__		)
       gpu.move_to([-1, -1, 0]		)
       self.add(UpperCamelCase__		)

       snake_case       : List[Any]    =       [mem.copy() for i in range(6		)]
       snake_case       : int    =       VGroup(*UpperCamelCase__		).arrange(UpperCamelCase__		,      buff=0		)

       snake_case       : Union[str, Any]    =       Text("Model"		,      font_size=24		)
       snake_case       : Dict    =       Group(UpperCamelCase__		,      UpperCamelCase__		).arrange(UpperCamelCase__		,      buff=0.5		,      aligned_edge=UpperCamelCase__		)
       model.move_to([3, -1.0, 0]		)
       self.add(UpperCamelCase__		)

       snake_case       : Optional[int]    =       []
       for i, rect in enumerate(UpperCamelCase__		):
           rect.set_stroke(UpperCamelCase__		)
           # target = fill.copy().set_fill(YELLOW, opacity=0.7)
           # target.move_to(rect)
           # self.add(target)

           snake_case       : str    =       Rectangle(height=0.46 / 4		,      width=0.46 / 3		).set_stroke(width=0.0		).set_fill(UpperCamelCase__		,      opacity=0.7		)

           if i == 0:
               cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT		)		,      buff=0.02		,      direction=UpperCamelCase__		)
               cpu_target.set_x(cpu_target.get_x() + 0.1		)
           elif i == 3:
               cpu_target.next_to(cpu_targs[0]		,      direction=UpperCamelCase__		,      buff=0.0		)
           else:
               cpu_target.next_to(cpu_targs[i - 1]		,      direction=UpperCamelCase__		,      buff=0.0		)
           self.add(UpperCamelCase__		)
           cpu_targs.append(UpperCamelCase__		)

       snake_case       : List[Any]    =       [mem.copy() for i in range(6		)]
       snake_case       : int    =       VGroup(*UpperCamelCase__		).arrange(UpperCamelCase__		,      buff=0		)

       snake_case       : Optional[int]    =       Text("Loaded Checkpoint"		,      font_size=24		)
       snake_case       : Dict    =       Group(UpperCamelCase__		,      UpperCamelCase__		).arrange(UpperCamelCase__		,      aligned_edge=UpperCamelCase__		,      buff=0.4		)
       checkpoint.move_to([3, 0.5, 0]		)

       snake_case       : Tuple    =       Square(side_length=2.2		)
       key.move_to([-5, 2, 0]		)

       snake_case       : Dict    =       MarkupText(
           F'<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'		,      font_size=18		,      )

       key_text.move_to([-5, 2.4, 0]		)

       self.add(UpperCamelCase__		,      UpperCamelCase__		)

       snake_case       : str    =       MarkupText(
           F'<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'		,      font_size=18		,      )

       blue_text.next_to(UpperCamelCase__		,      DOWN * 2.4		,      aligned_edge=key_text.get_left()		)

       snake_case       : int    =       MarkupText(
           F'Next, a <i><span fgcolor="{BLUE}">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor="{BLUE}">single shard</span>.'		,      font_size=24		,      )
       step_a.move_to([2, 2, 0]		)
       self.play(Write(UpperCamelCase__		)		,      Write(UpperCamelCase__		)		)

       self.play(Write(UpperCamelCase__		,      run_time=1		)		,      Create(UpperCamelCase__		,      run_time=1		)		)

       snake_case       : Any    =       []
       snake_case       : Any    =       []
       for i, rect in enumerate(UpperCamelCase__		):
           snake_case       : Dict    =       fill.copy().set_fill(UpperCamelCase__		,      opacity=0.7		)
           target.move_to(UpperCamelCase__		)
           first_animations.append(GrowFromCenter(UpperCamelCase__		,      run_time=1		)		)

           snake_case       : str    =       target.copy()
           cpu_target.generate_target()
           if i < 5:
               cpu_target.target.move_to(cpu_left_col_base[i + 1]		)
           else:
               cpu_target.target.move_to(cpu_right_col_base[i - 5]		)
           second_animations.append(MoveToTarget(UpperCamelCase__		,      run_time=1.5		)		)

       self.play(*UpperCamelCase__		)
       self.play(*UpperCamelCase__		)
       self.wait()









"""simple docstring"""



import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest







class _lowerCAmelCase							( snake_case_      ):
   __UpperCAmelCase					:			List[str]				    =    (KDPMaDiscreteScheduler,)
   __UpperCAmelCase					:			List[Any]				    =    10







   def 					lowerCamelCase						(      self		,      **UpperCamelCase__		)      ->						Optional[int]:

       '''simple docstring'''




       snake_case       : List[str]    =       {
           "num_train_timesteps": 1100,
           "beta_start": 0.0001,
           "beta_end": 0.02,
           "beta_schedule": "linear",
       }

       config.update(**UpperCamelCase__		)
       return config







   def 					lowerCamelCase						(      self		)      ->						Optional[int]:

       '''simple docstring'''




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







   def 					lowerCamelCase						(      self		)      ->						str:

       '''simple docstring'''




       for beta_start, beta_end in zip([0.00001, 0.0001, 0.001]		,      [0.0002, 0.002, 0.02]		):
           self.check_over_configs(beta_start=UpperCamelCase__		,      beta_end=UpperCamelCase__		)







   def 					lowerCamelCase						(      self		)      ->						Optional[Any]:

       '''simple docstring'''




       for schedule in ["linear", "scaled_linear"]:
           self.check_over_configs(beta_schedule=UpperCamelCase__		)







   def 					lowerCamelCase						(      self		)      ->						List[str]:

       '''simple docstring'''




       for prediction_type in ["epsilon", "v_prediction"]:
           self.check_over_configs(prediction_type=UpperCamelCase__		)







   def 					lowerCamelCase						(      self		)      ->						Optional[Any]:

       '''simple docstring'''




       snake_case       : str    =       self.scheduler_classes[0]
       snake_case       : str    =       self.get_scheduler_config(prediction_type="v_prediction"		)
       snake_case       : Dict    =       scheduler_class(**UpperCamelCase__		)

       scheduler.set_timesteps(self.num_inference_steps		)

       snake_case       : List[Any]    =       self.dummy_model()
       snake_case       : List[str]    =       self.dummy_sample_deter * scheduler.init_noise_sigma
       snake_case       : Optional[Any]    =       sample.to(UpperCamelCase__		)

       for i, t in enumerate(scheduler.timesteps		):
           snake_case       : int    =       scheduler.scale_model_input(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : int    =       model(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : Optional[Any]    =       scheduler.step(UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		)
           snake_case       : Optional[Any]    =       output.prev_sample

       snake_case       : Optional[Any]    =       torch.sum(torch.abs(UpperCamelCase__		)		)
       snake_case       : Any    =       torch.mean(torch.abs(UpperCamelCase__		)		)

       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.0002		) < 1e-3







   def 					lowerCamelCase						(      self		)      ->						Dict:

       '''simple docstring'''




       if torch_device == "mps":
           return
       snake_case       : Optional[Any]    =       self.scheduler_classes[0]
       snake_case       : Optional[int]    =       self.get_scheduler_config()
       snake_case       : Dict    =       scheduler_class(**UpperCamelCase__		)

       scheduler.set_timesteps(self.num_inference_steps		)

       snake_case       : List[Any]    =       self.dummy_model()
       snake_case       : Union[str, Any]    =       self.dummy_sample_deter * scheduler.init_noise_sigma
       snake_case       : List[str]    =       sample.to(UpperCamelCase__		)

       for i, t in enumerate(scheduler.timesteps		):
           snake_case       : List[Any]    =       scheduler.scale_model_input(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : Optional[int]    =       model(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : Optional[int]    =       scheduler.step(UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		)
           snake_case       : int    =       output.prev_sample

       snake_case       : int    =       torch.sum(torch.abs(UpperCamelCase__		)		)
       snake_case       : Union[str, Any]    =       torch.mean(torch.abs(UpperCamelCase__		)		)

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







   def 					lowerCamelCase						(      self		)      ->						Optional[int]:

       '''simple docstring'''




       if torch_device == "mps":
           return
       snake_case       : Any    =       self.scheduler_classes[0]
       snake_case       : Any    =       self.get_scheduler_config()
       snake_case       : Union[str, Any]    =       scheduler_class(**UpperCamelCase__		)

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

       snake_case       : Optional[Any]    =       self.dummy_model()
       snake_case       : Optional[int]    =       self.dummy_sample_deter.to(UpperCamelCase__		) * scheduler.init_noise_sigma

       for t in scheduler.timesteps:
           snake_case       : Optional[Any]    =       scheduler.scale_model_input(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : Any    =       model(UpperCamelCase__		,      UpperCamelCase__		)

           snake_case       : List[Any]    =       scheduler.step(UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		)
           snake_case       : Dict    =       output.prev_sample

       snake_case       : Dict    =       torch.sum(torch.abs(UpperCamelCase__		)		)
       snake_case       : str    =       torch.mean(torch.abs(UpperCamelCase__		)		)

       if str(UpperCamelCase__		).startswith("cpu"		):
           # The following sum varies between 148 and 156 on mps. Why?
           assert abs(result_sum.item() - 20.4125		) < 1e-2
           assert abs(result_mean.item() - 0.0266		) < 1e-3
       else:
           # CUDA
           assert abs(result_sum.item() - 20.4125		) < 1e-2
           assert abs(result_mean.item() - 0.0266		) < 1e-3





