
from manim import *



class 		UpperCamelCase__  (	__lowercase   ):
   def 				lowerCAmelCase      (self			: Union[str, Any]       ):
    __a			:  Union[str, Any]		    =  Rectangle(height=0.5 ,    width=0.5       )
    __a			:  str		    =  Rectangle(height=0.46 ,    width=0.46       ).set_stroke(width=0       )

    __a			:  Union[str, Any]		    =  [mem.copy() for i in range(6       )]
    __a			:  str		    =  [mem.copy() for i in range(6       )]
    __a			:  Optional[Any]		    =  VGroup(*snake_case_       ).arrange(snake_case_ ,    buff=0       )
    __a			:  Optional[Any]		    =  VGroup(*snake_case_       ).arrange(snake_case_ ,    buff=0       )
    __a			:  Optional[Any]		    =  VGroup(snake_case_ ,    snake_case_       ).arrange(snake_case_ ,    buff=0       )
    __a			:  str		    =  Text('''CPU''' ,    font_size=2_4       )
    __a			:  str		    =  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_       )

    __a			:  List[str]		    =  [mem.copy() for i in range(4       )]
    __a			:  Union[str, Any]		    =  VGroup(*snake_case_       ).arrange(snake_case_ ,    buff=0       )
    __a			:  Dict		    =  Text('''GPU''' ,    font_size=2_4       )
    __a			:  Optional[Any]		    =  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_       )

    __a			:  Optional[int]		    =  [mem.copy() for i in range(6       )]
    __a			:  str		    =  VGroup(*snake_case_       ).arrange(snake_case_ ,    buff=0       )

    __a			:  Union[str, Any]		    =  Text('''Model''' ,    font_size=2_4       )
    __a			:  List[Any]		    =  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_       )

    __a			:  int		    =  []
    for i, rect in enumerate(snake_case_       ):
     rect.set_stroke(snake_case_       )
     # target = fill.copy().set_fill(YELLOW, opacity=0.7)
     # target.move_to(rect)
     # self.add(target)

     __a			:  Dict		    =  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(cpu_targs[0] ,    direction=snake_case_ ,    buff=0.0       )
     else:
      cpu_target.next_to(cpu_targs[i - 1] ,    direction=snake_case_ ,    buff=0.0       )
     self.add(snake_case_       )
     cpu_targs.append(snake_case_       )

    __a			:  Optional[int]		    =  [mem.copy() for i in range(6       )]
    __a			:  Union[str, Any]		    =  VGroup(*snake_case_       ).arrange(snake_case_ ,    buff=0       )

    __a			:  List[Any]		    =  Text('''Loaded Checkpoint''' ,    font_size=2_4       )
    __a			:  List[str]		    =  Group(snake_case_ ,    snake_case_       ).arrange(snake_case_ ,    aligned_edge=snake_case_ ,    buff=0.4       )
    checkpoint.move_to([3, 0.5, 0]       )

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

    __a			:  Tuple		    =  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(snake_case_ ,    snake_case_       )

    __a			:  Optional[int]		    =  MarkupText(
        f"<span fgcolor='{BLUE}'>●</span> Checkpoint" ,    font_size=1_8 ,    )

    blue_text.next_to(snake_case_ ,    DOWN * 2.4 ,    aligned_edge=key_text.get_left()       )

    __a			:  Optional[int]		    =  MarkupText(
        f"Next, a <i><span fgcolor=\"{BLUE}\">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor=\"{BLUE}\">single shard</span>." ,    font_size=2_4 ,    )
    step_a.move_to([2, 2, 0]       )
    self.play(Write(snake_case_       ) ,    Write(snake_case_       )       )

    self.play(Write(snake_case_ ,    run_time=1       ) ,    Create(snake_case_ ,    run_time=1       )       )

    __a			:  Tuple		    =  []
    __a			:  str		    =  []
    for i, rect in enumerate(snake_case_       ):
     __a			:  List[str]		    =  fill.copy().set_fill(snake_case_ ,    opacity=0.7       )
     target.move_to(snake_case_       )
     first_animations.append(GrowFromCenter(snake_case_ ,    run_time=1       )       )

     __a			:  Any		    =  target.copy()
     cpu_target.generate_target()
     if i < 5:
      cpu_target.target.move_to(cpu_left_col_base[i + 1]       )
     else:
      cpu_target.target.move_to(cpu_right_col_base[i - 5]       )
     second_animations.append(MoveToTarget(snake_case_ ,    run_time=1.5       )       )

    self.play(*snake_case_       )
    self.play(*snake_case_       )
    self.wait()


def        __UpperCamelCase					(    lowerCAmelCase__							:    list[list[int | float]]  ):
 __a			:  int		    =  len(lowerCAmelCase__  )
 __a			:  Dict		    =  len(matrix[0]  )
 __a			:  Union[str, Any]		    =  min(lowerCAmelCase__  ,       lowerCAmelCase__  )

 for row in range(lowerCAmelCase__  ):
  # Check if diagonal element is not zero
  if matrix[row][row] != 0:
   # Eliminate all the elements below the diagonal
   for col in range(row + 1  ,       lowerCAmelCase__  ):
    __a			:  Dict		    =  matrix[col][row] / matrix[row][row]
    for i in range(lowerCAmelCase__  ,       lowerCAmelCase__  ):
     matrix[col][i] -= multiplier * matrix[row][i]
  else:
   # Find a non-zero diagonal element to swap rows
   __a			:  Optional[int]		    =  True
   for i in range(row + 1  ,       lowerCAmelCase__  ):
    if matrix[i][row] != 0:
     __a    ,	__a			:  Any		    =  matrix[i], matrix[row]
     __a			:  Union[str, Any]		    =  False
     break
   if reduce:
    rank -= 1
    for i in range(lowerCAmelCase__  ):
     __a			:  Optional[Any]		    =  matrix[i][rank]

            # Reduce the row pointer by one to stay on the same row
   row -= 1

 return rank


if __name__ == "__main__":
      import doctest

      doctest.testmod()
