forked from 3b1b/videos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attention.py
301 lines (272 loc) · 10.3 KB
/
attention.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from manim_imports_ext import *
from _2024.transformers.helpers import *
class AttentionPatterns(InteractiveScene):
def construct(self):
# Add sentence
phrase = " the fluffy blue creature foraged in a verdant forest"
phrase_mob = Text(phrase)
phrase_mob.move_to(2 * UP)
words = phrase.split(" ")
word2mob: Dict[str, VMobject] = {
word: phrase_mob[" " + word][0]
for word in words
}
word_mobs = VGroup(*word2mob.values())
self.play(
LaggedStartMap(FadeIn, word_mobs, shift=0.5 * UP, lag_ratio=0.25)
)
self.wait()
# Create word rects
word2rect: Dict[str, VMobject] = dict()
for word in words:
rect = SurroundingRectangle(word2mob[word])
rect.set_height(phrase_mob.get_height() + SMALL_BUFF, stretch=True)
rect.match_y(phrase_mob)
rect.set_stroke(GREY, 2)
rect.set_fill(GREY, 0.2)
word2rect[word] = rect
# Adjectives updating noun
adjs = ["fluffy", "blue", "verdant"]
nouns = ["creature", "forest"]
others = ["the", "foraged", "in", "a"]
adj_mobs, noun_mobs, other_mobs = [
VGroup(word2mob[substr] for substr in group)
for group in [adjs, nouns, others]
]
adj_rects, noun_rects, other_rects = [
VGroup(word2rect[substr] for substr in group)
for group in [adjs, nouns, others]
]
adj_rects.set_submobject_colors_by_gradient(BLUE_C, BLUE_D, GREEN)
noun_rects.set_color(GREY_BROWN).set_stroke(width=3)
kw = dict()
adj_arrows = VGroup(
Arrow(
adj_mobs[i].get_top(), noun_mobs[j].get_top(),
path_arc=-150 * DEGREES, buff=0.1, stroke_color=GREY_B
)
for i, j in [(0, 0), (1, 0), (2, 1)]
)
self.play(
LaggedStartMap(DrawBorderThenFill, adj_rects),
Animation(adj_mobs),
)
self.wait()
self.play(
LaggedStartMap(DrawBorderThenFill, noun_rects),
Animation(noun_mobs),
LaggedStartMap(ShowCreation, adj_arrows, lag_ratio=0.2, run_time=1.5),
)
kw = dict(time_width=2, max_stroke_width=10, lag_ratio=0.2, path_arc=150 * DEGREES)
self.play(
ContextAnimation(noun_mobs[0], adj_mobs[:2], strengths=[1, 1], **kw),
ContextAnimation(noun_mobs[1], adj_mobs[2:], strengths=[1], **kw),
)
self.wait()
# Show embeddings
all_rects = VGroup(*adj_rects, *noun_rects, *other_rects)
all_rects.sort(lambda p: p[0])
embeddings = VGroup(
NumericEmbedding(length=10).set_width(0.5).next_to(rect, DOWN, buff=1.5)
for rect in all_rects
)
emb_arrows = VGroup(
Arrow(all_rects[0].get_bottom(), embeddings[0].get_top()).match_x(rect)
for rect in all_rects
)
for index, vect in [(5, LEFT), (6, RIGHT)]:
embeddings[index].shift(0.1 * vect)
emb_arrows[index].shift(0.05 * vect)
self.play(
FadeIn(other_rects),
Animation(word_mobs),
LaggedStartMap(GrowArrow, emb_arrows),
LaggedStartMap(FadeIn, embeddings, shift=0.5 * DOWN),
FadeOut(adj_arrows)
)
self.wait()
# Mention dimension of embedding
frame = self.frame
brace = Brace(embeddings[0], LEFT, buff=SMALL_BUFF)
dim_value = Integer(12288)
dim_value.next_to(brace, LEFT)
dim_value.set_color(YELLOW)
self.play(
GrowFromCenter(brace),
CountInFrom(dim_value, 0),
frame.animate.move_to(LEFT)
)
self.wait()
# Ingest meaning and and position
images = Group(
ImageMobject(f"Dalle3_{word}").set_height(1.1).next_to(word2rect[word], UP)
for word in ["fluffy", "blue", "creature", "verdant", "forest"]
)
image_vects = VGroup(embeddings[i] for i in [1, 2, 3, 7, 8])
self.play(
LaggedStartMap(FadeIn, images, scale=2, lag_ratio=0.05)
)
self.play(
LaggedStart(
(self.bake_mobject_into_vector_entries(image, vect, group_type=Group)
for image, vect in zip(images, image_vects)),
group_type=Group,
lag_ratio=0.2,
run_time=4,
remover=True
),
)
self.wait()
self.add(embeddings, images)
# Show positions
pos_labels = VGroup(
Integer(n, font_size=36).next_to(rect, DOWN, buff=0.1)
for n, rect in enumerate(all_rects, start=1)
)
pos_labels.set_color(TEAL)
self.play(
LaggedStart(
(arrow.animate.scale(0.7, about_edge=DOWN)
for arrow in emb_arrows),
lag_ratio=0.1,
),
LaggedStartMap(FadeIn, pos_labels, shift=0.25 * DOWN, lag_ratio=0.1)
)
self.play(
LaggedStart(
(self.bake_mobject_into_vector_entries(pos, vect)
for pos, vect in zip(pos_labels, embeddings)),
lag_ratio=0.2,
run_time=4,
remover=True
),
)
self.wait()
# Collapse vectors
template = Tex(R"\vec{\textbf{E}}_{0}")
dec = template.make_number_changeable(0)
vect_syms = VGroup()
for n, rect in enumerate(all_rects, start=1):
dec.set_value(n)
sym = template.copy()
sym.next_to(rect, DOWN, buff=LARGE_BUFF)
sym.set_color(GREY_A)
vect_syms.add(sym)
prev_center = vect_syms.get_center()
vect_syms.arrange_to_fit_width(vect_syms.get_width())
vect_syms.move_to(prev_center)
emb_arrows.target = emb_arrows.generate_target()
for arrow, rect, sym in zip(emb_arrows.target, all_rects, vect_syms):
arrow.become(Arrow(
rect.get_bottom(),
sym[0].get_top(),
buff=SMALL_BUFF
))
all_brackets = VGroup(emb.get_brackets() for emb in embeddings)
for brackets in all_brackets:
brackets.target = brackets.generate_target()
brackets.target.stretch(0, 1, about_edge=UP)
brackets.target.set_fill(opacity=0)
ghost_syms = vect_syms.copy()
ghost_syms.set_opacity(0)
self.play(
LaggedStart(
(AnimationGroup(
LaggedStart(
(FadeTransform(entry, sym)
for entry in embedding.get_columns()[0]),
lag_ratio=0.01,
group_type=Group
),
MoveToTarget(brackets),
group_type=Group,
)
for sym, embedding, brackets in zip(ghost_syms, embeddings, all_brackets)),
group_type=Group
),
LaggedStartMap(FadeIn, vect_syms, shift=UP),
brace.animate.stretch(0.25, 1, about_edge=UP).set_opacity(0),
FadeOut(dim_value, 0.25 * UP),
MoveToTarget(emb_arrows, lag_ratio=0.1, run_time=2),
LaggedStartMap(FadeOut, pos_labels, shift=UP)
)
self.clear()
self.add(emb_arrows, all_rects, word_mobs, images, vect_syms)
self.wait()
# Preview desired updates
vect_sym_primes = VGroup(
sym.copy().add(Tex("'").move_to(sym.get_corner(UR) + 0.05 * DL))
for sym in vect_syms
)
vect_sym_primes.shift(2 * DOWN)
vect_sym_primes.set_color(TEAL)
full_connections = VGroup(
Line(sym1.get_bottom(), sym2.get_top(), buff=SMALL_BUFF)
for sym2 in vect_sym_primes
for sym1 in vect_syms
)
full_connections.set_stroke(GREY_B, 1)
blue_fluff = ImageMobject("BlueFluff")
verdant_forest = ImageMobject("VerdantForest")
for n, image in [(3, blue_fluff), (8, verdant_forest)]:
image.match_height(images)
image.scale(1.2)
image.next_to(vect_sym_primes[n], DOWN, buff=MED_SMALL_BUFF)
self.play(
ShowCreation(full_connections, lag_ratio=0.01, run_time=2),
LaggedStart(
(TransformFromCopy(sym1, sym2)
for sym1, sym2 in zip(vect_syms, vect_sym_primes)),
lag_ratio=0.05,
),
)
self.wait()
self.play(LaggedStart(
LaggedStart(
(FadeTransform(im.copy(), blue_fluff)
for im in images[:3]),
lag_ratio=0.02,
group_type=Group
),
LaggedStart(
(FadeTransform(im.copy(), verdant_forest)
for im in images[3:]),
lag_ratio=0.02,
group_type=Group
),
lag_ratio=0.5,
run_time=2
))
# Show black box that matrix multiples can be added to
in_arrows = VGroup(Vector(0.5 * DOWN).next_to(sym, DOWN) for sym in vect_syms)
dots = VGroup(Tex(R"\vdots").next_to(arrow, DOWN) for arrow in in_arrows)
box = Rectangle(vect_syms.get_width() + 1, 1.5)
box.set_fill(GREY_E, 1)
box.set_stroke(WHITE, 1)
box.next_to(in_arrows, DOWN)
out_arrows = in_arrows.copy()
out_arrows.next_to(box, DOWN)
self.add(box)
self.add(out_arrows)
self.add(vect_sym_primes)
# Ask questions
# Associate questions with vectors
# Show matrices
def bake_mobject_into_vector_entries(self, mob, vector, path_arc=30 * DEGREES, group_type=None):
entries = vector.get_entries()
mob_copies = mob.replicate(len(entries))
return AnimationGroup(
LaggedStart(
(FadeOutToPoint(mc, entry.get_center(), path_arc=path_arc)
for mc, entry in zip(mob_copies, entries)),
lag_ratio=0.05,
group_type=group_type,
run_time=2,
remover=True
),
RandomizeMatrixEntries(
vector,
rate_func=lambda t: clip(smooth(2 * t - 1), 0, 1),
run_time=2
),
)