Skip to content

Instantly share code, notes, and snippets.

@zhiics
Created December 9, 2019 19:32
Show Gist options
  • Save zhiics/30c989f8db719928da9405d33d40c095 to your computer and use it in GitHub Desktop.
Save zhiics/30c989f8db719928da9405d33d40c095 to your computer and use it in GitHub Desktop.
An example that ShapeFunc fails
import tvm
from tvm import relay
import numpy as np
x = relay.var("x", shape=(1, 1), dtype="int32")
x1 = relay.var("x1", shape=(1, 1), dtype="int32")
p0 = relay.var("p0", shape=(1, 1), dtype="int32")
p1 = relay.var("p1", shape=(1, 1), dtype="int32")
a0 = relay.reshape(p0, newshape=[1])
a1 = relay.zeros_like(a0)
a2 = relay.expand_dims(a1, axis=1)
a3 = relay.expand_dims(a0, axis=1)
a4 = relay.reshape(p1, newshape=[1])
a5 = relay.expand_dims(a4, axis=1)
a6 = (a2, a3, a5)
a7 = relay.concatenate(a6, axis=1)
a8 = relay.cast(a7, dtype="float32")
func = relay.Function([p0, p1],
a8,
ret_type=relay.TensorType(
shape=(relay.Any(), relay.Any()), dtype="float32"))
func = func.set_attribute("Primitive", tvm.expr.IntImm("int32", 1))
call = relay.Call(func, [x, x1])
mod = relay.Module.from_expr(call)
with relay.build_config(opt_level=3, disabled_pass=["AlterOpLayout"]):
exe = relay.vm.compile(mod, "llvm")
vm = relay.vm.VirtualMachine(exe)
vm.init(tvm.cpu())
x_data = np.random.random((1, 1)).astype("int32")
x1_data = np.random.random((1, 1)).astype("int32")
data = [x_data, x1_data]
vm.run(*data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment