Debian Codegraph @

manim_animations/dataloaders/stage_2.py

raw
1 # Copyright 2024 The HuggingFace Team. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from manim import *
16
17
18 class Stage2(Scene):
19 def construct(self):
20 # The dataset items
21 fill = Rectangle(height=0.46,width=0.46).set_stroke(width=0)
22 columns = [
23 VGroup(*[Rectangle(height=0.25,width=0.25,color="green") for i in range(8)]).arrange(RIGHT,buff=0)
24 for j in range(4)
25 ]
26 dataset_recs = VGroup(*columns).arrange(UP, buff=0)
27 dataset_text = Text("Dataset", font_size=24)
28 dataset = Group(dataset_recs,dataset_text).arrange(DOWN, buff=0.5, aligned_edge=DOWN)
29 dataset.move_to([-2,0,0])
30 self.add(dataset)
31
32 code = Code(
33 code="dataloader = DataLoader(...)\nfor batch in dataloader():\n\t...",
34 tab_width=4,
35 background="window",
36 language="Python",
37 font="Monospace",
38 font_size=14,
39 corner_radius=.2,
40 insert_line_no=False,
41 line_spacing=.75,
42 style=Code.styles_list[1],
43 )
44 code.move_to([-3.5, 2.5, 0])
45 self.add(code)
46
47 # The dataloader itself
48 dataloader = Group(
49 Rectangle(color="red", height=2, width=2),
50 Text("DataLoader", font_size=24)
51 ).arrange(DOWN, buff=.5, aligned_edge=DOWN)
52
53 sampler = Group(
54 Rectangle(color="blue", height=1, width=1),
55 Text("Sampler", font_size=12)
56 ).arrange(DOWN, buff=.25, aligned_edge=DOWN)
57 dataloader.move_to([1, 0, 0])
58 sampler.move_to([.75,.25,0])
59 self.add(dataloader)
60 self.add(sampler)
61
62 gpu_1 = Group(
63 Rectangle(color="white", height=1, width=1),
64 Text("GPU 1", font_size=12)
65 ).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4, 2, 0])
66 gpu_2 = Group(
67 Rectangle(color="white", height=1, width=1),
68 Text("GPU 2", font_size=12)
69 ).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4, .5, 0])
70 gpu_3 = Group(
71 Rectangle(color="white", height=1, width=1),
72 Text("GPU 3", font_size=12)
73 ).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4, -1, 0])
74 gpu_4 = Group(
75 Rectangle(color="white", height=1, width=1),
76 Text("GPU 4", font_size=12)
77 ).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4, -2.5, 0])
78 gpus = [gpu_1[0], gpu_2[0], gpu_3[0], gpu_4[0]]
79 self.add(gpu_1, gpu_2, gpu_3, gpu_4)
80
81 # Animate their existence
82 self.play(
83 Create(gpu_1[0], run_time=0.5),
84 Create(gpu_2[0], run_time=0.5),
85 Create(gpu_3[0], run_time=0.5),
86 Create(gpu_4[0], run_time=0.5),
87 Create(dataset_recs, run_time=1),
88 Create(sampler[0], run_time=1),
89 Create(dataloader[0], run_time=1)
90 )
91
92 step_1 = MarkupText(
93 f"Without any special care, \nthe same data is sent though each sampler, \nand the same samples are spit out on each GPU",
94 font_size=18
95 )
96 step_1.move_to([0, -2.5, 0])
97 self.play(
98 Write(step_1, run_time=4),
99 )
100
101 first_animations = []
102 second_animations = []
103
104
105 colors = ["BLUE_E", "DARK_BROWN", "GOLD_E", "GRAY_A"]
106 current_color = colors[0]
107 buff = 0
108 lr_buff = .25
109 old_target = None
110 new_datasets = []
111 for i,data in enumerate(dataset_recs[-1]):
112 if i % 2 == 0:
113 # current_color = colors[i//2]
114 current_color = "BLUE_E"
115 dataset_target = Rectangle(height=0.46/2,width=0.46/2).set_stroke(width=0.).set_fill(current_color, opacity=0.7)
116 dataset_target.move_to(data)
117 dataset_target.generate_target()
118 aligned_edge = ORIGIN
119 if i % 2 == 0:
120 old_target = dataset_target.target
121 buff -= .25
122 aligned_edge = LEFT
123 dataset_target.target.next_to(
124 sampler, buff=buff, direction=UP,
125 aligned_edge=LEFT
126 )
127 else:
128 dataset_target.target.next_to(
129 old_target, direction=RIGHT, buff=0.01,
130 )
131 new_datasets.append(dataset_target)
132 first_animations.append(data.animate(run_time=0.5).set_stroke(current_color))
133 second_animations.append(MoveToTarget(dataset_target, run_time=1.5))
134 self.play(*first_animations)
135 self.play(*second_animations)
136 self.wait()
137
138 move_animation = []
139
140 for j,gpu in enumerate(gpus):
141 buff = 0
142 for i,data in enumerate(new_datasets):
143 if i % 2 == 0:
144 current_color = colors[i//2]
145 if j != 3:
146 data = data.copy()
147 data.generate_target()
148 aligned_edge = ORIGIN
149 if i % 2 == 0:
150 old_target = data.target
151 buff -= .25
152 aligned_edge = LEFT
153 data.target.next_to(
154 gpu, buff=buff, direction=UP,
155 aligned_edge=LEFT
156 )
157 else:
158 data.target.next_to(
159 old_target, direction=RIGHT, buff=0.01,
160 )
161 move_animation.append(MoveToTarget(data, run_time=1.5))
162
163
164 self.play(*move_animation)
165
166 self.remove(step_1)
167 step_2 = MarkupText(
168 f"This behavior is undesireable, because we want\neach GPU to see different data for efficient training.",
169 font_size=18
170 )
171 step_2.move_to([0, -2.5, 0])
172
173 self.play(
174 Write(step_2, run_time=2.5),
175 )
176 self.wait()