-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch06.html
More file actions
959 lines (854 loc) · 69.4 KB
/
Copy pathch06.html
File metadata and controls
959 lines (854 loc) · 69.4 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>nano-vLLM · Ch06 · Prefill vs Decode</title>
<meta name="description" content="Prefill vs decode phases explained from scratch — why one is compute-bound and the other memory-bound, what TTFT and TPOT mean, and how chunked prefill bridges the gap.">
<meta property="og:title" content="nano-vLLM Ch06 — Prefill vs Decode">
<meta property="og:description" content="Same model, two completely different bottlenecks. Understanding the two phases of LLM inference is the key to understanding every performance optimisation.">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Space+Mono:ital,wght@0,400;0,700;1,400&family=Syne:wght@400;700;800&family=DM+Sans:ital,wght@0,300;0,400;0,500;1,300&display=swap" rel="stylesheet">
<style>
:root{
--bg:#08090d;--surface:#0f1117;--surface2:#161a24;
--border:#1e2535;--accent:#00e5ff;--accent2:#ff4d6d;
--accent3:#b4ff6f;--accent4:#ffb347;
--text:#e2e8f4;--muted:#6b7a99;--code-bg:#0a0e1a;
--ch-accent:var(--accent3);
}
*{margin:0;padding:0;box-sizing:border-box;}
html{scroll-behavior:smooth;}
body{font-family:'DM Sans',sans-serif;background:var(--bg);color:var(--text);line-height:1.7;overflow-x:hidden;}
body::before{content:'';position:fixed;inset:0;background:repeating-linear-gradient(0deg,transparent,transparent 2px,rgba(0,229,255,0.015) 2px,rgba(0,229,255,0.015) 4px);pointer-events:none;z-index:9999;}
/* NAV */
.top-nav{position:sticky;top:0;z-index:100;background:rgba(8,9,13,0.92);backdrop-filter:blur(12px);border-bottom:1px solid var(--border);padding:0 1.5rem;display:flex;overflow-x:auto;scrollbar-width:none;}
.top-nav::-webkit-scrollbar{display:none;}
.top-nav a{font-family:'Space Mono',monospace;font-size:0.6rem;letter-spacing:0.08em;text-transform:uppercase;color:var(--muted);text-decoration:none;padding:0.85rem 0.9rem;border-bottom:2px solid transparent;white-space:nowrap;transition:all 0.2s;}
.top-nav a:hover{color:var(--accent3);}
.top-nav a.active{color:var(--accent3);border-bottom-color:var(--accent3);}
/* HERO */
.hero{min-height:100vh;display:flex;flex-direction:column;justify-content:center;align-items:center;text-align:center;padding:4rem 2rem;position:relative;overflow:hidden;}
.hero-grid{position:absolute;inset:0;background-image:linear-gradient(rgba(180,255,111,0.03) 1px,transparent 1px),linear-gradient(90deg,rgba(180,255,111,0.03) 1px,transparent 1px);background-size:60px 60px;animation:gridDrift 20s linear infinite;}
@keyframes gridDrift{from{transform:translate(0,0);}to{transform:translate(60px,60px);}}
.hero-glow{position:absolute;width:600px;height:600px;border-radius:50%;background:radial-gradient(circle,rgba(180,255,111,0.06) 0%,transparent 70%);top:50%;left:50%;transform:translate(-50%,-50%);animation:glowPulse 6s ease-in-out infinite;}
@keyframes glowPulse{0%,100%{opacity:0.5;transform:translate(-50%,-50%) scale(1);}50%{opacity:1;transform:translate(-50%,-50%) scale(1.12);}}
.series-badge{font-family:'Space Mono',monospace;font-size:0.65rem;letter-spacing:0.15em;color:var(--accent3);border:1px solid rgba(180,255,111,0.3);padding:0.3rem 1rem;border-radius:2px;background:rgba(180,255,111,0.05);text-transform:uppercase;position:relative;z-index:1;animation:fadeUp 0.6s ease both;}
.chapter-num{font-family:'Syne',sans-serif;font-weight:800;font-size:clamp(5rem,15vw,10rem);line-height:1;color:var(--accent3);opacity:0.12;position:relative;z-index:1;animation:fadeUp 0.6s ease 0.05s both;letter-spacing:-0.05em;}
.hero h1{font-family:'Syne',sans-serif;font-weight:800;font-size:clamp(2rem,5vw,3.5rem);line-height:1.1;letter-spacing:-0.02em;margin-top:-0.5rem;position:relative;z-index:1;animation:fadeUp 0.6s ease 0.1s both;}
.hero-sub{font-size:1rem;font-weight:300;color:var(--muted);max-width:560px;margin:1rem auto 2.5rem;position:relative;z-index:1;animation:fadeUp 0.6s ease 0.15s both;}
.hero-nav{display:flex;gap:1rem;position:relative;z-index:1;animation:fadeUp 0.6s ease 0.2s both;flex-wrap:wrap;justify-content:center;}
.hero-nav a{font-family:'Space Mono',monospace;font-size:0.7rem;letter-spacing:0.08em;padding:0.6rem 1.25rem;border-radius:3px;text-decoration:none;transition:all 0.2s;}
.btn-primary{background:var(--accent3);color:#000;font-weight:700;}
.btn-primary:hover{background:#9be855;}
.btn-ghost{border:1px solid var(--border);color:var(--muted);}
.btn-ghost:hover{border-color:var(--accent3);color:var(--accent3);}
@keyframes fadeUp{from{opacity:0;transform:translateY(16px);}to{opacity:1;transform:translateY(0);}}
/* MAIN */
main{max-width:900px;margin:0 auto;padding:4rem 2rem 6rem;}
.section{margin-bottom:5rem;}
.section-label{font-family:'Space Mono',monospace;font-size:0.6rem;letter-spacing:0.2em;color:var(--ch-accent);text-transform:uppercase;margin-bottom:0.6rem;}
.section h2{font-family:'Syne',sans-serif;font-weight:800;font-size:clamp(1.6rem,3.5vw,2.4rem);line-height:1.1;margin-bottom:1.25rem;letter-spacing:-0.02em;}
.section h3{font-family:'Syne',sans-serif;font-weight:700;font-size:1.15rem;margin:2.5rem 0 0.75rem;}
.lead{font-size:0.95rem;color:var(--muted);max-width:720px;margin-bottom:1.75rem;line-height:1.85;}
.divider{height:1px;background:linear-gradient(90deg,transparent,var(--border),transparent);margin:3.5rem 0;}
/* CALLOUTS */
.callout{border-left:3px solid;padding:1rem 1.5rem;margin:1.5rem 0;border-radius:0 4px 4px 0;font-size:0.88rem;line-height:1.8;}
.callout>strong:first-child{font-family:'Space Mono',monospace;font-size:0.65rem;letter-spacing:0.1em;text-transform:uppercase;display:block;margin-bottom:0.4rem;}
.callout.info{border-color:var(--accent);background:rgba(0,229,255,0.05);}
.callout.info>strong:first-child{color:var(--accent);}
.callout.warn{border-color:var(--accent4);background:rgba(255,179,71,0.05);}
.callout.warn>strong:first-child{color:var(--accent4);}
.callout.insight{border-color:var(--accent3);background:rgba(180,255,111,0.05);}
.callout.insight>strong:first-child{color:var(--accent3);}
.callout.danger{border-color:var(--accent2);background:rgba(255,77,109,0.05);}
.callout.danger>strong:first-child{color:var(--accent2);}
/* CODE */
.code-label{font-family:'Space Mono',monospace;font-size:0.6rem;letter-spacing:0.12em;text-transform:uppercase;color:var(--accent3);background:rgba(180,255,111,0.08);border:1px solid rgba(180,255,111,0.2);padding:0.2rem 0.6rem;border-radius:2px;display:inline-block;margin-bottom:0.5rem;}
pre{background:var(--code-bg);border:1px solid var(--border);border-left:3px solid var(--accent3);border-radius:4px;padding:1.5rem;overflow-x:auto;font-family:'Space Mono',monospace;font-size:0.76rem;line-height:1.85;color:#c9d6ef;margin:0.5rem 0 1.5rem;}
pre .comment{color:#3d4f6b;font-style:italic;}
pre .keyword{color:var(--accent2);}
pre .string{color:var(--accent3);}
pre .num{color:var(--accent4);}
pre .fn{color:var(--accent);}
pre .cls{color:#c792ea;}
code{font-family:'Space Mono',monospace;font-size:0.82em;background:rgba(180,255,111,0.07);border:1px solid rgba(180,255,111,0.15);padding:0.1em 0.4em;border-radius:3px;color:var(--accent3);}
/* CARDS */
.card-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:1.25rem;margin:1.5rem 0;}
.card{background:var(--surface);border:1px solid var(--border);border-radius:4px;padding:1.5rem;position:relative;overflow:hidden;transition:border-color 0.25s,transform 0.25s;}
.card:hover{border-color:var(--card-accent,var(--accent3));transform:translateY(-3px);}
.card::before{content:'';position:absolute;top:0;left:0;right:0;height:2px;background:var(--card-accent,var(--accent3));}
.card h4{font-family:'Syne',sans-serif;font-weight:700;font-size:0.95rem;margin-bottom:0.5rem;}
.card p{font-size:0.82rem;color:var(--muted);line-height:1.7;}
/* DIAGRAM */
.diagram{background:var(--surface);border:1px solid var(--border);border-radius:4px;padding:2rem;margin:1.5rem 0;position:relative;}
.diagram-label{font-family:'Space Mono',monospace;font-size:0.6rem;letter-spacing:0.12em;text-transform:uppercase;color:var(--muted);position:absolute;top:1rem;right:1.25rem;}
/* PHASE COMPARE */
.phase-compare{display:grid;grid-template-columns:1fr 1fr;gap:1.25rem;margin:1.5rem 0;}
.phase-box{background:var(--surface);border:1px solid var(--border);border-radius:4px;padding:1.5rem;position:relative;overflow:hidden;}
.phase-box::before{content:'';position:absolute;top:0;left:0;right:0;height:3px;}
.phase-prefill::before{background:var(--accent2);}
.phase-decode::before{background:var(--accent3);}
.phase-tag{font-family:'Space Mono',monospace;font-size:0.6rem;letter-spacing:0.12em;text-transform:uppercase;display:block;margin-bottom:0.6rem;}
.phase-prefill .phase-tag{color:var(--accent2);}
.phase-decode .phase-tag{color:var(--accent3);}
.phase-box h4{font-family:'Syne',sans-serif;font-weight:700;font-size:1rem;margin-bottom:0.9rem;}
.phase-box ul{list-style:none;display:flex;flex-direction:column;gap:0.45rem;}
.phase-box li{font-size:0.82rem;color:var(--muted);padding-left:1.1rem;position:relative;line-height:1.6;}
.phase-prefill li::before{content:'→';position:absolute;left:0;color:var(--accent2);font-size:0.72rem;}
.phase-decode li::before{content:'→';position:absolute;left:0;color:var(--accent3);font-size:0.72rem;}
/* TOKEN STRIP */
.token-bar{display:flex;gap:4px;flex-wrap:wrap;margin:0.75rem 0;}
.tok{font-family:'Space Mono',monospace;font-size:0.68rem;padding:0.28rem 0.6rem;border-radius:3px;border:1px solid;transition:all 0.2s;}
.tok.prompt{background:rgba(255,77,109,0.08);border-color:rgba(255,77,109,0.35);color:var(--accent2);}
.tok.gen{background:rgba(180,255,111,0.08);border-color:rgba(180,255,111,0.35);color:var(--accent3);}
.tok.active{background:rgba(255,179,71,0.2);border-color:rgba(255,179,71,0.6);color:var(--accent4);}
/* BOTTLENECK VISUAL */
.bottleneck-grid{display:grid;grid-template-columns:1fr 1fr;gap:1.25rem;margin:1.5rem 0;}
.bn-box{background:var(--surface);border:1px solid var(--border);border-radius:4px;padding:1.25rem;position:relative;}
.bn-label{font-family:'Space Mono',monospace;font-size:0.62rem;letter-spacing:0.1em;text-transform:uppercase;margin-bottom:0.85rem;}
.bn-row{display:flex;align-items:center;gap:0.75rem;margin-bottom:0.6rem;}
.bn-name{font-size:0.75rem;color:var(--muted);min-width:80px;}
.bn-track{flex:1;background:var(--surface2);border-radius:2px;height:14px;overflow:hidden;border:1px solid var(--border);}
.bn-fill{height:100%;border-radius:2px;transition:width 0.4s;}
.bn-val{font-family:'Space Mono',monospace;font-size:0.62rem;min-width:38px;text-align:right;}
/* STEP ROW */
.step-row{display:flex;gap:1rem;align-items:flex-start;background:var(--surface);border:1px solid var(--border);border-radius:4px;padding:1rem 1.25rem;margin-bottom:0.75rem;}
.step-badge{width:30px;height:30px;border-radius:50%;display:flex;align-items:center;justify-content:center;font-family:'Space Mono',monospace;font-size:0.68rem;font-weight:700;flex-shrink:0;}
.step-row h4{font-family:'Syne',sans-serif;font-weight:700;font-size:0.9rem;margin-bottom:0.3rem;}
.step-row p{font-size:0.82rem;color:var(--muted);line-height:1.7;}
/* LATENCY METER */
.lat-row{display:flex;align-items:center;gap:0.75rem;margin-bottom:0.85rem;}
.lat-label{font-family:'Space Mono',monospace;font-size:0.65rem;min-width:130px;color:var(--muted);}
.lat-track{flex:1;background:var(--surface2);border-radius:3px;height:22px;overflow:hidden;border:1px solid var(--border);}
.lat-fill{height:100%;border-radius:3px;display:flex;align-items:center;padding-left:8px;font-family:'Space Mono',monospace;font-size:0.6rem;font-weight:700;transition:width 0.6s ease;}
/* QUIZ */
.quiz-block{background:var(--surface);border:1px solid var(--border);border-radius:4px;padding:1.5rem;margin-bottom:1.25rem;}
.quiz-block .q{font-family:'Syne',sans-serif;font-weight:700;font-size:0.95rem;margin-bottom:0.9rem;}
.quiz-opts{display:flex;flex-direction:column;gap:0.5rem;}
.quiz-opts button{text-align:left;padding:0.6rem 1rem;background:var(--surface2);border:1px solid var(--border);color:var(--muted);border-radius:3px;cursor:pointer;font-family:'DM Sans',sans-serif;font-size:0.85rem;line-height:1.5;transition:all 0.15s;}
.quiz-opts button:hover:not(:disabled){border-color:var(--muted);color:var(--text);}
.quiz-opts button.correct{background:rgba(180,255,111,0.1);border-color:rgba(180,255,111,0.4);color:var(--accent3);}
.quiz-opts button.wrong{background:rgba(255,77,109,0.08);border-color:rgba(255,77,109,0.3);color:var(--accent2);}
.quiz-fb{display:none;margin-top:0.75rem;padding:0.75rem 1rem;border-radius:3px;font-size:0.82rem;line-height:1.7;}
.quiz-fb.show{display:block;}
.quiz-fb.ok{background:rgba(180,255,111,0.07);border:1px solid rgba(180,255,111,0.2);color:var(--accent3);}
.quiz-fb.no{background:rgba(255,77,109,0.07);border:1px solid rgba(255,77,109,0.2);color:var(--accent2);}
/* TAKEAWAYS */
.takeaways{background:var(--surface2);border:1px solid var(--border);border-radius:4px;padding:1.75rem;}
.takeaways-label{font-family:'Space Mono',monospace;font-size:0.6rem;letter-spacing:0.15em;text-transform:uppercase;color:var(--accent3);margin-bottom:1.25rem;}
.takeaway-grid{display:grid;grid-template-columns:1fr 1fr;gap:0.85rem;}
.takeaway-item{display:flex;gap:0.75rem;align-items:flex-start;}
.takeaway-check{width:18px;height:18px;border-radius:2px;background:rgba(180,255,111,0.15);border:1px solid rgba(180,255,111,0.4);display:grid;place-items:center;flex-shrink:0;margin-top:3px;font-size:0.6rem;color:var(--accent3);font-weight:700;}
.takeaway-item p{font-size:0.82rem;color:var(--muted);line-height:1.65;}
.takeaway-item strong{color:var(--text);}
/* XREF + MISC */
.xref{font-family:'Space Mono',monospace;font-size:0.65rem;color:rgba(0,229,255,0.6);border:1px solid rgba(0,229,255,0.2);padding:0 0.3rem;border-radius:2px;margin-left:0.25rem;}
.misconception{background:var(--surface);border:1px solid var(--border);border-left:3px solid var(--accent2);border-radius:0 4px 4px 0;padding:1rem 1.25rem;margin-bottom:0.75rem;}
.misconception .myth{font-family:'Space Mono',monospace;font-size:0.65rem;letter-spacing:0.08em;color:var(--accent2);text-transform:uppercase;margin-bottom:0.3rem;}
.misconception .reality{font-size:0.82rem;color:var(--muted);line-height:1.7;}
.misconception .reality strong{color:var(--text);}
/* FOOTER */
.chapter-footer{border-top:1px solid var(--border);margin-top:5rem;padding:2.5rem 2rem;text-align:center;}
.footer-nav{display:flex;gap:1rem;justify-content:center;flex-wrap:wrap;margin-bottom:1.25rem;}
.footer-credit{font-family:'Space Mono',monospace;font-size:0.6rem;color:var(--muted);}
.footer-credit a{color:var(--accent3);text-decoration:none;}
@media(max-width:640px){
.takeaway-grid,.phase-compare,.bottleneck-grid{grid-template-columns:1fr;}
.hero-nav{flex-direction:column;align-items:center;}
}
</style>
</head>
<body>
<!-- NAV -->
<nav class="top-nav">
<a href="index.html">Index</a>
<a href="ch01.html">01 · Inference</a>
<a href="ch02.html">02 · Architecture</a>
<a href="ch03.html">03 · KV Cache</a>
<a href="ch04.html">04 · PagedAttention</a>
<a href="ch05.html">05 · Scheduler</a>
<a href="ch06.html" class="active">06 · Prefill vs Decode</a>
<a href="ch07.html">07 · Prefix Caching</a>
<a href="ch08.html">08 · Sampling</a>
<a href="ch09.html">09 · Parallelism</a>
<a href="ch10.html">10 · Optimizations</a>
<a href="ch11.html">11 · Benchmarks</a>
</nav>
<!-- HERO -->
<header class="hero">
<div class="hero-grid"></div>
<div class="hero-glow"></div>
<div class="series-badge">Chapter 06 of 11 · nano-vLLM Deep Dive</div>
<div class="chapter-num">06</div>
<h1>Prefill vs Decode</h1>
<p class="hero-sub">Same model. Two completely different bottlenecks. Understanding why prefill and decode behave so differently is the master key to every LLM performance optimisation.</p>
<div class="hero-nav">
<a href="ch05.html" class="btn-ghost">← Ch05: Scheduler</a>
<a href="ch07.html" class="btn-primary">Next: Prefix Caching →</a>
</div>
</header>
<main>
<!-- ══════════════════════════════
SECTION 1 — OPENING ANALOGY
══════════════════════════════ -->
<section class="section">
<div class="section-label">Section 1 — The Big Picture</div>
<h2>Two phases, two completely different problems</h2>
<p class="lead">You've seen prefill and decode mentioned throughout this series. Now it's time to understand them deeply — not just what they do, but <em>why they behave so differently</em> on the same GPU hardware. This distinction shapes every design decision in nano-vLLM and every optimisation in production inference engines.</p>
<div class="callout insight">
<strong>The Factory vs Conveyor Belt Analogy</strong>
Imagine a factory that builds cars. <strong>Prefill</strong> is like the initial assembly stage — dozens of workers all attack the car simultaneously, welding, wiring, and bolting in parallel. It's chaotic, power-intensive, and fast. Every worker is busy at once. The bottleneck is how many workers (compute units) you have. <strong>Decode</strong> is like the paint shop at the end — one robot arm applies paint to one car panel at a time, carefully, with a huge tank of paint (GPU memory) that it dips into on every stroke. The bottleneck isn't the robot arm's speed — it's how fast paint can be pumped from the tank. More robot arms don't help if the pump is already at capacity.
</div>
<p class="lead">The factory is <strong style="color:var(--text)">compute-bound</strong>. The paint shop is <strong style="color:var(--text)">memory-bandwidth bound</strong>. Same building, same power supply, completely different bottlenecks. This is exactly what happens with prefill and decode on a GPU.</p>
</section>
<div class="divider"></div>
<!-- ══════════════════════════════
SECTION 2 — PREFILL PHASE
══════════════════════════════ -->
<section class="section">
<div class="section-label">Section 2 — Prefill Phase</div>
<h2>Prefill — processing the input prompt</h2>
<p class="lead"><strong style="color:var(--text)">Prefill</strong> is the first phase of handling any request. When your prompt arrives — say, 500 tokens of text — the model processes all 500 tokens simultaneously in a single forward pass. All tokens go in together, all layers process them in parallel, and at the end the model has computed the Key and Value vectors for every input token and written them to the KV cache <span class="xref">→ Ch.03</span>.</p>
<h3>What makes prefill compute-bound?</h3>
<p class="lead">To understand "compute-bound", you first need to understand the concept of <strong style="color:var(--text)">arithmetic intensity</strong> — the ratio of math operations performed to memory bytes read. High arithmetic intensity = more computation per byte of data = the compute units (CUDA cores) are the bottleneck, not the memory bus.</p>
<div class="callout insight">
<strong>The Long Division Analogy</strong>
Imagine two tasks: (A) solving 1,000 long-division problems on paper — you write a number, do a lot of arithmetic, write the answer; (B) copying 1,000 numbers from one notebook to another — you read a number, write it down, move on. Task A is compute-intensive — you're doing lots of work per number you read. Task B is memory-bandwidth intensive — you're barely doing any work, you're just moving data. Prefill is task A. Decode is task B.
</div>
<p class="lead">During prefill, for each of the 500 input tokens, the model performs large matrix multiplications — multiplying the token's embedding vector against the model's weight matrices in every layer. Each weight matrix is read once but produces many multiply-accumulate operations. This is high arithmetic intensity. The GPU's thousands of floating-point units are all saturated. Adding more CUDA cores would make prefill faster. The memory bus is not the limit — the math units are.</p>
<div class="phase-compare" style="margin-top:1.5rem;">
<div class="phase-box phase-prefill">
<span class="phase-tag">Prefill — key properties</span>
<h4>Processes all input tokens at once</h4>
<ul>
<li>All N input tokens processed in one parallel forward pass</li>
<li>Produces K and V for every input token — writes full KV cache</li>
<li>Outputs logits only for the <em>last</em> token (to sample first output)</li>
<li>High arithmetic intensity — GPU compute units are saturated</li>
<li>Scales well with batch size — more sequences = better utilisation</li>
<li>A 1,000-token prompt may take 100–500ms on a single GPU</li>
<li>Uses <code>flash_attn_varlen_func</code> for variable-length batches</li>
</ul>
</div>
<div class="phase-box phase-decode">
<span class="phase-tag">Decode — key properties</span>
<h4>Generates tokens one at a time</h4>
<ul>
<li>Exactly one new token processed per forward pass, per sequence</li>
<li>Reads full KV cache for all prior tokens — writes only 1 new entry</li>
<li>Outputs logits for the next token to sample</li>
<li>Low arithmetic intensity — memory bandwidth is the bottleneck</li>
<li>Doesn't scale much with batch size — bottleneck is the weight read</li>
<li>A single decode step takes 20–50ms on a 7B model</li>
<li>Uses <code>flash_attn_with_kvcache</code> for paged KV reads</li>
</ul>
</div>
</div>
</section>
<div class="divider"></div>
<!-- ══════════════════════════════
SECTION 3 — DECODE PHASE
══════════════════════════════ -->
<section class="section">
<div class="section-label">Section 3 — Decode Phase</div>
<h2>Decode — generating tokens one at a time</h2>
<p class="lead">After prefill, the model enters the <strong style="color:var(--text)">decode phase</strong> — the autoregressive loop <span class="xref">→ Ch.01</span> where it generates one token per step. Each step processes only the single most-recently-generated token, reads the full KV cache for context, and samples the next token. This repeats until the sequence finishes.</p>
<h3>Why decode is memory-bandwidth bound</h3>
<p class="lead">Here's the key insight: to generate a single token, the GPU must read the entire model's weights from HBM <span class="xref">→ Ch.01</span> — all 7 billion parameters of a 7B model, or 14 GB at fp16. But it only uses those weights to perform computation for <em>one token's worth of input</em>. That's a tiny amount of math relative to the data movement. The arithmetic intensity is very low.</p>
<p class="lead">Think of it this way: you're reading 14 GB of data from HBM every decode step, but doing only a fraction of the math you'd do if you were processing a large batch of tokens. The memory bus is running flat out pumping weights from HBM to the compute cores, but the compute cores are only lightly loaded. Adding more CUDA cores would not help at all — they'd just sit idle waiting for data. The only thing that would help decode is faster HBM bandwidth.</p>
<div class="diagram">
<div class="diagram-label">Resource utilisation — prefill vs decode</div>
<div class="bottleneck-grid">
<div class="bn-box" style="border-color:rgba(255,77,109,0.25);">
<div class="bn-label" style="color:var(--accent2);">PREFILL — compute-bound</div>
<div class="bn-row">
<div class="bn-name">CUDA Cores</div>
<div class="bn-track"><div class="bn-fill" style="width:96%;background:var(--accent2);"></div></div>
<div class="bn-val" style="color:var(--accent2);">96%</div>
</div>
<div class="bn-row">
<div class="bn-name">HBM Bandwidth</div>
<div class="bn-track"><div class="bn-fill" style="width:45%;background:var(--muted);"></div></div>
<div class="bn-val" style="color:var(--muted);">45%</div>
</div>
<div class="bn-row">
<div class="bn-name">SRAM Usage</div>
<div class="bn-track"><div class="bn-fill" style="width:82%;background:var(--accent4);"></div></div>
<div class="bn-val" style="color:var(--accent4);">82%</div>
</div>
<p style="font-size:0.72rem;color:var(--muted);margin-top:0.5rem;line-height:1.6;">Compute units saturated — adding more CUDA cores would directly improve speed.</p>
</div>
<div class="bn-box" style="border-color:rgba(180,255,111,0.25);">
<div class="bn-label" style="color:var(--accent3);">DECODE — memory-bound</div>
<div class="bn-row">
<div class="bn-name">CUDA Cores</div>
<div class="bn-track"><div class="bn-fill" style="width:18%;background:var(--muted);"></div></div>
<div class="bn-val" style="color:var(--muted);">18%</div>
</div>
<div class="bn-row">
<div class="bn-name">HBM Bandwidth</div>
<div class="bn-track"><div class="bn-fill" style="width:95%;background:var(--accent3);"></div></div>
<div class="bn-val" style="color:var(--accent3);">95%</div>
</div>
<div class="bn-row">
<div class="bn-name">SRAM Usage</div>
<div class="bn-track"><div class="bn-fill" style="width:30%;background:var(--muted);"></div></div>
<div class="bn-val" style="color:var(--muted);">30%</div>
</div>
<p style="font-size:0.72rem;color:var(--muted);margin-top:0.5rem;line-height:1.6;">Memory bus saturated — more CUDA cores are wasted. Only faster HBM bandwidth helps.</p>
</div>
</div>
</div>
<div class="callout warn">
<strong>Batching helps decode more than you'd think</strong>
While decode is memory-bound per-token, running many sequences in the same decode batch actually improves efficiency. Why? Because all sequences in the batch share the same weight matrix reads — you pay the HBM read cost once, then use those weights for all N sequences. A decode batch of 32 sequences reads model weights once and computes 32 tokens simultaneously — same memory cost, 32× the output. This is why the scheduler <span class="xref">→ Ch.05</span> tries to keep the batch as full as possible even during decode.
</div>
</section>
<div class="divider"></div>
<!-- ══════════════════════════════
SECTION 4 — LATENCY METRICS
══════════════════════════════ -->
<section class="section">
<div class="section-label">Section 4 — Measuring Performance</div>
<h2>TTFT and TPOT — the two latency metrics</h2>
<p class="lead">Because prefill and decode behave so differently, inference engineers use two separate metrics to measure latency. Using just one metric hides the full picture.</p>
<h3>TTFT — Time To First Token</h3>
<p class="lead"><strong style="color:var(--text)">TTFT</strong> (Time To First Token) measures how long after you send a request until you see the <em>very first token</em> of the response appear. This is entirely determined by prefill duration — the model must finish processing your entire prompt before it can generate token 1.</p>
<p class="lead">TTFT is what users feel as "responsiveness". A system with slow prefill feels sluggish even if it generates subsequent tokens quickly. A 5-second TTFT feels like the system is hanging, even if tokens stream out rapidly after that. For interactive applications, TTFT is often the most important metric.</p>
<h3>TPOT — Time Per Output Token</h3>
<p class="lead"><strong style="color:var(--text)">TPOT</strong> (Time Per Output Token) measures the average time between each successive generated token after the first one. This is entirely determined by decode speed — specifically by HBM bandwidth, batch size, and model size.</p>
<p class="lead">TPOT is what users feel as "streaming speed" — how fast the text appears to flow after it starts. For long responses (essays, code, analyses), TPOT dominates the total wait time. A TPOT of 25ms means 40 tokens per second, which is roughly human reading speed — fast enough to feel natural.</p>
<div class="diagram">
<div class="diagram-label">Latency breakdown — example: 200-token prompt, 100-token response</div>
<p style="font-size:0.82rem;color:var(--muted);margin-bottom:1.25rem;">Drag the sliders to see how prompt length, response length, and model size affect TTFT and total time. <span style="font-size:0.72rem;color:var(--muted);font-style:italic;">(Estimates assume a single H100 GPU, fp16 precision, no batching — illustrative only.)</span></p>
<div style="display:grid;grid-template-columns:1fr 1fr;gap:1.5rem;margin-bottom:1.5rem;">
<div>
<label style="font-family:'Space Mono',monospace;font-size:0.65rem;color:var(--muted);display:block;margin-bottom:0.4rem;">PROMPT LENGTH (tokens)</label>
<div style="display:flex;align-items:center;gap:0.75rem;">
<input type="range" id="prompt-len" min="10" max="4000" step="10" value="200" style="flex:1;accent-color:var(--accent2);" oninput="updateLatency()">
<span style="font-family:'Space Mono',monospace;font-size:0.8rem;color:var(--accent2);min-width:50px;text-align:right;" id="prompt-val">200</span>
</div>
</div>
<div>
<label style="font-family:'Space Mono',monospace;font-size:0.65rem;color:var(--muted);display:block;margin-bottom:0.4rem;">RESPONSE LENGTH (tokens)</label>
<div style="display:flex;align-items:center;gap:0.75rem;">
<input type="range" id="resp-len" min="10" max="1000" step="10" value="100" style="flex:1;accent-color:var(--accent3);" oninput="updateLatency()">
<span style="font-family:'Space Mono',monospace;font-size:0.8rem;color:var(--accent3);min-width:50px;text-align:right;" id="resp-val">100</span>
</div>
</div>
</div>
<div style="display:grid;grid-template-columns:repeat(3,1fr);gap:1rem;margin-bottom:1.5rem;">
<div style="background:var(--surface2);border:1px solid var(--border);border-radius:4px;padding:1rem;text-align:center;">
<div style="font-family:'Space Mono',monospace;font-size:1.4rem;font-weight:700;color:var(--accent2);" id="ttft-val">–</div>
<div style="font-size:0.65rem;color:var(--muted);margin-top:0.2rem;">TTFT (prefill time)</div>
</div>
<div style="background:var(--surface2);border:1px solid var(--border);border-radius:4px;padding:1rem;text-align:center;">
<div style="font-family:'Space Mono',monospace;font-size:1.4rem;font-weight:700;color:var(--accent3);" id="tpot-val">–</div>
<div style="font-size:0.65rem;color:var(--muted);margin-top:0.2rem;">TPOT (per decode step)</div>
</div>
<div style="background:var(--surface2);border:1px solid rgba(180,255,111,0.3);border-radius:4px;padding:1rem;text-align:center;">
<div style="font-family:'Space Mono',monospace;font-size:1.4rem;font-weight:700;color:var(--text);" id="total-val">–</div>
<div style="font-size:0.65rem;color:var(--muted);margin-top:0.2rem;">Total response time</div>
</div>
</div>
<div class="lat-row">
<div class="lat-label">TTFT (prefill)</div>
<div class="lat-track">
<div id="ttft-bar" class="lat-fill" style="background:var(--accent2);color:#fff;"></div>
</div>
</div>
<div class="lat-row">
<div class="lat-label">Decode time</div>
<div class="lat-track">
<div id="decode-bar" class="lat-fill" style="background:var(--accent3);color:#000;"></div>
</div>
</div>
<p style="font-size:0.75rem;color:var(--muted);margin-top:0.5rem;line-height:1.6;" id="latency-note"></p>
</div>
</section>
<div class="divider"></div>
<!-- ══════════════════════════════
SECTION 5 — INTERACTIVE DEMO
══════════════════════════════ -->
<section class="section">
<div class="section-label">Section 5 — Live Simulation</div>
<h2>Watch prefill and decode happen — step by step</h2>
<p class="lead">Click the buttons below to see the two phases in action. Notice how prefill processes all input tokens in one shot, while decode advances one token at a time. Watch the KV cache fill up as each phase runs.</p>
<div class="diagram" style="padding:1.5rem;">
<div class="diagram-label">Phase simulator</div>
<!-- Stats bar -->
<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:0.75rem;margin-bottom:1.25rem;">
<div style="background:var(--surface2);border:1px solid var(--border);border-radius:4px;padding:0.7rem;text-align:center;">
<div style="font-family:'Space Mono',monospace;font-size:1.1rem;font-weight:700;" id="ph-phase-label">–</div>
<div style="font-size:0.6rem;color:var(--muted);margin-top:0.15rem;">Current phase</div>
</div>
<div style="background:var(--surface2);border:1px solid var(--border);border-radius:4px;padding:0.7rem;text-align:center;">
<div style="font-family:'Space Mono',monospace;font-size:1.1rem;font-weight:700;color:var(--accent2);" id="ph-tokens-proc">0</div>
<div style="font-size:0.6rem;color:var(--muted);margin-top:0.15rem;">Tokens processed</div>
</div>
<div style="background:var(--surface2);border:1px solid var(--border);border-radius:4px;padding:0.7rem;text-align:center;">
<div style="font-family:'Space Mono',monospace;font-size:1.1rem;font-weight:700;color:var(--accent3);" id="ph-tokens-gen">0</div>
<div style="font-size:0.6rem;color:var(--muted);margin-top:0.15rem;">Tokens generated</div>
</div>
<div style="background:var(--surface2);border:1px solid var(--border);border-radius:4px;padding:0.7rem;text-align:center;">
<div style="font-family:'Space Mono',monospace;font-size:1.1rem;font-weight:700;color:var(--accent);" id="ph-kv-used">0</div>
<div style="font-size:0.6rem;color:var(--muted);margin-top:0.15rem;">KV cache slots</div>
</div>
</div>
<!-- Token display -->
<div style="margin-bottom:1rem;">
<div style="font-family:'Space Mono',monospace;font-size:0.6rem;color:var(--muted);margin-bottom:0.4rem;letter-spacing:0.08em;text-transform:uppercase;">Prompt tokens (red = being processed, dim = waiting)</div>
<div class="token-bar" id="ph-prompt-bar"></div>
</div>
<div style="margin-bottom:1.25rem;">
<div style="font-family:'Space Mono',monospace;font-size:0.6rem;color:var(--muted);margin-bottom:0.4rem;letter-spacing:0.08em;text-transform:uppercase;">Generated tokens (appear one by one during decode)</div>
<div class="token-bar" id="ph-gen-bar" style="min-height:30px;"></div>
</div>
<!-- KV cache bar -->
<div style="margin-bottom:1.25rem;">
<div style="font-family:'Space Mono',monospace;font-size:0.6rem;color:var(--muted);margin-bottom:0.3rem;letter-spacing:0.08em;text-transform:uppercase;">KV Cache fill level</div>
<div style="background:var(--surface2);border:1px solid var(--border);border-radius:3px;height:14px;overflow:hidden;">
<div id="ph-kv-bar" style="height:100%;width:0%;transition:width 0.4s ease;display:flex;align-items:center;padding-left:6px;font-family:'Space Mono',monospace;font-size:0.58rem;font-weight:700;color:#000;"></div>
</div>
</div>
<!-- GPU activity -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:0.75rem;margin-bottom:1.25rem;">
<div style="background:var(--surface2);border:1px solid var(--border);border-radius:4px;padding:0.75rem;">
<div style="font-family:'Space Mono',monospace;font-size:0.58rem;color:var(--muted);margin-bottom:0.4rem;letter-spacing:0.08em;text-transform:uppercase;">CUDA Core utilisation</div>
<div style="background:var(--surface);border:1px solid var(--border);border-radius:2px;height:12px;overflow:hidden;">
<div id="ph-compute-bar" style="height:100%;width:0%;background:var(--accent2);border-radius:2px;transition:width 0.5s ease;"></div>
</div>
<div style="font-family:'Space Mono',monospace;font-size:0.62rem;color:var(--muted);margin-top:0.25rem;" id="ph-compute-pct">0%</div>
</div>
<div style="background:var(--surface2);border:1px solid var(--border);border-radius:4px;padding:0.75rem;">
<div style="font-family:'Space Mono',monospace;font-size:0.58rem;color:var(--muted);margin-bottom:0.4rem;letter-spacing:0.08em;text-transform:uppercase;">HBM Bandwidth utilisation</div>
<div style="background:var(--surface);border:1px solid var(--border);border-radius:2px;height:12px;overflow:hidden;">
<div id="ph-hbm-bar" style="height:100%;width:0%;background:var(--accent3);border-radius:2px;transition:width 0.5s ease;"></div>
</div>
<div style="font-family:'Space Mono',monospace;font-size:0.62rem;color:var(--muted);margin-top:0.25rem;" id="ph-hbm-pct">0%</div>
</div>
</div>
<!-- Note -->
<div style="font-family:'Space Mono',monospace;font-size:0.65rem;color:var(--muted);min-height:20px;margin-bottom:1rem;" id="ph-note"></div>
<!-- Controls -->
<div style="display:flex;gap:0.6rem;flex-wrap:wrap;">
<button onclick="phStep()" id="ph-btn" style="font-family:'Space Mono',monospace;font-size:0.65rem;padding:0.5rem 1.1rem;background:rgba(180,255,111,0.12);border:1px solid rgba(180,255,111,0.35);color:var(--accent3);border-radius:3px;cursor:pointer;font-weight:700;">▶ Run Prefill</button>
<button onclick="phReset()" style="font-family:'Space Mono',monospace;font-size:0.65rem;padding:0.5rem 1rem;background:transparent;border:1px solid var(--border);color:var(--muted);border-radius:3px;cursor:pointer;">↺ Reset</button>
</div>
</div>
</section>
<div class="divider"></div>
<!-- ══════════════════════════════
SECTION 6 — CHUNKED PREFILL
══════════════════════════════ -->
<section class="section">
<div class="section-label">Section 6 — Bridging the Gap</div>
<h2>Chunked prefill — the best of both worlds</h2>
<p class="lead">Prefill and decode have a tension: prefill is fast but blocks decode (no new tokens until prefill finishes), while decode is slow but generates tokens continuously. For a very long prompt, users stare at a blank screen during the entire prefill — terrible TTFT. <strong style="color:var(--text)">Chunked prefill</strong> resolves this tension.</p>
<h3>What is chunked prefill?</h3>
<p class="lead">Instead of processing the entire prompt in one massive forward pass, chunked prefill splits the prompt into smaller chunks — say, 512 tokens at a time. After each chunk is processed, decode steps for existing sequences can be interleaved. The prompt is still fully processed before the new request generates any tokens, but other requests already in DECODING state keep generating throughout.</p>
<div class="callout insight">
<strong>Why prefill can be chunked but decode cannot</strong>
Prefill is chunkable because each chunk produces independent KV data — tokens 0–511 generate the same K and V regardless of whether tokens 512–1023 are processed in the same batch or a later one. Decode cannot be chunked the same way because each new token causally depends on the previous one: you can't generate token N+1 until token N exists in the KV cache.
</div>
<div class="step-row">
<div class="step-badge" style="background:rgba(255,77,109,0.15);border:1px solid rgba(255,77,109,0.4);color:var(--accent2);">❌</div>
<div>
<h4>Without chunked prefill</h4>
<p>A 4,000-token prompt takes ~800ms to prefill on a 7B model. During those 800ms, every other sequence in the batch is blocked — no decode steps run. Users already waiting for their responses experience a 800ms pause. TTFT for the new request is 800ms. This is called <strong style="color:var(--text)">prefill starvation</strong>.</p>
</div>
</div>
<div class="step-row">
<div class="step-badge" style="background:rgba(180,255,111,0.15);border:1px solid rgba(180,255,111,0.4);color:var(--accent3);">✓</div>
<div>
<h4>With chunked prefill (chunk size = 512 tokens)</h4>
<p>The 4,000-token prompt is split into 8 chunks of 512. After each chunk (≈100ms), the scheduler runs a decode step for all waiting sequences. Existing responses continue streaming. The new request's TTFT is still ~800ms total, but other users see no pause. GPU utilisation stays high throughout. This is what nano-vLLM implements when <code>enforce_eager=False</code>.</p>
</div>
</div>
<div class="callout info">
<strong>Why not just make chunks tiny?</strong>
Smaller chunks mean more frequent interleaving — better for existing sequences, but worse for the new request's TTFT (more overhead per chunk). Larger chunks process more tokens per GPU step but block decode longer. The optimal chunk size is a tuning parameter: typically 512–2048 tokens, balancing TTFT against decode-step interruption frequency. nano-vLLM uses the scheduler's <span class="xref">→ Ch.05</span> continuous batching loop to mix prefill chunks and decode steps naturally in the same batch.
</div>
</section>
<div class="divider"></div>
<!-- ══════════════════════════════
SECTION 7 — NANO-VLLM CODE
══════════════════════════════ -->
<section class="section">
<div class="section-label">Section 7 — In nano-vLLM</div>
<h2>How the two phases appear in code</h2>
<p class="lead">The clearest place to see the prefill/decode split is in the attention layer — the same function handles both phases, but dispatches to a completely different underlying kernel based on <code>seq.is_prefill</code>.</p>
<div class="code-label">layers/attention.py — phase-aware forward pass</div>
<pre><span class="keyword">def</span> <span class="fn">forward</span>(self, q, k, v, kv_cache, slot_mapping, block_table, is_prefill):
<span class="comment"># Step 1: Write this step's new K and V to the cache (both phases do this)</span>
<span class="fn">store_kvcache_kernel</span>[num_tokens,](k, v, kv_cache, slot_mapping, ...)
<span class="keyword">if</span> is_prefill:
<span class="comment"># ── PREFILL PATH ──────────────────────────────────────────────
# All input tokens are in q, k, v as contiguous tensors.
# flash_attn_varlen_func handles variable-length sequences
# in a single batch, masking future tokens (causal mask).
# This is compute-intensive: full attention over N tokens.</span>
out = <span class="fn">flash_attn_varlen_func</span>(
q, k, v,
cu_seqlens_q=cu_seqlens, <span class="comment"># cumulative sequence lengths for batching</span>
cu_seqlens_k=cu_seqlens,
max_seqlen_q=max_seqlen,
causal=<span class="keyword">True</span>, <span class="comment"># don't attend to future tokens</span>
)
<span class="keyword">else</span>:
<span class="comment"># ── DECODE PATH ───────────────────────────────────────────────
# Only the new token is in q. K and V for all prior tokens
# are read from the paged KV cache via block_table.
# flash_attn_with_kvcache handles non-contiguous block reads.
# This is memory-bandwidth intensive: reads entire KV cache.</span>
out = <span class="fn">flash_attn_with_kvcache</span>(
q,
kv_cache[<span class="num">0</span>], <span class="comment"># the full K cache tensor (all blocks)</span>
kv_cache[<span class="num">1</span>], <span class="comment"># the full V cache tensor (all blocks)</span>
block_table=block_table, <span class="comment"># logical → physical block mapping</span>
cache_seqlens=seqlens, <span class="comment"># how many tokens are in cache per sequence</span>
)
<span class="keyword">return</span> out</pre>
<div class="callout info">
<strong>Same function, two completely different memory access patterns</strong>
In prefill, <code>q</code>, <code>k</code>, <code>v</code> are all dense tensors in contiguous memory — fast to access. In decode, <code>q</code> is tiny (one token per sequence), but <code>kv_cache[0]</code> and <code>kv_cache[1]</code> are the entire paged cache tensors scattered across non-contiguous blocks <span class="xref">→ Ch.04</span>. Reading all those scattered blocks is exactly the memory bandwidth bottleneck. <code>flash_attn_with_kvcache</code> is highly optimised to do this paged read as efficiently as possible — it's one of the most performance-critical kernels in the entire system.
</div>
<div class="code-label">engine.py — model_runner separates prefill and decode sequences</div>
<pre><span class="keyword">def</span> <span class="fn">run</span>(self, scheduler_output: <span class="cls">SchedulerOutput</span>):
<span class="comment"># Build a mixed batch: prefill sequences + decode sequences
# Each sequence knows its phase via seq.is_prefill</span>
all_seqs = scheduler_output.prefill + scheduler_output.decode
<span class="comment"># Collect token IDs for the forward pass
# Prefill sequences contribute ALL their tokens
# Decode sequences contribute only their LATEST token</span>
input_ids = []
<span class="keyword">for</span> seq <span class="keyword">in</span> all_seqs:
<span class="keyword">if</span> seq.is_prefill:
input_ids.<span class="fn">extend</span>(seq.tokens) <span class="comment"># all N prompt tokens</span>
<span class="keyword">else</span>:
input_ids.<span class="fn">append</span>(seq.tokens[-<span class="num">1</span>]) <span class="comment"># just the last generated token</span>
<span class="comment"># Single forward pass handles both prefill and decode sequences
# The attention layer dispatches to the right kernel per sequence</span>
logits = self.model.<span class="fn">forward</span>(input_ids, ...)</pre>
</section>
<div class="divider"></div>
<!-- ══════════════════════════════
SECTION 8 — WHY IT MATTERS
══════════════════════════════ -->
<section class="section">
<div class="section-label">Section 8 — Why It Matters</div>
<h2>Why understanding this changes how you think about LLM performance</h2>
<div class="card-grid">
<div class="card" style="--card-accent:var(--accent2);">
<h4>Prefill determines TTFT</h4>
<p>Long system prompts, RAG contexts, and long chat histories all increase prefill time. Prefix caching <span class="xref">→ Ch.07</span> is the primary way to reduce TTFT — by skipping prefill entirely for tokens already in the cache.</p>
</div>
<div class="card" style="--card-accent:var(--accent3);">
<h4>Decode determines throughput</h4>
<p>Generating 1,000 tokens takes 1,000 decode steps regardless of batch size. The only way to improve decode throughput is faster HBM bandwidth (better hardware) or smaller models (fewer weights to read per step).</p>
</div>
<div class="card" style="--card-accent:var(--accent);">
<h4>Flash attention solves both differently</h4>
<p>FlashAttention-2 uses tiling to reduce HBM traffic during prefill <span class="xref">→ Ch.10</span>. <code>flash_attn_with_kvcache</code> handles the paged, scattered reads during decode. Two different problems, two different kernel strategies.</p>
</div>
<div class="card" style="--card-accent:var(--accent4);">
<h4>Speculative decoding attacks decode cost</h4>
<p>Speculative decoding (not in nano-vLLM, but used in production vLLM) generates multiple draft tokens in one step and verifies them. It exploits the fact that decode is memory-bound and compute-underutilised — using the idle compute to speculatively generate ahead.</p>
</div>
</div>
</section>
<div class="divider"></div>
<!-- ══════════════════════════════
SECTION 9 — MISCONCEPTIONS
══════════════════════════════ -->
<section class="section">
<div class="section-label">Section 9 — Common Mistakes</div>
<h2>Things beginners get wrong about the two phases</h2>
<div class="misconception">
<div class="myth">✗ Myth 1 — "A faster GPU always means faster token generation"</div>
<div class="reality"><strong>Reality:</strong> "Faster GPU" can mean more CUDA cores or faster HBM bandwidth — and these help different phases. More CUDA cores help prefill (compute-bound). Faster HBM bandwidth helps decode (memory-bound). An H100 is faster than an A100 primarily because it has much higher HBM bandwidth (3.35 TB/s vs 2.0 TB/s), which is why it's so much faster at decode. The compute improvement is secondary for most inference workloads.</div>
</div>
<div class="misconception">
<div class="myth">✗ Myth 2 — "Prefill and decode use different model weights"</div>
<div class="reality"><strong>Reality:</strong> Both phases use exactly the same model weights — the same 7 billion parameters, the same weight matrices, the same transformer architecture. The difference is not <em>what</em> is computed, but <em>how much</em> is computed relative to <em>how much data is moved</em>. Prefill does more math per weight read; decode does less math per weight read. Same weights, different utilisation profiles.</div>
</div>
<div class="misconception">
<div class="myth">✗ Myth 3 — "TTFT and total response time are the same metric"</div>
<div class="reality"><strong>Reality:</strong> TTFT measures only prefill duration — the wait until token 1 appears. Total response time includes all decode steps too. A system with fast prefill but slow decode has great TTFT but poor total time. A system with slow prefill but very fast decode feels unresponsive (long TTFT) but finishes quickly once started. These are orthogonal — you need to optimise them separately with different techniques.</div>
</div>
</section>
<div class="divider"></div>
<!-- ══════════════════════════════
SECTION 10 — QUIZ
══════════════════════════════ -->
<section class="section">
<div class="section-label">Section 10 — Check Your Understanding</div>
<h2>Quiz</h2>
<p class="lead">Three questions on prefill, decode, and their bottlenecks. Wrong answers explain exactly where the reasoning went wrong.</p>
<div class="quiz-block">
<p class="q">1. A user complains that responses from your LLM service "feel slow to start but stream quickly once they begin." Which metric is the problem, and which phase is causing it?</p>
<div class="quiz-opts">
<button onclick="quiz(1,'a',false,'Slow streaming (once started) would be a high TPOT — a slow decode phase. But the complaint is the opposite: slow to start, fast once begun. "Slow to start" describes a high TTFT — the time before token 1 appears — which is determined entirely by prefill duration.')">High TPOT — the decode phase is too slow</button>
<button onclick="quiz(1,'b',true,'Correct! "Slow to start" = high TTFT = slow prefill. "Streams quickly once begun" = low TPOT = fast decode. The bottleneck is the prefill phase — likely caused by long prompts, a heavily loaded system, or lack of prefix caching. Optimising this means reducing prefill time: shorter prompts, prefix caching (Ch.07), or chunked prefill to avoid blocking.')">High TTFT — the prefill phase is slow, decode is fine</button>
<button onclick="quiz(1,'c',false,'These are the two separate metrics for the two phases. TTFT and TPOT can behave completely independently — fast prefill with slow decode, or slow prefill with fast decode, are both common in practice. You need to identify which one is the actual problem.')">Both TTFT and TPOT are high — both phases are slow</button>
</div>
<div class="quiz-fb" id="fb1"></div>
</div>
<div class="quiz-block">
<p class="q">2. During decode, the GPU's CUDA cores are only 18% utilised but HBM bandwidth is at 95%. What would most improve decode speed?</p>
<div class="quiz-opts">
<button onclick="quiz(2,'a',false,'Adding more CUDA cores would not help — decode is memory-bandwidth bound, not compute-bound. At 18% CUDA core utilisation, the existing cores are already underused. They\'re idle because they\'re waiting for data from HBM, not because there aren\'t enough of them. More cores would just mean more idle cores.')">Adding more CUDA cores to the GPU</button>
<button onclick="quiz(2,'b',true,'Correct. HBM bandwidth is the bottleneck at 95% — the memory bus is saturated. Faster HBM (higher GB/s) would deliver model weights to the compute cores more quickly, allowing more decode steps per second. This is the primary reason the H100 (3.35 TB/s HBM) is so much faster than the A100 (2.0 TB/s) for LLM decode — not its CUDA core count.')">Faster HBM memory bandwidth</button>
<button onclick="quiz(2,'c',false,'More SRAM (on-chip cache) helps workloads that are bottlenecked by repeated reads of the same small data. Decode is bottlenecked by reading the large model weights from HBM on every step — this is a streaming access pattern that can\'t be helped by caching, because the data (weights) is too large to fit in SRAM anyway.')">More SRAM on-chip cache</button>
</div>
<div class="quiz-fb" id="fb2"></div>
</div>
<div class="quiz-block">
<p class="q">3. Why does chunked prefill improve the experience for users whose requests are <em>already being decoded</em> — even though their requests are not the ones being prefilled?</p>
<div class="quiz-opts">
<button onclick="quiz(3,'a',false,'Chunked prefill does not make the model run faster per step. The same computation happens either way. What changes is the scheduling: instead of running one giant prefill that blocks everything, smaller prefill chunks are interleaved with decode steps, allowing other sequences to generate tokens during the gaps.')">It makes the model\'s forward pass run faster overall</button>
<button onclick="quiz(3,'b',false,'Chunked prefill does not reduce TPOT for individual sequences. TPOT is determined by model size and HBM bandwidth. What it prevents is decode steps being blocked for hundreds of milliseconds while a long prompt is being prefilled. The per-step decode time is the same; the gap between steps is what shrinks.')">It reduces the TPOT for decode sequences</button>
<button onclick="quiz(3,'c',true,'Correct. Without chunked prefill, a long prompt (e.g. 4,000 tokens) monopolises the GPU for ~800ms of continuous prefill. During that time, decode sequences cannot run — their streaming pauses. With chunked prefill, the 4,000-token prompt is split into 8 chunks of 500. Between each chunk, decode steps run normally. Existing users see continuous token streaming with no pause. The total prefill time is roughly the same, but the interruption to decode is eliminated.')">It prevents long prefill operations from blocking decode steps for other requests</button>
</div>
<div class="quiz-fb" id="fb3"></div>
</div>
</section>
<div class="divider"></div>
<!-- ══════════════════════════════
SECTION 11 — TAKEAWAYS
══════════════════════════════ -->
<section class="section">
<div class="section-label">Section 11 — Key Takeaways</div>
<h2>What you now know</h2>
<div class="takeaways">
<div class="takeaways-label">Chapter 06 — Summary</div>
<div class="takeaway-grid">
<div class="takeaway-item">
<div class="takeaway-check">✓</div>
<p><strong>Prefill is compute-bound.</strong> All input tokens processed in parallel, high arithmetic intensity, CUDA cores saturated. Bottleneck: how many compute units you have. Scales well with batch size.</p>
</div>
<div class="takeaway-item">
<div class="takeaway-check">✓</div>
<p><strong>Decode is memory-bandwidth bound.</strong> One token per step, 14 GB of weights read from HBM per step (7B model), CUDA cores underutilised. Bottleneck: HBM bandwidth. Adding more CUDA cores does not help.</p>
</div>
<div class="takeaway-item">
<div class="takeaway-check">✓</div>
<p><strong>TTFT measures prefill. TPOT measures decode.</strong> High TTFT = slow to start. High TPOT = slow streaming. They're independent — optimise them separately with different techniques.</p>
</div>
<div class="takeaway-item">
<div class="takeaway-check">✓</div>
<p><strong>Same weights, different bottlenecks.</strong> Prefill and decode use identical model weights. The difference is arithmetic intensity — how much math is done per byte of data read from HBM.</p>
</div>
<div class="takeaway-item">
<div class="takeaway-check">✓</div>
<p><strong>Chunked prefill bridges the gap.</strong> Splitting long prompts into chunks and interleaving decode steps prevents prefill starvation — existing sequences keep streaming without pause during long prompt processing.</p>
</div>
<div class="takeaway-item">
<div class="takeaway-check">✓</div>
<p><strong>Two phases, two attention kernels.</strong> Prefill uses <code>flash_attn_varlen_func</code> (dense, contiguous). Decode uses <code>flash_attn_with_kvcache</code> (paged, scattered). Same layer, different dispatch based on <code>seq.is_prefill</code>.</p>
</div>
</div>
</div>
</section>
</main>
<!-- FOOTER -->
<footer class="chapter-footer">
<div class="footer-nav">
<a href="ch05.html" style="font-family:'Space Mono',monospace;font-size:0.7rem;letter-spacing:0.08em;padding:0.6rem 1.25rem;border-radius:3px;text-decoration:none;border:1px solid var(--border);color:var(--muted);transition:all 0.2s;" onmouseover="this.style.borderColor='var(--accent3)';this.style.color='var(--accent3)'" onmouseout="this.style.borderColor='var(--border)';this.style.color='var(--muted)'">← Ch05: Scheduler</a>
<a href="index.html" style="font-family:'Space Mono',monospace;font-size:0.7rem;letter-spacing:0.08em;padding:0.6rem 1.25rem;border-radius:3px;text-decoration:none;border:1px solid var(--border);color:var(--muted);transition:all 0.2s;" onmouseover="this.style.borderColor='var(--accent3)';this.style.color='var(--accent3)'" onmouseout="this.style.borderColor='var(--border)';this.style.color='var(--muted)'">Series Index</a>
<a href="ch07.html" style="font-family:'Space Mono',monospace;font-size:0.7rem;letter-spacing:0.08em;padding:0.6rem 1.25rem;border-radius:3px;text-decoration:none;background:var(--accent3);color:#000;font-weight:700;transition:all 0.2s;" onmouseover="this.style.background='#9be855'" onmouseout="this.style.background='var(--accent3)'">Next: Prefix Caching →</a>
</div>
<div class="footer-credit">
nano-vLLM by <a href="https://github.com/GeeeekExplorer/nano-vllm" target="_blank">GeeeekExplorer</a> · Study series · MIT License
</div>
</footer>
<script>
(function(){
localStorage.setItem('ch06','1');
/* ── LATENCY CALCULATOR ── */
function updateLatency(){
const p = parseInt(document.getElementById('prompt-len').value);
const r = parseInt(document.getElementById('resp-len').value);
document.getElementById('prompt-val').textContent = p;
document.getElementById('resp-val').textContent = r;
// Estimates based on 7B model, single H100, no batching
// Prefill: ~0.35ms per token (H100, fp16, 7B)
// Decode: ~25ms per token (7B fp16, H100, batch=1)
const ttft_ms = Math.round(p * 0.35);
const tpot_ms = 25;
const decode_ms = r * tpot_ms;
const total_ms = ttft_ms + decode_ms;
const fmt = ms => ms >= 1000 ? (ms/1000).toFixed(1)+'s' : ms+'ms';
document.getElementById('ttft-val').textContent = fmt(ttft_ms);
document.getElementById('tpot-val').textContent = tpot_ms+'ms';
document.getElementById('total-val').textContent = fmt(total_ms);
const maxBar = Math.max(ttft_ms, decode_ms, 1);
const ttftPct = Math.min(ttft_ms / (ttft_ms + decode_ms) * 100, 100);
const decodePct = Math.min(decode_ms / (ttft_ms + decode_ms) * 100, 100);
const ttftBar = document.getElementById('ttft-bar');
ttftBar.style.width = Math.max(ttftPct, 1) + '%';
ttftBar.textContent = ttftPct > 8 ? fmt(ttft_ms) + ' (TTFT)' : '';
const decBar = document.getElementById('decode-bar');
decBar.style.width = Math.max(decodePct, 1) + '%';
decBar.textContent = decodePct > 8 ? fmt(decode_ms) + ' (decode)' : '';
const dom = ttft_ms > decode_ms ? 'prefill-dominated' : 'decode-dominated';
document.getElementById('latency-note').textContent =
`Total: ${fmt(total_ms)} · This response is ${dom}. ` +
(ttft_ms > decode_ms
? `Prefill takes ${Math.round(ttft_ms/total_ms*100)}% of total time — prefix caching (Ch.07) would help most here.`
: `Decode takes ${Math.round(decode_ms/total_ms*100)}% of total time — faster HBM or a smaller model would help most.`);
}
window.updateLatency = updateLatency;
updateLatency();
/* ── PHASE SIMULATOR ── */
const PROMPT_WORDS = ['The','quick','brown','fox','jumps','over','the','lazy','dog','near','the','old','oak','tree'];
const GEN_WORDS = ['It','was','a','bright','cold','day','in','April','and','the','clocks','were','striking','thirteen'];
let phState = 'idle'; // idle, prefill_done, decoding, done
let phGenIdx = 0;
function phRenderPrompt(highlightAll){
const bar = document.getElementById('ph-prompt-bar');
bar.innerHTML = PROMPT_WORDS.map((w,i) => {
const cls = highlightAll ? 'tok prompt active' : 'tok prompt';
return `<div class="${cls}" style="opacity:${highlightAll?1:0.4}">${w}</div>`;
}).join('');
}
function phUpdateStats(phase, proc, gen, kv){
const labels = {idle:'IDLE',prefill:'PREFILL',decoding:'DECODING',done:'DONE ✓'};
const cols = {idle:'var(--muted)',prefill:'var(--accent2)',decoding:'var(--accent3)',done:'var(--accent3)'};
const el = document.getElementById('ph-phase-label');
el.textContent = labels[phase]||phase;
el.style.color = cols[phase]||'var(--muted)';
document.getElementById('ph-tokens-proc').textContent = proc;
document.getElementById('ph-tokens-gen').textContent = gen;
document.getElementById('ph-kv-used').textContent = kv;
const maxKV = PROMPT_WORDS.length + GEN_WORDS.length;
const pct = Math.round(kv/maxKV*100);
const kvBar = document.getElementById('ph-kv-bar');
kvBar.style.width = pct + '%';
kvBar.style.background = phase === 'prefill' ? 'var(--accent2)' : 'var(--accent3)';
kvBar.style.color = '#000';
kvBar.textContent = pct > 8 ? kv + ' slots' : '';
// GPU indicators
if(phase === 'prefill'){
document.getElementById('ph-compute-bar').style.width = '95%';
document.getElementById('ph-compute-pct').textContent = '95% (saturated — compute-bound)';
document.getElementById('ph-hbm-bar').style.width = '42%';
document.getElementById('ph-hbm-pct').textContent = '42%';
} else if(phase === 'decoding'){
document.getElementById('ph-compute-bar').style.width = '18%';
document.getElementById('ph-compute-pct').textContent = '18% (underutilised)';
document.getElementById('ph-hbm-bar').style.width = '95%';
document.getElementById('ph-hbm-pct').textContent = '95% (saturated — memory-bound)';
} else {
document.getElementById('ph-compute-bar').style.width = '0%';
document.getElementById('ph-compute-pct').textContent = '0%';
document.getElementById('ph-hbm-bar').style.width = '0%';
document.getElementById('ph-hbm-pct').textContent = '0%';
}
}
window.phStep = function(){
const btn = document.getElementById('ph-btn');
if(phState === 'idle'){
// Run prefill — all prompt tokens at once
phRenderPrompt(true);
phUpdateStats('prefill', PROMPT_WORDS.length, 0, PROMPT_WORDS.length);
document.getElementById('ph-note').textContent =
`Prefill complete: all ${PROMPT_WORDS.length} prompt tokens processed in ONE forward pass. KV cache written for all of them. CUDA cores: 95% busy. This is compute-bound.`;
phState = 'prefill_done';
btn.textContent = '▶ Run Decode Step';
} else if(phState === 'prefill_done' || phState === 'decoding'){
if(phGenIdx >= GEN_WORDS.length){
phState = 'done';
phUpdateStats('done', PROMPT_WORDS.length, GEN_WORDS.length, PROMPT_WORDS.length + GEN_WORDS.length);
document.getElementById('ph-note').textContent = `Generation complete. ${GEN_WORDS.length} tokens generated in ${GEN_WORDS.length} separate decode steps. Total KV cache: ${PROMPT_WORDS.length + GEN_WORDS.length} slots.`;
btn.textContent = '✓ Done — Reset to replay';
btn.style.background = 'rgba(180,255,111,0.08)';
btn.style.borderColor = 'rgba(180,255,111,0.2)';
btn.style.color = 'var(--muted)';
btn.disabled = true;
return;
}
// One decode step
phState = 'decoding';
phGenIdx++;
const genBar = document.getElementById('ph-gen-bar');
const newTok = document.createElement('div');
newTok.className = 'tok gen';
newTok.textContent = GEN_WORDS[phGenIdx-1];
genBar.appendChild(newTok);
const kv = PROMPT_WORDS.length + phGenIdx;
phUpdateStats('decoding', PROMPT_WORDS.length, phGenIdx, kv);
document.getElementById('ph-note').textContent =
`Decode step ${phGenIdx}: processed only token "${GEN_WORDS[phGenIdx-1]}". Read KV cache for all ${kv-1} prior tokens. HBM bandwidth: 95% (memory-bound). CUDA cores: 18% (underutilised).`;
btn.textContent = phGenIdx < GEN_WORDS.length ? '▶ Next Decode Token' : '▶ Finish Generation';
}
};
window.phReset = function(){
phState = 'idle'; phGenIdx = 0;
phRenderPrompt(false);
document.getElementById('ph-gen-bar').innerHTML = '';
phUpdateStats('idle', 0, 0, 0);
document.getElementById('ph-note').textContent = 'Click "Run Prefill" to start.';
const btn = document.getElementById('ph-btn');
btn.textContent = '▶ Run Prefill';
btn.style.background = 'rgba(180,255,111,0.12)';
btn.style.borderColor = 'rgba(180,255,111,0.35)';
btn.style.color = 'var(--accent3)';
btn.disabled = false;
};
phReset();
/* ── QUIZ ── */
const CORRECT = {1:'b', 2:'b', 3:'c'};
window.quiz = function(n, opt, isCorrect, msg){
const fb = document.getElementById('fb'+n);
fb.textContent = msg;
fb.className = 'quiz-fb show ' + (isCorrect ? 'ok' : 'no');
fb.closest('.quiz-block').querySelectorAll('button').forEach((b,i) => {
b.disabled = true;
const letters = ['a','b','c'];
if(letters[i] === opt && !isCorrect) b.classList.add('wrong');
if(letters[i] === CORRECT[n]) b.classList.add('correct');
});
};
})();
</script>
<!--
SELF-CRITIQUE SCORES
Comprehensiveness : 5/5 — all terms defined: prefill, decode, compute-bound,
memory-bound, arithmetic intensity, TTFT, TPOT,
chunked prefill, prefill starvation. All xrefs present
(KV cache Ch.03, HBM Ch.01, scheduler Ch.05,
PagedAttention Ch.04, prefix caching Ch.07,
FlashAttention Ch.10). Edge cases: batching effect on
decode, chunked prefill chunk size trade-off, speculative
decoding mentioned for completeness. All mandatory sections
present.
Coherence : 5/5 — factory analogy → prefill properties → decode properties
→ bottleneck visual → TTFT/TPOT metrics + interactive
calculator → live phase simulator → chunked prefill →
code → why it matters → misconceptions → quiz →
takeaways. Analogy maps precisely to both phases before
any technical terms appear.
Depth : 5/5 — concrete numbers throughout: 0.35ms/token prefill, 25ms/
token decode (H100, 7B), 14 GB weight read per decode
step, 96% vs 18% CUDA core utilisation, 95% HBM
utilisation. Interactive TTFT/TPOT calculator shows
real numbers updating live. Phase simulator shows GPU
utilisation bars switching between compute-bound and
memory-bound as phases change. Chunked prefill before/after
with concrete 800ms example.
Beginner access : 5/5 — factory/paint shop analogy before "compute-bound" or
"memory-bound" are ever named; long division vs copying
numbers analogy for arithmetic intensity; "compute-bound"
and "memory-bound" defined as plain English before the
technical analysis; TTFT/TPOT introduced as user-facing
feelings ("responsiveness" and "streaming speed") before
technical definitions; phase simulator labels bottleneck
as "(saturated — compute-bound)" and "(underutilised)"
inline so the concept is always visible.
Known gaps : speculative decoding mentioned but explicitly deferred — not
in nano-vLLM scope. flash_attn internals deferred to Ch.10.
-->
</body>
</html>