Repository navigation
[ET-VK][conv2d] Fuse residual adds and activations into conv2d during lowering - #24823
Merged
Merged
Conversation
… lowering Pull Request resolved: #24808 ## Ulterior Motive Closing the DSR FP16 gap to LiteRT. DSR has 83 skip connections of the form `conv(...) + x * scale`, each lowered today to a separate `mul` and `add` dispatch. The previous commit added a conv2d epilogue that applies them inside every conv2d shader; this commit makes lowering use it. It also makes `et_vk.conv2d.with_epilogue` the single fused op for conv2d, replacing `et_vk.conv_with_clamp` there. ## Rationale **What**: A `conv2d_with_epilogue` pattern for `FusePatternsPass` that rewrites either of ``` activation(residual_op(convolution(...), residual [* scale])) activation(convolution(...)) ``` into `et_vk.conv2d.with_epilogue`, plus the op's definition and `op_registry` entry. A new `skip_fusion_patterns` compile option lets `WebGPUPartitioner` skip the pattern. **Why**: Without the pattern, no exported model reaches the epilogue. On DSR, an earlier revision of this stack shrank the Vulkan delegate from 469 to 229 operators and cut end-to-end latency 9.6% on Galaxy S23 and Pixel 8 Pro. The WebGPU runtime consumes Vulkan lowering but does not implement `et_vk.conv2d.with_epilogue`. ## Details - **Behavior** - conv2d + activation - **Before**: `FuseClampPass` rewrote it into `et_vk.conv_with_clamp`. - **After**: `et_vk.conv2d.with_epilogue`. At runtime both reach `add_conv2d` with an activation epilogue. - conv2d + residual, with or without an activation - **Before**: separate `aten.convolution`, `mul`, `add` and activation ops. - **After**: one `et_vk.conv2d.with_epilogue`. - conv1d + activation: unchanged. `Conv1dAsConv2dPass` and `FuseClampPass` still run after `FusePatternsPass`, so conv1d, including conv1d rewritten as conv2d, fuses into `et_vk.conv_with_clamp`, which stays registered for conv1d, WebGPU and existing `.pte` files. - WebGPU lowering: unchanged. `WebGPUPartitioner` skips `conv2d_with_epilogue`, so conv2d + activation still becomes `et_vk.conv_with_clamp`. - **Match** - A 2D, batch 1, FP16/FP32 `aten.convolution` (direct, pointwise, depthwise, grouped or transposed) with one user, whose input and output fit channels-packed textures. - Feeding `aten.add.Tensor`, `aten.sub.Tensor` or `aten.mul.Tensor` with a residual of the conv output's shape and dtype. The conv may be either operand of `mul`, and either operand of `add` when `alpha` is 1; it must be the first operand of `sub`. - When the residual is a `mul` of `x` and a `[1, C, 1, 1]` or `[C, 1, 1]` placeholder `scale`, and the residual op is the `mul`'s only user, `x` becomes the residual and `scale` the residual scale. The runtime binds the residual as a tensor and prepacks the scale, so when `scale` is not a constant or `x` is one, the `mul` stays separate and its output is the residual. - The residual cannot be the conv itself (`y + y` stays unfused), and a constant residual without a scale stays unfused. - `relu`, `hardtanh`, `clamp` or `leaky_relu` is fused as the activation when it is the only user of the binary op, or of the conv when there is no residual. A missing `hardtanh` bound defaults to -1 or 1. Activations with non-constant parameters, such as a `clamp` bound by a symbolic size, stay unfused. - **Partitioning**: As for the other patterns, `VulkanPartitioner` delegates a match's nodes without checking their support. The detector therefore repeats the conv's own checks (rank, batch, dtype, texture limits), so a conv that exceeds the texture limits stays on CPU unfused. - **Pattern replacement** - `replace_all_fusable_subgraphs` removes dead code after the detectors replace a match at a node, so nodes another pattern already replaced (for example the conv and relu of a quantized conv) cannot match again. - A replacement function may return `False` when it leaves the graph unchanged, as this pattern does for a constant residual. Such matches are not counted, so `FusePatternsPass` does not retrace the graph for them. - **Registry**: The residual is channels-packed texture3d like the input and output; weight, bias and scale are prepacked. - **`skip_fusion_patterns`**: a collection of pattern names; a string or an unknown name raises `ValueError`. It is serialized as a compile spec, and both the partitioner's pattern detection and `FusePatternsPass` honor it: ```python VulkanPartitioner(compile_options={"skip_fusion_patterns": {"conv2d_with_epilogue"}}) ``` - **Compatibility**: `.pte` files exported after this commit contain `et_vk.conv2d.with_epilogue` for every FP conv2d followed by an activation or a residual op, so they need a runtime that includes the previous commit. Existing `.pte` files keep loading, since `et_vk.conv_with_clamp` stays registered. - **WebGPU tests**: The op test generator and `test_conv_with_clamp.py` lowered through `VulkanPartitioner` directly; they now use `WebGPUPartitioner`. ``` schema: conv2d.with_epilogue(Tensor input, Tensor weight, Tensor? bias, SymInt[] stride, SymInt[] padding, SymInt[] dilation, bool transposed, SymInt[] output_padding, SymInt groups, Tensor? residual, Tensor? residual_scale, str residual_op, Scalar alpha, str activation, float activation_a, float activation_b) -> Tensor ``` ghstack-source-id: 469036207 Differential Revision: [D130216290](https://our.internmc.facebook.com/intern/diff/D130216290/)
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/24823
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 19 PendingAs of commit 2fbdf6b with merge base e5ffebe ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
SS-JIA
approved these changes
Nov 10, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR was created by the merge bot to help merge the original PR into the main branch.
ghstack PR number: #24808 by @SS-JIA
^ Please use this as the source of truth for the PR details, comments, and reviews
ghstack PR base: https://github.com/pytorch/executorch/tree/gh/SS-JIA/699/base
ghstack PR head: https://github.com/pytorch/executorch/tree/gh/SS-JIA/699/head
Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/SS-JIA/698/orig
Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/SS-JIA/699/orig
Differential Revision: D130216290
@diff-train-skip-merge