Debian Codegraph @

manim_animations/dataloaders/stage_6.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 Stage6(Scene):
19 def construct(self):
20 # The dataset items
21 colors = ["BLUE_E", "DARK_BROWN", "GOLD_E", "GRAY_A"]
22 fill = Rectangle(height=0.46,width=0.46).set_stroke(width=0)
23 columns = [
24 VGroup(*[Rectangle(height=0.25,width=0.25,color=colors[j]) for i in range(8)]).arrange(RIGHT,buff=0)
25 for j in range(4)
26 ]
27 dataset_recs = VGroup(*columns).arrange(UP, buff=0)
28 dataset_text = Text("Dataset", font_size=24)
29 dataset = Group(dataset_recs,dataset_text).arrange(DOWN, buff=0.5, aligned_edge=DOWN)
30 dataset.move_to([-2,0,0])
31 self.add(dataset)
32 code = Code(
33 code="# We enable this by default\naccelerator = Accelerator()\ndataloader = DataLoader(..., shuffle=True)\ndataloader = accelerator.prepare(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
49 sampler_1 = Group(
50 Rectangle(color="blue", height=1, width=1),
51 Text("Sampler GPU 1", font_size=12)
52 ).arrange(DOWN, buff=.25, aligned_edge=DOWN)
53 sampler_2 = Group(
54 Rectangle(color="blue", height=1, width=1),
55 Text("Sampler GPU 2", font_size=12)
56 ).arrange(DOWN, buff=.25, aligned_edge=DOWN)
57 sampler_3 = Group(
58 Rectangle(color="blue", height=1, width=1),
59 Text("Sampler GPU 3", font_size=12)
60 ).arrange(DOWN, buff=.25, aligned_edge=DOWN)
61 sampler_4 = Group(
62 Rectangle(color="blue", height=1, width=1),
63 Text("Sampler GPU 4", font_size=12)
64 ).arrange(DOWN, buff=.25, aligned_edge=DOWN)
65 sampler_1.move_to([2,2,0])
66 sampler_2.move_to([2,.5,0])
67 sampler_3.move_to([2,-1.,0])
68 sampler_4.move_to([2,-2.5,0])
69 self.add(sampler_1, sampler_2, sampler_3, sampler_4)
70 samplers = [sampler_1[0], sampler_2[0], sampler_3[0], sampler_4[0]]
71
72 gpu_1 = Group(
73 Rectangle(color="white", height=1, width=1),
74 Text("Output GPU 1", font_size=12)
75 ).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4.5, 2, 0])
76 gpu_2 = Group(
77 Rectangle(color="white", height=1, width=1),
78 Text("Output GPU 2", font_size=12)
79 ).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4.5, .5, 0])
80 gpu_3 = Group(
81 Rectangle(color="white", height=1, width=1),
82 Text("Output GPU 3", font_size=12)
83 ).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4.5, -1, 0])
84 gpu_4 = Group(
85 Rectangle(color="white", height=1, width=1),
86 Text("Output GPU 4", font_size=12)
87 ).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4.5, -2.5, 0])
88 gpus = [gpu_1[0], gpu_2[0], gpu_3[0], gpu_4[0]]
89 self.add(gpu_1, gpu_2, gpu_3, gpu_4)
90
91
92 first_animations = []
93 second_animations = []
94
95
96 colors = ["BLUE_E", "DARK_BROWN", "GOLD_E", "GRAY_A"]
97 current_color = colors[0]
98 buff = 0
99 lr_buff = .25
100 old_target = None
101 new_datasets = []
102 for i,row_data in enumerate(dataset_recs):
103 new_row = []
104 current_color = colors[i]
105 if i == 0:
106 idx = -3
107 elif i == 1:
108 idx = -2
109 elif i == 2:
110 idx = -1
111 elif i == 3:
112 idx = 0
113 for j,indiv_data in enumerate(row_data):
114 dataset_target = Rectangle(height=0.46/2,width=0.46/2).set_stroke(width=0.).set_fill(current_color, opacity=0.7)
115 dataset_target.move_to(indiv_data)
116 dataset_target.generate_target()
117 aligned_edge = ORIGIN
118 if j % 8 == 0:
119 aligned_edge = LEFT
120 old_target = dataset_target.target
121 dataset_target.target.next_to(
122 samplers[abs(idx)].get_corner(UP+LEFT), buff=.02, direction=RIGHT+DOWN,
123 )
124 dataset_target.target.set_x(dataset_target.target.get_x())
125 elif j % 4 == 0:
126 old_target = dataset_target.target
127 dataset_target.target.next_to(
128 samplers[abs(idx)].get_corner(UP+LEFT), buff=.02, direction=RIGHT+DOWN,
129 )
130 dataset_target.target.set_x(dataset_target.target.get_x())
131 dataset_target.target.set_y(dataset_target.target.get_y()-.25)
132 else:
133 dataset_target.target.next_to(
134 old_target, direction=RIGHT, buff=0.02,
135 )
136 old_target = dataset_target.target
137 new_row.append(dataset_target)
138 first_animations.append(indiv_data.animate(run_time=0.5).set_stroke(current_color))
139 second_animations.append(MoveToTarget(dataset_target, run_time=1.5))
140
141 new_datasets.append(new_row)
142 step_1 = MarkupText(
143 f"During shuffling, each mini-batch's\noutput order will be modified",
144 font_size=18
145 )
146 step_1.move_to([-1.5, -2, 0])
147
148 self.play(
149 Write(step_1, run_time=3),
150 )
151 self.play(
152 *first_animations,
153 )
154 self.play(*second_animations)
155 self.wait(duration=.5)
156
157 move_animation = []
158 import random
159 for i,row in enumerate(new_datasets):
160 row = [row[k] for k in random.sample(range(8), 8)]
161 current_color = colors[i]
162 if i == 0:
163 idx = -3
164 elif i == 1:
165 idx = -2
166 elif i == 2:
167 idx = -1
168 elif i == 3:
169 idx = 0
170 for j,indiv_data in enumerate(row):
171 indiv_data.generate_target()
172 aligned_edge = ORIGIN
173 if j % 8 == 0:
174 aligned_edge = LEFT
175 indiv_data.target.next_to(
176 gpus[abs(idx)].get_corner(UP+LEFT), buff=.02, direction=RIGHT+DOWN,
177 )
178 indiv_data.target.set_x(indiv_data.target.get_x())
179 elif j % 4 == 0:
180 indiv_data.target.next_to(
181 gpus[abs(idx)].get_corner(UP+LEFT), buff=.02, direction=RIGHT+DOWN,
182 )
183 indiv_data.target.set_x(indiv_data.target.get_x())
184 indiv_data.target.set_y(indiv_data.target.get_y()-.25)
185 else:
186 indiv_data.target.next_to(
187 old_target, direction=RIGHT, buff=0.02,
188 )
189 old_target = indiv_data.target
190 move_animation.append(MoveToTarget(indiv_data, run_time=1.5))
191
192 self.play(*move_animation)
193 self.wait()