The following graph has 5D input. It can be compiled, but it fails during the run. Compilation:
import tensorflow as tf
import numpy as np
from tvm import relay
import os
dtype='float32'
input_name = "input"
dshape=(1,8,2,2,512)
with tf.Session() as sess:
x = tf.placeholder(shape=dshape, dtype=dtype, name=input_name)
mp1 = tf.nn.max_pool3d(x, ksize=[1,8,1,1,1], padding="SAME", strides=[1,1,1,1,1],name="max_pool3d")
graph_def = sess.graph_def
output_graph_def = tf.graph_util.convert_variables_to_constants(
sess,
graph_def,
["max_pool3d",])
mod, params = relay.frontend.from_tensorflow(output_graph_def, layout='NCHW', shape={input_name: dshape})
target = "llvm"
with relay.build_config(opt_level=3):
graph, lib, params = relay.build(mod, target, params=params)
print("Compilation done")
print("Saving files")
out_folder = "./"
path_lib = out_folder + "model.so"
lib.export_library(path_lib)
print("export_library done")
with open(out_folder + "model.json", "w") as fo:
fo.write(graph)
with open(out_folder + "model.params", "wb") as fo:
fo.write(relay.save_param_dict(params))
print("Files saved to", out_folder)
Run:
import numpy as np
import tvm
from tvm.contrib import graph_runtime as runtime
import time
ms = lambda: int(round(time.time() * 1000))
input_name = "input"
dshape=(1,8,2,2,512)
data = np.random.sample(dshape).astype("float32")
print("data.shape", data.shape)
base = "./"
path_lib = base + "model.so"
path_graph = base + "model.json"
path_param = base + "model.params"
graph = open(path_graph).read()
params = bytearray(open(path_param, "rb").read())
lib = tvm.module.load(path_lib)
m = runtime.create(graph, lib, tvm.cpu())
m.load_params(params)
print("load_params done")
kwargs = {input_name: data}
for i in range(10):
t1 = ms()
m.run(**kwargs)
tvm_output0 = m.get_output(0).asnumpy()
t2 = ms()
print(tvm_output0.shape)
print("time: {:,} ms".format(t2-t1))
Error:
data.shape (1, 8, 2, 2, 512)
load_params done
Traceback (most recent call last):
File "./run-tvm.py", line 42, in <module>
m.run(**kwargs)
File "/usr/local/lib/python3.5/dist-packages/tvm-0.6.0-py3.5-linux-x86_64.egg/tvm/contrib/graph_runtime.py", line 169, in run
self._run()
File "/usr/local/lib/python3.5/dist-packages/tvm-0.6.0-py3.5-linux-x86_64.egg/tvm/_ffi/_ctypes/function.py", line 207, in __call__
raise get_last_ffi_error()
tvm._ffi.base.TVMError: Traceback (most recent call last):
[bt] (3) /usr/local/lib/python3.5/dist-packages/tvm-0.6.0-py3.5-linux-x86_64.egg/tvm/libtvm.so(TVMFuncCall+0x61) [0x7f2146ec1811]
[bt] (2) /usr/local/lib/python3.5/dist-packages/tvm-0.6.0-py3.5-linux-x86_64.egg/tvm/libtvm.so(tvm::runtime::GraphRuntime::Run()+0x47) [0x7f2146f37ef7]
[bt] (1) /usr/local/lib/python3.5/dist-packages/tvm-0.6.0-py3.5-linux-x86_64.egg/tvm/libtvm.so(+0xc39e67) [0x7f2146f37e67]
[bt] (0) /usr/local/lib/python3.5/dist-packages/tvm-0.6.0-py3.5-linux-x86_64.egg/tvm/libtvm.so(+0xbd1041) [0x7f2146ecf041]
File "/root/tvm/src/runtime/library_module.cc", line 91
TVMError: Check failed: ret == 0 (-1 vs. 0) : Assert fail: (2 == int32(arg1.shape[3])), Argument arg1.shape[3] has an unsatisfied constraint