'''simple docstring'''




from manim import *






class 		__A       (__lowerCamelCase   ):

       '''simple docstring'''





       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Optional[Any]       				=		Rectangle(height=0.5				,     width=0.5      )
              __UpperCAmelCase  :				str       				=		Rectangle(height=0.4_6				,     width=0.4_6      ).set_stroke(width=0      )

              __UpperCAmelCase  :				Optional[int]       				=		Rectangle(height=0.2_5				,     width=0.2_5      )

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

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

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

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

              __UpperCAmelCase  :				List[Any]       				=		[]
              __UpperCAmelCase  :				str       				=		[]

              for i, rect in enumerate(UpperCamelCase_      ):
                     __UpperCAmelCase  :				Optional[Any]       				=		fill.copy().set_fill(UpperCamelCase_				,     opacity=0.8      )
                     target.move_to(UpperCamelCase_      )
                     model_arr.append(UpperCamelCase_      )

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

              self.add(*UpperCamelCase_				,     *UpperCamelCase_      )

              __UpperCAmelCase  :				List[Any]       				=		[meta_mem.copy() for i in range(6      )]
              __UpperCAmelCase  :				List[Any]       				=		[meta_mem.copy() for i in range(6      )]
              __UpperCAmelCase  :				List[Any]       				=		VGroup(*UpperCamelCase_      ).arrange(UpperCamelCase_				,     buff=0      )
              __UpperCAmelCase  :				Any       				=		VGroup(*UpperCamelCase_      ).arrange(UpperCamelCase_				,     buff=0      )
              __UpperCAmelCase  :				List[Any]       				=		VGroup(UpperCamelCase_				,     UpperCamelCase_      ).arrange(UpperCamelCase_				,     buff=0      )
              __UpperCAmelCase  :				Any       				=		Text("Disk"				,     font_size=24      )
              __UpperCAmelCase  :				Union[str, Any]       				=		Group(UpperCamelCase_				,     UpperCamelCase_      ).arrange(UpperCamelCase_				,     buff=0.5				,     aligned_edge=UpperCamelCase_      )
              disk.move_to([-4, -1.2_5, 0]      )
              self.add(UpperCamelCase_				,     UpperCamelCase_      )

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

              __UpperCAmelCase  :				int       				=		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(UpperCamelCase_				,     UpperCamelCase_      )

              __UpperCAmelCase  :				Any       				=		MarkupText(
                  f"""<span fgcolor=\'{BLUE}\'>●</span> Checkpoint"""				,     font_size=18				,     )

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

              __UpperCAmelCase  :				str       				=		MarkupText(
                  f"""Now watch as an input is passed through the model\nand how the memory is utilized and handled."""				,     font_size=24				,     )
              step_a.move_to([2, 2, 0]      )

              self.play(Write(UpperCamelCase_      )      )

              __UpperCAmelCase  :				int       				=		Square(0.3      )
              input.set_fill(UpperCamelCase_				,     opacity=1.0      )
              input.set_stroke(width=0.0      )
              input.next_to(model_base[0]				,     UpperCamelCase_				,     buff=0.5      )

              self.play(Write(UpperCamelCase_      )      )

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

              self.play(FadeOut(UpperCamelCase_      )      )

              __UpperCAmelCase  :				List[Any]       				=		Arrow(start=UpperCamelCase_				,     end=UpperCamelCase_				,     color=UpperCamelCase_				,     buff=0.5      )
              a.next_to(model_arr[0].get_left()				,     UpperCamelCase_				,     buff=0.2      )

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

              __UpperCAmelCase  :				List[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=24				,     )
              step_a.move_to([2, 2, 0]      )

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

              __UpperCAmelCase  :				List[str]       				=		{"run_time": 1, "fade_in": True, "fade_out": True, "buff": 0.0_2}

              self.play(
                  Write(UpperCamelCase_      )				,     Circumscribe(model_arr[0]				,     color=UpperCamelCase_				,     **UpperCamelCase_      )				,     Circumscribe(model_cpu_arr[0]				,     color=UpperCamelCase_				,     **UpperCamelCase_      )				,     Circumscribe(gpu_rect[0]				,     color=UpperCamelCase_				,     **UpperCamelCase_      )				,     )
              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.0_2				,     UpperCamelCase_				,     buff=0.2      )

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

                     __UpperCAmelCase  :				Optional[Any]       				=		AnimationGroup(
                         FadeOut(UpperCamelCase_				,     run_time=0.5      )				,     MoveToTarget(UpperCamelCase_				,     run_time=0.5      )				,     FadeIn(UpperCamelCase_				,     run_time=0.5      )				,     lag_ratio=0.2      )

                     self.play(UpperCamelCase_      )

                     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[Any]       				=		0.7

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

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

                     __UpperCAmelCase  :				str       				=		a_c
                     __UpperCAmelCase  :				Any       				=		a_c.copy()

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

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

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

              self.wait()







'''simple docstring'''




from __future__ import annotations

import numpy as np
from numpy import floataa
from numpy.typing import NDArray

def _lowercase	(      lowerCamelCase__							,    lowerCamelCase__							,    lowerCamelCase__							,    lowerCamelCase__							,    )					->       list[float]:





       """simple docstring"""


       __UpperCAmelCase				, __UpperCAmelCase  :				Union[str, Any]       				=		coefficient_matrix.shape
       __UpperCAmelCase				, __UpperCAmelCase  :				Any       				=		constant_matrix.shape

       if rowsa != colsa:
              __UpperCAmelCase  :				str       				=		f"""Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}"""
              raise ValueError(lowerCamelCase__						)

       if colsa != 1:
              __UpperCAmelCase  :				Optional[Any]       				=		f"""Constant matrix must be nx1 but received {rowsa}x{colsa}"""
              raise ValueError(lowerCamelCase__						)

       if rowsa != rowsa:
              __UpperCAmelCase  :				Optional[int]       				=		(
                  "Coefficient and constant matrices dimensions must be nxn and nx1 but "
                  f"""received {rowsa}x{colsa} and {rowsa}x{colsa}"""
              )
              raise ValueError(lowerCamelCase__						)

       if len(lowerCamelCase__						) != rowsa:
              __UpperCAmelCase  :				List[str]       				=		(
                  "Number of initial values must be equal to number of rows in coefficient "
                  f"""matrix but received {len(lowerCamelCase__						)} and {rowsa}"""
              )
              raise ValueError(lowerCamelCase__						)

       if iterations <= 0:
              raise ValueError("Iterations must be at least 1"						)

       __UpperCAmelCase  :				NDArray[floataa]       				=		np.concatenate(
           (coefficient_matrix, constant_matrix)							,    axis=1						)

       __UpperCAmelCase				, __UpperCAmelCase  :				Tuple       				=		table.shape

       strictly_diagonally_dominant(lowerCamelCase__						)

       # Iterates the whole matrix for given number of times
       for _ in range(lowerCamelCase__						):
              __UpperCAmelCase  :				int       				=		[]
              for row in range(lowerCamelCase__						):
                     __UpperCAmelCase  :				List[str]       				=		0
                     for col in range(lowerCamelCase__						):
                            if col == row:
                                   __UpperCAmelCase  :				int       				=		table[row][col]
                            elif col == cols - 1:
                                   __UpperCAmelCase  :				Any       				=		table[row][col]
                            else:
                                   temp += (-1) * table[row][col] * init_val[col]
                     __UpperCAmelCase  :				List[Any]       				=		(temp + val) / denom
                     new_val.append(lowerCamelCase__						)
              __UpperCAmelCase  :				str       				=		new_val

       return [float(lowerCamelCase__						) for i in new_val]

def _lowercase	(      lowerCamelCase__						)					->       bool:





       """simple docstring"""


       __UpperCAmelCase				, __UpperCAmelCase  :				Optional[int]       				=		table.shape

       __UpperCAmelCase  :				str       				=		True

       for i in range(0							,    lowerCamelCase__						):
              __UpperCAmelCase  :				Union[str, Any]       				=		0
              for j in range(0							,    cols - 1						):
                     if i == j:
                            continue
                     else:
                            total += table[i][j]

              if table[i][i] <= total:
                     raise ValueError("Coefficient matrix is not strictly diagonally dominant"						)

       return is_diagonally_dominant


# Test Cases
if __name__ == "__main__":
   import doctest

   doctest.testmod()






