perf(knn): skip the brute-force candidate scan for rows with no closer neighbor #3986
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 501 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.372
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #3986"
general
weight: 0.35
Vika-F
Description
Fixes #3932.
In brute-force kNN,
getIndexesWithLessDistances(cpp/daal/src/algorithms/k_nearest_neighbors/bf_knn_impl.i) runs for every test row of each 128 x 128 distance block and collects the training indices closer than the row's current k-th neighbor. It is a stream compaction, which compilers do not vectorize except icx for AVX-512. So the sse2, avx2 and, with GCC, avx512 copies of the scan are the same scalar compare-and-branch loop. Once the neighbor heaps are full, most rows of a block hold no such distance and the scan finds nothing.This change computes the row minimum first, a
minreduction (PRAGMA_OMP_SIMD_ARGS) that vectorizes on every ISA, and returns no candidates when the minimum is not below the threshold. Rows that do have candidates take the existing loop. Results are bitwise identical: a row without candidates returned none before either, and a NaN distance is never taken as the minimum, as it was never selected.Why #3932 saw avx2 slower than sse2
The report measured a GCC build (
__release_lnx_gnu). With GCC the three copies of this scan are the same scalar code, so their relative speed follows where the linker places the hot loop, which moves with unrelated commits. Relative to native, GCC builds on an Intel CPU,sklearnexKNeighborsClassifier(algorithm="brute")on the issue's data:| GCC build | avx2 cap | sse2 cap |
|---|---:|---:|
| the issue's base commit (6fd5c2f73) | 1.22 | 1.21 |
| f0df92c73 | 1.00 | 1.47 |
| main (954619614) | 0.99 | 1.28 |
| either commit,
-falign-loops=64 -falign-functions=64| 0.91-1.02 | 0.93-0.98 |No kNN, threading or build-flag change lies between those commits. With icx the order is the expected one (avx2 1.29, sse2 1.47), because the avx512 copy vectorizes the scan with
vpcompressd. oneMKL is not involved: the dispatch cap applies to oneDAL only, and the same AVX-512sgemmkernel runs under every cap.Effect
Speedup of this change, same data, medians of interleaved runs on an Intel CPU (32 cores):
| build | native | avx2 cap | sse2 cap |
|---|---:|---:|---:|
| icx | 1.14 | 1.38 | 1.31 |
| GCC, 64-byte aligned | 1.61 | 1.63 | 1.25 |
| GCC, default flags, against the issue's base commit | 1.47 | 1.80 | 1.39 |
icx build, native dispatch:
| k |
predict|kneighbors||---:|---:|---:|
| 1 | 1.15 | 1.16 |
| 5 | 1.14 | 1.14 |
| 50 | 1.10 | 1.11 |
After the change avx2 is not slower than sse2 with either compiler.
Validation
predict, andkneighborsindices and distances, are bitwise identical to the unpatched build for icx and GCC under the native, avx2 and sse2 caps (make_classification 200000 x 32, k=5).bazel test --config=host //cpp/oneapi/dal/algo/knn:tests //examples/oneapi/cpp:knn //examples/daal/cpp:k_nearest_neighbors: 10 of 10 pass.No test is added: the change keeps every result the same, which the existing kNN tests check, and the gain is a run-time one.
---
<details>
<summary>Checklist:</summary>
Completeness and readability
Testing
Performance
</details>
🤖 Generated with Claude Code