





from manim import *
class     UpperCamelCase__					(     lowercase_				):






		'''simple docstring'''






		def 						snake_case__				(							self  )       -> Tuple:






									"""simple docstring"""
									lowercase_	:   Optional[int]  				=     Rectangle(height=0.5,   width=0.5  )
									lowercase_	:   str  				=     Rectangle(height=0.25,   width=0.25  )
									lowercase_	:   List[str]  				=     Rectangle(height=0.46,   width=0.46  ).set_stroke(width=0  )

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

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

									lowercase_	:   Optional[Any]  				=     [mem.copy() for i in range(6  )]
									lowercase_	:   Dict  				=     VGroup(*snake_case__  ).arrange(snake_case__,   buff=0  )

									lowercase_	:   Tuple  				=     Text("""Model""",   font_size=24  )
									lowercase_	:   List[str]  				=     Group(snake_case__,   snake_case__  ).arrange(snake_case__,   buff=0.5,   aligned_edge=snake_case__  )
									model.move_to([3, -1.0, 0]  )
									self.add(snake_case__  )

									lowercase_	:   int  				=     []
									lowercase_	:   List[Any]  				=     []
									lowercase_	:   str  				=     []

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

																lowercase_	:   str  				=     Rectangle(height=0.46 / 4,   width=0.46 / 3  ).set_stroke(width=0.0  ).set_fill(snake_case__,   opacity=0.7  )

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

									self.add(*snake_case__,   *snake_case__,   *snake_case__  )

									lowercase_	:   str  				=     [mem.copy() for i in range(6  )]
									lowercase_	:   Any  				=     VGroup(*snake_case__  ).arrange(snake_case__,   buff=0  )

									lowercase_	:   List[Any]  				=     Text("""Loaded Checkpoint""",   font_size=24  )
									lowercase_	:   Dict  				=     Group(snake_case__,   snake_case__  ).arrange(snake_case__,   buff=0.5,   aligned_edge=snake_case__  )
									checkpoint.move_to([3, 0.5, 0]  )

									self.add(snake_case__  )

									lowercase_	:   Tuple  				=     []
									lowercase_	:   int  				=     []

									for i, rect in enumerate(snake_case__  ):
																lowercase_	:   Tuple  				=     fill.copy().set_fill(snake_case__,   opacity=0.7  )
																target.move_to(snake_case__  )
																ckpt_arr.append(snake_case__  )

																lowercase_	:   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(snake_case__  )
									self.add(*snake_case__,   *snake_case__  )

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

									lowercase_	:   Union[str, Any]  				=     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(snake_case__,   snake_case__  )

									lowercase_	:   Union[str, Any]  				=     MarkupText(
									    f"""<span fgcolor='{BLUE}'>●</span> Checkpoint""",   font_size=18,   )

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

									lowercase_	:   str  				=     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]  )

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

									lowercase_	:   List[str]  				=     []
									for i, rect in enumerate(snake_case__  ):
																lowercase_	:   Tuple  				=     rect.copy()
																target.generate_target()
																target.target.move_to(disk_left_col_base[i]  ).scale(0.5  )
																animations.append(MoveToTarget(snake_case__,   run_time=1.5  )  )
									self.play(*snake_case__  )

									self.play(FadeOut(snake_case__  )  )

									lowercase_	:   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(snake_case__,   run_time=3  )  )

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

									self.wait()



'''simple docstring'''

from __future__ import annotations

from collections import namedtuple
from dataclasses import dataclass
@dataclass
class     __lowerCAmelCase		:





      '''simple docstring'''






      lowerCAmelCase__   :	int
      lowerCAmelCase__   :	TreeNode | None       =				None
      lowerCAmelCase__   :	TreeNode | None       =				None






lowerCamelCase				:  Optional[Any]    		=      namedtuple('CoinsDistribResult', 'moves excess')

def _SCREAMING_SNAKE_CASE (A			)			->			int:






       """simple docstring"""







       if root is None:
              return 0

       # Validation
       def count_nodes(A			) -> int:
              if node is None:
                     return 0

              return count_nodes(node.left			) + count_nodes(node.right			) + 1

       def count_coins(A			) -> int:
              if node is None:
                     return 0

              return count_coins(node.left			) + count_coins(node.right			) + node.data

       if count_nodes(A			) != count_coins(A			):
              raise ValueError('''The nodes number should be same as the number of coins'''			)

       # Main calculation
       def get_distrib(A			) -> CoinsDistribResult:

              if node is None:
                     return CoinsDistribResult(0    ,					1			)

              lowercase__     ,lowercase__      =					get_distrib(node.left			)
              lowercase__     ,lowercase__      =					get_distrib(node.right			)

              lowercase__      =					1 - left_distrib_excess
              lowercase__      =					1 - right_distrib_excess

              lowercase__      =					(
                  left_distrib_moves
                  + right_distrib_moves
                  + abs(A			)
                  + abs(A			)
              )
              lowercase__      =					node.data - coins_to_left - coins_to_right

              return CoinsDistribResult(A    ,					A			)

       return get_distrib(A			)[0]


if __name__ == "__main__":
 import doctest

 doctest.testmod()




