Model-Observatory / experiments /sound.test.cjs
alphabell-research's picture
Add five measured experiments and public CPU reproduction (#3)
c1bec47
Raw History Blame Contribute Delete
3.81 kB
'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const zlib = require('node:zlib');
const path = require('node:path');
const sound = require('./sound.js');
const traces = Object.fromEntries(['flint','qwen'].map(model => [model, JSON.parse(zlib.gunzipSync(fs.readFileSync(path.join(__dirname, '../traces/' + model + '-sky-q8.json.gz'))))]));
test('Flint score preserves all selected expert IDs and exact measured weights', () => {
const score = sound.buildScore(traces.flint, 12, .2);
assert.equal(score.events.length, 59);
for (const event of score.events) {
const block = traces.flint.frames[event.index].layers[12];
assert.deepEqual(event.voices.map(voice => voice.id), block.experts);
assert.deepEqual(event.voices.map(voice => voice.value), block.weights);
event.voices.forEach(voice => assert.equal(voice.amplitude, .11 * Math.sqrt(voice.value)));
}
});
test('Qwen score uses the exact six largest recurrent head RMS values and a capture-wide scale', () => {
const score = sound.buildScore(traces.qwen, 0, .2);
const maximum = Math.max(...traces.qwen.frames.flatMap(frame => frame.layers[0].heads));
assert.equal(score.maximum, maximum);
for (const event of score.events) {
const heads = traces.qwen.frames[event.index].layers[0].heads;
const expected = heads.map((value,id) => ({value,id})).sort((a,b) => b.value-a.value || a.id-b.id).slice(0,6);
assert.deepEqual(event.voices.map(({value,id}) => ({value,id})), expected);
event.voices.forEach(voice => { assert.equal(voice.normalized, voice.value / maximum); assert.equal(voice.waveform, 'triangle'); });
}
});
test('Synthesis is deterministic, non-silent, bounded, and aligned with token duration', () => {
for (const [model, layer] of [['flint',12], ['qwen',0]]) {
const score = sound.buildScore(traces[model], layer, .2), first = sound.renderPCM(score), second = sound.renderPCM(score);
assert.deepEqual(first, second);
assert.equal(first.length, Math.ceil(score.duration * sound.SAMPLE_RATE));
let peak = 0; for (const value of first) { assert.ok(Number.isFinite(value)); peak = Math.max(peak, Math.abs(value)); }
assert.ok(peak > .001 && peak < 1);
assert.equal(first[0], 0); assert.equal(first.at(-1), 0);
}
});
test('WAV encodes actual deterministic signed PCM with the chosen volume', () => {
const pcm = new Float32Array([0, .5, -.5, 1, -1]), buffer = sound.encodeWAV(pcm, .5), view = new DataView(buffer);
assert.equal(Buffer.from(buffer).toString('ascii', 0, 4), 'RIFF');
assert.equal(Buffer.from(buffer).toString('ascii', 8, 12), 'WAVE');
assert.equal(view.getUint32(24,true), 22050); assert.equal(view.getUint16(22,true), 1); assert.equal(view.getUint16(34,true),16);
assert.equal(view.getUint32(40,true), 10); assert.equal(buffer.byteLength,54);
assert.deepEqual(Array.from({length:5}, (_,i) => view.getInt16(44+i*2,true)), [0,8192,-8192,16384,-16384]);
assert.ok(new Int16Array(sound.encodeWAV(pcm,0),44).every(value => value === 0));
});
test('Reject dense Flint, full attention Qwen, excessive duration, and invalid volume', () => {
assert.throws(() => sound.buildScore(traces.flint,0));
const full = traces.qwen.architecture.layer_types.indexOf('full_attention');
assert.throws(() => sound.buildScore(traces.qwen,full));
assert.throws(() => sound.buildScore(traces.flint,12,2));
assert.throws(() => sound.renderPCM({duration:91,sampleRate:22050,events:[]}));
assert.throws(() => sound.encodeWAV(new Float32Array(1),-1));
});
test('ID pitch repeats every 15 IDs and Qwen is one octave above Flint', () => {
assert.equal(sound.frequency(2,'flint'), sound.frequency(17,'flint'));
assert.equal(sound.frequency(2,'qwen'), sound.frequency(2,'flint')*2);
});