diff --git a/src/hyllama.js b/src/hyllama.js index 028b828..3438f2b 100644 --- a/src/hyllama.js +++ b/src/hyllama.js @@ -11,23 +11,23 @@ export function ggufMetadata(arrayBuffer) { /** * Helper function to read string from DataView * @param {number} offset - * @returns {{ string: string, newOffset: number }} + * @returns {{ byteLength: number, value: string }} */ function readString(offset) { const length = view.getBigUint64(offset, true) - let string = '' + let value = '' for (let i = 0; i < length; i++) { - string += String.fromCharCode(view.getUint8(offset + 8 + i)) + value += String.fromCharCode(view.getUint8(offset + 8 + i)) } - return { string, newOffset: offset + 8 + Number(length) } + return { byteLength: 8 + Number(length), value } } /** * Helper function to read metadata value based on type * - * Decoded is an object with newOffset and value properties + * Decoded is an object with byteLength and value properties * - * @typedef {{ newOffset: number, value: any }} Decoded + * @typedef {{ byteLength: number, value: any }} Decoded * @param {number} type * @param {number} offset * @returns {Decoded} @@ -35,36 +35,35 @@ export function ggufMetadata(arrayBuffer) { function readMetadataValue(type, offset) { switch (type) { case 0: // UINT8 - return { value: view.getUint8(offset), newOffset: offset + 1 } + return { value: view.getUint8(offset), byteLength: 1 } case 1: // INT8 - return { value: view.getInt8(offset), newOffset: offset + 1 } + return { value: view.getInt8(offset), byteLength: 1 } case 2: // UINT16 - return { value: view.getUint16(offset, true), newOffset: offset + 2 } + return { value: view.getUint16(offset, true), byteLength: 2 } case 3: // INT16 - return { value: view.getInt16(offset, true), newOffset: offset + 2 } + return { value: view.getInt16(offset, true), byteLength: 2 } case 4: // UINT32 - return { value: view.getUint32(offset, true), newOffset: offset + 4 } + return { value: view.getUint32(offset, true), byteLength: 4 } case 5: // INT32 - return { value: view.getInt32(offset, true), newOffset: offset + 4 } + return { value: view.getInt32(offset, true), byteLength: 4 } case 6: // FLOAT32 - return { value: view.getFloat32(offset, true), newOffset: offset + 4 } + return { value: view.getFloat32(offset, true), byteLength: 4 } case 7: // BOOL - return { value: view.getUint8(offset) !== 0, newOffset: offset + 1 } + return { value: view.getUint8(offset) !== 0, byteLength: 1 } case 8: { // STRING - const { string, newOffset } = readString(offset) - return { value: string, newOffset } + return readString(offset) } case 9: { // ARRAY const arrayType = view.getUint32(offset, true) const arrayLength = view.getBigUint64(offset + 4, true) - let arrayOffset = offset + 12 + let arrayOffset = 12 const arrayValues = [] for (let i = 0; i < arrayLength; i++) { - const { value, newOffset } = readMetadataValue(arrayType, arrayOffset) + const { value, byteLength } = readMetadataValue(arrayType, offset + arrayOffset) arrayValues.push(value) - arrayOffset = newOffset + arrayOffset += byteLength } - return { value: arrayValues, newOffset: arrayOffset } + return { value: arrayValues, byteLength: arrayOffset } } default: throw new Error('Unsupported metadata type: ' + type) @@ -89,7 +88,7 @@ export function ggufMetadata(arrayBuffer) { for (let i = 0; i < metadataKVCount; i++) { // read key const keyResult = readString(offset) - offset = keyResult.newOffset + offset += keyResult.byteLength // read value type const valueType = view.getUint32(offset, true) @@ -97,9 +96,9 @@ export function ggufMetadata(arrayBuffer) { // read value const valueResult = readMetadataValue(valueType, offset) - offset = valueResult.newOffset + offset += valueResult.byteLength - metadata[keyResult.string] = valueResult.value + metadata[keyResult.value] = valueResult.value } const tensorInfos = [] @@ -107,7 +106,7 @@ export function ggufMetadata(arrayBuffer) { for (let i = 0; i < tensorCount; i++) { // read tensor name const keyResult = readString(offset) - offset = keyResult.newOffset + offset += keyResult.byteLength const nDims = view.getUint32(offset, true) offset += 4 @@ -125,7 +124,7 @@ export function ggufMetadata(arrayBuffer) { offset += 8 tensorInfos.push({ - name: keyResult.string, + name: keyResult.value, nDims, shape, type,