




from manim import *





class  __UpperCAmelCase					(	lowerCamelCase__						):



   def 	__magic_name__   (							self							:					int						):
     UpperCAmelCase	:							List[str]						=			Rectangle(height=0.5,		width=0.5						)
     UpperCAmelCase	:							Dict						=			Rectangle(height=0.4_6,		width=0.4_6						).set_stroke(width=0						)

     UpperCAmelCase	:							str						=			Rectangle(height=0.2_5,		width=0.2_5						)

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

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

     UpperCAmelCase	:							Optional[Any]						=			[mem.copy() for i in range(6						)]
     UpperCAmelCase	:							List[Any]						=			VGroup(*__A						).arrange(__A,		buff=0						)

     UpperCAmelCase	:							Tuple						=			Text('''Model''',		font_size=2_4						)
     UpperCAmelCase	:							Dict						=			Group(__A,		__A						).arrange(__A,		buff=0.5,		aligned_edge=__A						)
     model.move_to([3, -1.0, 0]						)
     self.add(__A						)

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

     for i, rect in enumerate(__A						):
       UpperCAmelCase	:							Optional[int]						=			fill.copy().set_fill(__A,		opacity=0.8						)
       target.move_to(__A						)
       model_arr.append(__A						)

       UpperCAmelCase	:							str						=			Rectangle(height=0.4_6,		width=0.4_6						).set_stroke(width=0.0						).set_fill(__A,		opacity=0.8						)
       cpu_target.move_to(cpu_left_col_base[i]						)
       model_cpu_arr.append(__A						)

     self.add(*__A,		*__A						)

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

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

     UpperCAmelCase	:							Dict						=			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(__A,		__A						)

     UpperCAmelCase	:							List[Any]						=			MarkupText(
         F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint''',		font_size=1_8,		)

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

     UpperCAmelCase	:							Tuple						=			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(__A						)						)

     UpperCAmelCase	:							Dict						=			Square(0.3						)
     input.set_fill(__A,		opacity=1.0						)
     input.set_stroke(width=0.0						)
     input.next_to(model_base[0],		__A,		buff=0.5						)

     self.play(Write(__A						)						)

     input.generate_target()
     input.target.next_to(model_arr[0],		direction=__A,		buff=0.0_2						)
     self.play(MoveToTarget(__A						)						)

     self.play(FadeOut(__A						)						)

     UpperCAmelCase	:							Tuple						=			Arrow(start=__A,		end=__A,		color=__A,		buff=0.5						)
     a.next_to(model_arr[0].get_left(),		__A,		buff=0.2						)

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

     UpperCAmelCase	:							Union[str, 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(__A,		run_time=3						)						)

     UpperCAmelCase	:							List[Any]						=			{'''run_time''': 1, '''fade_in''': True, '''fade_out''': True, '''buff''': 0.0_2}

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

     UpperCAmelCase	:							Tuple						=			a.copy()
     for i in range(6						):
       a_c.next_to(model_arr[i].get_right() + 0.0_2,		__A,		buff=0.2						)

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

       UpperCAmelCase	:							Dict						=			AnimationGroup(
           FadeOut(__A,		run_time=0.5						),		MoveToTarget(__A,		run_time=0.5						),		FadeIn(__A,		run_time=0.5						),		lag_ratio=0.2						)

       self.play(__A						)

       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	:							List[str]						=			0.7

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

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

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

       UpperCAmelCase	:							int						=			a_c
       UpperCAmelCase	:							Tuple						=			a_c.copy()

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

     UpperCAmelCase	:							str						=			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(__A,		run_time=3						),		MoveToTarget(__A						)						)

     self.wait()








import logging
import os

from .state import PartialState





class  __UpperCAmelCase					(	logging.LoggerAdapter						):



   @staticmethod
   def 	__magic_name__   (							__A							:					str						):
     UpperCAmelCase	:							Dict						=			PartialState()
     return not main_process_only or (main_process_only and state.is_main_process)





   def 	__magic_name__   (							self							:					Union[str, Any],		__A							:					Union[str, Any],		__A							:					Union[str, Any],		*__A							:					Optional[int],		**__A							:					Tuple						):
     if PartialState._shared_state == {}:
       raise RuntimeError(
           '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.'''						)
     UpperCAmelCase	:							List[str]						=			kwargs.pop('''main_process_only''',		__A						)
     UpperCAmelCase	:							int						=			kwargs.pop('''in_order''',		__A						)

     if self.isEnabledFor(__A						):
       if self._should_log(__A						):
         UpperCAmelCase			,						UpperCAmelCase	:							Dict						=			self.process(__A,		__A						)
         self.logger.log(__A,		__A,		*__A,		**__A						)

       elif in_order:
         UpperCAmelCase	:							Dict						=			PartialState()
         for i in range(state.num_processes						):
           if i == state.process_index:
             UpperCAmelCase			,						UpperCAmelCase	:							Optional[int]						=			self.process(__A,		__A						)
             self.logger.log(__A,		__A,		*__A,		**__A						)
           state.wait_for_everyone()






def       a__					(		UpperCAmelCase							:      str    ,			UpperCAmelCase							:      str = None      )  ->						Dict:
  if log_level is None:
    UpperCAmelCase	:							Union[str, Any]						=			os.environ.get('''ACCELERATE_LOG_LEVEL'''    ,			UpperCAmelCase      )
  UpperCAmelCase	:							Tuple						=			logging.getLogger(UpperCAmelCase      )
  if log_level is not None:
    logger.setLevel(log_level.upper()      )
    logger.root.setLevel(log_level.upper()      )
  return MultiProcessAdapter(UpperCAmelCase    ,			{}      )





