#[repr(C)]pub struct BlockQ3K {
pub hmask: [u8; 32],
pub qs: [u8; 64],
pub scales: [u8; 12],
pub d: f16,
}Expand description
Q3_K super-block: 256 weights quantized to 3 bits each.
Layout (110 bytes):
hmask: 32 bytes — high bit (bit 2) for each of the 256 weights, packed 8 per byte.qs: 64 bytes — low 2 bits for each of the 256 weights, packed 4 per byte.scales: 12 bytes — 4-bit scale values for 16 sub-blocks of 16 weights each. Each nibble is a signed 4-bit value (stored as u4, subtract 8 for range [-8..7]). Packing:scales[j/2] >> (4*(j%2)) & 0xFgives sub-block j’s raw scale.d: FP16 super-block scale.
Dequant: w[i] = d * sub_scale * q3_signed[i]
where q3_signed = ((low2 | (high1<<2)) as i32) - 4, range [-4, 3].
Fields§
§hmask: [u8; 32]High bit (bit 2) for each of 256 weights, packed 8 per byte.
qs: [u8; 64]Low 2 bits for each of 256 weights, packed 4 per byte (2 bits each, LSB first).
scales: [u8; 12]16 × 4-bit sub-block scales, 2 per byte (low nibble = sub 2i, high nibble = sub 2i+1).
d: f16Super-block scale (FP16).
Implementations§
Source§impl BlockQ3K
impl BlockQ3K
Sourcepub fn dequant(blocks: &[Self], output: &mut [f32]) -> BonsaiResult<()>
pub fn dequant(blocks: &[Self], output: &mut [f32]) -> BonsaiResult<()>
Dequantize a slice of Q3_K blocks into f32 output.
output must have length >= blocks.len() * QK_K (256 per block).
Sourcepub fn dequant_row_to_buf(blocks_for_row: &[Self], buf: &mut Vec<f32>)
pub fn dequant_row_to_buf(blocks_for_row: &[Self], buf: &mut Vec<f32>)
Dequantize a single row’s worth of Q3_K blocks into a pre-allocated buffer.
buf will be extended by blocks_for_row.len() * 256 elements.
Sourcepub fn quantize(input: &[f32]) -> BonsaiResult<Vec<Self>>
pub fn quantize(input: &[f32]) -> BonsaiResult<Vec<Self>>
Quantize f32 input into Q3_K blocks.
Input length must be a multiple of QK_K (256).
Uses symmetric per-sub-block quantization: 16 sub-blocks of 16 weights each, mapping each sub-block to the range [-4..3] with a 4-bit signed scale.
Sourcepub fn slice_from_bytes(data: &[u8]) -> BonsaiResult<&[Self]>
pub fn slice_from_bytes(data: &[u8]) -> BonsaiResult<&[Self]>
Zero-copy cast of a byte slice to a slice of BlockQ3K.
Returns error if length is not a multiple of BLOCK_Q3K_BYTES (110)
or if the pointer is not properly aligned.