Skip to content

Add native bf16 convolution (x86 AVX512/AVX2, Arm NEON and BF16) - #11292

Closed
JakeStevens wants to merge 4 commits into
google:masterfrom
JakeStevens:bf16-conv
Closed

JakeStevens wants to merge 4 commits into
google:masterfrom
JakeStevens:bf16-conv

Conversation

@JakeStevens

@JakeStevens JakeStevens commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Adds native bf16 convolution (bf16 input and filter, fp32 bias and output) through a new xnn_*_convolution2d_nhwc_bf16_f32 operator and bf16_f32 dwconv/igemm micro-kernels for AVX512BF16, AVX512SKX/AVX2, Arm BF16 and NEON. Previously a bf16 filter hit XNN_UNREACHABLE in xnn_define_convolution_2d. The subgraph keeps bf16 convolutions native only on CPUs with hardware bf16, except Apple's, which issue BFDOT at ~2/3 of the FMLA rate (0.65-0.69x of the fp32 fallback on an M4). Elsewhere the fp32 fallback is faster, and the non-BF16 kernels only serve the operator API.

Native vs the same bf16 graph through the fp32 fallback, 1 thread pinned, batch 1 (Arm: NDK r29 clang 21.0.0):

shape (W=H, CI->CO, 3x3) Genoa (AVX512BF16) Pixel 10 (Arm BF16) Pixel 6 Pro (no BF16, not used)
conv 112, 32->16 1.34x 1.37x 0.69x
conv 14, 576->160 1.96x 1.41x 0.73x
conv 7, 960->320 2.04x 1.80x 0.75x
dw-separable 7, 960->320 1.09x 1.05x 1.01x

Peak memory is 2-4x lower for dense convolutions. Each commit message has the full table.

Tests: the new bf16-f32 dwconv/igemm micro-kernel tests and the conv subgraph tests pass on Genoa, Pixel 10, Pixel 6 Pro and Apple M4 Max. The armeabi-v7a build compiles.

Authored with Claude Code.

Adds bf16 x bf16 -> fp32 depthwise (scalar, AVX2, AVX512SKX) and c2 igemm
(scalar, AVX512BF16) micro-kernels for native bf16 convolution, with their
packers and micro-kernel tests. Nothing dispatches them yet.

Tests on an AMD EPYC Genoa (GCC 15):

    bf16-f32-dwconv-minmax-test   # [  PASSED  ] 300 tests.
    bf16-f32-igemm-minmax-test    # [  PASSED  ] 364 tests.
Adds xnn_{create,reshape,setup}_convolution2d_nhwc_bf16_f32 and lets
xnn_define_{depthwise_,}convolution_2d accept bf16 filters, which previously
hit XNN_UNREACHABLE (e.g. the causal conv1d in Qwen3.5). The bf16 fallback
keeps a convolution native, with an fp32 output, only on CPUs with hardware
bf16 other than Apple's, and when its bias is absent or fp32; everything else
takes the existing fp32 rewrite.

Genoa (AVX512BF16 igemm, AVX512SKX dwconv), 1 thread pinned, batch 1,
subgraph-convolution-bench / subgraph-depthwise-separable-bench, median of 5;
the fp32 fallback is the same bf16 graph with the native path disabled:

| shape (W=H, CI->CO, 3x3) | fp32 fallback | native | speedup | peak memory |
|---|---:|---:|---:|---|
| conv 112, 32->16 | 860 us | 643 us | 1.34x | 3.2 -> 1.6 MiB |
| conv 56, 96->24 | 1195 us | 461 us | 2.59x | 1.8 -> 0.6 MiB |
| conv 14, 576->160 | 1344 us | 685 us | 1.96x | 6.9 -> 1.7 MiB |
| conv 7, 960->320 | 885 us | 434 us | 2.04x | 21.3 -> 5.3 MiB |
| dw-separable 112, 32->16 | 296 us | 267 us | 1.11x | 3.4 -> 2.6 MiB |
| dw-separable 14, 576->160 | 182 us | 177 us | 1.03x | 1.5 -> 1.2 MiB |
| dw-separable 7, 960->320 | 113 us | 103 us | 1.09x | 2.7 -> 2.5 MiB |

    convolution-2d-test --gtest_filter='*BF16*'             # passes
    depthwise-convolution-2d-test --gtest_filter='*BF16*'   # passes
Adds widen-and-FMA NEON bf16_f32 dwconv and AArch64 igemm micro-kernels, so
the bf16 convolution operator API works on Arm cores without FEAT_BF16. The
subgraph keeps the fp32 fallback on these cores because it is faster.

Pixel 6 Pro (Cortex-X1, no BF16, NDK r29 clang 21.0.0), 1 thread pinned,
bf16 graph, median of 3:

| shape (W=H, CI->CO, 3x3) | fp32 fallback | native neonfma | peak memory |
|---|---:|---:|---|
| conv 112, 32->16 | 1797 us | 2621 us | 3.2 -> 1.6 MiB |
| conv 14, 576->160 | 3946 us | 5396 us | 6.9 -> 1.7 MiB |
| conv 7, 960->320 | 2875 us | 3808 us | 21.3 -> 5.3 MiB |
| dw-separable 112, 32->16 | 760 us | 842 us | 3.4 -> 2.6 MiB |
| dw-separable 7, 960->320 | 342 us | 338 us | 2.7 -> 2.5 MiB |

Tests pass on the Pixel 6 Pro (neonbf16 cases skip) and an Apple M4 Max.
Adds BFDOT c2 igemm and BFMLALB/T dwconv micro-kernels and uses them on cores
with FEAT_BF16, where the subgraph now keeps bf16 convolutions native. Apple
cores keep the fp32 fallback: they issue BFDOT at ~2/3 of the FMLA rate, and on
an M4 (Apple clang 17.0.0, 1 thread) the native igemm convs below run at
0.65-0.69x of the fallback.

Pixel 10 (Tensor G5, NDK r29 clang 21.0.0), 1 thread pinned, bf16 graph,
median of 3:

| shape (W=H, CI->CO, 3x3) | fp32 fallback | native | speedup | peak memory |
|---|---:|---:|---:|---|
| conv 112, 32->16 | 1447 us | 1055 us | 1.37x | 3.2 -> 1.6 MiB |
| conv 56, 96->24 | 1574 us | 1115 us | 1.41x | 1.8 -> 0.5 MiB |
| conv 14, 576->160 | 3078 us | 2177 us | 1.41x | 6.9 -> 1.7 MiB |
| conv 7, 960->320 | 2753 us | 1533 us | 1.80x | 21.3 -> 5.3 MiB |
| dw-separable 112, 32->16 | 471 us | 364 us | 1.29x | 3.4 -> 2.6 MiB |
| dw-separable 14, 576->160 | 414 us | 382 us | 1.08x | 1.5 -> 1.2 MiB |
| dw-separable 7, 960->320 | 261 us | 249 us | 1.05x | 2.7 -> 2.5 MiB |

    bf16-f32-dwconv-minmax-test   # [  PASSED  ] 300 tests.
    bf16-f32-igemm-minmax-test    # [  PASSED  ] 312 tests.
    convolution-2d-test           # [  PASSED  ] 30 tests.
    depthwise-convolution-2d-test # [  PASSED  ] 25 tests.
@JakeStevens

Copy link
Copy Markdown
Contributor Author

Split into one PR per ISA: #11943 (scalar kernels, packers, tests), #11944 (operator and subgraph), #11945 (AVX512SKX dwconv), #11946 (AVX512BF16 igemm), #11947 (Arm BF16). The AVX2 and NEON widen-and-FMA kernels are dropped.

@JakeStevens JakeStevens closed this Nov 9, 2026
@JakeStevens
JakeStevens deleted the bf16-conv branch November 9, 2026 19:07
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