Skip to content

Add NEON bf16_f32 GEMM micro-kernels for Arm cores without BF16 - #11950

Open
JakeStevens wants to merge 2 commits into
google:masterfrom
JakeStevens:bf16-fc-3
Open

JakeStevens wants to merge 2 commits into
google:masterfrom
JakeStevens:bf16-fc-3

Conversation

@JakeStevens

Copy link
Copy Markdown
Contributor

Stacked on #11949; only the last commit is new here.

Adds widen-and-FMA AArch64 bf16_f32 GEMM micro-kernels as the non-BF16 branch of the Arm config, so the bf16 fully-connected and batch matrix multiply operator APIs work on cores without FEAT_BF16. xnn_init_bf16_f32_gemm_config() no longer requires BF16 hardware; the subgraph still does, because the fp32 fallback is faster for batched inputs on these cores.

Pixel 6 Pro (Cortex-X1, no BF16, NDK r29 clang 21.0.0), BF16FullyConnected, 1 thread pinned, median of 3, at the shapes measured there; native neonfma is forced by temporarily dropping the subgraph's hardware-bf16 check:

M K N fp32 fallback native neonfma peak memory
1 4096 512 320 us 227 us 16 -> 4 MiB
1 512 4096 312 us 219 us 16 -> 4 MiB
16 4096 512 1626 us 1759 us 16 -> 4 MiB
128 512 4096 10135 us 12664 us 16 -> 4 MiB

Tests: bf16-f32-gemm-minmax-test (242 tests), fully-connected-test, batch-matrix-multiply-test, fully-connected-nc-test and batch-matrix-multiply-nc-test pass on Apple M4 Max and Pixel 10. Before the split, bf16-f32-gemm-minmax-test (132 tests) and fully-connected-test (72 tests) passed on the Pixel 6 Pro. The armeabi-v7a build compiles.

Authored with Claude Code.

Adds BFDOT c2 bf16_f32 GEMM micro-kernels and an AArch64 bf16_f32 GEMM config
that uses them on cores with FEAT_BF16, so the subgraph now keeps bf16
fully-connected nodes native there. Apple cores keep the fp32 fallback: on an
M4 (Apple clang 17.0.0, 1 thread, native forced by temporarily dropping the
Apple clause) native BFDOT is 1.05-1.15x faster at M=1 but 0.64-0.66x at M=16
and 128. The bf16_f32 GEMM tester now passes a real cn_stride (the existing
kernels ignore it), and the test is registered in CMake.

Pixel 10 (Tensor G5, Cortex-X4 core, NDK r29 clang 21.0.0), 1 thread pinned,
BF16FullyConnected, median of 3; "before" is master, which rewrites the node
to fp32:

| M | K | N | before | after | speedup | peak memory |
|--:|--:|--:|---:|---:|---:|---|
| 1 | 4096 | 512 | 216 us | 124 us | 1.74x | 16 -> 4 MiB |
| 1 | 512 | 4096 | 171 us | 114 us | 1.50x | 16 -> 4 MiB |
| 16 | 4096 | 512 | 1273 us | 690 us | 1.84x | 16 -> 4 MiB |
| 16 | 512 | 4096 | 1145 us | 701 us | 1.63x | 16 -> 4 MiB |
| 128 | 4096 | 512 | 7230 us | 5089 us | 1.42x | 18 -> 4 MiB |
| 128 | 512 | 4096 | 7576 us | 5227 us | 1.45x | 16 -> 4 MiB |

    bf16-f32-gemm-minmax-test   # Apple M4 Max: [  PASSED  ] 132 tests.
    fully-connected-test        # Apple M4 Max: [  PASSED  ] 83 tests.
    bf16-f32-gemm-minmax-test   # Pixel 10: [  PASSED  ] 132 tests.
    fully-connected-test        # Pixel 10: [  PASSED  ] 72 tests.
Adds widen-and-FMA AArch64 bf16_f32 GEMM micro-kernels as the non-BF16 branch
of the Arm bf16_f32 GEMM config, so the bf16 fully-connected and batch matrix
multiply operator APIs work on cores without FEAT_BF16.
xnn_init_bf16_f32_gemm_config() no longer requires BF16 hardware; the subgraph
still does, because the fp32 fallback is faster for batched inputs here.

Pixel 6 Pro (Cortex-X1, no BF16, NDK r29 clang 21.0.0), 1 thread pinned,
BF16FullyConnected, median of 3, at the shapes measured there; native neonfma
is forced by temporarily dropping the subgraph's hardware-bf16 check:

| M | K | N | fp32 fallback | native neonfma | peak memory |
|--:|--:|--:|---:|---:|---|
| 1 | 4096 | 512 | 320 us | 227 us | 16 -> 4 MiB |
| 1 | 512 | 4096 | 312 us | 219 us | 16 -> 4 MiB |
| 16 | 4096 | 512 | 1626 us | 1759 us | 16 -> 4 MiB |
| 128 | 512 | 4096 | 10135 us | 12664 us | 16 -> 4 MiB |

    bf16-f32-gemm-minmax-test   # Apple M4 Max: [  PASSED  ] 242 tests.
    fully-connected-test        # Apple M4 Max: [  PASSED  ] 83 tests.
    bf16-f32-gemm-minmax-test   # Pixel 6 Pro, before the split: [  PASSED  ] 132 tests.
    fully-connected-test        # Pixel 6 Pro, before the split: [  PASSED  ] 72 tests.
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.

1 participant