nemo_automodel.shared.torch_patches
nemo_automodel.shared.torch_patches
Torch compatibility patches.
These patches are intentionally NOT applied at import nemo_automodel time to keep
tokenizer-only imports lightweight. Call apply_torch_patches() from code paths
that already depend on torch (training / distributed / dataloading).
Module Contents
Functions
Data
API
Return the float dtype among dtypes that every other one converts into losslessly.
Parameters:
Gradient dtypes from a single reduce-scatter group.
Returns: Any
The dtype with the largest element size; ties resolve to float32 over
Apply small, version/packaging-specific torch monkey patches.
This function is idempotent and safe to call multiple times.
Guard FSDP2 post-backward against params that were never unsharded.
PyTorch FSDP2 creates _unsharded_param lazily from an FSDP unit’s
forward pre-hook. If a separately wrapped unit is skipped by the batch
(for example a vision tower on text-only data), deferred post-backward can
dereference that missing field. Missing lazy state means there is no
unsharded grad to upcast, so the exact missing-field case can return early.
Give every FSDP2 reduce-scatter group local gradients of one dtype.
Gradient accumulation leaves a group holding reduce_dtype accumulations
for the parameters used so far, while any parameter whose gradient joins
later — a locally unused parameter zero-filled by PyTorch’s public API or
:func:patch_fsdp_unused_param_reduction, or one whose gradient lands after
its group’s post-backward already ran — contributes param_dtype.
foreach_reduce then aborts with FSDP reduce-scatter expects uniform gradient dtype.
Normalize and widen gradients at the last possible moment, inside
foreach_reduce itself. That placement matters:
FSDPParamnormally unwraps gradients through_get_grad_inner_tensor. PyTorch versions whose public unused-parameter API appendszeros_like(unsharded_param)directly can still leave aDTensorin this list, so unwrap that residual value before sizing the reduce-scatter buffer;- FSDP2’s own bookkeeping (
unsharded_param.grad/unsharded_accumulated_grad) is left exactly as upstream leaves it, so no later reader of that state sees anything unusual; foreach_reduceimmediately copies these gradients into areduce_dtypebuffer anyway, so widening first changes no value.
Uniform groups are passed straight through, so the upstream assertion still fires for genuinely inconsistent gradients such as fp8 weights that fail to produce higher-precision ones. The patch is process-global and idempotent.
Backport FSDP2 unused-parameter reduction when the public API is absent.
The patch is process-global and idempotent. It only fills a missing local
gradient with zeros immediately before FSDP2 post-backward reduction, so
ranks that skipped a parameter still participate in the same collective as
ranks that used it. Callers must first prefer the public
FSDPModule.set_reduce_scatter_unused_params API.
Raises:
RuntimeError: If the installed PyTorch exposes neither the public API nor the compatible private FSDP2 implementation.