Skip to main content

Module pack4

Module pack4 

Source
Expand description

INT4 nibble packing.

safetensors 0.7 has no native I4/U4 dtype, so packed INT4 tensors are stored as plain U8. Packing layout (this crate’s own convention – not bit-for-bit compatible with any particular GPTQ/AWQ int32-packing scheme, which vary across implementations and versions): two consecutive elements in row-major order share one byte, the first in the low nibble and the second in the high nibble, each a 4-bit two’s-complement value in -8..=7. A tensor with an odd element count gets one trailing byte whose high nibble is unused padding (0). The packed tensor’s logical shape and element count are recorded separately in the quantization_config metadata (see crate::format) since the packed U8 tensor’s own shape ([ceil(n / 2)]) doesn’t reflect it.

Functions§

pack_i4
Packs 4-bit two’s-complement values (each expected to be in -8..=7; out-of-range bits above the low nibble are silently dropped) two-per-byte, low nibble first.
unpack_i4
Inverse of pack_i4: unpacks n sign-extended 4-bit values from bytes.