File size: 14,128 Bytes
73927d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cd761d
73927d2
 
 
 
9cd761d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73927d2
 
 
 
 
9cd761d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73927d2
9cd761d
73927d2
 
 
 
9cd761d
 
73927d2
 
 
 
 
 
 
 
 
 
 
9cd761d
 
 
 
 
 
 
 
73927d2
 
 
 
 
 
 
9cd761d
73927d2
 
 
 
 
9cd761d
73927d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cd761d
 
 
73927d2
 
9cd761d
 
73927d2
 
9cd761d
 
 
 
73927d2
 
 
9cd761d
 
73927d2
9cd761d
 
 
 
73927d2
 
9cd761d
 
73927d2
 
 
 
9cd761d
73927d2
 
 
 
 
 
 
 
 
9cd761d
73927d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cd761d
73927d2
 
 
9cd761d
73927d2
 
 
 
9cd761d
73927d2
 
 
9cd761d
 
 
 
 
 
 
 
 
 
73927d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cd761d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73927d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cd761d
73927d2
 
 
 
 
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
{
 "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
}