tianxuan / scripts /start-training-v1.js
masterofthestars's picture
Tianxuan: full project + data (checkpoints, world_model, data without raw) with correct directory structure
db7bc06
Raw History Blame Contribute Delete
4.19 kB
#!/usr/bin/env node
/**
* 天玄 TianXuan - 启动训练并实时监控
*/
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const CHECKPOINT_DIR = 'data/checkpoints_v0.000001';
const TARGET_STEPS = 2000;
const POLL_INTERVAL = 5000; // 5秒轮询
console.log('='.repeat(70));
console.log('天玄 TianXuan - 启动训练 (v0.000001)');
console.log('='.repeat(70));
console.log('');
console.log('训练配置:');
console.log(' 语料长度: 10,000,000 字符');
console.log(' Epoch 数: 10');
console.log(' STDP: 启用 (rate=0.001)');
console.log(' STDA: 启用 (rate=0.0005)');
console.log(' 检查点: ' + CHECKPOINT_DIR);
console.log('');
// 启动训练进程
const trainCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const trainArgs = ['run', 'train', '--',
'--realcorpus', '0',
'--tokens', '10000000',
'--epochs', '10',
'--stdp', '1',
'--stda', '1',
'--out', CHECKPOINT_DIR,
'--seed', '42'
];
const train = spawn(trainCmd, trainArgs, {
cwd: process.cwd(),
stdio: 'inherit'
});
let lastBatch = 0;
let startTime = Date.now();
console.log('');
console.log('【训练已启动,开始监控...】');
console.log('');
// 监控函数
function poll() {
const metaPath = path.join(process.cwd(), CHECKPOINT_DIR, 'meta.json');
if (!fs.existsSync(metaPath)) {
return;
}
try {
const meta = JSON.parse(fs.readFileSync(metaPath, 'utf8'));
const currentBatch = meta.t || 0;
if (currentBatch !== lastBatch && currentBatch > 0) {
const elapsed = Date.now() - startTime;
const bps = currentBatch / (elapsed / 1000);
const progress = Math.min(100, (currentBatch / TARGET_STEPS) * 100);
console.log(`\n[${new Date().toLocaleTimeString('zh-CN')}] 进度: ${currentBatch}/${TARGET_STEPS} (${progress.toFixed(1)}%)`);
console.log(` 速度: ${bps.toFixed(1)} 步/秒 | 已用: ${Math.floor(elapsed/1000)}秒`);
// STDP 状态
if (meta.stdpState) {
const ratio = meta.stdpState.ltpSignalSum / Math.max(meta.stdpState.ltdSignalSum, 0.001);
console.log(` STDP: ${meta.stdpState.stepCount}次更新 | LTP/LTD=${ratio.toFixed(2)}`);
}
// STDA 状态
if (meta.stdaState) {
console.log(` STDA: 阈值偏移=${meta.stdaState.thresholdShift.toFixed(4)} | 激活=${meta.stdaState.actAvg.toFixed(4)}`);
}
// Titans 状态
if (meta.titansWriteLog) {
console.log(` Titans: ${meta.titansWriteLog.length}条记录`);
}
// Replay 状态
if (meta.replayState) {
console.log(` Replay: ${meta.replayState.buffer?.length}/${meta.replayState.maxCapacity}`);
}
// 生成测试
if (currentBatch > 0 && currentBatch % 100 === 0) {
console.log(` [生成测试]`);
try {
const { execSync } = require('child_process');
const out = execSync(`node sandbox/dist/ml/generate.js --checkpoint ${CHECKPOINT_DIR} --prompt "小明" --len 50`, {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'pipe']
});
const lines = out.split('\n').filter(l => l.trim());
const textLine = lines.find(l => l.startsWith('小明'));
if (textLine) {
const uniqueChars = new Set(textLine.split('')).size;
console.log(` "${textLine.slice(0, 30)}..." → 唯一字符: ${uniqueChars}`);
}
} catch (e) {
// 静默失败
}
}
lastBatch = currentBatch;
// 检查完成
if (currentBatch >= TARGET_STEPS) {
console.log('\n✅ 训练完成!');
process.exit(0);
}
}
} catch (e) {
// 静默忽略解析错误
}
}
// 启动轮询
const interval = setInterval(poll, POLL_INTERVAL);
// 处理训练进程退出
train.on('close', (code) => {
clearInterval(interval);
console.log(`\n训练进程已退出 (code: ${code})`);
process.exit(code);
});
// 处理中断
process.on('SIGINT', () => {
console.log('\n\n正在停止训练...');
train.kill('SIGINT');
clearInterval(interval);
process.exit(0);
});