Skip to main content

Module fp4

Module fp4 

Source
Expand description

Software e2m1 conversion, on u32 bit patterns only, so a backend with no 4-bit float type can still decode and encode fp4 from the bits in a word.

This is the fp4 counterpart of cubecl_core::post_processing::minifloat, and it exists for the same reason: e2m1 conversion is a CUDA intrinsic and nothing else. Every other backend either has no 4-bit float type at all or can only move one around, so a quantized kernel that reaches for e2m1x2::from_bits runs on one vendor. The arithmetic below runs everywhere.

It does not go through the general minifloat path. That one reconstructs an f32 bit pattern field by field, which is the only tractable way to cover eight exponent bits; e2m1 has four codes per sign and its eight magnitudes are {0, 0.5, 1, 1.5, 2, 3, 4, 6}, small enough that decoding is one select over the subnormal arm and encoding is a count of the midpoints a magnitude clears.

Modules§

e2m1_bits_to_float
e2m1_packed_bits_to_float
float_to_e2m1_bits

Functions§

e2m1_bits_to_float
Decode one e2m1 code per lane, held in the low nibble of each lane of code.
e2m1_packed_bits_to_float
Decode the N e2m1 codes packed into the low 4 * N bits of word, lowest nibble first.
float_to_e2m1_bits
Encode one e2m1 code per lane into the low nibble of each lane, rounding to nearest with ties to even and saturating at ±6.