-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gguf updates #543
Merged
Merged
Gguf updates #543
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,10 @@ class RangeView { | |
|
||
readonly view: DataView; | ||
|
||
constructor(public url: string) { | ||
constructor( | ||
public url: string, | ||
private _fetch: typeof fetch = fetch | ||
) { | ||
this.chunk = 0; | ||
/// TODO(fix typing) | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
|
@@ -78,7 +81,7 @@ class RangeView { | |
const range = [this.chunk * HTTP_CHUNK_SIZE, (this.chunk + 1) * HTTP_CHUNK_SIZE - 1]; | ||
const buf = new Uint8Array( | ||
await ( | ||
await fetch(this.url, { | ||
await this._fetch(this.url, { | ||
headers: { | ||
Range: `bytes=${range[0]}-${range[1]}`, | ||
}, | ||
|
@@ -115,54 +118,54 @@ function readVersionedSize(view: DataView, byteOffset: number, version: Version) | |
} | ||
} | ||
|
||
function readString(view: DataView, offset: number): { value: string; newOffset: number } { | ||
function readString(view: DataView, offset: number): { value: string; length: number } { | ||
const length = view.getBigUint64(offset, true); | ||
const value = new TextDecoder().decode(view.buffer.slice(offset + 8, offset + 8 + Number(length))); | ||
return { value, newOffset: offset + 8 + Number(length) }; | ||
return { value, length: 8 + Number(length) }; | ||
} | ||
|
||
function readMetadataValue( | ||
view: DataView, | ||
type: GGUFValueType, | ||
offset: number | ||
): { value: MetadataValue; newOffset: number } { | ||
): { value: MetadataValue; length: number } { | ||
switch (type) { | ||
case GGUFValueType.UINT8: | ||
return { value: view.getUint8(offset), newOffset: offset + 1 }; | ||
return { value: view.getUint8(offset), length: 1 }; | ||
case GGUFValueType.INT8: | ||
return { value: view.getInt8(offset), newOffset: offset + 1 }; | ||
return { value: view.getInt8(offset), length: 1 }; | ||
case GGUFValueType.UINT16: | ||
return { value: view.getUint16(offset, true), newOffset: offset + 2 }; | ||
return { value: view.getUint16(offset, true), length: 2 }; | ||
case GGUFValueType.INT16: | ||
return { value: view.getInt16(offset, true), newOffset: offset + 2 }; | ||
return { value: view.getInt16(offset, true), length: 2 }; | ||
case GGUFValueType.UINT32: | ||
return { value: view.getUint32(offset, true), newOffset: offset + 4 }; | ||
return { value: view.getUint32(offset, true), length: 4 }; | ||
case GGUFValueType.INT32: | ||
return { value: view.getInt32(offset, true), newOffset: offset + 4 }; | ||
return { value: view.getInt32(offset, true), length: 4 }; | ||
case GGUFValueType.FLOAT32: | ||
return { value: view.getFloat32(offset, true), newOffset: offset + 4 }; | ||
return { value: view.getFloat32(offset, true), length: 4 }; | ||
case GGUFValueType.BOOL: | ||
return { value: view.getUint8(offset) !== 0, newOffset: offset + 1 }; | ||
return { value: view.getUint8(offset) !== 0, length: 1 }; | ||
case GGUFValueType.STRING: | ||
return readString(view, offset); | ||
case GGUFValueType.ARRAY: { | ||
const arrayType = view.getUint32(offset, true); | ||
const arrayLength = view.getBigUint64(offset + 4, true); | ||
let arrayOffset = offset + 12; | ||
let length = 12; | ||
const arrayValues: MetadataValue[] = []; | ||
for (let i = 0; i < arrayLength; i++) { | ||
const { value, newOffset } = readMetadataValue(view, arrayType, arrayOffset); | ||
const { value, length: _length } = readMetadataValue(view, arrayType, offset + length); | ||
arrayValues.push(value); | ||
arrayOffset = newOffset; | ||
length += _length; | ||
} | ||
return { value: arrayValues, newOffset: arrayOffset }; | ||
return { value: arrayValues, length }; | ||
} | ||
case GGUFValueType.UINT64: | ||
return { value: view.getBigUint64(offset, true), newOffset: offset + 8 }; | ||
return { value: view.getBigUint64(offset, true), length: 8 }; | ||
case GGUFValueType.INT64: | ||
return { value: view.getBigInt64(offset, true), newOffset: offset + 8 }; | ||
return { value: view.getBigInt64(offset, true), length: 8 }; | ||
case GGUFValueType.FLOAT64: | ||
return { value: view.getFloat64(offset, true), newOffset: offset + 8 }; | ||
return { value: view.getFloat64(offset, true), length: 8 }; | ||
} | ||
} | ||
|
||
|
@@ -185,8 +188,8 @@ export interface GGUFParseOutput { | |
tensorInfos: GGUFTensorInfo[]; | ||
} | ||
|
||
export async function gguf(url: string): Promise<GGUFParseOutput> { | ||
const r = new RangeView(url); | ||
export async function gguf(url: string, _fetch: typeof fetch = fetch): Promise<GGUFParseOutput> { | ||
const r = new RangeView(url, _fetch); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
await r.fetchChunk(); | ||
|
||
if (r.view.getUint32(0, true) !== new DataView(ggufMagicNumber.buffer).getUint32(0, true)) { | ||
|
@@ -213,7 +216,7 @@ export async function gguf(url: string): Promise<GGUFParseOutput> { | |
|
||
// read key | ||
const keyResult = readString(r.view, offset); | ||
offset = keyResult.newOffset; | ||
offset += keyResult.length; | ||
|
||
// read value type | ||
const valueType = r.view.getUint32(offset, true); | ||
|
@@ -223,7 +226,7 @@ export async function gguf(url: string): Promise<GGUFParseOutput> { | |
throw new Error("Unsupported metadata type: " + valueType); | ||
} | ||
|
||
let valueResult: { value: MetadataValue; newOffset: number } | undefined; | ||
let valueResult: ReturnType<typeof readMetadataValue> | undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
while (!valueResult) { | ||
try { | ||
// read value | ||
|
@@ -236,7 +239,7 @@ export async function gguf(url: string): Promise<GGUFParseOutput> { | |
} | ||
} | ||
} | ||
offset = valueResult.newOffset; | ||
offset += valueResult.length; | ||
metadata[keyResult.value] = valueResult.value; | ||
} | ||
|
||
|
@@ -247,7 +250,7 @@ export async function gguf(url: string): Promise<GGUFParseOutput> { | |
|
||
// read tensor name | ||
const keyResult = readString(r.view, offset); | ||
offset = keyResult.newOffset; | ||
offset += keyResult.length; | ||
|
||
const nDims = r.view.getUint32(offset, true); | ||
offset += 4; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any way to pass it like this for consistency with safetensors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
682483a