| 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 Stage5(Scene): |
| 18 |
def construct(self): |
| 19 |
# The dataset items |
| 20 |
colors = ["BLUE_E", "DARK_BROWN", "GOLD_E", "GRAY_A"] |
| 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=colors[j]) 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 |
code = Code( |
| 32 |
code="# We enable this by default\naccelerator = Accelerator()\ndataloader = DataLoader(...)\ndataloader = accelerator.prepare(dataloader)\nfor batch in dataloader:\n\t...", |
| 33 |
tab_width=4, |
| 34 |
background="window", |
| 35 |
language="Python", |
| 36 |
font="Monospace", |
| 37 |
font_size=14, |
| 38 |
corner_radius=.2, |
| 39 |
insert_line_no=False, |
| 40 |
line_spacing=.75, |
| 41 |
style=Code.styles_list[1], |
| 42 |
) |
| 43 |
code.move_to([-3.5, 2.5, 0]) |
| 44 |
self.add(code) |
| 45 |
|
| 46 |
# The dataloader itself |
| 47 |
|
| 48 |
sampler_1 = Group( |
| 49 |
Rectangle(color="blue", height=1, width=1), |
| 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, width=1), |
| 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, width=1), |
| 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, width=1), |
| 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, width=1), |
| 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, width=1), |
| 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, width=1), |
| 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, width=1), |
| 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 |
# Animate their existence |
| 91 |
self.play( |
| 92 |
Create(gpu_1[0], run_time=1), |
| 93 |
Create(gpu_2[0], run_time=1), |
| 94 |
Create(gpu_3[0], run_time=1), |
| 95 |
Create(gpu_4[0], run_time=1), |
| 96 |
Create(dataset_recs, run_time=1), |
| 97 |
Create(sampler_1[0], run_time=1), |
| 98 |
Create(sampler_2[0], run_time=1), |
| 99 |
Create(sampler_3[0], run_time=1), |
| 100 |
Create(sampler_4[0], run_time=1), |
| 101 |
) |
| 102 |
|
| 103 |
first_animations = [] |
| 104 |
second_animations = [] |
| 105 |
|
| 106 |
|
| 107 |
colors = ["BLUE_E", "DARK_BROWN", "GOLD_E", "GRAY_A"] |
| 108 |
current_color = colors[0] |
| 109 |
buff = 0 |
| 110 |
lr_buff = .25 |
| 111 |
old_target = None |
| 112 |
new_datasets = [] |
| 113 |
for i,row_data in enumerate(dataset_recs): |
| 114 |
new_row = [] |
| 115 |
current_color = colors[i] |
| 116 |
if i == 0: |
| 117 |
idx = -3 |
| 118 |
elif i == 1: |
| 119 |
idx = -2 |
| 120 |
elif i == 2: |
| 121 |
idx = -1 |
| 122 |
elif i == 3: |
| 123 |
idx = 0 |
| 124 |
for j,indiv_data in enumerate(row_data): |
| 125 |
dataset_target = Rectangle(height=0.46/2,width=0.46/2).set_stroke(width=0.).set_fill(current_color, opacity=0.7) |
| 126 |
dataset_target.move_to(indiv_data) |
| 127 |
dataset_target.generate_target() |
| 128 |
aligned_edge = ORIGIN |
| 129 |
if j % 8 == 0: |
| 130 |
aligned_edge = LEFT |
| 131 |
dataset_target.target.next_to( |
| 132 |
samplers[abs(idx)].get_corner(UP+LEFT), buff=.02, direction=RIGHT+DOWN, |
| 133 |
) |
| 134 |
dataset_target.target.set_x(dataset_target.target.get_x()) |
| 135 |
elif j % 4 == 0: |
| 136 |
old_target = dataset_target.target |
| 137 |
dataset_target.target.next_to( |
| 138 |
samplers[abs(idx)].get_corner(UP+LEFT), buff=.02, direction=RIGHT+DOWN, |
| 139 |
) |
| 140 |
dataset_target.target.set_x(dataset_target.target.get_x()) |
| 141 |
dataset_target.target.set_y(dataset_target.target.get_y()-.25) |
| 142 |
else: |
| 143 |
dataset_target.target.next_to( |
| 144 |
old_target, direction=RIGHT, buff=0.02, |
| 145 |
) |
| 146 |
old_target = dataset_target.target |
| 147 |
new_row.append(dataset_target) |
| 148 |
first_animations.append(indiv_data.animate(run_time=0.5).set_stroke(current_color)) |
| 149 |
second_animations.append(MoveToTarget(dataset_target, run_time=1.5)) |
| 150 |
|
| 151 |
new_datasets.append(new_row) |
| 152 |
step_1 = MarkupText( |
| 153 |
f"Since we splice the dataset between each GPU,\nthe models weights can be averaged during `backward()`\nActing as though we did one giant epoch\nvery quickly.", |
| 154 |
font_size=18 |
| 155 |
) |
| 156 |
step_1.move_to([-2.5, -2, 0]) |
| 157 |
|
| 158 |
self.play( |
| 159 |
Write(step_1, run_time=3), |
| 160 |
) |
| 161 |
self.play( |
| 162 |
*first_animations, |
| 163 |
) |
| 164 |
self.play(*second_animations) |
| 165 |
self.wait(duration=.5) |
| 166 |
|
| 167 |
move_animation = [] |
| 168 |
import random |
| 169 |
for i,row in enumerate(new_datasets): |
| 170 |
# row = [row[k] for k in random.sample(range(8), 8)] |
| 171 |
current_color = colors[i] |
| 172 |
if i == 0: |
| 173 |
idx = -3 |
| 174 |
elif i == 1: |
| 175 |
idx = -2 |
| 176 |
elif i == 2: |
| 177 |
idx = -1 |
| 178 |
elif i == 3: |
| 179 |
idx = 0 |
| 180 |
for j,indiv_data in enumerate(row): |
| 181 |
indiv_data.generate_target() |
| 182 |
aligned_edge = ORIGIN |
| 183 |
if j % 8 == 0: |
| 184 |
aligned_edge = LEFT |
| 185 |
indiv_data.target.next_to( |
| 186 |
gpus[abs(idx)].get_corner(UP+LEFT), buff=.02, direction=RIGHT+DOWN, |
| 187 |
) |
| 188 |
indiv_data.target.set_x(indiv_data.target.get_x()) |
| 189 |
elif j % 4 == 0: |
| 190 |
indiv_data.target.next_to( |
| 191 |
gpus[abs(idx)].get_corner(UP+LEFT), buff=.02, direction=RIGHT+DOWN, |
| 192 |
) |
| 193 |
indiv_data.target.set_x(indiv_data.target.get_x()) |
| 194 |
indiv_data.target.set_y(indiv_data.target.get_y()-.25) |
| 195 |
else: |
| 196 |
indiv_data.target.next_to( |
| 197 |
old_target, direction=RIGHT, buff=0.02, |
| 198 |
) |
| 199 |
old_target = indiv_data.target |
| 200 |
move_animation.append(MoveToTarget(indiv_data, run_time=1.5)) |
| 201 |
|
| 202 |
self.play(*move_animation) |
| 203 |
self.wait() |