Skip to content

Commit

Permalink
Use byteLength instead of newOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Mar 12, 2024
1 parent 22f022f commit 4e01e17
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions src/hyllama.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,59 @@ 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}
*/
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)
Expand All @@ -89,25 +88,25 @@ 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)
offset += 4

// 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 = []

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
Expand All @@ -125,7 +124,7 @@ export function ggufMetadata(arrayBuffer) {
offset += 8

tensorInfos.push({
name: keyResult.string,
name: keyResult.value,
nDims,
shape,
type,
Expand Down

0 comments on commit 4e01e17

Please sign in to comment.