Skip to content

NXP backend: Condition Linear+BatchNorm fusing passes to valid combination only#17736

Open
StrycekSimon wants to merge 2 commits intopytorch:mainfrom
nxp-upstream:bugfix/EIEX-729-condition-linear-bn-fusing-passes-to-valid-combination-only
Open

NXP backend: Condition Linear+BatchNorm fusing passes to valid combination only#17736
StrycekSimon wants to merge 2 commits intopytorch:mainfrom
nxp-upstream:bugfix/EIEX-729-condition-linear-bn-fusing-passes-to-valid-combination-only

Conversation

@StrycekSimon
Copy link
Collaborator

@StrycekSimon StrycekSimon commented Feb 26, 2026

Summary

Introduce shape conditioning to Linear+BN fusion related passes.
Fix RemoveSimulatedLinearBatchNormFusionQATPass pass recompiling graph when not necessary.

Problem description:
Linear and BatchNorm layers are fusable only in cases where both layers work with the same dimension. Linear layer works strictly with only the last dimension of it's input. BatchNorm on the other hand works in channels dimension (the second one in most cases) except for one case when using BatchNorm1d and input shape [N, C] (see official PyTorch documentation for more info). In this one case both BatchNorm and Linear works on the same dimension and are correctly fusable.

Example:

inp = torch.randn((2, 2, 2)) # (N, C, L)
lin = nn.Linear(in_features=3, out_features=5) # Weight of shape (5, 3)
bn = nn.BatchNorm2d(num_features=2) # Running var shape (2, )

# With the following fusing equation:
# linear_w_fused = linear_w * (gamma / sqrt(var + eps))
# And the following shapes:
# linear_w.shape: (5, 3); var.shape: (2, )
# We cannot correctly perform the multiplication.

Test plan

New unit test cases were added + relevant older ones were adjusted.

cc @robert-kalmar @JakeStevens @digantdesai

Copilot AI review requested due to automatic review settings February 26, 2026 14:11
@pytorch-bot
Copy link

pytorch-bot bot commented Feb 26, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/17736

Note: Links to docs will display an error until the docs builds have been completed.

❌ 8 New Failures

As of commit da6cae3 with merge base f78535d (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 26, 2026
@StrycekSimon StrycekSimon added module: nxp Issues related to NXP Neutron NPU delegation and code under backends/nxp/ release notes: nxp Changes to the NXP Neutron backend delegate labels Feb 26, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR tightens the NXP backend’s Linear+BatchNorm fusion logic by conditioning fusion passes on valid shape combinations (only fuse when Linear and BN operate on the same dimension), and avoids unnecessary graph recompilation in the QAT “remove simulated fusion” pass.

Changes:

  • Add shape-based gating (via FX tensor_meta shape) to Linear+BN fusion passes to prevent invalid fusions.
  • Track whether graph edits were made in RemoveSimulatedLinearBatchNormFusionQATPass and only recompile when needed.
  • Update/refactor NXP backend tests and test models; add new negative tests ensuring incompatible Linear+BN are not fused.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
backends/nxp/aten_passes/fuse_batch_norm_with_linear_pass.py Adds output-shape conditioning before fusing Linear+BN.
backends/nxp/aten_passes/simulated_linear_bn_fusion_passes/add_simulated_linear_bn_fusion_qat_pass.py Adds output-shape conditioning to simulated Linear+BN QAT fusion insertion.
backends/nxp/aten_passes/simulated_linear_bn_fusion_passes/remove_simulated_linear_bn_fusion_qat_pass.py Returns/propagates a “made changes” flag to avoid unnecessary recompile.
backends/nxp/backend/graph_utils.py Adds get_output_shape helper for retrieving tensor_meta shapes.
backends/nxp/tests/ir/edge_passes/test_linear_bn_fusing.py Updates model usage and adds negative tests for non-fusable shapes.
backends/nxp/tests/models.py Refactors BN-related helper modules used by tests (but currently introduces a problematic import / rename compatibility issue).
backends/nxp/tests/test_batch_norm_fusion.py Updates fusion tests to use shared helpers and adds incompatible-shape full-pipeline coverage.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@StrycekSimon StrycekSimon force-pushed the bugfix/EIEX-729-condition-linear-bn-fusing-passes-to-valid-combination-only branch from 9a50827 to da6cae3 Compare February 26, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: nxp Issues related to NXP Neutron NPU delegation and code under backends/nxp/ release notes: nxp Changes to the NXP Neutron backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants