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
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FlashInfer: Attention States & Recursive Merge</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Inter', sans-serif;
            overflow-x: hidden;
        }
        .math {
            font-family: 'Times New Roman', serif;
            font-style: italic;
        }
        .node {
            transition: all 0.5s ease-in-out;
        }
        .arrow {
            stroke-dasharray: 10;
            animation: dash 1s linear infinite;
        }
        @keyframes dash {
            to {
                stroke-dashoffset: -20;
            }
        }
        .highlight {
            animation: pulse 2s infinite;
        }
        @keyframes pulse {
            0% { transform: scale(1); opacity: 1; }
            50% { transform: scale(1.05); opacity: 0.9; }
            100% { transform: scale(1); opacity: 1; }
        }
        .fade-in {
            opacity: 0;
            animation: fadeIn 1s forwards;
        }
        @keyframes fadeIn {
            to { opacity: 1; }
        }
        .equation {
            transition: all 0.5s ease;
        }
        .panel {
            transition: transform 0.5s ease-out;
        }
    </style>
</head>
<body class="bg-gray-100 text-gray-900">
    <div class="container mx-auto p-4 max-w-6xl">
        <header class="text-center my-8">
            <h1 class="text-4xl font-bold text-blue-800 mb-2">FlashInfer: Attention States & Recursive Merge</h1>
            <p class="text-xl text-gray-600">Visualizing how FlashInfer accelerates LLM inference</p>
        </header>

        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <h2 class="text-2xl font-semibold mb-4 text-blue-700">Key Innovation: Attention States</h2>
            <p class="mb-4">FlashInfer introduces the concept of <strong>attention states</strong>, which fully characterize the attention between a query and a set of key/value pairs. Each attention state consists of two components:</p>
            
            <div class="flex flex-col md:flex-row gap-4 mb-6">
                <div class="flex-1 bg-blue-50 p-4 rounded-lg">
                    <h3 class="font-medium text-blue-800 mb-2">Generalized Score (s)</h3>
                    <div class="flex justify-center">
                        <div class="math text-xl">
                            s(I) = log(βˆ‘<sub>i∈I</sub> exp(s<sub>i</sub>))
                        </div>
                    </div>
                    <p class="mt-2 text-sm text-gray-600">The log-sum-exp (LSE) of pre-softmax attention scores</p>
                </div>
                
                <div class="flex-1 bg-blue-50 p-4 rounded-lg">
                    <h3 class="font-medium text-blue-800 mb-2">Generalized Value (v)</h3>
                    <div class="flex justify-center">
                        <div class="math text-xl">
                            v(I) = βˆ‘<sub>i∈I</sub> softmax(s<sub>i</sub>)v<sub>i</sub>
                        </div>
                    </div>
                    <p class="mt-2 text-sm text-gray-600">The weighted sum of value vectors using the softmax of scores</p>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <h2 class="text-2xl font-semibold mb-4 text-blue-700">Recursive Merge Operator</h2>
            <p class="mb-4">The key insight of FlashInfer is that attention states can be <strong>merged</strong> efficiently. Given two attention states corresponding to different subsets of KV pairs, we can compute the attention state for their union:</p>
            
            <div class="bg-blue-50 p-4 rounded-lg mb-6">
                <div class="flex justify-center">
                    <div class="math text-xl">
                        [v(IβˆͺJ), s(IβˆͺJ)] = [v(I), s(I)] βŠ• [v(J), s(J)]
                    </div>
                </div>
            </div>
            
            <p class="mb-6">This merge operator (βŠ•) is <strong>commutative</strong> and <strong>associative</strong>, allowing flexible computation strategies.</p>
        </div>

        <!-- Interactive Visualization -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <h2 class="text-2xl font-semibold mb-4 text-blue-700">Interactive Visualization</h2>
            <p class="mb-6">This animation shows how FlashInfer computes attention for a query over 4 KV pairs by partitioning the work and merging results.</p>
            
            <div class="flex justify-center">
                <div class="relative" style="width: 700px; height: 650px;" id="visualization">
                    <!-- SVG will be inserted here -->
                </div>
            </div>
            
            <div class="flex justify-center mt-4">
                <button id="resetBtn" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg mr-4">
                    Reset Animation
                </button>
                <button id="playBtn" class="bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-lg">
                    Play Animation
                </button>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <h2 class="text-2xl font-semibold mb-4 text-blue-700">Applications</h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="bg-blue-50 p-4 rounded-lg">
                    <h3 class="font-medium text-blue-800 mb-2">Shared-Prefix Batch Decoding</h3>
                    <p>When multiple sequences share a common prefix (e.g., same prompt), compute the attention state for the shared part once, then merge with each sequence's unique suffix.</p>
                    <p class="mt-2 text-sm font-medium text-green-600">Up to 30x speedup in long-context scenarios</p>
                </div>
                
                <div class="bg-blue-50 p-4 rounded-lg">
                    <h3 class="font-medium text-blue-800 mb-2">KV Sequence Parallelism</h3>
                    <p>Partition long KV sequences across multiple processing units, compute partial attention states in parallel, then merge the results.</p>
                    <p class="mt-2 text-sm font-medium text-green-600">Improves GPU utilization for memory-constrained scenarios</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        // SVG for the visualization
        const svg = `
        <svg width="100%" height="100%" viewBox="0 0 700 650">
            <!-- Query Node -->
            <g class="fade-in" style="animation-delay: 0.5s">
                <circle id="queryNode" cx="350" cy="50" r="30" fill="#4299e1" />
                <text x="350" y="55" text-anchor="middle" fill="white" font-weight="bold">Q</text>
            </g>
            
            <!-- KV Nodes -->
            <g class="fade-in" style="animation-delay: 1s">
                <circle id="kv1" cx="175" cy="150" r="25" fill="#9ae6b4" />
                <text x="175" y="155" text-anchor="middle" fill="#2f855a" font-weight="bold">KV₁</text>
                
                <circle id="kv2" cx="275" cy="150" r="25" fill="#9ae6b4" />
                <text x="275" y="155" text-anchor="middle" fill="#2f855a" font-weight="bold">KVβ‚‚</text>
                
                <circle id="kv3" cx="425" cy="150" r="25" fill="#9ae6b4" />
                <text x="425" y="155" text-anchor="middle" fill="#2f855a" font-weight="bold">KV₃</text>
                
                <circle id="kv4" cx="525" cy="150" r="25" fill="#9ae6b4" />
                <text x="525" y="155" text-anchor="middle" fill="#2f855a" font-weight="bold">KVβ‚„</text>
            </g>
            
            <!-- Lines connecting Query to KVs -->
            <g class="fade-in" style="animation-delay: 1.5s">
                <path id="line1" d="M 350 80 L 175 125" stroke="#4299e1" stroke-width="2" fill="none" />
                <path id="line2" d="M 350 80 L 275 125" stroke="#4299e1" stroke-width="2" fill="none" />
                <path id="line3" d="M 350 80 L 425 125" stroke="#4299e1" stroke-width="2" fill="none" />
                <path id="line4" d="M 350 80 L 525 125" stroke="#4299e1" stroke-width="2" fill="none" />
            </g>
            
            <!-- Attention State Nodes -->
            <g id="stateNodes">
                <!-- These will be animated in via JS -->
                <g id="state1" opacity="0">
                    <circle cx="175" cy="250" r="25" fill="#feb2b2" />
                    <text x="175" y="255" text-anchor="middle" fill="#742a2a" font-weight="bold">s₁,v₁</text>
                </g>
                
                <g id="state2" opacity="0">
                    <circle cx="275" cy="250" r="25" fill="#feb2b2" />
                    <text x="275" y="255" text-anchor="middle" fill="#742a2a" font-weight="bold">sβ‚‚,vβ‚‚</text>
                </g>
                
                <g id="state3" opacity="0">
                    <circle cx="425" cy="250" r="25" fill="#feb2b2" />
                    <text x="425" y="255" text-anchor="middle" fill="#742a2a" font-weight="bold">s₃,v₃</text>
                </g>
                
                <g id="state4" opacity="0">
                    <circle cx="525" cy="250" r="25" fill="#feb2b2" />
                    <text x="525" y="255" text-anchor="middle" fill="#742a2a" font-weight="bold">sβ‚„,vβ‚„</text>
                </g>
                
                <!-- Merge Level 1 -->
                <g id="merge1" opacity="0">
                    <circle cx="225" cy="350" r="30" fill="#fbd38d" />
                    <text x="225" y="355" text-anchor="middle" fill="#7b341e" font-weight="bold">s₁₂,v₁₂</text>
                </g>
                
                <g id="merge2" opacity="0">
                    <circle cx="475" cy="350" r="30" fill="#fbd38d" />
                    <text x="475" y="355" text-anchor="middle" fill="#7b341e" font-weight="bold">s₃₄,v₃₄</text>
                </g>
                
                <!-- Final Merge -->
                <g id="finalMerge" opacity="0">
                    <circle cx="350" cy="450" r="35" fill="#d6bcfa" />
                    <text x="350" y="455" text-anchor="middle" fill="#553c9a" font-weight="bold">s₁₂₃₄,v₁₂₃₄</text>
                </g>
            </g>
            
            <!-- Merge Arrows - will be animated in -->
            <g id="mergeArrows">
                <!-- State to Merge Level 1 -->
                <path id="arrow1" opacity="0" d="M 175 275 L 210 325" stroke="#f6ad55" stroke-width="3" fill="none" class="arrow" marker-end="url(#arrowhead)" />
                <path id="arrow2" opacity="0" d="M 275 275 L 240 325" stroke="#f6ad55" stroke-width="3" fill="none" class="arrow" marker-end="url(#arrowhead)" />
                <path id="arrow3" opacity="0" d="M 425 275 L 460 325" stroke="#f6ad55" stroke-width="3" fill="none" class="arrow" marker-end="url(#arrowhead)" />
                <path id="arrow4" opacity="0" d="M 525 275 L 490 325" stroke="#f6ad55" stroke-width="3" fill="none" class="arrow" marker-end="url(#arrowhead)" />
                
                <!-- Merge Level 1 to Final -->
                <path id="arrow5" opacity="0" d="M 225 380 L 320 425" stroke="#b794f4" stroke-width="3" fill="none" class="arrow" marker-end="url(#arrowhead)" />
                <path id="arrow6" opacity="0" d="M 475 380 L 380 425" stroke="#b794f4" stroke-width="3" fill="none" class="arrow" marker-end="url(#arrowhead)" />
            </g>
            
            <!-- Equation Panel -->
            <g id="equationPanel" opacity="0" transform="translate(350, 580)">
                <rect x="-330" y="-48" width="660" height="72" rx="10" fill="#ebf8ff" stroke="#4299e1" stroke-width="2" />
                <text id="equationText" x="0" y="0" text-anchor="middle" font-family="monospace" font-size="14">
                    Attention States: Computing...
                </text>
            </g>
            
            <!-- Arrow marker definition -->
            <defs>
                <marker id="arrowhead" markerWidth="10" markerHeight="7" refX="9" refY="3.5" orient="auto">
                    <polygon points="0 0, 10 3.5, 0 7" fill="#000" />
                </marker>
            </defs>
        </svg>`;
        
        // Insert the SVG
        document.getElementById('visualization').innerHTML = svg;
        
        // Animation sequence
        let animationStep = 0;
        let animationInterval;
        const steps = [
            // Step 0: Already loaded query and KVs
            function() {
                document.getElementById('equationText').textContent = "Computing individual attention states for each KV pair";
                document.getElementById('equationPanel').setAttribute('opacity', '1');
            },
            // Step 1: Show state1
            function() {
                document.getElementById('state1').setAttribute('opacity', '1');
                document.getElementById('equationText').textContent = "s₁ = qΒ·k₁ᡀ, v₁ = softmax(s₁)Β·v₁";
            },
            // Step 2: Show state2
            function() {
                document.getElementById('state2').setAttribute('opacity', '1');
                document.getElementById('equationText').textContent = "sβ‚‚ = qΒ·kβ‚‚α΅€, vβ‚‚ = softmax(sβ‚‚)Β·vβ‚‚";
            },
            // Step 3: Show state3
            function() {
                document.getElementById('state3').setAttribute('opacity', '1');
                document.getElementById('equationText').textContent = "s₃ = qΒ·k₃ᡀ, v₃ = softmax(s₃)Β·v₃";
            },
            // Step 4: Show state4
            function() {
                document.getElementById('state4').setAttribute('opacity', '1');
                document.getElementById('equationText').textContent = "sβ‚„ = qΒ·kβ‚„α΅€, vβ‚„ = softmax(sβ‚„)Β·vβ‚„";
            },
            // Step 5: First merge arrows
            function() {
                document.getElementById('arrow1').setAttribute('opacity', '1');
                document.getElementById('arrow2').setAttribute('opacity', '1');
                document.getElementById('equationText').textContent = "Merging s₁,v₁ and sβ‚‚,vβ‚‚ using the βŠ• operator";
            },
            // Step 6: First merge result
            function() {
                document.getElementById('merge1').setAttribute('opacity', '1');
                document.getElementById('equationText').textContent = "s₁₂ = log(e^s₁ + e^sβ‚‚), v₁₂ = (e^s₁·v₁ + e^sβ‚‚Β·vβ‚‚)/(e^s₁ + e^sβ‚‚)";
            },
            // Step 7: Second merge arrows
            function() {
                document.getElementById('arrow3').setAttribute('opacity', '1');
                document.getElementById('arrow4').setAttribute('opacity', '1');
                document.getElementById('equationText').textContent = "Merging s₃,v₃ and sβ‚„,vβ‚„ using the βŠ• operator";
            },
            // Step 8: Second merge result
            function() {
                document.getElementById('merge2').setAttribute('opacity', '1');
                document.getElementById('equationText').textContent = "s₃₄ = log(e^s₃ + e^sβ‚„), v₃₄ = (e^s₃·v₃ + e^sβ‚„Β·vβ‚„)/(e^s₃ + e^sβ‚„)";
            },
            // Step 9: Final merge arrows
            function() {
                document.getElementById('arrow5').setAttribute('opacity', '1');
                document.getElementById('arrow6').setAttribute('opacity', '1');
                document.getElementById('equationText').textContent = "Final merge: Combining s₁₂,v₁₂ and s₃₄,v₃₄";
            },
            // Step 10: Final result
            function() {
                document.getElementById('finalMerge').setAttribute('opacity', '1');
                document.getElementById('finalMerge').classList.add('highlight');
                document.getElementById('equationText').textContent = "s₁₂₃₄ = log(e^s₁₂ + e^s₃₄), v₁₂₃₄ = (e^s₁₂·v₁₂ + e^s₃₄·v₃₄)/(e^s₁₂ + e^s₃₄)";
            },
            // Step 11: Show final explanation
            function() {
                document.getElementById('equationText').innerHTML = 
                    "Complete! This is equivalent to standard attention but computed in parallel.";
                clearInterval(animationInterval);
                animationStep = 0; // Reset for next time
            }
        ];
        
        function resetAnimation() {
            // Reset all animated elements
            clearInterval(animationInterval);
            
            const stateNodes = document.querySelectorAll('#stateNodes > g');
            stateNodes.forEach(node => {
                node.setAttribute('opacity', '0');
                node.classList.remove('highlight');
            });
            
            const arrows = document.querySelectorAll('#mergeArrows > path');
            arrows.forEach(arrow => {
                arrow.setAttribute('opacity', '0');
            });
            
            document.getElementById('equationPanel').setAttribute('opacity', '0');
            
            animationStep = 0;
        }
        
        function playAnimation() {
            resetAnimation();
            
            // Start the animation sequence
            steps[animationStep]();
            animationStep++;
            
            animationInterval = setInterval(() => {
                if (animationStep < steps.length) {
                    steps[animationStep]();
                    animationStep++;
                } else {
                    clearInterval(animationInterval);
                }
            }, 1500); // Animation step timing
        }
        
        // Button handlers
        document.getElementById('resetBtn').addEventListener('click', resetAnimation);
        document.getElementById('playBtn').addEventListener('click', playAnimation);
        
        // Auto-start animation when page loads
        window.addEventListener('load', () => {
            setTimeout(playAnimation, 1000);
        });
    </script>
</body>
</html>