Skip to content

Optimize shared distance calculations - #122

Merged
AP6YC merged 1 commit into
developfrom
optimize-distance-kernels
Sep 30, 2026
Merged

AP6YC merged 1 commit into
developfrom
optimize-distance-kernels

Conversation

@NiklasMelton

@NiklasMelton NiklasMelton commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • use SciPy's compiled direct-coordinate kernel for NumPy pairwise centroid distances
  • declare SciPy as a direct dependency while retaining the existing Numba kernel
  • share an allocation-light off-diagonal minimum across XB, GD43, and GD53
  • vectorize GD53 dispersion matrices and large-cluster row updates

Performance

Five alternating trials; median trial-average time; complete construction and computation included; OPENBLAS_NUM_THREADS=1.

10,000 samples, 2 features, 10 clusters

CVI Batch parent → PR Batch speedup Stream parent → PR Stream speedup
DB 0.573 → 0.552 ms 1.04x 289.612 → 288.916 ms 1.00x
XB 0.561 → 0.532 ms 1.06x 179.634 → 118.315 ms 1.52x
GD43 0.560 → 0.532 ms 1.05x 163.681 → 142.544 ms 1.15x
GD53 0.543 → 0.535 ms 1.01x 145.446 → 126.515 ms 1.15x
PS 0.528 → 0.513 ms 1.03x 465.225 → 466.041 ms 1.00x

4,096 samples, 8 features, 128 clusters

CVI Batch parent → PR Batch speedup Stream parent → PR Stream speedup
DB 2.733 → 2.299 ms 1.19x 4692.146 → 4687.501 ms 1.00x
XB 1.535 → 1.098 ms 1.40x 246.667 → 76.705 ms 3.22x
GD43 1.529 → 1.096 ms 1.39x 148.574 → 95.935 ms 1.55x
GD53 2.540 → 1.099 ms 2.31x 175.962 → 106.648 ms 1.65x
PS 1.640 → 1.245 ms 1.32x 1896.778 → 1901.027 ms 1.00x

scikit-learn reference

Workload CVI DB scikit-learn DB Relative performance
10k / 2D / 10 clusters 0.552 ms 0.937 ms CVI 1.70x faster
4096 / 8D / 128 clusters 2.299 ms 6.796 ms CVI 2.96x faster

The DB definitions differ: this package uses squared distances while scikit-learn uses Euclidean distances.

Validation

  • 813 passed, 140 optional-JAX skips
  • large-offset, empty, singleton, NumPy, and Numba distance tests pass
  • GD53 matrices and off-diagonal reductions match direct definitions

Issue

Closes #131

@NiklasMelton
NiklasMelton force-pushed the optimize-distance-kernels branch from d3e29b8 to 5c10cee Compare September 30, 2026 15:49
@NiklasMelton
NiklasMelton requested a review from AP6YC September 30, 2026 15:49
@AP6YC AP6YC self-assigned this Sep 30, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me! If it's done fast in scipy, I'm fine to add it as a reasonable mainline dependency.

Comment thread pyproject.toml

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an unreasonable dep addition, especially for the speedups from minimum_off_diagonal and distance kernels.

@AP6YC
AP6YC merged commit 0452d69 into develop Sep 30, 2026
7 checks passed
@AP6YC
AP6YC deleted the optimize-distance-kernels branch September 30, 2026 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize shared distance calculations

2 participants