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_bytes_to_f16
e2m1_packed_bits_to_float
e2m1_words_to_f16
float_to_e2m1_bits

Structs§

LowerFp4Cast
Lowers every cast from or to e2m1x2 onto the software codec, for a backend that has no native fp4 conversion.

Functions§

e2m1_bits_to_float
Decode one e2m1 code per lane, held in the low nibble of each lane of code.
e2m1_bytes_to_f16
[e2m1_words_to_f16] for bytes that do not fill a word, one e2m1x2 per lane of bytes: the high nibble moves up to the second half of the word first, one more mask and shift a pair.
e2m1_packed_bits_to_float
Decode the N e2m1 codes packed into the low 4 * N bits of word, lowest nibble first.
e2m1_words_to_f16
Decode the eight codes of each word of words, lowest nibble first, to f16.
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.

Type Aliases§

LowerFp4CastPass