pullback drops cotangents when f repeats a variable or contains wrt, breaking pushforward

Open
#2,700 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@BlackYuriJDU is already working on this.

Since Nov 24, 2026.

  • #2720 by @BlackYuriJDU — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
backend

Research direction

The bug is in pullback in pytensor/gradient.py, which keeps one cotangent per repeated variable and lets output cotangents override gradients from other outputs; pushforward in the same module depends on it. Start by running the reproduction snippets in the issue and checking which inputs explicit_graph_inputs returns. Done means all three pushforward cases return both tangents and the pullback cases return every cotangent, without breaking existing gradient tests.

Written by the indexing model from the issue text.

Description

bug gradients

pytensor.gradient.pushforward drops tangents through Add and Sub, so the directional derivative of a + b depends on only one of the two tangents. The cause is pullback, which keeps only one cotangent per variable when f repeats a variable. It also lets the cotangent of a variable in f override the gradient reaching it through the other outputs. The default pushforward pulls back through a first pullback whose outputs for Add are the output cotangent itself, so it hits both cases.

import pytensor.tensor as pt
from pytensor.gradient import pullback, pushforward
from pytensor.graph.traversal import explicit_graph_inputs

x = pt.dscalar("x")
y_bar_1 = pt.dscalar("y_bar_1")
y_bar_2 = pt.dscalar("y_bar_2")
y = x**2

list(explicit_graph_inputs(pullback([y, y], x, [y_bar_1, y_bar_2])))      # [x, y_bar_2], expected [x, y_bar_1, y_bar_2]
list(explicit_graph_inputs(pullback([x, 2 * x], x, [y_bar_1, y_bar_2])))  # [y_bar_1], expected [y_bar_1, y_bar_2]

a = pt.dscalar("a")
b = pt.dscalar("b")
a_dot = pt.dscalar("a_dot")
b_dot = pt.dscalar("b_dot")

list(explicit_graph_inputs(pushforward(a + b, [a, b], [a_dot, b_dot])))      # [b_dot], expected [a_dot, b_dot]
list(explicit_graph_inputs(pushforward(a - b, [a, b], [a_dot, b_dot])))      # [a_dot], expected [a_dot, b_dot]
list(explicit_graph_inputs(pushforward(a + 2 * b, [a, b], [a_dot, b_dot])))  # [a_dot], expected [a_dot, b_dot]

This came up while computing sparse Jacobians from colored pushforwards for #2578.

Dominant language
Python
Stars
655
Forks
266
Avg merge
3d 23h
Merged PRs (30d)
43

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pymc-devs/pytensor

All issues in pymc-devs/pytensor

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.