YMRohit Codex commited on
Commit
a35ff5c
·
1 Parent(s): face158

Rewrite Kernel Mint frontend JS

Browse files

Co-authored-by: Codex <codex@openai.com>

Files changed (1) hide show
  1. app.py +612 -241
app.py CHANGED
@@ -271,220 +271,416 @@ APP_HTML = APP_HTML.replace("__KM_REPLAYS__", REPLAYS_JSON)
271
  APP_JS = r"""
272
  () => {
273
  const ACTS = {
274
- gelu:{lbl:'GELU',verb:'smoothly bends',info:'GELU — the gentle, smooth bend used inside almost every transformer (GPT, LLaMA). Lets a little of the negatives through.',f:x=>0.5*x*(1+Math.tanh(0.7978845608*(x+0.044715*x*x*x)))},
275
- silu:{lbl:'SiLU',verb:'swishes',info:'SiLU / Swish — x times sigmoid(x). The bend in LLaMA-style feed-forwards. Smooth like GELU but cheaper.',f:x=>x/(1+Math.exp(-x))},
276
- relu:{lbl:'ReLU',verb:'snaps negatives to 0',info:'ReLU — the simplest bend: keep positives, snap negatives to zero. Fast, classic, a little blunt.',f:x=>Math.max(x,0)},
277
- tanh:{lbl:'Tanh',verb:'squashes to ±1',info:'Tanh — squashes every number into the range -1 to 1. Big numbers get flattened at the top and bottom.',f:x=>Math.tanh(x)},
278
- sigmoid:{lbl:'Sigmoid',verb:'squashes to 0–1',info:'Sigmoid — squashes every number into 0 to 1, like a probability. Used for gates and yes/no signals.',f:x=>1/(1+Math.exp(-x))},
279
- relu2:{lbl:'ReLU²',verb:'snaps then squares',info:'Squared ReLU — snap negatives to zero, then square what is left. A sharper, newer bend.',f:x=>{const r=Math.max(x,0);return r*r;}},
280
- gelu_erf:{lbl:'GELU·',verb:'bends (exact)',info:'GELU (exact) — the precise erf form of GELU. Behaves almost identically to plain GELU, computed exactly.',f:x=>0.5*x*(1+Math.tanh(0.7978845608*(x+0.044715*x*x*x)))},
281
- leaky_relu:{lbl:'Leaky',verb:'leaks negatives',info:'Leaky ReLU — like ReLU, but negatives leak through at 1/100th strength instead of becoming exactly zero.',f:x=>x>0?x:0.01*x},
282
- elu:{lbl:'ELU',verb:'curves negatives',info:'ELU — positives pass straight through; negatives curve smoothly down toward -1 instead of snapping to zero.',f:x=>x>0?x:Math.exp(x)-1},
283
- mish:{lbl:'Mish',verb:'self-gates',info:'Mish — a smooth, self-gating bend (x · tanh(softplus(x))). Newer, and often a touch better than GELU.',f:x=>x*Math.tanh(Math.log(1+Math.exp(x)))},
284
  };
285
  const NORMS = {
286
- rmsnorm:{lbl:'RMSNorm',info:'RMSNorm — makes a row of numbers a consistent size (divides by their root-mean-square), then scales. The tidy-up step in LLaMA, Qwen, Mistral.'},
287
- layernorm:{lbl:'LayerNorm',info:'LayerNorm — centres a row of numbers (subtract the average) and makes them a consistent size (divide by the spread). The classic tidy-up.'},
288
  };
289
- const MEM = {info:'Memory (residual) — before tidying up, add back the numbers you started with. This is the “residual connection” that lets deep AI models train at all.'};
290
  const CLASSICS = {
291
- softmax:{lbl:'Softmax 🎯',info:'Softmax — turns a row of numbers into probabilities that add to 1. This is how attention decides what to focus on, and how a model picks the next word.'},
292
- softmax_scale:{lbl:'Scaled softmax',info:'Scaled softmax — softmax after multiplying by a scale. The exact step before attention, where the scale keeps the numbers sane.'},
293
- swiglu:{lbl:'SwiGLU 🚪',info:'SwiGLU — SiLU(gate) × up. A “gated” feed-forward: one half decides how much of the other half to let through. Used in LLaMA.'},
294
- geglu:{lbl:'GeGLU 🚪',info:'GeGLU — like SwiGLU but with a GELU gate. Another gated feed-forward flavour.'},
295
- rmsnorm:{lbl:'RMSNorm 📐',info:'RMSNorm on its own — the tidy-up step, no bend after it.'},
296
- layernorm:{lbl:'LayerNorm 📐',info:'LayerNorm on its own — centre and rescale a row of numbers.'},
297
  };
298
  const INPUT = [0.6,-0.9,1.4,-0.3,0.2,-1.6,0.8,1.1,-0.5,0.0,1.7,-1.1,0.4,-0.2];
299
 
300
- const root=document.getElementById('km'); if(!root||root.dataset.init)return; root.dataset.init=1;
301
- const $=s=>root.querySelector(s), $$=s=>root.querySelectorAll(s);
302
- const esc=s=>String(s??'').replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
 
 
 
303
  let REPLAYS={};
304
  try{REPLAYS=JSON.parse(($('#km-replays-data')||{}).textContent||'{}');}catch(e){REPLAYS={};}
305
  const REPLAY_META={
306
- rmsnorm_gelu:{label:'RMSNorm → GELU',short:'RMSNorm → GELU'},
307
  softmax:{label:'Softmax',short:'Softmax'},
308
- add_layernorm_silu:{label:'Memory + LayerNorm → SiLU',short:'+residual → LayerNorm → SiLU'}
309
  };
310
  const REPLAY_ORDER=['rmsnorm_gelu','softmax','add_layernorm_silu'].filter(k=>REPLAYS[k]);
311
- const slots={mem:null,norm:'rmsnorm',act:'gelu',act2:null}; let mode='build', classic='softmax';
312
-
313
- // ---- page nav ----
314
- $$('.km-navb').forEach(b=>b.addEventListener('click',()=>{
315
- $$('.km-navb').forEach(x=>x.classList.toggle('on',x===b));
316
- $$('.km-page').forEach(p=>p.hidden = p.dataset.page!==b.dataset.page);
317
- }));
318
-
319
- // ---- info panel (knowledge) ----
320
- const info=$('#km-info'); let infoLock='';
321
- function setInfo(t,lock){ if(lock!==undefined)infoLock=lock; info.innerHTML='ℹ️ '+(t||infoLock||'Hover a block to learn what it does, or just drag one into the machine.');
322
- info.animate([{opacity:.4},{opacity:1}],{duration:180,easing:'cubic-bezier(0.23,1,0.32,1)'}); }
323
- function machineInfo(){ const parts=[];
324
- if(slots.mem)parts.push('add the start back'); parts.push((NORMS[slots.norm].lbl)+' tidies');
325
- parts.push((ACTS[slots.act].lbl)+' '+ACTS[slots.act].verb); if(slots.act2)parts.push((ACTS[slots.act2].lbl)+' again');
326
- return 'Your machine: '+parts.join(' → ')+'. The AI must fuse all of it into one correct kernel.'; }
327
-
328
- // ---- block tray ----
329
- const tray=$('#km-tray');
330
- const tiles=[{t:'mem',k:'mem',lbl:'🧠 memory',info:MEM.info},
331
- ...Object.entries(NORMS).map(([k,v])=>({t:'norm',k,lbl:'📐 '+v.lbl,info:v.info})),
332
- ...Object.entries(ACTS).map(([k,v])=>({t:'act',k,lbl:'🎨 '+v.lbl,info:v.info}))];
333
- tiles.forEach(ti=>{ const el=document.createElement('div'); el.className='km-tile t'+ti.t; el.textContent=ti.lbl;
334
- el.draggable=true; el.dataset.t=ti.t; el.dataset.k=ti.k; el.title=ti.info;
335
- el.addEventListener('mouseenter',()=>setInfo(ti.info));
336
- el.addEventListener('dragstart',e=>{e.dataTransfer.setData('t',ti.t);e.dataTransfer.setData('k',ti.k);el.classList.add('drag');setInfo(ti.info);});
337
- el.addEventListener('dragend',()=>el.classList.remove('drag'));
338
- el.addEventListener('click',()=>{const s=root.querySelector('.km-slot[data-accept="'+ti.t+'"]:not(.filled)')||root.querySelector('.km-slot[data-accept="'+ti.t+'"]'); if(s)placeInSlot(s,ti.t,ti.k);});
339
- tray.appendChild(el); });
340
- info.addEventListener('mouseleave',()=>{}); // info stays on last hovered
341
-
342
- function tileLabel(t,k){ return t==='mem'?'🧠 memory':t==='norm'?'📐 '+NORMS[k].lbl:'🎨 '+ACTS[k].lbl; }
343
- function placeInSlot(slot,t,k){
344
- if(slot.dataset.accept!==t){slot.animate([{transform:'translateX(-4px)'},{transform:'translateX(4px)'},{transform:'none'}],{duration:160});return;}
345
- const name=slot.dataset.slot; slots[name]=(name==='mem')?true:k;
346
- slot.classList.remove('fnorm','fact','fmem'); slot.classList.add('filled','f'+t);
347
- slot.innerHTML='<span class="km-fill">'+tileLabel(t,k)+'</span><button class="km-x" title="remove">✕</button>';
348
- slot.querySelector('.km-x').addEventListener('click',ev=>{ev.stopPropagation();clearSlot(slot,name);});
349
- slot.animate([{transform:'scale(0.92)',opacity:.4},{transform:'scale(1)',opacity:1}],{duration:240,easing:'cubic-bezier(0.23,1,0.32,1)'});
350
- setInfo(machineInfo(), machineInfo()); render();
351
- }
352
- function clearSlot(slot,name){ slots[name]= name==='norm'?'rmsnorm':name==='act'?'gelu':null;
353
- const ph={mem:'+ memory <i>(optional)</i>',norm:'📐 a tidy-up block',act:'🎨 a bend block',act2:'🌀 bend again <i>(hard mode)</i>'}[name];
354
- slot.classList.remove('filled','fnorm','fact','fmem'); slot.innerHTML='<span class="km-ph">'+ph+'</span>';
355
- setInfo(machineInfo(), machineInfo()); render(); }
356
- $$('.km-slot').forEach(slot=>{
357
- slot.addEventListener('dragover',e=>{e.preventDefault();slot.classList.add('over');});
358
- slot.addEventListener('dragleave',()=>slot.classList.remove('over'));
359
- slot.addEventListener('drop',e=>{e.preventDefault();slot.classList.remove('over');placeInSlot(slot,e.dataTransfer.getData('t'),e.dataTransfer.getData('k'));});
360
- });
361
 
362
- // ---- classics (each explained, clickable) ----
363
- const cwrap=$('#km-classics');
364
- Object.entries(CLASSICS).forEach(([k,v])=>{ const b=document.createElement('button'); b.className='km-chip'; b.textContent=v.lbl; b.title=v.info;
365
- b.addEventListener('mouseenter',()=>setInfo(v.info));
366
- b.addEventListener('click',()=>{classic=k;cwrap.querySelectorAll('.km-chip').forEach(c=>c.classList.remove('on'));b.classList.add('on');setInfo(v.info,v.info);render();});
367
- if(k==='softmax')b.classList.add('on'); cwrap.appendChild(b); });
368
-
369
- // ---- learn-page block glossary ----
370
- const lb=$('#km-learn-blocks');
371
- if(lb){ const all=[['🧠 memory',MEM.info],...Object.values(NORMS).map(v=>['📐 '+v.lbl,v.info]),...Object.values(ACTS).map(v=>['🎨 '+v.lbl,v.info])];
372
- lb.innerHTML=all.map(([n,d])=>'<div class="km-gloss"><b>'+n+'</b><span>'+d+'</span></div>').join(''); }
373
-
374
- // ---- mode toggle ----
375
- $$('.km-segb').forEach(t=>t.addEventListener('click',()=>{ mode=t.dataset.mode;
376
- $$('.km-segb').forEach(x=>x.classList.toggle('on',x===t));
377
- $('#km-buildmode').hidden=mode!=='build'; $('#km-classic').hidden=mode!=='classic';
378
- setInfo(mode==='classic'?CLASSICS[classic].info:machineInfo(), mode==='classic'?CLASSICS[classic].info:machineInfo()); render(); }));
379
-
380
- // ---- knobs ----
381
- const triesEl=$('#km-tries'),tempEl=$('#km-temp');
382
- triesEl.addEventListener('input',()=>$('#km-tries-v').textContent=triesEl.value);
383
- tempEl.addEventListener('input',()=>$('#km-temp-v').textContent=(tempEl.value/100).toFixed(2));
384
-
385
- // ---- visualisation ----
386
- const svg=$('#km-svg'),W=320,H=150,PAD=12,n=INPUT.length;
387
- const xs=i=>PAD+i*(W-2*PAD)/(n-1), ys=v=>{const t=Math.max(-2.2,Math.min(2.2,v));return H/2-t*(H/2-PAD)/2.2;};
388
- let dots=[],line,cur=INPUT.slice(),anim=null;
389
- function buildSvg(){ svg.innerHTML='';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390
  const mid=document.createElementNS('http://www.w3.org/2000/svg','line');
391
- mid.setAttribute('x1',0);mid.setAttribute('x2',W);mid.setAttribute('y1',H/2);mid.setAttribute('y2',H/2);
392
- mid.setAttribute('stroke','#1f2d26');mid.setAttribute('stroke-width','1');svg.appendChild(mid);
393
- line=document.createElementNS('http://www.w3.org/2000/svg','polyline');line.setAttribute('fill','none');
394
- line.setAttribute('stroke','#2dd4a7');line.setAttribute('stroke-width','2');line.setAttribute('stroke-linejoin','round');svg.appendChild(line);
 
395
  dots=cur.map((v,i)=>{const c=document.createElementNS('http://www.w3.org/2000/svg','circle');c.setAttribute('r','3.4');c.setAttribute('cx',xs(i));svg.appendChild(c);return c;});
396
- paint(cur); }
397
- function paint(v){ line.setAttribute('points',v.map((x,i)=>xs(i)+','+ys(x)).join(' '));
398
- v.forEach((x,i)=>{dots[i].setAttribute('cy',ys(x));dots[i].setAttribute('fill',x>=0?'#6ee7b7':'#fca5a5');}); }
 
 
 
 
399
  const ease=t=>1-Math.pow(1-t,3);
400
- function morph(to,ms=480){ const from=cur.slice(),t0=performance.now(); if(anim)cancelAnimationFrame(anim);
401
- const step=t=>{const k=Math.min(1,(t-t0)/ms),e=ease(k);cur=from.map((f,i)=>f+(to[i]-f)*e);paint(cur);if(k<1)anim=requestAnimationFrame(step);else cur=to.slice();};
402
- anim=requestAnimationFrame(step); }
403
- function normalize(a,kind){const m=a.reduce((s,x)=>s+x,0)/a.length;
404
- if(kind==='layernorm'){const v=a.reduce((s,x)=>s+(x-m)*(x-m),0)/a.length;return a.map(x=>(x-m)/Math.sqrt(v+1e-5));}
405
- const r=Math.sqrt(a.reduce((s,x)=>s+x*x,0)/a.length+1e-6);return a.map(x=>x/r);}
406
- function pipeline(){ if(mode==='classic'){let v=INPUT.slice();
407
- if(classic.startsWith('softmax')){const mx=Math.max(...v);const e=v.map(x=>Math.exp(x-mx));const s=e.reduce((a,b)=>a+b,0);v=e.map(x=>x/s*4-0.5);}
408
- else if(classic.includes('norm'))v=normalize(INPUT,classic); else v=INPUT.map(x=>x/(1+Math.exp(-x)));
409
- return [{l:'your numbers',v:INPUT.slice()},{l:CLASSICS[classic].lbl,v}];}
410
- const st=[{l:'your numbers',v:INPUT.slice()}]; let v=INPUT.slice();
411
- if(slots.mem){v=v.map((x,i)=>x+INPUT[(i+3)%n]*0.5);st.push({l:'+ memory added',v:v.slice()});}
412
- v=normalize(v,slots.norm);st.push({l:NORMS[slots.norm].lbl+' tidied them',v:v.slice()});
413
- v=v.map(ACTS[slots.act].f);st.push({l:ACTS[slots.act].lbl+' '+ACTS[slots.act].verb,v:v.slice()});
414
- if(slots.act2){v=v.map(ACTS[slots.act2].f);st.push({l:ACTS[slots.act2].lbl+' bent again',v:v.slice()});}
415
- return st; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
416
  let playing=false;
417
- function play(){ if(playing)return;playing=true;const st=pipeline();let i=0;
418
- const nx=()=>{if(i>=st.length){playing=false;return;}$('#km-stage').textContent=st[i].l;
419
- $('#km-stage').animate([{opacity:0,transform:'translateY(4px)'},{opacity:1,transform:'none'}],{duration:200,easing:'cubic-bezier(0.23,1,0.32,1)'});
420
- morph(st[i].v);i++;setTimeout(nx,680);}; nx(); }
421
- function render(){ const st=pipeline();$('#km-stage').textContent=st[st.length-1].l;morph(st[st.length-1].v,360);
422
- $('.km-slot-opt').style.opacity=mode==='build'?1:.4; updateReplayOffer(); }
423
-
424
- // ---- recipe + bridge ----
425
- function recipe(){return{mode,norm:slots.norm,residual:!!slots.mem,acts:[slots.act].concat(slots.act2?[slots.act2]:[]),classic,
426
- spark:$('#km-spark').value,k:+triesEl.value,temp:tempEl.value/100,pro:$('#km-pro').checked,name:$('#km-name').value};}
427
- function replayName(key){return (REPLAY_META[key]&&REPLAY_META[key].label) || (REPLAYS[key]&&REPLAYS[key].op) || key;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
  function matchingReplayKey(){
429
- if(mode==='classic' && classic==='softmax' && REPLAYS.softmax)return 'softmax';
430
- if(mode==='build' && !slots.mem && slots.norm==='rmsnorm' && slots.act==='gelu' && !slots.act2 && REPLAYS.rmsnorm_gelu)return 'rmsnorm_gelu';
431
- if(mode==='build' && slots.mem && slots.norm==='layernorm' && slots.act==='silu' && !slots.act2 && REPLAYS.add_layernorm_silu)return 'add_layernorm_silu';
432
  return null;
433
  }
434
  function updateReplayOffer(){
435
- const btn=$('#km-replay'), note=$('#km-replay-note'); if(!btn||!REPLAY_ORDER.length)return;
436
- const match=matchingReplayKey(), key=match || REPLAY_ORDER[0];
 
437
  btn.dataset.replayKey=key;
438
- btn.textContent=match?'▶ Watch this exact real mint':'▶ Watch a real mint (instant)';
439
- if(note)note.textContent=match
440
- ? replayName(key)+' has a recorded, verified run. Press BUILD afterward to mint your own live.'
441
- : 'Watch a real mint recorded earlier. Press BUILD afterward to mint your own live.';
442
  $$('.km-rpick').forEach(b=>b.classList.toggle('on',b.dataset.replayKey===key));
443
  }
444
  function renderReplayChoices(){
445
- const wrap=$('#km-replay-picks'); if(!wrap)return;
 
446
  if(!REPLAY_ORDER.length){wrap.hidden=true;return;}
447
  wrap.hidden=false;
448
- wrap.innerHTML=REPLAY_ORDER.map(k=>"<button class='km-rpick' data-replay-key='"+esc(k)+"'>"+esc((REPLAY_META[k]&&REPLAY_META[k].short)||replayName(k))+" <span>"+(+REPLAYS[k].speedup_compile).toFixed(3)+"×</span></button>").join('');
449
  updateReplayOffer();
450
  }
451
- function bridge(){const inp=document.querySelector('#km_recipe textarea');if(!inp)return;
452
- const set=Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype,'value').set;
453
- set.call(inp,JSON.stringify(recipe()));inp.dispatchEvent(new Event('input',{bubbles:true}));
454
- setTimeout(()=>{const g=document.querySelector('#km_go button')||document.querySelector('#km_go');if(g)g.click();},60);}
455
- const HIST=[];
456
  const sp=x=>Number.isFinite(+x)?(+x).toFixed(1).replace(/\.0$/,''):'?';
 
457
  const sp3=x=>Number.isFinite(+x)?(+x).toFixed(3).replace(/0+$/,'').replace(/\.$/,''):'?';
458
- const isOk=s=>String(s||'').toLowerCase()==='ok' || String(s||'').toLowerCase()==='pass';
459
  function attemptSummary(d){
460
  const statuses=Array.isArray(d.statuses)?d.statuses:[];
461
  const passed=statuses.length?statuses.filter(isOk).length:(Number.isFinite(+d.n_verified)?+d.n_verified:0);
462
- const total=statuses.length || (Number.isFinite(+d.k)?+d.k:passed||0);
463
  return {statuses,passed,total};
464
  }
465
- function attemptChips(d,liveCount){
466
- const a=attemptSummary(d), statuses=a.statuses.slice(0,liveCount??a.statuses.length);
467
- if(!statuses.length)return "";
468
  return "<div class='km-attempts'>"+statuses.map((s,i)=>{
469
  const ok=isOk(s);
470
- return "<span class='km-attempt "+(ok?'ok':'bad')+"'><b>"+(ok?'✅':'❌')+"</b> attempt "+(i+1)+" · "+esc(s)+"</span>";
471
  }).join('')+"</div>";
472
  }
473
- function setSource(d,kind){
474
- const source=$('#km-source'), code=$('#km-code'); if(!source||!code)return;
475
- if(!d.source){source.hidden=true;code.textContent='';return;}
476
- source.hidden=false;source.open=false;
477
- const sum=source.querySelector('summary'); if(sum)sum.textContent=(kind==='recorded'?'Recorded real Triton kernel source':'Live real Triton kernel source');
478
- code.textContent=d.source;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479
  }
480
  function wakingError(msg){return /waking|wake|timeout|timed out|still|unexpected|http|modal|loading/i.test(String(msg||''));}
481
- function showResult(d,opts={}){stopBuild(); stopReplay(); const v=$('#km-verdict');
482
- const source=$('#km-source'), code=$('#km-code'); if(source)source.hidden=true; if(code)code.textContent='';
 
 
 
 
 
483
  if(d.error){
484
- const replayBtn=REPLAY_ORDER.length?"<button class='km-inline-replay' data-replay-key='"+esc(matchingReplayKey()||REPLAY_ORDER[0])+"'>▶ Watch a real mint while it wakes</button>":"";
485
  const wake=wakingError(d.error);
 
 
486
  v.innerHTML="<div class='km-card err'>"+
487
- "<div class='km-result-top'><div class='km-verdict-icon bad'>"+(wake?'⏳':'❌')+"</div><div><span class='km-run-label'>live mint</span><b>"+(wake?'The live model is still waking':'The live mint did not finish')+"</b></div></div>"+
488
  "<p class='km-dim'>"+(wake?'The backend scales to zero, so the first live mint can take about 90 seconds. The replay below is a real earlier mint, not a mockup.':'Backend said: '+esc(d.error))+"</p>"+
489
  (wake?replayBtn+"<p class='km-rawerr'>Backend said: "+esc(d.error)+"</p>":replayBtn)+"</div>";
490
  return;
@@ -492,114 +688,289 @@ APP_JS = r"""
492
  const a=attemptSummary(d);
493
  if(!d.verified){
494
  v.innerHTML="<div class='km-card miss'>"+
495
- "<div class='km-result-top'><div class='km-verdict-icon bad'>❌</div><div><span class='km-run-label'>referee verdict</span><b>Not verified yet</b></div></div>"+
496
  "<div class='km-ref-summary'><b>"+a.passed+" of "+(a.total||'?')+"</b> attempts passed the referee</div>"+
497
- attemptChips(d)+"<p class='km-dim'>"+esc(d.explanation||'Press BUILD again, raise tries, or change a block.')+"</p></div>";
498
  return;
499
  }
500
- const won=+d.speedup_compile>1, recorded=opts.recorded;
501
- const right=recorded?null:(guess==='beat')===won;
502
- const greveal=recorded
503
- ? "<div class='km-guess-res recorded'>recorded live run · press BUILD to mint your own</div>"
504
- : "<div class='km-guess-res "+(right?'hit':'miss')+"'>🎯 you called <b>"+(guess==='beat'?'BEAT':'TIE')+"</b> — "+(right?'nailed it':'not this time')+"</div>";
505
- const crown=d.beat_champion?"<span class='km-crown'>👑 you beat the big 27B</span>":d.on_leaderboard?"<span class='km-board'>🏆 leaderboard</span>":"";
506
- v.innerHTML="<div class='km-card ok km-result-card'>"+greveal+crown+
507
- "<div class='km-result-top'><div class='km-verdict-icon'>✅</div><div><span class='km-run-label'>"+(recorded?'recorded real mint':'live mint')+"</span><b>Verified by the referee</b><span>"+esc(d.op||'kernel')+"</span></div></div>"+
508
- "<div class='km-speed-hero'><big>"+sp3(d.speedup_compile)+"×</big><span>vs torch.compile</span></div>"+
509
  "<div class='km-ref-summary'><b>"+a.passed+" of "+(a.total||'?')+"</b> attempts passed the referee</div>"+
510
  attemptChips(d)+
511
- "<div class='km-stats'><div><big>"+sp(d.speedup_eager)+"×</big><span>faster than PyTorch eager</span></div>"+
512
  "<div><big>"+(Number.isFinite(+d.gen_seconds)?sp(d.gen_seconds)+'s':'?')+"</big><span>generation time</span></div></div>"+
513
  "<p class='km-dim'>"+esc(d.explanation||'')+"</p></div>";
514
- v.firstElementChild.animate([{transform:'scale(0.96)',opacity:0},{transform:'scale(1)',opacity:1}],{duration:300,easing:'cubic-bezier(0.23,1,0.32,1)'});
515
- setSource(d,recorded?'recorded':'live');
516
- if(!recorded){
517
- HIST.unshift("<code>"+esc(d.op||'kernel')+"</code> · "+sp(d.speedup_eager)+"× faster");
518
- $('#km-hist').innerHTML=HIST.slice(0,10).map(h=>"<div class='km-hrow'>"+h+"</div>").join('');
 
519
  }
520
  }
 
521
  let replayTimer=null;
522
  function stopReplay(){if(replayTimer){clearTimeout(replayTimer);replayTimer=null;}}
523
  function startReplay(key){
524
- const d=REPLAYS[key]; if(!d)return;
525
- stopBuild(); stopReplay(); play();
526
- const v=$('#km-verdict'), source=$('#km-source'), code=$('#km-code');
527
- if(source)source.hidden=true; if(code)code.textContent='';
 
 
 
 
 
528
  const statuses=Array.isArray(d.statuses)?d.statuses:[];
529
- const total=statuses.length || (Number.isFinite(+d.k)?+d.k:4);
530
  const passed=statuses.length?statuses.filter(isOk).length:(Number.isFinite(+d.n_verified)?+d.n_verified:0);
531
  v.innerHTML="<div class='km-card building replaying'>"+
532
- "<div class='km-result-top'><div class='km-verdict-icon'>▶</div><div><span class='km-run-label recorded'>recorded real mint</span><b>"+esc(replayName(key))+"</b><span>This is a genuine earlier backend run. BUILD still mints live.</span></div></div>"+
533
- "<div class='km-replay-stage' id='km-replay-stage'>loading recorded attempts…</div>"+
534
  "<div class='km-attempts' id='km-replay-attempts'></div>"+
535
  "<div class='km-bar'><i id='km-replay-fill'></i></div>"+
536
- "<p class='km-dim'>The 1B model took "+(Number.isFinite(+d.gen_seconds)?sp(d.gen_seconds)+'s':'real time')+" on this recorded run; the instant replay shows the referee loop without waiting for cold start.</p></div>";
537
  const att=$('#km-replay-attempts'), stage=$('#km-replay-stage'), fill=$('#km-replay-fill');
538
  let i=0;
539
  const tick=()=>{
540
  if(i<total){
541
- const st=statuses[i] || (i<passed?'ok':'not verified'), ok=isOk(st);
542
  if(stage)stage.textContent='referee verdict for attempt '+(i+1)+' of '+total;
543
- if(att)att.insertAdjacentHTML('beforeend',"<span class='km-attempt "+(ok?'ok':'bad')+"'><b>"+(ok?'✅':'❌')+"</b> attempt "+(i+1)+" · "+esc(st)+"</span>");
544
  if(fill)fill.style.width=Math.round(((i+1)/(total+1))*100)+'%';
545
- i++; replayTimer=setTimeout(tick,360); return;
 
 
546
  }
547
  if(stage)stage.textContent='best verified kernel selected';
548
  if(fill)fill.style.width='100%';
549
- replayTimer=setTimeout(()=>showResult(d,{recorded:true,replayKey:key}),420);
550
  };
551
  replayTimer=setTimeout(tick,180);
552
  }
553
- function renderLB(rows){const el=$('#km-lb-view');if(!el)return; if(!rows.length){el.innerHTML="<i>no kernels yet — be the first!</i>";return;}
554
- const med=['🥇','🥈','🥉'];
555
- el.innerHTML="<table class='km-lbt'><thead><tr><th>#</th><th>machine</th><th>builder</th><th>vs compiler</th><th>vs PyTorch</th></tr></thead><tbody>"+
556
- rows.slice(0,25).map((r,i)=>"<tr class='"+(r.champion?'champ':'')+"'><td>"+(med[i]||(i+1))+"</td><td><code>"+r.op+"</code></td><td>"+r.name+"</td><td class='hot'>"+r.speedup_compile.toFixed(2)+"×</td><td>"+sp(r.speedup_eager)+"×</td></tr>").join('')+"</tbody></table>"; }
557
- function watch(id,cb){const el=document.querySelector(id);if(!el)return;
558
- const read=()=>{const s=el.querySelector('[data-blob]');if(s){try{cb(JSON.parse(atob(s.dataset.blob)));}catch(e){}}};
559
- new MutationObserver(read).observe(el,{childList:true,subtree:true});read();}
560
- setTimeout(()=>{watch('#km_result',showResult);watch('#km_lb',d=>renderLB(d.rows||[]));},250);
561
- const rf=$('#km-lb-refresh'); if(rf)rf.addEventListener('click',()=>{const i=document.querySelector('#km_recipe textarea');if(i){i.dispatchEvent(new Event('input',{bubbles:true}));}});
562
 
563
- // ---- guess mechanic ----
564
- let guess='beat';
565
- $$('.km-gbtn').forEach(b=>b.addEventListener('click',()=>{guess=b.dataset.g;$$('.km-gbtn').forEach(x=>x.classList.toggle('on',x===b));}));
566
- root.addEventListener('click',e=>{
567
- const b=e.target.closest('[data-replay-key]'); if(!b||!root.contains(b))return;
568
- const key=b.dataset.replayKey; if(!REPLAYS[key])return;
569
- e.preventDefault(); startReplay(key);
570
- });
571
-
572
- // ---- live build animation (the wait feels alive; every stage shown really happens) ----
573
  let buildTimer=null;
574
- function startBuild(){ stopReplay(); const k=+triesEl.value, t0=Date.now(), replayKey=matchingReplayKey()||REPLAY_ORDER[0]||'';
575
- const stages=['model waking…','drafting '+k+' kernels…','compiling Triton…','checking correctness vs PyTorch…','timing vs torch.compile…'];
576
- const stepHtml=stages.map((s,i)=>"<span class='"+(i===0?'on':'')+"'>"+s+"</span>").join('');
577
- $('#km-verdict').innerHTML="<div class='km-card building'><b id='km-bstage'>"+stages[0]+"</b>"+
 
 
 
 
 
578
  "<div class='km-build-steps' id='km-build-steps'>"+stepHtml+"</div>"+
579
  "<div class='km-bar'><i id='km-barfill'></i></div>"+
580
- "<span class='km-dim'>The AI drafts up to "+k+" kernels; the referee compiles, correctness-checks, and times every candidate. First build of a session can take about 90 seconds while the model wakes.</span>"+
581
- (replayKey?"<button class='km-inline-replay' data-replay-key='"+esc(replayKey)+"'>▶ Watch a real mint while this runs</button>":"")+"</div>";
582
  let i=0;
583
- buildTimer=setInterval(()=>{ i++; const el=$('#km-bstage');
584
- const ix=i%stages.length;
585
- if(el){el.textContent=stages[ix];el.animate([{opacity:.35,transform:'translateY(3px)'},{opacity:1,transform:'none'}],{duration:220,easing:'cubic-bezier(0.23,1,0.32,1)'});}
 
586
  $$('#km-build-steps span').forEach((s,j)=>{s.classList.toggle('on',j===ix);s.classList.toggle('done',j<ix);});
587
- const f=$('#km-barfill'); if(f)f.style.width=Math.min(92,(Date.now()-t0)/950)+'%';
588
- const ks=Object.keys(ACTS); morph(INPUT.map(ACTS[ks[Math.random()*ks.length|0]].f).map(x=>x*(0.7+Math.random()*0.5)),420); // the model "experimenting"
589
- },1500); }
590
- function stopBuild(){ if(buildTimer){clearInterval(buildTimer);buildTimer=null;} const f=$('#km-barfill'); if(f)f.style.width='100%'; render(); }
591
-
592
- $('#km-build').addEventListener('click',()=>{
593
- $('#km-build').animate([{transform:'scale(0.97)'},{transform:'scale(1)'}],{duration:140,easing:'cubic-bezier(0.23,1,0.32,1)'});
594
- play(); startBuild(); bridge(); });
595
- $('#km-surprise').addEventListener('click',()=>{const ks=Object.keys(ACTS),ns=Object.keys(NORMS);
596
- placeInSlot(root.querySelector('.km-slot[data-slot=norm]'),'norm',ns[Math.random()*ns.length|0]);
597
- placeInSlot(root.querySelector('.km-slot[data-slot=act]'),'act',ks[Math.random()*ks.length|0]);
598
- if(Math.random()<0.5)placeInSlot(root.querySelector('.km-slot[data-slot=mem]'),'mem','mem'); else clearSlot(root.querySelector('.km-slot[data-slot=mem]'),'mem'); play();});
599
-
600
- placeInSlot(root.querySelector('.km-slot[data-slot=norm]'),'norm','rmsnorm');
601
- placeInSlot(root.querySelector('.km-slot[data-slot=act]'),'act','gelu');
602
- buildSvg(); renderReplayChoices(); setInfo(machineInfo(), machineInfo()); render();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
603
  }
604
  """
605
 
 
271
  APP_JS = r"""
272
  () => {
273
  const ACTS = {
274
+ gelu:{lbl:'GELU',verb:'smoothly bends',info:'GELU is the gentle bend used inside many transformers. It lets a little of the negative side through.',f:x=>0.5*x*(1+Math.tanh(0.7978845608*(x+0.044715*x*x*x)))},
275
+ silu:{lbl:'SiLU',verb:'swishes',info:'SiLU, also called Swish, is x times sigmoid(x). It is common in LLaMA style feed-forward blocks.',f:x=>x/(1+Math.exp(-x))},
276
+ relu:{lbl:'ReLU',verb:'clips negatives',info:'ReLU keeps positive values and clips negative values to zero. It is fast, classic, and direct.',f:x=>Math.max(x,0)},
277
+ tanh:{lbl:'Tanh',verb:'squashes to -1..1',info:'Tanh squashes each value into the range from -1 to 1. Large values flatten near the top and bottom.',f:x=>Math.tanh(x)},
278
+ sigmoid:{lbl:'Sigmoid',verb:'squashes to 0..1',info:'Sigmoid squashes each value into the range from 0 to 1. It is often used for gates and probabilities.',f:x=>1/(1+Math.exp(-x))},
279
+ relu2:{lbl:'ReLU2',verb:'clips then squares',info:'Squared ReLU clips negative values to zero, then squares the remaining positive values.',f:x=>{const r=Math.max(x,0);return r*r;}},
280
+ gelu_erf:{lbl:'GELU exact',verb:'bends exactly',info:'Exact GELU uses the erf form of GELU. It behaves almost like the tanh approximation, but follows the exact formula.',f:x=>0.5*x*(1+Math.tanh(0.7978845608*(x+0.044715*x*x*x)))},
281
+ leaky_relu:{lbl:'Leaky ReLU',verb:'leaks negatives',info:'Leaky ReLU is like ReLU, but negative values leak through at a small scale instead of becoming zero.',f:x=>x>0?x:0.01*x},
282
+ elu:{lbl:'ELU',verb:'curves negatives',info:'ELU passes positive values through and curves negative values smoothly down toward -1.',f:x=>x>0?x:Math.exp(x)-1},
283
+ mish:{lbl:'Mish',verb:'self gates',info:'Mish is a smooth self-gating activation, x times tanh of softplus(x).',f:x=>x*Math.tanh(Math.log(1+Math.exp(x)))},
284
  };
285
  const NORMS = {
286
+ rmsnorm:{lbl:'RMSNorm',info:'RMSNorm makes a row of values a consistent size by dividing by root mean square, then scaling.'},
287
+ layernorm:{lbl:'LayerNorm',info:'LayerNorm centers a row of values by subtracting the average, then rescales by the spread.'},
288
  };
289
+ const MEM = {info:'Residual add means adding the input back before the next operation. This is the skip path used in deep models.'};
290
  const CLASSICS = {
291
+ softmax:{lbl:'Softmax',info:'Softmax turns a row of scores into probabilities that add to 1.'},
292
+ softmax_scale:{lbl:'Scaled softmax',info:'Scaled softmax applies a scale before softmax. It is the attention score step used before attention weights.'},
293
+ swiglu:{lbl:'SwiGLU',info:'SwiGLU is a gated feed-forward operation: SiLU on the gate multiplied by the up projection.'},
294
+ geglu:{lbl:'GeGLU',info:'GeGLU is a gated feed-forward operation like SwiGLU, but with a GELU gate.'},
295
+ rmsnorm:{lbl:'RMSNorm',info:'RMSNorm on its own, without an activation after it.'},
296
+ layernorm:{lbl:'LayerNorm',info:'LayerNorm on its own, without an activation after it.'},
297
  };
298
  const INPUT = [0.6,-0.9,1.4,-0.3,0.2,-1.6,0.8,1.1,-0.5,0.0,1.7,-1.1,0.4,-0.2];
299
 
300
+ const root=document.getElementById('km');
301
+ if(!root||root.dataset.init)return;
302
+ root.dataset.init='1';
303
+ const $=s=>root.querySelector(s), $$=s=>Array.from(root.querySelectorAll(s));
304
+ const cleanText=s=>String(s??'').replace(/\u2013|\u2014/g,' - ').replace(/\u2192/g,'->').replace(/\u00d7/g,'x').replace(/[\u2600-\u27BF]/g,'').replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,'').replace(/\s+/g,' ').trim();
305
+ const esc=s=>cleanText(s).replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
306
  let REPLAYS={};
307
  try{REPLAYS=JSON.parse(($('#km-replays-data')||{}).textContent||'{}');}catch(e){REPLAYS={};}
308
  const REPLAY_META={
309
+ rmsnorm_gelu:{label:'RMSNorm to GELU',short:'RMSNorm GELU'},
310
  softmax:{label:'Softmax',short:'Softmax'},
311
+ add_layernorm_silu:{label:'Residual plus LayerNorm to SiLU',short:'Residual LayerNorm SiLU'}
312
  };
313
  const REPLAY_ORDER=['rmsnorm_gelu','softmax','add_layernorm_silu'].filter(k=>REPLAYS[k]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
 
315
+ const state={tab:'build',mode:'build',slots:{mem:false,norm:'rmsnorm',act:'gelu',act2:null},classic:'softmax'};
316
+ let buildTries=4, expertTries=5, resolvedExpert=null, lastMintedMode='build';
317
+ const HIST=[];
318
+
319
+ function animate(el,frames,opts){ if(el&&el.animate)el.animate(frames,opts); }
320
+ function labelRecipe(r){
321
+ if(!r)return 'unknown';
322
+ if(r.mode==='classic')return CLASSICS[r.classic]?.lbl||r.classic||'classic op';
323
+ const p=[];
324
+ if(r.residual)p.push('residual add');
325
+ p.push(NORMS[r.norm]?.lbl||r.norm);
326
+ (r.acts||[]).forEach(a=>p.push(ACTS[a]?.lbl||a));
327
+ return p.join(' + ');
328
+ }
329
+ function machineInfo(){
330
+ if(state.mode==='classic')return CLASSICS[state.classic].info;
331
+ const p=[];
332
+ p.push(state.slots.mem?'add input':'no residual add');
333
+ p.push(NORMS[state.slots.norm].lbl);
334
+ p.push(ACTS[state.slots.act].lbl);
335
+ if(state.slots.act2)p.push(ACTS[state.slots.act2].lbl);
336
+ return 'Current operation: '+p.join(' + ')+'. The model must fuse it into one verified kernel.';
337
+ }
338
+ function setInfo(t){
339
+ const info=$('#km-info');
340
+ if(!info)return;
341
+ info.textContent=cleanText(t||machineInfo());
342
+ animate(info,[{opacity:.45},{opacity:1}],{duration:180,easing:'cubic-bezier(0.16,1,0.3,1)'});
343
+ }
344
+
345
+ function positionTabPill(){
346
+ const tabs=$('#km-tabs'), pill=$('#km-tab-pill'), btn=$('.km-tab.on');
347
+ if(!tabs||!pill||!btn)return;
348
+ const tr=tabs.getBoundingClientRect(), br=btn.getBoundingClientRect();
349
+ pill.style.width=br.width+'px';
350
+ pill.style.transform='translateX('+(br.left-tr.left-4)+'px)';
351
+ }
352
+ function showTab(tab){
353
+ state.tab=tab;
354
+ $$('.km-tab').forEach(b=>b.classList.toggle('on',b.dataset.tab===tab));
355
+ $$('.km-page').forEach(p=>{p.hidden=p.dataset.page!==tab;});
356
+ requestAnimationFrame(positionTabPill);
357
+ if(tab==='expert')resolveExpert();
358
+ }
359
+ $$('.km-tab').forEach(b=>b.addEventListener('click',()=>showTab(b.dataset.tab)));
360
+ window.addEventListener('resize',positionTabPill);
361
+
362
+ function optionInfo(slot,key){
363
+ if(slot==='mem')return key==='add'?MEM.info:'No residual add. The kernel starts from the input row and does not add a skip value.';
364
+ if(slot==='norm')return NORMS[key].info;
365
+ if(slot==='act'||slot==='act2')return key==='none'?'No second activation. The pipeline stops after the first activation.':ACTS[key].info;
366
+ return '';
367
+ }
368
+ function optionLabel(slot,key){
369
+ if(slot==='mem')return key==='add'?'add input':'none';
370
+ if(slot==='norm')return NORMS[key].lbl;
371
+ if(slot==='act'||slot==='act2')return key==='none'?'none':ACTS[key].lbl;
372
+ return key;
373
+ }
374
+ function selectedKey(slot){
375
+ if(slot==='mem')return state.slots.mem?'add':'none';
376
+ if(slot==='act2')return state.slots.act2||'none';
377
+ return state.slots[slot];
378
+ }
379
+ function setSlot(slot,key,quiet){
380
+ if(slot==='mem')state.slots.mem=key==='add';
381
+ else if(slot==='act2')state.slots.act2=key==='none'?null:key;
382
+ else state.slots[slot]=key;
383
+ const wrap=root.querySelector('.km-pick[data-slot="'+slot+'"]');
384
+ if(wrap)wrap.querySelectorAll('.km-opt').forEach(b=>b.classList.toggle('on',b.dataset.key===selectedKey(slot)));
385
+ if(!quiet)setInfo(optionInfo(slot,key));
386
+ render();
387
+ }
388
+ function renderPickers(){
389
+ $$('.km-pick[data-slot]').forEach(wrap=>{
390
+ const slot=wrap.dataset.slot;
391
+ let keys=[];
392
+ if(slot==='mem')keys=['none','add'];
393
+ if(slot==='norm')keys=Object.keys(NORMS);
394
+ if(slot==='act')keys=Object.keys(ACTS);
395
+ if(slot==='act2')keys=['none'].concat(Object.keys(ACTS));
396
+ wrap.innerHTML='';
397
+ keys.forEach(key=>{
398
+ const b=document.createElement('button');
399
+ b.type='button';
400
+ b.className='km-opt';
401
+ b.dataset.key=key;
402
+ b.textContent=optionLabel(slot,key);
403
+ b.title=optionInfo(slot,key);
404
+ b.classList.toggle('on',key===selectedKey(slot));
405
+ b.addEventListener('mouseenter',()=>setInfo(optionInfo(slot,key)));
406
+ b.addEventListener('focus',()=>setInfo(optionInfo(slot,key)));
407
+ b.addEventListener('click',()=>setSlot(slot,key));
408
+ wrap.appendChild(b);
409
+ });
410
+ });
411
+ }
412
+
413
+ function renderClassics(){
414
+ const cwrap=$('#km-classics');
415
+ if(!cwrap)return;
416
+ cwrap.innerHTML='';
417
+ Object.entries(CLASSICS).forEach(([k,v])=>{
418
+ const b=document.createElement('button');
419
+ b.type='button';
420
+ b.className='km-opt';
421
+ b.dataset.key=k;
422
+ b.textContent=v.lbl;
423
+ b.title=v.info;
424
+ b.classList.toggle('on',state.classic===k);
425
+ b.addEventListener('mouseenter',()=>setInfo(v.info));
426
+ b.addEventListener('focus',()=>setInfo(v.info));
427
+ b.addEventListener('click',()=>{
428
+ state.classic=k;
429
+ cwrap.querySelectorAll('.km-opt').forEach(x=>x.classList.toggle('on',x===b));
430
+ setInfo(v.info);
431
+ render();
432
+ });
433
+ cwrap.appendChild(b);
434
+ });
435
+ }
436
+ function setComposerMode(mode){
437
+ state.mode=mode;
438
+ $$('.km-segb').forEach(b=>b.classList.toggle('on',b.dataset.mode===mode));
439
+ const compose=$('#km-compose'), classic=$('#km-classic');
440
+ if(compose)compose.hidden=mode!=='build';
441
+ if(classic)classic.hidden=mode!=='classic';
442
+ setInfo(machineInfo());
443
+ render();
444
+ }
445
+ $$('.km-segb').forEach(b=>b.addEventListener('click',()=>setComposerMode(b.dataset.mode)));
446
+
447
+ function setupStepper(id,outId,initial,onChange){
448
+ const wrap=$(id), out=$(outId);
449
+ if(!wrap)return;
450
+ wrap.innerHTML='';
451
+ const set=v=>{
452
+ onChange(v);
453
+ if(out)out.textContent=String(v);
454
+ wrap.querySelectorAll('button').forEach(b=>b.classList.toggle('on',+b.dataset.v===v));
455
+ };
456
+ for(let i=1;i<=8;i++){
457
+ const b=document.createElement('button');
458
+ b.type='button';
459
+ b.dataset.v=String(i);
460
+ b.textContent=String(i);
461
+ b.addEventListener('click',()=>set(i));
462
+ wrap.appendChild(b);
463
+ }
464
+ set(initial);
465
+ }
466
+ function setupTemp(id,outId){
467
+ const el=$(id), out=$(outId);
468
+ if(!el)return;
469
+ const sync=()=>{if(out)out.textContent=(+el.value/100).toFixed(2);};
470
+ el.addEventListener('input',sync);
471
+ sync();
472
+ }
473
+
474
+ const svg=$('#km-svg'), W=320, H=150, PAD=12, n=INPUT.length;
475
+ const xs=i=>PAD+i*(W-2*PAD)/(n-1);
476
+ const ys=v=>{const t=Math.max(-2.2,Math.min(2.2,v));return H/2-t*(H/2-PAD)/2.2;};
477
+ let dots=[], line=null, cur=INPUT.slice(), anim=null;
478
+ function buildSvg(){
479
+ if(!svg)return;
480
+ svg.innerHTML='';
481
  const mid=document.createElementNS('http://www.w3.org/2000/svg','line');
482
+ mid.setAttribute('x1',0); mid.setAttribute('x2',W); mid.setAttribute('y1',H/2); mid.setAttribute('y2',H/2);
483
+ mid.setAttribute('stroke','#1f2d26'); mid.setAttribute('stroke-width','1'); svg.appendChild(mid);
484
+ line=document.createElementNS('http://www.w3.org/2000/svg','polyline');
485
+ line.setAttribute('fill','none'); line.setAttribute('stroke','#34e0a1'); line.setAttribute('stroke-width','2'); line.setAttribute('stroke-linejoin','round');
486
+ svg.appendChild(line);
487
  dots=cur.map((v,i)=>{const c=document.createElementNS('http://www.w3.org/2000/svg','circle');c.setAttribute('r','3.4');c.setAttribute('cx',xs(i));svg.appendChild(c);return c;});
488
+ paint(cur);
489
+ }
490
+ function paint(v){
491
+ if(!line)return;
492
+ line.setAttribute('points',v.map((x,i)=>xs(i)+','+ys(x)).join(' '));
493
+ v.forEach((x,i)=>{if(dots[i]){dots[i].setAttribute('cy',ys(x));dots[i].setAttribute('fill',x>=0?'#34e0a1':'#ff7a7a');}});
494
+ }
495
  const ease=t=>1-Math.pow(1-t,3);
496
+ function morph(to,ms=480){
497
+ if(!line)return;
498
+ const from=cur.slice(), t0=performance.now();
499
+ if(anim)cancelAnimationFrame(anim);
500
+ const step=t=>{
501
+ const k=Math.min(1,(t-t0)/ms), e=ease(k);
502
+ cur=from.map((f,i)=>f+(to[i]-f)*e);
503
+ paint(cur);
504
+ if(k<1)anim=requestAnimationFrame(step); else cur=to.slice();
505
+ };
506
+ anim=requestAnimationFrame(step);
507
+ }
508
+ function normalize(a,kind){
509
+ const m=a.reduce((s,x)=>s+x,0)/a.length;
510
+ if(kind==='layernorm'){
511
+ const v=a.reduce((s,x)=>s+(x-m)*(x-m),0)/a.length;
512
+ return a.map(x=>(x-m)/Math.sqrt(v+1e-5));
513
+ }
514
+ const r=Math.sqrt(a.reduce((s,x)=>s+x*x,0)/a.length+1e-6);
515
+ return a.map(x=>x/r);
516
+ }
517
+ function classicViz(key){
518
+ let v=INPUT.slice();
519
+ if(key.startsWith('softmax')){
520
+ const scale=key==='softmax_scale'?0.7:1;
521
+ const z=v.map(x=>x*scale), mx=Math.max(...z), e=z.map(x=>Math.exp(x-mx)), s=e.reduce((a,b)=>a+b,0);
522
+ v=e.map(x=>x/s*4-0.5);
523
+ }else if(key==='rmsnorm'||key==='layernorm'){
524
+ v=normalize(v,key);
525
+ }else if(key==='swiglu'){
526
+ v=v.map(x=>(x/(1+Math.exp(-x)))*(0.7+Math.abs(x)*0.3));
527
+ }else if(key==='geglu'){
528
+ v=v.map(x=>ACTS.gelu.f(x)*(0.7+Math.abs(x)*0.3));
529
+ }
530
+ return v;
531
+ }
532
+ function pipeline(){
533
+ if(state.mode==='classic')return [{l:'input',v:INPUT.slice()},{l:CLASSICS[state.classic].lbl,v:classicViz(state.classic)}];
534
+ const st=[{l:'input',v:INPUT.slice()}];
535
+ let v=INPUT.slice();
536
+ if(state.slots.mem){v=v.map((x,i)=>x+INPUT[(i+3)%n]*0.5);st.push({l:'residual add',v:v.slice()});}
537
+ v=normalize(v,state.slots.norm);st.push({l:NORMS[state.slots.norm].lbl,v:v.slice()});
538
+ v=v.map(ACTS[state.slots.act].f);st.push({l:ACTS[state.slots.act].lbl+' '+ACTS[state.slots.act].verb,v:v.slice()});
539
+ if(state.slots.act2){v=v.map(ACTS[state.slots.act2].f);st.push({l:ACTS[state.slots.act2].lbl+' second pass',v:v.slice()});}
540
+ return st;
541
+ }
542
  let playing=false;
543
+ function play(){
544
+ if(playing)return;
545
+ playing=true;
546
+ const st=pipeline();
547
+ let i=0;
548
+ const stage=$('#km-stage-label');
549
+ const next=()=>{
550
+ if(i>=st.length){playing=false;return;}
551
+ if(stage){stage.textContent=cleanText(st[i].l);animate(stage,[{opacity:0,transform:'translateY(4px)'},{opacity:1,transform:'none'}],{duration:200,easing:'cubic-bezier(0.16,1,0.3,1)'});}
552
+ morph(st[i].v);
553
+ i++;
554
+ setTimeout(next,680);
555
+ };
556
+ next();
557
+ }
558
+ function render(){
559
+ const st=pipeline(), stage=$('#km-stage-label');
560
+ if(stage)stage.textContent=cleanText(st[st.length-1].l);
561
+ morph(st[st.length-1].v,360);
562
+ updateReplayOffer();
563
+ }
564
+
565
+ function buildRecipe(){
566
+ const tempEl=$('#km-temp'), proEl=$('#km-pro'), nameEl=$('#km-name');
567
+ return {
568
+ mode:state.mode,
569
+ norm:state.slots.norm,
570
+ residual:!!state.slots.mem,
571
+ acts:[state.slots.act].concat(state.slots.act2?[state.slots.act2]:[]),
572
+ classic:state.classic,
573
+ spark:'',
574
+ k:buildTries,
575
+ temp:(+(tempEl?tempEl.value:70))/100,
576
+ pro:!!(proEl&&proEl.checked),
577
+ name:nameEl?nameEl.value:''
578
+ };
579
+ }
580
+ function expertRecipe(){
581
+ if(!resolvedExpert||resolvedExpert.unknown)return null;
582
+ const tempEl=$('#km-xtemp'), proEl=$('#km-xpro'), briefEl=$('#km-xbrief');
583
+ const r=Object.assign({},resolvedExpert.recipe);
584
+ r.k=expertTries;
585
+ r.temp=(+(tempEl?tempEl.value:70))/100;
586
+ r.pro=!!(proEl&&proEl.checked);
587
+ r.name='expert';
588
+ r.spark=briefEl?briefEl.value:'';
589
+ return r;
590
+ }
591
+ function currentRecipe(){
592
+ return state.tab==='expert'?(expertRecipe()||buildRecipe()):buildRecipe();
593
+ }
594
+ function bridge(recipe){
595
+ const inp=document.querySelector('#km_recipe textarea');
596
+ if(!inp)return;
597
+ const set=Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype,'value').set;
598
+ set.call(inp,JSON.stringify(recipe));
599
+ inp.dispatchEvent(new Event('input',{bubbles:true}));
600
+ inp.dispatchEvent(new Event('change',{bubbles:true}));
601
+ setTimeout(()=>{
602
+ const g=document.querySelector('#km_go button')||document.querySelector('#km_go');
603
+ if(g)g.click();
604
+ },60);
605
+ }
606
+
607
+ function replayName(key){return (REPLAY_META[key]&&REPLAY_META[key].label)||(REPLAYS[key]&&cleanText(REPLAYS[key].op))||key;}
608
  function matchingReplayKey(){
609
+ if(state.mode==='classic'&&state.classic==='softmax'&&REPLAYS.softmax)return 'softmax';
610
+ if(state.mode==='build'&&!state.slots.mem&&state.slots.norm==='rmsnorm'&&state.slots.act==='gelu'&&!state.slots.act2&&REPLAYS.rmsnorm_gelu)return 'rmsnorm_gelu';
611
+ if(state.mode==='build'&&state.slots.mem&&state.slots.norm==='layernorm'&&state.slots.act==='silu'&&!state.slots.act2&&REPLAYS.add_layernorm_silu)return 'add_layernorm_silu';
612
  return null;
613
  }
614
  function updateReplayOffer(){
615
+ const btn=$('#km-replay');
616
+ if(!btn||!REPLAY_ORDER.length)return;
617
+ const key=matchingReplayKey()||REPLAY_ORDER[0];
618
  btn.dataset.replayKey=key;
619
+ btn.textContent=matchingReplayKey()?'Watch this recorded mint':'Watch a recorded mint';
 
 
 
620
  $$('.km-rpick').forEach(b=>b.classList.toggle('on',b.dataset.replayKey===key));
621
  }
622
  function renderReplayChoices(){
623
+ const wrap=$('#km-replay-picks');
624
+ if(!wrap)return;
625
  if(!REPLAY_ORDER.length){wrap.hidden=true;return;}
626
  wrap.hidden=false;
627
+ wrap.innerHTML=REPLAY_ORDER.map(k=>"<button class='km-rpick' data-replay-key='"+esc(k)+"'>"+esc((REPLAY_META[k]&&REPLAY_META[k].short)||replayName(k))+" <span>"+sp3(REPLAYS[k].speedup_compile)+"x</span></button>").join('');
628
  updateReplayOffer();
629
  }
630
+
 
 
 
 
631
  const sp=x=>Number.isFinite(+x)?(+x).toFixed(1).replace(/\.0$/,''):'?';
632
+ const sp2=x=>Number.isFinite(+x)?(+x).toFixed(2):'?';
633
  const sp3=x=>Number.isFinite(+x)?(+x).toFixed(3).replace(/0+$/,'').replace(/\.$/,''):'?';
634
+ const isOk=s=>String(s||'').toLowerCase()==='ok'||String(s||'').toLowerCase()==='pass';
635
  function attemptSummary(d){
636
  const statuses=Array.isArray(d.statuses)?d.statuses:[];
637
  const passed=statuses.length?statuses.filter(isOk).length:(Number.isFinite(+d.n_verified)?+d.n_verified:0);
638
+ const total=statuses.length||(Number.isFinite(+d.k)?+d.k:passed||0);
639
  return {statuses,passed,total};
640
  }
641
+ function attemptChips(d,limit){
642
+ const a=attemptSummary(d), statuses=a.statuses.slice(0,limit??a.statuses.length);
643
+ if(!statuses.length)return '';
644
  return "<div class='km-attempts'>"+statuses.map((s,i)=>{
645
  const ok=isOk(s);
646
+ return "<span class='km-attempt "+(ok?'ok':'bad')+"'><b>"+(ok?'OK':'FAIL')+"</b> attempt "+(i+1)+", "+esc(s)+"</span>";
647
  }).join('')+"</div>";
648
  }
649
+ function target(mode){
650
+ if(mode==='expert'){
651
+ return {verdict:$('#km-xverdict'),source:$('#km-xsource'),code:$('#km-xsource .km-code')};
652
+ }
653
+ return {verdict:$('#km-verdict'),source:$('#km-source'),code:$('#km-code')};
654
+ }
655
+ function clearSource(mode){
656
+ const t=target(mode);
657
+ if(t.source)t.source.hidden=true;
658
+ if(t.code)t.code.textContent='';
659
+ }
660
+ function setSource(d,kind,mode){
661
+ const t=target(mode);
662
+ if(!t.source||!t.code)return;
663
+ if(!d.source){clearSource(mode);return;}
664
+ t.source.hidden=false;
665
+ t.source.open=false;
666
+ const sum=t.source.querySelector('summary');
667
+ if(sum)sum.textContent=kind==='recorded'?'Recorded Triton kernel source':'Live Triton kernel source';
668
+ t.code.textContent=d.source;
669
  }
670
  function wakingError(msg){return /waking|wake|timeout|timed out|still|unexpected|http|modal|loading/i.test(String(msg||''));}
671
+ function showResult(d,opts={}){
672
+ const mode=opts.mode||lastMintedMode||'build';
673
+ stopBuild();
674
+ stopReplay();
675
+ const t=target(mode), v=t.verdict;
676
+ if(!v)return;
677
+ clearSource(mode);
678
  if(d.error){
 
679
  const wake=wakingError(d.error);
680
+ const replayKey=matchingReplayKey()||REPLAY_ORDER[0]||'';
681
+ const replayBtn=replayKey?"<button class='km-inline-replay' data-replay-key='"+esc(replayKey)+"'>Watch a recorded mint while it wakes</button>":"";
682
  v.innerHTML="<div class='km-card err'>"+
683
+ "<div class='km-result-top'><div class='km-verdict-icon bad'>!</div><div><span class='km-run-label'>live mint</span><b>"+(wake?'The live model is still waking':'The live mint did not finish')+"</b></div></div>"+
684
  "<p class='km-dim'>"+(wake?'The backend scales to zero, so the first live mint can take about 90 seconds. The replay below is a real earlier mint, not a mockup.':'Backend said: '+esc(d.error))+"</p>"+
685
  (wake?replayBtn+"<p class='km-rawerr'>Backend said: "+esc(d.error)+"</p>":replayBtn)+"</div>";
686
  return;
 
688
  const a=attemptSummary(d);
689
  if(!d.verified){
690
  v.innerHTML="<div class='km-card miss'>"+
691
+ "<div class='km-result-top'><div class='km-verdict-icon bad'>NO</div><div><span class='km-run-label'>referee verdict</span><b>Not verified yet</b></div></div>"+
692
  "<div class='km-ref-summary'><b>"+a.passed+" of "+(a.total||'?')+"</b> attempts passed the referee</div>"+
693
+ attemptChips(d)+"<p class='km-dim'>"+esc(d.explanation||'Press Mint again, raise attempts, or change the operation.')+"</p></div>";
694
  return;
695
  }
696
+ const recorded=!!opts.recorded;
697
+ const note="<span class='km-pill-note "+(recorded?'recorded':'hit')+"'>"+(recorded?'recorded backend run':'live backend run')+"</span>";
698
+ const board=d.beat_champion?"<span class='km-crown'>27B champion beaten</span>":d.on_leaderboard?"<span class='km-board'>leaderboard result</span>":"";
699
+ v.innerHTML="<div class='km-card ok km-result-card'>"+note+board+
700
+ "<div class='km-result-top'><div class='km-verdict-icon'>OK</div><div><span class='km-run-label'>"+(recorded?'recorded mint':'live mint')+"</span><b>Verified by the referee</b><span>"+esc(d.op||'kernel')+"</span></div></div>"+
701
+ "<div class='km-speed-hero'><big>"+sp3(d.speedup_compile)+"x</big><span>vs torch.compile</span></div>"+
 
 
 
702
  "<div class='km-ref-summary'><b>"+a.passed+" of "+(a.total||'?')+"</b> attempts passed the referee</div>"+
703
  attemptChips(d)+
704
+ "<div class='km-stats'><div><big>"+sp(d.speedup_eager)+"x</big><span>faster than PyTorch eager</span></div>"+
705
  "<div><big>"+(Number.isFinite(+d.gen_seconds)?sp(d.gen_seconds)+'s':'?')+"</big><span>generation time</span></div></div>"+
706
  "<p class='km-dim'>"+esc(d.explanation||'')+"</p></div>";
707
+ animate(v.firstElementChild,[{transform:'scale(0.96)',opacity:0},{transform:'scale(1)',opacity:1}],{duration:300,easing:'cubic-bezier(0.16,1,0.3,1)'});
708
+ setSource(d,recorded?'recorded':'live',mode);
709
+ if(!recorded&&mode==='build'){
710
+ HIST.unshift("<code>"+esc(d.op||'kernel')+"</code><span>"+sp(d.speedup_eager)+"x faster</span>");
711
+ const hist=$('#km-hist');
712
+ if(hist)hist.innerHTML=HIST.slice(0,10).map(h=>"<div class='km-hrow'>"+h+"</div>").join('');
713
  }
714
  }
715
+
716
  let replayTimer=null;
717
  function stopReplay(){if(replayTimer){clearTimeout(replayTimer);replayTimer=null;}}
718
  function startReplay(key){
719
+ const d=REPLAYS[key];
720
+ if(!d)return;
721
+ lastMintedMode=state.tab==='expert'?'expert':'build';
722
+ stopBuild();
723
+ stopReplay();
724
+ play();
725
+ const t=target(lastMintedMode), v=t.verdict;
726
+ if(!v)return;
727
+ clearSource(lastMintedMode);
728
  const statuses=Array.isArray(d.statuses)?d.statuses:[];
729
+ const total=statuses.length||(Number.isFinite(+d.k)?+d.k:4);
730
  const passed=statuses.length?statuses.filter(isOk).length:(Number.isFinite(+d.n_verified)?+d.n_verified:0);
731
  v.innerHTML="<div class='km-card building replaying'>"+
732
+ "<div class='km-result-top'><div class='km-verdict-icon'>RUN</div><div><span class='km-run-label recorded'>recorded mint</span><b>"+esc(replayName(key))+"</b><span>This is a genuine earlier backend run. Mint still starts a live run.</span></div></div>"+
733
+ "<div class='km-replay-stage' id='km-replay-stage'>loading recorded attempts...</div>"+
734
  "<div class='km-attempts' id='km-replay-attempts'></div>"+
735
  "<div class='km-bar'><i id='km-replay-fill'></i></div>"+
736
+ "<p class='km-dim'>The model took "+(Number.isFinite(+d.gen_seconds)?sp(d.gen_seconds)+'s':'real time')+" on this recorded run. The replay shows the referee loop without waiting for cold start.</p></div>";
737
  const att=$('#km-replay-attempts'), stage=$('#km-replay-stage'), fill=$('#km-replay-fill');
738
  let i=0;
739
  const tick=()=>{
740
  if(i<total){
741
+ const st=statuses[i]||(i<passed?'ok':'not verified'), ok=isOk(st);
742
  if(stage)stage.textContent='referee verdict for attempt '+(i+1)+' of '+total;
743
+ if(att)att.insertAdjacentHTML('beforeend',"<span class='km-attempt "+(ok?'ok':'bad')+"'><b>"+(ok?'OK':'FAIL')+"</b> attempt "+(i+1)+", "+esc(st)+"</span>");
744
  if(fill)fill.style.width=Math.round(((i+1)/(total+1))*100)+'%';
745
+ i++;
746
+ replayTimer=setTimeout(tick,360);
747
+ return;
748
  }
749
  if(stage)stage.textContent='best verified kernel selected';
750
  if(fill)fill.style.width='100%';
751
+ replayTimer=setTimeout(()=>showResult(d,{recorded:true,mode:lastMintedMode,replayKey:key}),420);
752
  };
753
  replayTimer=setTimeout(tick,180);
754
  }
 
 
 
 
 
 
 
 
 
755
 
 
 
 
 
 
 
 
 
 
 
756
  let buildTimer=null;
757
+ function startBuild(mode,recipe){
758
+ stopReplay();
759
+ const t=target(mode), v=t.verdict;
760
+ if(!v)return;
761
+ clearSource(mode);
762
+ const k=recipe.k||4, started=Date.now(), replayKey=mode==='build'?(matchingReplayKey()||REPLAY_ORDER[0]||''):(REPLAY_ORDER[0]||'');
763
+ const stages=['model waking...','drafting '+k+' kernels...','compiling Triton...','checking correctness vs PyTorch...','timing vs torch.compile...'];
764
+ const stepHtml=stages.map((s,i)=>"<span class='"+(i===0?'on':'')+"'>"+esc(s)+"</span>").join('');
765
+ v.innerHTML="<div class='km-card building'><b class='km-bstage' id='km-bstage'>"+esc(stages[0])+"</b>"+
766
  "<div class='km-build-steps' id='km-build-steps'>"+stepHtml+"</div>"+
767
  "<div class='km-bar'><i id='km-barfill'></i></div>"+
768
+ "<span class='km-dim'>The model drafts up to "+k+" kernels. The referee compiles, correctness-checks, and times every candidate. First build of a session can take about 90 seconds while the model wakes.</span>"+
769
+ (replayKey?"<button class='km-inline-replay' data-replay-key='"+esc(replayKey)+"'>Watch a recorded mint while this runs</button>":"")+"</div>";
770
  let i=0;
771
+ buildTimer=setInterval(()=>{
772
+ i++;
773
+ const ix=i%stages.length, el=$('#km-bstage');
774
+ if(el){el.textContent=stages[ix];animate(el,[{opacity:.35,transform:'translateY(3px)'},{opacity:1,transform:'none'}],{duration:220,easing:'cubic-bezier(0.16,1,0.3,1)'});}
775
  $$('#km-build-steps span').forEach((s,j)=>{s.classList.toggle('on',j===ix);s.classList.toggle('done',j<ix);});
776
+ const f=$('#km-barfill');
777
+ if(f)f.style.width=Math.min(92,(Date.now()-started)/950)+'%';
778
+ const ks=Object.keys(ACTS);
779
+ morph(INPUT.map(ACTS[ks[Math.random()*ks.length|0]].f).map(x=>x*(0.7+Math.random()*0.5)),420);
780
+ },1500);
781
+ }
782
+ function stopBuild(){
783
+ if(buildTimer){clearInterval(buildTimer);buildTimer=null;}
784
+ const f=$('#km-barfill');
785
+ if(f)f.style.width='100%';
786
+ render();
787
+ }
788
+
789
+ function renderLB(rows){
790
+ const el=$('#km-lb-view');
791
+ if(!el)return;
792
+ if(!rows.length){el.innerHTML='<i>No kernels yet. Be the first.</i>';return;}
793
+ el.innerHTML="<table class='km-lbt'><thead><tr><th>#</th><th>machine</th><th>builder</th><th>vs compiler</th><th>vs PyTorch</th></tr></thead><tbody>"+
794
+ rows.slice(0,25).map((r,i)=>"<tr class='"+(r.champion?'champ':'')+"'><td class='km-rank'>"+(i+1)+"</td><td><code>"+esc(r.op||'kernel')+"</code></td><td>"+esc(r.name||'anonymous')+"</td><td class='hot'>"+sp2(r.speedup_compile)+"x</td><td>"+sp(r.speedup_eager)+"x</td></tr>").join('')+"</tbody></table>";
795
+ }
796
+ function watch(id,cb){
797
+ const el=document.querySelector(id);
798
+ if(!el)return;
799
+ const read=()=>{
800
+ const s=el.querySelector('[data-blob]');
801
+ if(s){try{cb(JSON.parse(atob(s.dataset.blob)));}catch(e){}}
802
+ };
803
+ new MutationObserver(read).observe(el,{childList:true,subtree:true});
804
+ read();
805
+ }
806
+ setTimeout(()=>{watch('#km_result',showResult);watch('#km_lb',d=>renderLB(d.rows||[]));},250);
807
+ const rf=$('#km-lb-refresh');
808
+ if(rf)rf.addEventListener('click',()=>{
809
+ const inp=document.querySelector('#km_recipe textarea');
810
+ if(!inp)return;
811
+ const set=Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype,'value').set;
812
+ set.call(inp,JSON.stringify(Object.assign({},currentRecipe(),{refresh:Date.now()})));
813
+ inp.dispatchEvent(new Event('input',{bubbles:true}));
814
+ inp.dispatchEvent(new Event('change',{bubbles:true}));
815
+ });
816
+
817
+ const CLASSIC_TERMS=[
818
+ {key:'softmax_scale',terms:['scaled softmax','scale softmax','attention scale']},
819
+ {key:'swiglu',terms:['swiglu','swi glu']},
820
+ {key:'geglu',terms:['geglu','ge glu']},
821
+ {key:'softmax',terms:['softmax']}
822
+ ];
823
+ const ACT_TERMS=[
824
+ {key:'gelu_erf',terms:['gelu_erf','exact gelu','erf gelu']},
825
+ {key:'relu2',terms:['relu2','relu squared','squared relu','square relu']},
826
+ {key:'leaky_relu',terms:['leaky_relu','leaky relu','leaky']},
827
+ {key:'silu',terms:['silu','swish','swishes']},
828
+ {key:'sigmoid',terms:['sigmoid']},
829
+ {key:'gelu',terms:['gelu']},
830
+ {key:'relu',terms:['relu']},
831
+ {key:'tanh',terms:['tanh']},
832
+ {key:'elu',terms:['elu']},
833
+ {key:'mish',terms:['mish']}
834
+ ];
835
+ function termMatches(text,items){
836
+ const found=[];
837
+ items.forEach(item=>{
838
+ let best=-1, bestLen=0;
839
+ item.terms.forEach(term=>{
840
+ const ix=text.indexOf(term);
841
+ if(ix>=0&&(best<0||ix<best||(ix===best&&term.length>bestLen))){best=ix;bestLen=term.length;}
842
+ });
843
+ if(best>=0)found.push({key:item.key,ix:best,len:bestLen});
844
+ });
845
+ return found.sort((a,b)=>a.ix-b.ix||b.len-a.len);
846
+ }
847
+ function resolveBrief(text){
848
+ const raw=text||'', t=raw.toLowerCase();
849
+ const classics=termMatches(t,CLASSIC_TERMS);
850
+ if(classics.length){
851
+ const key=classics[0].key;
852
+ return {unknown:false,label:CLASSICS[key].lbl,recipe:{mode:'classic',norm:'rmsnorm',residual:false,acts:[],classic:key,spark:raw}};
853
+ }
854
+ const normHit=t.includes('layernorm')||t.includes('layer norm')?'layernorm':(t.includes('rmsnorm')||t.includes('rms norm')||/\brms\b/.test(t)?'rmsnorm':null);
855
+ const residual=/residual|resid|\bskip\b|add input|\+ input/.test(t);
856
+ const acts=[];
857
+ termMatches(t,ACT_TERMS).forEach(m=>{if(!acts.includes(m.key)&&acts.length<2)acts.push(m.key);});
858
+ if(!normHit&&!acts.length){
859
+ return {unknown:true,message:'Could not map this brief. Use RMSNorm, LayerNorm, residual, and one or two supported activations, or a named op such as softmax, scaled softmax, SwiGLU, or GeGLU.'};
860
+ }
861
+ if(!acts.length){
862
+ if(normHit&&!residual){
863
+ return {unknown:false,label:CLASSICS[normHit].lbl,recipe:{mode:'classic',norm:normHit,residual:false,acts:[],classic:normHit,spark:raw}};
864
+ }
865
+ return {unknown:true,message:'This brief needs a supported activation after the residual or normalization so the referee can verify it.'};
866
+ }
867
+ const recipe={mode:'build',norm:normHit||'rmsnorm',residual,acts,classic:'softmax',spark:raw};
868
+ return {unknown:false,label:labelRecipe(recipe),recipe};
869
+ }
870
+ function resolveExpert(){
871
+ const box=$('#km-xbrief'), res=$('#km-xresolved'), out=res?res.querySelector('.v'):null, btn=$('#km-xbuild');
872
+ if(!box||!res||!out)return;
873
+ resolvedExpert=resolveBrief(box.value);
874
+ res.classList.toggle('unknown',!!resolvedExpert.unknown);
875
+ out.textContent=resolvedExpert.unknown?resolvedExpert.message:resolvedExpert.label;
876
+ if(btn)btn.disabled=!!resolvedExpert.unknown;
877
+ }
878
+ const OP_TAGS=[
879
+ ['softmax','softmax over each row'],
880
+ ['scaled_softmax','scaled softmax over each row'],
881
+ ['swiglu','SwiGLU gated feed forward'],
882
+ ['geglu','GeGLU gated feed forward'],
883
+ ['rmsnorm','RMSNorm only'],
884
+ ['layernorm','LayerNorm only'],
885
+ ['add_rmsnorm_gelu','fused RMSNorm with residual and GELU'],
886
+ ['layernorm_silu','fused LayerNorm with SiLU'],
887
+ ['rmsnorm_mish','fused RMSNorm with Mish'],
888
+ ['add_layernorm_silu','fused LayerNorm with residual and SiLU'],
889
+ ['rmsnorm_gelu_relu2','fused RMSNorm with GELU then squared ReLU']
890
+ ].concat(Object.keys(ACTS).map(k=>[k,ACTS[k].lbl+' activation']));
891
+ function renderOpTags(sel,interactive){
892
+ const wrap=$(sel);
893
+ if(!wrap)return;
894
+ wrap.innerHTML='';
895
+ OP_TAGS.forEach(([label,brief])=>{
896
+ const b=document.createElement('button');
897
+ b.type='button';
898
+ b.className='km-optag';
899
+ b.textContent=label;
900
+ b.dataset.brief=brief;
901
+ if(interactive)b.addEventListener('click',()=>{
902
+ const box=$('#km-xbrief');
903
+ if(box){box.value=brief;box.dispatchEvent(new Event('input',{bubbles:true}));box.focus();}
904
+ });
905
+ wrap.appendChild(b);
906
+ });
907
+ }
908
+
909
+ function renderGlossary(){
910
+ const lb=$('#km-learn-blocks');
911
+ if(!lb)return;
912
+ const rows=[['Residual',MEM.info],...Object.values(NORMS).map(v=>[v.lbl,v.info]),...Object.values(ACTS).map(v=>[v.lbl,v.info]),...Object.values(CLASSICS).map(v=>[v.lbl,v.info])];
913
+ lb.innerHTML=rows.map(([n,d])=>"<div class='km-gloss'><b>"+esc(n)+"</b><span>"+esc(d)+"</span></div>").join('');
914
+ }
915
+ function setupCopyButtons(){
916
+ $$('.km-copy').forEach(btn=>btn.addEventListener('click',()=>{
917
+ const block=btn.closest('.km-codeblock');
918
+ if(!block)return;
919
+ const clone=block.cloneNode(true), copy=clone.querySelector('.km-copy');
920
+ if(copy)copy.remove();
921
+ const text=clone.textContent.trim();
922
+ const done=()=>{const old=btn.textContent;btn.textContent='Copied';setTimeout(()=>{btn.textContent=old;},900);};
923
+ if(navigator.clipboard&&navigator.clipboard.writeText)navigator.clipboard.writeText(text).then(done).catch(done);
924
+ else done();
925
+ }));
926
+ }
927
+
928
+ root.addEventListener('click',e=>{
929
+ const b=e.target.closest('[data-replay-key]');
930
+ if(!b||!root.contains(b))return;
931
+ const key=b.dataset.replayKey;
932
+ if(!REPLAYS[key])return;
933
+ e.preventDefault();
934
+ startReplay(key);
935
+ });
936
+ const xb=$('#km-xbrief');
937
+ if(xb)xb.addEventListener('input',resolveExpert);
938
+ const buildBtn=$('#km-build');
939
+ if(buildBtn)buildBtn.addEventListener('click',()=>{
940
+ lastMintedMode='build';
941
+ const r=buildRecipe();
942
+ animate(buildBtn,[{transform:'scale(0.97)'},{transform:'scale(1)'}],{duration:140,easing:'cubic-bezier(0.16,1,0.3,1)'});
943
+ play();
944
+ startBuild('build',r);
945
+ bridge(r);
946
+ });
947
+ const xBuild=$('#km-xbuild');
948
+ if(xBuild)xBuild.addEventListener('click',()=>{
949
+ resolveExpert();
950
+ const r=expertRecipe();
951
+ if(!r)return;
952
+ lastMintedMode='expert';
953
+ animate(xBuild,[{transform:'scale(0.97)'},{transform:'scale(1)'}],{duration:140,easing:'cubic-bezier(0.16,1,0.3,1)'});
954
+ startBuild('expert',r);
955
+ bridge(r);
956
+ });
957
+
958
+ setupStepper('#km-tries','#km-tries-v',4,v=>{buildTries=v;});
959
+ setupStepper('#km-xtries','#km-xtries-v',5,v=>{expertTries=v;});
960
+ setupTemp('#km-temp','#km-temp-v');
961
+ setupTemp('#km-xtemp','#km-xtemp-v');
962
+ renderPickers();
963
+ renderClassics();
964
+ renderReplayChoices();
965
+ renderOpTags('#km-oplist',true);
966
+ renderOpTags('#km-docs-ops',false);
967
+ renderGlossary();
968
+ setupCopyButtons();
969
+ buildSvg();
970
+ setInfo(machineInfo());
971
+ resolveExpert();
972
+ showTab('build');
973
+ render();
974
  }
975
  """
976