Skip to content

Commit c2afbdc

Browse files
committed
cast between strict & nonStrict
1 parent 8767726 commit c2afbdc

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

packages/gguf/scripts/generate-llm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Attention<TArchitecture extends string> = Record<
2929
number
3030
>>;
3131
32-
type RopeScalingType = "none" | "linear" | "yarn";
32+
export type TransformerLLMRopeScalingType = "none" | "linear" | "yarn";
3333
type Rope<TArchitecture extends LLMArchitecture> = Partial<
3434
Record<
3535
\`\${TArchitecture}.rope.dimension_count\`
@@ -39,7 +39,7 @@ type Rope<TArchitecture extends LLMArchitecture> = Partial<
3939
| \`\${TArchitecture}.rope.scaling.original_context_length\`,
4040
number
4141
>
42-
& Record<\`\${TArchitecture}.rope.scaling.type\`, RopeScalingType>
42+
& Record<\`\${TArchitecture}.rope.scaling.type\`, TransformerLLMRopeScalingType>
4343
& Record<\`\${TArchitecture}.rope.finetuned\`, boolean>
4444
>;
4545

packages/gguf/src/transformer-llm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Attention<TArchitecture extends string> = Record<`${TArchitecture}.attentio
1919
>
2020
>;
2121

22-
type RopeScalingType = "none" | "linear" | "yarn";
22+
export type TransformerLLMRopeScalingType = "none" | "linear" | "yarn";
2323
type Rope<TArchitecture extends LLMArchitecture> = Partial<
2424
Record<
2525
| `${TArchitecture}.rope.dimension_count`
@@ -29,7 +29,7 @@ type Rope<TArchitecture extends LLMArchitecture> = Partial<
2929
| `${TArchitecture}.rope.scaling.original_context_length`,
3030
number
3131
> &
32-
Record<`${TArchitecture}.rope.scaling.type`, RopeScalingType> &
32+
Record<`${TArchitecture}.rope.scaling.type`, TransformerLLMRopeScalingType> &
3333
Record<`${TArchitecture}.rope.finetuned`, boolean>
3434
>;
3535

packages/gguf/src/types.spec.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
import { describe, it } from "vitest";
2-
import type { GGUFStrictType, GGUFMetadata, GGUFNonStrictType } from "./types";
2+
import type { gguf } from "./gguf";
3+
import type { GGUFMetadata, GGUFParseOutput, GGUFType } from "./types";
34

45
describe("gguf-types", () => {
5-
it("GGUFNonStrictType should be correct (at compile time)", async () => {
6+
it("gguf() type can be casted (at compile time)", async () => {
67
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7-
const model: GGUFMetadata<GGUFNonStrictType> = null as any;
8+
const result: Awaited<ReturnType<typeof gguf>> = null as any;
9+
const strictType = result as GGUFParseOutput<GGUFType.strict>;
10+
// @ts-expect-error because the key "abc" does not exist
11+
strictType.metadata.abc = 123;
12+
const nonStrictType = result as GGUFParseOutput<GGUFType.nonStrict>;
13+
nonStrictType.metadata.abc = 123; // PASS, because it can be anything
14+
// @ts-expect-error because ArrayBuffer is not a MetadataValue
15+
nonStrictType.metadata.fff = ArrayBuffer;
16+
});
17+
18+
it("GGUFType.nonStrict should be correct (at compile time)", async () => {
19+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20+
const model: GGUFMetadata<GGUFType.nonStrict> = null as any;
821
model.kv_count = 123n;
922
model.abc = 456; // PASS, because it can be anything
1023
});
1124

12-
it("GGUFStrictType should be correct (at compile time)", async () => {
25+
it("GGUFType.strict should be correct (at compile time)", async () => {
1326
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14-
const model: GGUFMetadata<GGUFStrictType> = null as any;
27+
const model: GGUFMetadata<GGUFType.strict> = null as any;
1528

1629
if (model["general.architecture"] === "whisper") {
1730
model["encoder.whisper.block_count"] = 0;

packages/gguf/src/types.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,17 @@ export type Whisper = GGUFGeneralInfo<"whisper"> &
110110

111111
/// Types for parse output
112112

113-
export type GGUFStrictType = true;
114-
export type GGUFNonStrictType = false;
113+
export enum GGUFType {
114+
strict,
115+
nonStrict,
116+
}
115117

116-
export type GGUFMetadata<T extends GGUFStrictType | GGUFNonStrictType = GGUFStrictType> = {
118+
export type GGUFMetadata<TGGUFType extends GGUFType = GGUFType.strict> = {
117119
version: Version;
118120
tensor_count: bigint;
119121
kv_count: bigint;
120-
} & (T extends GGUFStrictType ? GGUFModelKV : Record<string, MetadataValue>);
122+
} & GGUFModelKV &
123+
(TGGUFType extends GGUFType.strict ? unknown : Record<string, MetadataValue>);
121124

122125
export type GGUFModelKV = (NoModelMetadata | ModelMetadata) & (NoTokenizer | Tokenizer);
123126

@@ -129,7 +132,7 @@ export interface GGUFTensorInfo {
129132
offset: bigint;
130133
}
131134

132-
export interface GGUFParseOutput<T extends GGUFStrictType | GGUFNonStrictType = GGUFStrictType> {
133-
metadata: GGUFMetadata<T>;
135+
export interface GGUFParseOutput<TGGUFType extends GGUFType = GGUFType.strict> {
136+
metadata: GGUFMetadata<TGGUFType>;
134137
tensorInfos: GGUFTensorInfo[];
135138
}

0 commit comments

Comments
 (0)