Skip to main content

engram_parser/gguf/
dequant.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Packed-block dequantization for Q8_0, Q5_K, Q6_K, and IQ3_M.
4//!
5//! These helpers operate on already-sliced packed bytes (owned `Vec<u8>` or
6//! mmap-backed `&[u8]`). They perform no CUDA / host-register work.
7//!
8//! HuggingFace “IQ3_M” is a mixed-quant *preset*, not GGUF wire type 31.
9//! Wire 31 remains historical [`super::tensor::GGML_TYPE_Q4_0_4_4`]. The
10//! 111-byte IQ3_M block decoder uses the internal non-wire id
11//! [`super::tensor::GGML_TYPE_IQ3_M_BLOCK`].
12
13use super::tensor::{
14    DType, GGML_TYPE_IQ3_M_BLOCK, GGML_TYPE_Q5_K, GGML_TYPE_Q6_K, GGML_TYPE_Q8_0, f16_bits_to_f32,
15};
16use crate::error::{ParserError, Result};
17
18const Q8_0_BLOCK: usize = 32;
19const Q8_0_BYTES: usize = 34;
20const K_BLOCK: usize = 256;
21const Q5_K_BYTES: usize = 176;
22const Q6_K_BYTES: usize = 210;
23const IQ3_M_BYTES: usize = 111;
24
25/// Packed byte length of one row for a blocked quant dtype.
26pub fn packed_row_size(ggml_type: u32, width: usize) -> Result<usize> {
27    match ggml_type {
28        GGML_TYPE_Q8_0 => row_size_for_block(width, Q8_0_BLOCK, Q8_0_BYTES, "Q8_0"),
29        GGML_TYPE_Q5_K => row_size_for_block(width, K_BLOCK, Q5_K_BYTES, "Q5_K"),
30        GGML_TYPE_Q6_K => row_size_for_block(width, K_BLOCK, Q6_K_BYTES, "Q6_K"),
31        GGML_TYPE_IQ3_M_BLOCK => row_size_for_block(width, K_BLOCK, IQ3_M_BYTES, "IQ3_M"),
32        other => Err(unsupported(format!(
33            "row-size lookup is not implemented for ggml_type={other}"
34        ))),
35    }
36}
37
38/// Dequantize packed Q8_0 bytes (block size 32) to `f32`.
39///
40/// `dims` is GGUF innermost-first; `dims[0]` is the row width.
41pub fn dequantize_q8_0(packed: &[u8], dims: &[usize]) -> Result<Vec<f32>> {
42    dequantize_rows(
43        packed,
44        dims,
45        Q8_0_BLOCK,
46        Q8_0_BYTES,
47        "Q8_0",
48        dequantize_row_q8_0,
49    )
50}
51
52/// Dequantize packed Q5_K bytes (block size 256) to `f32`.
53pub fn dequantize_q5_k(packed: &[u8], dims: &[usize]) -> Result<Vec<f32>> {
54    dequantize_rows(
55        packed,
56        dims,
57        K_BLOCK,
58        Q5_K_BYTES,
59        "Q5_K",
60        dequantize_row_q5_k,
61    )
62}
63
64/// Dequantize packed Q6_K bytes (block size 256) to `f32`.
65pub fn dequantize_q6_k(packed: &[u8], dims: &[usize]) -> Result<Vec<f32>> {
66    dequantize_rows(
67        packed,
68        dims,
69        K_BLOCK,
70        Q6_K_BYTES,
71        "Q6_K",
72        dequantize_row_q6_k,
73    )
74}
75
76/// Dequantize packed IQ3_M *block-layout* bytes (111 bytes / 256 values) to `f32`.
77///
78/// This is **not** GGUF wire type 31 (`Q4_0_4_4`). See [`GGML_TYPE_IQ3_M_BLOCK`].
79pub fn dequantize_iq3_m(packed: &[u8], dims: &[usize]) -> Result<Vec<f32>> {
80    dequantize_rows(
81        packed,
82        dims,
83        K_BLOCK,
84        IQ3_M_BYTES,
85        "IQ3_M",
86        dequantize_row_iq3_m,
87    )
88}
89
90/// Dispatch packed dequant for the dtypes this crate implements.
91///
92/// Unknown / unimplemented dtypes fail closed. Adding a [`DType`] variant
93/// without updating this match is a compile error.
94pub fn dequantize_packed(dtype: DType, packed: &[u8], dims: &[usize]) -> Result<Vec<f32>> {
95    match dtype {
96        DType::Q8_0 => dequantize_q8_0(packed, dims),
97        DType::Q5_K => dequantize_q5_k(packed, dims),
98        DType::Q6_K => dequantize_q6_k(packed, dims),
99        DType::IQ3_M_BLOCK => dequantize_iq3_m(packed, dims),
100        other => Err(fail_closed_dequant(other)),
101    }
102}
103
104fn fail_closed_dequant(dtype: DType) -> ParserError {
105    // Exhaustive: a new `DType` variant that is not handled above must be
106    // named here (or in the `dequantize_packed` match) or the crate will not
107    // compile. The `never` binding documents the fail-closed default.
108    let unknown: DType = dtype;
109    match unknown {
110        DType::Q8_0 | DType::Q5_K | DType::Q6_K | DType::IQ3_M_BLOCK => {
111            unreachable!("handled by dequantize_packed")
112        }
113        DType::F32
114        | DType::F16
115        | DType::Q4_0
116        | DType::Q4_1
117        | DType::Q5_0
118        | DType::Q5_1
119        | DType::Q8_1
120        | DType::Q2_K
121        | DType::Q3_K
122        | DType::Q4_K
123        | DType::Q8_K
124        | DType::IQ2_XXS
125        | DType::IQ2_XS
126        | DType::IQ3_XXS
127        | DType::IQ1_S
128        | DType::IQ4_NL
129        | DType::IQ3_S
130        | DType::IQ2_S
131        | DType::IQ4_XS
132        | DType::IQ1_M
133        | DType::BF16
134        | DType::F64
135        | DType::I8
136        | DType::I16
137        | DType::I32
138        | DType::I64
139        | DType::Other(_) => unsupported(format!(
140            "no packed dequant for dtype {} (ggml_type={})",
141            unknown.label(),
142            unknown.ggml_type()
143        )),
144    }
145}
146
147pub(crate) fn dequantize_row_q8_0(row: &[u8], width: usize) -> Result<Vec<f32>> {
148    require_multiple(width, Q8_0_BLOCK, "Q8_0")?;
149    expect_row_len(row, width, Q8_0_BLOCK, Q8_0_BYTES, "Q8_0")?;
150    let mut out = Vec::with_capacity(width);
151    let (blocks, _) = row.as_chunks::<Q8_0_BYTES>();
152    for block in blocks {
153        let d = f16_bits_to_f32(u16::from_le_bytes([block[0], block[1]]));
154        for &quant in &block[2..Q8_0_BYTES] {
155            out.push((quant as i8) as f32 * d);
156        }
157    }
158    Ok(out)
159}
160
161pub(crate) fn dequantize_row_q5_k(row: &[u8], width: usize) -> Result<Vec<f32>> {
162    require_multiple(width, K_BLOCK, "Q5_K")?;
163    expect_row_len(row, width, K_BLOCK, Q5_K_BYTES, "Q5_K")?;
164    let mut out = Vec::with_capacity(width);
165    let (blocks, _) = row.as_chunks::<Q5_K_BYTES>();
166    for block in blocks {
167        let d = f16_bits_to_f32(u16::from_le_bytes([block[0], block[1]]));
168        let dmin = f16_bits_to_f32(u16::from_le_bytes([block[2], block[3]]));
169        let scales = &block[4..16];
170        let qh = &block[16..48];
171        let ql = &block[48..Q5_K_BYTES];
172
173        let mut is = 0usize;
174        let mut u1 = 1u8;
175        let mut u2 = 2u8;
176
177        let (ql_chunks, _) = ql.as_chunks::<32>();
178        for ql_chunk in ql_chunks {
179            let (sc1, m1) = scale_min_k4(is, scales);
180            let (sc2, m2) = scale_min_k4(is + 1, scales);
181            let d1 = d * sc1 as f32;
182            let mn1 = dmin * m1 as f32;
183            let d2 = d * sc2 as f32;
184            let mn2 = dmin * m2 as f32;
185
186            // ggml writes 32 low nibbles, then 32 high nibbles (`y[l]`, `y[l+32]`).
187            let mut group = [0f32; 64];
188            for (lane, &q) in ql_chunk.iter().enumerate() {
189                let qh_byte = qh[lane];
190                let hi1 = if qh_byte & u1 != 0 { 16 } else { 0 };
191                let hi2 = if qh_byte & u2 != 0 { 16 } else { 0 };
192                group[lane] = d1 * ((q & 0x0F) + hi1) as f32 - mn1;
193                group[lane + 32] = d2 * ((q >> 4) + hi2) as f32 - mn2;
194            }
195            out.extend_from_slice(&group);
196
197            is += 2;
198            u1 <<= 2;
199            u2 <<= 2;
200        }
201    }
202    Ok(out)
203}
204
205pub(crate) fn dequantize_row_q6_k(row: &[u8], width: usize) -> Result<Vec<f32>> {
206    require_multiple(width, K_BLOCK, "Q6_K")?;
207    expect_row_len(row, width, K_BLOCK, Q6_K_BYTES, "Q6_K")?;
208    let mut out = Vec::with_capacity(width);
209    let (blocks, _) = row.as_chunks::<Q6_K_BYTES>();
210    for block in blocks {
211        // ggml `block_q6_K`: ql(128) + qh(64) + scales(16) + d(2).
212        let ql = &block[0..128];
213        let qh = &block[128..192];
214        let scales = &block[192..208];
215        let d = f16_bits_to_f32(u16::from_le_bytes([block[208], block[209]]));
216        let mut decoded = [0f32; K_BLOCK];
217        for pass in 0..2usize {
218            let y = &mut decoded[pass * 128..];
219            let base = pass * 64;
220            let qh_base = pass * 32;
221            let sc_pass_base = pass * 8;
222            for l in 0..32 {
223                let is = l / 16;
224                let q1 = ((ql[base + l] & 0x0F) | ((qh[qh_base + l] & 3) << 4)) as i8 - 32;
225                let q2 =
226                    ((ql[base + 32 + l] & 0x0F) | (((qh[qh_base + l] >> 2) & 3) << 4)) as i8 - 32;
227                let q3 = ((ql[base + l] >> 4) | (((qh[qh_base + l] >> 4) & 3) << 4)) as i8 - 32;
228                let q4 =
229                    ((ql[base + 32 + l] >> 4) | (((qh[qh_base + l] >> 6) & 3) << 4)) as i8 - 32;
230                y[l] = d * (scales[sc_pass_base + is] as i8) as f32 * q1 as f32;
231                y[l + 32] = d * (scales[sc_pass_base + is + 2] as i8) as f32 * q2 as f32;
232                y[l + 64] = d * (scales[sc_pass_base + is + 4] as i8) as f32 * q3 as f32;
233                y[l + 96] = d * (scales[sc_pass_base + is + 6] as i8) as f32 * q4 as f32;
234            }
235        }
236        out.extend_from_slice(&decoded);
237    }
238    Ok(out)
239}
240
241pub(crate) fn dequantize_row_iq3_m(row: &[u8], width: usize) -> Result<Vec<f32>> {
242    require_multiple(width, K_BLOCK, "IQ3_M")?;
243    expect_row_len(row, width, K_BLOCK, IQ3_M_BYTES, "IQ3_M")?;
244    let mut out = Vec::with_capacity(width);
245    let (blocks, _) = row.as_chunks::<IQ3_M_BYTES>();
246    for block in blocks {
247        let d = f16_bits_to_f32(u16::from_le_bytes([block[0], block[1]]));
248        let hmask = &block[2..34];
249        let qs = &block[34..98];
250        let scales = &block[98..110];
251        let scales_h = block[110];
252        let sc = unpack_iq3_m_scales(scales, scales_h);
253        for i in 0..256 {
254            let qs_byte = qs[i / 4];
255            let qs_shift = (i % 4) * 2;
256            let low_2 = (qs_byte >> qs_shift) & 0x03;
257            let hmask_byte = hmask[i / 8];
258            let high_bit = (hmask_byte >> (i % 8)) & 0x01;
259            let q = low_2 | (high_bit << 2);
260            let q_signed = q as i8 - 4;
261            let scale = sc[i / 16] as f32;
262            out.push(d * scale * q_signed as f32);
263        }
264    }
265    Ok(out)
266}
267
268fn unpack_iq3_m_scales(scales: &[u8], scales_h: u8) -> [u8; 16] {
269    let mut sc = [0u8; 16];
270    let mut bit_pos = 0usize;
271    for sc_val in &mut sc {
272        let byte_idx = bit_pos / 8;
273        let bit_shift = bit_pos % 8;
274        let mut val = if byte_idx < 12 {
275            (scales[byte_idx] >> bit_shift) & 0x3F
276        } else {
277            0
278        };
279        if bit_shift > 2 && byte_idx + 1 < 12 {
280            let rem = 6 - (8 - bit_shift);
281            val |= (scales[byte_idx + 1] & ((1 << rem) - 1)) << (8 - bit_shift);
282        }
283        *sc_val = val;
284        bit_pos += 6;
285    }
286    let scales_h_u32 = u32::from(scales_h);
287    for (i, sc_val) in sc.iter_mut().enumerate() {
288        let high_bits = ((scales_h_u32 >> (i * 2)) & 0x03) as u8;
289        *sc_val |= high_bits << 6;
290    }
291    sc
292}
293
294fn scale_min_k4(index: usize, scales: &[u8]) -> (u8, u8) {
295    if index < 4 {
296        (scales[index] & 63, scales[index + 4] & 63)
297    } else {
298        (
299            (scales[index + 4] & 0x0F) | ((scales[index - 4] >> 6) << 4),
300            (scales[index + 4] >> 4) | ((scales[index] >> 6) << 4),
301        )
302    }
303}
304
305fn dequantize_rows(
306    packed: &[u8],
307    dims: &[usize],
308    block: usize,
309    bytes_per_block: usize,
310    label: &str,
311    dequant_row: fn(&[u8], usize) -> Result<Vec<f32>>,
312) -> Result<Vec<f32>> {
313    let (width, n_rows) = width_and_rows(dims)?;
314    require_multiple(width, block, label)?;
315    let row_bytes = (width / block)
316        .checked_mul(bytes_per_block)
317        .ok_or_else(|| invalid(format!("{label} row byte-length overflow")))?;
318    let expected = n_rows
319        .checked_mul(row_bytes)
320        .ok_or_else(|| invalid(format!("{label} packed byte-length overflow")))?;
321    if packed.len() != expected {
322        return Err(invalid(format!(
323            "{label} packed length mismatch: expected {expected} bytes for dims {dims:?}, got {}",
324            packed.len()
325        )));
326    }
327    let mut out = Vec::with_capacity(
328        width
329            .checked_mul(n_rows)
330            .ok_or_else(|| invalid(format!("{label} element count overflow")))?,
331    );
332    for row in packed.chunks_exact(row_bytes) {
333        out.extend_from_slice(&dequant_row(row, width)?);
334    }
335    Ok(out)
336}
337
338fn width_and_rows(dims: &[usize]) -> Result<(usize, usize)> {
339    let Some((&width, rest)) = dims.split_first() else {
340        return Err(unsupported("dequant dims must be non-empty"));
341    };
342    let n_rows = rest
343        .iter()
344        .try_fold(1usize, |acc, &d| acc.checked_mul(d))
345        .ok_or_else(|| invalid("dequant dims overflow"))?;
346    Ok((width, n_rows))
347}
348
349fn row_size_for_block(
350    width: usize,
351    block: usize,
352    bytes_per_block: usize,
353    label: &str,
354) -> Result<usize> {
355    require_multiple(width, block, label)?;
356    (width / block)
357        .checked_mul(bytes_per_block)
358        .ok_or_else(|| invalid(format!("{label} row byte-length overflow")))
359}
360
361fn require_multiple(width: usize, block: usize, label: &str) -> Result<()> {
362    if width == 0 {
363        return Err(unsupported(format!("{label} width must be positive")));
364    }
365    if width.is_multiple_of(block) {
366        Ok(())
367    } else {
368        Err(unsupported(format!(
369            "{label} width {width} is not divisible by {block}"
370        )))
371    }
372}
373
374fn expect_row_len(
375    row: &[u8],
376    width: usize,
377    block: usize,
378    bytes_per_block: usize,
379    label: &str,
380) -> Result<()> {
381    let expected = (width / block) * bytes_per_block;
382    if row.len() == expected {
383        Ok(())
384    } else {
385        Err(invalid(format!(
386            "{label} row length mismatch: expected {expected} bytes for width {width}, got {}",
387            row.len()
388        )))
389    }
390}
391
392fn unsupported(reason: impl Into<String>) -> ParserError {
393    ParserError::UnsupportedFormat {
394        path: "<dequantize>".into(),
395        reason: reason.into(),
396    }
397}
398
399fn invalid(reason: impl Into<String>) -> ParserError {
400    ParserError::InvalidLayout {
401        path: "<dequantize>".into(),
402        reason: reason.into(),
403    }
404}
405
406#[cfg(test)]
407mod tests {
408    use super::*;
409
410    fn f16_one() -> [u8; 2] {
411        0x3C00u16.to_le_bytes()
412    }
413
414    fn f16_two() -> [u8; 2] {
415        0x4000u16.to_le_bytes()
416    }
417
418    #[test]
419    fn q8_0_handmade_block() {
420        // scale = 2.0, quants: 0, 1, -1, then zeros.
421        let mut block = vec![0u8; Q8_0_BYTES];
422        block[0..2].copy_from_slice(&f16_two());
423        block[2] = 0;
424        block[3] = 1;
425        block[4] = (-1i8) as u8;
426        let out = dequantize_q8_0(&block, &[32]).expect("q8_0");
427        assert_eq!(out.len(), 32);
428        assert_eq!(out[0], 0.0);
429        assert_eq!(out[1], 2.0);
430        assert_eq!(out[2], -2.0);
431        assert!(out[3..].iter().all(|&v| v == 0.0));
432    }
433
434    #[test]
435    fn q8_0_denormal_scale() {
436        // Packed-scale denormal path: f16 scale bits 0x0001 = 2^-24.
437        // Dequantized output must equal quant_i8 * 2^-24 (RM-1461).
438        let scale = 2f32.powi(-24);
439        let mut block = vec![0u8; Q8_0_BYTES];
440        block[0..2].copy_from_slice(&0x0001u16.to_le_bytes());
441        block[2] = 0;
442        block[3] = 1;
443        block[4] = (-1i8) as u8;
444        block[5] = 127;
445        block[6] = (-128i8) as u8;
446        let out = dequantize_q8_0(&block, &[32]).expect("q8_0 denormal scale");
447        assert_eq!(out.len(), 32);
448        for (i, &quant) in [0i8, 1, -1, 127, -128].iter().enumerate() {
449            assert_eq!(out[i], quant as f32 * scale, "quant {quant} at index {i}");
450        }
451        assert!(out[5..].iter().all(|&v| v == 0.0));
452    }
453
454    #[test]
455    fn q8_0_unit_scale() {
456        let mut block = vec![0u8; Q8_0_BYTES];
457        block[0..2].copy_from_slice(&f16_one());
458        for q in &mut block[2..] {
459            *q = 1;
460        }
461        let out = dequantize_q8_0(&block, &[32]).expect("q8_0 ones");
462        assert!(out.iter().all(|&v| v == 1.0), "{out:?}");
463    }
464
465    #[test]
466    fn q5_k_handmade_ones() {
467        // Synthetic block: d=1, dmin=0, scales=0x01, ql=0x11, qh=0 → 1.0.
468        let mut block = vec![0u8; Q5_K_BYTES];
469        block[0..2].copy_from_slice(&f16_one());
470        for b in &mut block[4..16] {
471            *b = 0x01;
472        }
473        for b in &mut block[48..] {
474            *b = 0x11;
475        }
476        let out = dequantize_q5_k(&block, &[256]).expect("q5_k");
477        assert_eq!(out.len(), 256);
478        assert!(out.iter().all(|&v| v == 1.0), "got {:?}", &out[..8]);
479    }
480
481    #[test]
482    fn q6_k_handmade_minus_32() {
483        // ggml layout: ql(128) + qh(64) + scales(16) + d(2). ql/qh=0 → (0-32)*1 = -32.
484        let mut block = vec![0u8; Q6_K_BYTES];
485        for b in &mut block[192..208] {
486            *b = 1;
487        }
488        block[208..210].copy_from_slice(&f16_one());
489        let out = dequantize_q6_k(&block, &[256]).expect("q6_k");
490        assert_eq!(out.len(), 256);
491        assert!(out.iter().all(|&v| v == -32.0), "got {:?}", &out[..8]);
492    }
493
494    #[test]
495    fn q5_k_ggml_group_order() {
496        // Only ql[0] low nibble is 1. ggml emits y[0]=1, y[32]=0 — not interleaved.
497        let mut block = vec![0u8; Q5_K_BYTES];
498        block[0..2].copy_from_slice(&f16_one());
499        for b in &mut block[4..16] {
500            *b = 0x01;
501        }
502        block[48] = 0x01;
503        let out = dequantize_q5_k(&block, &[256]).expect("q5_k order");
504        assert_eq!(out[0], 1.0);
505        assert_eq!(out[32], 0.0);
506        assert!(out[1..32].iter().all(|&v| v == 0.0));
507    }
508
509    #[test]
510    fn q6_k_ggml_group_order() {
511        // ql[0]=1 → q1=-31 at y[0]; y[32]/y[64]/y[96] stay -32.
512        let mut block = vec![0u8; Q6_K_BYTES];
513        block[0] = 1;
514        for b in &mut block[192..208] {
515            *b = 1;
516        }
517        block[208..210].copy_from_slice(&f16_one());
518        let out = dequantize_q6_k(&block, &[256]).expect("q6_k order");
519        assert_eq!(out[0], -31.0);
520        assert_eq!(out[32], -32.0);
521        assert_eq!(out[64], -32.0);
522        assert_eq!(out[96], -32.0);
523    }
524
525    #[test]
526    fn iq3_m_zero_scales_vector() {
527        let mut block = vec![0u8; IQ3_M_BYTES];
528        block[0..2].copy_from_slice(&f16_one());
529        for b in &mut block[34..98] {
530            *b = 0x55;
531        }
532        let out = dequantize_iq3_m(&block, &[256]).expect("iq3_m");
533        assert_eq!(out.len(), 256);
534        assert!(out.iter().all(|&v| v == 0.0), "expected zeros, got {out:?}");
535    }
536
537    #[test]
538    fn iq3_m_nonzero_first_scale() {
539        let mut block = vec![0u8; IQ3_M_BYTES];
540        block[0..2].copy_from_slice(&f16_one());
541        for b in &mut block[34..98] {
542            *b = 0x55; // low-2 = 1 → q_signed = -3
543        }
544        block[98] = 0x01; // first 6-bit scale = 1
545        let out = dequantize_iq3_m(&block, &[256]).expect("iq3_m scaled");
546        assert!(out[..16].iter().all(|&v| v == -3.0), "{:?}", &out[..16]);
547        assert!(out[16..].iter().all(|&v| v == 0.0));
548    }
549
550    #[test]
551    fn packed_row_size_iq3_m_is_not_wire_31() {
552        assert_eq!(packed_row_size(GGML_TYPE_IQ3_M_BLOCK, 256).unwrap(), 111);
553        assert_eq!(packed_row_size(GGML_TYPE_IQ3_M_BLOCK, 512).unwrap(), 222);
554        assert!(packed_row_size(GGML_TYPE_IQ3_M_BLOCK, 255).is_err());
555        assert!(packed_row_size(31, 256).is_err());
556    }
557
558    #[test]
559    fn dequant_error_paths() {
560        assert!(dequantize_q8_0(&[0u8; 34], &[31]).is_err());
561        assert!(dequantize_q8_0(&[0u8; 33], &[32]).is_err());
562        assert!(dequantize_row_q8_0(&[0u8; 33], 32).is_err());
563        assert!(dequantize_row_q5_k(&[0u8; 175], 256).is_err());
564        assert!(dequantize_iq3_m(&[0u8; 111], &[255]).is_err());
565        assert!(dequantize_iq3_m(&[0u8; 110], &[256]).is_err());
566        assert!(dequantize_q5_k(&[0u8; 176], &[]).is_err());
567        assert!(dequantize_q8_0(&[], &[0]).is_err());
568        assert!(dequantize_packed(DType::F32, &[0u8; 4], &[1]).is_err());
569        assert!(dequantize_packed(DType::Other(31), &[0u8; 18], &[32]).is_err());
570        assert!(dequantize_packed(DType::Q5_K, &[0u8; 176], &[256]).is_ok());
571        assert!(dequantize_packed(DType::Q6_K, &[0u8; 210], &[256]).is_ok());
572        assert!(dequantize_packed(DType::IQ3_M_BLOCK, &[0u8; 111], &[256]).is_ok());
573    }
574
575    #[test]
576    fn dequantize_packed_dispatches_q8_0() {
577        let mut block = vec![0u8; Q8_0_BYTES];
578        block[0..2].copy_from_slice(&f16_one());
579        let out = dequantize_packed(DType::Q8_0, &block, &[32]).unwrap();
580        assert_eq!(out.len(), 32);
581    }
582}