File size: 3,818 Bytes
1cc8d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { createHash } from 'node:crypto';
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import {
  parseGguf,
  q4_KDecodeRow,
  q6_KDecodeSourceRow,
  q6_KDecodePackedRow,
  validateManifest,
  type PackageManifest,
} from '@botpsy/artifacts';

const root = new URL('../..', import.meta.url).pathname;
const artifactDir = join(root, 'artifacts/botpsy-0.8b-q4_k_m');
const ggufPath = join(root, 'model-src/gguf/botpsy-0.8b-q4_k_m-imat.gguf');
const available = existsSync(join(artifactDir, 'manifest.json')) && existsSync(ggufPath);

describe.skipIf(!available)('compiled Q4_K_M artifact', () => {
  const manifest = available
    ? validateManifest(JSON.parse(readFileSync(join(artifactDir, 'manifest.json'), 'utf8')) as PackageManifest)
    : ({ shards: [], tensors: [], arenas: [] } as unknown as PackageManifest);
  const ggufBytes = available ? readFileSync(ggufPath) : new Uint8Array();
  const gguf = available ? parseGguf(ggufBytes) : ({ tensors: [] } as unknown as ReturnType<typeof parseGguf>);
  const shards = available
    ? new Map(manifest.shards.map((shard) => [shard.name, new Uint8Array(readFileSync(join(artifactDir, shard.name)))]))
    : new Map<string, Uint8Array>();

  it('declares the calibrated mixed-format profile', () => {
    expect(manifest.numericalProfile.weightFormat).toBe('q4_k_m');
    expect(manifest.tensors.filter((tensor) => tensor.dtype === 'q4_k')).toHaveLength(162);
    expect(manifest.tensors.filter((tensor) => tensor.dtype === 'q6_k')).toHaveLength(25);
    expect(manifest.tensors.filter((tensor) => tensor.dtype === 'f32')).toHaveLength(133);
  });

  it('preserves Q4_K and Q6_K values across the packed layout', () => {
    for (const role of ['blk.0.gdn.in_a', 'blk.0.mlp.gate', 'blk.0.gdn.in_qkv', 'embed_tokens']) {
      const tensor = manifest.tensors.find((entry) => entry.role === role);
      expect(tensor).toBeDefined();
      const source = gguf.tensors.find((entry) => entry.name === tensor!.name);
      expect(source).toBeDefined();
      const cols = tensor!.shape[tensor!.shape.length - 1]!;
      const rowStride = tensor!.quantization!.rowStride;
      const row = Math.min(7, (tensor!.shape[0] ?? 1) - 1);
      const segment = tensor!.segments.find(
        (entry) => row * rowStride >= entry.tensorOffset && row * rowStride < entry.tensorOffset + entry.length,
      );
      expect(segment).toBeDefined();
      const shard = manifest.shards[segment!.buffer]!;
      const packed = shards.get(shard.name)!;
      const packedRow = packed.subarray(
        segment!.offset + row * rowStride - segment!.tensorOffset,
        segment!.offset + (row + 1) * rowStride - segment!.tensorOffset,
      );
      if (tensor!.dtype === 'q4_k') {
        const sourceRow = q4_KDecodeRow(
          ggufBytes,
          Number(source!.absoluteOffset) + row * rowStride,
          cols,
        );
        const actual = q4_KDecodeRow(packedRow, 0, cols);
        expect(Array.from(actual)).toEqual(Array.from(sourceRow));
      } else {
        const sourceRow = q6_KDecodeSourceRow(
          ggufBytes,
          Number(source!.absoluteOffset) + row * (cols / 256) * 210,
          cols,
        );
        const actual = q6_KDecodePackedRow(packedRow, 0, cols);
        expect(Array.from(actual)).toEqual(Array.from(sourceRow));
      }
    }
  });

  it('hashes every immutable shard', () => {
    for (const record of manifest.shards) {
      const bytes = shards.get(record.name)!;
      expect(bytes.length).toBe(record.byteLength);
      expect(createHash('sha256').update(bytes).digest('hex')).toBe(record.sha256);
    }
    expect(manifest.shards.reduce((sum, shard) => sum + shard.byteLength, 0)).toBe(
      manifest.arenas[0]!.byteLength,
    );
  });
});