Skip to content

[FIX][Relax] Guard BatchNorm folding contracts - #20737

Open
Yuhx141 wants to merge 3 commits into
apache:mainfrom
Yuhx141:yuhx/fix-relax-batchnorm-contracts-20261015
Open

Yuhx141 wants to merge 3 commits into
apache:mainfrom
Yuhx141:yuhx/fix-relax-batchnorm-contracts-20261015

Conversation

@Yuhx141

@Yuhx141 Yuhx141 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

This keeps Conv2D/BatchNorm folding within the contracts supported by its channel-scaling formula:

  • leave BatchNorm with disabled scale or center unchanged;
  • require the normalized BatchNorm axis to be the NCHW output-channel axis;
  • leave mixed convolution weight/output dtypes unchanged.

Unsupported cases retain the original graph. Existing supported channel-axis folds, including a negative channel axis, still run.

Closes #20530
Closes #20531
Closes #20655

Tests:

  • All 11 cases in tests/python/relax/test_transform_fold_batch_norm_to_conv2d.py passed with the current Python sources and an existing LLVM build.
  • All seven unsupported cases fail against the unmodified main implementation, including the NHWC case where height equals output channels. The four supported cases pass on both versions.
  • Ruff 0.12.3 and git diff --check passed.

@Yuhx141

Yuhx141 commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Could someone take a look at the BatchNorm folding guards? The tests check that unsupported cases keep the original graph and that channel-axis folds still work. Jenkins is green.

@Yuhx141

Yuhx141 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Hi @javierdejesusda, could you take a look at the BatchNorm folding guards when you get a chance? The tests cover both the supported channel-axis folds and the cases that should keep the original graph.

@javierdejesusda javierdejesusda 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.

Thanks for the ping. The guards look correct, but the new tests fail on current main:

  • check_well_formed now returns None (#20793), so drop the assert.
  • tvm.ir is gone, so use tvm_tirx.ir.assert_structural_equal.

With those two fixes, all 10 tests pass on main, and the 6 unsupported cases fail without the fix.

Could you add an NHWC case? With an OIHW kernel, batch_norm(axis=3) and H equal to C, main silently folds it into wrong output because the bias broadcasts over H. This PR correctly leaves it alone, but no test covers it.

@Yuhx141

Yuhx141 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for checking this, Javier. I updated both test APIs and added the NHWC/OIHW case with height equal to output channels. It fails without the guards, and all 11 tests pass with them. I pushed the update.

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

None yet

2 participants