@@ -10,6 +10,8 @@ export { parseGGUFQuantLabel, GGUF_QUANT_RE, GGUF_QUANT_RE_GLOBAL } from "@huggi
1010
1111export const RE_GGUF_FILE = / \. g g u f $ / ;
1212export 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
1315const PARALLEL_DOWNLOADS = 20 ;
1416
1517export interface GgufShardFileInfo {
@@ -384,14 +386,18 @@ export async function gguf(
384386 } ) ;
385387 }
386388
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+
387393 if ( params ?. computeParametersCount ) {
388394 const parameterCount = tensorInfos
389395 . map ( ( { shape } ) => shape . reduce ( ( acc , val ) => acc * Number ( val ) , 1 ) )
390396 . reduce ( ( acc , val ) => acc + val , 0 ) ;
391397
392- return { metadata, tensorInfos, parameterCount } ;
398+ return { metadata, tensorInfos, tensorDataOffset , parameterCount } ;
393399 } else {
394- return { metadata, tensorInfos } ;
400+ return { metadata, tensorInfos, tensorDataOffset } ;
395401 }
396402}
397403
@@ -429,7 +435,10 @@ export async function ggufAllShards(
429435 parameterCount : shards . map ( ( { parameterCount } ) => parameterCount ) . reduce ( ( acc , val ) => acc + val , 0 ) ,
430436 } ;
431437 } 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 } ;
434443 }
435444}
0 commit comments