Eigh of a constant gets a different eigenvector sign inside Scan than outside

Open
#2,724 10 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #2725 by @lyn1dev — closed without merging

Assessment

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

Research direction

Start by reading the scan_push_out_non_seq change referenced by commit 14da9a865 and trace how constant-only expressions are handled inside Scan. Compare the constant-folding path through NumPy with the compiled inner path described in the report. The issue is resolved when the repeated-eigenvalue example returns consistent eigenvector signs inside and outside Scan; a linked closed pull request may contain relevant work.

Written by the indexing model from the issue text.

Description

bug graph rewriting scan

A compiled function that reads the same pt.linalg.eigh output inside and outside a Scan gets two different values for it. Since 14da9a865, scan_push_out_non_seq keeps constant-only expressions inside the scan, so the Eigh runs twice. Constant folding evaluates the outer copy through numpy, and the numba-compiled inner function evaluates the inner one, and the two pick opposite signs for the eigenvectors of a repeated eigenvalue.

import pytensor
import pytensor.tensor as pt

_, eigenvectors = pt.linalg.eigh(25.0 * pt.eye(2))
corner = eigenvectors[0, 0]

trace = pytensor.scan(
    lambda total: total + corner, outputs_info=[pt.zeros(())], n_steps=2, return_updates=False
)
f = pytensor.function([], [trace[-1], corner])

print(f())  # [-2.0, 1.0], expected [2.0, 1.0] as on ae782b0fb
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.