-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Follow up #640 Ref comments: - #640 (review) by @julien-c suggests using a check `metadata["general.architecture"] === ...` to select the correct type - #640 (comment) by @coyotte508 suggests using less generic but more verbose code The type system introduce in this PR allows type-checking at both compile time & runtime: ```ts const model: GGUFMetadata<GGUFType.STRICT> = null as any; if (model["general.architecture"] === "whisper") { model["encoder.whisper.block_count"] = 0; // @ts-expect-error because it must be a number model["encoder.whisper.block_count"] = "abc"; } if (model["tokenizer.ggml.model"] === undefined) { // @ts-expect-error because it's undefined model["tokenizer.ggml.eos_token_id"] = 1; } if (model["tokenizer.ggml.model"] === "gpt2") { // @ts-expect-error because it must be a number model["tokenizer.ggml.eos_token_id"] = undefined; model["tokenizer.ggml.eos_token_id"] = 1; } if (model["general.architecture"] === "mamba") { model["mamba.ssm.conv_kernel"] = 0; // @ts-expect-error because it must be a number model["mamba.ssm.conv_kernel"] = "abc"; } if (model["general.architecture"] === "llama") { // @ts-expect-error llama does not have ssm.* keys model["mamba.ssm.conv_kernel"] = 0; } ``` Type checks can be disable with `GGUFMetadata<GGUFType.NON_STRICT>`
- Loading branch information
Showing
6 changed files
with
227 additions
and
90 deletions.
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { describe, it } from "vitest"; | ||
import type { gguf } from "./gguf"; | ||
import type { GGUFMetadata, GGUFParseOutput } from "./types"; | ||
|
||
describe("gguf-types", () => { | ||
it("gguf() type can be casted between STRICT and NON_STRICT (at compile time)", async () => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const result: Awaited<ReturnType<typeof gguf>> = { metadata: {} } as any; | ||
const strictType = result as GGUFParseOutput<{ strict: true }>; | ||
// @ts-expect-error because the key "abc" does not exist | ||
strictType.metadata.abc = 123; | ||
const nonStrictType = result as GGUFParseOutput<{ strict: false }>; | ||
nonStrictType.metadata.abc = 123; // PASS, because it can be anything | ||
// @ts-expect-error because ArrayBuffer is not a MetadataValue | ||
nonStrictType.metadata.fff = ArrayBuffer; | ||
}); | ||
|
||
it("GGUFType.NON_STRICT should be correct (at compile time)", async () => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const model: GGUFMetadata<{ strict: false }> = {} as any; | ||
model.kv_count = 123n; | ||
model.abc = 456; // PASS, because it can be anything | ||
}); | ||
|
||
it("GGUFType.STRICT should be correct (at compile time)", async () => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const model: GGUFMetadata<{ strict: true }> = {} as any; | ||
|
||
if (model["general.architecture"] === "whisper") { | ||
model["encoder.whisper.block_count"] = 0; | ||
// @ts-expect-error because it must be a number | ||
model["encoder.whisper.block_count"] = "abc"; | ||
} | ||
|
||
if (model["tokenizer.ggml.model"] === undefined) { | ||
// @ts-expect-error because it's undefined | ||
model["tokenizer.ggml.eos_token_id"] = 1; | ||
} | ||
if (model["tokenizer.ggml.model"] === "gpt2") { | ||
// @ts-expect-error because it must be a number | ||
model["tokenizer.ggml.eos_token_id"] = undefined; | ||
model["tokenizer.ggml.eos_token_id"] = 1; | ||
} | ||
|
||
if (model["general.architecture"] === "mamba") { | ||
model["mamba.ssm.conv_kernel"] = 0; | ||
// @ts-expect-error because it must be a number | ||
model["mamba.ssm.conv_kernel"] = "abc"; | ||
} | ||
if (model["general.architecture"] === "llama") { | ||
// @ts-expect-error llama does not have ssm.* keys | ||
model["mamba.ssm.conv_kernel"] = 0; | ||
} | ||
}); | ||
}); |
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