Skip to content

Scan invalid indexing is not always caught #215

Open
@ricardoV94

Description

@ricardoV94

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingscan

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions