Skip to content

[VM] Move param bind to OptimizeModule#7451

Merged
masahi merged 3 commits into
apache:mainfrom
masahi:vm-param-bind
Feb 13, 2021
Merged

[VM] Move param bind to OptimizeModule#7451
masahi merged 3 commits into
apache:mainfrom
masahi:vm-param-bind

Conversation

@masahi

@masahi masahi commented Feb 12, 2021

Copy link
Copy Markdown
Member

In VM, BindParamsByName is run inside VMCompiler::Lower(...) before OptimizeModule gets called. So when we try to dump the optimized module like this:

compiler = relay.vm.VMCompiler()
opt_mod, _ = compiler.optimize(mod, target, params=params)
print(opt_mod)

param binding doesn't happen, so the "optimized graph" has parameters as arguments. This means constant folding involving parameters cannot happen, so the optimized graph looks very different (e.g. there are many layout_transform) from the output of relay.optimize, which does param binding when relay.optimize is called.

IRModule Optimize(IRModule relay_module, const TargetsMap& targets,
const std::unordered_map<std::string, runtime::NDArray>& params) {
ICHECK(relay_module.defined()) << "The IRModule must be defined for the Relay compiler.";
if (params.size()) {
ICHECK(relay_module->ContainGlobalVar("main")) << "Missing the main entry function";
GlobalVar main_glb_var = relay_module->GetGlobalVar("main");
Function main_func = Downcast<Function>(relay_module->Lookup(main_glb_var));
auto new_main = BindParamsByName(main_func, params);

This PR moves param binding in VM to OptimizeModule, to be consistent with the graph codegen. Now VMCompiler.optimize(...) returns the graph with constants properly folded.

please review @zhiics @jroesch @icemelon9

Comment thread src/relay/backend/vm/compiler.cc Outdated
@jwfromm

jwfromm commented Feb 12, 2021

Copy link
Copy Markdown
Contributor

This makes a lot more sense to me, thanks Masa.

@icemelon icemelon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Could you add a test case for this?

@masahi

masahi commented Feb 12, 2021

Copy link
Copy Markdown
Member Author

@icemelon9 Added a test to verify that all params are bound in the output of vm.optimize(). This can be checked by counting the number of free vars in the main body. I think this is neat!

@masahi

masahi commented Feb 13, 2021

Copy link
Copy Markdown
Member Author

@icemelon9 ready to merge

Comment thread src/relay/backend/vm/compiler.cc Outdated
@masahi masahi force-pushed the vm-param-bind branch 2 times, most recently from 2847fbd to db2530a Compare February 13, 2021 07:26
@masahi masahi merged commit 0aa90b0 into apache:main Feb 13, 2021
Lokiiiiii pushed a commit to Lokiiiiii/tvm that referenced this pull request Mar 2, 2021
* [VM] Move param bind to OptimizeModule

* add test to verify the number of free vars after opt

* remove const from OptimizeModule
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Mar 2, 2021
* [VM] Move param bind to OptimizeModule

* add test to verify the number of free vars after opt

* remove const from OptimizeModule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants