Spaces:
Running

nao1357's picture
Publish verified recorded-trace explorer v0.1.0
57f9b3a verified
Raw History Blame Contribute Delete
7.46 kB
"use strict";
(() => {
const $ = (id) => document.getElementById(id);
const cache = new Map();
const inflight = new Map();
let trace = null;
let timer = null;
let generation = 0;
let time = 0;
let horizon = 1;
let shownGrids = {};
window.SMK_TRACE = (id, value) => {
cache.set(id, value);
const pending = inflight.get(id);
if (pending) { pending.resolve(value); inflight.delete(id); }
};
const entryFor = (id) => window.SMK_CATALOG.entries.find((x) => x.id === id);
function requestTrace(id) {
if (cache.has(id)) return Promise.resolve(cache.get(id));
if (inflight.has(id)) return inflight.get(id).promise;
const pending = {};
pending.promise = new Promise((resolve, reject) => {pending.resolve=resolve;pending.reject=reject;});
inflight.set(id,pending);
const entry=entryFor(id);
if (!entry) { pending.reject(new Error("Trace not in catalog")); return pending.promise; }
const script=document.createElement("script");
script.src=entry.file;
script.onload=() => { if (!cache.has(id)) {inflight.delete(id); pending.reject(new Error("Invalid trace payload"));} script.remove(); };
script.onerror=() => { inflight.delete(id); pending.reject(new Error("Could not load recorded trace")); script.remove(); };
document.head.appendChild(script);
return pending.promise;
}
function stop() {
if(timer!==null) clearInterval(timer);
timer=null;
$("play").textContent="▶ Play";
$("play").setAttribute("aria-label","Play recorded trajectory");
}
function play() {
if (!trace) return;
if(timer!==null) {stop();return;}
if(time>=48) {time=0;render();}
$("play").textContent="Ⅱ Pause";
$("play").setAttribute("aria-label","Pause recorded trajectory");
timer=setInterval(() => { if(time>=48){stop();return;} time++;render();if(time>=48)stop(); },Number($("speed").value));
}
function gray(id, values) {
const c=$(id), ctx=c.getContext("2d");
for(let i=0;i<256;i++) {const x=values[i];ctx.fillStyle=`rgb(${x},${x},${x})`;ctx.fillRect((i%16)*16,Math.floor(i/16)*16,16,16);}
}
function color(v) {
const x=Math.min(1,Math.abs(v));
const neutral=[240,243,235], signed=v<0?[169,93,55]:[20,133,126];
return `rgb(${neutral.map((a,i)=>Math.round(a+(signed[i]-a)*x)).join(",")})`;
}
function grid(id, values, unavailable) {
const c=$(id), ctx=c.getContext("2d");
shownGrids[id]=unavailable?null:values;
ctx.clearRect(0,0,c.width,c.height);
for(let i=0;i<128;i++){ctx.fillStyle=unavailable?"#e5e9e1":color(values[i]);ctx.fillRect((i%16)*32,Math.floor(i/16)*32,31,31);}
if(unavailable){ctx.fillStyle="#405550";ctx.textAlign="center";ctx.font="16px system-ui";ctx.fillText(unavailable,256,132);}
}
function summary(values) {
return `min ${Math.min(...values).toFixed(3)} · max ${Math.max(...values).toFixed(3)} · mean |value| ${(values.reduce((s,x)=>s+Math.abs(x),0)/128).toFixed(3)}`;
}
function render() {
if(!trace)return;
const is5=horizon===5, p=is5?"prediction_image_h5":"prediction_image", target=is5?"truth_image_h5":"truth_image", z=is5?"z5":"z1";
$("step").value=String(time);$("time").value=`${time} / 48`;
gray("observation",trace.observation_image[time]);gray("prediction",trace[p][time]);gray("truth",trace[target][time]);
$("observation-label").textContent=`o(${time}) · 16 × 16 grayscale`;
$("prediction-label").textContent=`predicted o(${time+horizon}) from h(${time})`;
$("truth-label").textContent=`actual o(${time+horizon}) · withheld target`;
$("mse").textContent=trace[is5?"mse5":"mse1"][time].toFixed(6);
$("copy").textContent=trace[is5?"copy_mse5":"copy_mse1"][time].toFixed(6);
$("actions").textContent=trace[is5?"planned_action_h5":"planned_action_h1"][time].join(" → ");
grid("hidden",trace.h[time],false);grid("prior",trace.prior[time],!trace.prior_computed[time]?"Not computed":trace.prior[time]===null?"Not adopted · value masked":false);grid("imagined",trace[z][time],false);
$("h-summary").textContent=summary(trace.h[time]);$("z-summary").textContent=summary(trace[z][time]);
$("z-label").textContent=is5?"z₅(t)":"z₁(t)";
$("prior-label").textContent=!trace.prior_computed[time]?"Separate update: no observation prior computed.":trace.prior_applied[time]?`Previous action → prior; adopted before o(${time}).`:"Episode start: computed but NOT adopted.";
$("prior-summary").textContent=trace.prior[time]!==null?summary(trace.prior[time]):"Value unavailable in the saved trace; no fabricated activation.";
}
function setProvenance(entry) {
const rows=[["Model",trace.model],["Route / case",`${trace.route} / ${trace.case_index}`],["Source NPZ SHA-256",entry.source_sha256],["Source checkpoint SHA-256",entry.checkpoint_sha256],["Display payload SHA-256",entry.payload_sha256],["Source manifest SHA-256",window.SMK_CATALOG.source_manifest_sha256]];
const list=$("provenance");list.replaceChildren();
for(const [label,value] of rows){const dt=document.createElement("dt"),dd=document.createElement("dd");dt.textContent=label;dd.textContent=value;list.append(dt,dd);}
}
async function changeTrace() {
stop();trace=null;
const token=++generation,id=`${$("route").value}/${$("arm").value}_s${$("seed").value}`;
$("status").textContent="Loading recorded trace…";$("play").disabled=true;
for(const canvasId of ["observation","prediction","truth","hidden","prior","imagined"]){const c=$(canvasId),ctx=c.getContext("2d");ctx.fillStyle="#eef1ec";ctx.fillRect(0,0,c.width,c.height);}
shownGrids={};$("provenance").replaceChildren();
for(const labelId of ["mse","copy","actions","h-summary","prior-summary","z-summary","prior-label"])$(labelId).textContent="—";
try {
const value=await requestTrace(id);
if(token!==generation)return;
trace=value;$("status").textContent=`${trace.model} · ${trace.route} · trained`;
$("play").disabled=false;setProvenance(entryFor(id));render();
} catch(err) {
if(token!==generation)return;
$("status").textContent="Trace loading failed. Reload the page or choose another trace.";
}
}
for(const id of ["arm","seed","route"])$(id).addEventListener("change",changeTrace);
$("horizon").addEventListener("change",()=>{horizon=Number($("horizon").value);render();});
$("step").addEventListener("input",()=>{stop();time=Number($("step").value);render();});
$("play").addEventListener("click",play);
$("back").addEventListener("click",()=>{stop();time=Math.max(0,time-1);render();});
$("next").addEventListener("click",()=>{stop();time=Math.min(48,time+1);render();});
$("speed").addEventListener("change",()=>{if(timer!==null){stop();play();}});
document.addEventListener("visibilitychange",()=>{if(document.hidden)stop();});
for(const id of ["hidden","prior","imagined"])$(id).addEventListener("mousemove",(event)=>{
const values=shownGrids[id];if(!values){$(id).title=trace&&trace.prior_computed[time]?"Computed but not adopted; saved value masked.":"No displayed value: not computed or loading.";return;}
const bounds=$(id).getBoundingClientRect(),x=Math.min(15,Math.max(0,Math.floor((event.clientX-bounds.left)/bounds.width*16))),y=Math.min(7,Math.max(0,Math.floor((event.clientY-bounds.top)/bounds.height*8))),index=y*16+x;
$(id).title=`Feature ${index}: ${values[index].toFixed(6)} (display value)`;
});
changeTrace();
})();