Spaces:
Sleeping
Sleeping
File size: 12,605 Bytes
e1f91c2 | 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 | (function () {
const STEP_KEY = 'transformer_tutorial_step';
const DONE_KEY = 'transformer_tutorial_done';
const FOOTER_NORMAL =
'<br><br><i>Click <b>Continue</b> to close this window and try it yourself. ' +
'When you are ready, click <b>Continue Tutorial</b> in the top right to move on ' +
'β or use <b>Back</b> to revisit the previous step.</i>';
const FOOTER_LAST =
'<br><br><i>Click <b>Finish</b> to close the tutorial β you have seen the whole ' +
'pipeline. <b>Back</b> still works if you want to revisit anything.</i>';
let currentStep = 0;
let driverObj = null;
function safeGet(key) {
try { return localStorage.getItem(key); } catch (e) { return null; }
}
function safeSet(key, value) {
try { localStorage.setItem(key, value); } catch (e) { /* ignore */ }
}
function safeRemove(key) {
try { localStorage.removeItem(key); } catch (e) { /* ignore */ }
}
function buildSteps() {
const raw = [
{
element: '#model-dropdown',
title: 'Step 1 of 12 β Pick a model',
body:
'Everything starts here. Different transformer models have different ' +
'sizes, training data, and quirks, so what you see downstream depends on ' +
'which one you load.<br><br>' +
'<b>Recommended:</b> GPT-2 (124M). It is small enough to run quickly on ' +
'any machine and well-studied, so the attention patterns you will see ' +
'have known interpretations.'
},
{
element: '#prompt-section',
title: 'Step 2 of 12 β Give it something to predict',
body:
'The prompt is what the model tries to continue. The four buttons above ' +
'load prompts chosen to activate specific, studyable behaviors.<br><br>' +
'<b>Recommended:</b> click <b>"Understand repetition"</b>. Its prompt ' +
'("The cat sat on the mat. The cat sat on the") triggers <i>induction ' +
'heads</i> β attention detectors that complete repeated patterns. We will ' +
'come back to those in the ablation step.'
},
{
element: '#max-new-tokens-slider',
title: 'Step 3 of 12 β How many words to generate',
body:
'This controls how far the model extends the prompt. One word is enough ' +
'to see the whole pipeline in action β every extra word makes the ' +
'visualizations busier without adding new concepts.<br><br>' +
'<b>Leave this at 1</b> for your first run.'
},
{
element: '#beam-width-slider',
title: 'Step 4 of 12 β Explore alternative completions',
body:
'Language models do not pick just one next word β they assign ' +
'probabilities to every word in the vocabulary. <i>Beam search</i> keeps ' +
'the top N candidates so you can compare them side-by-side.<br><br>' +
'Bumping this to 3 or 5 is a great way to see where the model is ' +
'confident vs. uncertain.'
},
{
element: '#generate-btn',
title: 'Step 5 of 12 β Run the model',
body:
'Clicking Analyze runs the prompt through every layer and records the ' +
'internal activations. This is what powers everything below β nothing in ' +
'the pipeline or ablation tools is available until this runs.<br><br>' +
'<b>Close this window, click Analyze, and wait for the pipeline to appear</b> ' +
'before moving on.'
},
{
element: '.stage-tokenization > summary',
title: 'Step 6 of 12 β Stage 1: Tokenization',
body:
'Models do not read words β they read <i>tokens</i>, integer IDs for ' +
'chunks of text. This stage shows exactly how your prompt was sliced up. ' +
'Surprising splits (like "running" β "run" + "ning") explain a lot of ' +
'later model behavior.<br><br>' +
'Close this window and click the stage header to expand it, then skim ' +
'the tokens.'
},
{
element: '.stage-embedding > summary',
title: 'Step 7 of 12 β Stage 2: Embedding',
body:
'Each token ID gets converted into a vector of numbers (its "meaning") ' +
'plus a second vector encoding its <i>position</i> in the sentence. ' +
'Without position encoding, the model could not tell "dog bites man" ' +
'from "man bites dog".'
},
{
element: '.stage-attention > summary',
title: 'Step 8 of 12 β Stage 3: Attention',
body:
'This is the heart of the transformer. Each token looks at every other ' +
'token and decides how much to pay attention to it. Different <i>heads</i> ' +
'specialize: some track previous tokens, some detect duplicates, some ' +
'complete patterns (induction). The bars show which specialties fired ' +
'most on your prompt.'
},
{
element: '.stage-mlp > summary',
title: 'Step 9 of 12 β Stage 4: Knowledge Retrieval (MLP)',
body:
'After attention has shuffled information between tokens, the MLP layer ' +
'transforms each token vector independently. It is where the model\'s ' +
'factual and associative knowledge lives β expand β nonlinear activation ' +
'β compress.'
},
{
element: '.stage-output > summary',
title: 'Step 10 of 12 β Stage 5: Output Selection',
body:
'The final vector is projected onto the full vocabulary, producing a ' +
'probability for every possible next token. The chart shows the top 5 ' +
'candidates. This is where the abstract math becomes a concrete word.'
},
{
element: '#ablation-tool',
title: 'Step 11 of 12 β Test a head\'s importance',
body:
'Now the fun part. <i>Ablation</i> means silencing a specific attention ' +
'head to see how much the output changes. If removing a head barely ' +
'affects the prediction, it was not doing much here. If it wrecks the ' +
'prediction, that head was critical.'
},
{
element: '#ablation-category-buttons',
title: 'Step 12 of 12 β Ablate the induction heads',
body:
'Because you used the repetition prompt, the <b>Induction</b> quick-' +
'select button is the most interesting one to try. Close this window, ' +
'click Induction to add every induction head, then hit <b>Run Ablation</b>.' +
'<br><br>You should see the ablated output diverge from the original β ' +
'concrete evidence that induction heads are what make the model complete ' +
'"The cat sat on the" with "mat". That is mechanistic interpretability ' +
'in action.'
}
];
const lastIndex = raw.length - 1;
return raw.map((s, i) => ({
element: s.element,
popover: {
title: s.title,
description: s.body + (i === lastIndex ? FOOTER_LAST : FOOTER_NORMAL)
}
}));
}
function ensureFloatBtn() {
let btn = document.getElementById('tutorial-float-btn');
if (btn) return btn;
btn = document.createElement('button');
btn.id = 'tutorial-float-btn';
btn.className = 'tutorial-float-btn';
btn.type = 'button';
btn.innerHTML =
'<span class="tutorial-float-btn-label">βΆ Continue Tutorial</span>' +
'<span class="tutorial-float-btn-dismiss" title="Dismiss tutorial">β</span>';
btn.addEventListener('click', (e) => {
if (e.target.closest('.tutorial-float-btn-dismiss')) {
e.stopPropagation();
finishTour();
} else {
resumeTour();
}
});
document.body.appendChild(btn);
return btn;
}
function showFloatBtn() {
const btn = ensureFloatBtn();
btn.style.display = 'flex';
}
function hideFloatBtn() {
const btn = document.getElementById('tutorial-float-btn');
if (btn) btn.style.display = 'none';
}
function createDriver() {
if (!window.driver || !window.driver.js || !window.driver.js.driver) {
console.error('Driver.js not loaded β tutorial cannot start');
return null;
}
return window.driver.js.driver({
showProgress: false,
allowClose: true,
nextBtnText: 'Continue',
doneBtnText: 'Finish',
prevBtnText: 'Back',
showButtons: ['previous', 'next', 'close'],
steps: buildSteps(),
onNextClick: () => {
const steps = buildSteps();
const nextIndex = currentStep + 1;
if (nextIndex >= steps.length) {
if (driverObj) driverObj.destroy();
finishTour();
return;
}
// Step 11 (index 10) has no action for the user to take, so
// advance straight to step 12 without minimizing to the
// floating "Continue Tutorial" button.
if (currentStep === 10) {
currentStep = nextIndex;
safeSet(STEP_KEY, String(currentStep));
driverObj.moveNext();
return;
}
if (driverObj) driverObj.destroy();
currentStep = nextIndex;
safeSet(STEP_KEY, String(currentStep));
showFloatBtn();
},
onPrevClick: () => {
currentStep = Math.max(0, currentStep - 1);
safeSet(STEP_KEY, String(currentStep));
if (driverObj) driverObj.destroy();
showFloatBtn();
},
onCloseClick: () => {
finishTour();
}
});
}
function startFresh() {
safeRemove(DONE_KEY);
currentStep = 0;
safeSet(STEP_KEY, '0');
hideFloatBtn();
driverObj = createDriver();
if (driverObj) driverObj.drive(0);
}
function resumeTour() {
hideFloatBtn();
driverObj = createDriver();
if (driverObj) driverObj.drive(currentStep);
}
function finishTour() {
safeSet(DONE_KEY, 'true');
safeRemove(STEP_KEY);
if (driverObj) {
try { driverObj.destroy(); } catch (e) { /* ignore */ }
driverObj = null;
}
hideFloatBtn();
}
function restoreFromStorage() {
if (safeGet(DONE_KEY) === 'true') return;
const saved = safeGet(STEP_KEY);
if (saved === null) return;
const n = parseInt(saved, 10);
if (isNaN(n) || n < 0) return;
currentStep = n;
showFloatBtn();
}
// Event delegation β Dash renders the DOM after load, so the header button
// may not exist when this script first runs.
document.addEventListener('click', (e) => {
if (e.target.closest && e.target.closest('#start-tutorial-btn')) {
startFresh();
}
});
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', restoreFromStorage);
} else {
restoreFromStorage();
}
})();
|