Repository navigation
Support bf16 in dotProduct and optimize the implementation slightly - #106569
Conversation
`arrayDotProduct` rejected `BFloat16` arrays with an exception, even though `arrayNorm`/`arrayDistance` already support them and `BFloat16` is the common storage format for embedding vectors. Template the existing auto-vectorized `dotProductImpl` kernel on `ArgumentType` and widen each element to `ResultType` with a `static_cast` inside the loop (the arrayNorm "widen in the kernel" approach). `BFloat16 x BFloat16` now accumulates in `Float32` (matching `arrayNorm`/`arrayDistance`); the widening is the identity when `ArgumentType == ResultType`, so the `Float32`/`Float64` and integer paths are byte-for-byte unchanged (verified at runtime). Because the kernel is a generic `MULTITARGET_FUNCTION`, the widened `BFloat16` path auto-vectorizes on every target - AVX-512 on x86 (`vpmovzxwd`+`vpslld`+FMA), NEON on ARM - rather than only x86. Measured `arrayDotProduct` `BFloat16` vs `Float32` (5M x 150, one thread): AMD Zen5 0.053s vs 0.094s (1.8x faster, half the bytes), Intel GNR 0.170s vs 0.230s. No native AVX512-BF16 (`dpbf16`) path yet - left as a follow-up. Co-Authored-By: Claude Opus 4.8 <[email protected]>
`arrayDotProduct` previously rejected `BFloat16`, so the new widened path had no coverage. Add `04312_array_dot_product_bfloat16`: the SIMD const-argument path over length-80 vectors checked against an independent `Float64` computation within a tolerance (stable across architectures), the `Float32` result type, and exact small-integer cases. Co-Authored-By: Claude Opus 4.8 <[email protected]>
`dotProduct.xml` covered only `Float32`/`Float64`. Split it into `dotProduct_bfloat16.xml`, `dotProduct_float32.xml` and `dotProduct_float64.xml`, one element type each (2 queries per file), and add the `BFloat16` case now that `arrayDotProduct` supports it. Splitting keeps each file well under the performance runner per-test query limit, the same issue that was hiding the tail of the combined `norm_distance` test, and leaves room to re-enable the integer cases without exceeding it. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The dispatch called the `NO_INLINE` `dotProductImpl` kernel once per row. For the float/BFloat16 fast path that call boundary stalls the hardware prefetcher between rows, leaving the memory-bound kernel waiting on loads - the same issue that regressed `Float64` array distances on Zen5. Move the per-row loop inside the kernel: add `dotProductBatchImpl` (both columns advance, sizes taken from the offsets) and `dotProductConstBatchImpl` (left vector fixed, right column advances) and call each once per column in the non-const and const-left-argument paths. The scalar fall-back path for mixed/integer types keeps its per-row loop. Results are unchanged (verified byte-for-byte against the per-row version on deterministic data; the official `02011_tuple_vector_functions` / `03269_bf16` tests still pass). Measured on AMD Zen5 (5M x 150, one thread) vs the per-row version: `Float32` const-arg 1.33x and self 1.28x, `Float64` const-arg 1.17x, `BFloat16` ~1.05-1.08x; no query regresses. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Workflow [PR], commit [38ad78b] Summary: ✅
AI ReviewSummaryThis PR adds Final VerdictStatus: ✅ Approve |
|
📊 Cloud Performance Report ✅ AI verdict: This PR is scoped entirely to the arrayDotProduct/dotProduct vector functions: it adds BFloat16 support and restructures the SIMD kernel into batched per-column calls. Neither ClickBench nor TPC-H queries call dotProduct, so none of the flagged deltas can plausibly come from this change. The eight ClickBench "improvements" (about 5-18%) and the single TPC-H Q4 "regression" (+15.5%, with noisy underlying measurements) are off-path run-to-run variance and have been downgraded. No actionable performance impact from this change on these benchmarks. clickbenchFlagged queries (8 of 43)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_officialFlagged queries (1 of 22)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
The registered `FunctionDocumentation` for `arrayDotProduct` and the public `dotProduct` did not describe `BFloat16` inputs or the same-type `BFloat16` -> `Float32` result rule, so the generated structured docs were stale. List `BFloat16` as a supported array element type and document that two `BFloat16` arrays accumulate in and return `Float32` (the same rule as two `Float32` arrays). Co-Authored-By: Claude Opus 4.8 <[email protected]>
The test used `range(80)`, but 80 is a multiple of the kernel unroll width (16), so `dotProductConstBatchImpl` exited after the unrolled loop and never ran the scalar tail. It also only exercised the constant-left batch path, never the separate non-const kernel (`dotProductBatchImpl`) used by `arrayDotProduct(v, v)`. Use `range(150)` (not a multiple of 16, so the tail runs) and add a column-vs-column `arrayDotProduct(v, v)` assertion checked against an independent `Float64` computation, so both batch kernels and their tails are covered. Co-Authored-By: Claude Opus 4.8 <[email protected]>
dotProductdotProduct and optimize the implementation slightly
rienath
left a comment
There was a problem hiding this comment.
Looks good. I left a few comments. On a side-note: could we move the PR to Improvement category and add label pr-performance manually as the OR includes both new capability and perf improvement? That'd make the life of release engineer easier down the line.
Also, I noticed that both arm and x86 machines in CI do not have performance difference on F32/F64. Do you have any hypotheses why on Zen5 and Intel GNR the performance changes?
The split into `dotProduct_{bfloat16,float32,float64}.xml` was done to stay under
the perf runner's per-test query limit, but that limit was removed (all tests run
now), so the split just produced three near-identical files that land in
different CI jobs and are harder to find. Fold them back into a single
`dotProduct.xml` with `element_type` ranging over `BFloat16`, `Float32`,
`Float64` (6 queries total).
Co-Authored-By: Claude Opus 4.8 <[email protected]>
The dot-product SIMD kernel only fires for same-type BFloat16 x BFloat16; a BFloat16 array against any other type falls to the scalar accumulator, which was untested. Add a distinct Float32 column and assert both orderings (BF16 x T and T x BF16) match each other and an independent Float64 reference, for both the column-vs-column path and the const-argument path, plus the mixed result type (BFloat16 x Float32 -> Float64). Co-Authored-By: Claude Opus 4.8 <[email protected]>
The |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 73/93 (78.49%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 3 line(s) · Uncovered code |
rienath
left a comment
There was a problem hiding this comment.
I've left a small nit, but otherwise looks good
| /// Manual unrolling with independent accumulators to break FP dependency chains. | ||
| /// With FMA latency ~4 cycles and throughput 1/cycle, we need >= 4 independent | ||
| /// chains to saturate the pipeline. 16 accumulators is enough to do that while | ||
| /// keeping the per-row reduction and scalar remainder small: a wider unroll | ||
| /// (e.g. 128/sizeof = 32 for Float32) only pays off for very long arrays and | ||
| /// noticeably regresses the short (~150-element) arrays typical of vector search. |
There was a problem hiding this comment.
Maybe don't need this block second time (it's very close to the first one and repeats same information)
…bf16 Support bf16 in `dotProduct` and optimize the implementation slightly
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added support for
BFloat16indotProductand improved dot product performance by batching the SIMD path.Unfortunately, CI won't show perf improvements from enabling bf16, because on master, all such queries will just throw an exception. Here are the custom measurements:
Version info
26.7.1.66