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); const shards = available ? new Map(manifest.shards.map((shard) => [shard.name, new Uint8Array(readFileSync(join(artifactDir, shard.name)))])) : new Map(); 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, ); }); });