Skip to content

fix(nxp): check the conv/linear node's user count in BatchNorm fusion#20601

Open
durvesh1992 wants to merge 2 commits into
pytorch:mainfrom
durvesh1992:fix/nxp-fuse-bn-single-user-check
Open

fix(nxp): check the conv/linear node's user count in BatchNorm fusion#20601
durvesh1992 wants to merge 2 commits into
pytorch:mainfrom
durvesh1992:fix/nxp-fuse-bn-single-user-check

Conversation

@durvesh1992

@durvesh1992 durvesh1992 commented Jun 29, 2026

Copy link
Copy Markdown

Summary

The _is_conv / _is_linear closures in the NXP BatchNorm fusion passes check len(node.users) instead of len(node_.users).

node_ is the closure's parameter (the conv/linear node being tested), but node resolves to the enclosing for node in graph_module.graph.nodes loop variable — which at the call site (_is_conv(bn_node.args[0])) is always the BatchNorm node.

The single-user guard exists to prevent fusion when the conv/linear output feeds consumers other than the BatchNorm, because folding the BatchNorm into the conv/linear weights changes that output for all consumers. Due to the typo the guard inspected the BatchNorm's user count rather than the conv/linear's, so a conv/linear with multiple consumers was still fused — corrupting the other consumers' inputs.

Affected files:

  • backends/nxp/aten_passes/fuse_batch_norm_with_conv_pass.py
  • backends/nxp/aten_passes/fuse_batch_norm_with_linear_pass.py

Test plan

Reproduced on a small torch.fx graph where the conv has two consumers (BatchNorm + a relu), so fusion must be skipped:

  • Before: the pass fuses anyway → model output max-abs-diff vs. reference ≈ 76.2 (corrupted).
  • After: the pass is correctly a no-op → diff 0.0.
  • After, single-consumer conv: still fuses correctly, numerics preserved (diff ≈ 3.8e-6).

The linear pass contains the identical typo and the identical one-character fix.

cc @robert-kalmar @JakeStevens @digantdesai @rascani

The _is_conv / _is_linear closures in the NXP BatchNorm fusion passes
checked len(node.users) instead of len(node_.users). node_ is the
closure parameter (the conv/linear node being tested), but node
resolves to the enclosing 'for node in graph_module.graph.nodes' loop
variable, which at the call site is always the BatchNorm node.

The single-user guard is meant to prevent fusion when the conv/linear
output feeds consumers other than the BatchNorm, since folding the
BatchNorm into the conv/linear weights changes that output for all of
them. Because of the typo the guard inspected the BatchNorm's user
count, so a conv/linear with multiple consumers was still fused,
corrupting the other consumers' inputs.
@pytorch-bot

pytorch-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 Cancelled Job

As of commit 4537786 with merge base b331ebd (image):

CANCELLED JOB - The following job was cancelled. Please retry:

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 Jun 29, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 29, 2026

Copy link
Copy Markdown

CLA Not Signed

@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@robert-kalmar

Copy link
Copy Markdown
Collaborator

CC @MartinPavella @roman-janik-nxp , @novak-vaclav

@durvesh1992 , do you mind to add a corresponding unit test as you described in the Test plan?

@robert-kalmar robert-kalmar added the module: nxp Issues related to NXP Neutron NPU delegation and code under backends/nxp/ label Jun 30, 2026
@MartinPavella MartinPavella self-requested a review June 30, 2026 08:16
@novak-vaclav novak-vaclav self-requested a review June 30, 2026 16:07
Cover both passes: a conv/linear with a single user has its BatchNorm
fused away, while a conv/linear whose output feeds an additional
consumer keeps its BatchNorm (fusion is skipped) and the numerical
result is preserved. The multi-user cases fail without the
node_.users fix in this PR.
@durvesh1992

Copy link
Copy Markdown
Author

Thanks for the review @robert-kalmar! I've added unit tests in backends/nxp/tests/test_fuse_batch_norm_single_user.py covering both passes: a conv/linear with a single user has its BatchNorm fused away, and a conv/linear whose output feeds an additional consumer keeps its BatchNorm (fusion skipped) with the numerical result preserved. The multi-user cases fail without the node_.users fix in this PR.

@durvesh1992

Copy link
Copy Markdown
Author

Heads up on the red EasyCLA check: the Meta CLA check passes, and I'm actively sorting out the Linux Foundation EasyCLA corporate authorization on my end (getting allowlisted under the company CCLA). The code and tests are ready for review in the meantime — I'll get the EasyCLA green as soon as the corporate approval lands. This same authorization also covers my other two ExecuTorch PRs (#20603, #20608).

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/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants