Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/evidence/amd-speed-20261114/raw/four-bit/CONTAINER.md # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / evidence / amd-speed-20261114 / raw / four-bit / CONTAINER.md` 89 lines · 5570 bytes Raw Blame History Rendered 1 # Q4D8: the 4-bit weight layout and its arithmetic contract 2 3 Version `Q4D8-1`. Lane uxl-brief/FourBit, 2026-11-13. Reference implementation: `tools/q4format.py` 4 (Python, an outside instrument). This file is the contract shared by the conversion tool, the quality 5 harness and the gfx942 kernels. All integers are little-endian. 6 7 ## 1. What is stored for one matrix 8 9 A linear layer `y = W x` with `W` of shape M x K (M outputs, K inputs) is stored as two tensors. 10 11 | Tensor | dtype | shape | bytes | 12 |---|---|---|---| 13 | `NAME.weight_q4` | U8 | M x (Kp/2) | M*Kp/2 | 14 | `NAME.weight_q4_scale` | BF16 | M x (nseg*64) | M*nseg*128 | 15 16 - `Kp = 512 * ceil(K/512)`: each row is padded with zero nibbles to a whole number of 512-column steps. 17 - `T = Kp/512` is the number of steps in a row. `SEG` (steps per segment, 1..16) is chosen per 18 tensor and recorded in the shard metadata; `nseg = ceil(T/SEG)`. 19 - Column `c` (0 <= c < Kp) lives in dword `j = c div 8` of its row, at bits `[4e+3 : 4e]` with 20 `e = c mod 8`. Equivalently: byte `c div 2`, low nibble for even `c`, high nibble for odd `c`. 21 - A nibble is a two's-complement signed 4-bit integer `q` in -8..7. 22 - The scale group of column `c` is `(seg, lane)` with `lane = j mod 64`, `step = j div 64`, 23 `seg = step div SEG`. Its scale is entry `seg*64 + lane` of the row's scale vector. 24 A group therefore holds 8 consecutive columns from each step of its segment: at most `8*SEG` 25 (<= 128) weights. 26 - A scale is a BF16 value (the upper 16 bits of an IEEE binary32), finite and >= +0. A group whose 27 weights are all zero has scale +0 and all-zero nibbles. 28 - The real weight is `w[c] = q[c] * scale[group(c)]`. Padding columns have `q = 0`. 29 30 Files are safetensors shards with the source shard names. Converted modules carry the two tensors above 31 in place of `NAME.weight` (F8_E4M3) and `NAME.weight_scale_inv` (BF16); every other tensor is copied 32 byte for byte. The safetensors `__metadata__` map carries: `q4_format = "Q4D8-1"`, `q4_seg` (JSON object: `NAME.weight_q4` -> SEG), 33 `q4_source_repo`, `q4_source_rev`, `q4_source_shard_sha256`, `q4_tool_sha256`, `q4_kept` (names kept 34 in their source form). No timestamps: the same inputs give the same bytes. 35 36 A reader refuses (nonzero exit, nothing written) when: the format string differs, `q4_seg` is outside 37 1..16, a `weight_q4` second dimension is not a multiple of 256, the scale tensor's shape disagrees with 38 M, Kp and that tensor's SEG, a scale is negative, infinite or NaN, or the file is truncated. 39 40 ## 2. What is stored for one activation vector 41 42 The kernels multiply integers. A float vector `x` (binary32, length K) is first turned into `Xq`: 43 44 1. `m` = largest `|x[i]|`. If any `x[i]` is NaN or infinite: `xscale = NaN`, all digits zero. 45 If the biased exponent of `m` is below 32 (`m < 2^-95`, which includes zero): `xscale = +0`, all 46 digits zero. 47 2. Otherwise `E = floor(log2(m))`, `xq[i] = roundTiesToEven(x[i] * 2^(13-E))` (an integer with 48 `|xq[i]| <= 16384`), and `xscale = 2^(E-13)` as binary32. 49 3. `xq[i]` for `K <= i < Kp` is 0. 50 4. Each `xq[i]` is written as four signed base-16 digits `d0..d3` in -8..7 with 51 `xq = d0 + 16 d1 + 256 d2 + 4096 d3`. With `u = xq mod 65536`, `r = ((u + 0x8888) mod 65536) xor 0x8888`, 52 digit `p` is nibble `p` of `r` read as a signed 4-bit integer. 53 5. Buffer `X`: for each dword index `j` (0 <= j < Kp/8), 16 bytes at offset `16j`: four u32 values 54 `P0, P1, P2, P3`; nibble `e` of `Pp` is digit `p` of `xq[8j+e]`. After the last dword, at offset 55 `2*Kp`, 16 bytes: `xscale` (binary32) then 12 zero bytes. Total `2*Kp + 16` bytes. 56 57 ## 3. The product 58 59 For row `r`, lane `l`, segment `s`: 60 61 ``` 62 I[r,s,l] = sum over steps t in segment s, e in 0..7 of q[r, 8*(64t+l)+e] * xq[8*(64t+l)+e] (exact integer, |I| <= 2^24) 63 A[r,l] = fold over s ascending of fma(float32(I[r,s,l]), scale[r,s,l], A) starting from +0.0 64 R[r] = butterfly over the 64 lane values: for d = 32,16,8,4,2,1: A[l] = A[l] + A[l xor d]; R = A[0] 65 y[r] = R[r] * xscale (every + and * a binary32 round-to-nearest-even; oracle: kernels/gate.py) 66 ``` 67 68 `I` is exact because `|q| <= 8`, `|xq| <= 16384` and a group has at most 128 weights, so `|I| <= 2^24` 69 and its conversion to binary32 is exact. Each plane sum is one `V_DOT8C_I32_I4` per 8 weights (section 3 70 of FORMAT.md). Results whose magnitude is below `2^-126` are outside the bit-exact contract. 71 72 The quality harness uses the same real-number definition (`w = q*scale`, `x' = xq*xscale`, 73 `y = W x'`) with float32 matrix products. It does not reproduce the kernel's summation order. The difference 74 is bounded by about (nseg + 6)·2^-24·Σ|terms|, relative to the sum of absolute products, not to |y|, so it is 75 not small relative to y under cancellation (review finding 14). 76 77 ## 4. How the conversion picks `q` and `scale` 78 79 For each group with source weights `w` (real numbers `e4m3 * weight_scale_inv`, exact in binary64): 80 81 1. `a = max |w|`. If `a = 0`: scale +0, all `q = 0`. 82 2. For clip ratios `rho = 1.00, 0.97, 0.94, ..., 0.40` (21 values, in this order): 83 `s = bf16(rho * a / 7)` (binary64 product and quotient, then rounded to 8 significant bits, ties to 84 even; a result below 2^-126 is replaced by 2^-126), `q = clamp(roundTiesToEven(w / s), -8, 7)`, 85 `err = sum (w - q s)^2` (binary64, summed in ascending step order, then ascending `e`). 86 3. Keep the first `rho` with the smallest `err`. 87 88 Source E4M3 NaN encodings (0x7F, 0xFF) are refused: the tool stops and names the tensor. The BF16 lm_head 89 source is converted the same way with `w` = its BF16 values.