





"""simple docstring"""




from manim import *




class 					_a		( _lowerCAmelCase							):






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




										'''simple docstring'''



										_UpperCamelCase     :   Optional[Any]      =       Rectangle(height=0.5,					width=0.5				)
										_UpperCamelCase     :   List[str]      =       Rectangle(height=0.46,					width=0.46				).set_stroke(width=0				)
										_UpperCamelCase     :   Tuple      =       Rectangle(height=0.25,					width=0.25				)

										_UpperCamelCase     :   str      =       [mem.copy() for i in range(6				)]
										_UpperCamelCase     :   Optional[int]      =       [mem.copy() for i in range(6				)]
										_UpperCamelCase     :   List[Any]      =       VGroup(*lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0				)
										_UpperCamelCase     :   int      =       VGroup(*lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0				)
										_UpperCamelCase     :   Optional[Any]      =       VGroup(lowerCAmelCase__,					lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0				)
										_UpperCamelCase     :   Union[str, Any]      =       Text('''CPU''',					font_size=2_4				)
										_UpperCamelCase     :   Optional[Any]      =       Group(lowerCAmelCase__,					lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0.5,					aligned_edge=lowerCAmelCase__				)
										cpu.move_to([-2.5, -0.5, 0]				)
										self.add(lowerCAmelCase__				)

										_UpperCamelCase     :   Union[str, Any]      =       [mem.copy() for i in range(4				)]
										_UpperCamelCase     :   List[str]      =       VGroup(*lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0				)
										_UpperCamelCase     :   Optional[int]      =       Text('''GPU''',					font_size=2_4				)
										_UpperCamelCase     :   List[str]      =       Group(lowerCAmelCase__,					lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0.5,					aligned_edge=lowerCAmelCase__				)
										gpu.move_to([-1, -1, 0]				)
										self.add(lowerCAmelCase__				)

										_UpperCamelCase     :   str      =       [mem.copy() for i in range(6				)]
										_UpperCamelCase     :   Any      =       VGroup(*lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0				)

										_UpperCamelCase     :   Any      =       Text('''Model''',					font_size=2_4				)
										_UpperCamelCase     :   List[str]      =       Group(lowerCAmelCase__,					lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0.5,					aligned_edge=lowerCAmelCase__				)
										model.move_to([3, -1.0, 0]				)
										self.add(lowerCAmelCase__				)

										_UpperCamelCase     :   str      =       []
										_UpperCamelCase     :   Union[str, Any]      =       []

										for i, rect in enumerate(lowerCAmelCase__				):
																rect.set_stroke(lowerCAmelCase__				)

																_UpperCamelCase     :   Any      =       Rectangle(height=0.46 / 4,					width=0.46 / 3				).set_stroke(width=0.0				).set_fill(lowerCAmelCase__,					opacity=0.7				)

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

										self.add(*lowerCAmelCase__,					*lowerCAmelCase__				)

										_UpperCamelCase     :   Optional[Any]      =       [meta_mem.copy() for i in range(6				)]
										_UpperCamelCase     :   int      =       [meta_mem.copy() for i in range(6				)]
										_UpperCamelCase     :   Tuple      =       VGroup(*lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0				)
										_UpperCamelCase     :   Any      =       VGroup(*lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0				)
										_UpperCamelCase     :   Any      =       VGroup(lowerCAmelCase__,					lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0				)
										_UpperCamelCase     :   List[Any]      =       Text('''Disk''',					font_size=2_4				)
										_UpperCamelCase     :   int      =       Group(lowerCAmelCase__,					lowerCAmelCase__				).arrange(lowerCAmelCase__,					buff=0.5,					aligned_edge=lowerCAmelCase__				)
										disk.move_to([-4.0, -1.25, 0]				)
										self.add(lowerCAmelCase__,					lowerCAmelCase__				)

										_UpperCamelCase     :   List[str]      =       []

										for i in range(6				):
																_UpperCamelCase     :   str      =       fill.copy().set_fill(lowerCAmelCase__,					opacity=0.8				)
																target.move_to(disk_left_col_base[i]				).scale(0.5				)
																cpu_disk_arr.append(lowerCAmelCase__				)

										self.add(*lowerCAmelCase__				)

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

										_UpperCamelCase     :   int      =       MarkupText(
										    f"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model""",					font_size=1_8,					)

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

										self.add(lowerCAmelCase__,					lowerCAmelCase__				)

										_UpperCamelCase     :   str      =       MarkupText(
										    f"""<span fgcolor='{BLUE}'>●</span> Checkpoint""",					font_size=1_8,					)

										blue_text.next_to(lowerCAmelCase__,					DOWN * 2.4,					aligned_edge=key_text.get_left()				)
										self.add(lowerCAmelCase__				)

										_UpperCamelCase     :   Any      =       MarkupText(f"""The offloaded weights are all sent to the CPU.""",					font_size=2_4				)
										step_a.move_to([2, 2, 0]				)

										self.play(Write(lowerCAmelCase__,					run_time=3				)				)

										for i in range(6				):
																_UpperCamelCase     :   Any      =       cpu_disk_arr[i]
																_UpperCamelCase     :   Any      =       rect.copy().set_fill(lowerCAmelCase__,					opacity=0.8				).scale(2.0				)
																cpa.generate_target()
																cpa.target.move_to(model_base[i]				)

																if i == 0:
																						rect.set_fill(lowerCAmelCase__,					opacity=0.8				)
																						rect.generate_target()
																						rect.target.move_to(cpu_left_col_base[0]				).scale(2.0				)

																						self.remove(
																						    *lowerCAmelCase__,					*lowerCAmelCase__,					)

																else:
																						rect.generate_target()
																						rect.target.move_to(cpu_left_col_base[i]				).scale(2.0				)
																self.play(MoveToTarget(lowerCAmelCase__				),					MoveToTarget(lowerCAmelCase__				),					model_base[i].animate.set_stroke(lowerCAmelCase__				)				)
										self.play(FadeOut(lowerCAmelCase__				)				)

										_UpperCamelCase     :   Dict      =       MarkupText(
										    f"""Finally, hooks are added to each weight in the model\nto transfer the weights from CPU to GPU\n\t\tand back when needed.""",					font_size=2_4,					)
										step_a.move_to([2, 2, 0]				)

										self.play(Write(lowerCAmelCase__,					run_time=3				)				)

										_UpperCamelCase     :   Dict      =       []
										_UpperCamelCase     :   Any      =       []
										for i in range(6				):
																_UpperCamelCase     :   Optional[Any]      =       Arrow(start=lowerCAmelCase__,					end=lowerCAmelCase__,					color=lowerCAmelCase__,					buff=0.5				)
																a.next_to(model_base[i].get_left(),					lowerCAmelCase__,					buff=0.2				)
																arrows.append(lowerCAmelCase__				)
																animations.append(Write(lowerCAmelCase__				)				)
										self.play(*lowerCAmelCase__				)
										self.wait()











from __future__ import annotations

from collections.abc import Iterable, Iterator
from typing import Any, Generic, TypeVar

__snake_case     :     str       =  TypeVar("""T""", bound=bool)


class      __SCREAMING_SNAKE_CASE       ( Generic[T]):






						def __init__(						self  ,    _UpperCamelCase ):






												"""simple docstring"""
												lowerCAmelCase__         =    value
												lowerCAmelCase__         =    None
												lowerCAmelCase__         =    None






						@property
						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												return self._value







						@staticmethod
						def    UpperCamelCase__   (						_UpperCamelCase  ,    _UpperCamelCase ):






												"""simple docstring"""
												if not roota:
																		return roota

												if not roota:
																		return roota

												if roota.value > roota.value:
																		lowerCAmelCase__    ,							lowerCAmelCase__         =    roota, roota

												lowerCAmelCase__         =    roota
												lowerCAmelCase__         =    roota.right
												lowerCAmelCase__         =    roota.left
												lowerCAmelCase__         =    SkewNode.merge(_UpperCamelCase  ,    _UpperCamelCase )

												return result




class      __SCREAMING_SNAKE_CASE       ( Generic[T]):






						def __init__(						self  ,    _UpperCamelCase = () ):






												"""simple docstring"""
												lowerCAmelCase__         =    None
												if data:
																		for item in data:
																								self.insert(_UpperCamelCase )






						def __bool__(						self ):






												"""simple docstring"""
												return self._root is not None






						def __iter__(						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    []
												while self:
																		result.append(self.pop() )

												# Pushing items back to the heap not to clear it.
												for item in result:
																		self.insert(_UpperCamelCase )

												return iter(_UpperCamelCase )






						def    UpperCamelCase__   (						self  ,    _UpperCamelCase ):






												"""simple docstring"""
												lowerCAmelCase__         =    SkewNode.merge(self._root  ,    SkewNode(_UpperCamelCase ) )






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    self.top()
												lowerCAmelCase__         =    (
												    SkewNode.merge(self._root.left  ,    self._root.right ) if self._root else None
												)

												return result






						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												if not self._root:
																		raise IndexError('Can\'t get top element for the empty heap.' )
												return self._root.value







						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    None


if __name__ == "__main__":
					import doctest

					doctest.testmod()





