{ "cells": [ { "cell_type": "markdown", "id": "substantial-impact", "metadata": {}, "source": [ "# Welcome to Manim!" ] }, { "cell_type": "markdown", "id": "first-armenia", "metadata": {}, "source": [ "This is a temporary test environment in which you can play around with Manim without the need of installing it locally. Some basic knowledge of Python is helpful! Keep in mind that this is a *temporary* environment, though: your changes will not be saved and cannot be shared with others. To save your work, you will need to download the notebook file (\"File > Download as > Notebook (.ipynb)\"). Enjoy!\n", "\n", "> *Useful resources:* [Documentation](https://docs.manim.community), [Discord](https://discord.gg/mMRrZQW), [Reddit](https://www.reddit.com/r/manim/)" ] }, { "cell_type": "markdown", "id": "honest-cruise", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "code", "execution_count": 1, "id": "f8ffa0be-d1ae-4b1d-8827-01e11ef5be68", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2024-05-18 06:19:15-- https://pic.onlinewebfonts.com/thumbnails/icons_308642.svg\n", "Resolving pic.onlinewebfonts.com (pic.onlinewebfonts.com)... 107.161.24.252\n", "Connecting to pic.onlinewebfonts.com (pic.onlinewebfonts.com)|107.161.24.252|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: unspecified [image/svg+xml]\n", "Saving to: ‘user.svg’\n", "\n", "user.svg [ <=> ] 1.44K --.-KB/s in 0s \n", "\n", "2024-05-18 06:19:15 (234 MB/s) - ‘user.svg’ saved [1470]\n", "\n" ] } ], "source": [ "!wget -O user.svg https://pic.onlinewebfonts.com/thumbnails/icons_308642.svg" ] }, { "cell_type": "code", "execution_count": 2, "id": "7743cc54-42db-4002-a785-60070ddb628e", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2024-05-18 06:19:16-- https://img.freepik.com/free-vector/yellow-beauty-butterfly-insect-icon_18591-82368.jpg\n", "Resolving img.freepik.com (img.freepik.com)... 23.215.0.170, 23.215.0.167, 2600:1408:5400:13::17cf:caca, ...\n", "Connecting to img.freepik.com (img.freepik.com)|23.215.0.170|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 75172 (73K) [image/jpeg]\n", "Saving to: ‘butterfly.jpg’\n", "\n", "butterfly.jpg 100%[===================>] 73.41K --.-KB/s in 0.005s \n", "\n", "2024-05-18 06:19:16 (14.6 MB/s) - ‘butterfly.jpg’ saved [75172/75172]\n", "\n" ] } ], "source": [ "!wget -O butterfly.jpg https://img.freepik.com/free-vector/yellow-beauty-butterfly-insect-icon_18591-82368.jpg" ] }, { "cell_type": "code", "execution_count": 3, "id": "64771639-9c66-49a9-ad1f-b5e4dc83a768", "metadata": { "tags": [] }, "outputs": [], "source": [ "from manim import *\n", "\n", "config.media_width = \"75%\"\n", "config.verbosity = \"WARNING\"" ] }, { "cell_type": "markdown", "id": "06a8c7a5-ac68-4c18-9d2f-6ad52cda7826", "metadata": {}, "source": [ "# Latency Visualization" ] }, { "cell_type": "code", "id": "257cf337-948e-4ef7-aae3-2fb7b5b58602", "metadata": { "tags": [] }, "source": [ "%%manim -qm --format mp4 LatencyVisualization\n", "\n", "from manim import *\n", "\n", "text = \"The cake is a lie\"\n", "text_list = text.split(\" \")\n", "rounds = 2\n", "class LatencyVisualization(Scene):\n", "\n", " def construct(self):\n", " # Setup LLM and user\n", " llm_box = Rectangle(width=2, height=1).set_fill(BLUE, opacity=0.5).set_stroke(BLUE, width=2).to_edge(LEFT)\n", " llm_text = Text(\"LLM\", color=WHITE).move_to(llm_box.get_center())\n", " user_svg = SVGMobject(\"user.svg\", fill_color=WHITE, stroke_color=WHITE).scale(0.5).to_edge(RIGHT)\n", " user_text = Text(\"User\", font_size=20).move_to(user_svg.get_bottom())\n", " user = VGroup(user_svg, user_text)\n", " \n", " title_text = Text(\"Latency\").scale(0.75).to_edge(UP)\n", " conclusion_text = Text(\"Latency is the time it takes for you to receive a response\").scale(0.5).to_edge(DOWN)\n", " self.play(FadeIn(llm_box), FadeIn(llm_text), FadeIn(user), Write(title_text), Write(conclusion_text))\n", "\n", " # Dynamic Packet Flow\n", " start_point = llm_box.get_right() + RIGHT * 0.5\n", " end_point = user_svg.get_left() + LEFT * 0.5\n", "\n", " packets = VGroup()\n", " \n", " # Timer setup\n", " timer = ValueTracker(0)\n", " timer_text = always_redraw(lambda: Text(f\"Latency: {timer.get_value():.2f}s\", font_size=24).to_corner(UR))\n", "\n", " self.add(timer_text)\n", "\n", " def create_packet(word):\n", " packet = Square(color=BLUE).scale(0.3).move_to(start_point)\n", " packet_text = Text(word, font_size=12).move_to(packet.get_center())\n", " packet_with_text = VGroup(packet, packet_text)\n", " packets.add(packet_with_text)\n", " self.add(packet_with_text)\n", " return packet_with_text.animate.move_to(end_point).set_rate_func(rate_functions.linear)\n", " \n", " for _ in range(rounds):\n", " for word in text_list:\n", " self.wait(0.2) # Adjust the wait time to change flow rate\n", " packet_animation = create_packet(word)\n", " self.play(packet_animation, timer.animate.set_value(timer.get_value() + 2), run_time=2)\n", " self.play(FadeOut(packets[-1]), run_time=0.5) # Fade out the last packet (the one that reached the user)\n", " timer.set_value(0) # Reset timer for next packet\n", "\n", " self.wait(1)\n", " self.play(FadeOut(packets))\n", "\n", " # Conclusion\n", " self.wait(2)\n", " self.play(FadeOut(conclusion_text), FadeOut(llm_box), FadeOut(llm_text), FadeOut(user), FadeOut(timer_text))\n" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "id": "59202547-0e0d-4ea1-a82a-1c12135844ed", "metadata": {}, "source": [ "%manim -qm --format gif LatencyVisualization" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "id": "9a4c8bd1-c03b-4f35-bb67-8ad3dd25df32", "metadata": {}, "source": [ "%manim -qm --format webm LatencyVisualization" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "d8e185f7-8abd-43ed-a3cd-f370553ad58d", "metadata": { "tags": [] }, "source": [ "# Throughput Visualization" ] }, { "cell_type": "code", "id": "37ad0a6c-e068-4ace-b31f-2c8c1eb8f52b", "metadata": { "tags": [] }, "source": [ "%%manim -qm --format mp4 ThroughputVisualization\n", "\n", "from manim import *\n", "\n", "class ThroughputVisualization(Scene):\n", " text = [\"His hands can't hit what he can't see\", \n", " \"Float like a 🦋, sting like a bee\",\n", " \"I'm so mean, I make medic -ine sick.\",\n", " \"Suffer now and retire as a champ -ion\"]\n", " batch_size = 4\n", " rounds = 1\n", "\n", " def construct(self):\n", " # Split text into words\n", " words = [sentence.split() for sentence in self.text]\n", " max_len = max(len(sentence) for sentence in words)\n", " \n", " # Extend shorter sentences with empty strings to match the longest sentence\n", " for sentence in words:\n", " sentence.extend([''] * (max_len - len(sentence)))\n", " \n", " # Transpose to get words from each sentence in each pass\n", " transposed_words = list(map(list, zip(*words)))\n", "\n", " # Setup LLM and user\n", " llm_box = Rectangle(width=2, height=1).set_fill(GREEN, opacity=0.5).set_stroke(GREEN, width=2).to_edge(LEFT).shift(DOWN)\n", " llm_text = Text(\"LLM\", color=WHITE).move_to(llm_box.get_center())\n", " user_svg = SVGMobject(\"user.svg\", fill_color=WHITE, stroke_color=WHITE).scale(0.5).to_edge(RIGHT).shift(DOWN)\n", " user_text = Text(\"User\", font_size=20).move_to(user_svg.get_bottom())\n", " user = VGroup(user_svg, user_text)\n", " \n", " title_text = Text(\"Throughput\").scale(0.75).to_edge(UP)\n", " conclusion_text = Text(\"Throughput is the amount of data processed in a given time\").scale(0.5).to_edge(DOWN)\n", " self.play(FadeIn(llm_box), FadeIn(llm_text), FadeIn(user), Write(title_text), Write(conclusion_text))\n", "\n", " # Dynamic Packet Flow\n", " start_point = llm_box.get_right() + RIGHT * 0.5\n", " end_point = user_svg.get_left() + LEFT * 1\n", " storage_start = start_point + UP * 2.5 + LEFT * 1.5\n", " \n", " # Define initial storage positions in a block formation\n", " storage_positions = [\n", " storage_start,\n", " storage_start + RIGHT * 6,\n", " storage_start + DOWN * 1,\n", " storage_start + RIGHT * 6 + DOWN * 1,\n", " ]\n", " storage_offsets = [0, 0, 0, 0] # Initialize offsets for each storage position\n", "\n", " packets = Group()\n", " \n", " # Timer setup\n", " timer = ValueTracker(0)\n", " timer_text = always_redraw(lambda: Text(f\"Time: {timer.get_value():.2f}s\\nThroughput: 4 tokens/s\", font_size=18).to_corner(UR))\n", "\n", " self.add(timer_text)\n", "\n", " def create_packet_group(words):\n", " packet_group = Group()\n", " for i, word in enumerate(words):\n", " row, col = divmod(i, 2)\n", " packet = Square(color=GREEN).scale(0.3).move_to(start_point + RIGHT * col * 0.6 + DOWN * row * 0.6)\n", " \n", " if \"🦋\" in word:\n", " butterfly_image = ImageMobject(\"butterfly.jpg\").scale_to_fit_height(0.3)\n", " butterfly_image.move_to(packet.get_center())\n", " packet_with_image = Group(packet, butterfly_image)\n", " else:\n", " packet_text = Text(word, font_size=12).move_to(packet.get_center())\n", " packet_with_image = Group(packet, packet_text)\n", " \n", " packet_group.add(packet_with_image)\n", " packet_group.next_to(llm_box)\n", " return packet_group\n", "\n", " for _ in range(self.rounds):\n", " for word_group in transposed_words:\n", " if any(word_group): # Check if there are any non-empty words\n", " packet_group = create_packet_group(word_group)\n", " packets.add(packet_group)\n", " self.play(FadeIn(packet_group))\n", " self.play(packet_group.animate.move_to(end_point), timer.animate.set_value(timer.get_value() + 1), run_time=1)\n", "\n", " # Determine which storage position to use\n", " new_storage_position = []\n", " for i, packet in enumerate(packet_group):\n", " storage_index = i % self.batch_size\n", " storage_position = storage_positions[storage_index] + RIGHT * storage_offsets[storage_index]\n", " new_storage_position.append(packet.animate.move_to(storage_position))\n", " storage_offsets[storage_index] += 0.6 # Adjust based on the width of packet group\n", " self.play(*new_storage_position, run_time=0.5)\n", "\n", " self.wait(0.1) # Adjust the wait time to change flow rate\n", " timer.set_value(0) # Reset timer for next batch\n", "\n", " self.wait(1)\n", "\n", " # Conclusion\n", " self.wait(2)\n", " self.play(FadeOut(packets), FadeOut(conclusion_text), FadeOut(llm_box), FadeOut(llm_text), FadeOut(user), FadeOut(timer_text))\n" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "id": "d36ce190-a3ce-461a-b364-91e73b1300da", "metadata": {}, "source": [ "%manim -qm --format gif ThroughputVisualization" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "id": "7dc2a1dd-489c-40ce-9c61-d097dee8a11d", "metadata": { "tags": [] }, "source": [ "%manim -qm --format webm ThroughputVisualization" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "execution_count": null, "id": "0d487ddf-4725-497e-a37b-2d9b6f69338f", "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 }