interplane.Get Omega
Read in your language

gpu-head-to-head

aien-dev/gpu-head-to-head

Brought by drakestapleton

branch main Branches Tags History

gpu-head-to-head / q35-9b / tools / test-ordered-weights.mjs

62 lines · 4075 bytes Raw Blame History
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import fs from 'node:fs';
4import os from 'node:os';
5import path from 'node:path';
6import {reorder,widenBf16,mapping} from './ordered-weights.mjs';
7import {ggufCatalog,safetensorsCatalog} from './weight-files.mjs';
8const c={linear_num_key_heads:2,linear_num_value_heads:6,num_hidden_layers:1,linear_value_head_dim:2,linear_key_head_dim:1};
9const words=xs=>{const b=Buffer.alloc(xs.length*2);xs.forEach((x,i)=>b.writeUInt16LE(x,i*2));return b;};
10const unpack=b=>Array.from({length:b.length/2},(_,i)=>b.readUInt16LE(i*2));
11// Golden addresses from the recorded Wolfram reshape/transpose query, not from this implementation.
12const golden=[0,1,6,7,2,3,8,9,4,5,10,11];
13test('row and column permutations match independent Wolfram address fixtures',()=>{
14 const data=words(Array.from({length:12},(_,i)=>i));
15 assert.deepEqual(unpack(reorder(data,[12],2,c,{axis:0,hd:2})),golden);
16 const two=words(Array.from({length:24},(_,i)=>i));
17 assert.deepEqual(unpack(reorder(two,[2,12],2,c,{axis:1,hd:2})),
18 [0,1,6,7,2,3,8,9,4,5,10,11,12,13,18,19,14,15,20,21,16,17,22,23]);
19});
20test('Q/K prefix stays fixed while only V rows move',()=>{
21 const data=words(Array.from({length:16},(_,i)=>i));
22 assert.deepEqual(unpack(reorder(data,[16],2,c,{axis:0,hd:2,prefix:4})),
23 [0,1,2,3,4,5,10,11,6,7,12,13,8,9,14,15]);
24});
25test('same-multiset wrong order is detected rather than accepted',()=>{
26 const src=words(Array.from({length:12},(_,i)=>i)),good=reorder(src,[12],2,c,{axis:0,hd:2});
27 assert.deepEqual(unpack(src).sort((a,b)=>a-b),unpack(good).sort((a,b)=>a-b));
28 assert(!src.equals(good));
29 assert.throws(()=>reorder(src,[12],2,{...c,linear_num_key_heads:4},{axis:0,hd:2}));
30 assert.throws(()=>reorder(src,[11],2,c,{axis:0,hd:2}));
31});
32test('BF16 widening copies bits and malformed bytes fail',()=>{
33 assert.deepEqual([...widenBf16(Buffer.from([0x80,0x3f,0,0x80]))],[0,0,0x80,0x3f,0,0,0,0x80]);
34 assert.throws(()=>widenBf16(Buffer.from([1])));
35});
36test('names and norm roles are explicit, with no unknown text-tensor fallback',()=>{
37 assert.equal(mapping('model.language_model.layers.0.post_attention_layernorm.weight',c).name,'blk.0.post_attention_norm.weight');
38 assert.equal(mapping('model.language_model.layers.0.linear_attn.in_proj_qkv.weight',c).prefix,4);
39 assert.equal(mapping('mtp.fc.weight',c),null);
40 assert.throws(()=>mapping('model.language_model.layers.0.unknown.weight',c));
41});
42const u32=n=>{const b=Buffer.alloc(4);b.writeUInt32LE(n);return b;};
43const u64=n=>{const b=Buffer.alloc(8);b.writeBigUInt64LE(BigInt(n));return b;};
44const str=s=>Buffer.concat([u64(Buffer.byteLength(s)),Buffer.from(s)]);
45test('binary readers accept exact fixtures and refuse truncation or bad types',()=>{
46 const root=fs.mkdtempSync(path.join(os.tmpdir(),'ordered-weight-control-'));
47 try{
48 const file=path.join(root,'model.gguf');
49 const header=Buffer.concat([Buffer.from('GGUF'),u32(3),u64(1),u64(0),str('fixture'),u32(1),u64(12),u32(30),u64(0)]);
50 const pad=Buffer.alloc((32-header.length%32)%32);
51 const bytes=Buffer.concat([header,pad,words(Array.from({length:12},(_,i)=>i))]);
52 fs.writeFileSync(file,bytes);
53 const g=ggufCatalog(file);assert.equal(g.tensors.get('fixture').bytes,24);g.close();
54 fs.writeFileSync(file,bytes.subarray(0,bytes.length-1));assert.throws(()=>ggufCatalog(file));
55 fs.writeFileSync(file,Buffer.from('GGUF'));assert.throws(()=>ggufCatalog(file));
56 const safepath=path.join(root,'fixture.safetensors');
57 const h=Buffer.from(JSON.stringify({fixture:{dtype:'BF16',shape:[12],data_offsets:[0,24]}}));
58 fs.writeFileSync(safepath,Buffer.concat([u64(h.length),h,words(Array.from({length:12},(_,i)=>i))]));
59 const s=safetensorsCatalog(root);assert.equal(s.tensors.size,1);s.close();
60 fs.writeFileSync(safepath,Buffer.concat([u64(h.length),h]));assert.throws(()=>safetensorsCatalog(root));
61 }finally{fs.rmSync(root,{recursive:true,force:true});}
62});