Skip to content

.backward()

Run backpropagation from this tensor.

If the tensor is unrealized (lazy mode), calls .realize() automatically before running backprop. This means you can always call .backward() directly without a separate .realize() step.

Computes gradients for all leaf tensors in the graph with comp_grad=True. This tensor's gradient is initialized to ones (assumes scalar loss). Non-leaf gradients are freed after the backward pass.

Raises:

  • RuntimeError –

    If comp_grad=False, tensor is empty, or backward has already been called on this non-leaf tensor.