Skip to main content

Module q5_k

Module q5_k 

Source
Expand description

The Q5_K weight encoder: a transcription of llama.cpp b7650’s quantize_row_q5_K_ref (ggml/src/ggml-quants.c:1467).

Q5_K is Q4_K’s super-block fit with two things changed and nothing else:

  • the codes run 0..=31 instead of 0..=15, and the candidate grid handed to make_qkx2_quants is (-0.5, 0.1, 15) rather than (-1.0, 0.1, 20) – both of which are [Q5_K_FIT], passed to the SAME [super::fit::fit_qk_super_block] Q4_K uses;
  • the fifth bit of each code goes into a separate 32-byte qh plane, which is the only packing this module writes out itself.

Two transcriptions of that shared fit is precisely the shape this repo has paid the most for: the copies agree the day they are written and drift the first time one of them is corrected. So the difference between Q4_K and Q5_K is four numbers in a struct, and if someone fixes the least-squares step it is fixed for both or for neither.

The qh bit assignment is worth stating because it is not the obvious one: the 256 codes are walked in four groups of 64, and within a group the first 32 codes’ high bits go to bit m1 of qh[j] and the second 32’s to bit m2, where m1/m2 start at 1 and 2 and shift LEFT BY TWO per group. So qh[j] holds the high bits of elements j, j+32, j+64, … in bit pairs, not one contiguous run. dequant_q5_k in this crate reads it back with the same u1 <<= 2 walk.

Functions§

encode_block_q5_k
Encodes one Q5_K super-block (exactly Q5_K_BLOCK_ELEMS values) and appends its Q5_K_BLOCK_BYTES bytes to out.
encode_row_q5_k
Encodes a whole row (or any slice whose length is a multiple of Q5_K_BLOCK_ELEMS) into Q5_K super-blocks, appending to out.