← Back to Pull requests

perf(knn): skip the brute-force candidate scan for rows with no closer neighbor #3986

Closed
unknown closed this pull request without merging on Nov 05, 2026 10:18 UTC
View on GitHub
unknown unknown commented on Nov 04, 2026 08:39 UTC
Author

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 min reduction (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, sklearnex KNeighborsClassifier(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-512 sgemm kernel 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, and kneighbors indices 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.

  • clang-format and pre-commit 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

  • I have commented my code, particularly in hard-to-understand areas.

  • I have updated the documentation to reflect the changes or created a separate PR with updates and provided its number in the description, if necessary: no documentation change.

  • Git commit message contains an appropriate signed-off-by string _(see CONTRIBUTING.md for details)_.

  • I have resolved any merge conflicts that might occur with the base branch.
  • Testing

  • I have run it locally and tested the changes extensively.

  • All CI jobs are green or I have provided justification why they aren't: draft, CI pending.
  • Performance

  • I have provided justification why performance and/or quality metrics have changed or why changes are not expected: see the tables above.
  • </details>

    🤖 Generated with Claude Code

    Diff: 501 lines (+375 / -126) across 6 files Reviewer: Vika-F Review duration: 1538m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.5% (MEDIUM) · Higher = larger blast radius
    47.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.8% · Safe to merge
    18.8%