Skip to navigation

nemo_automodel.components.models.inkling2.quantization

View as MarkdownOpen in Claude

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: amax is the expert’s largest magnitude unless the caller passes one, the block scale is e4m3(bmax / 6 * (denominator / amax)) saturated at 448, and each code is the nearest E2M1 value of w / (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**s for which bmax / 2**s still rounds to a finite E4M3 value, that is bmax / 2**s <= 464, and each value is the nearest E4M3 value of w / 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

NameDescription
Mxfp8ExpertsReleased MXFP8 storage of one grouped expert projection.
Nvfp4ExpertsReleased NVFP4 storage of one grouped expert projection.

Functions

NameDescription
_checked_expertsReject inputs the released layout cannot hold.
_experts_per_chunkExperts encoded together so that a chunk holds about _CHUNK_ELEMENTS elements.
_nearest_e2m1Round to the nearest E2M1 code, ties to even, saturating at 6.
quantize_mxfp8_expertsEncode grouped expert weights as released MXFP8 storage.
quantize_nvfp4_expertsEncode grouped expert weights as released NVFP4 storage.

Data

_CHUNK_ELEMENTS

_E2M1_MAX

_E2M1_MIDPOINTS

_E4M3_MAX

_E4M3_ROUNDS_FINITE

_MXFP8_BLOCK

_NVFP4_BLOCK

_UE8M0_BIAS

API

class nemo_automodel.components.models.inkling2.quantization.Mxfp8Experts()

Bases: NamedTuple

Released MXFP8 storage of one grouped expert projection.

scales
Tensor

uint8 tensor of shape [experts, output, input / 32]; byte s scales its 32 input columns by 2**(s - 127).

values
Tensor

float8_e4m3fn tensor of shape [experts, output, input].

class nemo_automodel.components.models.inkling2.quantization.Nvfp4Experts()

Bases: NamedTuple

Released NVFP4 storage of one grouped expert projection.

amax
Tensor

float32 tensor of shape [experts], the largest magnitude of each expert.

codes
Tensor

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.

scales
Tensor

float8_e4m3fn tensor of shape [experts, output, input / 16], one block scale per 16 input columns.

nemo_automodel.components.models.inkling2.quantization._checked_experts(
weight: torch.Tensor,
block: int
) -> None

Reject inputs the released layout cannot hold.

Parameters:

weight
torch.Tensor

Tensor of shape [experts, output, input], any floating dtype, on any device.

block
int

Input columns per scale; input must be a multiple of it.

nemo_automodel.components.models.inkling2.quantization._experts_per_chunk(
weight: torch.Tensor
) -> int

Experts encoded together so that a chunk holds about _CHUNK_ELEMENTS elements.

Parameters:

weight
torch.Tensor

Tensor of shape [experts, output, input].

Returns: int

A positive number of experts.

nemo_automodel.components.models.inkling2.quantization._nearest_e2m1(
scaled: torch.Tensor
) -> torch.Tensor

Round to the nearest E2M1 code, ties to even, saturating at 6.

Parameters:

scaled
torch.Tensor

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

nemo_automodel.components.models.inkling2.quantization.quantize_mxfp8_experts(
weight: torch.Tensor

Encode grouped expert weights as released MXFP8 storage.

Parameters:

weight
torch.Tensor

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.

nemo_automodel.components.models.inkling2.quantization.quantize_nvfp4_experts(
weight: torch.Tensor,
denominator: torch.Tensor,
amax: torch.Tensor | None = None

Encode grouped expert weights as released NVFP4 storage.

Parameters:

weight
torch.Tensor

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.

denominator
torch.Tensor

FP32 tensor of shape [1], the tensor’s released outer_scale_denominator; finite and positive.

amax
torch.Tensor | NoneDefaults to None

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.

nemo_automodel.components.models.inkling2.quantization._CHUNK_ELEMENTS = 1 << 26
nemo_automodel.components.models.inkling2.quantization._E2M1_MAX = 6.0
nemo_automodel.components.models.inkling2.quantization._E2M1_MIDPOINTS = (0.25, 0.75, 1.25, 1.75, 2.5, 3.5, 5.0)
nemo_automodel.components.models.inkling2.quantization._E4M3_MAX = 448.0
nemo_automodel.components.models.inkling2.quantization._E4M3_ROUNDS_FINITE = 464.0
nemo_automodel.components.models.inkling2.quantization._MXFP8_BLOCK = 32
nemo_automodel.components.models.inkling2.quantization._NVFP4_BLOCK = 16
nemo_automodel.components.models.inkling2.quantization._UE8M0_BIAS = 127