-
Notifications
You must be signed in to change notification settings - Fork 26.3k
optimize_for_mobile: bring packed params to root module #42041
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
Conversation
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: [ghstack-poisoned]
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: 09de2e1 Pull Request resolved: #42041
|
|
||
| for (Node* n : b->nodes()) { | ||
|
|
||
| // TODO before land: also check for n->inputs()[0]->node()->kind() == {ConvNd} |
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.
would love some tips on best way to do this
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.
💊 CI failures summary and remediationsAs of commit 145f9fb (more details on the Dr. CI page):
ci.pytorch.org: 1 failedThis comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group. This comment has been revised 42 times. |
kimishpatel
left a comment
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.
Looks pretty good. Left one comment.
test/test_mobile_optimizer.py
Outdated
|
|
||
| # generic case | ||
|
|
||
| m = Parent() |
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.
nit: can you put these two test cases in a loop?
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.
and conv module attribute is removed
| std::vector<Node*> getAttrNodes; | ||
| Node* curNode = getConvPackedParamsNode->inputs()[0]->node(); | ||
| while (!(curNode->outputs()[0]->type() == graph->inputs()[0]->type())) { | ||
| TORCH_CHECK( | ||
| curNode->kind() == prim::GetAttr, | ||
| "Attempted to add a non-prim::GetAttr node to a chain of prim::getAttr nodes."); | ||
| getAttrNodes.insert(getAttrNodes.begin(), curNode); | ||
| curNode = curNode->inputs()[0]->node(); | ||
| } | ||
| TORCH_CHECK(getAttrNodes.size() > 0, "Did not find a chain of prim::getAttr nodes"); |
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.
we have some helper function in quantization to do this: https://codebrowser.bddppq.com/pytorch/pytorch/torch/csrc/jit/passes/quantization/helper.cpp.html#_ZN5torch3jit16getInvokedModuleERNS0_6ModuleEPNS0_4NodeEPNS0_5ValueE, feel free to use them
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.
oh this one: https://codebrowser.bddppq.com/pytorch/pytorch/torch/csrc/jit/passes/quantization/helper.cpp.html#_ZN5torch3jit19getInvokedModuleOptERKNS0_6ModuleEPNS0_4NodeEPNS0_5ValueE since maybe we will fail to get module
| std::string newName = prefix + "." + suffix + "_packed_params"; | ||
|
|
||
| // make sure the attribute does not already exist | ||
| TORCH_CHECK( |
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.
if the name already exists maybe we can create a new one? e.g. https://codebrowser.bddppq.com/pytorch/pytorch/torch/csrc/jit/passes/quantization/insert_observers.cpp.html#937
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 would imagine if we hit a duplicate it would be more likely that this code has a bug, compared to the user having something named _jit_pass_hoist_conv_packed_params.{foo}.{bar}.{conv_foobar} before this pass.
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.
if . can appear in attribute names then we might hit duplicate in extreme cases, although it is very unlikely
e.g. consider {foo}.{bar} is also an attribute itself
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.
good point, thx, will update
| // traverse the chain to get packed params value | ||
| std::string curName = getAttrNodes[0]->s(attr::name); | ||
| Module curConvModule = rootModule.attr(curName).toModule(); | ||
| for (int idx = 1; idx < getAttrNodes.size(); idx++) { |
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.
lint
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: [ghstack-poisoned]
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: 444a54a Pull Request resolved: #42041
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: [ghstack-poisoned]
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: [ghstack-poisoned]
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: Differential Revision: [D22801605](https://our.internmc.facebook.com/intern/diff/D22801605) [ghstack-poisoned]
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: c83039b Pull Request resolved: #42041
jerryzh168
left a comment
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.
LGTM, please fix the failing tests.
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: Differential Revision: [D22801605](https://our.internmc.facebook.com/intern/diff/D22801605) [ghstack-poisoned]
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: 4efe840 Pull Request resolved: #42041
|
we should wait for #42462 to be resolved before landing this |
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: Differential Revision: [D22801605](https://our.internmc.facebook.com/intern/diff/D22801605) [ghstack-poisoned]
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: Differential Revision: [D22801605](https://our.internmc.facebook.com/intern/diff/D22801605) [ghstack-poisoned]
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: e2aad66 Pull Request resolved: #42041
| // %n = | ||
| // prim::GetAttr[name="{prefix}.name1{...}.name(n-1)._packed_params"][%self] | ||
| // | ||
| void hoistConvPackedParams( |
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.
Can we also add a pass for linear? Is there value in doing this for linear modules too?
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 don't see why not, although I'd defer to the mobile team on the priority.
test/test_mobile_optimizer.py
Outdated
| model_optim = optimize_for_mobile(model) | ||
| self.assertFalse(hasattr(model_optim.conv1, "bias")) | ||
| self.assertFalse(hasattr(model_optim.child.conv2, "bias")) | ||
|
|
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.
Is the understanding that one cannot serialize/deserialize a model after optimize_for_mobile is called?
…t module" Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: Differential Revision: [D22801605](https://our.internmc.facebook.com/intern/diff/D22801605) [ghstack-poisoned]
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: fd8104b Pull Request resolved: #42041
…ms to root module" Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: Differential Revision: [D22801605](https://our.internmc.facebook.com/intern/diff/D22801605) [ghstack-poisoned]
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: 7e3ba82 Pull Request resolved: #42041
kimishpatel
left a comment
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.
Looks good. Thanks.
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: Differential Revision: [D22801605](https://our.internmc.facebook.com/intern/diff/D22801605) [ghstack-poisoned]
Summary: Adds a pass to hoist conv packed params to root module. The benefit is that if there is nothing else in the conv module, subsequent passes will delete it, which will reduce module size. For context, freezing does not handle this because conv packed params is a custom object. Test Plan: ``` PYTORCH_JIT_LOG_LEVEL=">hoist_conv_packed_params.cpp" python test/test_mobile_optimizer.py TestOptimizer.test_hoist_conv_packed_params ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: b2ce0fc Pull Request resolved: #42041
|
resubmitted as #42740 due to a ghstack snafu |
Stack from ghstack:
Summary:
Adds a pass to hoist conv packed params to root module.
The benefit is that if there is nothing else in the conv module,
subsequent passes will delete it, which will reduce module size.
For context, freezing does not handle this because conv packed
params is a custom object.
Test Plan:
Reviewers:
Subscribers:
Tasks:
Tags:
Differential Revision: D22801605