| 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 |
class Stage7(Scene): |
| 18 |
def construct(self): |
| 19 |
# The dataset items |
| 20 |
code = Code( |
| 21 |
code="accelerator = Accelerator(dispatch_batches=True)\ndataloader = DataLoader(...)\ndataloader = accelerator.prepare(dataloader)\nfor batch in dataloader:\n\t...", |
| 22 |
tab_width=4, |
| 23 |
background="window", |
| 24 |
language="Python", |
| 25 |
font="Monospace", |
| 26 |
font_size=14, |
| 27 |
corner_radius=.2, |
| 28 |
insert_line_no=False, |
| 29 |
line_spacing=.75, |
| 30 |
style=Code.styles_list[1], |
| 31 |
) |
| 32 |
code.move_to([-3.5, 2.5, 0]) |
| 33 |
self.add(code) |
| 34 |
colors = ["BLUE_E", "DARK_BROWN", "GOLD_E", "GRAY_A"] |
| 35 |
fill = Rectangle(height=0.46,width=0.46).set_stroke(width=0) |
| 36 |
columns = [ |
| 37 |
VGroup(*[Rectangle(height=0.25,width=0.25,color=colors[j]) for i in range(8)]).arrange(RIGHT,buff=0) |
| 38 |
for j in range(4) |
| 39 |
] |
| 40 |
dataset_recs = VGroup(*columns).arrange(UP, buff=0) |
| 41 |
dataset_text = Text("Dataset", font_size=24) |
| 42 |
dataset = Group(dataset_recs,dataset_text).arrange(DOWN, buff=0.5, aligned_edge=DOWN) |
| 43 |
dataset.move_to([-2,0,0]) |
| 44 |
self.add(dataset) |
| 45 |
|
| 46 |
# The dataloader itself |
| 47 |
|
| 48 |
sampler_1 = Group( |
| 49 |
Rectangle(color="blue", height=1.02, width=1.02), |
| 50 |
Text("Sampler GPU 1", font_size=12) |
| 51 |
).arrange(DOWN, buff=.25, aligned_edge=DOWN) |
| 52 |
sampler_2 = Group( |
| 53 |
Rectangle(color="blue", height=1.02, width=1.02), |
| 54 |
Text("Sampler GPU 2", font_size=12) |
| 55 |
).arrange(DOWN, buff=.25, aligned_edge=DOWN) |
| 56 |
sampler_3 = Group( |
| 57 |
Rectangle(color="blue", height=1.02, width=1.02), |
| 58 |
Text("Sampler GPU 3", font_size=12) |
| 59 |
).arrange(DOWN, buff=.25, aligned_edge=DOWN) |
| 60 |
sampler_4 = Group( |
| 61 |
Rectangle(color="blue", height=1.02, width=1.02), |
| 62 |
Text("Sampler GPU 4", font_size=12) |
| 63 |
).arrange(DOWN, buff=.25, aligned_edge=DOWN) |
| 64 |
sampler_1.move_to([2,2,0]) |
| 65 |
sampler_2.move_to([2,.5,0]) |
| 66 |
sampler_3.move_to([2,-1.,0]) |
| 67 |
sampler_4.move_to([2,-2.5,0]) |
| 68 |
self.add(sampler_1, sampler_2, sampler_3, sampler_4) |
| 69 |
samplers = [sampler_1[0], sampler_2[0], sampler_3[0], sampler_4[0]] |
| 70 |
|
| 71 |
gpu_1 = Group( |
| 72 |
Rectangle(color="white", height=1.02, width=.98), |
| 73 |
Text("Output GPU 1", font_size=12) |
| 74 |
).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4.5, 2, 0]) |
| 75 |
gpu_2 = Group( |
| 76 |
Rectangle(color="white", height=1.02, width=.98), |
| 77 |
Text("Output GPU 2", font_size=12) |
| 78 |
).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4.5, .5, 0]) |
| 79 |
gpu_3 = Group( |
| 80 |
Rectangle(color="white", height=1.02, width=.98), |
| 81 |
Text("Output GPU 3", font_size=12) |
| 82 |
).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4.5, -1, 0]) |
| 83 |
gpu_4 = Group( |
| 84 |
Rectangle(color="white", height=1.02, width=.98), |
| 85 |
Text("Output GPU 4", font_size=12) |
| 86 |
).arrange(DOWN, buff=.25, aligned_edge=DOWN).move_to([4.5, -2.5, 0]) |
| 87 |
gpus = [gpu_1[0], gpu_2[0], gpu_3[0], gpu_4[0]] |
| 88 |
self.add(gpu_1, gpu_2, gpu_3, gpu_4) |
| 89 |
|
| 90 |
step_1 = MarkupText( |
| 91 |
f"When using a `DataLoaderDispatcher`, all\nof the samples are collected from GPU 0's dataset,\nthen divided and sent to each GPU.\nAs a result, this will be slower.", |
| 92 |
font_size=18 |
| 93 |
) |
| 94 |
step_1.move_to([-2.5, -2, 0]) |
| 95 |
|
| 96 |
self.play( |
| 97 |
Write(step_1, run_time=3.5), |
| 98 |
) |
| 99 |
|
| 100 |
first_animations = [] |
| 101 |
second_animations = [] |
| 102 |
|
| 103 |
|
| 104 |
colors = ["BLUE_E", "DARK_BROWN", "GOLD_E", "GRAY_A"] |
| 105 |
current_color = colors[0] |
| 106 |
ud_buff = 0.01 |
| 107 |
lr_buff = 0.01 |
| 108 |
old_target = None |
| 109 |
new_datasets = [] |
| 110 |
for i,row_data in enumerate(dataset_recs): |
| 111 |
new_row = [] |
| 112 |
current_color = colors[i] |
| 113 |
|
| 114 |
for j,indiv_data in enumerate(row_data): |
| 115 |
dataset_target = Rectangle(height=0.46/4,width=0.46/2).set_stroke(width=0.).set_fill(current_color, opacity=0.7) |
| 116 |
dataset_target.move_to(indiv_data) |
| 117 |
dataset_target.generate_target() |
| 118 |
aligned_edge = ORIGIN |
| 119 |
if j % 8 == 0: |
| 120 |
aligned_edge = LEFT |
| 121 |
dataset_target.target.next_to( |
| 122 |
samplers[0].get_corner(DOWN+LEFT), buff=0.0125, direction=RIGHT+UP, |
| 123 |
) |
| 124 |
dataset_target.target.set_x(dataset_target.target.get_x()) |
| 125 |
dataset_target.target.set_y(dataset_target.target.get_y() + (.25 * i)) |
| 126 |
elif j % 4 == 0: |
| 127 |
old_target = dataset_target.target |
| 128 |
dataset_target.target.next_to( |
| 129 |
samplers[0].get_corner(DOWN+LEFT), buff=0.0125, direction=RIGHT+UP, |
| 130 |
) |
| 131 |
dataset_target.target.set_x(dataset_target.target.get_x()) |
| 132 |
dataset_target.target.set_y(dataset_target.target.get_y()+.125 + (.25 * i)) |
| 133 |
else: |
| 134 |
dataset_target.target.next_to( |
| 135 |
old_target, direction=RIGHT, buff=0.0125, |
| 136 |
) |
| 137 |
old_target = dataset_target.target |
| 138 |
new_row.append(dataset_target) |
| 139 |
first_animations.append(indiv_data.animate(run_time=0.5).set_stroke(current_color)) |
| 140 |
second_animations.append(MoveToTarget(dataset_target, run_time=1.5)) |
| 141 |
|
| 142 |
new_datasets.append(new_row) |
| 143 |
self.play( |
| 144 |
*first_animations, |
| 145 |
) |
| 146 |
self.play(*second_animations) |
| 147 |
move_animation = [] |
| 148 |
for i,row in enumerate(new_datasets): |
| 149 |
current_color = colors[i] |
| 150 |
if i == 0: |
| 151 |
idx = -3 |
| 152 |
elif i == 1: |
| 153 |
idx = -2 |
| 154 |
elif i == 2: |
| 155 |
idx = -1 |
| 156 |
elif i == 3: |
| 157 |
idx = 0 |
| 158 |
for j,indiv_data in enumerate(row): |
| 159 |
indiv_data.generate_target() |
| 160 |
indiv_data.animate.stretch_to_fit_height(0.46/2) |
| 161 |
aligned_edge = ORIGIN |
| 162 |
if j % 8 == 0: |
| 163 |
aligned_edge = LEFT |
| 164 |
indiv_data.target.next_to( |
| 165 |
gpus[abs(idx)].get_corner(UP+LEFT), buff=.01, direction=RIGHT+DOWN, |
| 166 |
) |
| 167 |
indiv_data.target.set_x(indiv_data.target.get_x()) |
| 168 |
indiv_data.target.set_y(indiv_data.target.get_y()-.25) |
| 169 |
elif j % 4 == 0: |
| 170 |
indiv_data.target.next_to( |
| 171 |
gpus[abs(idx)].get_corner(UP+LEFT), buff=.01, direction=RIGHT+DOWN, |
| 172 |
) |
| 173 |
indiv_data.target.set_x(indiv_data.target.get_x()) |
| 174 |
else: |
| 175 |
indiv_data.target.next_to( |
| 176 |
old_target, direction=RIGHT, buff=0.01, |
| 177 |
) |
| 178 |
old_target = indiv_data.target |
| 179 |
move_animation.append(MoveToTarget(indiv_data, run_time=1.5)) |
| 180 |
|
| 181 |
self.play(*move_animation) |
| 182 |
self.wait() |