File size: 6,347 Bytes
27f871a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f8856
b422afa
4c458bf
75dda6d
 
 
27f871a
b7f8856
27f871a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b422afa
 
 
27f871a
75dda6d
 
27f871a
 
 
 
 
 
4c458bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27f871a
 
 
 
75dda6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27f871a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
quantized_by: ubergarm
pipeline_tag: text-generation
base_model: zai-org/GLM-4.7
license: mit
base_model_relation: quantized
tags:
- imatrix
- conversational
- ik_llama.cpp
- glm4_moe
language:
  - en
  - zh
---

## WIP
Currently cooking this now!

- [x] download bf16 safetensors https://huggingface.co/zai-org/GLM-4.7
- [x] use llama.cpp/convert_hf_to_gguf.py to create bf16 GGUF
- [x] calculate imatrix and upload to HF first so others can use as desired
- [x] cook Q8_0 and test perplexity of BF16 and Q8_0 for baseline data
- [x] adjust MTP nextn tensors to full q8_0 (won't effect RAM+VRAM usage otherwise)
- [x] cook IQ5_K with full q8_0 attn/shexp/first 3 dense layers and test
- [x] upload IQ5_K if all looking good
- [ ] upload smol-IQ4_KSS if all looking good
- [ ] continue with smaller quants
- [ ] check if any folks open discussions with desired RAM/VRAM breakpoints

## `ik_llama.cpp` imatrix Quantizations of zai-org/GLM-4.7
*NOTE* `ik_llama.cpp` can also run your existing GGUFs from bartowski, unsloth, mradermacher, etc if you want to try it out before downloading my quants.

Some of ik's new quants are supported with [Nexesenex/croco.cpp](https://github.com/Nexesenex/croco.cpp) fork of KoboldCPP with Windows builds for CUDA 12.9. Also check for [Windows builds by Thireus here.](https://github.com/Thireus/ik_llama.cpp/releases) which have been CUDA 12.8.

These quants provide best in class perplexity for the given memory footprint.

## Big Thanks
Shout out to Wendell and the **Level1Techs** crew, the community [Forums](https://forum.level1techs.com/t/deepseek-deep-dive-r1-at-home/225826), [YouTube Channel](https://www.youtube.com/@Level1Techs)!  **BIG thanks** for providing **BIG hardware** expertise and access to run these experiments and make these great quants available to the community!!!

Also thanks to all the folks in the quanting and inferencing community on [BeaverAI Club Discord](https://huggingface.co/BeaverAI) and on [r/LocalLLaMA](https://www.reddit.com/r/LocalLLaMA/) for tips and tricks helping each other run, test, and benchmark all the fun new models! Thanks to huggingface for hosting all these big quants!

Finally, I *really* appreciate the support from [aifoundry.org](https://aifoundry.org) so check out their open source RISC-V based solutions!

## Quant Collection
Perplexity computed against *wiki.test.raw*.

![Perplexity Chart](images/perplexity.png "Chart showing Perplexity improving as BPW increases.")

These first two are just test quants for baseline perplexity comparison:
* `BF16` 667.598 GiB (16.003 BPW)
  - Final estimate: PPL over 565 chunks for n_ctx=512 = 3.9267 +/- 0.02423
* `Q8_0` 354.794 GiB (8.505 BPW)
  - Final estimate: PPL over 565 chunks for n_ctx=512 = 3.9320 +/- 0.02428

## IQ5_K 250.635 GiB (6.008 BPW)
Final estimate: PPL over 565 chunks for n_ctx=512 = 3.9445 +/- 0.02439

<details>

<summary>👈 Secret Recipe</summary>

```bash
#!/usr/bin/env bash

custom="
# 93 Repeating Layers [0-92]

# Attention
blk\..*\.attn_q.*=q8_0
blk\..*\.attn_k.*=q8_0
blk\..*\.attn_v.*=q8_0
blk\..*\.attn_output.*=q8_0

# First 3 Dense Layers [0-2]
blk\..*\.ffn_down\.weight=q8_0
blk\..*\.ffn_(gate|up)\.weight=q8_0

# Shared Expert Layers [3-92]
blk\..*\.ffn_down_shexp\.weight=q8_0
blk\..*\.ffn_(gate|up)_shexp\.weight=q8_0

# Routed Experts Layers [3-92]
blk\..*\.ffn_down_exps\.weight=iq6_k
blk\..*\.ffn_(gate|up)_exps\.weight=iq5_k

# NextN MTP Layer [92]
# Leave full q8_0 as supposedly better for MTP
# (doesn't use RAM or VRAM otherwise so its fine)
blk\..*\.nextn\.embed_tokens\.weight=q8_0
blk\..*\.nextn\.shared_head_head\.weight=q8_0
blk\..*\.nextn\.eh_proj\.weight=q8_0

# Non-Repeating Layers
token_embd\.weight=iq6_k
output\.weight=iq6_k
"

custom=$(
  echo "$custom" | grep -v '^#' | \
  sed -Ez 's:\n+:,:g;s:,$::;s:^,::'
)

numactl -N ${SOCKET} -m ${SOCKET} \
./build/bin/llama-quantize \
    --custom-q "$custom" \
    --imatrix /mnt/data/models/ubergarm/GLM-4.7-GGUF/imatrix-GLM-4.7-BF16.dat \
    /mnt/data/models/ubergarm/GLM-4.7-GGUF/GLM-160x21B-4.7-BF16-00001-of-00015.gguf \
    /mnt/data/models/ubergarm/GLM-4.7-GGUF/GLM-4.7-IQ5_K.gguf \
    IQ5_K \
    128
```

</details>

## smol-IQ4_KSS TODO
Final estimate: PPL over 565 chunks for n_ctx=512 = TODO

<details>

<summary>👈 Secret Recipe</summary>

```bash
#!/usr/bin/env bash

custom="
# 93 Repeating Layers [0-92]

# Attention
blk\..*\.attn_q.*=q8_0
blk\..*\.attn_k.*=q8_0
blk\..*\.attn_v.*=q8_0
blk\..*\.attn_output.*=q8_0

# First 3 Dense Layers [0-2]
blk\..*\.ffn_down\.weight=q8_0
blk\..*\.ffn_(gate|up)\.weight=q8_0

# Shared Expert Layers [3-92]
blk\..*\.ffn_down_shexp\.weight=q8_0
blk\..*\.ffn_(gate|up)_shexp\.weight=q8_0

# Routed Experts Layers [3-92]
blk\..*\.ffn_down_exps\.weight=iq4_kss
blk\..*\.ffn_(gate|up)_exps\.weight=iq4_kss

# NextN MTP Layer [92]
blk\..*\.nextn\.embed_tokens\.weight=q8_0
blk\..*\.nextn\.shared_head_head\.weight=q8_0
blk\..*\.nextn\.eh_proj\.weight=q8_0

# Non-Repeating Layers
token_embd\.weight=iq4_k
output\.weight=iq6_k
"

custom=$(
  echo "$custom" | grep -v '^#' | \
  sed -Ez 's:\n+:,:g;s:,$::;s:^,::'
)

numactl -N ${SOCKET} -m ${SOCKET} \
./build/bin/llama-quantize \
    --custom-q "$custom" \
    --imatrix /mnt/data/models/ubergarm/GLM-4.7-GGUF/imatrix-GLM-4.7-BF16.dat \
    /mnt/data/models/ubergarm/GLM-4.7-GGUF/GLM-160x21B-4.7-BF16-00001-of-00015.gguf \
    /mnt/data/models/ubergarm/GLM-4.7-GGUF/GLM-4.7-smol-IQ4_KSS.gguf \
    IQ4_KSS \
    128
```

</details>

## Quick Start
```bash
# Clone and checkout
$ git clone https://github.com/ikawrakow/ik_llama.cpp
$ cd ik_llama.cpp

# Build for hybrid CPU+CUDA
$ cmake -B build -DCMAKE_BUILD_TYPE=Release -DGGML_CUDA=ON
$ cmake --build build --config Release -j $(nproc)

# I'll follow up with updated commands especially for 2 to 4 CUDA GPUs for "graph parallel"
# https://github.com/ikawrakow/ik_llama.cpp/pull/1080
echo TODO
```

## References
* [ik_llama.cpp](https://github.com/ikawrakow/ik_llama.cpp)
* [Getting Started Guide (already out of date lol)](https://github.com/ikawrakow/ik_llama.cpp/discussions/258)
* [ubergarm-imatrix-calibration-corpus-v02.txt](https://gist.github.com/ubergarm/edfeb3ff9c6ec8b49e88cdf627b0711a?permalink_comment_id=5682584#gistcomment-5682584)
* [Solid mainline quants by AesSedai/GLM-4.7-GGUF](https://huggingface.co/AesSedai/GLM-4.7-GGUF)