/** Bounded metadata/tensor-directory reader. Tensor weights are never read into memory. */ import { closeSync, fstatSync, openSync, readSync } from 'node:fs'; const MAX_HEADER = 96 * 1024 ** 2, MAX_COUNT = 2_000_000; const widths = { 0:1, 1:1, 2:2, 3:2, 4:4, 5:4, 6:4, 7:1, 10:8, 11:8, 12:8 }; // ggml block width / bytes. See gguf-py/gguf/constants.py, GGML_QUANT_SIZES. const tensorSizes = {0:[1,4],1:[1,2],2:[32,18],3:[32,20],6:[32,22],7:[32,24],8:[32,34],9:[32,36],10:[256,84],11:[256,110],12:[256,144],13:[256,176],14:[256,210],15:[256,292],16:[256,66],17:[256,74],18:[256,98],19:[256,50],20:[32,18],21:[256,110],22:[256,82],23:[256,136],24:[1,1],25:[1,2],26:[1,4],27:[1,8],28:[1,8],29:[256,56],30:[1,2],34:[256,54],35:[256,66],39:[32,17],40:[64,36],41:[128,18],42:[64,18]}; export function readGGUF(file) { const fd=openSync(file,'r'); try { const stat=fstatSync(fd);if(!stat.isFile())throw new Error('Choose a GGUF file.'); let p=0,cacheStart=-1,cacheBytes=0;const cache=Buffer.alloc(65536); const bounded=n=>{if(!Number.isSafeInteger(n)||n<0||p+n>stat.size||p+n>MAX_HEADER)throw new Error('The GGUF header is truncated or exceeds the supported metadata size.');}; function bytes(n){bounded(n);if(n>cache.length){const b=Buffer.alloc(n);if(readSync(fd,b,0,n,p)!==n)throw new Error('Could not read GGUF metadata.');p+=n;return b;}if(pcacheStart+cacheBytes){cacheStart=p;cacheBytes=readSync(fd,cache,0,Math.min(cache.length,stat.size-p),p);}const b=cache.subarray(p-cacheStart,p-cacheStart+n);p+=n;return b;} function u64(){const n=bytes(8).readBigUInt64LE();if(n>BigInt(Number.MAX_SAFE_INTEGER))throw new Error('GGUF metadata contains an unsupported integer.');return Number(n);} const u32=()=>bytes(4).readUInt32LE(); function string(keep=true,limit=8*1024**2){const n=u64();if(n>limit)throw new Error('GGUF metadata contains an oversized string.');bounded(n);if(!keep){p+=n;return null;}return bytes(n).toString('utf8');} function value(type,keep,depth=0){ if(type===8)return string(keep); if(type===9){if(depth>4)throw new Error('GGUF metadata arrays are too deeply nested.');const element=u32(),count=u64();if(count>MAX_COUNT)throw new Error('GGUF metadata array is too large.');if(widths[element]){bounded(count*widths[element]);p+=count*widths[element];}else for(let i=0;iBigInt(Number.MAX_SAFE_INTEGER))throw new Error('GGUF integer is too large.');return Number(n);}if(type===11){const n=b.readBigInt64LE();if(n>BigInt(Number.MAX_SAFE_INTEGER)||nMAX_COUNT||count>100_000)throw new Error('GGUF metadata contains too many entries.'); const metadata=Object.create(null),keys=new Set(); for(let i=0;i4096||(alignment&(alignment-1)))throw new Error('GGUF tensor alignment is invalid.'); let tensorEnd=0,parameters=0;const names=new Set(); for(let i=0;i4)throw new Error('GGUF tensor dimensions are invalid.');let elements=1;for(let d=0;dstat.size)throw new Error('The GGUF file is incomplete. Download all of its weights before adding it.'); return {metadata,version,tensorCount,parameters,size:stat.size,mtimeMs:stat.mtimeMs,ctimeMs:stat.ctimeMs}; } finally {closeSync(fd);} }