← Back to Pull requests

fix(hdbscan): give each medoid task its own distance tile #3883

Closed
unknown closed this pull request without merging on Oct 15, 2026 12:03 UTC
View on GitHub
unknown unknown commented on Oct 15, 2026 09:06 UTC
Author

Description

HDBSCAN medoids were not deterministic: on some machines a fit occasionally returned a different member of a cluster as its medoid. Labels, probabilities and centroids were not affected.

Cause. computeMedoids in hdbscan_centers_impl.i runs one task per (cluster, row block) and gave each thread one TlsMem distance tile. Inside a task, EuclideanDistances::finalize calls MKL's vSqrt, and under MKL's TBB threading layer that call can run in parallel. While the thread waits on it, TBB may start another medoid task on the same thread, and that task overwrites the shared tile. The interrupted task then scores its members against another cluster's distances.

Fix. Each task allocates its own tile of iCount x min(colBlock, cluster size), so a task started on the same thread can no longer overwrite it.

Where it reproduces.

  • GitHub runners with AMD EPYC 9V74 / 9V45 (Zen 4 / Zen 5) at default settings. It was seen in the scikit-learn-intelex HDBSCAN PR (uxlfoundation/scikit-learn-intelex#3124) and reproduced there with the oneDAL C++ API alone: about 6% of fits on today's nightly. It never happened on EPYC 7763 or with 1 or 2 TBB threads.

  • On Intel with MKL_CBWR=COMPATIBLE, which also makes that sqrt run in parallel: 52 to 74 bad fits out of 2000 before the fix, 0 out of 9000 after.

  • I confirmed the cause by instrumenting the kernel: a medoid task was entered while another task on the same thread was still inside finalize, and only that task's tile was wrong.
  • Test. "hdbscan: medoids are the same on every compute" repeats a fit on eight symmetric lattices, where each lattice center is the medoid, and checks every medoid on every run. Without the fix it fails under MKL_CBWR=COMPATIBLE on 4 CPUs (2 of 3 runs). With the fix it passed 10 of 10 runs. With the default MKL settings on Intel it passes either way, because there MKL runs that sqrt serially.

    Other kernels that use a TlsMem buffer across PairwiseDistances::finalize inside threader_for may have the same exposure; that is left for a separate change.

    ---

    <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 docs 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.

  • I have extended testing suite if new functionality was introduced in this PR.
  • Performance

  • I have provided justification why performance and/or quality metrics have changed or why changes are not expected. One tile allocation per task replaces one per thread; a task already computes a full tile of distances, so the allocation is not expected to be measurable.
  • </details>

    🤖 Generated with Claude Code

    Diff: 414 lines (+310 / -104) across 5 files Reviewer: Vika-F Review duration: 177m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 414 lines diff across 5 files
    0.45
    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.361
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3883"
    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: 44.9% (MEDIUM) · Higher = larger blast radius
    44.9%
    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: 17.7% · Safe to merge
    17.7%