-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualization.js
More file actions
3843 lines (3387 loc) · 147 KB
/
Copy pathvisualization.js
File metadata and controls
3843 lines (3387 loc) · 147 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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// KV Cache Growth Visualization
// Memory calculations based on LMCache KV Cache Calculator
// Source: https://lmcache.ai/kv_cache_calculator.html
// Project: https://github.com/LMCache/kvcache-view
// This visualization uses the exact formulas from LMCache's calculator
// to accurately compute KV cache memory requirements
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
// Check if mobile
const isMobile = window.matchMedia('(max-width: 768px)').matches
// Model configurations from LMCache
// Using distinct colors for each model
const models = [
{
name: 'Llama-3.2-1B',
params: 1.2,
layers: 16,
hidden_size: 2048,
num_heads: 32,
num_kv_heads: 8,
color: '#5FA3E6', // Light Blue
efficiency: 'high',
},
{
name: 'Phi-3.5-mini',
params: 3.8,
layers: 32,
hidden_size: 3072,
num_heads: 32,
num_kv_heads: 32,
color: '#00C853', // Green
efficiency: 'high',
},
{
name: 'Llama-3.1-8B',
params: 8,
layers: 32,
hidden_size: 4096,
num_heads: 32,
num_kv_heads: 8,
color: '#1428A0', // Deep Blue
efficiency: 'medium',
},
{
name: 'Gemma-2-9B',
params: 9,
layers: 42,
hidden_size: 3584,
num_heads: 16,
num_kv_heads: 8,
color: '#4285F4', // Google Blue
efficiency: 'high',
},
{
name: 'Qwen2.5-14B',
params: 14,
layers: 48,
hidden_size: 5120,
num_heads: 40,
num_kv_heads: 8,
color: '#FF9800', // Orange
efficiency: 'medium',
},
{
name: 'Phi-3.5-MoE',
params: 41.9, // 16 experts, 2 active
layers: 32,
hidden_size: 4096,
num_heads: 32,
num_kv_heads: 8,
num_local_experts: 16,
num_experts_per_tok: 2,
architecture: 'moe',
color: '#00BCD4', // Cyan
efficiency: 'high',
},
{
name: 'Gemma-2-27B',
params: 27,
layers: 46,
hidden_size: 4608,
num_heads: 32,
num_kv_heads: 16,
color: '#34A853', // Google Green
efficiency: 'medium',
},
{
name: 'Qwen2.5-32B',
params: 32,
layers: 64,
hidden_size: 5120,
num_heads: 40,
num_kv_heads: 8,
color: '#FFC107', // Amber
efficiency: 'medium',
},
{
name: 'Mixtral-8x7B',
params: 46.7, // 8 experts, 2 active = 12.9B active
layers: 32,
hidden_size: 4096,
num_heads: 32,
num_kv_heads: 8,
num_local_experts: 8,
num_experts_per_tok: 2,
architecture: 'moe',
color: '#9C27B0', // Deep Purple
efficiency: 'high',
},
{
name: 'Llama-3.1-70B',
params: 70,
layers: 80,
hidden_size: 8192,
num_heads: 64,
num_kv_heads: 8,
color: '#691FFF', // Purple
efficiency: 'low',
},
{
name: 'Qwen2.5-72B',
params: 72,
layers: 80,
hidden_size: 8192,
num_heads: 64,
num_kv_heads: 8,
color: '#FF5722', // Deep Orange
efficiency: 'low',
},
{
name: 'Qwen3-Next-80B',
params: 80,
active_params: 3,
layers: 48,
hidden_size: 2048,
num_heads: 16,
num_kv_heads: 2,
num_local_experts: 512,
num_experts_per_tok: 11,
architecture: 'qwen3-next',
color: '#795548', // Brown
efficiency: 'optimized',
},
{
name: 'Qwen3-Omni-30B',
params: 30,
active_params: 3,
layers: 36,
hidden_size: 4096,
num_heads: 32,
num_kv_heads: 8,
num_local_experts: 128,
num_experts_per_tok: 8,
architecture: 'moe',
color: '#8E24AA', // Purple
efficiency: 'optimized',
},
{
name: 'Mixtral-8x22B',
params: 141, // 8 experts, 2 active = 39.1B active
layers: 56,
hidden_size: 6144,
num_heads: 48,
num_kv_heads: 8,
num_local_experts: 8,
num_experts_per_tok: 2,
architecture: 'moe',
color: '#7B1FA2', // Dark Purple
efficiency: 'medium',
},
{
name: 'Llama-3.1-405B',
params: 405,
layers: 126,
hidden_size: 16384,
num_heads: 128,
num_kv_heads: 8,
color: '#E4002B', // Red
efficiency: 'very-low',
},
{
name: 'DeepSeek-V3 (671B)',
params: 671,
layers: 61,
kv_lora_rank: 512,
qk_rope_head_dim: 64,
color: '#FF6B00', // Orange
efficiency: 'optimized',
special: 'deepseek',
},
]
let currentModelIndex = 0
let currentTokens = 0
let maxTokens = 128000 // 128K context default (industry standard)
let animationSpeed = 50
let isPlaying = false // start paused so first click plays
let particles = []
let memoryBlocks = []
let waves = []
let currentDtype = 'FP16'
let currentFactoidIndex = 0
let pausedTime = 0 // Fixed time when paused (0 = frozen at start)
let animationTime = 0 // Current animation time
let lastFactoidUpdate = 0
// Get animation time that respects pause state
function getAnimationTime() {
return isPlaying ? Date.now() : pausedTime
}
let lastCriticalState = 'none'
let lastPopupTime = 0
const POPUP_COOLDOWN_MS = 10000
let includeWeights = true // Include model weights memory by default
let batchSize = 8 // Number of concurrent queries per GPU (modern production default)
let dataFlowParticles = [] // Particles flowing between HBM and GPU
let continuousBatching = false // Enable continuous batching with variable sequence lengths
let batchSequenceLengths = [] // Array of sequence lengths for each request in batch
let pagedAttention = false // Enable paged attention for memory fragmentation visualization
let flashAttention = false // Enable Flash Attention for tiled computation and reduced bandwidth
let sequenceColors = [] // Colors for each sequence in continuous batching
// GPU configurations (per-GPU memory in GiB)
const gpuConfigs = {
// NVIDIA - NVLink support and PCIe generations
'Tesla T4 16G': {
memGiB: 16,
label: 'Tesla T4 16G',
memType: 'GDDR6',
l2Cache: 6,
flashTileSize: 32,
nvlink: false,
pcieGen: 3,
},
'RTX 4090 24G': {
memGiB: 24,
label: 'RTX 4090 24G',
memType: 'GDDR6X',
l2Cache: 72,
flashTileSize: 64,
nvlink: false,
pcieGen: 4,
},
'L40S 48G': {
memGiB: 48,
label: 'L40S 48G',
memType: 'GDDR6',
l2Cache: 96,
flashTileSize: 64,
nvlink: false,
pcieGen: 4,
},
'A100 40G': {
memGiB: 40,
label: 'A100 40G',
memType: 'HBM2e',
l2Cache: 40,
flashTileSize: 128,
nvlink: true,
nvlinkBW: 600,
pcieGen: 4,
},
'A100 80G': {
memGiB: 80,
label: 'A100 80G',
memType: 'HBM2e',
l2Cache: 40,
flashTileSize: 128,
nvlink: true,
nvlinkBW: 600,
pcieGen: 4,
},
'H100 80G': {
memGiB: 80,
label: 'H100 80G',
memType: 'HBM3',
l2Cache: 50,
flashTileSize: 128,
nvlink: true,
nvlinkBW: 900,
pcieGen: 5,
},
'H200 141G': {
memGiB: 141,
label: 'H200 141G',
memType: 'HBM3e',
l2Cache: 50,
flashTileSize: 128,
nvlink: true,
nvlinkBW: 900,
pcieGen: 5,
},
'B100 80G': {
memGiB: 80,
label: 'B100 80G',
memType: 'HBM3e',
l2Cache: 60,
flashTileSize: 128,
nvlink: true,
nvlinkBW: 1800,
pcieGen: 6,
},
'B200 192G': {
memGiB: 192,
label: 'B200 192G',
memType: 'HBM3e',
l2Cache: 60,
flashTileSize: 128,
nvlink: true,
nvlinkBW: 1800,
pcieGen: 6,
},
'GB200 384G': {
memGiB: 384,
label: 'GB200 NVL2 384G',
memType: 'HBM3e',
l2Cache: 120,
flashTileSize: 256,
nvlink: true,
nvlinkBW: 1800,
pcieGen: 6,
},
// AMD Radeon Pro (workstation) - No Infinity Fabric Link on workstation cards
'AMD W7800 32G': {
memGiB: 32,
label: 'AMD W7800 32G',
memType: 'GDDR6',
l2Cache: 64,
flashTileSize: 64,
nvlink: false,
pcieGen: 4,
},
'AMD W7900 48G': {
memGiB: 48,
label: 'AMD W7900 48G',
memType: 'GDDR6',
l2Cache: 96,
flashTileSize: 64,
nvlink: false,
pcieGen: 4,
},
// AMD Instinct (data center) - Infinity Fabric Link support
'AMD MI210 64G': {
memGiB: 64,
label: 'AMD MI210 64G',
memType: 'HBM2e',
l2Cache: 32,
flashTileSize: 64,
nvlink: true,
nvlinkBW: 300,
ifl: true,
pcieGen: 4,
},
'AMD MI250X 128G': {
memGiB: 128,
label: 'AMD MI250X 128G',
memType: 'HBM2e',
l2Cache: 32,
flashTileSize: 64,
nvlink: true,
nvlinkBW: 400,
ifl: true,
pcieGen: 4,
},
'AMD MI300X 192G': {
memGiB: 192,
label: 'AMD MI300X 192G',
memType: 'HBM3',
l2Cache: 256,
flashTileSize: 128,
nvlink: true,
nvlinkBW: 896,
ifl: true,
pcieGen: 5,
},
// Intel (GPU + AI accelerators)
'Intel Arc A770 16G': {
memGiB: 16,
label: 'Intel Arc A770 16G',
memType: 'GDDR6',
l2Cache: 16,
flashTileSize: 32,
nvlink: false,
pcieGen: 4,
},
'Intel Max 1550 128G': {
memGiB: 128,
label: 'Intel Max 1550 128G',
memType: 'HBM2e',
l2Cache: 408,
flashTileSize: 128,
nvlink: false,
pcieGen: 4,
},
'Intel Gaudi2 96G': {
memGiB: 96,
label: 'Intel Gaudi2 96G',
memType: 'HBM2e',
l2Cache: 48,
flashTileSize: 64,
nvlink: false,
pcieGen: 4,
},
// Google TPU (approx per-chip HBM) - Has proprietary interconnect, PCIe for host connection
'Google TPU v3 16G': {
memGiB: 16,
label: 'Google TPU v3 16G',
memType: 'HBM2',
l2Cache: 16,
flashTileSize: 64,
nvlink: true,
nvlinkBW: 700,
tpuInterconnect: true,
pcieGen: 3,
},
'Google TPU v4 32G': {
memGiB: 32,
label: 'Google TPU v4 32G',
memType: 'HBM2',
l2Cache: 32,
flashTileSize: 128,
nvlink: true,
nvlinkBW: 1200,
tpuInterconnect: true,
pcieGen: 4,
},
// Graphcore and Cerebras - Proprietary fabrics
'Graphcore IPU Mk2 0.9G': {
memGiB: 0.9,
label: 'Graphcore IPU Mk2 0.9G',
memType: 'SRAM',
l2Cache: 900,
flashTileSize: 256,
nvlink: true,
nvlinkBW: 320,
ipuLink: true,
pcieGen: 4,
},
'Cerebras WSE-2 40G': {
memGiB: 40,
label: 'Cerebras WSE-2 40G',
memType: 'SRAM',
l2Cache: 40960,
flashTileSize: 512,
nvlink: false, // Single wafer, no multi-chip needed
pcieGen: 4,
},
// Qualcomm Cloud AI
'Qualcomm Cloud AI 100 32G': {
memGiB: 32,
label: 'Qualcomm Cloud AI 100 32G',
memType: 'LPDDR5',
l2Cache: 32,
flashTileSize: 64,
nvlink: false,
pcieGen: 4,
},
}
// Multi-GPU configuration
let gpuCount = 1 // Number of GPUs (powers of 2: 1, 2, 4, 8, 16, 32, 64, 128)
const validGPUCounts = [1, 2, 4, 8, 16, 32, 64, 128]
const pcie4Bandwidth = 32 // PCIe 4.0 x16 bandwidth in GB/s (most GPUs)
const pcie5Bandwidth = 64 // PCIe 5.0 x16 bandwidth in GB/s (H100, H200, MI300X)
const pcie6Bandwidth = 128 // PCIe 6.0 x16 bandwidth in GB/s (B100, B200, GB200)
let useHighSpeedInterconnect = true // Use NVLink/IFL when available vs PCIe
let currentGPU = 'H100 80G'
// Famous GPU datacenter configurations
const worldDatacenters = {
none: { name: 'None', gpus: null, gpu: null, model: null },
dgx_h100: { name: 'DGX H100', gpus: 8, gpu: 'H100 80G', model: 'Llama-3.1-70B', interconnect: 'nvlink' },
dgx_h200: { name: 'DGX H200', gpus: 8, gpu: 'H200 141G', model: 'Llama-3.1-70B', interconnect: 'nvlink' },
dgx_b200: { name: 'DGX B200', gpus: 8, gpu: 'B200 192G', model: 'Llama-3.1-405B', interconnect: 'nvlink' },
dgx_gb200: {
name: 'DGX GB200 NVL72',
gpus: 72,
gpu: 'GB200 384G',
model: 'Llama-3.1-405B',
interconnect: 'nvlink',
},
dgx_pod: { name: 'DGX SuperPOD', gpus: 32, gpu: 'H100 80G', model: 'Llama-3.1-405B', interconnect: 'nvlink' },
meta_rsc: { name: 'Meta RSC', gpus: 128, gpu: 'A100 80G', model: 'Llama-3.1-405B', interconnect: 'nvlink' },
openai_cluster: { name: 'OpenAI GPT-4', gpus: 64, gpu: 'A100 40G', model: 'Llama-3.1-70B', interconnect: 'nvlink' },
aws_p4: { name: 'AWS P4d.24xl', gpus: 8, gpu: 'A100 40G', model: 'Llama-3.1-8B', interconnect: 'nvlink' },
aws_p5: { name: 'AWS P5.48xl', gpus: 8, gpu: 'H100 80G', model: 'Llama-3.1-70B', interconnect: 'nvlink' },
gcp_a2: { name: 'GCP A2 Ultra', gpus: 16, gpu: 'A100 40G', model: 'Llama-3.1-70B', interconnect: 'nvlink' },
gcp_a3: { name: 'GCP A3 Mega', gpus: 8, gpu: 'H100 80G', model: 'Llama-3.1-70B', interconnect: 'nvlink' },
azure_nd96: { name: 'Azure NDv4', gpus: 8, gpu: 'A100 40G', model: 'Llama-3.1-8B', interconnect: 'nvlink' },
azure_ndh100: { name: 'Azure NDm H100', gpus: 8, gpu: 'H100 80G', model: 'Llama-3.1-70B', interconnect: 'nvlink' },
lambda_1x: { name: 'Lambda 1-Click', gpus: 1, gpu: 'H100 80G', model: 'Llama-3.1-8B', interconnect: 'none' },
lambda_8x: { name: 'Lambda Cloud 8x', gpus: 8, gpu: 'A100 80G', model: 'Llama-3.1-70B', interconnect: 'nvlink' },
amd_mi300: { name: 'AMD MI300X', gpus: 8, gpu: 'AMD MI300X 192G', model: 'Llama-3.1-70B', interconnect: 'ifl' },
intel_gaudi: {
name: 'Intel Gaudi 2',
gpus: 8,
gpu: 'Intel Gaudi2 96G',
model: 'Phi-3.5-mini',
interconnect: 'pcie',
},
tesla_dojo: { name: 'Tesla Dojo', gpus: 64, gpu: 'A100 40G', model: 'Llama-3.1-70B', interconnect: 'custom' },
}
let currentDatacenter = 'none'
// Set sane defaults based on GPU capabilities
function setGPUDefaults(gpuKey) {
const config = gpuConfigs[gpuKey]
if (!config) return
const memGiB = config.memGiB
const memType = config.memType
// Set appropriate context length based on GPU memory
if (memGiB >= 384) {
// Ultra high-end GPUs (GB200): 2M context
maxTokens = 2000000
currentTokens = Math.min(currentTokens, maxTokens * 0.5) // Start at 50%
} else if (memGiB >= 192) {
// Top-tier GPUs (B200, MI300X): 1M context
maxTokens = 1000000
currentTokens = Math.min(currentTokens, maxTokens * 0.5) // Start at 50%
} else if (memGiB >= 128) {
// High-end GPUs (H200, MI250X, Max 1550): 750K context
maxTokens = 750000
currentTokens = Math.min(currentTokens, maxTokens * 0.4) // Start at 40%
} else if (memGiB >= 80) {
// Premium GPUs (H100, B100, A100 80G): 512K context
maxTokens = 512000
currentTokens = Math.min(currentTokens, maxTokens * 0.3) // Start at 30%
} else if (memGiB >= 40) {
// High-end GPUs (A100 40G, L40S): 256K context
maxTokens = 256000
currentTokens = Math.min(currentTokens, maxTokens * 0.25) // Start at 25%
} else if (memGiB >= 24) {
// Enthusiast GPUs (RTX 4090): 128K context
maxTokens = 128000
currentTokens = Math.min(currentTokens, maxTokens * 0.2) // Start at 20%
} else if (memGiB >= 16) {
// Mid-range GPUs (Tesla T4, Arc A770): 64K context
maxTokens = 64000
currentTokens = Math.min(currentTokens, maxTokens * 0.15) // Start at 15%
} else {
// Lower-end or specialized (Graphcore): 32K context
maxTokens = 32000
currentTokens = Math.min(currentTokens, maxTokens * 0.1) // Start at 10%
}
// Set appropriate batch size based on memory and type
if (memType.includes('SRAM')) {
// Specialized accelerators: smaller batches, optimized for throughput
batchSize = memGiB > 10 ? 2 : 1
} else if (memGiB >= 80) {
// High-memory GPUs: larger batches
batchSize = 8
} else if (memGiB >= 40) {
// Mid-high memory: moderate batches
batchSize = 4
} else if (memGiB >= 16) {
// Mid-range: conservative batches
batchSize = 2
} else {
// Low memory: single batch
batchSize = 1
}
// Enable appropriate optimizations based on GPU capabilities
if (memType.includes('SRAM')) {
// SRAM-based accelerators: all optimizations enabled by default
continuousBatching = true
pagedAttention = true
flashAttention = true
} else if (memType.includes('HBM')) {
// HBM-based GPUs: enable CB and Flash by default
continuousBatching = true
pagedAttention = false // Start with just CB + Flash
flashAttention = true
} else {
// GDDR-based GPUs: more conservative defaults
continuousBatching = memGiB >= 24 // Only for high-memory GDDR
pagedAttention = false
flashAttention = memGiB >= 16 // Flash for mid-range and up
}
// Set appropriate default model based on GPU memory capacity
// Qwen3-Next-80B is perfect for high-memory GPUs due to its efficiency
if (memGiB >= 80) {
// Premium GPUs (H100, B100, A100 80G+, B200, GB200): Qwen3-Next-80B
const qwen3NextIndex = models.findIndex((m) => m.name === 'Qwen3-Next-80B')
if (qwen3NextIndex !== -1) {
currentModelIndex = qwen3NextIndex
}
} else if (memGiB >= 40) {
// High-end GPUs (A100 40G, L40S): Llama-3.1-8B
const llama8bIndex = models.findIndex((m) => m.name === 'Llama-3.1-8B')
if (llama8bIndex !== -1) {
currentModelIndex = llama8bIndex
}
} else if (memGiB >= 24) {
// Enthusiast GPUs (RTX 4090): Phi-3.5-mini
const phi35Index = models.findIndex((m) => m.name === 'Phi-3.5-mini')
if (phi35Index !== -1) {
currentModelIndex = phi35Index
}
} else {
// Lower memory GPUs: Llama-3.2-1B
const llama1bIndex = models.findIndex((m) => m.name === 'Llama-3.2-1B')
if (llama1bIndex !== -1) {
currentModelIndex = llama1bIndex
}
}
// Reset animation to start position
if (currentTokens === 0) {
currentTokens = maxTokens * 0.01 // Start at 1% to show some progress
}
// Update UI to reflect new model selection
updateInfoPanel()
initWaves()
}
function getCurrentGPUMemGiB() {
const cfg = gpuConfigs[currentGPU]
return cfg ? cfg.memGiB : 80
}
// Update UI controls to reflect current state
function updateControlStates() {
// Update batch size control
const batchBtn = document.getElementById('batchControl')
if (batchBtn) {
batchBtn.textContent = `Batch: ${batchSize}`
}
// Update context length control
const contextBtn = document.getElementById('contextControl')
if (contextBtn) {
contextBtn.textContent = `Context: ${formatMemoryValue(maxTokens)}`
}
// Update optimization toggle states
const cbBtn = document.getElementById('cbToggle')
if (cbBtn) {
const span = cbBtn.querySelector('span')
if (span) {
span.textContent = `CB: ${continuousBatching ? 'ON' : 'OFF'}`
}
cbBtn.classList.toggle('enabled', continuousBatching)
}
const paBtn = document.getElementById('paToggle')
if (paBtn) {
const span = paBtn.querySelector('span')
if (span) {
span.textContent = `PA: ${pagedAttention ? 'ON' : 'OFF'}`
}
paBtn.classList.toggle('enabled', pagedAttention)
}
const faBtn = document.getElementById('faToggle')
if (faBtn) {
const span = faBtn.querySelector('span')
if (span) {
span.textContent = `FA: ${flashAttention ? 'ON' : 'OFF'}`
}
faBtn.classList.toggle('enabled', flashAttention)
}
}
// Format large numbers for display
function formatMemoryValue(value) {
if (value >= 1000000) {
return (value / 1000000).toFixed(value % 1000000 === 0 ? 0 : 1) + 'M'
} else if (value >= 1000) {
return (value / 1000).toFixed(value % 1000 === 0 ? 0 : 1) + 'K'
}
return value.toString()
}
// SOTA context length presets
const contextPresets = {
'4K': 4096, // Original GPT-3.5, older models
'8K': 8192, // GPT-4 base
'16K': 16384, // GPT-3.5 Turbo 16K
'32K': 32768, // GPT-4 32K, Claude Instant
'64K': 64000, // Current standard context
'100K': 100000, // Claude 2.1
'128K': 128000, // GPT-4 Turbo, GPT-4o, Llama 3
'200K': 200000, // Claude 3 Opus/Sonnet/Haiku
'256K': 256000, // High-end production context
'512K': 512000, // H100/A100 80GB default
'1M': 1000000, // Gemini 1.5 Pro (public), Claude 3.5 Sonnet
'2M': 2000000, // Gemini 1.5 Pro (developer preview)
'10M': 10000000, // Research frontier (Magic, experimental)
}
// Data type configurations
const dtypeConfigs = {
FP32: { bytes: 4, name: 'float32', color: '#ff6b6b' },
FP16: { bytes: 2, name: 'float16', color: '#00d4ff' },
BF16: { bytes: 2, name: 'bfloat16', color: '#00ff88' },
INT8: { bytes: 1, name: 'int8', color: '#ffaa00' },
INT4: { bytes: 0.5, name: 'int4', color: '#ff00ff' },
}
// Resize canvas
function resizeCanvas() {
if (isMobile) {
// On mobile, account for header and controls
canvas.width = window.innerWidth
canvas.height = window.innerHeight - 250 // Leave room for header and controls
} else {
canvas.width = window.innerWidth
canvas.height = window.innerHeight
}
}
// Calculate KV cache size (from LMCache logic)
function calculateKVCacheSize(model, tokens, dtype = null) {
const selectedDtype = dtype || currentDtype
const dtype_size = dtypeConfigs[selectedDtype] ? dtypeConfigs[selectedDtype].bytes : 2
let total_elements
if (model.special === 'deepseek') {
// DeepSeek uses KV-LoRA compression
total_elements = model.layers * tokens * (model.kv_lora_rank + model.qk_rope_head_dim)
} else if (model.architecture === 'qwen3-next') {
// Qwen3-Next uses hybrid attention (1/4 layers use traditional attention, rest use linear)
// Only 1/4 of layers have KV cache
const head_size = model.hidden_size / model.num_heads
const layers_with_kv = Math.floor(model.layers / 4)
total_elements = 2 * layers_with_kv * tokens * model.num_kv_heads * head_size
} else {
// Standard calculation (includes MOE models - expert count doesn't affect KV cache)
const head_size = model.hidden_size / model.num_heads
total_elements = 2 * model.layers * tokens * model.num_kv_heads * head_size
}
const total_bytes = total_elements * dtype_size
return total_bytes / (1024 * 1024 * 1024) // Convert to GiB
}
// Generate distinct colors for each sequence in continuous batching
function generateSequenceColors(batchSize) {
const colors = [
'#FFD700', // Gold - prompt tokens
'#00CED1', // Dark Turquoise - sequence 2
'#FF69B4', // Hot Pink - sequence 3
'#32CD32', // Lime Green - sequence 4
'#FF6347', // Tomato - sequence 5
'#BA55D3', // Medium Orchid - sequence 6
'#4169E1', // Royal Blue - sequence 7
'#FF8C00', // Dark Orange - sequence 8
'#20B2AA', // Light Sea Green - sequence 9
'#DC143C', // Crimson - sequence 10
'#9370DB', // Medium Purple - sequence 11
'#00FA9A', // Medium Spring Green - sequence 12
'#FFA500', // Orange - sequence 13
'#87CEEB', // Sky Blue - sequence 14
'#FF1493', // Deep Pink - sequence 15
'#ADFF2F', // Green Yellow - sequence 16
]
const result = []
for (let i = 0; i < batchSize; i++) {
result.push(colors[i % colors.length])
}
return result
}
// Generate deterministic sequence length ratios for continuous batching
function getSequenceLengthRatio(index, batchSize) {
// For visual clarity, use more balanced ratios that ensure each sequence is visible
if (batchSize <= 4) {
// Small batch sizes: use well-distributed ratios for clear visualization
const smallBatchPatterns = {
2: [0.3, 0.7], // 30% vs 70%
3: [0.2, 0.4, 0.6], // 20%, 40%, 60%
4: [0.15, 0.25, 0.35, 0.5], // 15%, 25%, 35%, 50%
}
const pattern = smallBatchPatterns[batchSize]
return pattern[index % pattern.length]
}
// For larger batch sizes, use a more varied but still visible pattern
const pattern = [0.1, 0.2, 0.3, 0.4, 0.15, 0.45, 0.25, 0.55, 0.35, 0.65, 0.05, 0.5, 0.18, 0.7, 0.12, 0.85]
return pattern[index % pattern.length]
}
// Calculate attention matrix memory that would be needed without Flash Attention
function calculateAttentionMatrixSize(sequenceLength, batchSize = 1, dtype = null) {
const config = dtypeConfigs[dtype || currentDtype]
const bytesPerElement = config.bytes
const model = models[currentModelIndex]
const currentGPUConfig = gpuConfigs[currentGPU]
const totalGPUMemoryGiB = getCurrentGPUMemGiB()
// Flash Attention saves intermediate attention computation memory, not the full O(n²) matrix
// Traditional attention still uses chunking for very long sequences, so baseline isn't full matrix
// Calculate realistic working memory for attention computation
// This represents intermediate attention scores that would be held during computation
const numHeads = model.heads || 32
// For shorter sequences (<32k), traditional attention might materialize more of the matrix
// For longer sequences, even traditional implementations use chunking
const effectiveSequenceLength = Math.min(sequenceLength, 65536) // Cap at 64k for realistic baseline
// Chunk size that traditional attention would process at once (realistic: 1k-4k tokens)
const chunkSize = Math.min(4096, effectiveSequenceLength)
// Memory for attention scores during computation: chunk_size × seq_len × heads × batches
const workingMemoryBytes = chunkSize * effectiveSequenceLength * numHeads * batchSize * bytesPerElement
// Flash Attention reduces this by computing in smaller tiles (typically 64×64 or 128×128)
const flashTileSize = currentGPUConfig ? currentGPUConfig.flashTileSize : 64
const traditionalChunkBytes = workingMemoryBytes
const flashTileBytes = flashTileSize * flashTileSize * numHeads * batchSize * bytesPerElement
// Savings = traditional working memory - flash tile memory
const savingsBytes = Math.max(0, traditionalChunkBytes - flashTileBytes)
const savingsGiB = savingsBytes / 1024 ** 3
// Cap savings at 50% of total GPU memory (can't save more than what's physically possible)
const maxSavingsGiB = totalGPUMemoryGiB * 0.5
const cappedSavingsGiB = Math.min(savingsGiB, maxSavingsGiB)
// For very short sequences, savings are minimal
if (sequenceLength < 2048) {
return cappedSavingsGiB * 0.1 // Minimal savings for short sequences
}
return cappedSavingsGiB
}
// Calculate total KV cache for batch with continuous batching support
function calculateBatchKVCache(model, currentTokens, dtype = null) {
// Continuous batching only makes sense with batch size > 1
// With batch size = 1, there's no difference between continuous and traditional
if (continuousBatching && batchSize > 1) {
// Calculate based on variable sequence lengths with deterministic ratios
let totalKV = 0
let totalSeqLength = 0
for (let i = 0; i < batchSize; i++) {
// Use deterministic ratio for consistent calculations
const ratio = getSequenceLengthRatio(i, batchSize)
const seqLen = Math.floor(currentTokens * ratio)
totalSeqLength += seqLen
totalKV += calculateKVCacheSize(model, seqLen, dtype)
}
// Store average for display purposes
if (!batchSequenceLengths.length || batchSequenceLengths.length !== batchSize) {
batchSequenceLengths = []
for (let i = 0; i < batchSize; i++) {
const ratio = getSequenceLengthRatio(i, batchSize)
batchSequenceLengths.push(Math.floor(currentTokens * ratio))
}
}
return totalKV
} else {
// Traditional batching: all sequences same length
// Also used when batch size = 1 (continuous batching doesn't apply)
batchSequenceLengths = [] // Clear any stored lengths
return calculateKVCacheSize(model, currentTokens, dtype) * batchSize
}
}
// Calculate model weights memory in GiB
function calculateWeightMemoryGiB(model, dtype = null) {
const selectedDtype = dtype || currentDtype
const bytesPerParam = dtypeConfigs[selectedDtype] ? dtypeConfigs[selectedDtype].bytes : 2
// For MOE models with active_params specified, use active params for inference memory
// Otherwise use total params
const paramsToUse = model.active_params || model.params || 0
// params is in Billions (e.g., 70 for 70B). Convert to number of parameters
const numParams = paramsToUse * 1e9
const totalBytes = numParams * bytesPerParam
return totalBytes / (1024 * 1024 * 1024) // GiB
}
// Calculate GPUs needed (H100 has 80GB memory)
function calculateGPUsNeeded(memoryGiB) {
const per = getCurrentGPUMemGiB()
return Math.ceil(memoryGiB / per)
}
// Format memory size
function formatMemory(gib) {
if (gib < 1) {
return `${(gib * 1024).toFixed(1)} MiB`
} else if (gib < 1000) {
return `${gib.toFixed(1)} GiB`
} else {
return `${(gib / 1024).toFixed(2)} TiB`
}
}
// Format number with commas
function formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
// Create memory block particle
class MemoryBlock {
constructor(x, y, size, color) {
this.x = x
this.y = y
this.size = size
this.color = color
this.opacity = 1
this.velocity = {
x: (Math.random() - 0.5) * 2,
y: -Math.random() * 3 - 1,
}
this.life = 1
this.rotation = Math.random() * Math.PI * 2
this.rotationSpeed = (Math.random() - 0.5) * 0.1
}
update() {
this.x += this.velocity.x
this.y += this.velocity.y
this.velocity.y += 0.05 // gravity
this.life -= 0.01
this.opacity = this.life
this.rotation += this.rotationSpeed
}
draw() {
ctx.save()
ctx.globalAlpha = this.opacity
ctx.translate(this.x, this.y)
ctx.rotate(this.rotation)
// Draw glowing block
const gradient = ctx.createRadialGradient(0, 0, 0, 0, 0, this.size)
gradient.addColorStop(0, this.color)
gradient.addColorStop(1, 'transparent')
ctx.fillStyle = gradient
ctx.fillRect(-this.size / 2, -this.size / 2, this.size, this.size)
// Draw border
ctx.strokeStyle = this.color
ctx.lineWidth = 2
ctx.strokeRect(-this.size / 2, -this.size / 2, this.size, this.size)
ctx.restore()
}
}
// Data flow particle class
class DataFlowParticle {
constructor(startX, startY, endX, endY, color, speed = 0.02) {
this.startX = startX
this.startY = startY
this.endX = endX
this.endY = endY
this.x = startX
this.y = startY
this.progress = 0
this.speed = speed
this.color = color
this.size = 3 + Math.random() * 3
this.life = 1
this.trail = []
this.maxTrailLength = 10
}
update() {
this.progress += this.speed
// Move along path
this.x = this.startX + (this.endX - this.startX) * this.progress
this.y = this.startY + (this.endY - this.startY) * this.progress
// Add to trail
this.trail.push({ x: this.x, y: this.y })
if (this.trail.length > this.maxTrailLength) {
this.trail.shift()
}
// Check if reached destination
if (this.progress >= 1) {
this.life = 0
}
}
draw() {
// Draw trail
ctx.strokeStyle = this.color + '33'
ctx.lineWidth = this.size * 0.5
ctx.beginPath()
this.trail.forEach((point, i) => {
if (i === 0) {