



from manim import *






class      UpperCamelCase    (						_UpperCAmelCase     ):



	def 			__A							(       self		):
							A__							=       Rectangle(height=0.5			,	width=0.5		)
							A__							=       Rectangle(height=0.46			,	width=0.46		).set_stroke(width=0		)

							A__							=       Rectangle(height=0.25			,	width=0.25		)

							A__							=       [mem.copy() for i in range(6		)]
							A__							=       [mem.copy() for i in range(6		)]
							A__							=       VGroup(*UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0		)
							A__							=       VGroup(*UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0		)
							A__							=       VGroup(UpperCAmelCase__			,	UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0		)
							A__							=       Text("CPU"			,	font_size=24		)
							A__							=       Group(UpperCAmelCase__			,	UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0.5			,	aligned_edge=UpperCAmelCase__		)
							cpu.move_to([-2.5, -0.5, 0]		)
							self.add(UpperCAmelCase__		)

							A__							=       [mem.copy() for i in range(4		)]
							A__							=       VGroup(*UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0		)
							A__							=       Text("GPU"			,	font_size=24		)
							A__							=       Group(UpperCAmelCase__			,	UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0.5			,	aligned_edge=UpperCAmelCase__		)
							gpu.move_to([-1, -1, 0]		)
							self.add(UpperCAmelCase__		)

							A__							=       [mem.copy() for i in range(6		)]
							A__							=       VGroup(*UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0		)

							A__							=       Text("Model"			,	font_size=24		)
							A__							=       Group(UpperCAmelCase__			,	UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0.5			,	aligned_edge=UpperCAmelCase__		)
							model.move_to([3, -1.0, 0]		)
							self.add(UpperCAmelCase__		)

							A__							=       []
							A__							=       []

							for i, rect in enumerate(UpperCAmelCase__		):
													A__							=       fill.copy().set_fill(UpperCAmelCase__			,	opacity=0.8		)
													target.move_to(UpperCAmelCase__		)
													model_arr.append(UpperCAmelCase__		)

													A__							=       Rectangle(height=0.46			,	width=0.46		).set_stroke(width=0.0		).set_fill(UpperCAmelCase__			,	opacity=0.8		)
													cpu_target.move_to(cpu_left_col_base[i]		)
													model_cpu_arr.append(UpperCAmelCase__		)

							self.add(*UpperCAmelCase__			,	*UpperCAmelCase__		)

							A__							=       [meta_mem.copy() for i in range(6		)]
							A__							=       [meta_mem.copy() for i in range(6		)]
							A__							=       VGroup(*UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0		)
							A__							=       VGroup(*UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0		)
							A__							=       VGroup(UpperCAmelCase__			,	UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0		)
							A__							=       Text("Disk"			,	font_size=24		)
							A__							=       Group(UpperCAmelCase__			,	UpperCAmelCase__		).arrange(UpperCAmelCase__			,	buff=0.5			,	aligned_edge=UpperCAmelCase__		)
							disk.move_to([-4, -1.25, 0]		)
							self.add(UpperCAmelCase__			,	UpperCAmelCase__		)

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

							A__							=       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__		)

							A__							=       MarkupText(
							    F"""<span fgcolor='{BLUE}'>●</span> Checkpoint"""			,	font_size=18			,	)

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

							A__							=       MarkupText(
							    F"""Now watch as an input is passed through the model\nand how the memory is utilized and handled."""			,	font_size=24			,	)
							step_a.move_to([2, 2, 0]		)

							self.play(Write(UpperCAmelCase__		)		)

							A__							=       Square(0.3		)
							input.set_fill(UpperCAmelCase__			,	opacity=1.0		)
							input.set_stroke(width=0.0		)
							input.next_to(model_base[0]			,	UpperCAmelCase__			,	buff=0.5		)

							self.play(Write(UpperCAmelCase__		)		)

							input.generate_target()
							input.target.next_to(model_arr[0]			,	direction=UpperCAmelCase__			,	buff=0.02		)
							self.play(MoveToTarget(UpperCAmelCase__		)		)

							self.play(FadeOut(UpperCAmelCase__		)		)

							A__							=       Arrow(start=UpperCAmelCase__			,	end=UpperCAmelCase__			,	color=UpperCAmelCase__			,	buff=0.5		)
							a.next_to(model_arr[0].get_left()			,	UpperCAmelCase__			,	buff=0.2		)

							model_cpu_arr[0].generate_target()
							model_cpu_arr[0].target.move_to(gpu_rect[0]		)

							A__							=       MarkupText(
							    F"""As the input reaches a layer, the hook triggers\nand weights are moved from the CPU\nto the GPU and back."""			,	font_size=24			,	)
							step_a.move_to([2, 2, 0]		)

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

							A__							=       {"run_time": 1, "fade_in": True, "fade_out": True, "buff": 0.02}

							self.play(
							    Write(UpperCAmelCase__		)			,	Circumscribe(model_arr[0]			,	color=UpperCAmelCase__			,	**UpperCAmelCase__		)			,	Circumscribe(model_cpu_arr[0]			,	color=UpperCAmelCase__			,	**UpperCAmelCase__		)			,	Circumscribe(gpu_rect[0]			,	color=UpperCAmelCase__			,	**UpperCAmelCase__		)			,	)
							self.play(MoveToTarget(model_cpu_arr[0]		)		)

							A__							=       a.copy()
							for i in range(6		):
													a_c.next_to(model_arr[i].get_right() + 0.02			,	UpperCAmelCase__			,	buff=0.2		)

													input.generate_target()
													input.target.move_to(model_arr[i].get_right() + 0.02		)

													A__							=       AnimationGroup(
													    FadeOut(UpperCAmelCase__			,	run_time=0.5		)			,	MoveToTarget(UpperCAmelCase__			,	run_time=0.5		)			,	FadeIn(UpperCAmelCase__			,	run_time=0.5		)			,	lag_ratio=0.2		)

													self.play(UpperCAmelCase__		)

													model_cpu_arr[i].generate_target()
													model_cpu_arr[i].target.move_to(cpu_left_col_base[i]		)

													if i < 5:
																			model_cpu_arr[i + 1].generate_target()
																			model_cpu_arr[i + 1].target.move_to(gpu_rect[0]		)
																			if i >= 1:
																									A__							=       0.7

																			self.play(
																			    Circumscribe(model_arr[i]			,	**UpperCAmelCase__		)			,	Circumscribe(cpu_left_col_base[i]			,	**UpperCAmelCase__		)			,	Circumscribe(cpu_left_col_base[i + 1]			,	color=UpperCAmelCase__			,	**UpperCAmelCase__		)			,	Circumscribe(gpu_rect[0]			,	color=UpperCAmelCase__			,	**UpperCAmelCase__		)			,	Circumscribe(model_arr[i + 1]			,	color=UpperCAmelCase__			,	**UpperCAmelCase__		)			,	)
																			if i < 1:
																									self.play(
																									    MoveToTarget(model_cpu_arr[i]		)			,	MoveToTarget(model_cpu_arr[i + 1]		)			,	)
																			else:
																									self.play(
																									    MoveToTarget(model_cpu_arr[i]			,	run_time=0.7		)			,	MoveToTarget(model_cpu_arr[i + 1]			,	run_time=0.7		)			,	)
													else:
																			model_cpu_arr[i].generate_target()
																			model_cpu_arr[i].target.move_to(cpu_left_col_base[-1]		)
																			input.generate_target()
																			input.target.next_to(model_arr[-1].get_right()			,	RIGHT + 0.02			,	buff=0.2		)

																			self.play(
																			    Circumscribe(model_arr[-1]			,	color=UpperCAmelCase__			,	**UpperCAmelCase__		)			,	Circumscribe(cpu_left_col_base[-1]			,	color=UpperCAmelCase__			,	**UpperCAmelCase__		)			,	Circumscribe(gpu_rect[0]			,	color=UpperCAmelCase__			,	**UpperCAmelCase__		)			,	)

																			self.play(MoveToTarget(model_cpu_arr[i]		)		)

													A__							=       a_c
													A__							=       a_c.copy()

							input.generate_target()
							input.target.next_to(model_base[-1]			,	RIGHT + 0.02			,	buff=0.5		)
							self.play(
							    FadeOut(UpperCAmelCase__		)			,	FadeOut(UpperCAmelCase__			,	run_time=0.5		)			,	)

							A__							=       MarkupText(F"""Inference on a model too large for GPU memory\nis successfully completed."""			,	font_size=24		)
							step_a.move_to([2, 2, 0]		)

							self.play(Write(UpperCAmelCase__			,	run_time=3		)			,	MoveToTarget(UpperCAmelCase__		)		)

							self.wait()










import math
def  UpperCamelCase						(       _A			:				float			,							_A			:				float    )->       float:





						"""simple docstring"""





						if (
						    not isinstance(_A			,							(int, float)    )
						    or power_factor < -1
						    or power_factor > 1
						):
												raise ValueError("power_factor must be a valid float value between -1 and 1."    )
						return apparent_power * power_factor
def  UpperCamelCase						(       _A			:				float			,							_A			:				float    )->       float:





						"""simple docstring"""





						if (
						    not isinstance(_A			,							(int, float)    )
						    or power_factor < -1
						    or power_factor > 1
						):
												raise ValueError("power_factor must be a valid float value between -1 and 1."    )
						return apparent_power * math.sqrt(1 - power_factor**2    )


if __name__ == "__main__":
						import doctest

						doctest.testmod()





