Creating tvm.target twice will create empty params

Calling target = tvm.target.create('llvm') twice will create empty params.

The following is the code snippet I used, which was copied from the quick start tutorial: https://docs.tvm.ai/tutorials/relay_quick_start.html

opt_level = 3
target = tvm.target.create('llvm')
with relay.build_config(opt_level=opt_level):
    graph, lib, params = relay.build_module.build(
        mod, target, params=params)

I ran it in jupyter notebook on both Mac and Linux platform.

Only if I ran this segment twice, the value of params will be {}

Trying to look into the code of autotvm.task.dispatcher, seems that there is somthing wrong with the context cache.

After analyzing the code segment in the tutorial, I found that the issue was caused by reuse of the variable “params”.

Variable “params” contained the parameters from the original model, then it was passed into relay.build_model.build(), then replaced by the return value params.

If we call the relay.build_model.build() twice, we will pass the built params into the build method again, which generate empty params.

Suggest to modify the tutorial code, to separate the original parameters and built parameters, something like this:

with relay.build_config(opt_level=opt_level):
    graph, lib, params = relay.build_module.build(
        mod, target, params=original_params)
1 Like