<|start|>system<|message|>You are ChatGPT, a large language model trained by OpenAI.
Knowledge cutoff: 2024-06
Current date: 2026-10-15

Reasoning: low

# Valid channels: analysis, commentary, final. Channel must be included for every message.<|end|><|start|>developer<|message|># Instructions

Answer concisely.<|end|><|start|>user<|message|>Review this Rust code and point out any bug.

//! Expanding ggml weight blocks into the layout of the NPU graph: int8 values `[rows][groups][32]` and one
//! half-precision scale per group of 32, `[rows][groups]`, both written as raw bytes (the scale little-endian).
//!
//! Q8_0 blocks already are that (a half scale and 32 int8), so they are copied. MXFP4 blocks hold 32 four-bit
//! E2M1 codes and an E8M0 exponent; the doubled value table (0, 1, 2, 3, 4, 6, 8, 12 and their negatives, all
//! integers) times `2^(e - 128)` is exact in int8 and half precision for every exponent that matters.

pub const GROUP: usize = 32;
pub const Q8_0_BYTES: usize = 34;
pub const MXFP4_BYTES: usize = 17;

/// Doubled E2M1 magnitudes by 3-bit code.
const MAGNITUDE: [i8; 8] = [0, 1, 2, 3, 4, 6, 8, 12];

/// Half-precision bits of `2^(e - 128)` (the MXFP4 scale times one half, matching the doubled table).
pub fn mxfp4_scale_bits(e: u8) -> u16 {
    let power = i32::from(e) - 128;
    match power {
        ..=-25 => 0,
        -24..=-15 => 1u16 << (power + 24), // subnormal: 2^power = 2^-24 * 2^(power + 24)
        -14..=15 => ((power + 15) as u16) << 10,
        _ => 0x7BFF, // beyond the half range; MXFP4 weights never get here
    }
}

fn expand_q8_0_block(block: &[u8; Q8_0_BYTES], q: &mut [u8], s: &mut [u8]) {
    s.copy_from_slice(&block[..2]);
    q.copy_from_slice(&block[2..]);
}

fn expand_mxfp4_block(block: &[u8; MXFP4_BYTES], q: &mut [u8], s: &mut [u8]) {
    s.copy_from_slice(&mxfp4_scale_bits(block[0]).to_le_bytes());
    for (j, byte) in block[1..].iter().enumerate() {
        for (position, code) in [(j, byte & 15), (j + 16, byte >> 4)] {
            let magnitude = MAGNITUDE[usize::from(code & 7)];
            q[position] = (if code & 8 != 0 { -magnitude } else { magnitude }) as u8;
        }
    }
}

/// Which block format the bytes are in.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Format {
    Q8_0,
    Mxfp4,
}

impl Format {
    pub fn block_bytes(self) -> usize {
        match self {
            Self::Q8_0 => Q8_0_BYTES,
            Self::Mxfp4 => MXFP4_BYTES,
        }
    }
}

/// Expand `blocks` (a whole number of blocks) into `q` (32 bytes per block) and `s` (2 bytes per block).
pub fn expand(format: Format, blocks: &[u8], q: &mut [u8], s: &mut [u8]) {
    let n = blocks.len() / format.block_bytes();
    assert!(
        q.len() >= n * GROUP && s.len() >= n * 2,
        "output too small for {n} blocks"
    );
    let outputs = q
        .as_chunks_mut::<GROUP>()
        .0
        .iter_mut()
        .zip(s.as_chunks_mut::<2>().0.iter_mut());
    match format {
        Format::Q8_0 => <|end|><|start|>assistant<|channel|>final<|message|>