


from manim import *




class    UpperCAmelCase					(       __snake_case    ):




			def   lowerCamelCase_					(						self							:		Dict						):







						"""simple docstring"""







						UpperCamelCase         =					Rectangle(height=0.5       ,       width=0.5						)
						UpperCamelCase         =					Rectangle(height=0.46       ,       width=0.46						).set_stroke(width=0						)

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

						UpperCamelCase         =					[mem.copy() for i in range(1						)]
						UpperCamelCase         =					VGroup(*__magic_name__						).arrange(__magic_name__       ,       buff=0						)
						UpperCamelCase         =					Text("""GPU"""       ,       font_size=2_4						)
						UpperCamelCase         =					Group(__magic_name__       ,       __magic_name__						).arrange(__magic_name__       ,       buff=0.5       ,       aligned_edge=__magic_name__						)
						gpu.align_to(__magic_name__       ,       __magic_name__						)
						gpu.set_x(gpu.get_x() - 1						)

						self.add(__magic_name__						)

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

						UpperCamelCase         =					Text("""Model"""       ,       font_size=2_4						)
						UpperCamelCase         =					Group(__magic_name__       ,       __magic_name__						).arrange(__magic_name__       ,       buff=0.5       ,       aligned_edge=__magic_name__						)
						model.move_to([3, -1.0, 0]						)

						self.play(
						    Create(__magic_name__       ,       run_time=1						)       ,       Create(__magic_name__       ,       run_time=1						)       ,       Create(__magic_name__       ,       run_time=1						)       ,       )

						UpperCamelCase         =					MarkupText(
						    F'First, an empty model skeleton is loaded\ninto <span fgcolor=\'{YELLOW}\'>memory</span> without using much RAM.'       ,       font_size=2_4       ,       )

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

						UpperCamelCase         =					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]						)

						step_a.move_to([2, 2, 0]						)
						self.play(Write(__magic_name__       ,       run_time=2.5						)       ,       Write(__magic_name__						)       ,       Write(__magic_name__						)						)

						self.add(__magic_name__						)

						UpperCamelCase         =					[]
						UpperCamelCase         =					[]
						UpperCamelCase         =					[]
						for i, rect in enumerate(__magic_name__						):
									UpperCamelCase         =					Rectangle(height=0.46       ,       width=0.46						).set_stroke(width=0.0						).set_fill(__magic_name__       ,       opacity=0.7						)
									cpu_target.move_to(__magic_name__						)
									cpu_target.generate_target()
									UpperCamelCase         =					0.46 / 4
									UpperCamelCase         =					0.46 / 3

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

									first_animations.append(rect.animate(run_time=0.5						).set_stroke(__magic_name__						)						)
									second_animations.append(MoveToTarget(__magic_name__       ,       run_time=1.5						)						)

						self.play(*__magic_name__						)
						self.play(*__magic_name__						)

						self.wait()








import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available






def 							_lowercase    (		*SCREAMING_SNAKE_CASE_   :		List[Any]	):





			"""simple docstring"""




			if not isinstance(SCREAMING_SNAKE_CASE_       ,  SCREAMING_SNAKE_CASE_	):
						UpperCamelCase         =					list(SCREAMING_SNAKE_CASE_	)
			for i in range(len(SCREAMING_SNAKE_CASE_	)	):
						UpperCamelCase         =					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 							_lowercase    (		SCREAMING_SNAKE_CASE_   :		Exception	):





			"""simple docstring"""




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






def 							_lowercase    (		SCREAMING_SNAKE_CASE_   :		callable = None       ,  SCREAMING_SNAKE_CASE_   :		int = 128	):





			"""simple docstring"""




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

			UpperCamelCase         =					starting_batch_size

			def decorator(*SCREAMING_SNAKE_CASE_   :		Union[str, Any]       ,  **SCREAMING_SNAKE_CASE_   :		Tuple	):
						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         =					list(inspect.signature(SCREAMING_SNAKE_CASE_	).parameters.keys()	)
						# Guard against user error
						if len(SCREAMING_SNAKE_CASE_	) < (len(SCREAMING_SNAKE_CASE_	) + 1):
									UpperCamelCase         =					""", """.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(SCREAMING_SNAKE_CASE_       ,  *SCREAMING_SNAKE_CASE_       ,  **SCREAMING_SNAKE_CASE_	)
									except Exception as e:
												if should_reduce_batch_size(SCREAMING_SNAKE_CASE_	):
															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




