Skip to content

[quant] add quantized::batch_norm#39910

Closed
jerryzh168 wants to merge 8 commits intogh/jerryzh168/345/basefrom
gh/jerryzh168/345/head
Closed

[quant] add quantized::batch_norm#39910
jerryzh168 wants to merge 8 commits intogh/jerryzh168/345/basefrom
gh/jerryzh168/345/head

Conversation

@jerryzh168
Copy link
Copy Markdown
Contributor

@jerryzh168 jerryzh168 commented Jun 12, 2020

Stack from ghstack:

Summary:
We need this for graph mode quantization, since we only have aten::batch_norm the dimension
is only known at runtime, we'll need to quantize it to quantized::batch_norm

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Differential Revision: D22012281

Summary:
We need this for graph mode quantization, since we only have `aten::batch_norm` the dimension
is only known at runtime, we'll need to quantize it to `quantized::batch_norm`

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

[ghstack-poisoned]
@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented Jun 12, 2020

💊 CI failures summary and remediations

As of commit cefc65d (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 or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 24 times.

Summary:
We need this for graph mode quantization, since we only have `aten::batch_norm` the dimension
is only known at runtime, we'll need to quantize it to `quantized::batch_norm`

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

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

[ghstack-poisoned]
Summary:
We need this for graph mode quantization, since we only have `aten::batch_norm` the dimension
is only known at runtime, we'll need to quantize it to `quantized::batch_norm`

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

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

[ghstack-poisoned]
Summary:
We need this for graph mode quantization, since we only have `aten::batch_norm` the dimension
is only known at runtime, we'll need to quantize it to `quantized::batch_norm`

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

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

[ghstack-poisoned]
Summary:
We need this for graph mode quantization, since we only have `aten::batch_norm` the dimension
is only known at runtime, we'll need to quantize it to `quantized::batch_norm`

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

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

[ghstack-poisoned]
Summary:
We need this for graph mode quantization, since we only have `aten::batch_norm` the dimension
is only known at runtime, we'll need to quantize it to `quantized::batch_norm`

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

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

[ghstack-poisoned]
Summary:
We need this for graph mode quantization, since we only have `aten::batch_norm` the dimension
is only known at runtime, we'll need to quantize it to `quantized::batch_norm`

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

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

[ghstack-poisoned]
@vkuzo
Copy link
Copy Markdown
Contributor

vkuzo commented Jun 15, 2020

since we only have aten::batch_norm the dimension
is only known at runtime

for dimension, are we talking about BatchNorm{1|2|3}d? If so, can we get it from the module, since the majority of users will probably be using the default PT modules?

}

TORCH_LIBRARY_IMPL(quantized, QuantizedCPU, m) {
m.impl("batch_norm", q_batch_norm_impl<false>);
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.

is this function pointing to the 2d/3d implementations, but not supporting the 1d implementation? Not sure if that's super intuitive. Can we add a comment on why this is needed?

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.

yeah, ideally this should support 1d as well, but there is no support for 1d atm. sure I'll add a comment

@jerryzh168
Copy link
Copy Markdown
Contributor Author

since we only have aten::batch_norm the dimension
is only known at runtime

for dimension, are we talking about BatchNorm{1|2|3}d? If so, can we get it from the module, since the majority of users will probably be using the default PT modules?

yeah, dim refers to batch_norm 1d/2d/3d, this informations is not available in aten ops, it's available only in module. we can't get it from module, we are dealing with aten ops in the IR.

@vkuzo
Copy link
Copy Markdown
Contributor

vkuzo commented Jun 15, 2020

yeah, dim refers to batch_norm 1d/2d/3d, this informations is not available in aten ops, it's available only in module. we can't get it from module, we are dealing with aten ops in the IR.

makes sense for the current implementation. I'm more asking for my own understanding, this info is in the graph before inlining, and we technically could spend eng time to preserve this info through the passes (by adding a new attribute, etc) if we really needed to (not saying we should do this, just trying to understand). Just curious if I understood that correctly.

Summary:
We need this for graph mode quantization, since we only have `aten::batch_norm` the dimension
is only known at runtime, we'll need to quantize it to `quantized::batch_norm`

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

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

[ghstack-poisoned]
@jerryzh168
Copy link
Copy Markdown
Contributor Author

(not saying we should do this, just trying to understand). Just curious if I understood that correctly.

the dimension info is encoded in the check function right now: https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/batchnorm.py#L190, the check function will be different for each module. we'll need to analyze the code to extract this info.

Another thing is if people use F.batch_norm(https://github.com/pytorch/pytorch/blob/master/torch/nn/functional.py#L1998), there is no way we can get the dimension info, the dimension is known at runtime.

@jerryzh168
Copy link
Copy Markdown
Contributor Author

In general this comes from the discrepancy of the APIs between floating point op and quantized op, we have aten::batch_norm in fp but quantized::batch_norm_2d and quantized::batch_norm_3d in quantized.

although in the future I think we should move towards breaking aten::batch_norm to aten::batch_norm_{n}d, this will allow us to handle inheritance.

@vkuzo
Copy link
Copy Markdown
Contributor

vkuzo commented Jun 15, 2020

the dimension info is encoded in the check function right now: https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/batchnorm.py#L190, the check function will be different for each module. we'll need to analyze the code to extract this info.

oh, I was asking about parsing it from the module type instead, not from the graph of the function calls. I.e. if someone is running a vanilla nn.BatchNorm{1|2|3}d Is that challenging / not possible to capture that from the module graph before any kind of inlining, and then assign it to the correponding aten node in the inlining pass? To clarify, not suggesting it, more just looking to understand curious on if this possible in the future if it's needed.

@jerryzh168
Copy link
Copy Markdown
Contributor Author

jerryzh168 commented Jun 16, 2020

the dimension info is encoded in the check function right now: https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/batchnorm.py#L190, the check function will be different for each module. we'll need to analyze the code to extract this info.

oh, I was asking about parsing it from the module type instead, not from the graph of the function calls. I.e. if someone is running a vanilla nn.BatchNorm{1|2|3}d Is that challenging / not possible to capture that from the module graph before any kind of inlining, and then assign it to the correponding aten node in the inlining pass? To clarify, not suggesting it, more just looking to understand curious on if this possible in the future if it's needed.

This is certainly possible, but I don't think we'll do this in the future. I think you'll get a better idea as you work more on graph mode.

We would like to restrict the analysis and transformations locally instead of spreading it out, e.g. you mentioned we need to get this information in module and then "pass" it to aten node, we would only do this when it is absolutely necessary I think, since this can be achieved with a much simpler alternative, as shown in the PR, I don't think it make sense to do it.

@vkuzo
Copy link
Copy Markdown
Contributor

vkuzo commented Jun 16, 2020

the dimension info is encoded in the check function right now: https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/batchnorm.py#L190, the check function will be different for each module. we'll need to analyze the code to extract this info.

oh, I was asking about parsing it from the module type instead, not from the graph of the function calls. I.e. if someone is running a vanilla nn.BatchNorm{1|2|3}d Is that challenging / not possible to capture that from the module graph before any kind of inlining, and then assign it to the correponding aten node in the inlining pass? To clarify, not suggesting it, more just looking to understand curious on if this possible in the future if it's needed.

This is certainly possible, but I don't think we'll do this in the future. I think you'll get a better idea as you work more on graph mode.

We would like to restrict the analysis and transformations locally instead of spreading it out, e.g. you mentioned we need to get this information in module and then "pass" it to aten node, we would only do this when it is absolutely necessary I think, since this can be achieved with a much simpler alternative, as shown in the PR, I don't think it make sense to do it.

thanks a ton. This is really helpful to understand the past tradeoffs and design principles taken in this codebase.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request has been merged in 1a388da.

@facebook-github-bot facebook-github-bot deleted the gh/jerryzh168/345/head branch June 19, 2020 14:16
xwang233 pushed a commit to xwang233/pytorch that referenced this pull request Jun 20, 2020
Summary:
Pull Request resolved: pytorch#39910

We need this for graph mode quantization, since we only have `aten::batch_norm` the dimension
is only known at runtime, we'll need to quantize it to `quantized::batch_norm`

Test Plan: Imported from OSS

Differential Revision: D22012281

fbshipit-source-id: 2973d86a17a02b7bdc36bd1e703e91584d9139d0
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Pull Request resolved: pytorch#39910

We need this for graph mode quantization, since we only have `aten::batch_norm` the dimension
is only known at runtime, we'll need to quantize it to `quantized::batch_norm`

Test Plan: Imported from OSS

Differential Revision: D22012281

fbshipit-source-id: 2973d86a17a02b7bdc36bd1e703e91584d9139d0
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.

4 participants