Skip to content

[ET-VK][conv2d] Use * and + instead of fma() on f16 vectors - #24939

Merged
SS-JIA merged 1 commit into
gh/SS-JIA/706/origfrom
gh/SS-JIA/707/orig
Nov 12, 2026
Merged

SS-JIA merged 1 commit into
gh/SS-JIA/706/origfrom
gh/SS-JIA/707/orig

Conversation

@pytorchbot

Copy link
Copy Markdown
Collaborator

This PR was created by the merge bot to help merge the original PR into the main branch.
ghstack PR number: #24856 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/707/base
ghstack PR head: https://github.com/pytorch/executorch/tree/gh/SS-JIA/707/head
Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/SS-JIA/706/orig
Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/SS-JIA/707/orig
Differential Revision: D130986954
@diff-train-skip-merge

## Ulterior Motive

Optimizing the FP16 1x1 convolutions of the DSR model on mobile GPUs.

## Rationale

**What**: A `mul_add(a, b, c)` helper in `common.glslh` computes `a * b + c`. In shaders that define `HAS_FLOAT16_ACCUMULATION`, it uses `*` and `+` for `f16vec4` and `fma()` for `vec4`; in other shaders it is `fma()`. The GEMM tile helper and `conv2d_tiled` call it instead of `fma()`, and the conv2d shaders that accumulate in f16 vectors define the marker: `conv2d_pw_tiled`, `conv2d_tiled` and the buffer input `conv2d_gemm`.

**Why**: Adreno 740 and 750 run `fma()` on f16 vectors at about half the speed of a multiply and an add. With this commit, Half pointwise convolutions take 0.37 to 0.49 of the time on a Galaxy S23 (Adreno 740) and a Galaxy S24 (Adreno 750). Replacing `fma()` in every shader regardless of type made Float shaders up to 1.35 times as slow on the same phones, so the form depends on the type.

## Details

- **Before**: Multiply-accumulates call `fma()` for both dtypes.
- **After**: In the Half variants of `conv2d_pw_tiled`, `conv2d_tiled` and the buffer input `conv2d_gemm`, multiply-accumulates on f16 vectors compute `c + a * b`. Every other shader still calls `fma()`, including the other users of the GEMM tile helper (linear, matmul, Q4 and Q8 linear, fused SDPA).

`f16vec4` is a type only when `GL_EXT_shader_explicit_arithmetic_types_float16` is enabled, so a shader opts in by defining `HAS_FLOAT16_ACCUMULATION` before it includes `common.glslh`; without it, `mul_add` is a macro for `fma()`. Two alternatives do not work:

- Enabling the extension in `common.glslh` for every shader changes implicit conversions in shaders that do not use f16 arithmetic (`max_pool2d`'s `mix()` call becomes ambiguous).
- The extension's own macro cannot be the guard: glslang defines `GL_EXT_shader_explicit_arithmetic_types_float16` whether or not the extension is enabled.

Other shaders that accumulate in f16 vectors can opt in once their accuracy and performance are measured.

`c + a * b` rounds the product before the add, which `fma()` does not. On Adreno 740 and 750, the f16 error statistics of some conv2d correctness configurations change; all still pass. Outputs on Adreno 840, Mali-G715 and Xclipse 940 are bit-identical to the parent's.

Only 16 SPIR-V modules change: the Half `conv2d_pw_tiled` (4), `conv2d_tiled` (11) and `conv2d_gemm_buffer_half` modules.

Differential Revision: [D130986954](https://our.internmc.facebook.com/intern/diff/D130986954/)


ghstack-source-id: 469962064
Pull-Request: #24856
@pytorchbot
pytorchbot requested a review from SS-JIA as a code owner November 12, 2026 00:08
@pytorch-bot

pytorch-bot Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 1 New Failure, 14 Pending, 1 Unrelated Failure

As of commit 957a369 with merge base 115655c (image):

NEW FAILURE - The following job has failed:

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.

@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 Nov 12, 2026
@SS-JIA
SS-JIA added this pull request to stack #24958 November 12, 2026 00:37
@SS-JIA
SS-JIA merged commit 8be9456 into main Nov 12, 2026
@SS-JIA
SS-JIA deleted the gh/SS-JIA/707/orig branch November 12, 2026 00:38

This branch was previously deployed

1 inactive deployment
cadence — 957a369a Deployed Nov 12, 2026 by pytorchbot
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants