Benjamin-Wegener commited on
Commit
b5204d4
·
verified ·
1 Parent(s): 32ddb51

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +289 -3
README.md CHANGED
@@ -1,3 +1,289 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: gemma
3
+ language:
4
+ - en
5
+ tags:
6
+ - gguf
7
+ - gguf-apex
8
+ - apex-quant
9
+ - gemma4
10
+ - moe
11
+ - quantized
12
+ - q3_k_m
13
+ - imatrix
14
+ - reap
15
+ - heretic
16
+ - uncensored
17
+ - llama-cpp
18
+ base_model:
19
+ - coder3101/gemma-4-19b-a4b-it-REAP-heretic
20
+ model_creator: Benjamin-Wegener
21
+ quantization_method: APEX with importance matrix
22
+ ---
23
+
24
+ # Gemma 4 19B-A4B-IT REAP Heretic — APEX Q3_K_M
25
+
26
+ **APEX-quantized GGUF model** of [gemma-4-19b-a4b-it-REAP-heretic](https://huggingface.co/coder3101/gemma-4-19b-a4b-it-REAP-heretic) using importance-matrix calibration and tensor-specific quantization.
27
+
28
+ | Property | Value |
29
+ |----------|-------|
30
+ | **Source Model** | coder3101/gemma-4-19b-a4b-it-REAP-heretic |
31
+ | **Quantization** | Q3_K_M with imatrix + tensor-type config |
32
+ | **Model Size** | ~11 GB (F16: ~37 GB) |
33
+ | **BPW** | 5.07 (Bits Per Weight) |
34
+ | **Tensors** | 658 total, 60 with fallback quantization |
35
+
36
+ ## Model Lineage
37
+
38
+ This model went through several processing stages:
39
+
40
+ ```
41
+ google/gemma-4-26b-a4b-it (Original, 26B)
42
+ ↓ REAP (30% Expert Pruning)
43
+ 0xSero/gemma-4-19b-a4b-it-REAP (19B)
44
+ ↓ Heretic/Abliteration (ARA)
45
+ coder3101/gemma-4-19b-a4b-it-REAP-heretic (uncensored)
46
+ ↓ APEX Quantization (this upload)
47
+ Benjamin-Wegener/gemma-4-19b-a4b-it-REAP-heretic-APEX-mini (GGUF Q3_K_M, ~11 GB)
48
+ ```
49
+
50
+ ## APEX Quantization
51
+
52
+ This quantization follows the **[APEX](https://github.com/mudler/apex-quant)** approach by mudler, which optimizes MoE models through tensor-specific quantization strategies and importance-matrix calibration.
53
+
54
+ ### Steps Performed
55
+
56
+ 1. **F16 GGUF Conversion**
57
+ Safetensors → `gemma4-19b-a4b-reap-heretic-f16.gguf` (36.9 GB)
58
+ Using `convert_hf_to_gguf.py` from llama.cpp
59
+
60
+ 2. **Importance Matrix Generation**
61
+ Calibration with ~48,600 tokens from diverse sources:
62
+ - Multi-turn chat (~30%)
63
+ - Code (~25%)
64
+ - Reasoning (~25%)
65
+ - Tool-calling (~20%)
66
+
67
+ Source: [apex-quant/apex_calibration_data](https://github.com/mudler/apex-quant/tree/main/apex_calibration_data)
68
+
69
+ 3. **Tensor-Specific Quantization**
70
+ Using the matched configuration file `gemma4_19b_REAP_heretic_mini.txt` with:
71
+ - **Q8_0**: Router tensors (`ffn_gate_inp`)
72
+ - **Q5_K**: Shared FFN (`ffn_gate`, `ffn_up`, `ffn_down`) in later layers
73
+ - **Q4_K**: Attention tensors (`attn_q`, `attn_k`, `attn_v`, `attn_output`)
74
+ - **Q3_K**: Fused expert tensors (`ffn_gate_up_exps`, `ffn_down_exps`)
75
+
76
+ ### Quantization Config Highlights
77
+
78
+ | Tensor Type | Quantization | Rationale |
79
+ |-------------|--------------|-----------|
80
+ | `ffn_gate_inp` (router) | Q8_0 | Router logits need high precision |
81
+ | `ffn_gate_up_exps` | Q3_K | Largest tensors, aggressive compression |
82
+ | `ffn_down_exps` | Q3_K | Largest tensors, aggressive compression |
83
+ | `ffn_gate/up/down` (shared) | Q4_K–Q5_K | Fewer experts, higher precision |
84
+ | `attn_q/k/v/output` | Q3_K–Q4_K | Attention varies by layer |
85
+
86
+ Full config: [gemma4_19b_REAP_heretic_mini.txt](https://github.com/Benjamin-Wegener/apex-quant/blob/main/configs/gemma4_19b_REAP_heretic_mini.txt)
87
+
88
+ ### Result
89
+
90
+ ```
91
+ Model size (F16): 35,206.24 MiB (16.01 BPW)
92
+ Quant size: 11,149.34 MiB (5.07 BPW)
93
+ Compression: ~70% size reduction
94
+ ```
95
+
96
+ ### GPU Offloading (Vulkan)
97
+
98
+ All 31 layers successfully offloaded to GPU:
99
+
100
+ ```
101
+ llama_model_load_from_file_impl: using device Vulkan0 (AMD Radeon 680M)
102
+ load_tensors: offloaded 31/31 layers to GPU
103
+ load_tensors: Vulkan0 model buffer size = 11,149.34 MiB
104
+ ```
105
+
106
+ Requires llama.cpp compiled with `-DGGML_VULKAN=ON`.
107
+
108
+ ## Usage
109
+
110
+ ### llama.cpp
111
+
112
+ ```bash
113
+ # Download
114
+ huggingface-cli download Benjamin-Wegener/gemma-4-19b-a4b-it-REAP-heretic-APEX-mini \
115
+ --include "*.gguf"
116
+
117
+ # Inference
118
+ llama-cli \
119
+ -m gemma4-19b-a4b-reap-heretic-APEX-mini.gguf \
120
+ -p "Explain quantum computing" \
121
+ -n 512 \
122
+ -ngl 99
123
+ ```
124
+
125
+ ### Python (llama-cpp-python)
126
+
127
+ ```python
128
+ from llama_cpp import Llama
129
+
130
+ llm = Llama(
131
+ model_path="gemma4-19b-a4b-reap-heretic-APEX-mini.gguf",
132
+ n_gpu_layers=-1, # All layers on GPU
133
+ n_ctx=8192, # Context size
134
+ verbose=False
135
+ )
136
+
137
+ messages = [
138
+ {"role": "user", "content": "Write a Python function for binary search."}
139
+ ]
140
+
141
+ output = llm.create_chat_completion(
142
+ messages=messages,
143
+ max_tokens=2048,
144
+ temperature=0.7,
145
+ top_p=0.95,
146
+ top_k=64
147
+ )
148
+
149
+ print(output["choices"][0]["message"]["content"])
150
+ ```
151
+
152
+ ### OpenAI-Compatible Server
153
+
154
+ ```bash
155
+ llama-server \
156
+ -m gemma4-19b-a4b-reap-heretic-APEX-mini.gguf \
157
+ --host 0.0.0.0 \
158
+ --port 8080 \
159
+ --n-gpu-layers 99 \
160
+ --ctx-size 8192
161
+ ```
162
+
163
+ Then use with any OpenAI-compatible client:
164
+
165
+ ```python
166
+ from openai import OpenAI
167
+
168
+ client = OpenAI(base_url="http://localhost:8080/v1", api_key="none")
169
+
170
+ response = client.chat.completions.create(
171
+ model="gemma-4-19b",
172
+ messages=[{"role": "user", "content": "Hello!"}],
173
+ max_tokens=1024
174
+ )
175
+ ```
176
+
177
+ ## Model Architecture
178
+
179
+ | Property | Value |
180
+ |----------|-------|
181
+ | **Architecture** | Gemma4ForCausalLM (MoE) |
182
+ | **Total Parameters** | ~19.02B |
183
+ | **Active Parameters/Token** | ~4B |
184
+ | **Experts per Layer** | 90 (of 128, 30% removed) |
185
+ | **Active Experts/Token** | 8 |
186
+ | **Transformer Layers** | 30 |
187
+ | **Embedding Size** | 2816 |
188
+ | **Vocabulary** | 262,144 tokens |
189
+ | **Context Window** | 262,144 tokens |
190
+ | **Sliding Window** | 1024 (25 layers), full attention (layers 5, 11, 17, 23, 29) |
191
+
192
+ ## REAP Pruning (Pre-Quantization)
193
+
194
+ The source model was compressed using **REAP** (Router-weighted Expert Activation Pruning):
195
+
196
+ | Metric | Original (26B) | REAP 30% (19B) |
197
+ |--------|----------------|----------------|
198
+ | Total Parameters | ~26B | 19.02B |
199
+ | Experts/Layer | 128 | 90 |
200
+ | Active Params/Tok | ~4B | ~4B |
201
+ | Disk Size (BF16) | ~52 GB | ~36 GB |
202
+
203
+ REAP removes 30% of MoE experts (38 of 128 per layer) while preserving routing behavior.
204
+
205
+ ## Heretic Abliteration (Pre-Quantization)
206
+
207
+ Uncensored behavior was achieved using **Heretic** v1.2.0 with the **Arbitrary-Rank Ablation (ARA)** method:
208
+
209
+ | Parameter | Value |
210
+ |-----------|-------|
211
+ | `start_layer_index` | 14 |
212
+ | `end_layer_index` | 25 |
213
+ | `preserve_good_behavior_weight` | 0.7884 |
214
+ | `steer_bad_behavior_weight` | 0.0002 |
215
+ | `overcorrect_relative_weight` | 1.0972 |
216
+ | `neighbor_count` | 7 |
217
+
218
+ **Result:** Refusals reduced from 94/100 → 6/100 with KL divergence of only 0.0290.
219
+
220
+ ## Important Notes
221
+
222
+ ### Imatrix-Based Quantization
223
+
224
+ Unlike naive quantization, APEX uses an **importance matrix** determined during calibration. This matrix identifies which tensors and weights are critical for model quality, enabling:
225
+
226
+ - Tensor-specific quantization levels
227
+ - Better quality at the same bitrate
228
+ - MoE-specific handling of expert tensors
229
+
230
+ **Note:** The imatrix was generated using a diverse calibration dataset (no Wikipedia!), covering chat, code, reasoning, and tool-calling.
231
+
232
+ ### Fallback Quantization
233
+
234
+ 60 of 658 tensors required fallback quantization. These are primarily normalization and scaling tensors that were kept in F32.
235
+
236
+ ### Expected Quality
237
+
238
+ The APEX method (imatrix + tensor-specific config) is expected to deliver higher quality than standard Q3_K_M quantization without calibration. The matched config accounts for the special tensor architecture of this REAP+Heretic model:
239
+
240
+ - Fused expert tensors: `ffn_gate_up_exps`, `ffn_down_exps`
241
+ - Shared FFN tensors: `ffn_gate`, `ffn_up`, `ffn_down`
242
+ - No `attn_v` on full-attention layers (5, 11, 17, 23, 29)
243
+
244
+ ## Acknowledgments & Sources
245
+
246
+ - **Original Model**: [google/gemma-4-26b-a4b-it](https://huggingface.co/google/gemma-4-26b-a4b-it)
247
+ - **REAP Pruning**: [0xSero/gemma-4-19b-a4b-it-REAP](https://huggingface.co/0xSero/gemma-4-19b-a4b-it-REAP)
248
+ - **Heretic Abliteration**: [coder3101/gemma-4-19b-a4b-it-REAP-heretic](https://huggingface.co/coder3101/gemma-4-19b-a4b-it-REAP-heretic)
249
+ - **APEX Quantization**: [mudler/apex-quant](https://github.com/mudler/apex-quant)
250
+ - **Custom Config & Repo**: [Benjamin-Wegener/apex-quant](https://github.com/Benjamin-Wegener/apex-quant)
251
+ - **llama.cpp**: [ggml-org/llama.cpp](https://github.com/ggml-org/llama.cpp)
252
+ - **REAP Paper**: [arxiv.org/abs/2510.13999](https://arxiv.org/abs/2510.13999)
253
+ - **Heretic**: [github.com/p-e-w/heretic](https://github.com/p-e-w/heretic)
254
+
255
+ ## Citation
256
+
257
+ If you use this model in your work:
258
+
259
+ ```bibtex
260
+ @misc{wegener2025gemma4-19b-apex,
261
+ title={{Gemma 4 19B-A4B-IT REAP Heretic APEX Q3\_K\_M}},
262
+ author={Wegener, Benjamin},
263
+ year={2025},
264
+ howpublished={\url{https://huggingface.co/Benjamin-Wegener/gemma-4-19b-a4b-it-REAP-heretic-APEX-mini}},
265
+ note={APEX-quantized model based on REAP-pruned and Heretic-abliterated Gemma 4}
266
+ }
267
+ ```
268
+
269
+ For the underlying REAP method:
270
+
271
+ ```bibtex
272
+ @inproceedings{lasby2025reap,
273
+ title={{REAP} the Experts: Why Pruning Prevails for One-Shot {MoE} Compression},
274
+ author={Lasby, Mike and others},
275
+ booktitle={International Conference on Learning Representations (ICLR)},
276
+ year={2026},
277
+ url={https://arxiv.org/abs/2510.13999}
278
+ }
279
+ ```
280
+
281
+ ## License
282
+
283
+ This model is subject to the **Gemma License** of the original model. The quantization is considered a derivative work.
284
+
285
+ ---
286
+
287
+ **Created by:** [Benjamin-Wegener](https://github.com/Benjamin-Wegener)
288
+ **Quantization Date:** April 2025
289
+ **APEX Repo:** [github.com/Benjamin-Wegener/apex-quant](https://github.com/Benjamin-Wegener/apex-quant)