oxillama-quant 0.1.2

Quantization kernels for all GGUF quantization types
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Q4_K reference (naive) implementation.
//!
//! Q4_K block format (144 bytes per 256 weights):
//! - 2 bytes: FP16 super-block scale (d)
//! - 2 bytes: FP16 super-block minimum (dmin)
//! - 12 bytes: 8 sub-block scales + 8 sub-block mins, 6-bit each, packed
//! - 128 bytes: 256 × 4-bit unsigned nibbles packed (2 per byte)
//!
//! 8 sub-blocks of 32 weights each.
//! Weight formula: `w = d * scale_i * q - dmin * min_i` where q is 4-bit (0..15).
//!
//! Effective: 4.5 bits/weight.

use crate::error::{QuantError, QuantResult};
use crate::traits::QuantKernel;
use crate::types::QuantTensor;

const Q4_K_BLOCK_SIZE: usize = 256;
const Q4_K_BLOCK_BYTES: usize = 144;

/// Reference (naive scalar) Q4_K kernel.
pub struct Q4KRef;

/// Decode the 6-bit packed scales and mins for Q4_K.
///
/// Returns (scales[8], mins[8]) where each is a 6-bit value.
fn decode_scales_mins(scales_raw: &[u8]) -> ([u8; 8], [u8; 8]) {
    let mut sc = [0u8; 8];
    let mut mn = [0u8; 8];

    // Sub-blocks 0..3: straightforward 6-bit extraction
    for j in 0..4 {
        sc[j] = scales_raw[j] & 0x3F;
        mn[j] = scales_raw[j + 4] & 0x3F;
    }

    // Sub-blocks 4..7: assembled from high bits of bytes 0..3/4..7 and bytes 8..11
    for j in 4..8 {
        let lo_sc = scales_raw[j + 4] & 0x0F;
        let hi_sc = (scales_raw[j - 4] >> 6) & 0x03;
        sc[j] = lo_sc | (hi_sc << 4);

        let lo_mn = (scales_raw[j + 4] >> 4) & 0x0F;
        let hi_mn = (scales_raw[j] >> 6) & 0x03;
        mn[j] = lo_mn | (hi_mn << 4);
    }

    (sc, mn)
}

impl QuantKernel for Q4KRef {
    fn dequant_block(&self, block: &[u8], output: &mut [f32]) -> QuantResult<()> {
        if block.len() < Q4_K_BLOCK_BYTES {
            return Err(QuantError::BufferTooSmall {
                needed: Q4_K_BLOCK_BYTES,
                available: block.len(),
            });
        }
        if output.len() < Q4_K_BLOCK_SIZE {
            return Err(QuantError::BufferTooSmall {
                needed: Q4_K_BLOCK_SIZE,
                available: output.len(),
            });
        }

        let d = f16_to_f32(u16::from_le_bytes([block[0], block[1]]));
        let dmin = f16_to_f32(u16::from_le_bytes([block[2], block[3]]));
        let scales_raw = &block[4..16];
        let qs = &block[16..144]; // 128 bytes of nibble data

        let (sc, mn) = decode_scales_mins(scales_raw);

        // Process 4 groups of 64 weights (2 sub-blocks of 32 per group)
        let mut is = 0usize; // sub-block index
        let mut qs_offset = 0usize;
        let mut out_offset = 0usize;

        for _group in 0..4 {
            let d1 = d * sc[is] as f32;
            let m1 = dmin * mn[is] as f32;
            let d2 = d * sc[is + 1] as f32;
            let m2 = dmin * mn[is + 1] as f32;

            // Low nibbles → first 32 weights (sub-block `is`)
            for l in 0..32 {
                let q = (qs[qs_offset + l] & 0x0F) as f32;
                output[out_offset + l] = d1 * q - m1;
            }

            // High nibbles → next 32 weights (sub-block `is+1`)
            for l in 0..32 {
                let q = ((qs[qs_offset + l] >> 4) & 0x0F) as f32;
                output[out_offset + 32 + l] = d2 * q - m2;
            }

            is += 2;
            qs_offset += 32;
            out_offset += 64;
        }

        Ok(())
    }

    fn gemv(
        &self,
        quant_matrix: &QuantTensor,
        input: &[f32],
        output: &mut [f32],
    ) -> QuantResult<()> {
        let n_rows = quant_matrix.shape[0];
        let n_cols = if quant_matrix.shape.len() > 1 {
            quant_matrix.shape[1]
        } else {
            quant_matrix.n_elements() / n_rows
        };

        if input.len() < n_cols {
            return Err(QuantError::DimensionMismatch {
                expected: n_cols,
                got: input.len(),
            });
        }
        if output.len() < n_rows {
            return Err(QuantError::DimensionMismatch {
                expected: n_rows,
                got: output.len(),
            });
        }

        let blocks_per_row = n_cols.div_ceil(Q4_K_BLOCK_SIZE);
        let row_bytes = blocks_per_row * Q4_K_BLOCK_BYTES;

        for (row, out) in output.iter_mut().enumerate().take(n_rows) {
            let row_start = row * row_bytes;
            let mut sum = 0.0f32;

            for blk in 0..blocks_per_row {
                let block_offset = row_start + blk * Q4_K_BLOCK_BYTES;
                let block = &quant_matrix.data[block_offset..block_offset + Q4_K_BLOCK_BYTES];

                let d = f16_to_f32(u16::from_le_bytes([block[0], block[1]]));
                let dmin = f16_to_f32(u16::from_le_bytes([block[2], block[3]]));
                let scales_raw = &block[4..16];
                let qs = &block[16..144];
                let input_offset = blk * Q4_K_BLOCK_SIZE;

                let (sc, mn) = decode_scales_mins(scales_raw);

                let mut is = 0usize;
                let mut qs_off = 0usize;
                let mut w_off = input_offset;

                for _group in 0..4 {
                    let d1 = d * sc[is] as f32;
                    let m1 = dmin * mn[is] as f32;
                    let d2 = d * sc[is + 1] as f32;
                    let m2 = dmin * mn[is + 1] as f32;

                    for l in 0..32 {
                        let idx = w_off + l;
                        if idx < n_cols {
                            let q = (qs[qs_off + l] & 0x0F) as f32;
                            sum += (d1 * q - m1) * input[idx];
                        }
                    }
                    for l in 0..32 {
                        let idx = w_off + 32 + l;
                        if idx < n_cols {
                            let q = ((qs[qs_off + l] >> 4) & 0x0F) as f32;
                            sum += (d2 * q - m2) * input[idx];
                        }
                    }

                    is += 2;
                    qs_off += 32;
                    w_off += 64;
                }
            }

            *out = sum;
        }

        Ok(())
    }

    fn gemm(
        &self,
        quant_matrix: &QuantTensor,
        input: &[f32],
        output: &mut [f32],
        m: usize,
        n: usize,
        k: usize,
    ) -> QuantResult<()> {
        for row in 0..m {
            let input_row = &input[row * k..(row + 1) * k];
            let output_row = &mut output[row * n..(row + 1) * n];
            self.gemv(quant_matrix, input_row, output_row)?;
        }
        Ok(())
    }

    /// Override of `matvec_q8_fused` required because the trait default is wrong for Q4_K.
    ///
    /// # Why the default is broken for Q4_K
    /// The trait default assumes `blocks_per_row = n_cols / block_size` maps 1-to-1 with
    /// Q8_0 blocks.  For Q4_K (block_size=256), each Q4_K weight block spans 256 weights =
    /// 8 Q8_0 activation blocks (each 32 weights).  The default would only allocate
    /// `blocks_per_row * 34` bytes for activations (8× too small) and would panic with
    /// `a_scratch = vec![0f32; 32]` for `valid=256`.
    ///
    /// # Block mapping
    /// - 1 Q4_K weight block (144 bytes, 256 weights) ↔ 8 Q8_0 activation blocks (34 bytes each).
    /// - Sub-block `s` (0..8) of weight block `blk` uses Q8_0 activation at index `blk*8 + s`.
    ///
    /// # Formula per sub-block `s`
    /// `contrib_s = (d·sc[s]·d_a)·Σ(q_w·q_a) − (dmin·mn[s]·d_a)·Σ(q_a)`
    fn matvec_q8_fused(
        &self,
        weights: &[u8],
        acts_q8: &[u8],
        out: &mut [f32],
        n_rows: usize,
        n_cols: usize,
    ) -> QuantResult<()> {
        if out.len() < n_rows {
            return Err(QuantError::DimensionMismatch {
                expected: n_rows,
                got: out.len(),
            });
        }

        let blocks_per_row = n_cols.div_ceil(Q4_K_BLOCK_SIZE);
        let row_bytes = blocks_per_row * Q4_K_BLOCK_BYTES;
        // Each Q4_K block maps to 8 Q8_0 blocks.
        let q8_blocks_per_row = blocks_per_row * 8;
        let acts_needed = q8_blocks_per_row * Q8_0_BLOCK_BYTES;

        if weights.len() < n_rows * row_bytes {
            return Err(QuantError::BufferTooSmall {
                needed: n_rows * row_bytes,
                available: weights.len(),
            });
        }
        if acts_q8.len() < acts_needed {
            return Err(QuantError::BufferTooSmall {
                needed: acts_needed,
                available: acts_q8.len(),
            });
        }

        for (row, out_val) in out.iter_mut().enumerate().take(n_rows) {
            let row_start = row * row_bytes;
            let mut sum = 0.0f32;

            for blk in 0..blocks_per_row {
                let block_offset = row_start + blk * Q4_K_BLOCK_BYTES;
                let block = &weights[block_offset..block_offset + Q4_K_BLOCK_BYTES];

                let d = f16_to_f32(u16::from_le_bytes([block[0], block[1]]));
                let dmin = f16_to_f32(u16::from_le_bytes([block[2], block[3]]));
                let (sc, mn) = decode_scales_mins(&block[4..16]);
                let qs = &block[16..144]; // 128 nibble bytes

                let input_offset = blk * Q4_K_BLOCK_SIZE;

                let mut is = 0usize;
                let mut qs_off = 0usize;
                let mut w_off = input_offset;

                // 4 groups of 2 sub-blocks each.
                for _group in 0..4 {
                    // Sub-block `is` (lo nibbles) — Q8_0 activation block index `blk*8 + is`.
                    let a_idx_lo = blk * 8 + is;
                    let a_start_lo = a_idx_lo * Q8_0_BLOCK_BYTES;
                    let a_block_lo = &acts_q8[a_start_lo..a_start_lo + Q8_0_BLOCK_BYTES];
                    let d_a_lo = f16_to_f32(u16::from_le_bytes([a_block_lo[0], a_block_lo[1]]));
                    let q8_lo = &a_block_lo[2..]; // 32 i8 values

                    let da_lo = d * sc[is] as f32;
                    let m_lo = dmin * mn[is] as f32;

                    // Sub-block `is+1` (hi nibbles) — Q8_0 activation block index `blk*8 + is + 1`.
                    let a_idx_hi = blk * 8 + is + 1;
                    let a_start_hi = a_idx_hi * Q8_0_BLOCK_BYTES;
                    let a_block_hi = &acts_q8[a_start_hi..a_start_hi + Q8_0_BLOCK_BYTES];
                    let d_a_hi = f16_to_f32(u16::from_le_bytes([a_block_hi[0], a_block_hi[1]]));
                    let q8_hi = &a_block_hi[2..]; // 32 i8 values

                    let da_hi = d * sc[is + 1] as f32;
                    let m_hi = dmin * mn[is + 1] as f32;

                    // Lo nibbles → first 32 weights of this group (sub-block `is`).
                    // Formula: Σ (da_lo * q_w - m_lo) * q_a_lo
                    //        = da_lo * Σ(q_w * q_a_lo) - m_lo * Σ(q_a_lo)
                    let mut dot_lo = 0.0f32;
                    let mut sum_a_lo = 0.0f32;
                    for l in 0..32 {
                        let idx = w_off + l;
                        if idx < n_cols {
                            let q_w = (qs[qs_off + l] & 0x0F) as f32;
                            let q_a = q8_lo[l] as i8 as f32;
                            dot_lo += q_w * q_a;
                            sum_a_lo += q_a;
                        }
                    }
                    sum += (da_lo * dot_lo - m_lo * sum_a_lo) * d_a_lo;

                    // Hi nibbles → next 32 weights (sub-block `is+1`).
                    let mut dot_hi = 0.0f32;
                    let mut sum_a_hi = 0.0f32;
                    for l in 0..32 {
                        let idx = w_off + 32 + l;
                        if idx < n_cols {
                            let q_w = ((qs[qs_off + l] >> 4) & 0x0F) as f32;
                            let q_a = q8_hi[l] as i8 as f32;
                            dot_hi += q_w * q_a;
                            sum_a_hi += q_a;
                        }
                    }
                    sum += (da_hi * dot_hi - m_hi * sum_a_hi) * d_a_hi;

                    is += 2;
                    qs_off += 32;
                    w_off += 64;
                }
            }

            *out_val += sum; // ACCUMULATE
        }

        Ok(())
    }

    fn block_size(&self) -> usize {
        Q4_K_BLOCK_SIZE
    }

    fn block_bytes(&self) -> usize {
        Q4_K_BLOCK_BYTES
    }

    fn name(&self) -> &'static str {
        "Q4_K"
    }
}

/// Q8_0 block constants for the fused GEMV override.
const Q8_0_BLOCK_BYTES: usize = 34;

fn f16_to_f32(bits: u16) -> f32 {
    half::f16::from_bits(bits).to_f32()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_q4_k_block(d: f32, dmin: f32, scales: &[u8; 12], qs: &[u8; 128]) -> Vec<u8> {
        let mut block = Vec::with_capacity(Q4_K_BLOCK_BYTES);
        block.extend_from_slice(&half::f16::from_f32(d).to_bits().to_le_bytes());
        block.extend_from_slice(&half::f16::from_f32(dmin).to_bits().to_le_bytes());
        block.extend_from_slice(scales);
        block.extend_from_slice(qs);
        block
    }

    #[test]
    fn test_dequant_zero_scale() {
        // d=0, dmin=0 → all weights should be 0
        let block = make_q4_k_block(0.0, 0.0, &[0; 12], &[0; 128]);
        let kernel = Q4KRef;
        let mut output = vec![0.0f32; 256];
        kernel.dequant_block(&block, &mut output).unwrap();
        for &v in &output {
            assert!((v).abs() < 1e-5, "expected 0, got {v}");
        }
    }

    #[test]
    fn test_dequant_uniform() {
        // d=1.0, dmin=0.0, all scales=1 (sub-blocks 0..3), all nibbles=8
        // Weight = 1.0 * 1 * 8 - 0 = 8.0
        let mut scales = [0u8; 12];
        // Set sub-block scales 0..3 to 1 (lower 6 bits of bytes 0..3)
        scales[0] = 1;
        scales[1] = 1;
        scales[2] = 1;
        scales[3] = 1;
        // Sub-block scales 4..7: stored in bytes 8..11 lower 4 bits, with high bits from bytes 0..3 upper 2 bits
        scales[8] = 1;
        scales[9] = 1;
        scales[10] = 1;
        scales[11] = 1;

        // All nibbles = 8: byte = 0x88 (lo=8, hi=8)
        let qs = [0x88u8; 128];

        let block = make_q4_k_block(1.0, 0.0, &scales, &qs);
        let kernel = Q4KRef;
        let mut output = vec![0.0f32; 256];
        kernel.dequant_block(&block, &mut output).unwrap();

        // All weights should be 1.0 * 1 * 8 = 8.0
        for (i, &v) in output.iter().enumerate() {
            assert!((v - 8.0).abs() < 0.01, "weight[{i}] = {v}, expected 8.0");
        }
    }

    #[test]
    fn test_gemv_q4_k() {
        // Create a simple 1-row, 256-col Q4_K tensor
        // d=1.0, dmin=0, all scales=1, all nibbles=1
        // Weight = 1.0 * 1 * 1 - 0 = 1.0
        let mut scales = [0u8; 12];
        scales[..4].fill(1); // sub-blocks 0..3 scale=1
        scales[8..12].fill(1); // sub-blocks 4..7 scale=1

        // All nibbles = 1: lo=1, hi=1 → byte = 0x11
        let qs = [0x11u8; 128];

        let block = make_q4_k_block(1.0, 0.0, &scales, &qs);
        let tensor = QuantTensor::new(block, vec![1, 256], oxillama_gguf::GgufTensorType::Q4K);

        let input = vec![1.0f32; 256];
        let mut output = vec![0.0f32; 1];
        let kernel = Q4KRef;
        kernel.gemv(&tensor, &input, &mut output).unwrap();

        // All 256 weights = 1.0, all inputs = 1.0 → dot = 256.0
        assert!(
            (output[0] - 256.0).abs() < 1.0,
            "expected ~256.0, got {}",
            output[0]
        );
    }

    // ── matvec_q8_fused (Q4_K override) ──────────────────────────────────

    fn make_q8_0_block(scale: f32, qs: &[i8; 32]) -> Vec<u8> {
        let mut block = Vec::with_capacity(34);
        let d_bits = half::f16::from_f32(scale).to_bits();
        block.extend_from_slice(&d_bits.to_le_bytes());
        for &q in qs {
            block.push(q as u8);
        }
        block
    }

    #[test]
    fn test_q4k_fused_zero_activations() {
        // Zero activations → output must stay zero.
        let mut scales = [0u8; 12];
        scales[..4].fill(1);
        scales[8..12].fill(1);
        let qs = [0x88u8; 128]; // non-zero weights (q=8)
        let w_block = make_q4_k_block(1.0, 0.0, &scales, &qs);

        // 8 Q8_0 blocks all zero.
        let mut acts: Vec<u8> = Vec::new();
        for _ in 0..8 {
            acts.extend_from_slice(&make_q8_0_block(1.0, &[0i8; 32]));
        }

        let mut out = vec![0.0f32; 1];
        let kernel = Q4KRef;
        kernel
            .matvec_q8_fused(&w_block, &acts, &mut out, 1, 256)
            .expect("q4k fused zero acts");
        assert!(out[0].abs() < 1e-5, "expected 0, got {}", out[0]);
    }

    #[test]
    fn test_q4k_fused_accumulates() {
        // Verify ACCUMULATE semantics.
        let w_block = make_q4_k_block(0.0, 0.0, &[0u8; 12], &[0u8; 128]);

        let mut acts: Vec<u8> = Vec::new();
        for _ in 0..8 {
            acts.extend_from_slice(&make_q8_0_block(1.0, &[0i8; 32]));
        }

        let mut out = vec![42.0f32; 1];
        let kernel = Q4KRef;
        kernel
            .matvec_q8_fused(&w_block, &acts, &mut out, 1, 256)
            .expect("q4k fused accumulate");
        assert!(
            (out[0] - 42.0).abs() < 1e-5,
            "accumulation broken: got {}",
            out[0]
        );
    }

    #[test]
    fn test_q4k_fused_matches_unfused() {
        // Q4_K fused GEMV must match dequant + f32 GEMV (unfused) within tol 1e-3.
        let n_cols = 256usize;
        let mut scales = [0u8; 12];
        scales[0] = 5;
        scales[1] = 3;
        scales[2] = 7;
        scales[3] = 2;
        scales[4] = 4;
        scales[5] = 6;
        scales[6] = 1;
        scales[7] = 3;
        scales[8] = 9;
        scales[9] = 11;
        scales[10] = 13;
        scales[11] = 15;
        let qs_nibbles = [0xA5u8; 128]; // lo=5, hi=10
        let d_w = 0.5f32;
        let dmin_w = 0.1f32;
        let w_block = make_q4_k_block(d_w, dmin_w, &scales, &qs_nibbles);

        let d_a = 0.25f32;
        let q8_vals: [i8; 32] = [
            1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, 15, -16, -1, 2, -3, 4, -5, 6, -7,
            8, -9, 10, -11, 12, -13, 14, -15, 16,
        ];

        // Build 8 Q8_0 blocks (one per Q4_K sub-block).
        let mut acts: Vec<u8> = Vec::new();
        for _ in 0..8 {
            acts.extend_from_slice(&make_q8_0_block(d_a, &q8_vals));
        }

        // Build f32 input for unfused path (256 values = 8 × 32 i8 activations).
        let input: Vec<f32> = (0..8)
            .flat_map(|_| q8_vals.iter().map(|&q| q as f32 * d_a))
            .collect();

        let tensor = QuantTensor::new(
            w_block.clone(),
            vec![1, n_cols],
            oxillama_gguf::GgufTensorType::Q4K,
        );
        let kernel = Q4KRef;

        let mut out_unfused = vec![0.0f32; 1];
        kernel
            .gemv(&tensor, &input, &mut out_unfused)
            .expect("q4k unfused gemv");

        let mut out_fused = vec![0.0f32; 1];
        kernel
            .matvec_q8_fused(&w_block, &acts, &mut out_fused, 1, n_cols)
            .expect("q4k fused");

        let err = (out_fused[0] - out_unfused[0]).abs();
        assert!(
            err < 1e-3,
            "q4k fused vs unfused: fused={} unfused={} err={}",
            out_fused[0],
            out_unfused[0],
            err
        );
    }

    #[test]
    fn test_q4k_fused_multi_row() {
        // 4 rows × 256 cols.
        let n_rows = 4usize;
        let n_cols = 256usize;
        let mut scales = [0u8; 12];
        scales[..4].fill(1);
        scales[8..12].fill(1);
        let qs = [0x55u8; 128]; // lo=5, hi=5

        let d_a = 0.5f32;
        let q8_vals: [i8; 32] = [
            2, -1, 4, -3, 6, -5, 8, -7, 1, -2, 3, -4, 5, -6, 7, -8, -2, 1, -4, 3, -6, 5, -8, 7, -1,
            2, -3, 4, -5, 6, -7, 8,
        ];

        let scales_w = [0.5f32, 1.0f32, 0.25f32, 0.1f32];
        let mut weights: Vec<u8> = Vec::new();
        for &s in &scales_w {
            weights.extend_from_slice(&make_q4_k_block(s, 0.0, &scales, &qs));
        }

        // 8 Q8_0 blocks for 1 Q4_K block.
        let mut acts: Vec<u8> = Vec::new();
        for _ in 0..8 {
            acts.extend_from_slice(&make_q8_0_block(d_a, &q8_vals));
        }

        // Build f32 input (256 values).
        let input: Vec<f32> = (0..8)
            .flat_map(|_| q8_vals.iter().map(|&q| q as f32 * d_a))
            .collect();

        let kernel = Q4KRef;

        let mut out_unfused = vec![0.0f32; n_rows];
        for row in 0..n_rows {
            let row_start = row * Q4_K_BLOCK_BYTES;
            let row_data = weights[row_start..row_start + Q4_K_BLOCK_BYTES].to_vec();
            let tensor = QuantTensor::new(
                row_data,
                vec![1, n_cols],
                oxillama_gguf::GgufTensorType::Q4K,
            );
            kernel
                .gemv(&tensor, &input, &mut out_unfused[row..row + 1])
                .expect("q4k unfused row");
        }

        let mut out_fused = vec![0.0f32; n_rows];
        kernel
            .matvec_q8_fused(&weights, &acts, &mut out_fused, n_rows, n_cols)
            .expect("q4k fused multi-row");

        for i in 0..n_rows {
            let err = (out_fused[i] - out_unfused[i]).abs();
            assert!(
                err < 1e-3,
                "row {i}: fused={} unfused={} err={}",
                out_fused[i],
                out_unfused[i],
                err
            );
        }
    }
}