Reshape MAX API OP giving error

My code:
fn main() raises:
dim1 = Dim(2)
dim2 = Dim(2)
x = List[Dim] ()
x.append(dim1)
x.append(dim2)

var graph0 = Graph(in_types=List[Type](TensorType(DType.float32, 1, 2, 2)))
var r = ops.reshape(graph0[0], x)
fn main() raises:
dim1 = Dim(2)
dim2 = Dim(2)
x = List[Dim] ()
x.append(dim1)
x.append(dim2)

var graph0 = Graph(in_types=List[Type](TensorType(DType.float32, 1, 2, 2)))
var r = ops.reshape(graph0[0], x)
Error: candidate not viable: argument #1 cannot be converted from 'List[Dim, 0]' to 'List[Dim, 0]'
2 Replies
capt_falamer
capt_falamer2mo ago
I'm not getting any errors when running this (same as above but with the import statements):
from max.graph import Graph, ops
from max.graph.type import TensorType, Dim, Type

fn main() raises:
dim1 = Dim(2)
dim2 = Dim(2)
x = List[Dim] ()
x.append(dim1)
x.append(dim2)

var graph0 = Graph(in_types=List[Type](TensorType(DType.float32, 1, 2, 2)))
var r = ops.reshape(graph0[0], x)
from max.graph import Graph, ops
from max.graph.type import TensorType, Dim, Type

fn main() raises:
dim1 = Dim(2)
dim2 = Dim(2)
x = List[Dim] ()
x.append(dim1)
x.append(dim2)

var graph0 = Graph(in_types=List[Type](TensorType(DType.float32, 1, 2, 2)))
var r = ops.reshape(graph0[0], x)
what version of mojo/max are you running?
taalhaataahir01022001
Sorry my bad!! Didn't make the correct imports Also I'm facing some issues with the code. ops.reshapes is showing unexpected behaviour. Here's my code:
from max.graph.type import TensorType, Dim, Type
from random import seed
from max import engine
from max.tensor import Tensor
from max.graph import Graph, ops

fn main() raises:
seed(43)
var session = engine.InferenceSession()
var t = Tensor[DType.float32].randn((1, 2, 2))
print("t:\n", t)
dim1 = Dim(2)
dim2 = Dim(2)
x = List[Dim]()
x.append(dim1)
x.append(dim2)

var graph0 = Graph(in_types=List[Type](TensorType(DType.float32, 1, 2, 2)))
var r = ops.reshape(graph0[0], x)
graph0.output(r)
graph0.verify()
var rs = session.load(graph0)
results = rs.execute("input0", t)
var output = results.get[DType.float32]("output0")
print("output:\n",output)
from max.graph.type import TensorType, Dim, Type
from random import seed
from max import engine
from max.tensor import Tensor
from max.graph import Graph, ops

fn main() raises:
seed(43)
var session = engine.InferenceSession()
var t = Tensor[DType.float32].randn((1, 2, 2))
print("t:\n", t)
dim1 = Dim(2)
dim2 = Dim(2)
x = List[Dim]()
x.append(dim1)
x.append(dim2)

var graph0 = Graph(in_types=List[Type](TensorType(DType.float32, 1, 2, 2)))
var r = ops.reshape(graph0[0], x)
graph0.output(r)
graph0.verify()
var rs = session.load(graph0)
results = rs.execute("input0", t)
var output = results.get[DType.float32]("output0")
print("output:\n",output)
Output: t: Tensor([[[1.2788280248641968, -1.2873831987380981], [-0.56482106447219849, 1.5331770181655884]]], dtype=float32, shape=1x2x2) output: Tensor([[0.0, 0.0], [-0.56482106447219849, 1.5331770181655884]], dtype=float32, shape=2x2) But if I add the following code at the end of my code:
print("t.reshape((2,2)):\n", t.reshape((2,2)))
print("t.reshape((2,2)):\n", t.reshape((2,2)))
The output becomes correct: t: Tensor([[[1.2788280248641968, -1.2873831987380981], [-0.56482106447219849, 1.5331770181655884]]], dtype=float32, shape=1x2x2) output: Tensor([[1.2788280248641968, -1.2873831987380981], [-0.56482106447219849, 1.5331770181655884]], dtype=float32, shape=2x2) t.reshape((2,2)): Tensor([[1.2788280248641968, -1.2873831987380981], [-0.56482106447219849, 1.5331770181655884]], dtype=float32, shape=2x2) And it's not the case with reshape only. e.g., this program uses split op:
fn main() raises:
seed(43)
var session = engine.InferenceSession()
var t = Tensor[DType.float32].randn((6, 4))
print("t:\n", t)

var splitting = Graph(in_types=List[Type](TensorType(DType.float32, 6,4)))
var s = ops.split[2](splitting[0], ((2, 4)))
var o = List[Symbol] ()
o.append(s[0])
o.append(s[1])
splitting.output(o)
splitting.verify()
var splt = session.load(splitting)

results = splt.execute("input0", t)
var output = results.get[DType.float32]("output0")
print("output:\n",output)

var output1 = results.get[DType.float32] ("output1")
print("output1:\n", output1)
fn main() raises:
seed(43)
var session = engine.InferenceSession()
var t = Tensor[DType.float32].randn((6, 4))
print("t:\n", t)

var splitting = Graph(in_types=List[Type](TensorType(DType.float32, 6,4)))
var s = ops.split[2](splitting[0], ((2, 4)))
var o = List[Symbol] ()
o.append(s[0])
o.append(s[1])
splitting.output(o)
splitting.verify()
var splt = session.load(splitting)

results = splt.execute("input0", t)
var output = results.get[DType.float32]("output0")
print("output:\n",output)

var output1 = results.get[DType.float32] ("output1")
print("output1:\n", output1)
And this is the output log:
t:
Tensor([[1.2788280248641968, -1.2873831987380981, -0.56482106447219849, 1.5331770181655884],
[0.80901926755905151, -0.24991747736930847, -0.8085181713104248, 0.42681333422660828],
[1.8432825803756714, -0.60748469829559326, 0.24848183989524841, -0.3757627010345459],
[1.6167833805084229, 1.0938529968261719, 0.35813808441162109, 0.071096442639827728],
[1.5071860551834106, -0.54008448123931885, -0.18514452874660492, 0.30583590269088745],
[0.34934023022651672, -1.6976139545440674, -1.2638821601867676, -0.97573941946029663]], dtype=float32, shape=6x4)
output:
Tensor([[2.2701208643344754e+37, 4.2833490159016683e-41, -0.56482106447219849, 1.5331770181655884],
[0.80901926755905151, -0.24991747736930847, -0.8085181713104248, 0.42681333422660828]], dtype=float32, shape=2x4)
output1:
Tensor([[1.8432825803756714, -0.60748469829559326, 0.24848183989524841, -0.3757627010345459],
[1.6167833805084229, 1.0938529968261719, 0.35813808441162109, 0.071096442639827728],
[1.5071860551834106, -0.54008448123931885, -0.18514452874660492, 0.30583590269088745],
[0.34934023022651672, -1.6976139545440674, -1.2638821601867676, -0.97573941946029663]], dtype=float32, shape=4x4)
t:
Tensor([[1.2788280248641968, -1.2873831987380981, -0.56482106447219849, 1.5331770181655884],
[0.80901926755905151, -0.24991747736930847, -0.8085181713104248, 0.42681333422660828],
[1.8432825803756714, -0.60748469829559326, 0.24848183989524841, -0.3757627010345459],
[1.6167833805084229, 1.0938529968261719, 0.35813808441162109, 0.071096442639827728],
[1.5071860551834106, -0.54008448123931885, -0.18514452874660492, 0.30583590269088745],
[0.34934023022651672, -1.6976139545440674, -1.2638821601867676, -0.97573941946029663]], dtype=float32, shape=6x4)
output:
Tensor([[2.2701208643344754e+37, 4.2833490159016683e-41, -0.56482106447219849, 1.5331770181655884],
[0.80901926755905151, -0.24991747736930847, -0.8085181713104248, 0.42681333422660828]], dtype=float32, shape=2x4)
output1:
Tensor([[1.8432825803756714, -0.60748469829559326, 0.24848183989524841, -0.3757627010345459],
[1.6167833805084229, 1.0938529968261719, 0.35813808441162109, 0.071096442639827728],
[1.5071860551834106, -0.54008448123931885, -0.18514452874660492, 0.30583590269088745],
[0.34934023022651672, -1.6976139545440674, -1.2638821601867676, -0.97573941946029663]], dtype=float32, shape=4x4)
It is also extracting the wrong first 2 outputs in row 1.
Want results from more Discord servers?
Add your server