
"""simple docstring"""





import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available

def 					_UpperCamelCase    (			*UpperCamelCase							)			->			List[str]:


			"""simple docstring"""



			if not isinstance(lowerCamelCase_				,       lowerCamelCase_							):
						__UpperCAmelCase	: Union[str, Any]		=					list(lowerCamelCase_							)
			for i in range(len(lowerCamelCase_							)							):
						__UpperCAmelCase	: Union[str, Any]		=					None
			gc.collect()
			if is_xpu_available():
						torch.xpu.empty_cache()
			elif is_npu_available():
						torch.npu.empty_cache()
			else:
						torch.cuda.empty_cache()
			return objects

def 					_UpperCamelCase    (			UpperCamelCase							)			->			str:


			"""simple docstring"""
			__UpperCAmelCase	: List[str]		=					[
			    """CUDA out of memory.""",  # CUDA OOM
			    """cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.""",  # CUDNN SNAFU
			    """DefaultCPUAllocator: can't allocate memory""",  # CPU OOM
			]
			if isinstance(lowerCamelCase_				,       lowerCamelCase_							) and len(exception.args							) == 1:
						return any(err in exception.args[0] for err in _statements							)
			return False





def 					_UpperCamelCase    (			UpperCamelCase = None				,       UpperCamelCase = 128							)			->			List[Any]:


			"""simple docstring"""



			if function is None:
						return functools.partial(lowerCamelCase_				,       starting_batch_size=lowerCamelCase_							)

			__UpperCAmelCase	: str		=					starting_batch_size

			def decorator(*UpperCamelCase				,       **UpperCamelCase							):
						nonlocal batch_size
						gc.collect()
						if is_xpu_available():
									torch.xpu.empty_cache()
						elif is_npu_available():
									torch.npu.empty_cache()
						else:
									torch.cuda.empty_cache()
						__UpperCAmelCase	: int		=					list(inspect.signature(lowerCamelCase_							).parameters.keys()							)
						# Guard against user error
						if len(lowerCamelCase_							) < (len(lowerCamelCase_							) + 1):
									__UpperCAmelCase	: str		=					""", """.join([f"{arg}={value}" for arg, value in zip(params[1:]				,       args[1:]							)]							)
									raise TypeError(
									    f"Batch size was passed into `{function.__name__}` as the first argument when called."
									    f"Remove this as the decorator already does so: `{function.__name__}({arg_str})`"							)
						while True:
									if batch_size == 0:
												raise RuntimeError("No executable batch size found, reached zero."							)
									try:
												return function(lowerCamelCase_				,       *lowerCamelCase_				,       **lowerCamelCase_							)
									except Exception as e:
												if should_reduce_batch_size(lowerCamelCase_							):
															gc.collect()
															if is_xpu_available():
																		torch.xpu.empty_cache()
															elif is_npu_available():
																		torch.npu.empty_cache()
															else:
																		torch.cuda.empty_cache()
															batch_size //= 2
												else:
															raise

			return decorator








"""simple docstring"""





from manim import *



class      a__				(	__magic_name__   ):


			def       a_     (      self      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: List[str]		=					Rectangle(height=0.5      ,  width=0.5)
						__UpperCAmelCase	: List[str]		=					Rectangle(height=0.25      ,  width=0.25)
						__UpperCAmelCase	: Dict		=					Rectangle(height=0.46      ,  width=0.46).set_stroke(width=0)

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

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

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

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

						__UpperCAmelCase	: str		=					[]
						__UpperCAmelCase	: Tuple		=					[]
						__UpperCAmelCase	: Any		=					[]

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

									__UpperCAmelCase	: Optional[int]		=					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(model_cpu_arr[0]      ,  direction=UpperCamelCase_      ,  buff=0.0)
									else:
												cpu_target.next_to(model_cpu_arr[i - 1]      ,  direction=UpperCamelCase_      ,  buff=0.0)
									self.add(UpperCamelCase_)
									model_cpu_arr.append(UpperCamelCase_)

						self.add(*UpperCamelCase_      ,  *UpperCamelCase_      ,  *UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					[mem.copy() for i in range(6)]
						__UpperCAmelCase	: Dict		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)

						__UpperCAmelCase	: Tuple		=					Text("Loaded Checkpoint"      ,  font_size=24)
						__UpperCAmelCase	: Any		=					Group(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0.5      ,  aligned_edge=UpperCamelCase_)
						checkpoint.move_to([3, 0.5, 0])

						self.add(UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					[]
						__UpperCAmelCase	: int		=					[]

						for i, rect in enumerate(UpperCamelCase_):
									__UpperCAmelCase	: Optional[Any]		=					fill.copy().set_fill(UpperCamelCase_      ,  opacity=0.7)
									target.move_to(UpperCamelCase_)
									ckpt_arr.append(UpperCamelCase_)

									__UpperCAmelCase	: Optional[Any]		=					target.copy()
									if i < 5:
												cpu_target.move_to(cpu_left_col_base[i + 1])
									else:
												cpu_target.move_to(cpu_right_col_base[i - 5])
									ckpt_cpu_arr.append(UpperCamelCase_)
						self.add(*UpperCamelCase_      ,  *UpperCamelCase_)

						__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=18      ,  )

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

						self.add(UpperCamelCase_      ,  UpperCamelCase_)

						__UpperCAmelCase	: Optional[Any]		=					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_)

						__UpperCAmelCase	: Dict		=					MarkupText(
						    F"Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device."      ,  font_size=24      ,  )
						step_a.move_to([2, 2, 0])

						__UpperCAmelCase	: List[Any]		=					[meta_mem.copy() for i in range(6)]
						__UpperCAmelCase	: Optional[int]		=					[meta_mem.copy() for i in range(6)]
						__UpperCAmelCase	: int		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)
						__UpperCAmelCase	: Union[str, Any]		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)
						__UpperCAmelCase	: Tuple		=					VGroup(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)
						__UpperCAmelCase	: Optional[int]		=					Text("Disk"      ,  font_size=24)
						__UpperCAmelCase	: str		=					Group(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0.5      ,  aligned_edge=UpperCamelCase_)
						disk.move_to([-4.0, -1.25, 0])
						self.play(Write(UpperCamelCase_      ,  run_time=3)      ,  Write(UpperCamelCase_      ,  run_time=1)      ,  Create(UpperCamelCase_      ,  run_time=1))

						__UpperCAmelCase	: str		=					[]
						for i, rect in enumerate(UpperCamelCase_):
									__UpperCAmelCase	: Union[str, Any]		=					rect.copy()
									target.generate_target()
									target.target.move_to(disk_left_col_base[i]).scale(0.5)
									animations.append(MoveToTarget(UpperCamelCase_      ,  run_time=1.5))
						self.play(*UpperCamelCase_)

						self.play(FadeOut(UpperCamelCase_))

						__UpperCAmelCase	: Union[str, Any]		=					MarkupText(F"Then, the checkpoint is removed from memory\nthrough garbage collection."      ,  font_size=24)
						step_a.move_to([2, 2, 0])

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

						self.play(
						    FadeOut(UpperCamelCase_      ,  UpperCamelCase_      ,  *UpperCamelCase_      ,  *UpperCamelCase_)      ,  )

						self.wait()


