Skip to content
fn

make_step

→callable
make_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:

  1. Calls loss_fn(model(x), *extra_inputs) once under _tracing to capture the op DAG (forward + loss).
  2. 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.
  3. Runs the executable, populates a Lucid autograd.Function with the resulting (loss, grads) so the returned scalar loss has a working grad_fn and loss.backward() drives .grad on 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.
  4. 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_grad retraces.

Parameters

Model whose forward will be traced + compiled.
loss_fncallable
loss_fn(model_output, *extra_inputs) -> Tensor scalar loss.
dynamicbool= False
Accepted 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}= 1
Number 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

callable

step(x, *extra_inputs) -> Tensor — the scalar loss. loss.backward() works the same way as the eager baseline.

Raises

TypeError
segments is neither an integer nor a string (a float, None, a bool).
ValueError
segments 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-instance
  • cached version of the same dispatch.
  • make_segmented_step—the segments > 1 implementation (executable
  • splitting for memory-bounded deep training).

Used by 2