Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VM] Move param bind to OptimizeModule #7451

Merged
merged 3 commits into from
Feb 13, 2021
Merged

Conversation

masahi
Copy link
Member

@masahi masahi commented Feb 12, 2021

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

@jwfromm
Copy link
Contributor

jwfromm commented Feb 12, 2021

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

Copy link
Member

@icemelon icemelon left a comment

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
Copy link
Member Author

masahi commented Feb 12, 2021

@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
Copy link
Member Author

masahi commented Feb 13, 2021

@icemelon9 ready to merge

src/relay/backend/vm/compiler.cc Outdated Show resolved Hide resolved
@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