Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/validate_wrapper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ jobs:
run: >-
cmake --build build/wrapper-contract --target
llamadart_owner_api_test llamadart_owner_reentrancy_test
llamadart_f16_dot_accumulation_test
llamadart_speculative_api_test
llamadart_build_identity_test
llamadart_cancel_flag_test
Expand Down Expand Up @@ -717,6 +718,7 @@ jobs:
cmake --build ${{ matrix.build_dir }} --target
llamadart_owner_api_test llamadart_owner_reentrancy_test llamadart_exit_teardown_test llamadart_image_token_limit_test
llamadart_build_identity_test llamadart_cancel_flag_test
llamadart_f16_dot_accumulation_test
- name: Validate wrapper exports and the static destructor binding
run: >-
python3 tools/validate_exports.py --format nm --tool nm
Expand All @@ -727,8 +729,23 @@ jobs:
# the Metal library, which takes most of a minute.
- name: Run the exit teardown scenarios
run: >-
ctest --test-dir ${{ matrix.build_dir }} -R "owner_|exit_teardown|image_token_limit|build_identity|cancel_flag"
ctest --test-dir ${{ matrix.build_dir }} -R "owner_|exit_teardown|image_token_limit|build_identity|cancel_flag|f16_dot_accumulation"
--timeout 300 --output-on-failure
# Apple CPUs have FHM, so the shipped build takes the FMLAL branch of
# patch 0005. ARMv8.2-A with FP16 compiles its conversion branch, as on
# the armv8.2_2 Linux and Android variants.
- name: Run the F16 accumulation test on the conversion path
if: matrix.build == 'release'
run: |
cmake -S . -B build/f16-conversion -G Ninja -DCMAKE_BUILD_TYPE=Release \
-DGGML_NATIVE=OFF -DGGML_CPU_ARM_ARCH=armv8.2-a+fp16+dotprod \
-DGGML_METAL=OFF -DGGML_OPENMP=OFF -DGGML_CCACHE=OFF -DLLAMADART_BUILD_TESTS=ON
cmake --build build/f16-conversion --target llamadart_f16_dot_accumulation_test
if objdump -d build/f16-conversion/llamadart_f16_dot_accumulation_test | grep -q fmlal; then
echo "The conversion-path build contains FMLAL" >&2
exit 1
fi
build/f16-conversion/llamadart_f16_dot_accumulation_test
# A model that nothing tracks aborts at exit when Metal residency sets
# are live. Whether a runner has them is outside this repository's
# control, so their absence is reported instead of failing every native
Expand Down
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,19 @@ target_include_directories(llamadart_lib PRIVATE

if (LLAMADART_BUILD_TESTS)
enable_testing()
add_executable(llamadart_f16_dot_accumulation_test tests/f16_dot_accumulation_test.cpp)
target_compile_features(llamadart_f16_dot_accumulation_test PRIVATE cxx_std_17)
target_link_libraries(llamadart_f16_dot_accumulation_test PRIVATE ggml ggml-base)
if (GGML_BACKEND_DL)
target_compile_definitions(llamadart_f16_dot_accumulation_test PRIVATE
LLAMADART_TEST_CPU_BACKEND="$<TARGET_FILE:ggml-cpu>")
add_dependencies(llamadart_f16_dot_accumulation_test ggml-cpu)
else()
target_link_libraries(llamadart_f16_dot_accumulation_test PRIVATE ggml-cpu)
endif()
add_test(NAME llamadart_f16_dot_accumulation_test COMMAND llamadart_f16_dot_accumulation_test)
set_tests_properties(llamadart_f16_dot_accumulation_test PROPERTIES TIMEOUT 30)

add_library(llamadart_owner_creation_gate SHARED tests/owner_creation_gate.cpp)
target_compile_features(llamadart_owner_creation_gate PRIVATE cxx_std_17)
target_include_directories(llamadart_owner_creation_gate PRIVATE
Expand Down
4 changes: 4 additions & 0 deletions docs/linux_arm64_cpu_variants.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ enables. The compiler also uses these where they apply:
and 3 `stlur` (LRCPC2, ARMv8.4) instructions, plus the same inline LSE.
Neither is a scored feature; ARMv8.6 guarantees both, and the I8MM that
scores this variant is an ARMv8.6 feature.
- `linux_armv8.6_1` and `android_armv8.6_1` also use FMLAL/FMLAL2 (FHM) in the
F16 dot kernel of carried patch 0005: FP16 on ARMv8.4 or later implies FHM,
so the compiler defines `__ARM_FEATURE_FP16_FML`. The `armv8.2_2` variants
use the conversion path instead.
- `libggml-cpu.so` (ARMv8.0) contains LSE only inside libgcc's outline-atomic
helpers, each of which tests `__aarch64_have_lse_atomics` before reaching the
LSE instruction. It contains no LRCPC, LRCPC2 or CRC.
Expand Down
105 changes: 105 additions & 0 deletions docs/native174_f16_dot_accumulation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# ARM F16 dot accumulation (#174)

Patches 0005 and 0006 repair non-SVE ARM F16 dot products that overflow in
F16 before being returned as F32. Tracking:
[native #174](https://github.com/leehack/llamadart-native/issues/174),
[upstream #31734](https://github.com/ggml-org/llama.cpp/issues/31734)
(open, unfixed in llama.cpp `v0.9.0` and `master` as of 2026-11-07).
Published artifacts stay affected until a release carrying both patches
replaces them.

## Defect

With `__ARM_FEATURE_FP16_VECTOR_ARITHMETIC` (macOS arm64 and the arm64 iOS
simulator by default, `+fp16` Linux and Android variants; not iOS devices,
whose default target lacks it), two CPU kernels multiply and sum F16 inputs in
F16 and overflow past 65504:

- `ggml_vec_dot_f16` (one query column) maps `GGML_F16_DOT_VEC` to
`float16x8_t` unless the build has SVE. Only AVX512-FP16 and SVE widen.
- llamafile `tinyBLAS<8, float16x8_t, ...>` (eight or more query columns,
`llamafile_sgemm`) accumulates `float16x8_t` tiles. ggml builds it with
`GGML_LLAMAFILE`, which Android turns off and which ggml compiles out under
SVE or I8MM, so it reaches macOS arm64 and Linux `linux_armv8.2_2`.

Shipped `v0.8.0-3` macOS ARM64 and Linux `linux_armv8.2_2`/`linux_armv8.6_1`
modules return `inf` for finite operator inputs at magnitude 80/90/100; the
Linux armv8.0 baseline does not. Ordinary F16 K attention then produces NaN,
for one query and, through GEMM, for eight or more. Severity is P2: no
public-model failure is established.

## Repair

Both patches change build-tree copies only.

- 0005: under NEON, FMA, IEEE F16, FP16 vector arithmetic and no SVE,
`ggml_vec_dot_f16` multiplies, accumulates and reduces in F32 with four
low/high accumulator pairs (the original 32-element unroll), an eight-element
tail and a scalar tail. With `__ARM_FEATURE_FP16_FML` it uses FMLAL/FMLAL2;
otherwise it converts and uses F32 FMA.
- 0006: the FP16 branch of `llamafile_sgemm` instantiates
`tinyBLAS<4, float32x4_t, float32x4_t, ggml_fp16_t, ggml_fp16_t, float>`,
which loads F16 and accumulates in F32, instead of the F16 tile.

ISA flags, runtime dispatch, ABI, CPU variant selection and cache defaults are
unchanged. Both patches exceed the policy's "few lines" condition; the
maintainer approved the
[narrow exception](../patches/llama.cpp/README.md#exception-for-native-174).

## Performance

Apple M4 Max, CPU only, `GGML_NATIVE=OFF`, against the original kernels:

| Case | Cost |
| --- | --- |
| F16 mat-vec 4096x4096, 1 thread: FMLAL / conversion path | +13% / +78% time |
| F16 GEMM 4096x4096, 32–512 columns, 1 or 8 threads (0006) | +125–160% time |
| Same with `GGML_LLAMAFILE=OFF` instead of 0006 | +201–248% time; Q4_0/Q8_0 GEMM +50–58% |
| Llama-3.2-1B Q4_K_M pp512, F16 KV, 8 threads, FA off / on | -4.7% / -1.5% tokens/s |
| Same, 1 thread, FA off | -4.0% tokens/s |
| LFM2-VL-450M Q4_0 pp512, F16 KV, 8 threads, FA off | -3.0% tokens/s |
| Same models with `GGML_LLAMAFILE=OFF` instead of 0006, FA off | -7.4% to -9.1% tokens/s |

F16-weight models pay the GEMM cost on most of their prefill: the
`linux_armv8.2_2` F16 prefill gain over the armv8.0 baseline came from the F16
tile and is expected to largely go away (not yet measured there). A widening FMLAL tile was slower than the
conversion tile (+270% time) because its accumulators exceed the register file.

## Checks

- `llamadart_f16_dot_accumulation_test` (CTest, `validate_wrapper.yml`) runs
production graph/backend calls on one thread: the magnitude sweep, widths
1–129 around every unroll and tail boundary, cancellation, subnormal,
F16-max and product-overflow inputs, a cancellation whose F16 partial sums
overflow, 8/16/32-column GEMM cases, F32 controls, and ordinary and flash
attention over 16 keys for one and eight queries with an unsaturated
softmax. Weight rows are followed by NaN padding, so a read past the row
fails. It reports every failing check. The macOS release lanes also run it
built for `armv8.2-a+fp16+dotprod` to cover the conversion branch.
- Without the patches it fails on Apple silicon: 30 checks with neither, and
the seven GEMM checks plus eight-query ordinary attention with 0005 alone.
A `n - n % 4` tail mutation of 0005 fails 15 checks through the padding. Zero-length and
strided query tensors are not covered.
- `tools/linux_cpu_variant_smoke.c` runs the single-column magnitude sweep on
each selected Linux arm64 CPU module and rejects nonfinite matmul output.
- Always-active guards (`n >= 0` in 0005, `k >= 0` in 0006) carry the
artifact markers: Android and Apple for 0005, Apple for 0006 (Android builds
no llamafile). They prove inclusion, not correctness.
- The Android ISA audit decodes with `+fp16fml` so FMLAL is not reported as an
unknown word. Source fingerprints and SVE/SME range checks are unchanged;
they cover pristine upstream, not the patched copies.

## Before publication

These gates hold before any release carries the patches, including the
`v0.9.0` release that the daily stable detector publishes once its ISA audit
passes on `main`:

- Run the test against each published CPU module: Android variants on a
device, Linux variants, Apple.
- Compare prefill/decode throughput on an Android device for each variant with
the previous release, F16 KV with FA off and on, and an F16-weight model on
`linux_armv8.2_2`.

Retire a patch when the pinned upstream and every compatibility lane carry a
verified upstream repair; then add its `upstream_fix` text.
58 changes: 58 additions & 0 deletions patches/llama.cpp/0005-arm-f16-dot-f32-accumulation.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--- a/ggml/src/ggml-cpu/vec.cpp
+++ b/ggml/src/ggml-cpu/vec.cpp
@@ -263,4 +263,5 @@

void ggml_vec_dot_f16(int n, float * GGML_RESTRICT s, size_t bs, ggml_fp16_t * GGML_RESTRICT x, size_t bx, ggml_fp16_t * GGML_RESTRICT y, size_t by, int nrc) {
+ GGML_ASSERT(n >= 0 && "llamadart F16 dot F32 accumulation");
assert(nrc == 1);
GGML_UNUSED(nrc);
@@ -272,5 +273,48 @@


-#if defined(GGML_SIMD)
+#if defined(__ARM_NEON) && defined(__ARM_FEATURE_FMA) && defined(__ARM_FP16_FORMAT_IEEE) && \
+ defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && !defined(__ARM_FEATURE_SVE)
+ // F16 products and partial sums can overflow before the F32 result is stored.
+ float32x4_t sum_lo[4], sum_hi[4];
+ for (int j = 0; j < 4; ++j) {
+ sum_lo[j] = vdupq_n_f32(0.0f);
+ sum_hi[j] = vdupq_n_f32(0.0f);
+ }
+ const int np = n - n % 32;
+ const int np8 = n - n % 8;
+ int i = 0;
+ for (; i < np; i += 32) {
+ for (int j = 0; j < 4; ++j) {
+ const float16x8_t vx = vld1q_f16((const __fp16 *) (x + i + 8*j));
+ const float16x8_t vy = vld1q_f16((const __fp16 *) (y + i + 8*j));
+#if defined(__ARM_FEATURE_FP16_FML)
+ sum_lo[j] = vfmlalq_low_f16(sum_lo[j], vx, vy);
+ sum_hi[j] = vfmlalq_high_f16(sum_hi[j], vx, vy);
+#else
+ sum_lo[j] = vfmaq_f32(sum_lo[j], vcvt_f32_f16(vget_low_f16(vx)), vcvt_f32_f16(vget_low_f16(vy)));
+ sum_hi[j] = vfmaq_f32(sum_hi[j], vcvt_f32_f16(vget_high_f16(vx)), vcvt_f32_f16(vget_high_f16(vy)));
+#endif
+ }
+ }
+ for (; i < np8; i += 8) {
+ const float16x8_t vx = vld1q_f16((const __fp16 *) (x + i));
+ const float16x8_t vy = vld1q_f16((const __fp16 *) (y + i));
+#if defined(__ARM_FEATURE_FP16_FML)
+ sum_lo[0] = vfmlalq_low_f16(sum_lo[0], vx, vy);
+ sum_hi[0] = vfmlalq_high_f16(sum_hi[0], vx, vy);
+#else
+ sum_lo[0] = vfmaq_f32(sum_lo[0], vcvt_f32_f16(vget_low_f16(vx)), vcvt_f32_f16(vget_low_f16(vy)));
+ sum_hi[0] = vfmaq_f32(sum_hi[0], vcvt_f32_f16(vget_high_f16(vx)), vcvt_f32_f16(vget_high_f16(vy)));
+#endif
+ }
+ for (int j = 1; j < 4; ++j) {
+ sum_lo[0] = vaddq_f32(sum_lo[0], sum_lo[j]);
+ sum_hi[0] = vaddq_f32(sum_hi[0], sum_hi[j]);
+ }
+ sumf = (ggml_float) vaddvq_f32(vaddq_f32(sum_lo[0], sum_hi[0]));
+ for (; i < n; ++i) {
+ sumf += (ggml_float) GGML_CPU_FP16_TO_FP32(x[i]) * (ggml_float) GGML_CPU_FP16_TO_FP32(y[i]);
+ }
+#elif defined(GGML_SIMD)
#if defined(__ARM_FEATURE_SVE)
const int ggml_f16_epr = svcnth();
18 changes: 18 additions & 0 deletions patches/llama.cpp/0006-arm-f16-gemm-f32-accumulation.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--- a/ggml/src/ggml-cpu/llamafile/sgemm.cpp
+++ b/ggml/src/ggml-cpu/llamafile/sgemm.cpp
@@ -3900,4 +3900,4 @@
assert(m >= 0);
assert(n >= 0);
- assert(k >= 0);
+ GGML_ASSERT(k >= 0 && "llamadart F16 GEMM F32 accumulation");
assert(lda >= k);
@@ -4065,6 +4065,7 @@
#elif defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && !defined(_MSC_VER)
if (n < 8)
return false;
if (Btype == GGML_TYPE_F16) {
- tinyBLAS<8, float16x8_t, float16x8_t, ggml_fp16_t, ggml_fp16_t, float> tb{ params,
+ // F16 products and partial sums can overflow before the F32 result is stored.
+ tinyBLAS<4, float32x4_t, float32x4_t, ggml_fp16_t, ggml_fp16_t, float> tb{ params,
k, (const ggml_fp16_t *)A, lda,
(const ggml_fp16_t *)B, ldb,
17 changes: 17 additions & 0 deletions patches/llama.cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ these hold:
The v0.7.0 embedding-mode reservation repair and its exact baseline/candidate
evidence are documented in [v070_native_contracts.md](../../docs/v070_native_contracts.md).

## Exception for native #174

The maintainer approved a narrow exception to the first two eligibility
conditions above for patches 0005 and 0006 only. Disabling FP16 vector
arithmetic or llamafile is a build workaround that slows unrelated kernels
(llamafile off costs Q4_0/Q8_0 prefill too), and the F16 dot repair is more
than a few lines.

Published ARM64 artifacts return nonfinite results for finite F16 dot
products. Patch 0005 changes only the non-SVE NEON `ggml_vec_dot_f16`
(FMLAL/FMLAL2 with FP16 FML, conversion and F32 FMA otherwise); patch 0006
switches the FP16 llamafile GEMM tile to F32 accumulation. Neither changes ISA
flags, ABI, variants or cache defaults. Performance cost, checks and
publication gates are in
[native174_f16_dot_accumulation.md](../../docs/native174_f16_dot_accumulation.md).
This exception does not extend eligibility to other patches.

## Files

- `series.json` lists the patches in the order they apply. Each entry has the
Expand Down
19 changes: 19 additions & 0 deletions patches/llama.cpp/series.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@
"path": "ggml/src/ggml-metal/kernels/unary.metal",
"text": "inline constexpr bfloat4 ggml_metal_unary_cast<bfloat4, float4>(float4 x) {"
}
},
{
"file": "0005-arm-f16-dot-f32-accumulation.patch",
"tracking": "https://github.com/leehack/llamadart-native/issues/174",
"upstream": "https://github.com/ggml-org/llama.cpp/issues/31734",
"android_markers": [
{"library": "libggml-cpu.so", "text": "llamadart F16 dot F32 accumulation"}
],
"apple_markers": [
{"library": "libllamadart.dylib", "text": "llamadart F16 dot F32 accumulation"}
]
},
{
"file": "0006-arm-f16-gemm-f32-accumulation.patch",
"tracking": "https://github.com/leehack/llamadart-native/issues/174",
"upstream": "https://github.com/ggml-org/llama.cpp/issues/31734",
"apple_markers": [
{"library": "libllamadart.dylib", "text": "llamadart F16 GEMM F32 accumulation"}
]
}
]
}
Loading
Loading