-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
fix relay.build to not change the module argument in place #5822
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the fix!
src/relay/backend/build_module.cc
Outdated
@@ -244,6 +244,9 @@ class RelayBuildModule : public runtime::ModuleNode { | |||
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); | |||
// copy module to avoid changing our input | |||
relay_module = IRModule(relay_module->functions, relay_module->type_definitions, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback!
OK, so I changed it to call CopyOnWrite
and it appears to work.
To understand this better, I got the re-instatiation from FunctionPassNode::operator()
here:
https://github.com/apache/incubator-tvm/blob/99745a44407f2d1bd06b8c6a47e6c6c5239ec665/src/relay/ir/transform.cc#L123
is there a reason that isn't done via CopyOnWrite
?
Superficially, it seems that the update is then
https://github.com/apache/incubator-tvm/blob/99745a44407f2d1bd06b8c6a47e6c6c5239ec665/src/relay/ir/transform.cc#L135
which would seem very similar to the Update
done in the Optimize
https://github.com/apache/incubator-tvm/blob/99745a44407f2d1bd06b8c6a47e6c6c5239ec665/src/relay/backend/build_module.cc#L247
which itself just calls add
https://github.com/apache/incubator-tvm/blob/99745a44407f2d1bd06b8c6a47e6c6c5239ec665/src/ir/module.cc#L271-L273
Is something that could also use CopyOnWrite
or is it not applicable there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must admit I'm not understanding this very well yet, in particular in for the use of the new relay_module_ptr
I get from CopyOnWrite
and the original module.
Which one would I use for what after CopyOnWrite?
758002d
to
fa9f8ec
Compare
Now it's not working anymore. :( I think I need a hint. |
Ooops let me take a closer look |
What I would do instead is to simply move Note that for mods pass from python, CopyOnWrite always triggers a copy, unless we do |
fa9f8ec
to
469b9ea
Compare
@tqchen Thank you for the hint. I applied it, but I must admit I'm not entirely certain what's going on. Do I need to call Update on the ptr returned by CopyOnWrite or on original module? Previously I called it on the original module, but 1) I'm not sure, 2) then the compiler complains about the return of CopyOnWrite being unused. Also, it seems that CopyOnWrite + Update is certain to do the copy, is it? |
469b9ea
to
9f1d172
Compare
We don't have to use the ptr of CopyOnWrite in this case, the module also get updated to points to the new copy . |
@t-vi Here is what is going on: Internally, |
Thanks for the explanation, I'll have to read the source a bit more, but now I can do so with a high-level sense of what's going on. 🙂 Happily the CI likes it better now, too. |
Thanks @t-vi for the contribution ! Thanks @junrushao1994 for reviewing |
This fixes the observation at
https://discuss.tvm.ai/t/tvm-relay-build-modifying-its-argument/6958