grad(outputs: Tensor | Sequence[Tensor], inputs: Tensor | Sequence[Tensor], grad_outputs: Tensor | Sequence[Tensor | None] | None = None, retain_graph: bool | None = None, create_graph: bool = False, only_inputs: bool = True, allow_unused: bool | None = None, is_grads_batched: bool = False, materialize_grads: bool = False)Compute gradients of outputs w.r.t. inputs, returning them as a tuple.
The "functional" gradient interface — invoke once to get the partial
derivatives back without touching .grad on any leaf tensor.
Useful for higher-order differentiation, gradient-based meta-learning,
or any pattern where you want to use the gradients as input to a new
computation rather than to update parameters in-place.
Parameters
requires_grad
set in the graph that produced it.requires_grad=True if you want grads
flowing into intermediate nodes).None — for the whole argument or one
entry — is an implicit 1, allowed only for a real output of
one element.retain_ graphbool= Nonecreate_graph.create_ graphbool= FalseTrue, build the autograd graph of the gradient itself so
the returned tensors are differentiable — used by
gradgradcheck and other higher-order recipes.only_ inputsbool= Trueinputs.allow_ unusedbool= NoneTrue, return None for any inputs entry that lies
outside the computation graph of outputs. Defaults to
materialize_grads.is_ grads_ batchedbool= FalseTrue, interpret the leading dimension of each tensor in
grad_outputs as a batch dimension and compute a batch of
vector-Jacobian products. Defaults to False.materialize_ gradsbool= FalseTrue, replace None gradients for unused inputs with
zero-filled tensors matching each input's shape, dtype, and device.
Defaults to False.Returns
tuple[Tensor or None, ...]One gradient per element of inputs, in the same order.
Entries are None only when allow_unused=True (and
materialize_grads=False) and the input is disconnected.
Raises
RuntimeErrorgrad_outputs differs from the number of
outputs, a non-scalar output has no seed, or (without
allow_unused) an input is unreachable from outputs.ValueErrorallow_unused=False and materialize_grads=True.TypeErrorNone.(ShapeMismatch, DtypeMismatch, DeviceMismatch)Notes
Mathematically, grad computes the vector-Jacobian product
via one reverse-mode pass. Unlike Tensor.backward, it does
NOT accumulate into .grad — leaf tensors' existing .grad
values are preserved across the call. For chained gradient
computations (Hessian-vector products, MAML inner loops) this is the
right primitive.
Examples
Scalar output — no seed needed:
>>> import lucid
>>> from lucid.autograd import grad
>>> x = lucid.tensor([1.0, 2.0, 3.0], requires_grad=True)
>>> y = (x * x).sum()
>>> (gx,) = grad(y, [x])
>>> gx # equals 2 * x
tensor([2., 4., 6.])
Vector output — explicit seed:
>>> z = x * x
>>> seed = lucid.ones_like(z)
>>> (gx,) = grad(z, [x], grad_outputs=[seed])
Higher-order with create_graph=True:
>>> y = (x ** 3).sum()
>>> (g,) = grad(y, [x], create_graph=True)
>>> (gg,) = grad(g.sum(), [x]) # second derivative: 6x
>>> gg
tensor([6., 12., 18.])