{ "cells": [ { "cell_type": "markdown", "id": "substantial-impact", "metadata": {}, "source": [ "# Welcome to Manim!" ] }, { "cell_type": "code", "execution_count": 1, "id": "11e0c778-c8bc-4972-8bc3-8dc929de6caa", "metadata": { "tags": [] }, "outputs": [], "source": [ "from manim import *\n", "\n", "config.media_width = \"75%\"\n", "config.verbosity = \"WARNING\"" ] }, { "cell_type": "markdown", "id": "antique-knock", "metadata": {}, "source": [ "## LoRA" ] }, { "cell_type": "code", "execution_count": 81, "id": "f7875452-fdc2-4f5d-8da2-a333f2a82e64", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
Manim Community v0.18.1\n",
       "\n",
       "
\n" ], "text/plain": [ "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n", "\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
[06/09/24 14:46:08] WARNING  Output format set as webm, this can be slower than other formats         utils.py:1061\n",
       "
\n" ], "text/plain": [ "\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" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%manim -qm --format webm LoRA\n", "\n", "from manim import *\n", "\n", "class LoRA(Scene):\n", " def construct(self):\n", " text_rotation = PI/2\n", " # Create the input\n", " input_rect = Rectangle(width=1, height=4, color=GREEN).to_edge(LEFT)\n", " input_label = Tex(\"$x \\\\in \\\\mathbb{R}^{d}$\", color=WHITE).move_to(input_rect).rotate(text_rotation)\n", " \n", " # Create weights\n", " weights = Rectangle(width=3, height=3, color=BLUE).move_to(input_rect).shift(RIGHT*3.5).shift(UP*1.75)\n", " weights_label = Tex(\"$W \\\\in \\\\mathbb{R}^{d \\\\times d}$\", color=WHITE).move_to(weights)\n", " vector_d = Tex(\"$d$\", color=WHITE).next_to(weights, UP, buff=0.2)\n", "\n", " # Create trapezoids\n", " 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", " trapezoid1.next_to(weights, DOWN, buff=0.5)\n", " trapezoid1.align_to(weights, LEFT) \n", " trapezoid1_label = Tex(\"$A$\", color=WHITE).move_to(trapezoid1)\n", "\n", " 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", " trapezoid2.next_to(trapezoid1, RIGHT, buff=0.5)\n", " trapezoid2.align_to(weights, RIGHT) \n", " trapezoid2_label = Tex(\"$B$\", color=WHITE).move_to(trapezoid2)\n", " \n", " 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", " vector_r = Tex(\"$r$\", color=GREY).next_to(r_brace, RIGHT, buff=0.1)\n", " \n", " wx = Rectangle(width=1, height=3, color=PURPLE).move_to(weights, RIGHT).shift(2*RIGHT)\n", " wx_label = Tex(\"$Wx$\").move_to(wx)\n", " \n", " lorax = Rectangle(width=1, height=3, color=PURPLE).move_to(trapezoid2, RIGHT).shift(2*RIGHT)\n", " lorax_label = Tex(\"$BAx$\").move_to(lorax)\n", " \n", " h = Rectangle(width=1, height=4, color=RED).align_to(lorax, RIGHT).align_to(input_rect, DOWN).shift(2*RIGHT)\n", " h_label = Tex(\"$h$\").move_to(h)\n", "\n", " # Arrows\n", " arrow_input_weights = Arrow(input_rect.get_right(), weights.get_left(), buff=0.1, color=GREY)\n", " arrow_weights_wx = Arrow(weights.get_right(), wx.get_left(), buff=0.1, color=GREY)\n", " arrow_input_matA = Arrow(input_rect.get_right(), trapezoid1.get_left(), buff=0.1, color=GREY)\n", " arrow_matB_BAx = Arrow(trapezoid2.get_right(), lorax.get_left(), buff=0.1, color=GREY)\n", " \n", " # Create Plus Sign\n", " vertical_line = Line(UP, DOWN, stroke_width=12, color=GREY)\n", " horizontal_line = Line(LEFT, RIGHT, stroke_width=12, color=GREY)\n", " plus_sign = VGroup(vertical_line, horizontal_line).scale(0.25)\n", " plus_sign.move_to(h, LEFT).shift(0.75*LEFT)\n", " \n", " # Legend Box\n", " legend_box = VGroup(\n", " Tex(\"input\", color=GREEN),\n", " Tex(\"frozen weights\", color=BLUE),\n", " Tex(\"low rank matrices\", color=ORANGE),\n", " Tex(\"activations\", color=PURPLE),\n", " Tex(\"output\", color=RED)\n", " ).arrange(DOWN, aligned_edge=LEFT, buff=0.1).scale(0.7)\n", " \n", " legend_background = Rectangle(width=legend_box.width+0.6, height=legend_box.height+0.6, stroke_color=GREY)\n", " legend_background.to_edge(RIGHT).to_edge(UP)\n", " legend_background.align_to(weights, UP)\n", " legend_box.move_to(legend_background.get_center())\n", "\n", " # Adjust elements\n", " block_elements = [input_rect, weights, trapezoid1, trapezoid2, wx, lorax, h, legend_background]\n", " label_elements = [input_label, weights_label, trapezoid1_label, trapezoid2_label, wx_label, lorax_label, h_label, vector_r, r_brace, legend_box]\n", " arrow_elements = [arrow_input_weights, arrow_weights_wx, arrow_input_matA, arrow_matB_BAx, plus_sign]\n", " for element in block_elements + label_elements + arrow_elements:\n", " element.shift(0.2 * DOWN)\n", "\n", " # Add title\n", " title = Text(\"LoRA Inference\", font_size=36, color=WHITE).to_edge(UP).shift(0.30*UP)\n", " block_elements += title\n", " \n", " # Animation for emphasis\n", " self.play(FadeIn(*block_elements), run_time=1/3)\n", " self.wait(1/3)\n", " \n", " # Animation for emphasis\n", " self.play(FadeIn(*label_elements), run_time=1/3)\n", " self.wait(1/3)\n", " \n", " # Animation for emphasis\n", " self.play(FadeIn(*arrow_elements), run_time=1/3)\n", " self.wait(1/3)\n", " \n", " # Explain LoRA\n", " explain_shift = 0.45\n", " explain = Tex(\"$A,B $\", \"$ \\\\in \\\\mathbb{R}^{d \\\\times r}$\", \" LoRA reduces an input to a low rank dim: \", \"$r$\", color=WHITE)\n", " explain = explain.set_color_by_tex('$A,B $', ORANGE).set_color_by_tex('$r$', YELLOW)\n", " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n", " self.play(FadeIn(explain), vector_r.animate.set_color(YELLOW), r_brace.animate.set_color(YELLOW), run_time=1)\n", " 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", " self.wait(1)\n", " self.play(FadeOut(explain), vector_r.animate.set_color(GREY), r_brace.animate.set_color(GREY), run_time=1)\n", " self.wait(0.5)\n", "\n", " # Explain Wx\n", " explain = Tex(\"$Wx$\", \" is calculated from our \", \"input\", \" and our \", \"pre-trained weights\", color=WHITE)\n", " explain = explain.set_color_by_tex('Wx', PURPLE).set_color_by_tex(\"input\", GREEN).set_color_by_tex(\"pre-trained weights\", BLUE)\n", " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n", " self.play(FadeIn(explain), arrow_input_weights.animate.set_color(YELLOW), arrow_weights_wx.animate.set_color(YELLOW), run_time=1)\n", " 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", " self.wait(1)\n", " self.play(FadeOut(explain), arrow_input_weights.animate.set_color(GREY), arrow_weights_wx.animate.set_color(GREY))\n", " self.wait(0.5)\n", " \n", " # Explain BAx\n", " explain = Tex(\"$BAx$\", \" is calculated from our \", \"input\", \" and our \", \"low-rank adapters\", color=WHITE)\n", " explain = explain.set_color_by_tex('BAx', PURPLE).set_color_by_tex(\"input\", GREEN).set_color_by_tex(\"low-rank adapters\", ORANGE)\n", " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n", " self.play(FadeIn(explain), arrow_input_matA.animate.set_color(YELLOW), arrow_matB_BAx.animate.set_color(YELLOW), run_time=1)\n", " 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", " self.wait(1)\n", " self.play(FadeOut(explain), arrow_input_matA.animate.set_color(GREY), arrow_matB_BAx.animate.set_color(GREY))\n", " self.wait(0.5)\n", " \n", " # Explain h\n", " explain = Tex(\"$h=Wx + BAx$\", \" is the sum of \", \"$Wx$\", \" and \", \"$BAx$\", color=WHITE)\n", " explain = explain.set_color_by_tex('$h=Wx + BAx$', RED).set_color_by_tex(\"$Wx$\", PURPLE).set_color_by_tex(\"$BAx$\", PURPLE)\n", " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n", " self.play(FadeIn(explain), plus_sign.animate.set_color(YELLOW), run_time=1)\n", " 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", " self.wait(1)\n", " self.play(FadeOut(explain), plus_sign.animate.set_color(GREY), run_time=1)\n", " self.wait(0.5)\n", " " ] }, { "cell_type": "code", "execution_count": 82, "id": "cbe45556-cbb0-47a1-8d21-f25ff1f4e0d3", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
Manim Community v0.18.1\n",
       "\n",
       "
\n" ], "text/plain": [ "Manim Community \u001b[32mv0.\u001b[0m\u001b[32m18.1\u001b[0m\n", "\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
[06/09/24 14:47:19] WARNING  Output format set as webm, this can be slower than other formats         utils.py:1061\n",
       "
\n" ], "text/plain": [ "\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" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ " \r" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%manim -qm --format webm MultiLoRA\n", "\n", "from manim import *\n", "\n", "class MultiLoRA(Scene):\n", " def construct(self):\n", " # Add title\n", " title = Text(\"Multi-LoRA Inference\", font_size=36, color=WHITE).to_edge(UP).shift(0.30*UP)\n", " \n", " # Create the base input\n", " x_width = 2.5\n", " input_rect = Rectangle(width=x_width, height=2, color=GREEN).to_edge(LEFT, buff=0.5)\n", " input_label = Tex(\"$x$\", color=WHITE).move_to(input_rect)\n", "\n", " # Create W matrix rectangle\n", " w_rect = Rectangle(width=2.25, height=2.25, color=BLUE).next_to(input_rect, RIGHT, buff=1.25).shift(1.25*UP)\n", " w_label = Text(\"W\", color=WHITE).scale(0.75).move_to(w_rect)\n", "\n", " # Create batched inputs for weight multiplication\n", " x1_w = Rectangle(width=x_width, height=0.75, color=GREEN).next_to(w_rect, RIGHT, buff=1.125).shift(0.75*UP)\n", " x1_w_label = Tex(\"$x_1$\", color=WHITE).move_to(x1_w)\n", " x2_w = Rectangle(width=x_width, height=1.25, color=GREEN).next_to(x1_w, DOWN)\n", " x2_w_label = Tex(\"$x_2$\", color=WHITE).move_to(x2_w)\n", "\n", " # Create LoRA computation blocks\n", " l1_rect = Rectangle(width=2.25, height=1, color=ORANGE).next_to(input_rect, RIGHT, buff=1.25).shift(1.25*DOWN)\n", " l1_label = Tex(\"$B_1 * A_1$\", color=WHITE).scale(0.75).move_to(l1_rect)\n", " l2_rect = Rectangle(width=2.25, height=1, color=ORANGE).next_to(l1_rect, DOWN)\n", " l2_label = Tex(\"$B_2 * A_2$\", color=WHITE).scale(0.75).move_to(l2_rect)\n", " \n", " 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", " x1_l_label = Tex(\"$x_1$\", color=WHITE).move_to(x1_l)\n", " x2_l = Rectangle(width=x_width, height=1.25, color=GREEN).next_to(x1_l, DOWN)\n", " x2_l_label = Tex(\"$x_2$\", color=WHITE).move_to(x2_l)\n", "\n", " # Grouping boxes\n", " group_box_w = SurroundingRectangle(VGroup(x1_w, x2_w, w_rect), color=GREY, buff=0.1)\n", " group_box_l = SurroundingRectangle(VGroup(x1_l, x2_l, l1_rect, l2_rect), color=GREY, buff=0.1)\n", "\n", " # Labels for Grouping Boxes\n", " group_box_w_label = Text(\"Batched Base Computation\", font_size=24, color=GREY).next_to(group_box_w, UP, buff=0.1)\n", " group_box_l_label = Text(\"Batched LoRA Computation\", font_size=24, color=GREY).next_to(group_box_l, UP, buff=0.1)\n", "\n", " # Create Multiplication Sign\n", " times_x = (x2_w.get_left()[0] + w_rect.get_right()[0]) / 2\n", " times_sign_w = self.create_multiplication_sign(w_rect.get_center()).set_x(times_x)\n", " times_sign_l = self.create_multiplication_sign(VGroup(l1_rect, l2_rect).get_center()).set_x(times_x)\n", " \n", " # Output\n", " output_rect = Rectangle(width=x_width, height=2, color=RED).to_edge(RIGHT, buff=0.5)\n", " output_label = Tex(\"$h$\", color=WHITE).move_to(output_rect)\n", " \n", " # Create Plus Sign\n", " plus_x = (group_box_w.get_right()[0] + output_rect.get_left()[0]) / 2\n", " plus_sign = self.create_plus_sign(output_rect.get_center()).set_x(plus_x)\n", " \n", " # Create Arrows\n", " arrow_input_weights = Arrow(input_rect.get_right(), group_box_w.get_left(), buff=0.1, color=GREY)\n", " arrow_input_lora = Arrow(input_rect.get_right(), group_box_l.get_left(), buff=0.1, color=GREY)\n", " \n", " # Grouping elements\n", " block_elements = [input_rect, x1_w, x2_w, w_rect, x1_l, x2_l, l1_rect, l2_rect, output_rect]\n", " 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", " arrow_elements = [arrow_input_weights, arrow_input_lora]\n", " sign_elements = [times_sign_w, times_sign_l, plus_sign]\n", " group_box_elements = [group_box_w, group_box_l, group_box_w_label, group_box_l_label]\n", " \n", " # Fade in elements\n", " self.play(FadeIn(title), run_time=1/3)\n", " self.play(*[FadeIn(element) for element in block_elements + label_elements], run_time=1/3)\n", " self.play(*[FadeIn(element) for element in arrow_elements], run_time=1/3)\n", " self.play(*[FadeIn(element) for element in sign_elements], run_time=1/3)\n", " self.play(*[FadeIn(element) for element in group_box_elements], run_time=1/3)\n", "\n", " # Explain LoRA\n", " explain_shift = 0.3\n", " \n", " explain = Tex(\"$x$\", \" is a heterogeneous batch of \", \"inputs\", \" to different \", \" LoRA adapters\", color=WHITE)\n", " explain = explain.set_color_by_tex('$x$', GREEN).set_color_by_tex('inputs', GREEN).set_color_by_tex(' LoRA adapters', ORANGE)\n", " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n", " x_elements = [x1_w, x2_w, x1_l, x2_l]\n", " self.play(FadeIn(explain), run_time=2/3)\n", " self.play(Indicate(input_rect), run_time=2/3)\n", " self.play(*[Indicate(arrow_element, rate_func=there_and_back) for arrow_element in arrow_elements], run_time=2/3)\n", " self.play(*[Indicate(x_element, rate_func=there_and_back) for x_element in x_elements], run_time=1)\n", " self.wait(1)\n", " self.play(FadeOut(explain), run_time=1)\n", " self.wait(0.5)\n", "\n", " # Explain Wx\n", " explain = Tex(\"Batched Base Computation\", \" = \", \"frozen weights\", \" $*$ \", \"input\", color=WHITE)\n", " explain = explain.set_color_by_tex('input', GREEN).set_color_by_tex('frozen weights', BLUE)\n", " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n", " x_elements = [x1_w, x2_w]\n", " self.play(FadeIn(explain), group_box_w.animate.set_color(YELLOW), group_box_w_label.animate.set_color(YELLOW), run_time=1)\n", " self.play(Indicate(w_rect), run_time=0.5)\n", " self.play(Indicate(times_sign_w), run_time=0.5)\n", " self.play(*[Indicate(x_element, rate_func=there_and_back) for x_element in x_elements], run_time=0.5)\n", " self.wait(1)\n", " self.play(FadeOut(explain), group_box_w.animate.set_color(GREY), group_box_w_label.animate.set_color(GREY), run_time=1)\n", " self.wait(0.5)\n", "\n", " # Explain BAx\n", " explain = Tex(\"Batched LoRA Computation\", \" = \", \"LoRA adapters\", \" $*$ \", \"input\", color=WHITE)\n", " explain = explain.set_color_by_tex('input', GREEN).set_color_by_tex('LoRA adapters', ORANGE)\n", " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n", " x_elements = [x1_l, x2_l]\n", " l_elements = [l1_rect, l2_rect]\n", " self.play(FadeIn(explain), group_box_l.animate.set_color(YELLOW), group_box_l_label.animate.set_color(YELLOW), run_time=1)\n", " self.play(*[Indicate(l_element, rate_func=there_and_back) for l_element in l_elements], run_time=0.5)\n", " self.play(Indicate(times_sign_l), run_time=0.5)\n", " self.play(*[Indicate(x_element, rate_func=there_and_back) for x_element in x_elements], run_time=0.5)\n", " self.wait(1)\n", " self.play(FadeOut(explain), group_box_l.animate.set_color(GREY), group_box_l_label.animate.set_color(GREY), run_time=1)\n", " self.wait(0.5)\n", "\n", " # Explain Output\n", " explain = Tex(\"$h$\", \" = \", \"Batched Base Computation\", \" + \", \"Batched LoRA Computation\", color=WHITE)\n", " 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", " explain = explain.to_edge(DOWN).shift(explain_shift*DOWN)\n", " x_elements = [x1_l, x2_l]\n", " l_elements = [l1_rect, l2_rect]\n", " 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", " self.play(Indicate(output_rect), run_time=0.5)\n", " self.play(Indicate(VGroup(group_box_w, group_box_w_label)), run_time=0.5)\n", " self.play(Indicate(plus_sign), run_time=0.5)\n", " self.play(Indicate(VGroup(group_box_l, group_box_l_label)), run_time=0.5)\n", " self.wait(1)\n", " 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", " self.wait(0.5)\n", "\n", " def create_multiplication_sign(self, position):\n", " vertical_line = Line(UP*0.5 + LEFT*0.5, DOWN*0.5 + RIGHT*0.5, stroke_width=12, color=GREY)\n", " horizontal_line = Line(UP*0.5 + RIGHT*0.5, DOWN*0.5 + LEFT*0.5, stroke_width=12, color=GREY)\n", " times_sign = VGroup(vertical_line, horizontal_line).scale(0.25)\n", " times_sign.move_to(position)\n", " return times_sign\n", "\n", " def create_plus_sign(self, position):\n", " vertical_line = Line(UP, DOWN, stroke_width=12, color=GREY)\n", " horizontal_line = Line(LEFT, RIGHT, stroke_width=12, color=GREY)\n", " plus_sign = VGroup(vertical_line, horizontal_line).scale(0.25)\n", " plus_sign.move_to(position)\n", " return plus_sign\n" ] }, { "cell_type": "code", "execution_count": null, "id": "afdd5793-1466-4d05-b3e5-e926cf509e9f", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.5" } }, "nbformat": 4, "nbformat_minor": 5 }