Skip to content

[xnnpack] Support bf16 delegation for fully-connected - #21409

Open
JakeStevens wants to merge 2 commits into
pytorch:mainfrom
JakeStevens:xnnpack-partition-bf16
Open

[xnnpack] Support bf16 delegation for fully-connected#21409
JakeStevens wants to merge 2 commits into
pytorch:mainfrom
JakeStevens:xnnpack-partition-bf16

Conversation

@JakeStevens

Copy link
Copy Markdown
Contributor

Summary

Adds XNNPACK delegation support for bf16 fully-connected, including bf16 dynamic-quant (8da4w). This lets bf16 models (e.g. google/gemma-3-1b-it exported with --dtype bfloat16) lower their linear layers to XNNPACK instead of falling back to portable.

Changes (delegation only):

  • partition/config/xnnpack_config.py — allow torch.bfloat16 as a valid partitioned dtype.
  • operators/node_visitor.py — serialize bf16 tensors as xnn_datatype_bf16.
  • operators/op_linear.py — force the bias to fp32 for bf16 FC (XNNPACK's bf16 FC is bf16_bf16_f32: bf16 activation/weight, fp32 bias).
  • runtime/XNNCompiler.cpp — XNNPACK only provides a bf16_bf16_f32 fully-connected (bf16 in → fp32 out). When the serialized graph asks for a bf16 output (fully-bf16 model), define the FC with an fp32 intermediate output and append an xnn_define_convert (fp32 → bf16) so the delegate boundary stays bf16.

Dependency

Depends on #21400 (vendored XNNPACK bump to 92a7ad5) — merge that first.

The bf16 dynamic-quant path relies on the qd8_bf16_qb4w subgraph fully-connected added in google/XNNPACK#10818, which only exists in the bumped revision. This PR is stacked on #21400, so its diff currently shows the bump commit as well; once #21400 lands I'll rebase and this PR will reduce to just the delegation changes above.

Test plan

Exported google/gemma-3-1b-it via optimum-executorch (XNNPACK recipe, custom SDPA + KV cache):

optimum-cli export executorch --model google/gemma-3-1b-it --task text-generation \
  --recipe xnnpack --use_custom_sdpa --use_custom_kv_cache \
  --qlinear 8da4w --qembedding 8w --dtype bfloat16 --output_dir <out>
  • Lowering succeeds; the FC nodes delegate to XNNPACK as qd8_bf16_qb4w.
  • Runtime forward on the resulting .pte produces finite bfloat16 logits, with argmax matching the fp32 and bf16-no-quant baselines (argmax == 107 on the smoke input).

Bumps backends/xnnpack/third-party/XNNPACK from 1adaa7c to 92a7ad5
(google/XNNPACK master, PR pytorch#10818), adding the qd8_bf16_qb4w
fully-connected path to the subgraph layer. Required for bf16 dynamic
quant (8da4w) to lower and run via XNNPACK.

This XNNPACK revision splits two new source files out of existing
translation units, so the ExecuTorch Buck build (which enumerates
sources explicitly rather than via CMake) must list them or linking
fails with undefined symbols:
- src/subgraph/rewrites/cvt_to_fp32.cc (fp16/bf16 -> fp32 fallback
  rewrite; defines xnn_subgraph_fallback_from_{fp16,bf16}_to_fp32 and
  xnn_subgraph_alias_fp32_fallback_data) -> added to the 'subgraph' target.
- src/xnnpack/init-once.c (defines xnn_init_once_impl /
  xnn_reset_all_init_guards; in CMake it is part of the
  xnnpack-hardware-config object lib) -> added to the 'XNNPACK' target
  next to configs/hardware-config.c.

It also adds new AVX512-FP16 micro-kernels (e.g. f16-vapproxgelu) that
crash clang-cl 18's codegen on Windows. clang-cl is reported to CMake as
Clang (not MSVC), so XNNPACK's own MSVC gate for AVX512FP16 does not
apply; disable XNNPACK_ENABLE_AVX512FP16 on Windows in the existing
WIN32 block alongside the other options that don't build there.
Enable the XNNPACK delegate to partition and run bf16 fully-connected
(aten.linear / aten.addmm) instead of forcing everything through fp32.

XNNPACK's only bf16 fully-connected type is bf16_bf16_f32: bf16 activation
x bf16 weight -> fp32 output, with an fp32 bias. Wire that up:

- partition/config/xnnpack_config.py: allow torch.bfloat16 in the
  partitioner's valid dtypes so bf16 nodes get delegated.
- operators/node_visitor.py: serialize bf16 tensors as xnn_datatype_bf16
  (previously defaulted to fp32 while holding bf16 bytes -> NaN); keep
  bf16 constant weights in bf16; force the FC bias to fp32 in the shared
  input/weight/bias serializer (covers addmm).
- operators/op_linear.py: force the bias to fp32 for bf16 linears.
- runtime/XNNCompiler.cpp: when a fully-connected has bf16 in/weight/out,
  define an fp32 intermediate output and append a convert (fp32 -> bf16)
  so the delegate boundary stays bf16.

Requires a compiler new enough for XNNPACK's AVX512BF16 microkernels
(gcc >= 13 / clang >= 15).

Verified: bf16 linear/addmm delegate to XNNPACK with runtime parity vs
eager (max abs diff 0.008); gemma-3-1b-it bf16 export goes from 262
all-fp32 delegates to 446 delegates carrying bf16 I/O and runs end to end.
@JakeStevens
JakeStevens requested a review from digantdesai as a code owner July 27, 2026 19:29
@pytorch-bot

pytorch-bot Bot commented Jul 27, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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 f6b3565 with merge base 1a770b8 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

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.

@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 Jul 27, 2026
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.

1 participant