Skip to content

[Feature] Add TensorDict.backward() - #1733

Merged
vmoens merged 1 commit into
pytorch:mainfrom
vmoens:backward-td
Jul 7, 2026
Merged

[Feature] Add TensorDict.backward()#1733
vmoens merged 1 commit into
pytorch:mainfrom
vmoens:backward-td

Conversation

@vmoens

@vmoens vmoens commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Description

Adds TensorDictBase.backward() with semantics matching torch.Tensor.backward() across differentiable tensor leaves.

tensordict.backward(
    gradient=None,
    retain_graph=None,
    create_graph=False,
    inputs=None,
)

Behavior

  • Recursively collects differentiable tensor leaves and differentiates the graph through a single torch.autograd.backward(tensors, grad_tensors, ...) call. Non-differentiable leaves are ignored; an error is raised when no leaf requires gradients.
  • With gradient=None, an implicit gradient of 1 is used when every differentiable leaf is scalar, so loss_td.backward() is equivalent to loss_td.sum(reduce=True).backward(). The standard PyTorch-style error is raised if any differentiable leaf is non-scalar.
  • With an explicit gradient, a matching TensorDictBase is required. Gradients are matched to leaves by nested key, with clear errors for missing keys and incompatible shapes. Heterogeneous leaf shapes, devices and dtypes are supported, which enables weighted losses:
weights = TensorDict(
    actor_loss=torch.tensor(0.5),
    critic_loss=torch.tensor(1.0),
)
loss_td.backward(weights)
  • retain_graph, create_graph and inputs are forwarded consistently with torch.Tensor.backward(); inputs also accepts a TensorDictBase.
  • TensorDict.sum() semantics are unchanged: plain .sum() still reduces each leaf independently and returns a TensorDict, and .sum(reduce=True) remains the explicit full reduction.
  • The method is exposed on tensorclasses through the TD fallback list (with the matching .pyi stub).

Tests

TestBackward in test/tensordict/test_methods.py covers scalar and non-scalar leaves, nested keys, explicit gradient TensorDicts, weighted gradients (including parity with (loss_td * weights).sum(reduce=True).backward()), mixed differentiable/non-differentiable leaves, missing/incompatible/mis-typed gradients, no differentiable leaves, retain_graph/create_graph/inputs forwarding, and parity with the equivalent plain-PyTorch expressions. A parametrized test_backward runs against all TensorDict variants (nested, stacked, permuted, h5, params, ...), plus a tensorclass test.

Docs

Documented in the "Gradient computation" section of docs/source/reference/td.rst; the docstring ships a runnable example.

🤖 Generated with Claude Code

Add TensorDictBase.backward() mirroring torch.Tensor.backward() across
differentiable tensor leaves:

- Recursively collects leaves that require gradients; non-differentiable
  leaves are ignored, and an error is raised when none requires grad.
- With gradient=None, uses an implicit gradient of 1 when every
  differentiable leaf is scalar (equivalent to sum(reduce=True).backward()),
  and raises the standard PyTorch-style error otherwise.
- With an explicit gradient, requires a TensorDictBase whose entries match
  the differentiable leaves by nested key, with clear errors for missing
  keys and shape mismatches. Heterogeneous shapes, dtypes and devices are
  supported, enabling weighted losses via a matching TensorDict.
- Forwards retain_graph, create_graph and inputs (accepting a TensorDictBase
  for the latter) to a single torch.autograd.backward() call.

The method is exposed on tensorclasses through the TD fallback list.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 7, 2026
@github-actions github-actions Bot added Feature New feature documentation Improvements or additions to documentation Test tensorclass labels Jul 7, 2026
@vmoens
vmoens merged commit 78f212a into pytorch:main Jul 7, 2026
67 of 69 checks passed
@vmoens
vmoens deleted the backward-td branch July 7, 2026 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. documentation Improvements or additions to documentation Feature New feature tensorclass Test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant