You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ab3100f
Browse filesBrowse the repository at this point in the historyBrowse files
[ET-VK][conv2d] Fuse residual adds and activations into conv2d during 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/)
0 commit comments