pymc-devs / pymc-devs/pytensor
Scan invalid indexing is not always caught
Open
Nobody has claimed this yet.
bug
scan
- Dominant language
- Python
- Stars
- 644
- Forks
- 208
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 16
Description
Description
import numpy as np
import pytensor
import pytensor.tensor as pt
print("Testing with n_steps")
x = pt.scalar("x")
for n_steps in (0, 2, 100, 1):
ys, _, = pytensor.scan(
lambda xtm1: xtm1 + 1,
outputs_info=[x],
n_steps=n_steps,
)
for test in (200, -200):
try:
ys[test].eval({x: 0})
except IndexError:
pass
else:
print(f"ys[{test}] did not raise for {n_steps=}")
print("Testing with sequences")
xs = pt.vector("xs")
for n_steps in (0, 2, 100, 1):
zs, _, = pytensor.scan(
lambda o, x: o + x,
outputs_info=[pt.zeros(())],
sequences=[xs]
)
for test in (200, -200):
try:
zs[test].eval({xs: np.arange(n_steps)})
except IndexError:
pass
except ValueError:
print(f"zs[{test}] failed with a wrong ValueError for {n_steps=}")
else:
print(f"zs[{test}] did not raise for {n_steps=}")
Testing with n_steps
ys[-200] did not raise for n_steps=2
ys[-200] did not raise for n_steps=100
ys[200] did not raise for n_steps=1
ys[-200] did not raise for n_steps=1
Testing with sequences
zs[200] failed with a wrong ValueError for n_steps=0
zs[-200] failed with a wrong ValueError for n_steps=0
zs[-200] did not raise for n_steps=2
zs[-200] did not raise for n_steps=100
zs[-200] did not raise for n_steps=1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the examples using pytensor.scan, the ys/zs indexing expressions, and eval calls shown in the issue. Trace how out-of-range positive and negative indexes are handled for n_steps and sequences. Done means invalid indexes consistently raise IndexError instead of being silently accepted or producing the reported ValueError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100