Skip to content

Python binding api to optimize for mobile model on script module.#36357

Closed
xcheng16 wants to merge 5 commits intogh/xcheng16/17/basefrom
gh/xcheng16/17/head
Closed

Python binding api to optimize for mobile model on script module.#36357
xcheng16 wants to merge 5 commits intogh/xcheng16/17/basefrom
gh/xcheng16/17/head

Conversation

@xcheng16
Copy link
Copy Markdown
Contributor

@xcheng16 xcheng16 commented Apr 10, 2020

Stack from ghstack:

Summary:
Creating a python api entry to optimize mobile model which takes a scripted module as argument and returns an optimized scripted module. The initial optimization features includes inserting and folding prepack ops.

Test Plan:
python test/test_optimizer.py

Differential Revision: D20946076

NOTE FOR REVIEWERS: This PR has internal Facebook specific changes or comments, please review them on Phabricator!

xcheng16 pushed a commit that referenced this pull request Apr 10, 2020
Differential Revision: [D20946076](https://our.internmc.facebook.com/intern/diff/D20946076/)

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D20946076/)!

ghstack-source-id: 101896315
Pull Request resolved: #36357
@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented Apr 10, 2020

💊 Build failures summary and remediations

As of commit 81504d3 (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This 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.

See how this bot performed.

This comment has been revised 67 times.

xcheng16 pushed a commit that referenced this pull request Apr 10, 2020
Pull Request resolved: #36357


ghstack-source-id: 101907180

Differential Revision: [D20946076](https://our.internmc.facebook.com/intern/diff/D20946076/)
xcheng16 pushed a commit that referenced this pull request Apr 11, 2020
xcheng16 pushed a commit that referenced this pull request Apr 11, 2020
Copy link
Copy Markdown
Contributor

@AshkanAliabadi AshkanAliabadi left a comment

Choose a reason for hiding this comment

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

Many thanks for doing this Xingying! This innocent PR enables some of the most important FP32 optimizations we've been working on these past few months. :) Exciting.

Comment thread torch/utils/optimizer.py
@@ -0,0 +1,26 @@
"""
This module contains utility method for mobile model optimization and lint.
"""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would still like to evangelize for this function to be placed somewhere more accessible, ideally in the torch.jit Python module itself, given its impact on the overall performance on mobile. The difference between running vs neglecting to run this function on a model (potentially because the user is not aware of it) at this point is a factor of at least 2x on FP32. Otherwise we will be mostly reliant on documentation and samples to publicize the presence of this function. In my opinion this function is more than a utility - more like a necessity.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am not convinced that by placing in torch.jit namespace it will require less evangelization. We will still have to advertise in best practices for mobile etc. And of course we can move this if it is seems more expedient.
I agree on the part that this should be more of a necessity than utility though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

how about we create a new mobile folder like one of other proposal @dreiss had. Having the path like torch.mobile.optimize_for_mobile instead of utility if you both feel utility does not give much of necessity.

Comment thread torch/utils/optimizer.py Outdated
buffer = io.BytesIO()
torch.jit.save(scripted_model, buffer)
buffer.seek(0)
optimized_script_model = torch.jit.load(buffer)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Curious why we have to save and load the model? Does torch._C._jit_pass_optimize_for_mobile "enable" the pass, and it's only executed on save?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes. @xcheng16, are you doing this because _jit_pass_optimize_for_mobile does inplace transformation of the script module? I think it probably is best for us to change that. Then you can just do return torch._C._jit_pass_optimize_for_mobile(scripted_model._c)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

torch._C._jit_pass_optimize_for_mobile(scripted_model._c) does not provide the inplace transformation of the script module, that is why I save and load again. I can check on _jit_pass_optimize_for_mobile method for why it is the case.

Comment thread test/test_optimizer.py Outdated
Comment on lines +6 to +30
batch_size = 2
input_channels_per_group = 6
height = 16
width = 16
output_channels_per_group = 6
groups = 4
kernel_h = kernel_w = 3
stride_h = stride_w = 1
pad_h = pad_w = 1
dilation = 1
input_channels = input_channels_per_group * groups
output_channels = output_channels_per_group * groups
strides = (stride_h, stride_w)
paddings = (pad_h, pad_w)
dilations = (dilation, dilation)
conv_weight_shape = (output_channels, input_channels_per_group, kernel_h, kernel_w)
conv_bias_shape = (output_channels)

input_data = torch.rand((batch_size, input_channels, height, width))
conv_weight = torch.rand((output_channels, input_channels_per_group, kernel_h, kernel_w))
conv_bias = torch.rand((output_channels))
result = F.conv2d(input_data, conv_weight, conv_bias, strides, paddings, dilations, groups)
weight_output_dim = 24
linear_input_shape = result.shape[1]
linear_weight_shape = (weight_output_dim, linear_input_shape)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you move all of this in test_optimize_for_mobile function? We shouldn't have any global variable initialization that is not ncessary.

Comment thread torch/utils/optimizer.py
@@ -0,0 +1,26 @@
"""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we name this file mobile_optimizer? True that our prepacking and packed op optimizations are not strictly mobile specific, but I wonder if we will want to put other non-mobile specific optimization in torch.utils.optimizer namespace.
If we do, maybe we should get a consensus from others who will want to have their optimizations places here? Maybe quantization team? @AshkanAliabadi @dreiss your opinions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If we move it to mobile folder, does it still necessary to check the name into mobile_optimizer?

Comment thread torch/utils/optimizer.py
import torch


def optimize_for_mobile(scripted_model):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, I think it is important to point out that current optimization pass does the optimization inplace. Maybe we can change that so we return a new module.

…module."


Summary:
Creating a python api entry to optimize mobile model which takes a scripted module as argument and returns an optimized scripted module.  The initial optimization features includes inserting and folding prepack ops.

Test Plan:
python test/test_optimizer.py  

Differential Revision: [D20946076](https://our.internmc.facebook.com/intern/diff/D20946076/)

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D20946076/)!

[ghstack-poisoned]
xcheng16 pushed a commit that referenced this pull request Apr 15, 2020
…module."


Summary:
Creating a python api entry to optimize mobile model which takes a scripted module as argument and returns an optimized scripted module.  The initial optimization features includes inserting and folding prepack ops.

Test Plan:
python test/test_optimizer.py  

Differential Revision: [D20946076](https://our.internmc.facebook.com/intern/diff/D20946076/)

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D20946076/)!

[ghstack-poisoned]
xcheng16 pushed a commit that referenced this pull request Apr 16, 2020
…module."


Summary:
Creating a python api entry to optimize mobile model which takes a scripted module as argument and returns an optimized scripted module.  The initial optimization features includes inserting and folding prepack ops.

Test Plan:
python test/test_optimizer.py  

Differential Revision: [D20946076](https://our.internmc.facebook.com/intern/diff/D20946076/)

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D20946076/)!

[ghstack-poisoned]
xcheng16 pushed a commit that referenced this pull request Apr 16, 2020
…module."


Summary:
Creating a python api entry to optimize mobile model which takes a scripted module as argument and returns an optimized scripted module.  The initial optimization features includes inserting and folding prepack ops.

Test Plan:
python test/test_optimizer.py  

Differential Revision: [D20946076](https://our.internmc.facebook.com/intern/diff/D20946076/)

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D20946076/)!

[ghstack-poisoned]
xcheng16 pushed a commit that referenced this pull request Apr 16, 2020
Comment thread test/test_mobile_optimizer.py Outdated
linear_weight_shape = (weight_output_dim, linear_input_shape)

class MyTestModule(torch.nn.Module):
def __init__(self, activation_fn=F.relu):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since this test is really use for relu only, lets remove activation_fn. Just called relu directly in forward.

Comment thread test/test_mobile_optimizer.py Outdated
Comment on lines +73 to +85
pattern_count_map = {"Tensor = aten::conv2d": -1,
"Tensor = prim::CallFunction": -1,
"prepacked::conv2d_clamp_prepack": -1,
"prepacked::conv2d_clamp_run": 1,
"prepacked::linear_clamp_prepack": -1,
"prepacked::linear_clamp_run": 1}
for pattern, v in pattern_count_map.items():
if (v == 0):
FileCheck().check(pattern).run(optimized_scripted_model.graph)
elif (v == -1):
FileCheck().check_not(pattern).run(optimized_scripted_model.graph)
else:
FileCheck().check_count(pattern, v, exactly=True).run(optimized_scripted_model.graph)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you can replace this part with FileCheck() directly. I used it earlier for convenience. I think you can just have a series of filechecks.

FileCheck().check_not("Tensor = aten::conv2d") \
                    .check_not("Tensor = prim::CallFunction") \
                    .check_not("prepacked::conv2d_clamp_prepack") \
                    .check("prepacked::conv2d_clamp_run")....\
                    .run(optimized_scripted_model.graph)

Comment thread torch/utils/mobile_optimizer.py Outdated
scripted_model: An instance of torch script module with type of ScriptModule

Returns:
scripted_model: A new optimized torch script module, the method does not do
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You dont need to say, it doe not do inplace transformation. Given it already returns a new module, I think it is expected.

Copy link
Copy Markdown
Contributor

@kimishpatel kimishpatel left a comment

Choose a reason for hiding this comment

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

Small changes.

…module."


Summary:
Creating a python api entry to optimize mobile model which takes a scripted module as argument and returns an optimized scripted module.  The initial optimization features includes inserting and folding prepack ops.

Test Plan:
python test/test_optimizer.py  

Differential Revision: [D20946076](https://our.internmc.facebook.com/intern/diff/D20946076/)

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D20946076/)!

[ghstack-poisoned]
xcheng16 pushed a commit that referenced this pull request Apr 17, 2020
Copy link
Copy Markdown
Contributor

@kimishpatel kimishpatel left a comment

Choose a reason for hiding this comment

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

Looks great. Thanks Xingying.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request has been merged in 86f354c.

@facebook-github-bot facebook-github-bot deleted the gh/xcheng16/17/head branch April 21, 2020 14:16
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
…torch#36357)

Summary:
Pull Request resolved: pytorch#36357
ghstack-source-id: 101907180

Creating a python api entry to optimize mobile model which takes a scripted module as argument and returns an optimized scripted module. The initial optimization features includes inserting and folding prepack ops.

Test Plan: python test/test_optimizer.py

Differential Revision: D20946076

fbshipit-source-id: 93cb4a5bb2371128f802d738eb26d0a4f3b2fe10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants