Skip to content

Add Arm BF16 (BFDOT/BFMLAL) bf16 convolution micro-kernels - #11947

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

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

Conversation

@JakeStevens

Copy link
Copy Markdown
Contributor

Adds BFDOT c2 igemm and BFMLALB/T dwconv micro-kernels and uses them for bf16
convolutions on Arm64 cores with FEAT_BF16. 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, measured without the exclusion) the native igemm convs
below run at 0.65-0.69x of the fallback.

Pixel 10 (Tensor G5, Cortex-X4 core, NDK r29 clang 21.0.0), 1 thread pinned,
median of 3, subgraph-convolution-bench BF16Convolution and
subgraph-depthwise-separable-bench BF16DepthwiseSeparable; the fp32 fallback
is the parent commit:

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     # Pixel 10: [  PASSED  ] 180 tests.
bf16-f32-igemm-minmax-test      # Pixel 10: [  PASSED  ] 182 tests.
convolution-2d-test             # Pixel 10: [  PASSED  ] 30 tests.
depthwise-convolution-2d-test   # Pixel 10: [  PASSED  ] 25 tests.
convolution-nhwc-test --gtest_filter='*BF16*'   # Pixel 10: [  PASSED  ] 84 tests.

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

Authored with Claude Code.

Adds bf16 x bf16 -> fp32 scalar depthwise and c2 igemm micro-kernels, their
weight packers and micro-kernel tests, as the base for a native bf16
convolution. The scalar micro-kernels are reference implementations that test
the packers and aren't dispatched; the SIMD micro-kernels and the operator
follow in separate changes.

    bf16-f32-dwconv-minmax-test   # Apple M4 Max: [  PASSED  ] 60 tests.
    bf16-f32-igemm-minmax-test    # Apple M4 Max: [  PASSED  ] 52 tests.
Adds xnn_{create,reshape,setup}_convolution2d_nhwc_bf16_f32 and lets
xnn_define_{depthwise_,}convolution_2d accept bf16 inputs and filters, which
they previously rejected with xnn_status_unsupported_parameter (e.g. the
causal conv1d in Qwen3.5). The bf16 fallback keeps a convolution native, with
an fp32 output, when a bf16_f32 micro-kernel is available on a CPU with
hardware bf16 other than Apple's and its bias is absent or fp32. No configs
have micro-kernels yet, so for now bf16 convolutions run through the existing
fp32 rewrite. Also adds bf16_f32 operator tests (skipped until a config has
micro-kernels), BF16Convolution and BF16DepthwiseSeparable subgraph
benchmarks, and the CMake target for the existing subgraph convolution
benchmark.

    convolution-2d-test             # Apple M4 Max: [  PASSED  ] 30 tests.
    depthwise-convolution-2d-test   # Apple M4 Max: [  PASSED  ] 25 tests.
    convolution-nhwc-test           # Apple M4 Max: [  PASSED  ] 911 tests (bf16_f32 skipped).
Adds widen-and-FMA AVX512SKX bf16_f32 depthwise micro-kernels. The subgraph
uses them for depthwise bf16 convolutions on CPUs with AVX512BF16; the
operator API uses them on any AVX512SKX CPU.

Genoa, 1 thread pinned, batch 1, subgraph-depthwise-separable-bench
BF16DepthwiseSeparable, median of 5; the fp32 fallback is the parent commit.
The pointwise convolution runs in fp32 either way, because its input is the
depthwise convolution's fp32 output:

| shape (W=H, CI->CO, 3x3) | fp32 fallback | native | speedup | peak memory |
|---|---:|---:|---:|---|
| 112, 32->16 | 296 us | 267 us | 1.11x | 3.4 -> 2.6 MiB |
| 14, 576->160 | 182 us | 177 us | 1.03x | 1.5 -> 1.2 MiB |
| 7, 960->320 | 113 us | 103 us | 1.09x | 2.7 -> 2.5 MiB |

bf16-f32-dwconv-minmax-test passed on the Genoa (GCC 15) before these
changes were split up. At this commit an x86-64 build runs under Rosetta on an
Apple M4, where the AVX512SKX tests are skipped and the rest pass.
Adds VDPBF16PS c2 bf16_f32 igemm micro-kernels (1-8 x 16/32) and uses 7x32
and 1x32 for bf16 convolutions on CPUs with AVX512BF16.

Genoa, 1 thread pinned, batch 1, subgraph-convolution-bench BF16Convolution,
median of 5; the fp32 fallback is the parent commit:

| shape (W=H, CI->CO, 3x3) | fp32 fallback | native | speedup | peak memory |
|---|---:|---:|---:|---|
| 112, 32->16 | 860 us | 643 us | 1.34x | 3.2 -> 1.6 MiB |
| 56, 96->24 | 1195 us | 461 us | 2.59x | 1.8 -> 0.6 MiB |
| 14, 576->160 | 1344 us | 685 us | 1.96x | 6.9 -> 1.7 MiB |
| 7, 960->320 | 885 us | 434 us | 2.04x | 21.3 -> 5.3 MiB |

bf16-f32-igemm-minmax-test passed on the Genoa (GCC 15) before these changes
were split up. At this commit an x86-64 build runs under Rosetta on an Apple
M4, where the AVX512 tests are skipped and the rest pass.
Adds BFDOT c2 igemm and BFMLALB/T dwconv micro-kernels and uses them for bf16
convolutions on Arm64 cores with FEAT_BF16. 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, measured without the exclusion) the native igemm convs
below run at 0.65-0.69x of the fallback.

Pixel 10 (Tensor G5, Cortex-X4 core, NDK r29 clang 21.0.0), 1 thread pinned,
median of 3, subgraph-convolution-bench BF16Convolution and
subgraph-depthwise-separable-bench BF16DepthwiseSeparable; the fp32 fallback
is the parent commit:

| 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     # Pixel 10: [  PASSED  ] 180 tests.
    bf16-f32-igemm-minmax-test      # Pixel 10: [  PASSED  ] 182 tests.
    convolution-2d-test             # Pixel 10: [  PASSED  ] 30 tests.
    depthwise-convolution-2d-test   # Pixel 10: [  PASSED  ] 25 tests.
    convolution-nhwc-test --gtest_filter='*BF16*'   # Pixel 10: [  PASSED  ] 84 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