ferrox-cuda 0.20.0

CUDA/NVRTC kernels for the Ferrox inference engine
Documentation
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! The 256-element K-quant matvec kernels: Q2_K, Q3_K, Q4_K, Q5_K and
//! Q6_K.
//!
//! Moved here verbatim from `gpu.rs`; see [`super::legacy`] for why.
//! Q2_K and Q3_K joined on 2026-09-09 and have no coalesced twin.
//! These are the *uncoalesced* kernels. Q4_K, Q5_K, Q6_K and Q8_0 also
//! have coalesced rewrites, which still live in `gpu.rs` beside
//! `coalesced_matvec_kernel` because choosing between them is a launch
//! decision rather than a format one.

pub const Q4_K_MATVEC_KERNEL_SRC: &str = r#"
extern "C" __device__ float ferrox_f16_to_f32(unsigned short bits) {
    unsigned int sign = (bits >> 15) & 0x1u;
    unsigned int exp = (bits >> 10) & 0x1Fu;
    unsigned int mant = bits & 0x3FFu;
    float scale;
    if (exp == 0) {
        scale = ldexpf((float)mant, -24);
    } else if (exp == 31) {
        scale = mant ? __int_as_float(0x7fc00000) : __int_as_float(0x7f800000);
    } else {
        scale = ldexpf((float)(mant | 0x400), (int)exp - 25);
    }
    return sign ? -scale : scale;
}

extern "C" __device__ void ferrox_q4_k_scale_min(
    int j, const unsigned char* scales, unsigned char* sc, unsigned char* m
) {
    if (j < 4) {
        *sc = scales[j] & 63;
        *m = scales[j + 4] & 63;
    } else {
        *sc = (scales[j + 4] & 0x0F) | ((scales[j - 4] >> 6) << 4);
        *m = (scales[j + 4] >> 4) | ((scales[j] >> 6) << 4);
    }
}

extern "C" __global__ void q4_k_matvec(
    const unsigned char* weights, // [rows * row_bytes], row_bytes = n_blocks_per_row * 144
    const float* x,               // [cols]
    float* out,                   // [rows]
    int rows,
    int row_bytes,
    int n_blocks_per_row
) {
    int row = blockIdx.x;
    if (row >= rows) return;
    const unsigned char* row_ptr = weights + (size_t)row * row_bytes;

    __shared__ float partial[256];
    float acc = 0.0f;

    for (int blk = threadIdx.x; blk < n_blocks_per_row; blk += blockDim.x) {
        const unsigned char* block = row_ptr + blk * 144;
        unsigned short d_bits = (unsigned short)block[0] | ((unsigned short)block[1] << 8);
        unsigned short dmin_bits = (unsigned short)block[2] | ((unsigned short)block[3] << 8);
        float d = ferrox_f16_to_f32(d_bits);
        float dmin = ferrox_f16_to_f32(dmin_bits);
        const unsigned char* scales = block + 4;
        const unsigned char* qs = block + 16;
        int x_base = blk * 256;

        int is = 0, q_off = 0, base = 0;
        #pragma unroll
        for (int oi = 0; oi < 4; oi++) {
            unsigned char sc1, m1, sc2, m2;
            ferrox_q4_k_scale_min(is, scales, &sc1, &m1);
            ferrox_q4_k_scale_min(is + 1, scales, &sc2, &m2);
            float d1 = d * (float)sc1, min1 = dmin * (float)m1;
            float d2 = d * (float)sc2, min2 = dmin * (float)m2;
            #pragma unroll
            for (int l = 0; l < 32; l++) {
                acc += (d1 * (float)(qs[q_off + l] & 0x0F) - min1) * x[x_base + base + l];
            }
            #pragma unroll
            for (int l = 0; l < 32; l++) {
                acc += (d2 * (float)(qs[q_off + l] >> 4) - min2) * x[x_base + base + 32 + l];
            }
            q_off += 32;
            base += 64;
            is += 2;
        }
    }

    partial[threadIdx.x] = acc;
    __syncthreads();
    for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) {
        if (threadIdx.x < stride) {
            partial[threadIdx.x] += partial[threadIdx.x + stride];
        }
        __syncthreads();
    }
    if (threadIdx.x == 0) {
        out[row] = partial[0];
    }
}
"#;

/// CUDA C source for a fused Q5_K dequant+dot kernel: same super-block/
/// scale-min structure as Q4_K, but each nibble gets a 5th bit from a
/// 32-byte `qh` buffer (mirrors `ferrox_quant::dot_q5_k_f32_scalar`
/// exactly: 176-byte blocks = 2-byte `d` + 2-byte `dmin` + 12 bytes
/// scales + 32 bytes `qh` + 128 bytes `qs`).
///
/// Verified: compiled by NVRTC and executed on a real GPU, matching
/// the CPU reference exactly -- see the module doc comment.
pub const Q5_K_MATVEC_KERNEL_SRC: &str = r#"
extern "C" __device__ float ferrox_f16_to_f32(unsigned short bits) {
    unsigned int sign = (bits >> 15) & 0x1u;
    unsigned int exp = (bits >> 10) & 0x1Fu;
    unsigned int mant = bits & 0x3FFu;
    float scale;
    if (exp == 0) {
        scale = ldexpf((float)mant, -24);
    } else if (exp == 31) {
        scale = mant ? __int_as_float(0x7fc00000) : __int_as_float(0x7f800000);
    } else {
        scale = ldexpf((float)(mant | 0x400), (int)exp - 25);
    }
    return sign ? -scale : scale;
}

extern "C" __device__ void ferrox_q4_k_scale_min(
    int j, const unsigned char* scales, unsigned char* sc, unsigned char* m
) {
    if (j < 4) {
        *sc = scales[j] & 63;
        *m = scales[j + 4] & 63;
    } else {
        *sc = (scales[j + 4] & 0x0F) | ((scales[j - 4] >> 6) << 4);
        *m = (scales[j + 4] >> 4) | ((scales[j] >> 6) << 4);
    }
}

extern "C" __global__ void q5_k_matvec(
    const unsigned char* weights, // [rows * row_bytes], row_bytes = n_blocks_per_row * 176
    const float* x,               // [cols]
    float* out,                   // [rows]
    int rows,
    int row_bytes,
    int n_blocks_per_row
) {
    int row = blockIdx.x;
    if (row >= rows) return;
    const unsigned char* row_ptr = weights + (size_t)row * row_bytes;

    __shared__ float partial[256];
    float acc = 0.0f;

    for (int blk = threadIdx.x; blk < n_blocks_per_row; blk += blockDim.x) {
        const unsigned char* block = row_ptr + blk * 176;
        unsigned short d_bits = (unsigned short)block[0] | ((unsigned short)block[1] << 8);
        unsigned short dmin_bits = (unsigned short)block[2] | ((unsigned short)block[3] << 8);
        float d = ferrox_f16_to_f32(d_bits);
        float dmin = ferrox_f16_to_f32(dmin_bits);
        const unsigned char* scales = block + 4;
        const unsigned char* qh = block + 16;
        const unsigned char* qs = block + 48;
        int x_base = blk * 256;

        int is = 0;
        unsigned char u1 = 1, u2 = 2;
        #pragma unroll
        for (int oi = 0; oi < 4; oi++) {
            unsigned char sc1, m1, sc2, m2;
            ferrox_q4_k_scale_min(is, scales, &sc1, &m1);
            ferrox_q4_k_scale_min(is + 1, scales, &sc2, &m2);
            float d1 = d * (float)sc1, min1 = dmin * (float)m1;
            float d2 = d * (float)sc2, min2 = dmin * (float)m2;
            const unsigned char* ql = qs + oi * 32;
            int xb = x_base + oi * 64;
            #pragma unroll
            for (int l = 0; l < 32; l++) {
                int hi = (qh[l] & u1) ? 16 : 0;
                acc += (d1 * (float)((ql[l] & 0x0F) + hi) - min1) * x[xb + l];
            }
            #pragma unroll
            for (int l = 0; l < 32; l++) {
                int hi = (qh[l] & u2) ? 16 : 0;
                acc += (d2 * (float)((ql[l] >> 4) + hi) - min2) * x[xb + 32 + l];
            }
            is += 2;
            u1 <<= 2;
            u2 <<= 2;
        }
    }

    partial[threadIdx.x] = acc;
    __syncthreads();
    for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) {
        if (threadIdx.x < stride) {
            partial[threadIdx.x] += partial[threadIdx.x + stride];
        }
        __syncthreads();
    }
    if (threadIdx.x == 0) {
        out[row] = partial[0];
    }
}
"#;

/// CUDA C source for a fused Q6_K dequant+dot kernel: mirrors
/// `ferrox_quant::dot_q6_k_f32_scalar`'s exact math (210-byte blocks of
/// 256 elements: 128 bytes `ql` + 64 bytes `qh` + 16 *signed* int8
/// scale bytes + 2-byte `d`, split into two 128-element halves). The
/// 16 per-sub-block scales are signed in the GGUF Q6_K format --
/// an earlier version of this kernel (and of the scalar CPU path it
/// mirrors) read them as unsigned, which agreed with itself but not
/// with the format; both were fixed together and are covered by the
/// negative-scale golden in `ferrox-quant`
/// (`q6_k_signed_scale_dequant_matches_independent_python_reference`).
pub const Q6_K_MATVEC_KERNEL_SRC: &str = r#"
extern "C" __device__ float ferrox_f16_to_f32(unsigned short bits) {
    unsigned int sign = (bits >> 15) & 0x1u;
    unsigned int exp = (bits >> 10) & 0x1Fu;
    unsigned int mant = bits & 0x3FFu;
    float scale;
    if (exp == 0) {
        scale = ldexpf((float)mant, -24);
    } else if (exp == 31) {
        scale = mant ? __int_as_float(0x7fc00000) : __int_as_float(0x7f800000);
    } else {
        scale = ldexpf((float)(mant | 0x400), (int)exp - 25);
    }
    return sign ? -scale : scale;
}

extern "C" __global__ void q6_k_matvec(
    const unsigned char* weights, // [rows * row_bytes], row_bytes = n_blocks_per_row * 210
    const float* x,               // [cols]
    float* out,                   // [rows]
    int rows,
    int row_bytes,
    int n_blocks_per_row
) {
    int row = blockIdx.x;
    if (row >= rows) return;
    const unsigned char* row_ptr = weights + (size_t)row * row_bytes;

    __shared__ float partial[256];
    float acc = 0.0f;

    for (int blk = threadIdx.x; blk < n_blocks_per_row; blk += blockDim.x) {
        const unsigned char* block = row_ptr + blk * 210;
        const unsigned char* ql_full = block;
        const unsigned char* qh_full = block + 128;
        const unsigned char* sc_full = block + 192;
        unsigned short d_bits = (unsigned short)block[208] | ((unsigned short)block[209] << 8);
        float d = ferrox_f16_to_f32(d_bits);
        int x_base = blk * 256;

        #pragma unroll
        for (int half = 0; half < 2; half++) {
            const unsigned char* ql = ql_full + half * 64;
            const unsigned char* qh = qh_full + half * 32;
            const unsigned char* sc = sc_full + half * 8;
            int xh_base = x_base + half * 128;

            #pragma unroll
            for (int l = 0; l < 32; l++) {
                int is = l / 16;
                int q1 = (int)((ql[l] & 0x0F) | ((qh[l] & 0x03) << 4)) - 32;
                int q2 = (int)((ql[l + 32] & 0x0F) | (((qh[l] >> 2) & 0x03) << 4)) - 32;
                int q3 = (int)((ql[l] >> 4) | (((qh[l] >> 4) & 0x03) << 4)) - 32;
                int q4 = (int)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 0x03) << 4)) - 32;
                acc += d * (float)(signed char)sc[is] * (float)q1 * x[xh_base + l];
                acc += d * (float)(signed char)sc[is + 2] * (float)q2 * x[xh_base + l + 32];
                acc += d * (float)(signed char)sc[is + 4] * (float)q3 * x[xh_base + l + 64];
                acc += d * (float)(signed char)sc[is + 6] * (float)q4 * x[xh_base + l + 96];
            }
        }
    }

    partial[threadIdx.x] = acc;
    __syncthreads();
    for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) {
        if (threadIdx.x < stride) {
            partial[threadIdx.x] += partial[threadIdx.x + stride];
        }
        __syncthreads();
    }
    if (threadIdx.x == 0) {
        out[row] = partial[0];
    }
}
"#;

/// Fused Q2_K dequant+dot. 84-byte super-blocks of 256 elements:
/// 16 scale bytes FIRST, then 64 bytes of packed 2-bit quants, then
/// `half d` and `half dmin` at the END.
///
/// Each scale byte holds two 4-bit fields: the low nibble scales the
/// quant, the high nibble scales the min that is subtracted. Mirrors
/// `ferrox_quant::dequant_q2_k`'s loop order exactly -- `n` over the
/// two 128-element halves, `j` over the four 2-bit fields
/// (`shift = 2j`), then two 16-element halves with consecutive scale
/// indices -- because that order IS the element order, and a kernel
/// that walks it differently pairs every quant with the wrong
/// activation.
///
/// **UNVERIFIED ON HARDWARE.** No GPU has run this, and there is no
/// host harness for a matvec kernel.
pub const Q2_K_MATVEC_KERNEL_SRC: &str = r#"
extern "C" __global__ void q2_k_matvec(
    const unsigned char* weights, // [rows * row_bytes]
    const float* x,               // [cols]
    float* out,                   // [rows]
    int rows,
    int row_bytes,
    int n_blocks_per_row
) {
    int row = blockIdx.x;
    if (row >= rows) return;

    const unsigned char* row_ptr = weights + (size_t)row * row_bytes;

    __shared__ float partial[256];
    float acc = 0.0f;

    for (int b = threadIdx.x; b < n_blocks_per_row; b += blockDim.x) {
        const unsigned char* block = row_ptr + (size_t)b * 84;
        const unsigned char* scales = block;
        const unsigned char* qs = block + 16;

        unsigned short dbits = (unsigned short)block[80] | ((unsigned short)block[81] << 8);
        unsigned short mbits = (unsigned short)block[82] | ((unsigned short)block[83] << 8);
        float dv[2];
        unsigned short src[2];
        src[0] = dbits;
        src[1] = mbits;
        #pragma unroll
        for (int k = 0; k < 2; k++) {
            unsigned int sign = (src[k] >> 15) & 0x1u;
            unsigned int exp = (src[k] >> 10) & 0x1Fu;
            unsigned int mant = src[k] & 0x3FFu;
            float v;
            if (exp == 0) {
                v = ldexpf((float)mant, -24);
            } else if (exp == 31) {
                v = mant ? __int_as_float(0x7fc00000) : __int_as_float(0x7f800000);
            } else {
                v = ldexpf((float)(mant | 0x400), (int)exp - 25);
            }
            dv[k] = sign ? -v : v;
        }
        const float d = dv[0];
        const float dmin = dv[1];

        int base = b * 256;
        int idx = 0;
        int is = 0;
        for (int n = 0; n < 2; n++) {
            const unsigned char* q = qs + 32 * n;
            int shift = 0;
            for (int j = 0; j < 4; j++) {
                #pragma unroll
                for (int half = 0; half < 2; half++) {
                    unsigned char sc = scales[is++];
                    float dl = d * (float)(sc & 0x0F);
                    float ml = dmin * (float)(sc >> 4);
                    const unsigned char* qh = q + 16 * half;
                    #pragma unroll
                    for (int l = 0; l < 16; l++) {
                        acc += (dl * (float)((qh[l] >> shift) & 3) - ml) * x[base + idx];
                        idx++;
                    }
                }
                shift += 2;
            }
        }
    }

    partial[threadIdx.x] = acc;
    __syncthreads();

    for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) {
        if (threadIdx.x < stride) {
            partial[threadIdx.x] += partial[threadIdx.x + stride];
        }
        __syncthreads();
    }

    if (threadIdx.x == 0) {
        out[row] = partial[0];
    }
}
"#;

/// Fused Q3_K dequant+dot. 110-byte super-blocks of 256 elements: 32
/// `hmask` bytes, 64 bytes of packed 2-bit quants, 12 scale bytes,
/// `half d`.
///
/// The third bit of each quant is a BIT PLANE in `hmask`, and it is
/// INVERTED: a set bit means bias 0, a clear bit means bias 4. `m`
/// sweeps all eight bit positions across the whole super-block rather
/// than restarting per half. Mirrors `ferrox_quant::dequant_q3_k`'s
/// loop order, which is the element order.
///
/// **UNVERIFIED ON HARDWARE.** No GPU has run this, and there is no
/// host harness for a matvec kernel.
pub const Q3_K_MATVEC_KERNEL_SRC: &str = r#"
__device__ __forceinline__ unsigned char ferrox_q3_k_scale_mv(
    const unsigned char* raw, int is
) {
    if (is < 4) {
        return (unsigned char)((raw[is] & 0xF) | (((raw[is + 8] >> 0) & 3) << 4));
    } else if (is < 8) {
        return (unsigned char)((raw[is] & 0xF) | (((raw[is + 4] >> 2) & 3) << 4));
    } else if (is < 12) {
        return (unsigned char)((raw[is - 8] >> 4) | (((raw[is] >> 4) & 3) << 4));
    } else {
        return (unsigned char)((raw[is - 8] >> 4) | (((raw[is - 4] >> 6) & 3) << 4));
    }
}

extern "C" __global__ void q3_k_matvec(
    const unsigned char* weights, // [rows * row_bytes]
    const float* x,               // [cols]
    float* out,                   // [rows]
    int rows,
    int row_bytes,
    int n_blocks_per_row
) {
    int row = blockIdx.x;
    if (row >= rows) return;

    const unsigned char* row_ptr = weights + (size_t)row * row_bytes;

    __shared__ float partial[256];
    float acc = 0.0f;

    for (int b = threadIdx.x; b < n_blocks_per_row; b += blockDim.x) {
        const unsigned char* block = row_ptr + (size_t)b * 110;
        const unsigned char* hmask = block;
        const unsigned char* qs = block + 32;
        const unsigned char* scales = block + 96;

        unsigned short bits = (unsigned short)block[108] | ((unsigned short)block[109] << 8);
        unsigned int sign = (bits >> 15) & 0x1u;
        unsigned int exp = (bits >> 10) & 0x1Fu;
        unsigned int mant = bits & 0x3FFu;
        float d_all;
        if (exp == 0) {
            d_all = ldexpf((float)mant, -24);
        } else if (exp == 31) {
            d_all = mant ? __int_as_float(0x7fc00000) : __int_as_float(0x7f800000);
        } else {
            d_all = ldexpf((float)(mant | 0x400), (int)exp - 25);
        }
        if (sign) d_all = -d_all;

        int base = b * 256;
        int idx = 0;
        int is = 0;
        unsigned char m = 1;
        for (int n = 0; n < 2; n++) {
            const unsigned char* q = qs + 32 * n;
            int shift = 0;
            for (int j = 0; j < 4; j++) {
                #pragma unroll
                for (int half = 0; half < 2; half++) {
                    float dl = d_all * ((float)ferrox_q3_k_scale_mv(scales, is) - 32.0f);
                    is++;
                    const unsigned char* qh = q + 16 * half;
                    const unsigned char* hh = hmask + 16 * half;
                    #pragma unroll
                    for (int l = 0; l < 16; l++) {
                        int raw = (int)((qh[l] >> shift) & 3);
                        int bias = (hh[l] & m) ? 0 : 4;
                        acc += dl * (float)(raw - bias) * x[base + idx];
                        idx++;
                    }
                }
                shift += 2;
                m <<= 1;
            }
        }
    }

    partial[threadIdx.x] = acc;
    __syncthreads();

    for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) {
        if (threadIdx.x < stride) {
            partial[threadIdx.x] += partial[threadIdx.x + stride];
        }
        __syncthreads();
    }

    if (threadIdx.x == 0) {
        out[row] = partial[0];
    }
}
"#;