Datasets:

Modalities:
Image
Size:
< 1K
Libraries:
Datasets
License:
derek-thomas commited on
Commit
3beacc0
·
verified ·
1 Parent(s): 6ee1af7

Upload LoRA.ipynb

Browse files
Files changed (1) hide show
  1. blog/multi-lora-serving/LoRA.ipynb +459 -0
blog/multi-lora-serving/LoRA.ipynb ADDED
@@ -0,0 +1,459 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "substantial-impact",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Welcome to Manim!"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 1,
14
+ "id": "11e0c778-c8bc-4972-8bc3-8dc929de6caa",
15
+ "metadata": {
16
+ "tags": []
17
+ },
18
+ "outputs": [],
19
+ "source": [
20
+ "from manim import *\n",
21
+ "\n",
22
+ "config.media_width = \"75%\"\n",
23
+ "config.verbosity = \"WARNING\""
24
+ ]
25
+ },
26
+ {
27
+ "cell_type": "markdown",
28
+ "id": "antique-knock",
29
+ "metadata": {},
30
+ "source": [
31
+ "## LoRA"
32
+ ]
33
+ },
34
+ {
35
+ "cell_type": "code",
36
+ "execution_count": 81,
37
+ "id": "f7875452-fdc2-4f5d-8da2-a333f2a82e64",
38
+ "metadata": {
39
+ "tags": []
40
+ },
41
+ "outputs": [
42
+ {
43
+ "data": {
44
+ "text/html": [
45
+ "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
46
+ "\n",
47
+ "</pre>\n"
48
+ ],
49
+ "text/plain": [
50
+ "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
51
+ "\n"
52
+ ]
53
+ },
54
+ "metadata": {},
55
+ "output_type": "display_data"
56
+ },
57
+ {
58
+ "data": {
59
+ "text/html": [
60
+ "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">[06/09/24 14:46:08] </span><span style=\"color: #800000; text-decoration-color: #800000\">WARNING </span> Output format set as webm, this can be slower than other formats <a href=\"file:///home/user/miniconda/lib/python3.9/site-packages/manim/_config/utils.py\" target=\"_blank\"><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">utils.py</span></a><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">:</span><a href=\"file:///home/user/miniconda/lib/python3.9/site-packages/manim/_config/utils.py#1061\" target=\"_blank\"><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">1061</span></a>\n",
61
+ "</pre>\n"
62
+ ],
63
+ "text/plain": [
64
+ "\u001b[2;36m[06/09/24 14:46:08]\u001b[0m\u001b[2;36m \u001b[0m\u001b[31mWARNING \u001b[0m Output format set as webm, this can be slower than other formats \u001b]8;id=477302;file:///home/user/miniconda/lib/python3.9/site-packages/manim/_config/utils.py\u001b\\\u001b[2mutils.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=649502;file:///home/user/miniconda/lib/python3.9/site-packages/manim/_config/utils.py#1061\u001b\\\u001b[2m1061\u001b[0m\u001b]8;;\u001b\\\n"
65
+ ]
66
+ },
67
+ "metadata": {},
68
+ "output_type": "display_data"
69
+ },
70
+ {
71
+ "name": "stderr",
72
+ "output_type": "stream",
73
+ "text": [
74
+ " \r"
75
+ ]
76
+ },
77
+ {
78
+ "data": {
79
+ "text/html": [
80
+ "<video src=\"media/jupyter/LoRA@[email protected]\" controls autoplay loop style=\"max-width: 75%;\" >\n",
81
+ " Your browser does not support the <code>video</code> element.\n",
82
+ " </video>"
83
+ ],
84
+ "text/plain": [
85
+ "<IPython.core.display.Video object>"
86
+ ]
87
+ },
88
+ "metadata": {},
89
+ "output_type": "display_data"
90
+ }
91
+ ],
92
+ "source": [
93
+ "%%manim -qm --format webm LoRA\n",
94
+ "\n",
95
+ "from manim import *\n",
96
+ "\n",
97
+ "class LoRA(Scene):\n",
98
+ " def construct(self):\n",
99
+ " text_rotation = PI/2\n",
100
+ " # Create the input\n",
101
+ " input_rect = Rectangle(width=1, height=4, color=GREEN).to_edge(LEFT)\n",
102
+ " input_label = Tex(\"$x \\\\in \\\\mathbb{R}^{d}$\", color=WHITE).move_to(input_rect).rotate(text_rotation)\n",
103
+ " \n",
104
+ " # Create weights\n",
105
+ " weights = Rectangle(width=3, height=3, color=BLUE).move_to(input_rect).shift(RIGHT*3.5).shift(UP*1.75)\n",
106
+ " weights_label = Tex(\"$W \\\\in \\\\mathbb{R}^{d \\\\times d}$\", color=WHITE).move_to(weights)\n",
107
+ " vector_d = Tex(\"$d$\", color=WHITE).next_to(weights, UP, buff=0.2)\n",
108
+ "\n",
109
+ " # Create trapezoids\n",
110
+ " trapezoid1 = Polygon([-2, 1, 0], [2, 1, 0], [0.5, -0.5, 0], [-0.5, -0.5, 0], color=ORANGE).rotate(PI/2).scale(0.7)\n",
111
+ " trapezoid1.next_to(weights, DOWN, buff=0.5)\n",
112
+ " trapezoid1.align_to(weights, LEFT) \n",
113
+ " trapezoid1_label = Tex(\"$A$\", color=WHITE).move_to(trapezoid1)\n",
114
+ "\n",
115
+ " trapezoid2 = Polygon([-0.5, 0.5, 0], [0.5, 0.5, 0], [2, -1, 0], [-2, -1, 0], color=ORANGE).rotate(PI/2).scale(0.7)\n",
116
+ " trapezoid2.next_to(trapezoid1, RIGHT, buff=0.5)\n",
117
+ " trapezoid2.align_to(weights, RIGHT) \n",
118
+ " trapezoid2_label = Tex(\"$B$\", color=WHITE).move_to(trapezoid2)\n",
119
+ " \n",
120
+ " r_brace = Brace(Line([-0.5, 0.5, 0], [0.5, 0.5, 0]), color=GREY).rotate(PI/2).move_to(trapezoid1, RIGHT).shift(.3*RIGHT).scale(0.7)\n",
121
+ " vector_r = Tex(\"$r$\", color=GREY).next_to(r_brace, RIGHT, buff=0.1)\n",
122
+ " \n",
123
+ " wx = Rectangle(width=1, height=3, color=PURPLE).move_to(weights, RIGHT).shift(2*RIGHT)\n",
124
+ " wx_label = Tex(\"$Wx$\").move_to(wx)\n",
125
+ " \n",
126
+ " lorax = Rectangle(width=1, height=3, color=PURPLE).move_to(trapezoid2, RIGHT).shift(2*RIGHT)\n",
127
+ " lorax_label = Tex(\"$BAx$\").move_to(lorax)\n",
128
+ " \n",
129
+ " h = Rectangle(width=1, height=4, color=RED).align_to(lorax, RIGHT).align_to(input_rect, DOWN).shift(2*RIGHT)\n",
130
+ " h_label = Tex(\"$h$\").move_to(h)\n",
131
+ "\n",
132
+ " # Arrows\n",
133
+ " arrow_input_weights = Arrow(input_rect.get_right(), weights.get_left(), buff=0.1, color=GREY)\n",
134
+ " arrow_weights_wx = Arrow(weights.get_right(), wx.get_left(), buff=0.1, color=GREY)\n",
135
+ " arrow_input_matA = Arrow(input_rect.get_right(), trapezoid1.get_left(), buff=0.1, color=GREY)\n",
136
+ " arrow_matB_BAx = Arrow(trapezoid2.get_right(), lorax.get_left(), buff=0.1, color=GREY)\n",
137
+ " \n",
138
+ " # Create Plus Sign\n",
139
+ " vertical_line = Line(UP, DOWN, stroke_width=12, color=GREY)\n",
140
+ " horizontal_line = Line(LEFT, RIGHT, stroke_width=12, color=GREY)\n",
141
+ " plus_sign = VGroup(vertical_line, horizontal_line).scale(0.25)\n",
142
+ " plus_sign.move_to(h, LEFT).shift(0.75*LEFT)\n",
143
+ " \n",
144
+ " # Legend Box\n",
145
+ " legend_box = VGroup(\n",
146
+ " Tex(\"input\", color=GREEN),\n",
147
+ " Tex(\"frozen weights\", color=BLUE),\n",
148
+ " Tex(\"low rank matrices\", color=ORANGE),\n",
149
+ " Tex(\"activations\", color=PURPLE),\n",
150
+ " Tex(\"output\", color=RED)\n",
151
+ " ).arrange(DOWN, aligned_edge=LEFT, buff=0.1).scale(0.7)\n",
152
+ " \n",
153
+ " legend_background = Rectangle(width=legend_box.width+0.6, height=legend_box.height+0.6, stroke_color=GREY)\n",
154
+ " legend_background.to_edge(RIGHT).to_edge(UP)\n",
155
+ " legend_background.align_to(weights, UP)\n",
156
+ " legend_box.move_to(legend_background.get_center())\n",
157
+ "\n",
158
+ " # Adjust elements\n",
159
+ " block_elements = [input_rect, weights, trapezoid1, trapezoid2, wx, lorax, h, legend_background]\n",
160
+ " label_elements = [input_label, weights_label, trapezoid1_label, trapezoid2_label, wx_label, lorax_label, h_label, vector_r, r_brace, legend_box]\n",
161
+ " arrow_elements = [arrow_input_weights, arrow_weights_wx, arrow_input_matA, arrow_matB_BAx, plus_sign]\n",
162
+ " for element in block_elements + label_elements + arrow_elements:\n",
163
+ " element.shift(0.2 * DOWN)\n",
164
+ "\n",
165
+ " # Add title\n",
166
+ " title = Text(\"LoRA Inference\", font_size=36, color=WHITE).to_edge(UP).shift(0.30*UP)\n",
167
+ " block_elements += title\n",
168
+ " \n",
169
+ " # Animation for emphasis\n",
170
+ " self.play(FadeIn(*block_elements), run_time=1/3)\n",
171
+ " self.wait(1/3)\n",
172
+ " \n",
173
+ " # Animation for emphasis\n",
174
+ " self.play(FadeIn(*label_elements), run_time=1/3)\n",
175
+ " self.wait(1/3)\n",
176
+ " \n",
177
+ " # Animation for emphasis\n",
178
+ " self.play(FadeIn(*arrow_elements), run_time=1/3)\n",
179
+ " self.wait(1/3)\n",
180
+ " \n",
181
+ " # Explain LoRA\n",
182
+ " explain_shift = 0.45\n",
183
+ " explain = Tex(\"$A,B $\", \"$ \\\\in \\\\mathbb{R}^{d \\\\times r}$\", \" LoRA reduces an input to a low rank dim: \", \"$r$\", color=WHITE)\n",
184
+ " explain = explain.set_color_by_tex('$A,B $', ORANGE).set_color_by_tex('$r$', YELLOW)\n",
185
+ " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n",
186
+ " self.play(FadeIn(explain), vector_r.animate.set_color(YELLOW), r_brace.animate.set_color(YELLOW), run_time=1)\n",
187
+ " self.play(Indicate(trapezoid1, rate_func=there_and_back), Indicate(trapezoid2, rate_func=there_and_back), Indicate(vector_r), Flash(r_brace), run_time=1)\n",
188
+ " self.wait(1)\n",
189
+ " self.play(FadeOut(explain), vector_r.animate.set_color(GREY), r_brace.animate.set_color(GREY), run_time=1)\n",
190
+ " self.wait(0.5)\n",
191
+ "\n",
192
+ " # Explain Wx\n",
193
+ " explain = Tex(\"$Wx$\", \" is calculated from our \", \"input\", \" and our \", \"pre-trained weights\", color=WHITE)\n",
194
+ " explain = explain.set_color_by_tex('Wx', PURPLE).set_color_by_tex(\"input\", GREEN).set_color_by_tex(\"pre-trained weights\", BLUE)\n",
195
+ " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n",
196
+ " self.play(FadeIn(explain), arrow_input_weights.animate.set_color(YELLOW), arrow_weights_wx.animate.set_color(YELLOW), run_time=1)\n",
197
+ " self.play(Indicate(input_rect, rate_func=there_and_back), Indicate(weights, rate_func=there_and_back), Indicate(wx, rate_func=there_and_back), run_time=1)\n",
198
+ " self.wait(1)\n",
199
+ " self.play(FadeOut(explain), arrow_input_weights.animate.set_color(GREY), arrow_weights_wx.animate.set_color(GREY))\n",
200
+ " self.wait(0.5)\n",
201
+ " \n",
202
+ " # Explain BAx\n",
203
+ " explain = Tex(\"$BAx$\", \" is calculated from our \", \"input\", \" and our \", \"low-rank adapters\", color=WHITE)\n",
204
+ " explain = explain.set_color_by_tex('BAx', PURPLE).set_color_by_tex(\"input\", GREEN).set_color_by_tex(\"low-rank adapters\", ORANGE)\n",
205
+ " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n",
206
+ " self.play(FadeIn(explain), arrow_input_matA.animate.set_color(YELLOW), arrow_matB_BAx.animate.set_color(YELLOW), run_time=1)\n",
207
+ " self.play(Indicate(input_rect, rate_func=there_and_back), Indicate(trapezoid1, rate_func=there_and_back), Indicate(trapezoid2, rate_func=there_and_back), Indicate(lorax, rate_func=there_and_back), run_time=1)\n",
208
+ " self.wait(1)\n",
209
+ " self.play(FadeOut(explain), arrow_input_matA.animate.set_color(GREY), arrow_matB_BAx.animate.set_color(GREY))\n",
210
+ " self.wait(0.5)\n",
211
+ " \n",
212
+ " # Explain h\n",
213
+ " explain = Tex(\"$h=Wx + BAx$\", \" is the sum of \", \"$Wx$\", \" and \", \"$BAx$\", color=WHITE)\n",
214
+ " explain = explain.set_color_by_tex('$h=Wx + BAx$', RED).set_color_by_tex(\"$Wx$\", PURPLE).set_color_by_tex(\"$BAx$\", PURPLE)\n",
215
+ " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n",
216
+ " self.play(FadeIn(explain), plus_sign.animate.set_color(YELLOW), run_time=1)\n",
217
+ " self.play(Indicate(wx, rate_func=there_and_back), Indicate(lorax, rate_func=there_and_back), Indicate(h, rate_func=there_and_back), run_time=1)\n",
218
+ " self.wait(1)\n",
219
+ " self.play(FadeOut(explain), plus_sign.animate.set_color(GREY), run_time=1)\n",
220
+ " self.wait(0.5)\n",
221
+ " "
222
+ ]
223
+ },
224
+ {
225
+ "cell_type": "code",
226
+ "execution_count": 82,
227
+ "id": "cbe45556-cbb0-47a1-8d21-f25ff1f4e0d3",
228
+ "metadata": {
229
+ "tags": []
230
+ },
231
+ "outputs": [
232
+ {
233
+ "data": {
234
+ "text/html": [
235
+ "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Manim Community <span style=\"color: #008000; text-decoration-color: #008000\">v0.18.1</span>\n",
236
+ "\n",
237
+ "</pre>\n"
238
+ ],
239
+ "text/plain": [
240
+ "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n",
241
+ "\n"
242
+ ]
243
+ },
244
+ "metadata": {},
245
+ "output_type": "display_data"
246
+ },
247
+ {
248
+ "data": {
249
+ "text/html": [
250
+ "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">[06/09/24 14:47:19] </span><span style=\"color: #800000; text-decoration-color: #800000\">WARNING </span> Output format set as webm, this can be slower than other formats <a href=\"file:///home/user/miniconda/lib/python3.9/site-packages/manim/_config/utils.py\" target=\"_blank\"><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">utils.py</span></a><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">:</span><a href=\"file:///home/user/miniconda/lib/python3.9/site-packages/manim/_config/utils.py#1061\" target=\"_blank\"><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">1061</span></a>\n",
251
+ "</pre>\n"
252
+ ],
253
+ "text/plain": [
254
+ "\u001b[2;36m[06/09/24 14:47:19]\u001b[0m\u001b[2;36m \u001b[0m\u001b[31mWARNING \u001b[0m Output format set as webm, this can be slower than other formats \u001b]8;id=998741;file:///home/user/miniconda/lib/python3.9/site-packages/manim/_config/utils.py\u001b\\\u001b[2mutils.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=978614;file:///home/user/miniconda/lib/python3.9/site-packages/manim/_config/utils.py#1061\u001b\\\u001b[2m1061\u001b[0m\u001b]8;;\u001b\\\n"
255
+ ]
256
+ },
257
+ "metadata": {},
258
+ "output_type": "display_data"
259
+ },
260
+ {
261
+ "name": "stderr",
262
+ "output_type": "stream",
263
+ "text": [
264
+ " \r"
265
+ ]
266
+ },
267
+ {
268
+ "data": {
269
+ "text/html": [
270
+ "<video src=\"media/jupyter/MultiLoRA@[email protected]\" controls autoplay loop style=\"max-width: 75%;\" >\n",
271
+ " Your browser does not support the <code>video</code> element.\n",
272
+ " </video>"
273
+ ],
274
+ "text/plain": [
275
+ "<IPython.core.display.Video object>"
276
+ ]
277
+ },
278
+ "metadata": {},
279
+ "output_type": "display_data"
280
+ }
281
+ ],
282
+ "source": [
283
+ "%%manim -qm --format webm MultiLoRA\n",
284
+ "\n",
285
+ "from manim import *\n",
286
+ "\n",
287
+ "class MultiLoRA(Scene):\n",
288
+ " def construct(self):\n",
289
+ " # Add title\n",
290
+ " title = Text(\"Multi-LoRA Inference\", font_size=36, color=WHITE).to_edge(UP).shift(0.30*UP)\n",
291
+ " \n",
292
+ " # Create the base input\n",
293
+ " x_width = 2.5\n",
294
+ " input_rect = Rectangle(width=x_width, height=2, color=GREEN).to_edge(LEFT, buff=0.5)\n",
295
+ " input_label = Tex(\"$x$\", color=WHITE).move_to(input_rect)\n",
296
+ "\n",
297
+ " # Create W matrix rectangle\n",
298
+ " w_rect = Rectangle(width=2.25, height=2.25, color=BLUE).next_to(input_rect, RIGHT, buff=1.25).shift(1.25*UP)\n",
299
+ " w_label = Text(\"W\", color=WHITE).scale(0.75).move_to(w_rect)\n",
300
+ "\n",
301
+ " # Create batched inputs for weight multiplication\n",
302
+ " x1_w = Rectangle(width=x_width, height=0.75, color=GREEN).next_to(w_rect, RIGHT, buff=1.125).shift(0.75*UP)\n",
303
+ " x1_w_label = Tex(\"$x_1$\", color=WHITE).move_to(x1_w)\n",
304
+ " x2_w = Rectangle(width=x_width, height=1.25, color=GREEN).next_to(x1_w, DOWN)\n",
305
+ " x2_w_label = Tex(\"$x_2$\", color=WHITE).move_to(x2_w)\n",
306
+ "\n",
307
+ " # Create LoRA computation blocks\n",
308
+ " l1_rect = Rectangle(width=2.25, height=1, color=ORANGE).next_to(input_rect, RIGHT, buff=1.25).shift(1.25*DOWN)\n",
309
+ " l1_label = Tex(\"$B_1 * A_1$\", color=WHITE).scale(0.75).move_to(l1_rect)\n",
310
+ " l2_rect = Rectangle(width=2.25, height=1, color=ORANGE).next_to(l1_rect, DOWN)\n",
311
+ " l2_label = Tex(\"$B_2 * A_2$\", color=WHITE).scale(0.75).move_to(l2_rect)\n",
312
+ " \n",
313
+ " x1_l = Rectangle(width=x_width, height=0.75, color=GREEN).next_to(l1_rect, RIGHT, buff=1.125).align_to(l1_rect, UP)\n",
314
+ " x1_l_label = Tex(\"$x_1$\", color=WHITE).move_to(x1_l)\n",
315
+ " x2_l = Rectangle(width=x_width, height=1.25, color=GREEN).next_to(x1_l, DOWN)\n",
316
+ " x2_l_label = Tex(\"$x_2$\", color=WHITE).move_to(x2_l)\n",
317
+ "\n",
318
+ " # Grouping boxes\n",
319
+ " group_box_w = SurroundingRectangle(VGroup(x1_w, x2_w, w_rect), color=GREY, buff=0.1)\n",
320
+ " group_box_l = SurroundingRectangle(VGroup(x1_l, x2_l, l1_rect, l2_rect), color=GREY, buff=0.1)\n",
321
+ "\n",
322
+ " # Labels for Grouping Boxes\n",
323
+ " group_box_w_label = Text(\"Batched Base Computation\", font_size=24, color=GREY).next_to(group_box_w, UP, buff=0.1)\n",
324
+ " group_box_l_label = Text(\"Batched LoRA Computation\", font_size=24, color=GREY).next_to(group_box_l, UP, buff=0.1)\n",
325
+ "\n",
326
+ " # Create Multiplication Sign\n",
327
+ " times_x = (x2_w.get_left()[0] + w_rect.get_right()[0]) / 2\n",
328
+ " times_sign_w = self.create_multiplication_sign(w_rect.get_center()).set_x(times_x)\n",
329
+ " times_sign_l = self.create_multiplication_sign(VGroup(l1_rect, l2_rect).get_center()).set_x(times_x)\n",
330
+ " \n",
331
+ " # Output\n",
332
+ " output_rect = Rectangle(width=x_width, height=2, color=RED).to_edge(RIGHT, buff=0.5)\n",
333
+ " output_label = Tex(\"$h$\", color=WHITE).move_to(output_rect)\n",
334
+ " \n",
335
+ " # Create Plus Sign\n",
336
+ " plus_x = (group_box_w.get_right()[0] + output_rect.get_left()[0]) / 2\n",
337
+ " plus_sign = self.create_plus_sign(output_rect.get_center()).set_x(plus_x)\n",
338
+ " \n",
339
+ " # Create Arrows\n",
340
+ " arrow_input_weights = Arrow(input_rect.get_right(), group_box_w.get_left(), buff=0.1, color=GREY)\n",
341
+ " arrow_input_lora = Arrow(input_rect.get_right(), group_box_l.get_left(), buff=0.1, color=GREY)\n",
342
+ " \n",
343
+ " # Grouping elements\n",
344
+ " block_elements = [input_rect, x1_w, x2_w, w_rect, x1_l, x2_l, l1_rect, l2_rect, output_rect]\n",
345
+ " label_elements = [input_label, x1_w_label, x2_w_label, w_label, x1_l_label, x2_l_label, l1_label, l2_label, output_label]\n",
346
+ " arrow_elements = [arrow_input_weights, arrow_input_lora]\n",
347
+ " sign_elements = [times_sign_w, times_sign_l, plus_sign]\n",
348
+ " group_box_elements = [group_box_w, group_box_l, group_box_w_label, group_box_l_label]\n",
349
+ " \n",
350
+ " # Fade in elements\n",
351
+ " self.play(FadeIn(title), run_time=1/3)\n",
352
+ " self.play(*[FadeIn(element) for element in block_elements + label_elements], run_time=1/3)\n",
353
+ " self.play(*[FadeIn(element) for element in arrow_elements], run_time=1/3)\n",
354
+ " self.play(*[FadeIn(element) for element in sign_elements], run_time=1/3)\n",
355
+ " self.play(*[FadeIn(element) for element in group_box_elements], run_time=1/3)\n",
356
+ "\n",
357
+ " # Explain LoRA\n",
358
+ " explain_shift = 0.3\n",
359
+ " \n",
360
+ " explain = Tex(\"$x$\", \" is a heterogeneous batch of \", \"inputs\", \" to different \", \" LoRA adapters\", color=WHITE)\n",
361
+ " explain = explain.set_color_by_tex('$x$', GREEN).set_color_by_tex('inputs', GREEN).set_color_by_tex(' LoRA adapters', ORANGE)\n",
362
+ " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n",
363
+ " x_elements = [x1_w, x2_w, x1_l, x2_l]\n",
364
+ " self.play(FadeIn(explain), run_time=2/3)\n",
365
+ " self.play(Indicate(input_rect), run_time=2/3)\n",
366
+ " self.play(*[Indicate(arrow_element, rate_func=there_and_back) for arrow_element in arrow_elements], run_time=2/3)\n",
367
+ " self.play(*[Indicate(x_element, rate_func=there_and_back) for x_element in x_elements], run_time=1)\n",
368
+ " self.wait(1)\n",
369
+ " self.play(FadeOut(explain), run_time=1)\n",
370
+ " self.wait(0.5)\n",
371
+ "\n",
372
+ " # Explain Wx\n",
373
+ " explain = Tex(\"Batched Base Computation\", \" = \", \"frozen weights\", \" $*$ \", \"input\", color=WHITE)\n",
374
+ " explain = explain.set_color_by_tex('input', GREEN).set_color_by_tex('frozen weights', BLUE)\n",
375
+ " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n",
376
+ " x_elements = [x1_w, x2_w]\n",
377
+ " self.play(FadeIn(explain), group_box_w.animate.set_color(YELLOW), group_box_w_label.animate.set_color(YELLOW), run_time=1)\n",
378
+ " self.play(Indicate(w_rect), run_time=0.5)\n",
379
+ " self.play(Indicate(times_sign_w), run_time=0.5)\n",
380
+ " self.play(*[Indicate(x_element, rate_func=there_and_back) for x_element in x_elements], run_time=0.5)\n",
381
+ " self.wait(1)\n",
382
+ " self.play(FadeOut(explain), group_box_w.animate.set_color(GREY), group_box_w_label.animate.set_color(GREY), run_time=1)\n",
383
+ " self.wait(0.5)\n",
384
+ "\n",
385
+ " # Explain BAx\n",
386
+ " explain = Tex(\"Batched LoRA Computation\", \" = \", \"LoRA adapters\", \" $*$ \", \"input\", color=WHITE)\n",
387
+ " explain = explain.set_color_by_tex('input', GREEN).set_color_by_tex('LoRA adapters', ORANGE)\n",
388
+ " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n",
389
+ " x_elements = [x1_l, x2_l]\n",
390
+ " l_elements = [l1_rect, l2_rect]\n",
391
+ " self.play(FadeIn(explain), group_box_l.animate.set_color(YELLOW), group_box_l_label.animate.set_color(YELLOW), run_time=1)\n",
392
+ " self.play(*[Indicate(l_element, rate_func=there_and_back) for l_element in l_elements], run_time=0.5)\n",
393
+ " self.play(Indicate(times_sign_l), run_time=0.5)\n",
394
+ " self.play(*[Indicate(x_element, rate_func=there_and_back) for x_element in x_elements], run_time=0.5)\n",
395
+ " self.wait(1)\n",
396
+ " self.play(FadeOut(explain), group_box_l.animate.set_color(GREY), group_box_l_label.animate.set_color(GREY), run_time=1)\n",
397
+ " self.wait(0.5)\n",
398
+ "\n",
399
+ " # Explain Output\n",
400
+ " explain = Tex(\"$h$\", \" = \", \"Batched Base Computation\", \" + \", \"Batched LoRA Computation\", color=WHITE)\n",
401
+ " explain = explain.set_color_by_tex(\"$h$\", RED).set_color_by_tex('Batched Base Computation', BLUE).set_color_by_tex('Batched LoRA Computation', ORANGE)\n",
402
+ " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n",
403
+ " x_elements = [x1_l, x2_l]\n",
404
+ " l_elements = [l1_rect, l2_rect]\n",
405
+ " self.play(FadeIn(explain),group_box_w.animate.set_color(BLUE), group_box_w_label.animate.set_color(BLUE), group_box_l.animate.set_color(ORANGE), group_box_l_label.animate.set_color(ORANGE), run_time=1)\n",
406
+ " self.play(Indicate(output_rect), run_time=0.5)\n",
407
+ " self.play(Indicate(VGroup(group_box_w, group_box_w_label)), run_time=0.5)\n",
408
+ " self.play(Indicate(plus_sign), run_time=0.5)\n",
409
+ " self.play(Indicate(VGroup(group_box_l, group_box_l_label)), run_time=0.5)\n",
410
+ " self.wait(1)\n",
411
+ " self.play(FadeOut(explain),group_box_w.animate.set_color(GREY), group_box_w_label.animate.set_color(GREY), group_box_l.animate.set_color(GREY), group_box_l_label.animate.set_color(GREY), run_time=1)\n",
412
+ " self.wait(0.5)\n",
413
+ "\n",
414
+ " def create_multiplication_sign(self, position):\n",
415
+ " vertical_line = Line(UP*0.5 + LEFT*0.5, DOWN*0.5 + RIGHT*0.5, stroke_width=12, color=GREY)\n",
416
+ " horizontal_line = Line(UP*0.5 + RIGHT*0.5, DOWN*0.5 + LEFT*0.5, stroke_width=12, color=GREY)\n",
417
+ " times_sign = VGroup(vertical_line, horizontal_line).scale(0.25)\n",
418
+ " times_sign.move_to(position)\n",
419
+ " return times_sign\n",
420
+ "\n",
421
+ " def create_plus_sign(self, position):\n",
422
+ " vertical_line = Line(UP, DOWN, stroke_width=12, color=GREY)\n",
423
+ " horizontal_line = Line(LEFT, RIGHT, stroke_width=12, color=GREY)\n",
424
+ " plus_sign = VGroup(vertical_line, horizontal_line).scale(0.25)\n",
425
+ " plus_sign.move_to(position)\n",
426
+ " return plus_sign\n"
427
+ ]
428
+ },
429
+ {
430
+ "cell_type": "code",
431
+ "execution_count": null,
432
+ "id": "afdd5793-1466-4d05-b3e5-e926cf509e9f",
433
+ "metadata": {},
434
+ "outputs": [],
435
+ "source": []
436
+ }
437
+ ],
438
+ "metadata": {
439
+ "kernelspec": {
440
+ "display_name": "Python 3 (ipykernel)",
441
+ "language": "python",
442
+ "name": "python3"
443
+ },
444
+ "language_info": {
445
+ "codemirror_mode": {
446
+ "name": "ipython",
447
+ "version": 3
448
+ },
449
+ "file_extension": ".py",
450
+ "mimetype": "text/x-python",
451
+ "name": "python",
452
+ "nbconvert_exporter": "python",
453
+ "pygments_lexer": "ipython3",
454
+ "version": "3.9.5"
455
+ }
456
+ },
457
+ "nbformat": 4,
458
+ "nbformat_minor": 5
459
+ }