tianxuan / src /ml /debug-gen2.ts
masterofthestars's picture
Tianxuan: full project + data (checkpoints, world_model, data without raw) with correct directory structure
db7bc06
Raw History Blame Contribute Delete
1.66 kB
import * as path from "path";
import { Engine } from "./engine";
async function main(): Promise<void> {
const engine = Engine.load(path.resolve("sandbox/ck-full-multi"));
// 直接测试 Titans 记忆
const titansMem = (engine as any).titansMem;
console.log("TitansMem slots:", titansMem.cfg.slots);
console.log("TitansMem chars (first 10):", titansMem.chars.slice(0, 10));
console.log("TitansMem occupancy:", titansMem.occupancy().toFixed(2));
// 测试 recallAt
const promptIds = engine.tokenizer.encode("小明");
const ctxWin = promptIds.slice(-engine.model.cfg.ctx);
console.log("ctxWin:", ctxWin);
const q = Float64Array.from(engine.model.embedAvg(ctxWin));
const cache = { query: new Float64Array(0), keys: new Float64Array(0), values: new Float64Array(0), weights: new Float64Array(0) };
const mem = titansMem.read(q, cache);
console.log("hitSlots:", mem.hitSlots);
console.log("weights (first 10):", cache.weights.slice(0, 10));
// 找到权重最大的槽位
let best = 0;
for (let i = 1; i < cache.weights.length; i++) {
if (cache.weights[i] > cache.weights[best]) best = i;
}
console.log("best slot:", best, "weight:", cache.weights[best]);
console.log("chars[best]:", titansMem.chars[best]);
console.log("chars[best % slots]:", titansMem.chars[best % titansMem.cfg.slots]);
// 测试生成
const res = await engine.generate({ prompt: "小明", maxLen: 20, budget: 3, temperature: 0.85, topK: 5 });
console.log("\n生成结果:", res.text);
console.log("路由统计:", res.routes.map(r => `L${r.depth}:${r.char}`).join(""));
}
main().catch(err => console.error(err));