Mario867 commited on
Commit
2e8abe1
·
verified ·
1 Parent(s): 73b292b

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +124 -0
README.md ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: gguf
3
+ base_model: Inkling-2-Mini-UD-Q3_K_XL
4
+ tags:
5
+ - gguf
6
+ - llama-cpp
7
+ - quantization
8
+ - q4_k
9
+ - q4_0
10
+ - nvfp4
11
+ - vulkan
12
+ - polaris
13
+ - rx580
14
+ - tesla
15
+ ---
16
+
17
+ # Inkling-2-Mini-UD-Q3_K_XL — Q4 Repack (`NVFP4` $\rightarrow$ `Q4_K` / `Q4_0`)
18
+
19
+ ## Model Overview
20
+
21
+ * **Base Model:** `Inkling-2-Mini-UD-Q3_K_XL` (GGUF v3, `13,631,779,296` bytes / ~12.70 GiB)
22
+ * **Modification Type:** Size-neutral, in-place quantization repack (`NVFP4` $\rightarrow$ `Q4_K` / `Q4_0`) using `ggml_quantize_chunk()` via `libggml-base.so.1`
23
+ * **Target Hardware:** GPUs lacking native OCP FP4 (`device->ocp_fp4 == 0`) or BF16 accumulation support—specifically **AMD Polaris** (e.g., Radeon RX 580) and **older NVIDIA GPUs** (e.g., Tesla / pre-Ada cards) running `llama.cpp` / Vulkan
24
+ * **File Size & Layout:** Byte-identical to the source GGUF (`13,631,779,296` bytes); all tensor offsets, padding, metadata, and non-NVFP4 tensors remain completely untouched
25
+
26
+ ---
27
+
28
+ ## Why This Repack Exists
29
+
30
+ On GPUs without native OCP FP4 hardware support (`fp4: 0`), `llama.cpp` falls back to software-side emulation for `GGML_TYPE_NVFP4` tensors:
31
+
32
+ 1. **Shared-Memory LUT Staging:** In `ggml-vulkan.cpp`, `if (src0_type == GGML_TYPE_NVFP4 && !device->ocp_fp4)` stages a 256-entry lookup table in shared memory to decode FP4 weights.
33
+ 2. **Forced BF16 Accumulation:** In `llama-graph.cpp`, `if (w->type == GGML_TYPE_NVFP4) ggml_prec_set_acc(res, GGML_PREC_BF16)` forces BF16 accumulation on every NVFP4 matmul, triggering a second software fallback on devices with `bf16: 0`.
34
+
35
+ Rewriting the 32 NVFP4 MoE expert tensors into **Q4_K** and **Q4_0** preserves the exact **4.5 bits per weight ($\frac{9}{16}$ byte/element)** density while routing those tensors through standard, first-class `llama.cpp` and Vulkan kernels.
36
+
37
+ ---
38
+
39
+ ## Quantization & Layout Design
40
+
41
+ `NVFP4`, `Q4_K`, and `Q4_0` all use **$\frac{9}{16}$ bytes (0.5625 B) per weight**:
42
+
43
+ | Format | Block Structure | Bytes per Element | Row Width Requirement |
44
+ | :--- | :--- | :--- | :--- |
45
+ | `NVFP4` (source) | 36 B / 64 weights | $\frac{9}{16}$ (0.5625 B) | `ne0 % 64 == 0` |
46
+ | `Q4_K` (target) | 144 B / 256 weights | $\frac{9}{16}$ (0.5625 B) | `ne0 % 256 == 0` |
47
+ | `Q4_0` (target) | 18 B / 32 weights | $\frac{9}{16}$ (0.5625 B) | `ne0 % 32 == 0` |
48
+
49
+ ### Per-Tensor Target Selection
50
+ Because `blk.N.ffn_down_exps.weight` has a row width of `1152` and $1152 \pmod{256} = 128$, `Q4_K` cannot represent those rows without altering tensor dimensions or file layout. The conversion script selects targets per tensor:
51
+ * **`ne0 % 256 == 0` $\rightarrow$ `Q4_K`:** Applied to `ffn_gate_up_exps.weight` (`ne0 = 2304`, `1296 B/row`), providing 6-bit sub-block scales.
52
+ * **Otherwise $\rightarrow$ `Q4_0`:** Applied to `ffn_down_exps.weight` (`ne0 = 1152`, `648 B/row`), keeping row byte sizes and file offsets identical.
53
+
54
+ ### Preserved Scale Arithmetic
55
+ * The **40 per-expert `*.scale` tensors are left untouched**.
56
+ * In `llama-graph.cpp` (`build_lora_mm`), expert scales multiply the matmul *output* (`res = ggml_mul(ctx0, res, w_s)`), not the stored weights directly. Keeping dequantized weight magnitudes at their original scale ($\approx \pm 1440$) preserves the downstream arithmetic.
57
+
58
+ ---
59
+
60
+ ## Conversion Summary & Verification
61
+
62
+ ### Tensor Breakdown (`Inkling-2-Mini-UD-Q3_K_XL.gguf` $\rightarrow$ `.q4.gguf`)
63
+ Converted across 32 expert tensors in 165 seconds (~49 MiB/s across 6 threads):
64
+
65
+ | Metric / Tensor Type | Before | After |
66
+ | :--- | :--- | :--- |
67
+ | `NVFP4` tensors | 32 tensors (7.83 GiB) | 0 tensors |
68
+ | `Q4_K` tensors | 3 | 15 (12 converted) |
69
+ | `Q4_0` tensors | 0 | 20 (converted) |
70
+ | Total file size | `13,631,779,296` bytes | `13,631,779,296` bytes |
71
+ | Tensor offsets & padding | Baseline | All identical |
72
+
73
+ ### Pre-Conversion Validation
74
+ * **NVFP4 Dequantization Parity:** Vectorized NumPy UE4M3 + E2M1 dequantization matched `ggml`'s `dequantize_row_nvfp4` via `ctypes` with `0.000e+00` max absolute difference across 5 random buffers.
75
+ * **Synthetic Round-Trip SNR:** `21.3 dB` for `Q4_0` (`ne0 = 1152`) and `23.0 dB` for `Q4_K` (`ne0 = 2304`).
76
+ * **Row-Size Neutrality:** `648 B/row` $\rightarrow$ `648 B/row` (`ne0 = 1152`) and `1296 B/row` $\rightarrow$ `1296 B/row` (`ne0 = 2304`).
77
+
78
+ ### Real-Weight Fidelity (512 Rows per Sampled Tensor vs. Decoded Source NVFP4)
79
+
80
+ | Tensor | Target Type | Signal RMS | Error RMS | SNR |
81
+ | :--- | :--- | :--- | :--- | :--- |
82
+ | `blk.6.ffn_gate_up_exps.weight` | `q4_K` | `483.12` | `33.80` | `23.10 dB` |
83
+ | `blk.11.ffn_gate_up_exps.weight` | `q4_K` | `219.06` | `15.41` | `23.06 dB` |
84
+ | `blk.6.ffn_down_exps.weight` | `q4_0` | `485.03` | `41.81` | `21.29 dB` |
85
+ | `blk.11.ffn_down_exps.weight` | `q4_0` | `170.03` | `14.93` | `21.13 dB` |
86
+
87
+ * **Untouched Tensor Check:** Confirmed byte-identical across `output.weight`, `blk.0.attn_q.weight`, `token_embd.weight`, and `blk.11.ffn_down_exps.scale`.
88
+
89
+ ---
90
+
91
+ ## Usage & Reproduction
92
+
93
+ ### Running with `llama.cpp`
94
+ Verify that `NVFP4` tensors are eliminated at load time:
95
+
96
+ ```bash
97
+ llama-server -m Inkling-2-Mini-UD-Q3_K_XL.q4.gguf -lv 4 ... 2>&1 | grep -- '- type'
98
+ ```
99
+
100
+ Expected output includes `nvfp4: 0`, `q4_K: 15`, and `q4_0: 20`.
101
+
102
+ ### Reproducing the Conversion (`nvfp4_to_q4.py`)
103
+ The conversion script requires only Python 3, `numpy`, and an existing `libggml-base.so.1` build:
104
+
105
+ ```bash
106
+ # Run built-in self-tests
107
+ python3 nvfp4_to_q4.py --selftest
108
+
109
+ # Inspect NVFP4 tensors and target mapping
110
+ python3 nvfp4_to_q4.py --list -i Inkling-2-Mini-UD-Q3_K_XL.gguf
111
+
112
+ # Perform size-neutral conversion
113
+ python3 nvfp4_to_q4.py -i Inkling-2-Mini-UD-Q3_K_XL.gguf -o Inkling-2-Mini-UD-Q3_K_XL.q4.gguf
114
+ ```
115
+
116
+ *(On NixOS / Nix environments, invoke via `nix-shell -p python3Packages.numpy --run "..."`.)*
117
+
118
+ ---
119
+
120
+ ## Limitations & Caveats
121
+
122
+ * **Second-Generation 4-Bit Quantization:** Repacking already-quantized `NVFP4` weights into `Q4_K`/`Q4_0` introduces one additional generation of 4-bit quantization error (~21–23 dB SNR relative to the decoded `NVFP4` weights), isolated to the MoE expert layers.
123
+ * **`Q4_0` on `1152`-Wide Down Projections:** `Q4_0` (~21.1–21.3 dB SNR) is used for `ffn_down_exps.weight` to preserve exact byte offsets without relayout. Higher-precision formats (such as `Q5_0` or `Q6_K`) would require shifting tensor offsets and increasing file size.
124
+ * **Memory Footprint Unchanged:** Because this repack is strictly size-neutral (~12.70 GiB total file size), VRAM capacity and CPU/RAM offload requirements remain identical to the base file.