Skip to main content

encode_block_q8_0

Function encode_block_q8_0 

Source
pub fn encode_block_q8_0(block: &[f32; 32], out: &mut Vec<u8>)
Expand description

Encodes one Q8_0 block (exactly Q8_0_BLOCK_ELEMS values) and appends its Q8_0_BLOCK_BYTES bytes to out.

Every arithmetic choice here mirrors ggml-quants.c:

  • amax is folded with a > b ? a : b, not Rust’s f32::max. They differ on NaN – f32::max returns the non-NaN operand, ggml’s macro propagates it – and a checkpoint with a NaN weight should produce llama.cpp’s bytes, not politely different ones.
  • The scale is applied as a multiply by 1/d, not a divide by d. v * (1.0/d) and v / d differ by an ulp for many inputs, and an ulp either side of .5 is a different roundf result, so a divide here would disagree with llama.cpp on real weights.
  • d == 0 (an all-zero block) yields the reciprocal 0.0, so the stored scale is +0.0 and every quant is 0. The obvious alternative, storing a scale of 1.0, dequantizes identically and is therefore invisible to every test that checks values – and produces a file that differs from llama.cpp’s in bytes.