Skip to content
fn

fused_step

→Callable[..., Tensor]
fused_step(model: Module, loss_fn: Callable[..., Tensor], optimizer: Optimizer, grad_scaler: GradScaler | None = None)

Return a callable that runs one fused training step.

The first call traces model(*x); loss_fn(out, *targets) and the optimizer update once under a single Tracer, plumbs the ghost-grad placeholders, and compiles the resulting graph into one MPSGraphExecutable that runs forward + loss + backward (via MPSGraph autodiff) + optimizer update in a single submission. Subsequent calls reuse the cached executable.

The step matches an eager zero_grad(); loss.backward(); optimizer.step() loop: a parameter the loss does not depend on, or one with requires_grad=False, gets no gradient and is left untouched (state and step count included), and every hyper-parameter is read from optimizer.param_groups on each call, so LR schedulers work. optimizer.state_dict() / load_state_dict() checkpoint the state this step updates (the compiled optimizer owns it from fused_step(...) on — see compile_optimizer).

Parameters

modelModule
The trainable nn.Module. Parameters from optimizer.param_groups must reference these tensors.
loss_fnCallable
Scalar-loss-returning callable invoked as loss_fn(model_output, *targets). Captured by identity in the trace, so passing a fresh closure each call defeats the cache.
optimizerOptimizer
Any optimizer compile_optimizer supports, with any number of parameter groups.
grad_scalerGradScaler= None
When provided and enabled, the fused step replicates the eager lucid.amp.GradScaler contract inside the compiled executable (scales loss before backward, unscales gradients in F32, guards updates with where(found_inf, old, new), and reads found_inf back to drive scaler.update()).

Returns

Callable[..., Tensor]

A callable step(*args) where args is (model_input, *loss_targets). Returns the scalar loss Tensor from the just-completed step.

Raises

RuntimeError
On the first call, when no parameter both requires grad and reaches the loss (there would be nothing to step). On any call given a tensor that requires grad — the output of an eager trunk, say: the step cannot return a gradient to it. Detach it, or use make_step, whose loss carries the input gradients.

Notes

A tensor requiring grad that optimizer does not hold — a loss weight left out of it — is not stepped, and gets no .grad: the backward runs inside the executable and the loss returned has no grad_fn. Put it in the optimizer, or use make_step.

Parameters on the CPU leave nothing to compile (the executables run on Metal only): the call runs the eager step — zero_grad(), forward, backward(), optimizer.step() of the optimizer passed in, which keeps its own state — and returns the eager loss, with a one-time CompileFallbackWarning.

Examples

>>> import lucid, lucid.nn as nn, lucid.nn.functional as F
>>> import lucid.optim as optim
>>> from lucid.compile import fused_step
>>> model = nn.Linear(8, 4).to('metal')
>>> opt = optim.Adam(model.parameters(), lr=1e-3)
>>> step = fused_step(model, F.cross_entropy, opt)
>>> for batch in batches:
...     loss = step(batch.x, batch.target)       # one executable submission

See Also

Used by 1