Skip to content

Add NEON-BF16 bf16 binary elementwise micro-kernels - #11952

Merged
2 commits merged into
google:masterfrom
JakeStevens:bf16-binary-2
Nov 13, 2026
Merged

2 commits merged into
google:masterfrom
JakeStevens:bf16-binary-2

Conversation

@JakeStevens

Copy link
Copy Markdown
Contributor

Title: Add NEON-BF16 bf16 binary elementwise micro-kernels

Stacked on #11951; only the last commit is new.

Adds bf16 binary micro-kernels that round with BFCVTN/BFCVTN2 instead of the software round to nearest even, and uses them on ARM64 CPUs with FEAT_BF16.

vbinary-bench, u16 kernels (the ones the configs use), 8192 elements, median of 5, ns. Pixel 10: Cortex-X4 core pinned, NDK r29 clang 21.0.0; M4 Max: Apple clang 17.0.0, unpinned:

op Pixel 10 NEON Pixel 10 NEON-BF16 speedup M4 Max NEON M4 Max NEON-BF16 speedup
add 1268 977 1.30x 927 530 1.75x
mul 1388 976 1.42x 937 534 1.75x
sqrdiff 1588 1080 1.47x 1123 681 1.65x
prelu 2165 1597 1.36x 1745 1149 1.52x
div 2859 2590 1.10x 1040 697 1.49x

Tests: bf16-v*, binary-test and binary-elementwise-nd-test pass on an Apple M4 Max.

Authored with Claude Code.

Adds bf16 binary elementwise configs (add, sub, mul, div, max, min, prelu,
sqrdiff, with broadcast and reversed variants) and ARM64 NEON micro-kernels
that compute in fp32 and round to nearest even once. The bf16 -> fp32 fallback
now keeps bf16 binary nodes native when a config exists; ARM32 and x86 keep the
fp32 fallback. The micro-kernel tests check results bit for bit, and the binary
subgraph benchmark gets bf16 cases and a CMake target.

Pixel 6 Pro (Cortex-X1 core pinned, no BF16), NDK r29 clang 21.0.0, single
thread, 8192 elements: a native bf16 add takes 1748 ns, an f32 add 931 ns, and
a bf16 add through the fp32 fallback ~4900 ns.

subgraph-binary-bench BF16Binary on the same core, median of 3, bf16 nodes
forced through the fp32 fallback vs native:

| op | shape | fp32 fallback | native | speedup |
|---|---|---:|---:|---:|
| add | 1x256 | 0.27 us | 0.095 us | 2.8x |
| add | 1x4096 | 2.70 us | 1.02 us | 2.6x |
| add | 256x4096 | 1069 us | 278 us | 3.8x |
| multiply | 256x4096 | 1107 us | 295 us | 3.8x |
| divide | 256x4096 | 1112 us | 396 us | 2.8x |
| prelu | 256x4096 | 1094 us | 422 us | 2.6x |

Tests: bf16-v* (bit-exact, including subnormal, tie, overflow, signed-zero and
infinity cases), binary-test and binary-elementwise-nd-test pass on an Apple
M4 Max. NativeBinaryOpsStayBf16 is in the Bazel-only
bf16_to_fp32_fallback_test (it depends on litert/tensor, which CMake doesn't
build), so it hasn't been run locally.
Adds bf16 binary micro-kernels that round with BFCVTN/BFCVTN2 instead of the
software round to nearest even, and uses them on ARM64 CPUs with FEAT_BF16.

vbinary-bench, u16 kernels (the ones the configs use), 8192 elements, median
of 5, ns. Pixel 10: Cortex-X4 core pinned, NDK r29 clang 21.0.0; M4 Max:
Apple clang 17.0.0, unpinned:

| op | Pixel 10 NEON | Pixel 10 NEON-BF16 | speedup | M4 Max NEON | M4 Max NEON-BF16 | speedup |
|---|---:|---:|---:|---:|---:|---:|
| add | 1268 | 977 | 1.30x | 927 | 530 | 1.75x |
| mul | 1388 | 976 | 1.42x | 937 | 534 | 1.75x |
| sqrdiff | 1588 | 1080 | 1.47x | 1123 | 681 | 1.65x |
| prelu | 2165 | 1597 | 1.36x | 1745 | 1149 | 1.52x |
| div | 2859 | 2590 | 1.10x | 1040 | 697 | 1.49x |

Tests: bf16-v*, binary-test and binary-elementwise-nd-test pass on an Apple
M4 Max.
@dsharlet

Copy link
Copy Markdown
Collaborator

This looks overlapping with #11951 . I approved that one first. Let's merge that, then rebase this. Approving them all at once is going to result in a lot of code migration machinary churning on conflicts.

@copybara-service copybara-service Bot closed this pull request by merging all changes into google:master in 694850e Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants