Repository navigation
Fold batch norms into aten.convolution in prepare_pt2e (#5102) - #5102
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/5102
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit ff814ee with merge base 4ab9d61 ( BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@mcremon-meta has exported this pull request. If you are a Meta employee, you can view the originating Diff in D131429944. |
|
@claude review and also explain |
|
Claude finished @vkuzo's task in 1m 26s —— View job PR #5102 review: fold batch norms into
|
|
when do you run decomposition before prepare_pt2e? we expect quantizer to use non decomposed aten IR I think: https://docs.pytorch.org/ao/stable/pt2e_quantization/pt2e_quantizer.html#a-note-on-ir-for-pt2e-quantization-flow |
we run decompositions at the export stage in a few of our flows, and keep some operators from being decomposed. That's very useful for the quantizer, it allows to cap the number of ops we can see to core aten + whatever we decide to retain |
|
OK, @claude is it better to implement this in pt2e core flow or backend specific quantizer through ? |
|
Claude finished @jerryzh168's task in 46s —— View job Core pt2e flow or
|
| Core (this PR) | transform_for_annotation |
|
|---|---|---|
| Duplicated code | none | each decomposing backend copies the fold or calls private helpers |
| IR contract | slightly widens what core accepts | unchanged |
| QAT parity | PTQ only, QAT still misses it | the backend does whatever it needs |
| Risk to existing users | ~none (only adds a match) | none |
Core is the better place, for two reasons: the fold is backend-agnostic, and core already does it. Decomposing before prepare_pt2e is a real pattern in ExecuTorch flows, as @mcremon-meta described. If this lands in core, I'd suggest two follow-ups:
- Add a short note to the docstring or the IR docs saying that
aten.convolutionis accepted by the BN fold only, so nobody reads it as full core-ATen support. - Either open an issue for the QAT gap (
_fuse_conv_bn_qat) or state in the PR that only PTQ is covered.
If maintainers want to keep core strictly on pre-dispatch ATen, the fallback is to move this into the decomposing backend's transform_for_annotation. Ideally _fuse_conv_bn_ would also gain a public way to pass in a conv predicate, so backends don't have to copy the matching code.
|
@jerryzh168 yes I opened this PR trying to avoid putting it in |
|
@mcremon-meta running conv bn folding by default is not what we want in core I think, but we didn't give a lot of thought to this at the time and there doesn't seem to be a strong enough motivation to separate it out. I also have seen people complaining about it but don't remember what is the resolution for that. But given the PR seems low risk I think it's fine to land. it's easy to change in the future as well if we want a different design. For QAT I think it does not support decomposed ops anyways, so it is better to not support convolution, maybe we should make sure there is a check for that in fold conv bn for QAT and error out is convolution op is detected. cc @claude can you double check that? |
|
Claude finished @jerryzh168's task in 1m 16s —— View job Does QAT conv-BN fusion reject
|
|
|
||
|
|
||
| def _is_bn_foldable_conv_node(n: Node) -> bool: | ||
| """ |
There was a problem hiding this comment.
maybe add a comment to explain why we don't extend _is_conv_node is because _is_conv_or_conv_transpose_node is used by qat path
There was a problem hiding this comment.
also current naming is not very descriptive of the situation I feel, maybe:
_is_aten_or_core_aten_bn_foldable_conv_node
There was a problem hiding this comment.
core aten is aten, right? I'm not against doing that, but it feels like it's a bit much. Up to you!
There was a problem hiding this comment.
core aten IR and aten IR are two different IRs I think, only aten IR support training I believe
There was a problem hiding this comment.
I see, in the end I don't have a preference on the name. Whatever will get this landed is fine with me :)
Summary: `prepare_pt2e` folds a batch norm into the conv before it, but only recognizes `conv1d`, `conv2d`, `conv3d` and `conv_transpose*`. A model decomposed to core ATen before `prepare_pt2e` has `aten.convolution` there instead, which they decompose to, so its batch norms are left unfolded. `_get_conv_bn_fusion_nodes` now also accepts a non-transposed `aten.convolution`, directly or through the same contiguous/clone and non-channel slice it already handles. A transposed one is left alone. The change is limited to the fold. `_is_conv_node` is unchanged: quantizers use it to pick the convs they annotate (e.g. XNNPACK's `_do_annotate_conv`), and matching `aten.convolution` there would include transposed ones. Reviewed By: ethansfng Differential Revision: D131429944
dc90803 to
e22e841
Compare
| ) | ||
|
|
||
| # The patterns below can't match a decomposed conv and would leave its BN unfused. | ||
| functional_bn = torch.ops.aten._native_batch_norm_legit_functional.default |
There was a problem hiding this comment.
just to confirm, is this only the bn op in core aten IR (after run_decomposition)? cc @claude please confirm
| ): | ||
| for user in conv_node.users: | ||
| if _is_bn_node(user) or user.target == functional_bn: | ||
| raise ValueError( |
There was a problem hiding this comment.
I think warning is enough
Summary: `prepare_pt2e` folds a batch norm into the conv before it, but only recognizes `conv1d`, `conv2d`, `conv3d` and `conv_transpose*`. A model decomposed to core ATen before `prepare_pt2e` has `aten.convolution` there instead, which they decompose to, so its batch norms are left unfolded. `_get_conv_bn_fusion_nodes` now uses `_is_aten_or_core_aten_bn_foldable_conv_node`, which also accepts a non-transposed `aten.convolution`, directly or through the same contiguous/clone and non-channel slice it already handles. A transposed one is left alone: `fold_bn_weights_into_conv_node` tells a transposed conv by its op, so it would scale its weight along the wrong dim. The change is limited to the PTQ fold. `_is_conv_node` is unchanged: quantizers use it to pick the convs they annotate (e.g. XNNPACK's `_do_annotate_conv`), and the QAT conv-bn fusion uses it too, which doesn't support core ATen. The QAT fusion matches patterns traced from `F.conv*`, so it never matched a decomposed conv, and `prepare_qat_pt2e` silently left its batch norm unfused. `_fuse_conv_bn_qat` now warns when an `aten.convolution` feeds a batch norm. After `run_decompositions()` a batch norm is `_native_batch_norm_legit_functional` in train mode, `_native_batch_norm_legit_no_training` in eval mode and `_native_batch_norm_legit.no_stats` with `track_running_stats=False`, and the warning covers all three. A decomposed conv without a batch norm doesn't warn. Reviewed By: ethansfng Differential Revision: D131429944
e22e841 to
2ce3761
Compare
Summary: `prepare_pt2e` folds a batch norm into the conv before it, but only recognizes `conv1d`, `conv2d`, `conv3d` and `conv_transpose*`. A model decomposed to core ATen before `prepare_pt2e` has `aten.convolution` there instead, which they decompose to, so its batch norms are left unfolded. `_get_conv_bn_fusion_nodes` now uses `_is_aten_or_core_aten_bn_foldable_conv_node`, which also accepts a non-transposed `aten.convolution`, directly or through the same contiguous/clone and non-channel slice it already handles. A transposed one is left alone: `fold_bn_weights_into_conv_node` tells a transposed conv by its op, so it would scale its weight along the wrong dim. The change is limited to the PTQ fold. `_is_conv_node` is unchanged: quantizers use it to pick the convs they annotate (e.g. XNNPACK's `_do_annotate_conv`), and the QAT conv-bn fusion uses it too, which doesn't support core ATen. The QAT fusion matches patterns traced from `F.conv*`, so it never matched a decomposed conv, and `prepare_qat_pt2e` silently left its batch norm unfused. `_fuse_conv_bn_qat` now warns when an `aten.convolution` feeds a batch norm. After `run_decompositions()` a batch norm is `_native_batch_norm_legit_functional` in train mode, `_native_batch_norm_legit_no_training` in eval mode and `_native_batch_norm_legit.no_stats` with `track_running_stats=False`, and the warning covers all three. A decomposed conv without a batch norm doesn't warn. Reviewed By: ethansfng Differential Revision: D131429944
2ce3761 to
ff814ee
Compare
|
@jerryzh168 just pushed a new version, let me know if it looks good to you! |
Summary: Advance `TORCHAO_NIGHTLY_VERSION` and `CU134_TORCHAO_NIGHTLY_VERSION` from `0.19.0.dev20261103` to `0.19.0.dev20261118` and align `third-party/ao` with the nightly's source commit, `3f5febc5c6c0cd87e606c26fd41dade1a963d967`. This includes pytorch/ao#5102, allowing `prepare_pt2e` to fold batch norms into `aten.convolution` after decomposition to core ATen. The existing `setup.py` dependency bound derives from these constants and advances automatically to `torchao>=0.19.0.dev20261118,<0.20`. The selected nightly is published for CPU, CUDA 13.2 and CUDA 13.4. CUDA 13.0's newest published nightly remains `0.19.0.dev20261103`, so fresh CUDA 13.0 installs cannot resolve this new pin from that channel. This limitation needs resolution before landing. This change was authored with Claude Code. Differential Revision: D132976839 Pull Request resolved: #25223
#25223 (164233d) advanced `TORCHAO_NIGHTLY_VERSION` to `0.19.0.dev20261118`, but the newest TorchAO nightly on the `cu130` channel is `0.19.0.dev20261103`. Every CUDA 13.0 job has failed to install since then: ``` ERROR: No matching distribution found for torchao==0.19.0.dev20261118 ``` (CUDA 13.4 jobs, e.g. `test-executorch-cuda-build-13.4`, still pass.) This adds `CU130_TORCHAO_NIGHTLY_VERSION = "0.19.0.dev20261103"` and returns it from `get_torchao_version` for the `/cu130` URL, unless torchao is built from source. `setup.py` gets the torchao bound from `get_torchao_version`, so the wheel metadata changes with it. CPU, CUDA 13.2 and CUDA 13.4 stay on `dev20261118`. `update_pytorch_pin.py` already matches `CU\d+_TORCHAO_NIGHTLY_VERSION`, so later pin bumps update the new constant too. Note: CUDA 13.0 won't get pytorch/ao#5102 (batch norm folding into `aten.convolution`) until the cu130 channel publishes again. ## Test plan `python -m pytest .ci/scripts/tests/test_cu134_dependencies.py .ci/scripts/tests/test_cuda_workflow.py` passed (23 tests). Both tests now cover CUDA 13.0 explicitly. CUDA CI on this PR is the real check.
Summary:
prepare_pt2efolds a batch norm into the conv before it, but only recognizesconv1d,conv2d,conv3dandconv_transpose*. A model decomposed to coreATen before
prepare_pt2ehasaten.convolutionthere instead, which theydecompose to, so its batch norms are left unfolded.
_get_conv_bn_fusion_nodesnow uses_is_aten_or_core_aten_bn_foldable_conv_node,which also accepts a non-transposed
aten.convolution, directly or through thesame contiguous/clone and non-channel slice it already handles. A transposed one
is left alone:
fold_bn_weights_into_conv_nodetells a transposed conv by itsop, so it would scale its weight along the wrong dim.
The change is limited to the PTQ fold.
_is_conv_nodeis unchanged: quantizersuse it to pick the convs they annotate (e.g. XNNPACK's
_do_annotate_conv),and the QAT conv-bn fusion uses it too, which doesn't support core ATen.
The QAT fusion matches patterns traced from
F.conv*, so it never matched adecomposed conv, and
prepare_qat_pt2esilently left its batch norm unfused._fuse_conv_bn_qatnow warns when anaten.convolutionfeeds a batch norm.After
run_decompositions()a batch norm is_native_batch_norm_legit_functionalin train mode,
_native_batch_norm_legit_no_trainingin eval mode and_native_batch_norm_legit.no_statswithtrack_running_stats=False, and thewarning covers all three. A decomposed conv without a batch norm doesn't warn.
Reviewed By: ethansfng
Differential Revision: D131429944