← Back to Pull requests

perf(hdbscan): stop calling a length-1 vPowx per coordinate in the Minkowski distance #3895

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 20, 2026 08:34 UTC
Author

Problem

MinkowskiDist raised every coordinate difference with MathInst::sPowx and took the root with another sPowx. With MKL, sPowx is vPowx(1, &in, p, &r), a VML call of length 1. So one distance cost d + 1 VML calls, in pointDist, bboxLowerBound and blockDist.

As a result, minkowski with p not in {1, 2, inf} was 0.26-0.6x scikit-learn single-threaded on ball_tree and brute. p = 1 and p = 2 were not affected, because scikit-learn-intelex maps them to manhattan and euclidean.

Fix

All of this is in hdbscan_distance_utils.h.

  • p a multiple of 1/2 in [1, 8] (1, 1.5, 2, 2.5, 3, ...): the terms are raised by repeated multiplication, with one sqrt for the half. The root is skipped for p = 1 and is sqrt for p = 2.

  • Any other p: the terms are staged in a 256-element buffer, so one vPowx call covers a whole row, or several short rows in blockDist.

  • blockDist: takes all the roots of a block with one vPowx (or vSqrt) call.

  • Brute force: the Minkowski matrix is filled with one blockDist call per row, through a new useBlockDist template flag on fillFullDistMatrix. Manhattan and Chebyshev keep the per-pair loop; routing them through blockDist measured 3-6% slower.
  • hdbscan_ball_tree_batch_impl.i is not touched.

    Results:

  • Non-half p goes through the same vPowx as before, now on many terms per call; the test checks the MST weights against std::pow to tolerance, and in the measured runs (p = 2.7) the MST weights came out identical.

  • Multiplication path (p a multiple of 1/2 up to 8): results match pow to tolerance, not bitwise. The largest relative MST weight difference is 2.2e-16 to 4.2e-16 in float64, and the labels are identical in every measured case.

  • MKL and the reference/OpenBLAS backend: the MathInst calls used are the same on both, so the change applies to both. On the reference backend sPowx was already pow, so the gain there is smaller.
  • Test

    New test hdbscan: minkowski merge distances match the MST of std::pow distances in test/batch.cpp. Each method and float type is checked against a reference that uses neither oneDAL code nor tie handling:

  • p = 1.5 and 3 (multiplication) and 2.7 (vPowx);

  • 3 columns, and 300 columns, which do not fit one buffer;

  • min_samples = 1, so the merge distances equal the MST weights;

  • the sorted merge distances are compared with a Prim MST over std::pow distances.
  • bazel test --config=host //cpp/oneapi/dal/algo/hdbscan:tests passes. A gcc -Wall -Wextra -Werror syntax check of the three DAAL HDBSCAN sources with the reference backend passes too.

    Measured effect

    Setup:

  • param_blobs_n10000_d4 (10000 x 4, float64) from the HDBSCAN benchmark study;

  • Make CPU release of this branch and of main (same base);

  • scikit-learn-intelex sklearnex.preview.cluster.HDBSCAN, scikit-learn 1.9.1;

  • numactl --physcpubind=0 / 0-7 / 0-55 --membind=0 on a 2-socket Intel CPU, with OMP/MKL/OPENBLAS_NUM_THREADS, threadpool_limits and n_jobs all set to the thread count.
  • Each process warms up, then runs 3 timed fits. main, this PR and scikit-learn processes are interleaved, with the order alternated over 3 rounds. Values are medians of 9 fits per side, given as ratios: speedup vs main is main time / PR time, and "vs sklearn" is sklearn time / sklearnex time, so > 1 means sklearnex is faster.

    The machine is shared; the load average was recorded for every process.

    | algorithm | p | threads | speedup vs main | main vs sklearn | this PR vs sklearn |
    |---|---|---|---|---|---|
    | kd_tree | 1.5 | 1 | 2.72x | 1.46 | 3.98 |
    | kd_tree | 1.5 | 8 | 2.65x | 11.35 | 30.03 |
    | kd_tree | 1.5 | 56 | 2.47x | 67.15 | 165.56 |
    | kd_tree | 2.7 | 1 | 2.36x | 1.07 | 2.54 |
    | kd_tree | 2.7 | 56 | 2.15x | 51.47 | 110.76 |
    | kd_tree | 3 | 1 | 4.02x | 1.40 | 5.61 |
    | kd_tree | 3 | 8 | 3.87x | 10.94 | 42.38 |
    | kd_tree | 3 | 56 | 3.34x | 65.99 | 220.52 |
    | kd_tree | 1 (control) | 1 | 1.01x | 2.78 | 2.81 |
    | kd_tree | 1 (control) | 8 | 1.01x | 21.02 | 21.22 |
    | kd_tree | 1 (control) | 56 | 1.01x | 60.69 | 61.04 |
    | kd_tree | 2 (control) | 1 | 1.00x | 3.53 | 3.53 |
    | kd_tree | 2 (control) | 8 | 1.00x | 22.90 | 22.93 |
    | kd_tree | 2 (control) | 56 | 1.03x | 61.73 | 63.52 |
    | ball_tree | 1.5 | 1 | 2.70x | 0.58 | 1.56 |
    | ball_tree | 1.5 | 8 | 2.78x | 3.87 | 10.74 |
    | ball_tree | 1.5 | 56 | 3.27x | 19.95 | 65.26 |
    | ball_tree | 2.7 | 1 | 2.47x | 0.35 | 0.86 |
    | ball_tree | 2.7 | 56 | 2.89x | 12.29 | 35.51 |
    | ball_tree | 3 | 1 | 4.01x | 0.38 | 1.54 |
    | ball_tree | 3 | 8 | 4.12x | 2.65 | 10.93 |
    | ball_tree | 3 | 56 | 4.70x | 13.35 | 62.75 |
    | ball_tree | 1 (control) | 1 | 0.98x | 1.49 | 1.46 |
    | ball_tree | 1 (control) | 8 | 0.98x | 15.08 | 14.82 |
    | ball_tree | 1 (control) | 56 | 1.00x | 56.18 | 55.93 |
    | ball_tree | 2 (control) | 1 | 1.01x | 1.26 | 1.28 |
    | ball_tree | 2 (control) | 8 | 1.01x | 11.71 | 11.87 |
    | ball_tree | 2 (control) | 56 | 1.00x | 48.79 | 48.75 |
    | brute | 1.5 | 1 | 7.25x | 0.38 | 2.76 |
    | brute | 1.5 | 8 | 6.24x | 0.92 | 5.72 |
    | brute | 1.5 | 56 | 4.71x | 1.38 | 6.47 |
    | brute | 2.7 | 1 | 8.77x | 0.26 | 2.26 |
    | brute | 2.7 | 56 | 5.65x | 0.97 | 5.46 |
    | brute | 3 | 1 | 10.00x | 0.26 | 2.57 |
    | brute | 3 | 8 | 8.32x | 0.63 | 5.28 |
    | brute | 3 | 56 | 6.42x | 0.98 | 6.32 |
    | brute | 1 (control) | 1 | 1.00x | 1.07 | 1.07 |
    | brute | 1 (control) | 8 | 1.00x | 3.70 | 3.68 |
    | brute | 1 (control) | 56 | 1.01x | 6.75 | 6.80 |
    | brute | 2 (control) | 1 | 1.00x | 1.20 | 1.20 |
    | brute | 2 (control) | 8 | 1.01x | 4.22 | 4.25 |
    | brute | 2 (control) | 56 | 1.00x | 7.19 | 7.20 |

    The brute rows come from the final commit. The tree rows come from the commit before it, which only differed in how fillFullDistMatrix treats non-Minkowski metrics; the tree paths do not use that function.

    Remaining gap: ball_tree with a p that is not a multiple of 1/2 (2.7 here) is still 0.86x scikit-learn single-threaded. Each tree distance still needs one vPowx call for its coordinates and one sPowx call for its root.

    🤖 Generated with Claude Code

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: Vika-F Review duration: 48202m
    All Vouch review confidence checks passed
    Residual risk 0.33 = Change Risk (0.59) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.59 · 650 lines diff across 8 files
    0.59
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    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.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #3895"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.7% 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: 59% (MEDIUM) · Higher = larger blast radius
    59%
    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: 44.7% · Lower = rushed or shallow review
    44.7%
    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: 32.6% · Safe to merge
    32.6%