fn
make_step
→callablemake_step(model: Module, loss_fn: Callable[..., Tensor], dynamic: bool = False, segments: int | str = 1)Return a callable that runs one compiled training step.
The returned step(x, *extra_inputs) callable:
- Calls
loss_fn(model(x), *extra_inputs)once under_tracingto capture the op DAG (forward + loss). - Compiles a single MPSGraph executable that produces the loss
and the gradient of the loss w.r.t. every model parameter and
every input that requires grad, and every tensor the loss owns (a
learned weight it closes over), via
compile_trace_with_backward. - Runs the executable, populates a Lucid
autograd.Functionwith the resulting (loss, grads) so the returned scalar loss has a workinggrad_fnandloss.backward()drives.gradon every parameter and loss weight — and on through an input computed by eager ops, so a compiled head trains the eager trunk that feeds it. - Caches the executable so a second call with the same input /
parameter signature is a pure run. Which inputs require grad is
part of the signature: a step whose inputs need no gradient
computes none. A loss weight changing
requires_gradretraces.
Parameters
modelnn.ModuleModel whose forward will be traced + compiled.
loss_ fncallableloss_fn(model_output, *extra_inputs) -> Tensor scalar loss.dynamicbool= FalseAccepted for API symmetry with
lucid.compile, but the compiled
training step is always per-shape static: any batch size works (one
fwd+bwd executable per distinct shape), it just does not share a single
symbolic-batch executable across sizes.segmentsint or {auto}= 1Number of executable segments, a positive integer (default
1 — the
monolithic single executable described above). When > 1 and
model is an nn.Sequential, the model is split into that
many contiguous groups of children, each compiled into its own forward
- backward executable and stitched with eager autograd
(
make_segmented_step)."auto"probes a few candidate counts on the first batch and keeps the fastest (make_autoseg_step).
Returns
callablestep(x, *extra_inputs) -> Tensor — the scalar loss.
loss.backward() works the same way as the eager baseline.
Raises
TypeErrorsegments is neither an integer nor a string (a float, None, a
bool).ValueErrorsegments is below 1, or a string other than "auto".Notes
A step whose gradient passes through a custom
lucid.autograd.Function runs eagerly, with a one-time
CompileFallbackWarning: the executable can only differentiate the
ops forward recorded, and the backward the Function defines
— a straight-through estimator, a scaled or reversed gradient — would
not run.
Examples
>>> import lucid, lucid.nn as nn, lucid.nn.functional as F
>>> import lucid.optim as optim
>>> from lucid.compile import make_step
>>> model = nn.Linear(8, 4).to('metal')
>>> step = make_step(model, lambda y, t: F.cross_entropy(y, t))
>>> opt = optim.SGD(model.parameters(), lr=0.1)
>>> for batch in batches:
... opt.zero_grad()
... loss = step(batch.x, batch.target)
... loss.backward()
... opt.step()See Also
- lucid.compile.compile—module-level compile entrypoint.
- lucid.compile.compile_optimizer—even tighter form that fuses
the optimizer update into the same MPSGraph executable.lucid.compile._entry.module.CompiledModule.step—per-instancecached version of the same dispatch.make_segmented_step—thesegments > 1implementation (executablesplitting for memory-bounded deep training).