Repository navigation
Fuse Conv with Add when the bias is the first Add input - #33819
Open
Yash Dayani (YashDayani) wants to merge 1 commit into
Open
Yash Dayani (YashDayani) wants to merge 1 commit into
Yash Dayani (YashDayani) wants to merge 1 commit into
Conversation
ConvAddFusion only looked for the constant bias at Add input 1, so Add(bias, Conv(X, W)) was not fused. Add is commutative, so take the bias from whichever Add input is not the Conv output. Fixes microsoft#33818
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Author
|
@microsoft-github-policy-service agree |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
ConvAddFusionfolds a constant per-channelAddinto theConvbias, but it only looked for that constant atAddinput 1. As a result,Add(bias, Conv(X, W))was left unfused whileAdd(Conv(X, W), bias)was fused.Addis commutative, so both forms should fuse.onnxruntime/core/optimizer/conv_add_fusion.cc: a new helper,GetAddBiasInputIndex, returns theAddinput that is not theConvoutput.SatisfyConditionandApplyuse it instead of a hard-coded index 1.SatisfyConditionalready requires theAddto have exactly one input edge, and that edge comes from theConv. So exactly oneAddinput is theConvoutput, and the other must be a constant.Add(conv_out, conv_out)has two input edges and is still rejected.[1, C, 1, 1]or[C, 1, 1], so the operand order does not change the output shape or values.FinalizeNodeFusiondoes not depend on whichAddinput theConvoutput uses.onnxruntime/test/optimizer/graph_transform_test.cc:FuseConvAddWithBiasOnEitherAddInputchecks both operand orders, each with and without aConvbias. It asserts that theAddis fused away and compares outputs against the unoptimized graph. Without this change, the two bias-first cases fail.NegativeFuseConvAddWithNonConstantOtherInputchecks thatAdd(Y, Conv(X, W)), whereYis a graph input, andAdd(conv_out, conv_out)are still not fused.Tested on macOS arm64 (Release, CPU): all ConvAdd tests pass, as do 934 tests matching
GraphTransformation*:NchwcOptimizer*:QDQTransformer*:TransposeOptimizer*:*Fusion*:*Optimizer*. The changed files are formatted with the repo's pinned clang-format.Motivation and Context
Fixes #33818. Models whose exporter emits the bias as the first
Addoperand kept a separateAddafter every suchConv, at all optimization levels.ConvMulFusion(conv_mul_fusion.cc) has the same limitation forMul(scale, Conv(X, W)). I left it out of this PR to keep the change focused, and can follow up separately.