AMAImedia
/

AMAImedia commited on
Commit
cca3925
·
verified ·
1 Parent(s): 0486bb1

add gguf/README.md from AngelSlim/Hy4-preview-GGUF

Browse files
Files changed (1) hide show
  1. gguf/README.md +249 -0
gguf/README.md ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hy4-preview GGUF
2
+
3
+ Two GGUF builds of HY4-Preview: https://huggingface.co/tencent/Hy4-preview
4
+
5
+ **Language / 语言:** [English](#english) · [中文](#中文)
6
+
7
+ | file | size | bpw | notes |
8
+ |---|---:|---:|---|
9
+ | `Hy4-preview-Q4_K_M.gguf` | 435.20 GiB | 4.86 | standard 4-bit, safe default |
10
+ | `Hy4-preview-STQ1_0.gguf` | 213.66 GiB | 2.38 | mixed 1-2 bit, half the size |
11
+
12
+ **Neither file runs on stock llama.cpp.** The `hyv4` architecture is not upstream. Apply the
13
+ patches in `hy4-preview-patch/`
14
+
15
+ ---
16
+
17
+ <a name="english"></a>
18
+ ## English
19
+
20
+ ### 1. What these are
21
+
22
+ **`Hy4-preview-Q4_K_M.gguf`** — a conventional Q4_K_M. Most tensors are Q4_K; `ffn_down_exps`
23
+ gets Q6_K on 37 layers via llama.cpp's own logic. Use this unless you are memory-constrained.
24
+
25
+ **`Hy4-preview-STQ1_0.gguf`** — mixed precision at ~2.38 bpw, roughly **half the size** for the
26
+ same model. The routed-expert `gate`/`up` projections run at 1.3125 bpw (STQ1_0) on 29 layers and
27
+ 2.0625 bpw (IQ2_XXS) on the other 48. See section 3.
28
+
29
+ Type histograms:
30
+
31
+ ```
32
+ Q4_K_M: F32 1080 / Q4_K 901 / Q8_0 78 / Q6_K 75
33
+ STQ1_0: F32 1080 / Q8_0 354 / Q5_K 234 / Q6_K 234 / IQ2_XXS 96 / IQ3_XXS 74 / STQ1_0 58 / IQ4_XS 3 / Q4_K 1
34
+ ```
35
+
36
+ ### 2. Running them
37
+
38
+ Build a patched llama.cpp
39
+
40
+
41
+ ```bash
42
+ git clone https://github.com/ggml-org/llama.cpp && cd llama.cpp
43
+ git checkout 0cea36222
44
+
45
+ git apply hy4-preview-patch/0001-hyv4-architecture.patch
46
+ git apply hy4-preview-patch/0002-stq1_0-quant-and-cuda.patch # skip if only using Q4_K_M
47
+
48
+ export PATH=/usr/local/cuda-13.0/bin:$PATH CUDACXX=/usr/local/cuda-13.0/bin/nvcc
49
+ cmake -B build-cuda -DGGML_CUDA=ON -DLLAMA_CURL=OFF -DGGML_NATIVE=OFF \
50
+ -DCMAKE_BUILD_TYPE=Release -DCMAKE_CUDA_ARCHITECTURES=90 \
51
+ -DLLAMA_BUILD_UI=OFF -DLLAMA_USE_PREBUILT_UI=OFF
52
+ cmake --build build-cuda --target llama-cli llama-bench llama-quantize -j 48
53
+ ```
54
+
55
+ Set `-DCMAKE_CUDA_ARCHITECTURES` for your GPU (90 = H20/H100). Both `-DLLAMA_BUILD_UI=OFF` and
56
+ `-DLLAMA_USE_PREBUILT_UI=OFF` are needed for an offline build; the first alone still downloads
57
+ prebuilt assets.
58
+
59
+ Then
60
+
61
+ ```bash
62
+ # single prompt
63
+ build-cuda/bin/llama-cli -m Hy4-preview-Q4_K_M.gguf -ngl 99 -c 8192 \
64
+ --temp 0 -n 512 --no-warmup --jinja -st -f prompt.txt
65
+
66
+ # throughput
67
+ build-cuda/bin/llama-bench -m Hy4-preview-STQ1_0.gguf -ngl 99 -p 512 -n 128 -r 3
68
+ ```
69
+
70
+ - **`--jinja` is required for chat.** The HY4 chat template matches no llama.cpp built-in family.
71
+ - **Keep the GGUF on local disk.** llama.cpp mmaps weights; over NFS random page faults run at
72
+ ~12 MB/s, turning a 1-minute load into hours.
73
+ - **Use `-st -f prompt.txt` for a single prompt.** `-no-cnv` is ignored in this build and it will
74
+ spin printing `>` on EOF.
75
+ - VRAM for full residency: ~435 GiB (Q4_K_M) or ~214 GiB (STQ1_0). With less, lower `-ngl`.
76
+
77
+ Measured on 8x H20:
78
+
79
+ | | prefill (pp512) | decode (tg128) |
80
+ |---|---:|---:|
81
+ | STQ1_0 | 204.56 ± 1.42 t/s | 19.52 ± 0.01 t/s |
82
+
83
+
84
+
85
+ Python tools reading these files must use the patched `gguf-py` with an **absolute** path:
86
+ `sys.path.insert(0, '/path/to/llama.cpp/gguf-py')`.
87
+ ### 3. STQ1_0 and the mixed-precision strategy
88
+
89
+ **The format.** STQ1_0 comes from llama.cpp PR #22836. Weights are ternary `{-d, 0, +d}`, with
90
+ **exactly one of every four lanes forced to zero** (3:4 sparsity). Each group of 4 weights is a
91
+ 4-bit code plus a 1-bit table-select, indexing a 32-entry codebook; one fp16 scale covers 256
92
+ weights. That is `2 + 32 + 8 = 42` bytes per 256 weights = **1.3125 bpw**.
93
+
94
+ **Our encoder.** Upstream's quantizer targets QAT inputs already on the ternary grid: it ignores
95
+ the imatrix, sets `d = amax`, and zeroes `argmin |x|`. That is weak for post-training
96
+ quantization. We keep the format byte-identical and change only two decisions:
97
+
98
+ 1. **Weighted least-squares scale**, `d = sum(w*sel*x) / sum(w*sel^2)` instead of `d = amax`.
99
+ 2. **Imatrix-aware zero placement** — zero the lane minimising `w[j]*(x[j]^2 - (|x[j]|-d)^2)`,
100
+ the *incremental* cost rather than the smallest magnitude.
101
+
102
+ alternating for 3 rounds. Measured on 1200 real expert rows: the LS scale alone gives **-89.7%**
103
+ weighted SSD, and the imatrix terms a further **-4.1%** of the remainder. The headline win is the
104
+ scale — `amax` pins `d` to the single largest outlier among 256 weights.
105
+
106
+ **Where the bits go.** The three routed-expert families are 97.7% of all parameters, so the
107
+ recipe spends freely on everything else:
108
+
109
+ | family | STQ1_0 build | why |
110
+ |---|---|---|
111
+ | `ffn_gate_exps` / `ffn_up_exps` | STQ1_0 (29 layers) / IQ2_XXS (48 layers) | the bulk; layer choice is imatrix-derived |
112
+ | `ffn_down_exps` | IQ3_XXS, IQ4_XS on last 3 | **writes straight into the residual stream**, so its error is not attenuated by a later gate — deliberately 2 levels higher |
113
+ | attention out / gate / q_a | Q5_K | llama.cpp only auto-bumps these when `n_expert == 8`; HY4 has 256 |
114
+ | MLA `q_b`/`k_b`/`v_b`/`kv_a_mqa` | Q8_0 | HY4's *split* names miss llama.cpp's substring match, so they get no automatic bump |
115
+ | DSA indexer | Q8_0 / F32 | 105 tensors, 0.21 GiB total, gates which 2048 tokens each query sees |
116
+ | iHC `*_fn`, router, norms, sink | F32 | mirrors the reference's `_keep_in_fp32_modules` |
117
+ | `output` (lm_head) | F32 | via `--leave-output-tensor` |
118
+
119
+ ### 4. Building a runtime
120
+
121
+ #### Re-quantizing from bf16
122
+
123
+ The recipe files are included. **An imatrix is mandatory for STQ1_0** — its encoder uses it for
124
+ the scale solve and zero placement.
125
+
126
+ ```bash
127
+ build-cuda/bin/llama-quantize --dry-run --imatrix imatrix.gguf \
128
+ --tensor-type-file Hy4-preview-STQ1_0.tensortypes --leave-output-tensor \
129
+ HY4.bf16.gguf out.gguf IQ1_M # Q4_K_M build: use Q4_K_M as the base ftype
130
+ ```
131
+
132
+ ---
133
+
134
+ <a name="中文"></a>
135
+ ## 中文
136
+
137
+ ### 1. 这是什么
138
+
139
+ **`Hy4-preview-Q4_K_M.gguf`** —— 常规 Q4_K_M。多数张量为 Q4_K,`ffn_down_exps` 由 llama.cpp
140
+ 自身逻辑提到 Q6_K(37 层)。**没有显存压力就用这个。**
141
+
142
+ **`Hy4-preview-STQ1_0.gguf`** —— 约 2.38 bpw 的混合精度,同一个模型**体积减半**。路由专家的
143
+ `gate`/`up` 在 29 层用 1.3125 bpw(STQ1_0),另 48 层用 2.0625 bpw(IQ2_XXS)。见第 3 节。
144
+
145
+ 类型直方图:
146
+
147
+ ```
148
+ Q4_K_M: F32 1080 / Q4_K 901 / Q8_0 78 / Q6_K 75
149
+ STQ1_0: F32 1080 / Q8_0 354 / Q5_K 234 / Q6_K 234 / IQ2_XXS 96 / IQ3_XXS 74 / STQ1_0 58 / IQ4_XS 3 / Q4_K 1
150
+ ```
151
+
152
+ ### 2. 如何使用
153
+
154
+
155
+ ```bash
156
+ git clone https://github.com/ggml-org/llama.cpp && cd llama.cpp
157
+ git checkout 0cea36222
158
+
159
+ git apply hy4-preview-patch/0001-hyv4-architecture.patch
160
+ git apply hy4-preview-patch/0002-stq1_0-quant-and-cuda.patch # 只用 Q4_K_M 可跳过
161
+
162
+ export PATH=/usr/local/cuda-13.0/bin:$PATH CUDACXX=/usr/local/cuda-13.0/bin/nvcc
163
+ cmake -B build-cuda -DGGML_CUDA=ON -DLLAMA_CURL=OFF -DGGML_NATIVE=OFF \
164
+ -DCMAKE_BUILD_TYPE=Release -DCMAKE_CUDA_ARCHITECTURES=90 \
165
+ -DLLAMA_BUILD_UI=OFF -DLLAMA_USE_PREBUILT_UI=OFF
166
+ cmake --build build-cuda --target llama-cli llama-bench llama-quantize -j 48
167
+ ```
168
+
169
+ `-DCMAKE_CUDA_ARCHITECTURES` 按自己的 GPU 设置(90 = H20/H100)。离线构建**同时**需要
170
+ `-DLLAMA_BUILD_UI=OFF` 与 `-DLLAMA_USE_PREBUILT_UI=OFF`,只给前者仍会去下载预构建资源。
171
+
172
+
173
+ ```bash
174
+ # 单条 prompt
175
+ build-cuda/bin/llama-cli -m Hy4-preview-Q4_K_M.gguf -ngl 99 -c 8192 \
176
+ --temp 0 -n 512 --no-warmup --jinja -st -f prompt.txt
177
+
178
+ # 测速
179
+ build-cuda/bin/llama-bench -m Hy4-preview-STQ1_0.gguf -ngl 99 -p 512 -n 128 -r 3
180
+ ```
181
+
182
+ - **chat 必须加 `--jinja`。** HY4 的 chat template 不匹配 llama.cpp 任何内置模板家族。
183
+ - **GGUF 必须放本地盘。** llama.cpp 用 mmap,NFS 随机页错误约 12 MB/s,本来 1 分钟的加载会变
184
+ 成几小时。
185
+ - **单条 prompt 用 `-st -f prompt.txt`。** 本 build 忽略 `-no-cnv`,遇 EOF 会一直打印 `>`。
186
+ - 全量驻留显存需求:约 435 GiB(Q4_K_M)或约 214 GiB(STQ1_0)。不够就降低 `-ngl`。
187
+
188
+ 在 8 x H20 上实测(已确认 GPU 空闲、权重全驻显存):
189
+
190
+ | | 预填充 (pp512) | 解码 (tg128) |
191
+ |---|---:|---:|
192
+ | STQ1_0 | 204.56 ± 1.42 t/s | 19.52 ± 0.01 t/s |
193
+
194
+
195
+ 读这些文件的 Python 工具必须用打过补丁的 `gguf-py`,且用**绝对路径**:
196
+ `sys.path.insert(0, '/path/to/llama.cpp/gguf-py')`。
197
+
198
+ ### 3. STQ1_0 与混合精度策略
199
+
200
+ **格式。** STQ1_0 来自 llama.cpp PR #22836。权重为三值 `{-d, 0, +d}`,且**每 4 个 lane 强制
201
+ 一个为零**(3:4 稀疏)。每 4 个权重存成 4-bit code 加 1-bit 选表位,索引一张 32 项码本;每
202
+ 256 个权重共用一个 fp16 scale。即每 256 权重 `2 + 32 + 8 = 42` 字节 = **1.3125 bpw**。
203
+
204
+ **我们的编码器。** 上游的量化器面向已落在三值网格上的 QAT 输入:直接忽略 imatrix,取
205
+ `d = amax`,并把零放在 `argmin |x|`。这对训练后量化(PTQ)很弱。我们保持格式**逐字节一致**,
206
+ 只改两个决策:
207
+
208
+ 1. **加权最小二乘 scale**:`d = sum(w*sel*x) / sum(w*sel^2)`,取代 `d = amax`。
209
+ 2. **imatrix-aware 零位置**:零掉使 `w[j]*(x[j]^2 - (|x[j]|-d)^2)` 最小的 lane,即比较**增量**
210
+ 代价,而非单纯的最小幅值。
211
+
212
+ 两者交替 3 轮。在 1200 行真实专家权重上实测:仅最小二乘 scale 就带来 **-89.7%** 加权 SSD,
213
+ imatrix 项在残差上再补 **-4.1%**。**主要收益来自 scale**——`amax` 会把 `d` 钉在 256 个权重里
214
+ 的单个最大离群值上。
215
+
216
+ **bit 花在哪。** 三个路由专家族占全部参数的 97.7%,所以配方在其余张量上舍得花:
217
+
218
+ | 张量族 | STQ1_0 产物 | 原因 |
219
+ |---|---|---|
220
+ | `ffn_gate_exps` / `ffn_up_exps` | STQ1_0(29 层)/ IQ2_XXS(48 层)| 体积主体;选层由 imatrix 推导 |
221
+ | `ffn_down_exps` | IQ3_XXS,最后 3 层 IQ4_XS | **直接写回残差流**,误差不会被后续 gate 衰减,故刻意高两档 |
222
+ | attention out / gate / q_a | Q5_K | llama.cpp 只在 `n_expert == 8` 时自动提档,而 HY4 有 256 个专家 |
223
+ | MLA `q_b`/`k_b`/`v_b`/`kv_a_mqa` | Q8_0 | HY4 的**拆分**命名匹配不上 llama.cpp 的子串规则,完全拿不到自动提档 |
224
+ | DSA indexer | Q8_0 / F32 | 105 个张量共 0.21 GiB,却是决定每个 query 能看到哪 2048 个 token 的闸门 |
225
+ | iHC `*_fn`、router、norms、sink | F32 | 对齐参考实现的 `_keep_in_fp32_modules` |
226
+ | `output`(lm_head)| F32 | 通过 `--leave-output-tensor` |
227
+
228
+
229
+
230
+ #### 从 bf16 重新量化
231
+
232
+ 配方文件已随附。**STQ1_0 强制需要 imatrix**——它的编码器要用 imatrix 做 scale 求解与零位置选择。
233
+
234
+ ```bash
235
+ build-cuda/bin/llama-quantize --dry-run --imatrix imatrix.gguf \
236
+ --tensor-type-file Hy4-preview-STQ1_0.tensortypes --leave-output-tensor \
237
+ HY4.bf16.gguf out.gguf IQ1_M # Q4_K_M 产物:基础 ftype 用 Q4_K_M
238
+ ```
239
+ ---
240
+
241
+ ## Files
242
+
243
+ ```
244
+ hy4-preview-patch/
245
+ 0001-hyv4-architecture.patch 18 files, +1632/-3 both GGUFs need this
246
+ 0002-stq1_0-quant-and-cuda.patch 25 files, +683/-4 STQ1_0 only
247
+ Hy4-preview-STQ1_0.tensortypes the STQ1_0 recipe
248
+ Hy4-preview-Q4_K_M.tensortypes the Q4_K_M recipe
249
+ ```