-
Notifications
You must be signed in to change notification settings - Fork 699
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
[graph] Add clone() method for Module, copying all non-placeholder nodes #2197
Conversation
Thanks for working on this clone! This feature may be helpful for graph partitioning :) I would like to understand why in your case the placeholders could be shared by modules. Does that mean all the modules must use the same input? |
@beicy I believe the placeholder's must be shared with the clone if we want to drive it with the same kind of Context. All cloned modules must use the same input name and types/shape but the actual input & output tensors live in the Context object and each module can be driven by distinct contexts. So we can do something like this:
And output1 and output2 will contain the output of their respective inputs, even if the execution happened in parallel. |
The next step will be to avoid the double compile and clone the CompiledFunction. |
@nickgg Thanks! I misunderstood the "shared placeholders" -- I thought they shared the same memory location. I think it will be good to add your above example to unittests:) |
@nickgg Why do we need to clone the module? I thought that we just want to clone functions within the module No? |
@nadavrot I think cloning the module is useful for two reasons:
|
@nickgg RE: # 2, our optimizations should create a copy of the Constant being modified (e.g. see |
I'm concerned by needing to recompile a function just to run it on a different device. Is there a fundamental reason why that is necessary? |
@bertmaher No, theres not - it just seemed like an easier intermediate step for prototyping. I think since our designs have moved past this and this seems like not a safe or smart thing to do I'll just abandon this and we can come back to it if we need it. |
Description: Add ability to clone a
Module
, which creates a copy of the graph and all constants but that shares Placeholders and Types.** For Constants: we copy the backing Tensor memory, since compiling Functions in a Module can be destructive of the Constant representation.
** For Placeholders we now have multiple Modules holding a raw ptr, and need a count to manage lifetimes. The obvious solution is to use a shared_ptr here but that would change a lot of interfaces and I wasn't sure about it. Instead I've added a
Refcounted
trait which has a similar effect. I think we should work out the right ownership model here sometime soon.** For Types we can just keep a shared_ptr to the list since it is not modified after graph create time. There is no thread safety across cloned Modules, just as there is no thread safety when using a single Module.
Testing: ninja test with the new test verifying cloned modules share only Placeholder objects in the graph.
Documentation: n/a