Here is the script that reproduces the issue: ``` import sys import os
import tvm from tvm import relay from tvm import autotvm from tvm.autotvm.tuner import XGBTuner, GATuner, RandomTuner, GridSearchTuner from tvm.autotvm.graph_tuner import DPTuner, PBQPTuner import tflite.Model # # This function loads the model # def get_model(input_type): # # Models come from https://www.tensorflow.org/lite/guide/hosted_models # The path is hard coded, change to match your setup # base = "./my_models/mobilenet_v2/" model_name = base + "mobilenet_v2_1.0_224_quant.tflite" # # Model parameters # input_name = "input" dshape = (1, 224, 224, 3) with open(model_name, "rb") as f: tflite_model_buf = f.read() assert input_type == "uint8", "Quantized models use uint8 input_type" tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0) mod, params =\ relay.frontend.from_tflite(tflite_model, shape_dict={input_name: dshape}, dtype_dict={input_name: input_type}) # # Assume model from TFLite is NHWC, convert to NCHW # # print(mod["main"].body, file=open('relay_NHWC.txt', 'w')) with relay.build_config(opt_level=3): seq = tvm.transform.Sequential([ relay.transform.RemoveUnusedFunctions(), relay.transform.ConvertLayout(layout) ]) mod = seq(mod) # print(mod["main"].body, file=open('relay_NCHW.txt', 'w')) return mod, params, input_name, dshape # # Kernel tuning function # def tune_kernels(tasks, n_trial, measure_option, tuner='random', early_stopping=None, log_filename='tuning.log', graph_filename=None, layout=None, target=None, context=None, use_existing=None): for i, task in enumerate(tasks): prefix = "[Task %2d/%2d] " % (i+1, len(tasks)) # create tuner if tuner == 'xgb' or tuner == 'xgb-rank': tuner_obj = XGBTuner(task, loss_type='rank') elif tuner == 'ga': tuner_obj = GATuner(task, pop_size=50) elif tuner == 'random': tuner_obj = RandomTuner(task) elif tuner == 'gridsearch': tuner_obj = GridSearchTuner(task) else: raise ValueError("Invalid tuner: " + tuner) # do tuning n_trial = min(n_trial, len(task.config_space)) tuner_obj.tune(n_trial=n_trial, early_stopping=early_stopping, measure_option=measure_option, callbacks=[ autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.log_to_file(log_filename)]) # # Use graph tuner to achieve graph level optimal schedules # Set use_DP=False if it takes too long to finish. # def tune_graph(graph, input_name, dshape, records, opt_sch_file, use_DP=True): target_op = [relay.op.get("nn.conv2d"), relay.op.get("nn.contrib_depthwise_conv2d_NCHWc"), relay.op.get("nn.contrib_conv2d_NCHWc"), ] Tuner = DPTuner if use_DP else PBQPTuner executor = Tuner(graph, {input_name: dshape}, records, target_op, target) executor.benchmark_layout_transform(min_exec_num=2000) executor.run() executor.write_opt_sch2record_file(opt_sch_file) # # Tune the graph # def tune(tuning_opt, input_type): # # Read the model and get the relevant parameters, then extract workload # mod, params, input_name, data_shape = get_model(input_type) # # Tune the tasks # target_op = [relay.op.get("nn.conv2d"), relay.op.get("nn.contrib_depthwise_conv2d_NCHWc"), relay.op.get("nn.contrib_conv2d_NCHWc"), ] tasks = autotvm.task.extract_from_program(mod["main"], target=target, params=params, ops=target_op) tune_kernels(tasks, **tuning_opt) tune_graph(mod["main"], input_name, data_shape, sch_log, graph_log, True) with autotvm.apply_graph_best(graph_log): logging.info("Compiling the schedule") with relay.build_config(opt_level=3): graph, lib, params = relay.build_module.build( mod, target=target, params=params) # # Export the model # base = "./out/" lib.export_library(base + "binary.so") with open(base + "graph.json", "w") as fo: fo.write(graph) with open(base + "params.params", "wb") as fo: fo.write(relay.save_param_dict(params)) if __name__ == "__main__": # # Global variables that define the model # target = "llvm -mcpu=skylake" ctx = tvm.cpu() model_name = "mobilenetv2" dtype_input = "uint8" layout = "NCHW" batch_size = 1 num_threads = 4 os.environ["TVM_NUM_THREADS"] = str(num_threads) # # Set the log filenames # graph_log = "%s_%s_graph_opt.log" % (model_name, dtype_input) sch_log = "%s_%s.log" % (model_name, dtype_input) # # Tuning parameters # tuning_option = { "log_filename": sch_log, "graph_filename": graph_log, "layout": layout, "target": target, "context": ctx, "tuner": "random", "n_trial": 20, "early_stopping": None, "measure_option": autotvm.measure_option( builder=autotvm.LocalBuilder(timeout=100), runner=autotvm.LocalRunner(number=10, repeat=1, min_repeat_ms=1000), ), } tune(tuning_option, dtype_input) ``` The problem should look like (just showing the first few lines): ``` %0 = layout_transform(%input, src_layout="NHWC", dst_layout="NCHW"); %1 = layout_transform(%v_param_1, src_layout="HWIO", dst_layout="OIHW"); %2 = qnn.conv2d(%0, %1, 128, 122, 0.0078125f, 0.0339689f, strides=[2, 2], padding=[0, 0, 1, 1], channels=32, kernel_size=[3, 3], out_dtype="int32") an internal invariant was violated while typechecking your program [13:38:14] /Users/alopez/Documents/Code/LatentAI/tvm/src/relay/qnn/op/convolution.cc:50: Check failed: data->dtype == DataType::Int(8) || data->dtype == DataType::UInt(8): Expected qnn conv2d type(int8, uint8) for input but was float32 ; ; %3 = expand_dims(%v_param_2, axis=0, num_newaxis=3); %4 = layout_transform(%3, src_layout="NHWC", dst_layout="NCHW"); %5 = add(%2, %4); %6 = qnn.requantize(%5, 0.000265382f, 0, 0.0235285f, 0, axis=1, out_dtype="uint8") an internal invariant was violated while typechecking your program [13:38:14] /Users/alopez/Documents/Code/LatentAI/tvm/src/relay/qnn/op/requantize.cc:250: Check failed: data != nullptr: .... ``` Let me know if you need more information, and thanks for looking into this! --- [Visit Topic](https://discuss.tvm.ai/t/autotvm-task-extract-from-program-in-tflite/6578/17) to respond. You are receiving this because you enabled mailing list mode. To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/c68ac2ad7b4199bd0035874dcf5516e204d7d7a686be36274d2bd7fd2b848b12).