Skip to content

Quantize Weight for Gemm/Conv on Quantized Model #22969

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

Merged
merged 5 commits into from
Jan 8, 2025

Conversation

centwang
Copy link
Contributor

Some quantized models have QDQ around Conv/Gemm but the weight and/or bias are not quantized. This PR adds WeightBiasQuantization optimizer to quantize float weight and/or bias to INT8 and INT32 tensors respectively. We only do this for weight and/or bias initializer so that ConstantFolding will fold the sub-graph to real quantized initializers during the graph optimization next round.

@centwang centwang marked this pull request as ready for review November 28, 2024 10:20
Copy link
Contributor

@skottmckay skottmckay left a comment

Choose a reason for hiding this comment

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

:shipit:

Copy link
Contributor

@adrianlizarraga adrianlizarraga left a comment

Choose a reason for hiding this comment

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

Thank you!

@centwang centwang merged commit ff0ab0a into main Jan 8, 2025
95 checks passed
@centwang centwang deleted the weicwang/weight_quantization branch January 8, 2025 02:00
snnn pushed a commit that referenced this pull request Jan 8, 2025
Some quantized models have QDQ around Conv/Gemm but the weight and/or
bias are not quantized. This PR adds WeightBiasQuantization optimizer to
quantize float weight and/or bias to INT8 and INT32 tensors
respectively. We only do this for weight and/or bias initializer so that
ConstantFolding will fold the sub-graph to real quantized initializers
during the graph optimization next round.
tarekziade pushed a commit to tarekziade/onnxruntime that referenced this pull request Jan 10, 2025
Some quantized models have QDQ around Conv/Gemm but the weight and/or
bias are not quantized. This PR adds WeightBiasQuantization optimizer to
quantize float weight and/or bias to INT8 and INT32 tensors
respectively. We only do this for weight and/or bias initializer so that
ConstantFolding will fold the sub-graph to real quantized initializers
during the graph optimization next round.
guschmue pushed a commit that referenced this pull request Jan 12, 2025
Some quantized models have QDQ around Conv/Gemm but the weight and/or
bias are not quantized. This PR adds WeightBiasQuantization optimizer to
quantize float weight and/or bias to INT8 and INT32 tensors
respectively. We only do this for weight and/or bias initializer so that
ConstantFolding will fold the sub-graph to real quantized initializers
during the graph optimization next round.
ashrit-ms pushed a commit that referenced this pull request Mar 17, 2025
Some quantized models have QDQ around Conv/Gemm but the weight and/or
bias are not quantized. This PR adds WeightBiasQuantization optimizer to
quantize float weight and/or bias to INT8 and INT32 tensors
respectively. We only do this for weight and/or bias initializer so that
ConstantFolding will fold the sub-graph to real quantized initializers
during the graph optimization next round.
adrianlizarraga added a commit that referenced this pull request Apr 24, 2025
…wnstream node is not QuantizeLinear (#24537)

### Description
Updates the WeightBiasQuantization optimizer to skip processing on
Conv/Gemm nodes if the downstream child node is not a QuantizeLinear.

#### Before this PR
Original graph:
```
input_0 -> DQ -> Conv -> graph_output (or non-Q node)
                 ^  ^
                 |  |
weights_f32------+
                    |
bias_f32------------+
```
Becomes:

```
input_0 -> DQ ------> Conv -> graph_output (or non-Q node)
                      ^  ^
                      |  |
weights_quant -> DQ --+
                         |
bias_quant -> DQ --------+
```
The above is **NOT** a valid QDQ node unit for Conv because the Conv's
output is not consumed by a QuantizeLinear node.

#### With this PR
The above example graph remains unchanged after L1 optimizations:
```
input_0 -> DQ -> Conv -> graph_output (or non-Q node)
                 ^  ^
                 |  |
weights_f32------+
                    |
bias_f32------------+
```


### Motivation and Context
Caused inaccuracy for a customer model. Automatically quantizing the
weights and biases of a Conv/Gemm is detrimental if the output of the
Conv/Gemm is not consumed by a QuantizeLinear node. In this scenario,
the whole node group is not considered a valid QDQ node unit, and so the
EP has to run the Conv/Gemm as float32/float16 anyway. If the Conv/Gemm
is running as float32/float16, then quantizing the weights and biases
introduces inaccuracy for no gain.

PR that originally added this optimizer:
#22969
vraspar pushed a commit that referenced this pull request Apr 28, 2025
…wnstream node is not QuantizeLinear (#24537)

### Description
Updates the WeightBiasQuantization optimizer to skip processing on
Conv/Gemm nodes if the downstream child node is not a QuantizeLinear.

#### Before this PR
Original graph:
```
input_0 -> DQ -> Conv -> graph_output (or non-Q node)
                 ^  ^
                 |  |
weights_f32------+
                    |
bias_f32------------+
```
Becomes:

```
input_0 -> DQ ------> Conv -> graph_output (or non-Q node)
                      ^  ^
                      |  |
weights_quant -> DQ --+
                         |
bias_quant -> DQ --------+
```
The above is **NOT** a valid QDQ node unit for Conv because the Conv's
output is not consumed by a QuantizeLinear node.

#### With this PR
The above example graph remains unchanged after L1 optimizations:
```
input_0 -> DQ -> Conv -> graph_output (or non-Q node)
                 ^  ^
                 |  |
weights_f32------+
                    |
bias_f32------------+
```


### Motivation and Context
Caused inaccuracy for a customer model. Automatically quantizing the
weights and biases of a Conv/Gemm is detrimental if the output of the
Conv/Gemm is not consumed by a QuantizeLinear node. In this scenario,
the whole node group is not considered a valid QDQ node unit, and so the
EP has to run the Conv/Gemm as float32/float16 anyway. If the Conv/Gemm
is running as float32/float16, then quantizing the weights and biases
introduces inaccuracy for no gain.

PR that originally added this optimizer:
#22969
ankitm3k pushed a commit to intel/onnxruntime that referenced this pull request May 12, 2025
…wnstream node is not QuantizeLinear (microsoft#24537)

### Description
Updates the WeightBiasQuantization optimizer to skip processing on
Conv/Gemm nodes if the downstream child node is not a QuantizeLinear.

#### Before this PR
Original graph:
```
input_0 -> DQ -> Conv -> graph_output (or non-Q node)
                 ^  ^
                 |  |
weights_f32------+
                    |
bias_f32------------+
```
Becomes:

```
input_0 -> DQ ------> Conv -> graph_output (or non-Q node)
                      ^  ^
                      |  |
weights_quant -> DQ --+
                         |
bias_quant -> DQ --------+
```
The above is **NOT** a valid QDQ node unit for Conv because the Conv's
output is not consumed by a QuantizeLinear node.

#### With this PR
The above example graph remains unchanged after L1 optimizations:
```
input_0 -> DQ -> Conv -> graph_output (or non-Q node)
                 ^  ^
                 |  |
weights_f32------+
                    |
bias_f32------------+
```


### Motivation and Context
Caused inaccuracy for a customer model. Automatically quantizing the
weights and biases of a Conv/Gemm is detrimental if the output of the
Conv/Gemm is not consumed by a QuantizeLinear node. In this scenario,
the whole node group is not considered a valid QDQ node unit, and so the
EP has to run the Conv/Gemm as float32/float16 anyway. If the Conv/Gemm
is running as float32/float16, then quantizing the weights and biases
introduces inaccuracy for no gain.

PR that originally added this optimizer:
microsoft#22969
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants