-
Notifications
You must be signed in to change notification settings - Fork 82
[Rewriter] Implement zero bias removal for Conv operations and related rules #2555
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
Open
whyvineet
wants to merge
18
commits into
microsoft:main
Choose a base branch
from
whyvineet:remove-optional-bias
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+596
−4
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6cfd6fd
[Rewriter] Implement zero bias removal for Conv operations and relate…
whyvineet 48037a8
Merge branch 'microsoft:main' into remove-optional-bias
whyvineet fd028ee
Merge branch 'main' of github-personal:whyvineet/onnxscript into remo…
whyvineet 3742e7b
[Rewriter] Enhance zero bias removal for Conv, ConvTranspose, Gemm, a…
whyvineet 8bfa65f
Refactor zero bias removal tests to use helper function and improve s…
whyvineet 6fc3ca7
Merge branch 'main' of github-personal:whyvineet/onnxscript into remo…
whyvineet b93a56c
Refactor test cases for zero bias removal to improve readability and …
whyvineet e322625
Remove duplicate import of _fuse_batchnorm in rewriter module
whyvineet 121360e
Refactor zero bias removal logic to streamline input handling and enh…
whyvineet ee7dafa
Refactor Gemm operation pattern and check method to align with zero b…
whyvineet 8ef6c41
Enhance zero bias removal logic to filter bias parameters and preserv…
whyvineet 2b9dda4
Refactor bias removal logic to directly use operation inputs, improvi…
whyvineet 153b4e7
Remove redundant domain attribute from operation inputs in _RemoveZer…
whyvineet ce64fb7
Merge branch 'main' into remove-optional-bias
justinchuby a94f8b9
Merge HEAD, branch 'remove-optional-bias' of github-personal:whyvinee…
whyvineet 86de85f
Refactor IR value creation in tests to use `ir.Value` for consistency…
whyvineet d62eafb
Revert "Refactor IR value creation in tests to use `ir.Value` for con…
whyvineet d4f73dd
Enhance attribute comparison in optimization tests to handle list vs …
whyvineet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
|
||
# Licensed under the MIT License. | ||
"""Remove optional bias when it is all zero from Conv and related operations.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import ClassVar | ||
|
||
import numpy as np | ||
|
||
from onnxscript import ir | ||
from onnxscript.ir import convenience | ||
from onnxscript.rewriter._basics import MatchResult | ||
from onnxscript.rewriter._rewrite_rule import RewriteRuleClassBase, RewriteRuleSet | ||
|
||
|
||
class _RemoveZeroBiasBase(RewriteRuleClassBase): | ||
"""Base class for removing zero bias from operations.""" | ||
|
||
def rewrite(self, op: ir.tape.Tape, out: ir.Value, **_) -> ir.Value: | ||
"""Remove the bias input from the operation.""" | ||
node = out.producer() | ||
|
||
return op.op( | ||
self.op_type, | ||
inputs=node.inputs[:-1], | ||
attributes=node.attributes, | ||
) | ||
|
||
def _check_bias_is_zero(self, bias_value: ir.Value) -> MatchResult: | ||
"""Check if the bias value is present and is all zeros.""" | ||
check_result = MatchResult() | ||
|
||
# Check if bias is a constant/initializer | ||
bias_tensor = convenience.get_const_tensor(bias_value) | ||
if bias_tensor is None: | ||
return check_result.fail("Bias is not a constant/initializer.") | ||
|
||
# Check if bias is all zeros | ||
bias_array = bias_tensor.numpy() | ||
if not np.allclose(bias_array, 0.0, atol=1e-8): | ||
return check_result.fail("Bias is not all zeros.") | ||
|
||
return check_result | ||
|
||
def check(self, context, x: ir.Value, w: ir.Value, b: ir.Value, **_) -> MatchResult: | ||
"""Check if the bias is present and is all zeros.""" | ||
del context # Unused | ||
return self._check_bias_is_zero(b) | ||
|
||
|
||
class RemoveZeroBiasFromConv(_RemoveZeroBiasBase): | ||
"""Remove zero bias from Conv operations.""" | ||
|
||
op_type: ClassVar = "Conv" | ||
|
||
def pattern(self, op: ir.tape.Tape, x: ir.Value, w: ir.Value, b: ir.Value) -> ir.Value: | ||
return op.Conv(x, w, b, _outputs=["out"]) | ||
|
||
|
||
class RemoveZeroBiasFromConvTranspose(_RemoveZeroBiasBase): | ||
"""Remove zero bias from ConvTranspose operations.""" | ||
|
||
op_type: ClassVar = "ConvTranspose" | ||
|
||
def pattern(self, op: ir.tape.Tape, x: ir.Value, w: ir.Value, b: ir.Value) -> ir.Value: | ||
return op.ConvTranspose(x, w, b, _outputs=["out"]) | ||
|
||
|
||
class RemoveZeroBiasFromQLinearConv(_RemoveZeroBiasBase): | ||
"""Remove zero bias from QLinearConv operations.""" | ||
|
||
op_type: ClassVar = "QLinearConv" | ||
|
||
def pattern( | ||
self, | ||
op: ir.tape.Tape, | ||
x, | ||
x_scale, | ||
x_zero_point, | ||
w, | ||
w_scale, | ||
w_zero_point, | ||
y_scale, | ||
y_zero_point, | ||
b: ir.Value, | ||
) -> ir.Value: | ||
return op.QLinearConv( | ||
x, | ||
x_scale, | ||
x_zero_point, | ||
w, | ||
w_scale, | ||
w_zero_point, | ||
y_scale, | ||
y_zero_point, | ||
b, | ||
_outputs=["out"], | ||
) | ||
|
||
|
||
class RemoveZeroBiasFromGemm(_RemoveZeroBiasBase): | ||
"""Remove zero bias from Gemm operations.""" | ||
|
||
op_type: ClassVar = "Gemm" | ||
|
||
def pattern(self, op: ir.tape.Tape, x: ir.Value, w: ir.Value, b: ir.Value) -> ir.Value: | ||
return op.Gemm(x, w, b, _outputs=["out"]) | ||
|
||
|
||
# Create rule instances | ||
remove_zero_bias_from_conv_rule = RemoveZeroBiasFromConv().rule() | ||
remove_zero_bias_from_conv_transpose_rule = RemoveZeroBiasFromConvTranspose().rule() | ||
remove_zero_bias_from_qlinear_conv_rule = RemoveZeroBiasFromQLinearConv().rule() | ||
remove_zero_bias_from_gemm_rule = RemoveZeroBiasFromGemm().rule() | ||
|
||
rules = RewriteRuleSet( | ||
[ | ||
remove_zero_bias_from_conv_rule, | ||
remove_zero_bias_from_conv_transpose_rule, | ||
remove_zero_bias_from_qlinear_conv_rule, | ||
remove_zero_bias_from_gemm_rule, | ||
] | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.