Skip to content

Commit 1140e0c

Browse files
snowyungxson
andauthored
feat: add GGMLFileQuantizationType and apply to test (huggingface#806)
@mishig25 that's it for huggingface#794 --------- Co-authored-by: Xuan Son Nguyen <[email protected]>
1 parent 6cd5358 commit 1140e0c

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

packages/gguf/src/gguf.spec.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { beforeAll, describe, expect, it } from "vitest";
22
import type { GGUFParseOutput } from "./gguf";
3-
import { GGMLQuantizationType, gguf, ggufAllShards, parseGgufShardFilename } from "./gguf";
3+
import { GGMLFileQuantizationType, GGMLQuantizationType, gguf, ggufAllShards, parseGgufShardFilename } from "./gguf";
44
import fs from "node:fs";
55

66
const URL_LLAMA = "https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/191239b/llama-2-7b-chat.Q2_K.gguf";
@@ -21,9 +21,11 @@ describe("gguf", () => {
2121
if (!fs.existsSync(".cache")) {
2222
fs.mkdirSync(".cache");
2323
}
24-
const res = await fetch(URL_BIG_METADATA);
25-
const arrayBuf = await res.arrayBuffer();
26-
fs.writeFileSync(".cache/model.gguf", Buffer.from(arrayBuf));
24+
if (!fs.existsSync(".cache/model.gguf")) {
25+
const res = await fetch(URL_BIG_METADATA);
26+
const arrayBuf = await res.arrayBuffer();
27+
fs.writeFileSync(".cache/model.gguf", Buffer.from(arrayBuf));
28+
}
2729
});
2830

2931
it("should parse a llama2 7b", async () => {
@@ -37,7 +39,7 @@ describe("gguf", () => {
3739
tensor_count: 291n,
3840
kv_count: 19n,
3941
"general.architecture": "llama",
40-
"general.file_type": 10,
42+
"general.file_type": GGMLFileQuantizationType.MOSTLY_Q2_K,
4143
"general.name": "LLaMA v2",
4244
"general.quantization_version": 2,
4345
"llama.attention.head_count": 32,
@@ -96,7 +98,7 @@ describe("gguf", () => {
9698
tensor_count: 291n,
9799
kv_count: 24n,
98100
"general.architecture": "llama",
99-
"general.file_type": 17,
101+
"general.file_type": GGMLFileQuantizationType.MOSTLY_Q5_K_M,
100102
"general.name": "mistralai_mistral-7b-instruct-v0.2",
101103
"general.quantization_version": 2,
102104
"llama.attention.head_count": 32,
@@ -134,7 +136,7 @@ describe("gguf", () => {
134136
tensor_count: 164n,
135137
kv_count: 21n,
136138
"general.architecture": "gemma",
137-
"general.file_type": GGMLQuantizationType.Q8_K, // 15
139+
"general.file_type": GGMLFileQuantizationType.MOSTLY_Q4_K_M,
138140
"general.name": "gemma-2b-it",
139141
"general.quantization_version": 2,
140142
"gemma.attention.head_count": 8,
@@ -171,7 +173,7 @@ describe("gguf", () => {
171173
tensor_count: 197n,
172174
kv_count: 23n,
173175
"general.architecture": "bert",
174-
"general.file_type": GGMLQuantizationType.F16,
176+
"general.file_type": GGMLFileQuantizationType.MOSTLY_F16,
175177
"general.name": "bge-small-en-v1.5",
176178
"bert.attention.causal": false,
177179
"bert.attention.head_count": 12,

packages/gguf/src/gguf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isBackend } from "./utils/isBackend";
44
import { promisesQueue } from "./utils/promisesQueue";
55

66
export type { MetadataBaseValue, MetadataValue, Version, GGUFMetadata, GGUFTensorInfo, GGUFParseOutput } from "./types";
7-
export { GGUFValueType, GGMLQuantizationType, Architecture } from "./types";
7+
export { GGUFValueType, GGMLFileQuantizationType, GGMLQuantizationType, Architecture } from "./types";
88
export { GGUF_QUANT_DESCRIPTIONS } from "./quant-descriptions";
99

1010
export const RE_GGUF_FILE = /\.gguf$/;

packages/gguf/src/types.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,45 @@ export type MetadataValue = MetadataBaseValue | MetadataBaseValue[] | MetadataVa
66

77
export type Version = 1 | 2 | 3;
88

9+
export enum GGMLFileQuantizationType {
10+
MOSTLY_F32 = 0,
11+
MOSTLY_F16 = 1,
12+
MOSTLY_Q4_0 = 2,
13+
MOSTLY_Q4_1 = 3,
14+
MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
15+
// MOSTLY_Q4_2 = 5, // support has been removed
16+
// MOSTLY_Q4_3 = 6, // support has been removed
17+
MOSTLY_Q8_0 = 7,
18+
MOSTLY_Q5_0 = 8,
19+
MOSTLY_Q5_1 = 9,
20+
MOSTLY_Q2_K = 10,
21+
MOSTLY_Q3_K_S = 11,
22+
MOSTLY_Q3_K_M = 12,
23+
MOSTLY_Q3_K_L = 13,
24+
MOSTLY_Q4_K_S = 14,
25+
MOSTLY_Q4_K_M = 15,
26+
MOSTLY_Q5_K_S = 16,
27+
MOSTLY_Q5_K_M = 17,
28+
MOSTLY_Q6_K = 18,
29+
MOSTLY_IQ2_XXS = 19,
30+
MOSTLY_IQ2_XS = 20,
31+
MOSTLY_Q2_K_S = 21,
32+
MOSTLY_IQ3_XS = 22,
33+
MOSTLY_IQ3_XXS = 23,
34+
MOSTLY_IQ1_S = 24,
35+
MOSTLY_IQ4_NL = 25,
36+
MOSTLY_IQ3_S = 26,
37+
MOSTLY_IQ3_M = 27,
38+
MOSTLY_IQ2_S = 28,
39+
MOSTLY_IQ2_M = 29,
40+
MOSTLY_IQ4_XS = 30,
41+
MOSTLY_IQ1_M = 31,
42+
MOSTLY_BF16 = 32,
43+
MOSTLY_Q4_0_4_4 = 33,
44+
MOSTLY_Q4_0_4_8 = 34,
45+
MOSTLY_Q4_0_8_8 = 35,
46+
}
47+
948
export enum GGMLQuantizationType {
1049
F32 = 0,
1150
F16 = 1,
@@ -60,7 +99,7 @@ export type Architecture = (typeof ARCHITECTURES)[number];
6099
export interface GGUFGeneralInfo<TArchitecture extends Architecture> {
61100
"general.architecture": TArchitecture;
62101
"general.name"?: string;
63-
"general.file_type"?: number;
102+
"general.file_type"?: GGMLFileQuantizationType;
64103
"general.quantization_version"?: number;
65104
}
66105

0 commit comments

Comments
 (0)