nemo_automodel.components.optim.nvfp4_resident_adam
nemo_automodel.components.optim.nvfp4_resident_adam
Adam step of resident NVFP4 expert weights in two kernels.
Transformer Engine FusedAdam steps an NVFP4 weight in three passes over its FP32 master weight: it casts the
BF16 gradient to FP32, runs Adam, and quantizes the master back into the weight. enable_nvfp4_fused_adam_step
replaces these for the NVFP4Tensor parameters of a model, which are the expert weights of
GroupedExpertsTENVFP4, with one kernel that runs Adam on the BF16 gradient
and records the largest magnitude of each updated master, and one kernel that writes the row-wise and column-wise
NVFP4 operands of every weight from its master. Master weights, moments, codes, block scales and outer scales
come out bit-identical to the FusedAdam step; the optimizer state stays where FusedAdam keeps it, so
checkpoints and schedulers are unaffected.
The kernels are built once per build directory with torch.utils.cpp_extension.load_inline (CUDA toolkit
required) under $NEMO_NVFP4_ADAM_BUILD_DIR or ~/.cache/nemo_automodel/nvfp4_adam.
Module Contents
Classes
Functions
Data
API
Step hooks of one FusedAdam that run the Adam step of some of its NVFP4 weights in the two kernels.
The pre-step hook takes the gradients of those weights away, so FusedAdam.step advances its step counter
and updates every other parameter but skips them; the post-step hook puts the gradients back and steps the
weights with the group’s hyperparameters. A weight keeps the FusedAdam step while it has no optimizer
state yet (its first step), when its state is not FP32 tensors, or when its gradient is not BF16; so does
every weight of a step that is given a closure or a gradient scaler.
Pre-step hook: take the gradients of the weights the kernels will step.
Parameters:
The optimizer about to step.
Positional arguments of step, the optimizer first.
Keyword arguments of step.
Post-step hook: return the gradients and step their weights.
Parameters:
The optimizer that just stepped.
Positional arguments of step, the optimizer first.
Keyword arguments of step.
Build directory of this source text, torch version and device architecture.
Whether the requantization kernel writes weight the way its own quantizer does.
Parameters:
NVFP4 weight of logical shape [output_features, input_features].
Returns: bool
True for 2D block scales, rounding to nearest, and both the row-wise and the column-wise operand.
Import the extension from its built library, building it on first use.
load_inline takes a file lock in the build directory on every call, and a process killed while holding
it leaves a lock that blocks every later caller. A finished library is therefore imported directly, and a
lock older than any build is removed.
Step the resident NVFP4 expert weights of every model part in the fused kernels.
Applies to the NVFP4Tensor parameters of the model parts (the expert weights of
GroupedExpertsTENVFP4) that are 2D block-scaled, rounded to nearest and hold both operands, and to
Transformer Engine FusedAdam optimizers with FP32 master weights that are not
capturable. Every other weight and optimizer keeps the FusedAdam step, as do all weights when the kernels
cannot be built (logged as a warning).
Parameters:
Pipeline model parts paired one-to-one with optimizers.
Optimizers that hold the parameters of the matching model part.
Raises:
ValueError: If there is not one optimizer per model part.