Skip to content

[Rewriter] Support omitted pads and ensure dtype safety in Conv affine fusion - #3213

Open
Hardik K (hardikkaurani) wants to merge 10 commits into
microsoft:mainfrom
hardikkaurani:fix/conv-affine-padding-and-dtype-safety
Open

Hardik K (hardikkaurani) wants to merge 10 commits into
microsoft:mainfrom
hardikkaurani:fix/conv-affine-padding-and-dtype-safety

Conversation

@hardikkaurani

@hardikkaurani Hardik K (hardikkaurani) commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR improves AffineConvFusion and ConvAffineFusion in _fuse_conv_affine.py by supporting omitted default padding attributes, validating padding configurations, and preserving weight and bias dtypes.

Problem

  1. Omitted Default Pads: ONNX defaults omitted Conv.pads to zero padding. The existing pattern required explicit pads=[0, 0, 0, 0], missing valid unpadded convolutions.
  2. Padding Validation: Fusion must reject non-zero padding, unsupported auto_pad values, and attribute references.
  3. Weight Rank: Hardcoded reduction axes only supported 2D convolutions, failing for 1D and higher-dimensional convolutions.
  4. Dtype Promotion: Mixed-precision scalar operations could unintentionally promote fused weights and biases to DOUBLE, violating ONNX Conv dtype constraints.

Solution

  • Relaxed Conv Pattern: Removed the hardcoded pads requirement to support omitted default padding.
  • Padding Validation: Added checks for zero-valued or omitted pads, auto_pad values of NOTSET or VALID, and safe rejection of attribute references.
  • Rank & Channel Validation: Validate weight rank (at least 3), one-dimensional bias, and matching output channel counts.
  • Flexible Spatial Reduction: Replaced hardcoded axes with tuple(range(1, w_value.ndim)).
  • Dtype Preservation: Explicitly preserve the original weight and bias dtypes using .astype().
  • Regression Tests: Added coverage for omitted pads, auto_pad configurations, reference attribute rejection, and mixed-precision dtype preservation, including ONNX checker validation.

…eFusionBase

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…nvFusion

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…conv affine bias

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
… pads attribute

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…ejection in AffineConvFusion

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 14, 2026 22:29

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Hardik K (hardikkaurani) commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi Ti-Tai Wang (@titaiwangms),

I've opened this PR to improve AffineConvFusion and ConvAffineFusion in _fuse_conv_affine.py:

  • Omitted default pads matching: In ONNX, Conv.pads defaults to 0 along all spatial dimensions when omitted. Removed hardcoded pads=[0, 0, 0, 0] from AffineConvFusion.pattern so standard unpadded convolutions are correctly matched.
  • Padding & auto_pad attribute validation: Added validation in check() ensuring that if pads is present it contains only zeros, that auto_pad is either "NOTSET" or "VALID", and that attribute references are safely rejected.
  • Flexible spatial dimensions: Updated AffineConvFusion.rewrite to reduce affine bias across tuple(range(1, w_value.ndim)) instead of hardcoding axis=(1, 2, 3), supporting arbitrary spatial ranks.
  • Dtype preservation: Preserved original w_value.dtype and b_value.dtype via .astype() to prevent mixed-precision scalars (e.g. float64) from promoting weights/bias to DOUBLE and violating ONNX Conv constraints.
  • Tests: Added regression tests for omitted pads, auto_pad="VALID" / "SAME_UPPER", reference attribute rejection, and mixed-precision dtype preservation verified with full ONNX checker validation.

All 12 dedicated tests and all 392 common rewriter tests pass locally. Whenever you have time, I'd appreciate your review and feedback. Thank you!

…ence attribute rejection in Conv affine fusion

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>

Copilot AI left a comment

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.

🟡 Changes recommended

The fusion matcher reads weight and bias data before checking eligibility, causing an uncaught FileNotFoundError for unavailable external weights instead of safely rejecting the match.

1 open issue

Review effort: Balanced

…oading tensor data

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…ine fusion

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Ti-Tai Wang (@titaiwangms),

I've pushed updates addressing the Copilot review feedback:

  • Deferred tensor data reads in fusion check: In _fuse_conv_affine.py, _ConvAffineFusionBase.check now validates scale and offset first, and checks Conv weight and bias rank/channel constraints using tensor shape metadata (w_val.shape, b_val.shape) instead of calling .numpy(). Weight and bias array materialization is deferred entirely to rewrite().
  • Early rejection on padding: In AffineConvFusion.check, pads and auto_pad attributes are checked upfront before delegating to _ConvAffineFusionBase.check, ensuring padded convolutions reject early without inspecting operands.
  • Regression test coverage: Added unit tests in _fuse_conv_affine_extended_test.py verifying that both ConvAffineFusion (with non-constant scale) and AffineConvFusion (with padding) safely reject matches with external weights whose data file is unavailable, without raising FileNotFoundError.

All 14 dedicated tests in _fuse_conv_affine*_test.py and all 394 common rewriter rules tests pass cleanly. Please let me know if any further changes are desired. Thank you!

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Thanks for the review and approval, Ti-Tai Wang (@titaiwangms)! All tests and checks are passing cleanly. Ready to be merged whenever convenient.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

3 participants