nemo_automodel.components.models.inkling2.quantization
nemo_automodel.components.models.inkling2.quantization
Nearest NVFP4 and MXFP8 encoding of grouped expert weights in the released Inkling2 layout.
Inkling2StateDictAdapter decodes a released NVFP4 expert value as
(e2m1(code) * e4m3(block_scale)) * (amax / denominator) with one block scale per 16 input columns, one
amax per expert and one denominator per tensor, and a released MXFP8 value as
e4m3(value) * 2**(scale - 127) with one ue8m0 scale byte per row and 32 input columns. The encoders
below are the nearest inverses that hf_quant_config.json names (nvfp4_nearest with
amax_denominator scales, mxfp8_nearest with ue8m0 scales):
- NVFP4:
amaxis the expert’s largest magnitude unless the caller passes one, the block scale ise4m3(bmax / 6 * (denominator / amax))saturated at 448, and each code is the nearest E2M1 value ofw / (block_scale * amax / denominator), ties to even, saturated at 6. A negative value that rounds to zero keeps its sign (code 8), as in the release. - MXFP8: the scale is the smallest power of two
2**sfor whichbmax / 2**sstill rounds to a finite E4M3 value, that isbmax / 2**s <= 464, and each value is the nearest E4M3 value ofw / 2**s, ties to even, keeping the sign of zero.
Both are fixed points on released bytes: decoding a released expert and encoding it again with its released
amax reproduces its codes and scales. Arithmetic is FP32; inputs of any floating dtype are widened exactly.
Module Contents
Classes
Functions
Data
API
Bases: NamedTuple
Released MXFP8 storage of one grouped expert projection.
uint8 tensor of shape [experts, output, input / 32]; byte s scales its 32 input columns by
2**(s - 127).
float8_e4m3fn tensor of shape [experts, output, input].
Bases: NamedTuple
Released NVFP4 storage of one grouped expert projection.
float32 tensor of shape [experts], the largest magnitude of each expert.
uint8 tensor of shape [experts, output, input / 2]. Byte k holds the E2M1 codes of input
columns 2k (low nibble) and 2k + 1 (high nibble); bit 3 of a code is its sign.
float8_e4m3fn tensor of shape [experts, output, input / 16], one block scale per 16 input columns.
Reject inputs the released layout cannot hold.
Parameters:
Tensor of shape [experts, output, input], any floating dtype, on any device.
Input columns per scale; input must be a multiple of it.
Experts encoded together so that a chunk holds about _CHUNK_ELEMENTS elements.
Parameters:
Tensor of shape [experts, output, input].
Returns: int
A positive number of experts.
Round to the nearest E2M1 code, ties to even, saturating at 6.
Parameters:
FP32 tensor of shape […], already divided by its decode scale.
Returns: torch.Tensor
uint8 codes of shape […]: bits 0-2 index the magnitudes 0, 0.5, 1, 1.5, 2, 3, 4, 6 and bit 3 is the
Encode grouped expert weights as released MXFP8 storage.
Parameters:
Tensor of shape [experts, output, input] in any floating dtype, finite, with input a
multiple of 32. Rows are encoded independently, one scale per row and 32 input columns.
Returns: Mxfp8Experts
Newly allocated E4M3 values and ue8m0 scale bytes on weight’s device; nothing aliases the input.
Encode grouped expert weights as released NVFP4 storage.
Parameters:
Tensor of shape [experts, output, input] in any floating dtype, finite, with input a
multiple of 16. For gate/up projections the output rows keep the released interleaving; this
function does not reorder rows.
FP32 tensor of shape [1], the tensor’s released outer_scale_denominator; finite
and positive.
Optional FP32 tensor of shape [experts], finite and nonnegative, the outer amax of each
expert, such as the released one. None uses each expert’s largest magnitude. Magnitudes
above a given amax saturate at block scale 448 and code 6.
Returns: Nvfp4Experts
Newly allocated codes, block scales and amax on weight’s device; nothing aliases the inputs.