inside-the-router / src /trace.js
misalignedandmad's picture
Clarify prompt and token provenance
1e9146f verified
Raw History Blame Contribute Delete
3.38 kB
export const GPT_OSS = Object.freeze({
model: "openai/gpt-oss-20b",
layers: 24,
experts: 32,
topK: 4,
hiddenSize: 2880,
expertIntermediateSize: 2880,
attentionHeads: 64,
vocabSize: 201088,
slidingWindow: 128,
fullAttentionEvery: 2,
});
function hash(text) {
let value = 2166136261;
for (let index = 0; index < text.length; index += 1) {
value ^= text.charCodeAt(index);
value = Math.imul(value, 16777619);
}
return value >>> 0;
}
function random(seed) {
let state = seed || 1;
return () => {
state ^= state << 13;
state ^= state >>> 17;
state ^= state << 5;
return (state >>> 0) / 4294967296;
};
}
export function tokenize(sentence) {
return sentence
.trim()
.match(/[A-Za-z]+(?:'[A-Za-z]+)?|\d+|[^\s\w]/g)
?.map((text, index) => ({
text: index === 0 || /^[^A-Za-z0-9]/.test(text) ? text : ` ${text}`,
id: hash(`${text}:${index}`) % GPT_OSS.vocabSize,
index,
})) ?? [];
}
export function routeToken(token, layer) {
const rng = random(hash(`${token.id}:${token.text}:${layer}`));
const chosen = new Set();
const values = [];
while (values.length < GPT_OSS.topK) {
const expert = Math.floor(rng() * GPT_OSS.experts);
if (chosen.has(expert)) continue;
chosen.add(expert);
values.push({ expert, raw: 0.25 + rng() * 1.5 });
}
const total = values.reduce((sum, item) => sum + item.raw, 0);
return values
.map(({ expert, raw }) => ({ expert, weight: raw / total }))
.sort((a, b) => b.weight - a.weight);
}
export function buildTrace(sentence) {
const tokens = tokenize(sentence).slice(0, 14);
const paragraph = "Moonlight is reflected sunlight, so it does not physically carry extra cold; the chill is a judgment assembled by contrast. At night the air is often cooler, the bright disk looks silver against a dark sky, and our visual system associates its blue-white palette with ice and shadow. The model builds an answer in much the same incremental way: each next token is a small probability-weighted decision, repeated until a complete thought seems to have arrived.";
return {
schema: "inside-the-router-teaching-trace-v1",
provenance: "deterministic simulated routing; not captured model activations",
model: GPT_OSS,
prompt: sentence,
tokens: tokens.map((token) => ({
...token,
origin: "user",
layers: Array.from({ length: GPT_OSS.layers }, (_, layer) => ({
layer,
attention: (layer + 1) % GPT_OSS.fullAttentionEvery === 0 ? "full" : "sliding",
routes: routeToken(token, layer),
})),
})),
output_text: paragraph,
decode: tokenize(paragraph).map((token) => token.text),
};
}
export async function fetchLiveTrace(sentence, options = {}) {
const baseUrl = options.baseUrl ?? import.meta.env.VITE_TRACE_API ?? "";
const token = options.token ?? import.meta.env.VITE_TRACE_TOKEN;
const maxNewTokens = options.maxNewTokens ?? 144;
const response = await fetch(`${baseUrl.replace(/\/$/, "")}/api/v1/traces`, {
method: "POST",
headers: {
"content-type": "application/json",
...(token ? { authorization: `Bearer ${token}` } : {}),
},
body: JSON.stringify({ prompt: sentence, max_new_tokens: maxNewTokens }),
});
if (!response.ok) throw new Error(`Trace service returned ${response.status}`);
return response.json();
}