nemo_automodel.components.moe.optimized_ops
nemo_automodel.components.moe.optimized_ops
Memory-optimized MoE elementwise ops extracted from experts.py.
Chunked custom-autograd router-weight fp32 multiply: computes the identical fp32 math in row chunks and saves only low-precision inputs, removing the full-size fp32 intermediates that otherwise pin ~7 GiB blocks per MoE layer under activation checkpointing.
Module Contents
Classes
Functions
Data
API
Bases: Function
Chunked fp32 router-weight multiply that saves only the raw inputs.
The plain (x.float() * probs.float()).to(dtype) lets autograd keep
full-size fp32 [tokens, hidden] intermediates alive for backward (the
upcast input, the product, and a recompute copy under activation
checkpointing). This Function computes the same fp32 multiply in row
chunks and saves only the low-precision inputs. The forward is
bitwise-identical; the backward matches autograd’s fp32 chain
(grad_x = (g_f32 * probs_f32).to(x.dtype),
grad_probs = (g_f32 * x_f32).sum(-1, keepdim=True) cast to
probs.dtype).
Compute chunked fp32 gradients for the router-weight multiply.
Parameters:
Autograd context holding the saved inputs.
Upstream gradient of shape [tokens, hidden].
Returns: torch.Tensor | None
Tuple (grad_x, grad_probs, None, None) where grad_x has
Multiply expert outputs by routing probabilities in fp32.
Parameters:
Autograd context.
Expert outputs of shape [tokens, hidden].
Routing probabilities of shape [tokens, 1].
Output dtype (the dispatcher’s expected activation dtype, or fp32 for the scatter-add reduction path).
Whether backward needs x. x is only consumed by
the probs gradient; when probs carries no grad (e.g.
FakeBalancedGate emits constant weights), saving it would pin
a full-size [tokens, hidden] tensor per MoE layer across the
activation-checkpointing backward window for nothing. Callers
pass probs.requires_grad.
Returns: torch.Tensor
Tensor of shape [tokens, hidden] and dtype out_dtype.
Apply routing probabilities to expert outputs with fp32 arithmetic.
Large 2-D row-aligned inputs go through the chunked custom Function so autograd does not retain full-size fp32 intermediates; every other shape keeps the plain eager multiply (bitwise-identical result).
Parameters:
Expert down-projection outputs of shape [tokens, hidden].
Routing probabilities broadcastable against
output2, typically of shape [tokens, 1].
Output dtype.
Returns: torch.Tensor
(output2 * permuted_probs) computed in fp32 and cast to