homunculus / code /falcon_demo /index.html
LiteMind's picture
Sync mobile+console fixes from Space
d3dd96f verified
Raw History Blame Contribute Delete
10.4 kB
<!doctype html>
<html lang="en" dir="ltr"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Falcon-H1 90M: exact widening, no training</title>
<style>
:root{--bg:#0d1117;--card:#161b22;--line:#30363d;--fg:#e6edf3;--mut:#8b949e;--ok:#3fb950;--bad:#f85149;--acc:#58a6ff}
*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--fg);font:15px/1.6 system-ui,Segoe UI,Tahoma,sans-serif}
main{max-width:1100px;margin:0 auto;padding:18px}h1{font-size:21px;margin:.2em 0}h2{font-size:15px;margin:0 0 8px;color:var(--acc)}
.card{background:var(--card);border:1px solid var(--line);border-radius:10px;padding:14px;margin:12px 0}
button{background:#238636;color:#fff;border:0;border-radius:7px;padding:8px 16px;font:inherit;cursor:pointer}button:disabled{opacity:.45;cursor:default}
button.sec{background:#30363d}input[type=text],textarea,select{background:#0d1117;color:var(--fg);border:1px solid var(--line);border-radius:7px;padding:7px;font:inherit}
textarea{width:100%;direction:auto}label{margin-inline-end:14px;white-space:nowrap}.mut{color:var(--mut);font-size:13px}
.cols{display:grid;grid-template-columns:1fr 1fr;gap:12px}@media(max-width:760px){.cols{grid-template-columns:1fr}}
.out{min-height:120px;white-space:pre-wrap;direction:auto;background:#0d1117;border:1px solid var(--line);border-radius:8px;padding:10px;font-family:ui-monospace,Consolas,monospace;font-size:14px}
.bar{height:8px;background:#21262d;border-radius:4px;overflow:hidden}.bar>i{display:block;height:100%;width:0;background:var(--acc)}
.big{font-size:19px;font-weight:600}.ok{color:var(--ok)}.bad{color:var(--bad)}table{border-collapse:collapse;width:100%}td,th{border-bottom:1px solid var(--line);padding:4px 8px;text-align:start;font-size:13px}
@media(max-width:760px){main{padding:12px}label{white-space:normal;display:inline-block;padding:6px 0}input[type=checkbox]{width:18px;height:18px;vertical-align:middle}button,select,input[type=text]{min-height:40px}.card{overflow-x:auto}}
</style></head><body><main>
<h1>Falcon-H1-Tiny-90M: same model, wider, no training</h1>
<div class="mut">The official Falcon model from TII (91 million parameters, hybrid attention + Mamba2 + MLP). The page downloads the original weights from Hugging Face, widens them in the browser by copying units and dividing the outgoing weights by the number of copies (no noise, no training), then generates text with both models side by side and measures the logit difference at every step. The engine is plain JavaScript and agrees with HF transformers to about 1e-5 to 1e-4 on logits of magnitude tens.</div>
<div class="card"><h2>1) Load the original model</h2>
<button id="load">Load weights (182 MB from Hugging Face)</button> <span id="lstat" class="mut"></span>
<div class="bar" style="margin-top:8px"><i id="lbar"></i></div></div>
<div class="card"><h2>2) Widen (zero training)</h2>
<div>
<label><input type="checkbox" class="ax" value="mlp" checked> MLP width</label>
<label><input type="checkbox" class="ax" value="attn" checked> attention heads (GQA)</label>
<label><input type="checkbox" class="ax" value="heads" checked> Mamba heads</label>
<label><input type="checkbox" class="ax" value="dstate" checked> Mamba state size</label>
<label><input type="checkbox" class="ax" value="res"> residual stream (d_model, multiplies memory)</label>
<label>Factor k: <select id="k"><option>2</option><option>3</option><option>4</option></select></label>
<button id="widen" disabled>Widen</button></div>
<div id="wstat" class="mut" style="margin-top:8px"></div>
<div class="mut">Not widened: head_dim (RoPE depends on it), the number of layers, the vocabulary size. These are limits of the method. Note: without symmetry-breaking noise, duplicated units receive identical gradients and stay identical, so they add no learning capacity (this follows from identical gradients; our numerical check of it is unconfirmed, see paper 5).</div></div>
<div class="card"><h2>3) Try it yourself</h2>
<textarea id="prompt" rows="2">Why is the sky blue? Answer in two sentences.</textarea>
<div style="margin-top:8px"><label><input type="checkbox" id="chat" checked> Chat template (Instruct)</label>
<label>Max tokens: <input type="text" id="maxn" value="40" size="3"></label>
<button id="gen" disabled>Generate with both</button> <button id="stop" class="sec" disabled>Stop</button></div>
<div class="cols" style="margin-top:10px"><div><b>Original</b> <span class="mut" id="ostat"></span><div class="out" id="oout"></div></div>
<div><b>Widened</b> <span class="mut" id="wstat2"></span><div class="out" id="wout"></div></div></div>
<div id="verdict" class="big" style="margin-top:10px"></div>
<div id="live" class="mut"></div></div>
<div class="card"><h2>Check log</h2><table id="log"><tr><th>Prompt</th><th>Widening</th><th>Identical tokens</th><th>Max logit diff</th><th>argmax</th></tr></table></div>
<div class="mut">Source: <a style="color:var(--acc)" href="https://huggingface.co/tiiuae/Falcon-H1-Tiny-90M-Instruct">tiiuae/Falcon-H1-Tiny-90M-Instruct</a>. The very small difference (about 1e-4 on logits of magnitude tens) is of the same size as the float32 versus float64 difference of the original model (paper 5).</div>
</main>
<script type="module">
import {parseSafetensors,buildModel,widen,newState,step,argmax} from './engine.js';
import {makeTokenizer} from './tokenizer.js';
const HF='https://huggingface.co/tiiuae/Falcon-H1-Tiny-90M-Instruct/resolve/main/';
const $=id=>document.getElementById(id);let base=null,wide=null,tok=null,cfg=null,stopFlag=false,wdesc='';
const nparams=M=>{let n=M.E.length+M.fn.length;for(const l of M.L)for(const v of Object.values(l))n+=v.length;return n};
const fmt=n=>(n/1e6).toFixed(1)+'M';
async function getJSON(u){const r=await fetch(u);if(!r.ok)throw new Error('HTTP '+r.status+' '+u);return await r.json()}
$('load').onclick=async()=>{
$('load').disabled=true;$('lstat').textContent='Loading config and tokenizer...';
cfg=await getJSON(HF+'config.json');tok=makeTokenizer(await getJSON(HF+'tokenizer.json'));
let r;try{r=await fetch(HF+'model.safetensors')}catch(e){r=await fetch('https://hf-mirror.com/tiiuae/Falcon-H1-Tiny-90M-Instruct/resolve/main/model.safetensors')}const tot=+r.headers.get('content-length')||182304120,rd=r.body.getReader(),buf=new Uint8Array(tot);let got=0;
for(;;){const {done,value}=await rd.read();if(done)break;buf.set(value,got);got+=value.length;$('lbar').style.width=(100*got/tot)+'%';$('lstat').textContent=(got/1e6).toFixed(0)+' / '+(tot/1e6).toFixed(0)+' MB'}
$('lstat').textContent='Converting weights...';await new Promise(r=>setTimeout(r,20));
base=buildModel(parseSafetensors(buf.buffer),cfg);
$('lstat').innerHTML='<span class="ok">Ready: original model '+fmt(nparams(base))+' parameters.</span>';$('widen').disabled=false;
};
$('widen').onclick=async()=>{
const axes=[...document.querySelectorAll('.ax:checked')].map(e=>e.value),k=+$('k').value;
if(!axes.length){$('wstat').textContent='Select at least one axis';return}
$('widen').disabled=true;$('wstat').textContent='Widening...';await new Promise(r=>setTimeout(r,30));
const t=performance.now();wide=null;wide=widen(base,axes,k);wdesc=axes.join('+')+' x'+k;
$('wstat').innerHTML='<span class="ok">Done in '+((performance.now()-t)/1000).toFixed(1)+' s: '+fmt(nparams(base))+' ← <b>'+fmt(nparams(wide))+'</b> parameters ('+wdesc+'). No training, no noise.</span>';
$('widen').disabled=false;$('gen').disabled=false;
};
$('stop').onclick=()=>stopFlag=true;
const maxdiff=(a,b)=>{let m=0;for(let j=0;j<a.length;j++){const d=Math.abs(a[j]-b[j]);if(d>m)m=d}return m};
$('gen').onclick=async()=>{
const q=$('prompt').value.trim();if(!q)return;const maxn=Math.max(1,Math.min(200,+$('maxn').value||40));
const text=$('chat').checked?'\n<|im_start|>user\n'+q+'<|im_end|>\n<|im_start|>assistant\n':q;
const ids=tok.encode(text);$('gen').disabled=true;$('stop').disabled=false;stopFlag=false;$('oout').textContent='';$('wout').textContent='';$('verdict').textContent='';
const A=newState(base),B=newState(wide);let la,lb;const ga=[],gb=[];let maxd=0,amSame=true,tO=0,tW=0,doneA=false,doneB=false;
for(let i=0;i<ids.length;i++){let t=performance.now();la=step(base,A,ids[i]);tO+=performance.now()-t;t=performance.now();lb=step(wide,B,ids[i]);tW+=performance.now()-t;
maxd=Math.max(maxd,maxdiff(la,lb));if(argmax(la)!==argmax(lb))amSame=false;
if(i%4==0){$('live').textContent='Reading prompt '+(i+1)+'/'+ids.length;await new Promise(r=>setTimeout(r))}}
for(let n=0;n<maxn&&!stopFlag&&(!doneA||!doneB);n++){
const ta=argmax(la),tb=argmax(lb);
if(!doneA){ga.push(ta);if(ta===tok.imEnd||ta===tok.eos)doneA=true}
if(!doneB){gb.push(tb);if(tb===tok.imEnd||tb===tok.eos)doneB=true}
$('oout').textContent=tok.decode(ga);$('wout').textContent=tok.decode(gb);
if(doneA&&doneB)break;
let t=performance.now();if(!doneA)la=step(base,A,ta);tO+=performance.now()-t;t=performance.now();if(!doneB)lb=step(wide,B,tb);tW+=performance.now()-t;
if(!doneA&&!doneB){maxd=Math.max(maxd,maxdiff(la,lb));if(argmax(la)!==argmax(lb))amSame=false}
$('live').textContent='token '+(n+1)+' | max logit diff so far: '+maxd.toExponential(2);await new Promise(r=>setTimeout(r));
}
let same=0;const L=Math.min(ga.length,gb.length);for(let i=0;i<L;i++)if(ga[i]===gb[i])same++;
const ident=ga.length===gb.length&&same===ga.length;
$('verdict').innerHTML=ident?'<span class="ok">✔ Texts identical character by character ('+ga.length+'/'+ga.length+' tokens)</span>':'<span class="bad">✘ Different: '+same+'/'+Math.max(ga.length,gb.length)+' tokens identical</span>';
$('live').textContent='max logit diff over all steps: '+maxd.toExponential(2)+' | original speed '+(tO/Math.max(1,ga.length+ids.length)).toFixed(0)+' ms/token, widened '+(tW/Math.max(1,gb.length+ids.length)).toFixed(0)+' ms/token';
$('ostat').textContent='('+ga.length+' tokens)';$('wstat2').textContent='('+gb.length+' tokens)';
const tr=document.createElement('tr');tr.innerHTML='<td></td><td>'+wdesc+'</td><td class="'+(ident?'ok':'bad')+'">'+same+'/'+Math.max(ga.length,gb.length)+'</td><td>'+maxd.toExponential(2)+'</td><td class="'+(amSame?'ok':'bad')+'">'+(amSame?'same top token at every step':'differs')+'</td>';tr.firstChild.textContent=q;$('log').appendChild(tr);
$('gen').disabled=false;$('stop').disabled=true;
};
</script></body></html>