class

no_grad

no_grad()
source

Context manager / decorator that disables gradient tracking.

Inside a no_grad scope every op skips the graph-building bookkeeping that autograd normally performs: no autograd node is registered, no activations are saved, and the produced tensors carry requires_grad=False regardless of their inputs. The flag is process-global, so this affects all code running inside the scope.

Use no_grad for inference, for validation loops, and for parameter updates that should not themselves be differentiable (e.g. EMA accumulation, optimizer steps if performed outside an Optimizer object). The savings — fewer Python objects allocated, no saved activations holding memory, no overhead on each op — are typically large.

The previous gradient mode is restored on exit (RAII), so no_grad may be safely nested inside enable_grad and vice-versa.

Parameters

None
no_grad takes no arguments; configure with set_grad_enabled if a dynamic flag is needed.

Attributes

_prevbool
Internal — saved grad-mode flag captured at __enter__ time and restored at __exit__.

Notes

Gradient mode controls whether autograd records the chain rule

Lx=iLyiyix.\frac{\partial \mathcal{L}}{\partial x} = \sum_i \frac{\partial \mathcal{L}}{\partial y_i} \cdot \frac{\partial y_i}{\partial x}.

With no_grad the graph that this sum is computed over is never built, so calling backward afterwards is a no-op on tensors produced inside the scope.

Examples

As a context manager:
>>> import lucid
>>> from lucid.autograd import no_grad
>>> x = lucid.tensor([1.0, 2.0], requires_grad=True)
>>> with no_grad():
...     y = x * 2
>>> y.requires_grad
False
As a decorator:
>>> @no_grad()
... def eval_step(x):
...     return (x * x).sum()

Methods (3)

dunder

__call__

_F
__call__(fn: _F)
source

Forward to the underlying callable (see class docstring).

dunder

__enter__

no_grad
__enter__()
source

Enter the context. Returns self so the value can be bound via with ... as.

dunder

__exit__

None
__exit__(args: object = ())
source

Exit the context, restoring any state that was modified on entry.