Closed Ch0ronomato closed 3 weeks ago
Which version of numpy?
Numpy is
numpy 1.26.4 py310hd45542a_0 conda-forge
You had some problems in how you specified the graph. The error is a bit obtuse, but what happened is that you defined a pytensor function from scalar inputs to scalar outputs (note that end
is a float32, not a vector of float32). Instead you want to do something like this:
vector_end = pt.vector(dtype="float32")
_, p, _, _, done = e(n_steps, f0, f1, vector_end, i)
fn = pytensor.function([n_steps,
vector_end,
In(f0, value=0),
In(f1, value=1),
In(i, value=2)], [p, done])
print(fn(np.float32(7.0), np.array([10.0, 5.0, 6.0]).astype("float32")))
Note how n_steps is still left as a flot32, not a tensor of float32, so I define it as np.float32
In general your example is still a bit odd, in that we usually use scalar types only to define the inner ScalarLoop graph, but then use only elemwise types in the actual function. I also swapped the float32 for int32 in the variables that seem to behave as integers, not that it matters. The full example looks like:
import numpy as np
import pytensor
import pytensor.tensor as pt
from pytensor.scalar import ScalarLoop, float32, int32
from pytensor.tensor.elemwise import Elemwise
n_steps = int32("n_steps")
f0 = float32("f0")
f1 = float32("f1")
end = int32("end")
i = int32("end")
op = ScalarLoop(
init=[f0, f1, end, i],
update=[
pytensor.scalar.basic.identity(f1),
f0 + f1,
pytensor.scalar.basic.identity(end),
i + 1
],
until=i >= end,
)
e = Elemwise(op)
# Elemwise takes as inputs tensor types. If you pass scalars you will see a `TensorFromScalar` Op in the graph
n_steps = pt.scalar("n_steps", dtype="int32")
f0 = pt.scalar("f0", dtype="float32")
f1 = pt.scalar("f1", dtype="float32")
end = pt.vector("end", dtype="int32")
i = pt.scalar("i", dtype="int32")
_, p, _, _, done = e(n_steps, f0, f1, end, i)
fn = pytensor.function([n_steps, f0, f1, end, i], [p, done])
print(
fn(
n_steps=np.array(7).astype("int32"),
f0=np.array(0.0).astype("float32"),
f1=np.array(1.0).astype("float32"),
end=np.array([10, 5, 6]).astype("int32"),
i=np.array(2).astype("int32"),
)
)
# [array([21., 5., 8.], dtype=float32), array([False, True, True])]
Sounds good - thanks for letting me know the example also looks strange. Will help me when actually testing the right thing.
Describe the issue:
Wrote a small example to get the
Elemwise
andScalarLoop
pattern. The code computes the(n + 1)th
fibonacci number. When running it usinglinker=py
, things work fine. When running it using the default cvm, I getScalar check failed (numpy_int32)
.Reproducable code example:
Error message:
PyTensor version information:
Python 3.11.9
; cloned from repo and ran. All config below.Context for the issue:
This doesn't particularly affect me. I want to make sure the torch scalarloop and elemwise work well, and this seemed like a good way to test that; also ran into this.