






from manim import *



class   lowerCamelCase      (_snake_case			):







		'''simple docstring'''



		def 					__UpperCAmelCase			(							self   )	->							Any:
							UpperCAmelCase_    :      Tuple            =						Rectangle(height=0.5				,		width=0.5   )
							UpperCAmelCase_    :      Optional[Any]            =						Rectangle(height=0.46				,		width=0.46   ).set_stroke(width=0   )

							UpperCAmelCase_    :      Optional[Any]            =						Rectangle(height=0.25				,		width=0.25   )

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

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

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

							UpperCAmelCase_    :      Any            =						Text('Model'				,		font_size=2_4   )
							UpperCAmelCase_    :      Optional[int]            =						Group(__lowerCAmelCase				,		__lowerCAmelCase   ).arrange(__lowerCAmelCase				,		buff=0.5				,		aligned_edge=__lowerCAmelCase   )
							model.move_to([3, -1.0, 0]   )
							self.add(__lowerCAmelCase   )

							UpperCAmelCase_    :      str            =						[]
							UpperCAmelCase_    :      Any            =						[]

							for i, rect in enumerate(__lowerCAmelCase   ):
												UpperCAmelCase_    :      Union[str, Any]            =						fill.copy().set_fill(__lowerCAmelCase				,		opacity=0.8   )
												target.move_to(__lowerCAmelCase   )
												model_arr.append(__lowerCAmelCase   )

												UpperCAmelCase_    :      Tuple            =						Rectangle(height=0.46				,		width=0.46   ).set_stroke(width=0.0   ).set_fill(__lowerCAmelCase				,		opacity=0.8   )
												cpu_target.move_to(cpu_left_col_base[i]   )
												model_cpu_arr.append(__lowerCAmelCase   )

							self.add(*__lowerCAmelCase				,		*__lowerCAmelCase   )

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

							UpperCAmelCase_    :      List[Any]            =						Square(side_length=2.2   )
							key.move_to([-5, 2, 0]   )

							UpperCAmelCase_    :      List[Any]            =						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_    :      List[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"Now watch as an input is passed through the model\nand how the memory is utilized and handled."				,		font_size=2_4				,		)
							step_a.move_to([2, 2, 0]   )

							self.play(Write(__lowerCAmelCase   )   )

							UpperCAmelCase_    :      Tuple            =						Square(0.3   )
							input.set_fill(__lowerCAmelCase				,		opacity=1.0   )
							input.set_stroke(width=0.0   )
							input.next_to(model_base[0]				,		__lowerCAmelCase				,		buff=0.5   )

							self.play(Write(__lowerCAmelCase   )   )

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

							self.play(FadeOut(__lowerCAmelCase   )   )

							UpperCAmelCase_    :      List[str]            =						Arrow(start=__lowerCAmelCase				,		end=__lowerCAmelCase				,		color=__lowerCAmelCase				,		buff=0.5   )
							a.next_to(model_arr[0].get_left()				,		__lowerCAmelCase				,		buff=0.2   )

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

							UpperCAmelCase_    :      Any            =						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=2_4				,		)
							step_a.move_to([2, 2, 0]   )

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

							UpperCAmelCase_    :      Union[str, Any]            =						{'run_time': 1, 'fade_in': True, 'fade_out': True, 'buff': 0.02}

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

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

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

												UpperCAmelCase_    :      Dict            =						AnimationGroup(
												    FadeOut(__lowerCAmelCase				,		run_time=0.5   )				,		MoveToTarget(__lowerCAmelCase				,		run_time=0.5   )				,		FadeIn(__lowerCAmelCase				,		run_time=0.5   )				,		lag_ratio=0.2   )

												self.play(__lowerCAmelCase   )

												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:
																						UpperCAmelCase_    :      Union[str, Any]            =						0.7

																	self.play(
																	    Circumscribe(model_arr[i]				,		**__lowerCAmelCase   )				,		Circumscribe(cpu_left_col_base[i]				,		**__lowerCAmelCase   )				,		Circumscribe(cpu_left_col_base[i + 1]				,		color=__lowerCAmelCase				,		**__lowerCAmelCase   )				,		Circumscribe(gpu_rect[0]				,		color=__lowerCAmelCase				,		**__lowerCAmelCase   )				,		Circumscribe(model_arr[i + 1]				,		color=__lowerCAmelCase				,		**__lowerCAmelCase   )				,		)
																	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=__lowerCAmelCase				,		**__lowerCAmelCase   )				,		Circumscribe(cpu_left_col_base[-1]				,		color=__lowerCAmelCase				,		**__lowerCAmelCase   )				,		Circumscribe(gpu_rect[0]				,		color=__lowerCAmelCase				,		**__lowerCAmelCase   )				,		)

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

												UpperCAmelCase_    :      Dict            =						a_c
												UpperCAmelCase_    :      Union[str, Any]            =						a_c.copy()

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

							UpperCAmelCase_    :      int            =						MarkupText(f"Inference on a model too large for GPU memory\nis successfully completed."				,		font_size=2_4   )
							step_a.move_to([2, 2, 0]   )

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

							self.wait()













# This code is adapted from OpenAI's release
# https://github.com/openai/human-eval/blob/master/human_eval/execution.py

import contextlib
import faulthandler
import io
import multiprocessing
import os
import platform
import signal
import tempfile







def    __lowerCamelCase					(							__a   :List[str]    ,     __a   :List[Any]    ,     __a   :Union[str, Any]    ,     __a   :List[Any]    )		->				Dict:






		"""simple docstring"""

		A__        =   multiprocessing.Manager()
		A__        =   manager.list()

		A__        =   multiprocessing.Process(target=__a    ,     args=(check_program, result, timeout)    )
		p.start()
		p.join(timeout=timeout + 1    )
		if p.is_alive():
				p.kill()

		if not result:
				result.append("""timed out"""    )

		return {
		    "task_id": task_id,
		    "passed": result[0] == "passed",
		    "result": result[0],
		    "completion_id": completion_id,
		}







def    __lowerCamelCase					(							__a   :Optional[Any]    ,     __a   :Any    ,     __a   :List[Any]    )		->				Union[str, Any]:






		"""simple docstring"""

		with create_tempdir():
				# These system calls are needed when cleaning up tempdir.
				import os
				import shutil

				A__        =   shutil.rmtree
				A__        =   os.rmdir
				A__        =   os.chdir

				# Disable functionalities that can make destructive changes to the test.
				reliability_guard()

				# Run program.
				try:
						A__        =   {}
						with swallow_io():
								with time_limit(__a    ):
										exec(__a    ,     __a    )
						result.append("""passed"""    )
				except TimeoutException:
						result.append("""timed out"""    )
				except BaseException as e:
						result.append(F'failed: {e}'    )

				# Needed for cleaning up.
				A__        =   rmtree
				A__        =   rmdir
				A__        =   chdir







@contextlib.contextmanager
def    __lowerCamelCase					(							__a   :List[str]    )		->				Dict:






		"""simple docstring"""

		def signal_handler(__a   :List[Any]    ,     __a   :Optional[Any]    ):
				raise TimeoutException("""Timed out!"""    )

		signal.setitimer(signal.ITIMER_REAL    ,     __a    )
		signal.signal(signal.SIGALRM    ,     __a    )
		try:
				yield
		finally:
				signal.setitimer(signal.ITIMER_REAL    ,     0    )







@contextlib.contextmanager
def    __lowerCamelCase					(							)		->				Union[str, Any]:






		"""simple docstring"""

		A__        =   WriteOnlyStringIO()
		with contextlib.redirect_stdout(__a    ):
				with contextlib.redirect_stderr(__a    ):
						with redirect_stdin(__a    ):
								yield







@contextlib.contextmanager
def    __lowerCamelCase					(							)		->				Dict:






		"""simple docstring"""

		with tempfile.TemporaryDirectory() as dirname:
				with chdir(__a    ):
						yield dirname


class   A			(SCREAMING_SNAKE_CASE    ):
			'''simple docstring'''




			pass


class   A			(io.StringIO    ):
			'''simple docstring'''




			def    a_       (			self   :    Any							,	*__lowerCAmelCase   :    List[str]							,	**__lowerCAmelCase   :    str   )	->	Dict:







					"""simple docstring"""






					raise OSError






			def    a_       (			self   :    Optional[Any]							,	*__lowerCAmelCase   :    Any							,	**__lowerCAmelCase   :    Optional[int]   )	->	str:







					"""simple docstring"""






					raise OSError






			def    a_       (			self   :    Optional[Any]							,	*__lowerCAmelCase   :    Any							,	**__lowerCAmelCase   :    Any   )	->	int:







					"""simple docstring"""






					raise OSError







			def    a_       (			self   :    str							,	*__lowerCAmelCase   :    Any							,	**__lowerCAmelCase   :    Union[str, Any]   )	->	int:







					"""simple docstring"""






					return False


class   A			(contextlib._RedirectStream    ):  # type: ignore
			'''simple docstring'''




			__lowerCamelCase	:       Union[str, Any]							=    '''stdin'''







@contextlib.contextmanager
def    __lowerCamelCase					(							__a   :Union[str, Any]    )		->				List[str]:






		"""simple docstring"""

		if root == ".":
				yield
				return
		A__        =   os.getcwd()
		os.chdir(__a    )
		try:
				yield
		except BaseException as exc:
				raise exc
		finally:
				os.chdir(__a    )







def    __lowerCamelCase					(							__a   :Union[str, Any]=None    )		->				Dict:






		"""simple docstring"""

		if maximum_memory_bytes is not None:
				import resource

				resource.setrlimit(resource.RLIMIT_AS    ,     (maximum_memory_bytes, maximum_memory_bytes)    )
				resource.setrlimit(resource.RLIMIT_DATA    ,     (maximum_memory_bytes, maximum_memory_bytes)    )
				if not platform.uname().system == "Darwin":
						resource.setrlimit(resource.RLIMIT_STACK    ,     (maximum_memory_bytes, maximum_memory_bytes)    )

		faulthandler.disable()

		import builtins

		A__        =   None
		A__        =   None

		import os

		A__        =   """1"""

		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None

		import shutil

		A__        =   None
		A__        =   None
		A__        =   None

		import subprocess

		A__        =   None  # type: ignore

		A__        =   None

		import sys

		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None
		A__        =   None



