When I ignore the auto tuning, TVM and Keras have a consistent prediction result. However, After tuning the the operator with tuning time 2, the compiled model lost the ability of predicting.
WARNING:root:Could not find any valid schedule for task Task(func_name=layout_transform, args=(('TENSOR', (10, 4, 28, 28, 128), 'float32'), 'NCHW128c', 'NCHW2c'), kwargs={}, workload=('layout_transform', ('TENSOR', (10, 4, 28, 28, 128), 'float32'), 'NCHW128c', 'NCHW2c')). A file containing the errors has been written to /tmp/tvm_tuning_errors_irwzqkj2.log.
The /tmp/tvm_tuning_errors_irwzqkj2.log is following:
This is the result that comparing the prediction result beteen Keras and model TVM with auto tuning.
Script are following
import keras
import os
import tvm
import tvm.relay as relay
import numpy as np
from PIL import Image
from tvm.contrib import graph_runtime
from tvm.autotvm.tuner import XGBTuner, GATuner, RandomTuner, GridSearchTuner
from tvm.autotvm.graph_tuner import DPTuner, PBQPTuner
from tvm import autotvm
def getTestData(pic_number):
x_return = []
pic = Image.open("test_pic.png")
pic = np.array(pic)
for i in range(pic_number):
x_return.append(pic)
return np.array(x_return)
def compare(model_name):
batch_size = 10 # test_pic number, you can change the batch_size here!
x_test = getTestData(batch_size) # get test images
predict_model = keras.models.load_model(model_name)
res_keras = predict_model.predict(x_test)
# ############################ load keras model to relay IRModule ##################################
input_shape = (batch_size,3,224,224)
output_shape = (batch_size, 1000)
shape_dict = {"input_1": input_shape}
target = 'llvm -mcpu=core-avx2'
ctx = tvm.cpu(0)
irmod, params = relay.frontend.from_keras(predict_model, shape_dict)
# ########################## Compile the RelayIR ####################################################
with autotvm.apply_graph_best("resnet50-imagenet_origin.h5_graph_opt_tuning_2.log"):
with tvm.transform.PassContext(opt_level=3):
graph, lib, params = relay.build_module.build(
irmod, target=target, params=params)
module = graph_runtime.create(graph, lib, ctx)
test_x_tvm = x_test.transpose([0, 3, 1, 2])
dtype = 'float32'
data = test_x_tvm.astype(dtype)
module.set_input("input_1", data)
module.set_input(**params)
module.run()
res_tvm = module.get_output(0, tvm.nd.empty(output_shape)).asnumpy()
# ########################### calc tvm accuracy #################################################
np.testing.assert_allclose(res_keras,res_tvm,rtol=1e-5,atol=1e-5)
if __name__ == '__main__':
model_name = "resnet50-imagenet_origin.h5"
compare(model_name)
resnet50-imagenet_origin.h5_graph_opt_tuning_2.log can be accessed with this link: