IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Python class

NVFP4Tensor

NVFP4Tensor​

class max.nn.common_layers.quant_tensor.NVFP4Tensor(*, weight, weight_scale, weight_scale_2, input_scale, block_size=(1, 16))

source

Bases: _QuantizedWeight

NVFP4 (modelopt) block-scaled quantized tensor.

NVFP4 packs two float4_e2m1fn values per uint8 and applies two scale levels: a per-16-element block scale (float8_e4m3fn) and a per-tensor global scale (weight_scale_2, float32). The activation scale (input_scale, float32) is static — loaded from the checkpoint — and used to normalize activations before dynamic block quantization (see _matmul_nvfp4()).

Parameters:

  • weight (Tensor) – Packed uint8 weight data of shape [rows, cols // 2].
  • weight_scale (Tensor) – Per-block float8_e4m3fn scales of shape [ceil(rows / block_m), ceil(cols / block_k)].
  • weight_scale_2 (Tensor) – Per-tensor float32 global scale, scalar () (or [num_experts] when stacked for grouped MoE).
  • input_scale (Tensor) – Static per-tensor float32 activation scale, scalar () (or [num_experts] when stacked).
  • block_size (tuple[int, int]) – Per-block size as (block_m, block_k); (1, 16) for NVFP4.

block_size​

property block_size: tuple[int, int]

source

Quantization block shape as (rows, cols).

input_scale​

input_scale: Tensor

source

local_experts()​

local_experts(device)

source

device’s experts of a stack sharded on its expert axis.

Unlike local_shards, the per-expert host scales are cut to the same experts.

Parameters:

device (int)

Return type:

NVFP4Tensor

local_shards​

property local_shards: tuple[NVFP4Tensor, ...]

source

Per-device shards, pairing each weight shard with its scales.

mesh​

property mesh: DeviceMesh

source

Device mesh of the packed weight tensor.

shard()​

shard(axis, mesh)

source

Co-shard weight/weight_scale along axis onto mesh.

Parameters:

Return type:

NVFP4Tensor

stacked_zeros()​

classmethod stacked_zeros(num_experts, shape, *, scales_per_expert=1, block_size=(1, 16), device=None)

source

Builds a zero-filled stack of num_experts weights of shape.

weight and weight_scale gain a leading expert axis on device. The per-tensor scales stay on the host as [num_experts], or [num_experts, scales_per_expert] for a fused weight that keeps each part’s scale.

Parameters:

Return type:

NVFP4Tensor

to()​

to(target)

source

Returns a copy with weight and weight_scale on target.

Parameters:

target (Device | DeviceMesh | DeviceMapping)

Return type:

NVFP4Tensor

weight_scale​

weight_scale: Tensor

source

weight_scale_2​

weight_scale_2: Tensor

source

zeros()​

classmethod zeros(shape, *, block_size=(1, 16))

source

Builds a zero-filled NVFP4Tensor of logical shape.

shape is the unpacked Linear-convention [out_dim, in_dim]. The packed weight leaf has half the trailing dimension.

Parameters:

Return type:

NVFP4Tensor