@@ -10,6 +10,8 @@ export { parseGGUFQuantLabel, GGUF_QUANT_RE, GGUF_QUANT_RE_GLOBAL } from "@huggi
10
10
11
11
export const RE_GGUF_FILE = / \. g g u f $ / ;
12
12
export const RE_GGUF_SHARD_FILE = / ^ (?< prefix > .* ?) - (?< shard > \d { 5 } ) - o f - (?< total > \d { 5 } ) \. g g u f $ / ;
13
+ const GGUF_DEFAULT_ALIGNMENT = 32 ; // defined in ggml.h
14
+ const GGML_PAD = ( x : number , n : number ) => ( x + n - 1 ) & ~ ( n - 1 ) ; // defined in ggml.h
13
15
const PARALLEL_DOWNLOADS = 20 ;
14
16
15
17
export interface GgufShardFileInfo {
@@ -384,14 +386,18 @@ export async function gguf(
384
386
} ) ;
385
387
}
386
388
389
+ // calculate absolute offset of tensor data
390
+ const alignment : number = Number ( metadata [ "general.alignment" ] ?? GGUF_DEFAULT_ALIGNMENT ) ;
391
+ const tensorDataOffset = BigInt ( GGML_PAD ( offset , alignment ) ) ;
392
+
387
393
if ( params ?. computeParametersCount ) {
388
394
const parameterCount = tensorInfos
389
395
. map ( ( { shape } ) => shape . reduce ( ( acc , val ) => acc * Number ( val ) , 1 ) )
390
396
. reduce ( ( acc , val ) => acc + val , 0 ) ;
391
397
392
- return { metadata, tensorInfos, parameterCount } ;
398
+ return { metadata, tensorInfos, tensorDataOffset , parameterCount } ;
393
399
} else {
394
- return { metadata, tensorInfos } ;
400
+ return { metadata, tensorInfos, tensorDataOffset } ;
395
401
}
396
402
}
397
403
@@ -429,7 +435,10 @@ export async function ggufAllShards(
429
435
parameterCount : shards . map ( ( { parameterCount } ) => parameterCount ) . reduce ( ( acc , val ) => acc + val , 0 ) ,
430
436
} ;
431
437
} else {
432
- const { metadata, tensorInfos, parameterCount } = await gguf ( url , { ...params , computeParametersCount : true } ) ;
433
- return { shards : [ { metadata, tensorInfos } ] , parameterCount } ;
438
+ const { metadata, tensorInfos, tensorDataOffset, parameterCount } = await gguf ( url , {
439
+ ...params ,
440
+ computeParametersCount : true ,
441
+ } ) ;
442
+ return { shards : [ { metadata, tensorInfos, tensorDataOffset } ] , parameterCount } ;
434
443
}
435
444
}
0 commit comments