---
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*.

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
👈 Secret Recipe
```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
```
## smol-IQ4_KSS TODO
Final estimate: PPL over 565 chunks for n_ctx=512 = TODO
👈 Secret Recipe
```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
```
## 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)