Skip to content

[ET-VK][bf16] Generate bfloat16 shader variants - #25228

Open
SS-JIA wants to merge 2 commits into
gh/SS-JIA/731/basefrom
gh/SS-JIA/731/head
Open

SS-JIA wants to merge 2 commits into
gh/SS-JIA/731/basefrom
gh/SS-JIA/731/head

Conversation

@SS-JIA

@SS-JIA SS-JIA commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Ulterior Motive

Running bfloat16 models, such as Muse Glimmer, natively through ET-VK. This stack adds the bfloat16 dtype and the elementwise operators that exercise it. This commit teaches shader codegen about bfloat16.

Rationale

What: Shader YAML can set DTYPE: bfloat16. The generated shader stores such tensors as bfloat16_t / bf16vec4 (GL_EXT_bfloat16), computes in fp32, and rounds every conversion to bfloat16 to nearest even.

Why: There is no bfloat16 image format and GLSL allows no arithmetic on bfloat16, so bfloat16 needs its own type mapping. Some drivers truncate fp32 to bfloat16 conversions; NVIDIA driver 580 does so even when the shader requests the SPIR-V RoundingModeRTE execution mode.

Details

  • Buffer types are bfloat16_t / bf16vecN; texel and compute types are float / vec4. GLSL forbids implicit conversion to bfloat16, so a shader that forgets to widen fails to compile.
  • For bfloat16, the extension helpers emit GL_EXT_bfloat16 and round_to_bf16. Macros route the single-argument bfloat16_t and bf16vec4 constructors through round_to_bf16, which rounds the fp32 bit pattern to nearest even and keeps NaNs quiet, so a NaN payload in the dropped bits cannot truncate to infinity. This mirrors the existing rounding of rgba16f image stores. Other conversions, such as to bf16vec2, are not rounded; no shader uses them.
  • Variants that pair DTYPE: bfloat16 with texture storage in any *STORAGE parameter are skipped, so a shader with several storage parameters, such as STORAGE and WEIGHT_STORAGE, keeps only all-buffer bfloat16 variants.
  • get_higher_precision_dtype ranks bfloat16 with half. Neither represents the other, so a half and bfloat16 pair gives float.
  • No shader uses bfloat16 yet, so the generated shaders are unchanged.

Authored with Claude Code.

Differential Revision: D132361766

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 c9a1733 with merge base ba0d826 (image):

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.

@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.

[ghstack-poisoned]

This branch was previously deployed

1 inactive deployment
cadence — c9a1733d Deployed Nov 20, 2026 by SS-JIA via hifi-op-test / hifi4 #39684
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. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant