Spaces:
Running
Running
File size: 51,523 Bytes
38a76a8 5bf3c1c 38a76a8 |
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Autoregressive LLM Interactiveractive Lab</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.8/dist/chart.umd.min.js"></script>
<!-- Tailwind Configuration -->
<script>
tailwind.config = {
theme: {
extend: {
colors: {
primary: '#3b82f6',
secondary: '#8b5cf6',
attention: '#facc15',
token: '#06b6d4',
success: '#22c55e',
danger: '#ef4444',
dark1: '#0f172a',
dark2: '#1e293b',
dark3: '#334155',
dark4: '#475569',
},
fontFamily: {
sans: ['Inter', 'system-ui', 'sans-serif'],
mono: ['JetBrains Mono', 'monospace'],
},
}
}
}
</script>
<style type="text/tailwindcss">
@layer utilities {
.content-auto {
content-visibility: auto;
}
.bg-gradient-llm {
background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
}
.text-shadow {
text-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.grid-bg {
background-size: 60px 60px;
background-image:
linear-gradient(to right, rgba(255,255,255,0.05) 1px, transparent 1px),
linear-gradient(to bottom, rgba(255,255,255,0.05) 1px, transparent 1px);
}
.card-hover {
transition: all 0.3s ease;
}
.card-hover:hover {
transform: translateY(-5px);
box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.2);
}
}
</style>
</head>
<body class="bg-gradient-llm text-gray-100 min-h-screen font-sans overflow-x-hidden">
<!-- Navigation Bar -->
<nav class="bg-white text-dark1 shadow-md fixed top-0 left-0 right-0 z-50">
<div class="container mx-auto px-4 py-3 flex justify-between items-center">
<div class="flex items-center space-x-3">
<i class="fa fa-sitemap text-2xl text-primary"></i>
<h1 class="text-xl font-bold">Autoregressive LLM Lab</h1>
</div>
<div class="text-sm text-dark4 italic">Step-by-step token generation</div>
</div>
</nav>
<!-- Main Content -->
<main class="container mx-auto pt-20 px-4 pb-10 grid grid-cols-1 lg:grid-cols-12 gap-6">
<!-- Left Column -->
<section class="lg:col-span-3 space-y-6">
<!-- Concept Card -->
<div class="bg-dark2 rounded-xl shadow-lg p-5 card-hover">
<h2 class="text-xl font-bold mb-4 flex items-center">
<i class="fa fa-lightbulb-o text-attention mr-2"></i>
Core Concepts
</h2>
<p class="text-gray-300 mb-4">
Autoregressive language models generate text one token at a time,
where each new token depends on all previous tokens in the sequence.
</p>
<p class="text-gray-300">
This approach enables the model to capture long-range dependencies
and produce coherent, contextually relevant text.
</p>
</div>
<!-- Formula Card -->
<div class="bg-dark2 rounded-xl shadow-lg p-5 card-hover">
<h2 class="text-xl font-bold mb-4 flex items-center">
<i class="fa fa-calculator text-primary mr-2"></i>
Autoregressive Formula
</h2>
<div class="bg-dark3 rounded-lg p-4 my-4 formula-container relative overflow-hidden">
<div id="formula-animation" class="font-mono text-lg">
P(x) = Π<sub>t=1</sub><sup>T</sup> P(x<sub>t</sub> | x<sub><t</sub>)
</div>
<div class="absolute inset-0 bg-gradient-to-r from-dark3 via-transparent to-dark3 pointer-events-none hidden" id="formula-highlighter"></div>
</div>
<p class="text-sm text-gray-400">
The probability of a sequence is the product of the probabilities of each token
given all previous tokens in the sequence.
</p>
<button id="animate-formula" class="mt-3 text-sm bg-primary/20 hover:bg-primary/30 text-primary px-3 py-1 rounded transition-colors">
Animate Formula
</button>
</div>
<!-- Model Features -->
<div class="bg-dark2 rounded-xl shadow-lg p-5 card-hover">
<h2 class="text-xl font-bold mb-4 flex items-center">
<i class="fa fa-list-ul text-secondary mr-2"></i>
Model Features
</h2>
<ul class="space-y-2 text-gray-300">
<li class="flex items-start">
<i class="fa fa-check-circle text-success mt-1 mr-2"></i>
<span>Generates text sequentially, one token at a time</span>
</li>
<li class="flex items-start">
<i class="fa fa-check-circle text-success mt-1 mr-2"></i>
<span>Each prediction depends on all previous context</span>
</li>
<li class="flex items-start">
<i class="fa fa-check-circle text-success mt-1 mr-2"></i>
<span>Uses causal masking to prevent future information leakage</span>
</li>
<li class="flex items-start">
<i class="fa fa-check-circle text-success mt-1 mr-2"></i>
<span>Probabilistic output allows for creative variations</span>
</li>
<li class="flex items-start">
<i class="fa fa-check-circle text-success mt-1 mr-2"></i>
<span>Parameters control randomness and diversity</span>
</li>
</ul>
</div>
</section>
<!-- Middle Column -->
<section class="lg:col-span-6 space-y-6">
<!-- Visualization Canvas -->
<div class="bg-dark2 rounded-xl shadow-lg p-5 card-hover">
<h2 class="text-xl font-bold mb-4 flex items-center">
<i class="fa fa-area-chart text-token mr-2"></i>
Transformer Visualization
</h2>
<div class="relative">
<canvas id="llm-canvas" width="1000" height="600" class="w-full h-auto bg-dark1 grid-bg rounded-lg shadow-inner"></canvas>
<!-- Canvas Indicators -->
<div class="absolute top-3 left-3 bg-dark2/80 backdrop-blur-sm px-3 py-1 rounded text-sm font-mono">
<span>t = <span id="current-token-position">1</span></span>
</div>
<div class="absolute top-3 right-3 bg-dark2/80 backdrop-blur-sm px-3 py-1 rounded text-sm font-mono" id="confidence-indicator">
Confidence: <span class="text-success">0.85</span>
</div>
<div class="absolute bottom-3 left-3 bg-dark2/80 backdrop-blur-sm px-3 py-1 rounded text-xs font-mono max-w-xs" id="structure-path">
Path: Embedding → Attention → FFN → Softmax
</div>
<div class="absolute bottom-3 right-3 bg-dark2/80 backdrop-blur-sm px-3 py-1 rounded text-xs font-mono" id="dependency-indicator">
Depends only on previous tokens
</div>
</div>
</div>
<!-- Control Buttons -->
<div class="bg-dark2 rounded-xl shadow-lg p-5 card-hover">
<h2 class="text-xl font-bold mb-4 flex items-center">
<i class="fa fa-play-circle text-attention mr-2"></i>
Generation Controls
</h2>
<div class="flex flex-wrap gap-3 mb-6">
<button id="btn-pause" class="bg-dark3 hover:bg-dark4 text-white px-5 py-2 rounded-lg flex items-center transition-colors">
<i class="fa fa-pause mr-2"></i> Pause
</button>
<button id="btn-step" class="bg-primary hover:bg-primary/80 text-white px-5 py-2 rounded-lg flex items-center transition-colors">
<i class="fa fa-step-forward mr-2"></i> Step
</button>
<button id="btn-run" class="bg-success hover:bg-success/80 text-white px-5 py-2 rounded-lg flex items-center transition-colors">
<i class="fa fa-play mr-2"></i> Run
</button>
</div>
<!-- View Toggle -->
<div class="mb-6">
<h3 class="text-sm font-semibold text-gray-400 mb-2">VIEW MODE</h3>
<div class="flex gap-2">
<button class="view-toggle bg-primary text-white px-3 py-1 rounded text-sm transition-colors active" data-view="full">Full Pipeline</button>
<button class="view-toggle bg-dark3 hover:bg-dark4 px-3 py-1 rounded text-sm transition-colors" data-view="embedding">Embedding</button>
<button class="view-toggle bg-dark3 hover:bg-dark4 px-3 py-1 rounded text-sm transition-colors" data-view="attention">Attention</button>
</div>
</div>
<!-- Sliders -->
<div class="space-y-5">
<!-- Number of Layers -->
<div>
<div class="flex justify-between mb-1">
<label for="num-layers" class="text-sm font-medium">Number of Layers</label>
<span id="num-layers-value" class="text-sm font-mono">8</span>
</div>
<input type="range" id="num-layers" min="2" max="24" value="8"
class="w-full h-2 bg-dark3 rounded-lg appearance-none cursor-pointer accent-primary">
</div>
<!-- Number of Attention Heads -->
<div>
<div class="flex justify-between mb-1">
<label for="num-heads" class="text-sm font-medium">Attention Heads</label>
<span id="num-heads-value" class="text-sm font-mono">12</span>
</div>
<input type="range" id="num-heads" min="1" max="32" value="12"
class="w-full h-2 bg-dark3 rounded-lg appearance-none cursor-pointer accent-primary">
</div>
<!-- Temperature -->
<div>
<div class="flex justify-between mb-1">
<label for="temperature" class="text-sm font-medium">Temperature</label>
<span id="temperature-value" class="text-sm font-mono">1.0</span>
</div>
<input type="range" id="temperature" min="0.1" max="2.0" step="0.1" value="1.0"
class="w-full h-2 bg-dark3 rounded-lg appearance-none cursor-pointer accent-primary">
</div>
<!-- Top-k -->
<div>
<div class="flex justify-between mb-1">
<label for="top-k" class="text-sm font-medium">Top-k</label>
<span id="top-k-value" class="text-sm font-mono">50</span>
</div>
<input type="range" id="top-k" min="1" max="100" value="50"
class="w-full h-2 bg-dark3 rounded-lg appearance-none cursor-pointer accent-primary">
</div>
<!-- Top-p -->
<div>
<div class="flex justify-between mb-1">
<label for="top-p" class="text-sm font-medium">Top-p</label>
<span id="top-p-value" class="text-sm font-mono">0.9</span>
</div>
<input type="range" id="top-p" min="0.1" max="1.0" step="0.1" value="0.9"
class="w-full h-2 bg-dark3 rounded-lg appearance-none cursor-pointer accent-primary">
</div>
<!-- Deterministic Toggle -->
<div class="flex items-center justify-between pt-2">
<label class="text-sm font-medium" for="deterministic">Deterministic Mode</label>
<label class="relative inline-flex items-center cursor-pointer">
<input type="checkbox" id="deterministic" class="sr-only peer">
<div class="w-11 h-6 bg-dark3 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-primary"></div>
</label>
</div>
</div>
</div>
</section>
<!-- Right Column -->
<section class="lg:col-span-3 space-y-6">
<!-- Model Architecture -->
<div class="bg-dark2 rounded-xl shadow-lg p-5 card-hover">
<h2 class="text-xl font-bold mb-4 flex items-center">
<i class="fa fa-cubes text-secondary mr-2"></i>
Model Architecture
</h2>
<div class="space-y-4">
<div class="bg-dark3/50 p-3 rounded-lg border-l-4 border-token">
<h3 class="font-bold text-sm mb-1">Embedding Layer</h3>
<p class="text-xs text-gray-300">Maps input tokens to vector space</p>
</div>
<div class="bg-dark3/50 p-3 rounded-lg border-l-4 border-attention">
<h3 class="font-bold text-sm mb-1">Positional Encoding</h3>
<p class="text-xs text-gray-300">Adds positional information using sine waves</p>
</div>
<div class="bg-dark3/50 p-3 rounded-lg border-l-4 border-primary">
<h3 class="font-bold text-sm mb-1">Transformer Blocks</h3>
<p class="text-xs text-gray-300">Multi-layer stack with attention mechanisms</p>
</div>
<div class="bg-dark3/50 p-3 rounded-lg border-l-4 border-secondary">
<h3 class="font-bold text-sm mb-1">Multi-Head Attention</h3>
<p class="text-xs text-gray-300">Captures relationships between tokens</p>
</div>
<div class="bg-dark3/50 p-3 rounded-lg border-l-4 border-primary">
<h3 class="font-bold text-sm mb-1">Feed Forward Network</h3>
<p class="text-xs text-gray-300">Processes each position independently</p>
</div>
<div class="bg-dark3/50 p-3 rounded-lg border-l-4 border-success">
<h3 class="font-bold text-sm mb-1">Output Layer</h3>
<p class="text-xs text-gray-300">Produces probability distribution over tokens</p>
</div>
</div>
</div>
<!-- Token Sequence -->
<div class="bg-dark2 rounded-xl shadow-lg p-5 card-hover">
<h2 class="text-xl font-bold mb-4 flex items-center">
<i class="fa fa-code text-token mr-2"></i>
Generated Tokens
</h2>
<div class="bg-dark3 rounded-lg p-3 min-h-[120px] mb-3">
<div id="token-sequence" class="flex flex-wrap gap-2">
<span class="bg-token/20 text-token px-2 py-1 rounded text-sm">Once</span>
<span class="bg-token/20 text-token px-2 py-1 rounded text-sm">upon</span>
<span class="bg-token/20 text-token px-2 py-1 rounded text-sm">a</span>
<span class="bg-dark4/50 text-gray-400 px-2 py-1 rounded text-sm animate-pulse">...</span>
</div>
</div>
<div class="text-sm text-gray-400">
<p>Input prompt: <span class="text-gray-300 font-mono">"Once upon a"</span></p>
</div>
</div>
<!-- Probability Distribution -->
<div class="bg-dark2 rounded-xl shadow-lg p-5 card-hover">
<h2 class="text-xl font-bold mb-4 flex items-center">
<i class="fa fa-bar-chart text-success mr-2"></i>
Token Probabilities
</h2>
<div class="h-[250px] relative">
<canvas id="probability-chart"></canvas>
</div>
<div class="mt-3 text-xs text-gray-400">
<p>Showing top 5 probable next tokens</p>
</div>
</div>
<!-- Data Information -->
<div class="bg-dark2 rounded-xl shadow-lg p-5 card-hover">
<h2 class="text-xl font-bold mb-4 flex items-center">
<i class="fa fa-database text-primary mr-2"></i>
Tensor Shapes
</h2>
<div class="space-y-3 text-sm">
<div class="flex justify-between">
<span class="text-gray-400">Input tokens:</span>
<span class="font-mono">[1, 3]</span>
</div>
<div class="flex justify-between">
<span class="text-gray-400">Embedding:</span>
<span class="font-mono">[1, 3, 4096]</span>
</div>
<div class="flex justify-between">
<span class="text-gray-400">Attention weights:</span>
<span class="font-mono">[12, 1, 3, 3]</span>
</div>
<div class="flex justify-between">
<span class="text-gray-400">FFN output:</span>
<span class="font-mono">[1, 3, 4096]</span>
</div>
<div class="flex justify-between">
<span class="text-gray-400">Logits:</span>
<span class="font-mono">[1, 3, 50257]</span>
</div>
</div>
</div>
</section>
</main>
<script>
// DOM Elements
const canvas = document.getElementById('llm-canvas');
const ctx = canvas.getContext('2d');
const probabilityChartCtx = document.getElementById('probability-chart').getContext('2d');
// Control Elements
const btnPause = document.getElementById('btn-pause');
const btnStep = document.getElementById('btn-step');
const btnRun = document.getElementById('btn-run');
const viewToggles = document.querySelectorAll('.view-toggle');
const numLayersSlider = document.getElementById('num-layers');
const numHeadsSlider = document.getElementById('num-heads');
const temperatureSlider = document.getElementById('temperature');
const topKSlider = document.getElementById('top-k');
const topPSlider = document.getElementById('top-p');
const deterministicToggle = document.getElementById('deterministic');
// Display Elements
const numLayersValue = document.getElementById('num-layers-value');
const numHeadsValue = document.getElementById('num-heads-value');
const temperatureValue = document.getElementById('temperature-value');
const topKValue = document.getElementById('top-k-value');
const topPValue = document.getElementById('top-p-value');
const currentTokenPosition = document.getElementById('current-token-position');
const confidenceIndicator = document.getElementById('confidence-indicator');
const tokenSequence = document.getElementById('token-sequence');
const formulaHighlighter = document.getElementById('formula-highlighter');
const animateFormulaBtn = document.getElementById('animate-formula');
// State
let state = {
viewMode: 'full',
numLayers: 8,
numHeads: 12,
temperature: 1.0,
topK: 50,
topP: 0.9,
deterministic: false,
currentStep: 1,
isRunning: false,
animationId: null,
tokens: ['Once', 'upon', 'a'],
nextTokens: [
{ token: 'time', probability: 0.85 },
{ token: 'day', probability: 0.07 },
{ token: 'moment', probability: 0.03 },
{ token: 'night', probability: 0.02 },
{ token: 'week', probability: 0.01 }
]
};
// Initialize probability chart
let probabilityChart = new Chart(probabilityChartCtx, {
type: 'bar',
data: {
labels: state.nextTokens.map(t => t.token),
datasets: [{
label: 'Probability',
data: state.nextTokens.map(t => t.probability),
backgroundColor: [
'rgba(34, 197, 94, 0.7)', // Green for highest probability
'rgba(59, 130, 246, 0.7)',
'rgba(139, 92, 246, 0.7)',
'rgba(249, 115, 22, 0.7)',
'rgba(239, 68, 68, 0.7)'
],
borderColor: [
'rgba(34, 197, 94, 1)',
'rgba(59, 130, 246, 1)',
'rgba(139, 92, 246, 1)',
'rgba(249, 115, 22, 1)',
'rgba(239, 68, 68, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
max: 1.0,
grid: {
color: 'rgba(255, 255, 255, 0.1)'
},
ticks: {
color: 'rgba(255, 255, 255, 0.7)'
}
},
x: {
grid: {
color: 'rgba(255, 255, 255, 0.1)'
},
ticks: {
color: 'rgba(255, 255, 255, 0.7)'
}
}
},
plugins: {
legend: {
display: false
}
},
animation: {
duration: 500
}
}
});
// Update chart with new data
function updateProbabilityChart() {
probabilityChart.data.labels = state.nextTokens.map(t => t.token);
probabilityChart.data.datasets[0].data = state.nextTokens.map(t => t.probability);
probabilityChart.update();
}
// Canvas dimensions and center point
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
// Animation loop for continuous visual updates
function animate() {
// Redraw the visualization with updated animations
drawLLM();
// Continue the animation loop
if (state.animationId) {
state.animationId = requestAnimationFrame(animate);
}
}
// Start the animation loop
function startAnimation() {
if (!state.animationId) {
state.animationId = requestAnimationFrame(animate);
}
}
// Stop the animation loop
function stopAnimation() {
if (state.animationId) {
cancelAnimationFrame(state.animationId);
state.animationId = null;
}
}
// Draw the entire LLM visualization
function drawLLM() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw based on view mode
if (state.viewMode === 'full' || state.viewMode === 'embedding') {
drawEmbeddingLayer();
drawPositionalEncoding();
}
if (state.viewMode === 'full' || state.viewMode === 'attention') {
drawTransformerLayers();
drawAttentionConnections();
}
if (state.viewMode === 'full') {
drawFeedForwardNetworks();
drawOutputLayer();
}
// Draw input tokens
drawInputTokens();
}
// Draw input tokens at the bottom left
function drawInputTokens() {
const startX = 100;
const startY = canvas.height - 100;
const tokenWidth = 80;
const tokenHeight = 40;
state.tokens.forEach((token, index) => {
const x = startX + index * (tokenWidth + 20);
// Draw token background with subtle animation
const pulse = 0.02 + Math.sin(Date.now() / 1000 + index) * 0.01;
ctx.fillStyle = `rgba(6, 182, 212, ${0.2 + pulse})`;
ctx.fillRect(x, startY, tokenWidth, tokenHeight);
// Draw token border
ctx.strokeStyle = 'rgba(6, 182, 212, 1)';
ctx.strokeRect(x, startY, tokenWidth, tokenHeight);
// Draw token text
ctx.fillStyle = 'rgba(6, 182, 212, 1)';
ctx.font = '14px JetBrains Mono, monospace';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(token, x + tokenWidth / 2, startY + tokenHeight / 2);
// Draw connection to embedding layer
if (state.viewMode === 'full' || state.viewMode === 'embedding') {
ctx.beginPath();
ctx.moveTo(x + tokenWidth / 2, startY);
ctx.lineTo(x + tokenWidth / 2, startY - 60);
ctx.strokeStyle = 'rgba(6, 182, 212, 0.6)';
ctx.lineWidth = 2;
ctx.stroke();
}
});
}
// Draw embedding layer
function drawEmbeddingLayer() {
const startX = 100;
const startY = canvas.height - 200;
const embeddingWidth = 80;
const embeddingHeight = 30;
const vectorCount = 8;
// Draw embedding layer label
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
ctx.font = '16px Inter, sans-serif';
ctx.textAlign = 'left';
ctx.fillText('Embedding Layer', startX, startY - 20);
state.tokens.forEach((_, tokenIndex) => {
const tokenX = startX + tokenIndex * (embeddingWidth + 20);
// Draw vector bars for each token embedding with animation
for (let i = 0; i < vectorCount; i++) {
const x = tokenX + (i * embeddingWidth / vectorCount);
const height = 30 + Math.sin(Date.now() / 500 + tokenIndex * 0.5 + i) * 10;
ctx.fillStyle = `rgba(59, 130, 246, ${0.5 + Math.sin(Date.now() / 1000 + i) * 0.2})`;
ctx.fillRect(x, startY + (30 - height), embeddingWidth / vectorCount - 1, height);
}
});
}
// Draw positional encoding as waves over embeddings
function drawPositionalEncoding() {
const startX = 100;
const startY = canvas.height - 200;
const embeddingWidth = 80;
// Draw positional encoding label
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
ctx.font = '16px Inter, sans-serif';
ctx.textAlign = 'left';
ctx.fillText('Positional Encoding', startX, startY - 45);
state.tokens.forEach((_, tokenIndex) => {
const tokenX = startX + tokenIndex * (embeddingWidth + 20);
// Draw wave for positional encoding with animation
ctx.beginPath();
ctx.strokeStyle = 'rgba(250, 204, 21, 0.6)';
ctx.lineWidth = 2;
for (let x = 0; x <= embeddingWidth; x++) {
const y = Math.sin((x / embeddingWidth) * Math.PI * 2 + tokenIndex * 0.5 + Date.now() / 1000) * 15;
if (x === 0) {
ctx.moveTo(tokenX + x, startY - 10 + y);
} else {
ctx.lineTo(tokenX + x, startY - 10 + y);
}
}
ctx.stroke();
});
}
// Draw transformer layers with attention heads
function drawTransformerLayers() {
const layerWidth = 800;
const layerHeight = 50;
const startX = centerX - layerWidth / 2;
const startY = 150;
const layerSpacing = 10;
// Draw transformer layers label
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
ctx.font = '16px Inter, sans-serif';
ctx.textAlign = 'left';
ctx.fillText('Transformer Layers', startX, startY - 20);
for (let i = 0; i < state.numLayers; i++) {
const y = startY + i * (layerHeight + layerSpacing);
// Create gradient for layer background
const gradient = ctx.createLinearGradient(startX, y, startX + layerWidth, y);
gradient.addColorStop(0, 'rgba(51, 65, 85, 0.8)');
gradient.addColorStop(1, 'rgba(71, 85, 105, 0.8)');
// Draw layer background
ctx.fillStyle = gradient;
ctx.fillRect(startX, y, layerWidth, layerHeight);
// Draw layer border with subtle animation
const borderOpacity = 0.3 + Math.sin(Date.now() / 2000 + i) * 0.2;
ctx.strokeStyle = `rgba(59, 130, 246, ${borderOpacity})`;
ctx.strokeRect(startX, y, layerWidth, layerHeight);
// Draw layer label
ctx.fillStyle = 'rgba(255, 255, 255, 0.7)';
ctx.font = '12px JetBrains Mono, monospace';
ctx.textAlign = 'left';
ctx.fillText(`Layer ${i + 1}`, startX + 10, y + 30);
// Draw attention heads
drawAttentionHeads(startX, y, layerWidth, layerHeight, i);
}
}
// Draw attention heads within a transformer layer
function drawAttentionHeads(layerX, layerY, layerWidth, layerHeight, layerIndex) {
const headRadius = 8;
const headsPerRow = Math.ceil(Math.sqrt(state.numHeads));
const headSpacing = 30;
const startHeadX = layerX + layerWidth - 10 - (headsPerRow * headSpacing);
const startHeadY = layerY + (layerHeight / 2) - (Math.ceil(state.numHeads / headsPerRow) * headSpacing / 2);
for (let i = 0; i < state.numHeads; i++) {
const row = Math.floor(i / headsPerRow);
const col = i % headsPerRow;
const x = startHeadX + col * headSpacing;
const y = startHeadY + row * headSpacing;
// Draw attention head
ctx.beginPath();
ctx.arc(x, y, headRadius, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(250, 204, 21, 0.3)';
ctx.fill();
ctx.strokeStyle = 'rgba(250, 204, 21, 0.8)';
ctx.lineWidth = 1;
ctx.stroke();
// Add pulsing animation
const pulse = 0.5 + Math.sin(Date.now() / 500 + i * 0.5 + layerIndex) * 0.5;
ctx.beginPath();
ctx.arc(x, y, headRadius * (1 + pulse * 0.2), 0, Math.PI * 2);
ctx.strokeStyle = `rgba(250, 204, 21, ${0.2 + pulse * 0.2})`;
ctx.stroke();
}
}
// Draw attention connections between tokens
function drawAttentionConnections() {
const tokenStartX = 100;
const tokenStartY = canvas.height - 100;
const tokenWidth = 80;
const layerStartX = centerX - 400;
const layerStartY = 150;
// Draw attention connections with varying opacity based on "weight"
for (let i = 0; i < state.tokens.length; i++) {
const fromX = tokenStartX + i * (tokenWidth + 20) + tokenWidth / 2;
const fromY = tokenStartY - 60;
for (let j = 0; j < state.tokens.length; j++) {
// Apply causal mask - only connections to previous or current tokens
if (j > i) continue;
const toX = layerStartX + 50 + j * 100;
const toY = layerStartY + 25;
// Create gradient for the connection
const gradient = ctx.createLinearGradient(fromX, fromY, toX, toY);
gradient.addColorStop(0, 'rgba(250, 204, 21, 0.1)');
gradient.addColorStop(1, 'rgba(250, 204, 21, 0.4)');
// Draw connection line with animation
const weight = 0.3 + Math.sin(Date.now() / 1000 + i * 0.5 + j) * 0.2;
ctx.beginPath();
ctx.moveTo(fromX, fromY);
// Create a curve for the connection
const controlX1 = fromX + (toX - fromX) / 3;
const controlY1 = fromY - 50;
const controlX2 = toX - (toX - fromX) / 3;
const controlY2 = toY - 50;
ctx.bezierCurveTo(controlX1, controlY1, controlX2, controlY2, toX, toY);
ctx.strokeStyle = gradient;
ctx.lineWidth = 1 + weight * 2;
ctx.stroke();
}
}
}
// Draw feed forward networks
function drawFeedForwardNetworks() {
const layerWidth = 800;
const layerHeight = 50;
const startX = centerX - layerWidth / 2;
const startY = 150;
const layerSpacing = 10;
for (let i = 0; i < state.numLayers; i++) {
const y = startY + i * (layerHeight + layerSpacing);
// Draw FFN indicators as expanding/contracting bars
const ffnStartX = startX + 200;
const ffnWidth = 300;
const barCount = 10;
for (let b = 0; b < barCount; b++) {
const barWidth = ffnWidth / barCount - 1;
const x = ffnStartX + b * (ffnWidth / barCount);
// Animation for expanding/contracting
const animationFactor = 0.5 + Math.sin(Date.now() / 800 + i * 0.3 + b * 0.5) * 0.5;
const height = 10 + animationFactor * 20;
ctx.fillStyle = `rgba(139, 92, 246, ${0.4 + animationFactor * 0.3})`;
ctx.fillRect(x, y + (layerHeight - height) / 2, barWidth, height);
}
// Draw FFN label
ctx.fillStyle = 'rgba(255, 255, 255, 0.7)';
ctx.font = '12px JetBrains Mono, monospace';
ctx.textAlign = 'left';
ctx.fillText('FFN', ffnStartX, y + layerHeight + 15);
}
}
// Draw output layer with softmax distribution
function drawOutputLayer() {
const outputWidth = 300;
const outputHeight = 100;
const startX = canvas.width - 350;
const startY = centerY - 50;
// Draw output layer background
ctx.fillStyle = 'rgba(33, 41, 55, 0.8)';
ctx.fillRect(startX, startY, outputWidth, outputHeight);
// Draw output layer border
ctx.strokeStyle = 'rgba(34, 197, 94, 0.7)';
ctx.strokeRect(startX, startY, outputWidth, outputHeight);
// Draw output layer label
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
ctx.font = '16px Inter, sans-serif';
ctx.textAlign = 'left';
ctx.fillText('Output Layer (Softmax)', startX, startY - 20);
// Draw probability bars for top tokens with animation
const barWidth = outputWidth / state.nextTokens.length - 10;
const maxBarHeight = outputHeight - 20;
state.nextTokens.forEach((token, index) => {
// Add animation factor
const animationFactor = 1 + Math.sin(Date.now() / 1500 + index) * 0.05;
const x = startX + 10 + index * (barWidth + 10);
const barHeight = token.probability * maxBarHeight * animationFactor;
// Color based on probability
let color;
if (index === 0) {
color = 'rgba(34, 197, 94, 0.8)'; // Green for highest
} else if (index === 1) {
color = 'rgba(59, 130, 246, 0.8)'; // Blue
} else if (index === 2) {
color = 'rgba(139, 92, 246, 0.8)'; // Purple
} else if (index === 3) {
color = 'rgba(249, 115, 22, 0.8)'; // Orange
} else {
color = 'rgba(239, 68, 68, 0.8)'; // Red
}
// Draw bar
ctx.fillStyle = color;
ctx.fillRect(x, startY + outputHeight - 10 - barHeight, barWidth, barHeight);
// Draw token label
ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
ctx.font = '10px JetBrains Mono, monospace';
ctx.textAlign = 'center';
ctx.fillText(token.token, x + barWidth / 2, startY + outputHeight - 3);
});
}
// Generate next token based on current parameters
function generateNextToken() {
// In a real implementation, this would use the actual model
// Here we simulate based on current parameters
// Sort tokens by probability
let sortedTokens = [...state.nextTokens].sort((a, b) => b.probability - a.probability);
// Apply top-k filtering
if (state.topK < sortedTokens.length) {
sortedTokens = sortedTokens.slice(0, state.topK);
}
// Apply top-p filtering
let cumulativeProb = 0;
const filteredTokens = [];
for (const token of sortedTokens) {
cumulativeProb += token.probability;
filteredTokens.push(token);
if (cumulativeProb >= state.topP) break;
}
// Apply temperature scaling
const scaledProbs = filteredTokens.map(t => {
return {
...t,
probability: Math.exp(Math.log(t.probability) / state.temperature)
};
});
// Normalize probabilities
const sum = scaledProbs.reduce((acc, t) => acc + t.probability, 0);
const normalizedProbs = scaledProbs.map(t => ({
...t,
probability: t.probability / sum
}));
// Select token (deterministic or stochastic)
let selectedToken;
if (state.deterministic) {
// Pick most probable token
selectedToken = normalizedProbs[0];
} else {
// Stochastic sampling based on probabilities
let random = Math.random();
let cumulative = 0;
for (const token of normalizedProbs) {
cumulative += token.probability;
if (random <= cumulative) {
selectedToken = token;
break;
}
}
}
// Add selected token to sequence with animation
addTokenWithAnimation(selectedToken.token);
// Update confidence indicator
const confidenceEl = confidenceIndicator.querySelector('span');
confidenceEl.textContent = selectedToken.probability.toFixed(2);
if (selectedToken.probability > 0.7) {
confidenceEl.className = 'text-success';
} else if (selectedToken.probability < 0.3) {
confidenceEl.className = 'text-danger';
} else {
confidenceEl.className = 'text-yellow-500';
}
// Generate new next tokens for demonstration
generateNewNextTokens(selectedToken.token);
// Update current step
state.currentStep++;
currentTokenPosition.textContent = state.currentStep;
// Update visualizations
updateProbabilityChart();
}
// Add token with slide-in animation
function addTokenWithAnimation(token) {
// Add to state
state.tokens.push(token);
// Create element with animation
const tokenEl = document.createElement('span');
tokenEl.className = 'bg-token/20 text-token px-2 py-1 rounded text-sm transition-all transform translate-x-[-20px] opacity-0';
tokenEl.textContent = token;
tokenSequence.appendChild(tokenEl);
// Remove loading indicator if present
const loadingIndicator = tokenSequence.querySelector('.animate-pulse');
if (loadingIndicator) {
loadingIndicator.remove();
}
// Trigger animation
setTimeout(() => {
tokenEl.classList.remove('translate-x-[-20px]', 'opacity-0');
tokenEl.classList.add('animate-pulse');
// Remove pulse after animation
setTimeout(() => {
tokenEl.classList.remove('animate-pulse');
}, 1000);
}, 50);
// Scroll to end
tokenSequence.scrollLeft = tokenSequence.scrollWidth;
}
// Generate new next tokens for demonstration
function generateNewNextTokens(previousToken) {
// In a real implementation, this would come from the model
// Here we generate plausible next tokens based on the previous one
const tokenOptions = {
'Once': ['upon', 'a', 'the', 'this', 'there'],
'upon': ['a', 'the', 'one', 'two', 'three'],
'a': ['time', 'day', 'moment', 'night', 'week'],
'time': ['long', 'ago', 'passed', 'came', 'when'],
'long': ['ago', 'time', 'story', 'journey', 'distance'],
'ago': ['there', 'in', 'on', 'during', 'before'],
'there': ['lived', 'was', 'came', 'stood', 'appeared'],
'lived': ['a', 'the', 'man', 'woman', 'king'],
'man': ['who', 'that', 'with', 'from', 'named'],
'who': ['lived', 'had', 'was', 'wanted', 'dreamed'],
'lived': ['in', 'by', 'near', 'with', 'without'],
'in': ['a', 'the', 'this', 'that', 'another'],
'kingdom': ['far', 'near', 'great', 'small', 'mysterious'],
'far': ['away', 'from', 'beyond', 'north', 'south'],
'away': ['from', 'in', 'with', 'to', 'toward']
};
// Get possible next tokens or use default
const possibleTokens = tokenOptions[previousToken] || ['and', 'but', 'so', 'then', 'thus'];
// Generate probabilities that sum to 1
let probabilities = [];
let total = 0;
// Create some random probabilities
for (let i = 0; i < 5; i++) {
const p = Math.random();
probabilities.push(p);
total += p;
}
// Normalize
probabilities = probabilities.map(p => p / total);
// Sort to have highest probability first
probabilities.sort((a, b) => b - a);
// Create new next tokens array
state.nextTokens = possibleTokens.map((token, index) => ({
token,
probability: probabilities[index]
}));
}
// Animate the formula explanation
function animateFormula() {
let position = 0;
const duration = 3000; // 3 seconds
const startTime = Date.now();
formulaHighlighter.classList.remove('hidden');
function animate() {
const elapsed = Date.now() - startTime;
const progress = Math.min(elapsed / duration, 1);
// Move highlighter from left to right
position = progress * 100;
formulaHighlighter.style.background = `linear-gradient(to right, transparent ${position - 10}%, rgba(59, 130, 246, 0.2) ${position - 5}%, rgba(59, 130, 246, 0.2) ${position + 5}%, transparent ${position + 10}%)`;
if (progress < 1) {
requestAnimationFrame(animate);
} else {
setTimeout(() => {
formulaHighlighter.classList.add('hidden');
}, 500);
}
}
animate();
}
// Animation loop for continuous running
function generationLoop() {
if (!state.isRunning) return;
// Generate a new token every 2 seconds
setTimeout(() => {
generateNextToken();
if (state.isRunning) {
requestAnimationFrame(generationLoop);
}
}, 2000);
}
// Event Listeners
btnPause.addEventListener('click', () => {
state.isRunning = false;
});
btnStep.addEventListener('click', () => {
state.isRunning = false;
generateNextToken();
});
btnRun.addEventListener('click', () => {
if (!state.isRunning) {
state.isRunning = true;
generationLoop();
}
});
viewToggles.forEach(toggle => {
toggle.addEventListener('click', () => {
// Update active state
viewToggles.forEach(t => t.classList.remove('active', 'bg-primary', 'text-white'));
viewToggles.forEach(t => t.classList.add('bg-dark3'));
toggle.classList.add('active', 'bg-primary', 'text-white');
toggle.classList.remove('bg-dark3');
// Update view mode
state.viewMode = toggle.dataset.view;
});
});
numLayersSlider.addEventListener('input', () => {
state.numLayers = parseInt(numLayersSlider.value);
numLayersValue.textContent = state.numLayers;
});
numHeadsSlider.addEventListener('input', () => {
state.numHeads = parseInt(numHeadsSlider.value);
numHeadsValue.textContent = state.numHeads;
});
temperatureSlider.addEventListener('input', () => {
state.temperature = parseFloat(temperatureSlider.value);
temperatureValue.textContent = state.temperature;
});
topKSlider.addEventListener('input', () => {
state.topK = parseInt(topKSlider.value);
topKValue.textContent = state.topK;
});
topPSlider.addEventListener('input', () => {
state.topP = parseFloat(topPSlider.value);
topPValue.textContent = state.topP;
});
deterministicToggle.addEventListener('change', () => {
state.deterministic = deterministicToggle.checked;
});
animateFormulaBtn.addEventListener('click', animateFormula);
// Initial setup
window.addEventListener('load', () => {
// Start the continuous animation loop for visual elements
startAnimation();
updateProbabilityChart();
});
// Cleanup on window unload
window.addEventListener('unload', () => {
stopAnimation();
});
</script>
</body>
</html>
|