



from manim import *


class 	lowerCamelCase__ (		snake_case_   ):


						def   _lowerCamelCase			( self						:  str			):
							a__:       Union[str, Any]											=Rectangle(height=0.5      ,       width=0.5			)
							a__:       Tuple											=Rectangle(height=0.4_6      ,       width=0.4_6			).set_stroke(width=0			)
							a__:       List[str]											=Rectangle(height=0.2_5      ,       width=0.2_5			)

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

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

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

							a__:       Optional[int]											=Text("Model"      ,       font_size=2_4			)
							a__:       Tuple											=Group(_a      ,       _a			).arrange(_a      ,       buff=0.5      ,       aligned_edge=_a			)
							model.move_to([3, -1.0, 0]			)
							self.add(_a			)

							a__:       Union[str, Any]											=[]
							a__:       List[str]											=[]

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

								a__:       Any											=Rectangle(height=0.4_6 / 4      ,       width=0.4_6 / 3			).set_stroke(width=0.0			).set_fill(_a      ,       opacity=0.7			)

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

							self.add(*_a      ,       *_a			)

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

							a__:       Dict											=[]

							for i in range(6			):
								a__:       List[str]											=fill.copy().set_fill(_a      ,       opacity=0.8			)
								target.move_to(disk_left_col_base[i]			).scale(0.5			)
								cpu_disk_arr.append(_a			)

							self.add(*_a			)

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

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

							a__:       Optional[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			)

							a__:       Any											=MarkupText(F"The offloaded weights are all sent to the CPU."      ,       font_size=2_4			)
							step_a.move_to([2, 2, 0]			)

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

							for i in range(6			):
								a__:       Dict											=cpu_disk_arr[i]
								a__:       List[Any]											=rect.copy().set_fill(_a      ,       opacity=0.8			).scale(2.0			)
								cpa.generate_target()
								cpa.target.move_to(model_base[i]			)

								if i == 0:
									rect.set_fill(_a      ,       opacity=0.8			)
									rect.generate_target()
									rect.target.move_to(cpu_left_col_base[0]			).scale(2.0			)

									self.remove(
									    *_a      ,       *_a      ,       )

								else:
									rect.generate_target()
									rect.target.move_to(cpu_left_col_base[i]			).scale(2.0			)
								self.play(MoveToTarget(_a			)      ,       MoveToTarget(_a			)      ,       model_base[i].animate.set_stroke(_a			)			)
							self.play(FadeOut(_a			)			)

							a__:       Union[str, Any]											=MarkupText(
							    F"Finally, hooks are added to each weight in the model\nto transfer the weights from CPU to GPU\n\t\tand back when needed."      ,       font_size=2_4      ,       )
							step_a.move_to([2, 2, 0]			)

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

							a__:       List[str]											=[]
							a__:       Dict											=[]
							for i in range(6			):
								a__:       Optional[Any]											=Arrow(start=_a      ,       end=_a      ,       color=_a      ,       buff=0.5			)
								a.next_to(model_base[i].get_left()      ,       _a      ,       buff=0.2			)
								arrows.append(_a			)
								animations.append(Write(_a			)			)
							self.play(*_a			)
							self.wait()










import json
import os
from dataclasses import dataclass

import numpy as np
import pyarrow as pa

import datasets
from utils import get_duration


__UpperCAmelCase						  =     10_00_00_00_00_00
__UpperCAmelCase						  =     1_00_00

__UpperCAmelCase  ,							__UpperCAmelCase						  =     os.path.split(__file__)
__UpperCAmelCase						  =     os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))





def 						__lowerCamelCase       (      __magic_name__	:  int     , __magic_name__	:  int						):
							a__:       Any											=pa.Table.from_pydict({"col": [0] * chunk_size}						)
							a__:       Optional[Any]											=pa.concat_tables([table] * (num_examples // chunk_size)						)
							return datasets.Dataset(__magic_name__     , fingerprint="table_100B"						)


@dataclass
class 	lowerCamelCase__ :
						_lowerCAmelCase								= 42
						_lowerCAmelCase								= 42
						_lowerCAmelCase								= 42
						_lowerCAmelCase								= 42


						def   _lowerCamelCase			( self						:  Union[str, Any]			):
													a__:       Tuple											=np.random.default_rng(self.seed			)
													a__:       Tuple											=rng.integers(low=self.low      ,       high=self.high      ,       size=self.size			).tolist()


						def __iter__( self						:  Any			):
													return iter(self._sampled_values			)







						def __len__( self						:  Any			):
													return self.size





@get_duration
def 						__lowerCamelCase       (      __magic_name__	:  datasets.Dataset						):
							a__:       Union[str, Any]											=dataset[0]





@get_duration
def 						__lowerCamelCase       (      __magic_name__	:  datasets.Dataset						):
							a__:       Optional[Any]											=dataset[-1]





@get_duration
def 						__lowerCamelCase       (      __magic_name__	:  datasets.Dataset						):
							a__:       List[str]											=dataset[range(len(__magic_name__						) // 2     , len(__magic_name__						) // 2 + 1_024						)]





@get_duration
def 						__lowerCamelCase       (      __magic_name__	:  datasets.Dataset						):
							a__:       Optional[int]											=dataset[RandIter(0     , len(__magic_name__						)     , 1_024     , seed=42						)]





def 						__lowerCamelCase       (      ):
							a__:       Dict											={"num examples": SPEED_TEST_N_EXAMPLES}
							a__:       Union[str, Any]											=(get_first_row, get_last_row, get_batch_of_aaaa_rows, get_batch_of_aaaa_random_rows)
							print("generating dataset"						)
							a__:       Dict											=generate_100B_dataset(num_examples=__magic_name__     , chunk_size=__magic_name__						)
							print("Functions"						)
							for func in functions:
														print(func.__name__						)
														a__:       Dict											=func(__magic_name__						)

							with open(__magic_name__     , "wb"						) as f:
														f.write(json.dumps(__magic_name__						).encode("utf-8"						)						)


if __name__ == "__main__":  # useful to run the profiler
		benchmark_table_100B()





