Skip to content

Unify the plain-commit and trial add paths - #150

Open
NiklasMelton wants to merge 3 commits into
developfrom
unify-add-paths
Open

NiklasMelton wants to merge 3 commits into
developfrom
unify-add-paths

Conversation

@NiklasMelton

Copy link
Copy Markdown
Collaborator

Closes #148.

What changed

Each incremental index (CH, WB, XB, DB, GD43, GD53, PS, cSIL, rCIP) now has one add implementation. The nine per-index _param_inc methods are gone (about 640 lines). The base _param_inc plans the single label and then applies it, so plain commits, trials and memoized commits all run the same formulas.

  • Shared formulas. Counts, centroids and compactness come from helpers in _trial (centroid_update, compactness_update). They accept either one vector or one row per slot. cSIL's centered moments work the same way through its own _moment_update.
  • Score-only work runs only when scoring. _plan_add_index (CH/WB separation terms, candidate distance or dispersion rows) is now computed only for scored plans. Applying a slot uses the scored row if there is one. Otherwise it computes that slot's row with the same _add_row.
  • Cheap one-label plans. Plain commits plan one label, and those plans keep per-slot values in one-element lists. AddPlan.vectorize() converts them to arrays only before scoring. Building 1-row arrays was the fixed overhead that blocked this in Trial mode, move, and lifecycle methods #147.
  • Same rounding. One-vector updates use np.inner. Rows use np.vecdot, which runs the same inner-product loop per row. With NumPy < 2, the fallback is an exact per-row np.inner loop.

Numerics

Plain streaming commits are bit for bit identical to 0.7.2/develop. I checked full object state and every returned score with strict types for all nine indices. The check covered both backends, d ∈ {1, 3, 8, 13}, K ∈ {2, 7, 40} and 600-sample streams, plus a remove and an uncached move. The trial tests now require exact agreement between plain and memoized commits, so the 1e-12 cross-path tolerance (CROSS_PATH) is removed. test_plain_commits_do_not_use_the_trial_path is replaced by a test that plain commits plan without scoring.

Speed

New benchmarks/benchmark_streaming_add.py times _param_inc per sample against another checkout, alternating subprocess rounds. Results at K=40, d=8, median of 5 rounds, macOS, OPENBLAS_NUM_THREADS=1, with other load on the machine:

index develop (µs) this PR (µs) change
CH 10.58 10.87 +2.7%
WB 10.64 11.03 +3.7%
XB 9.36 10.04 +7.3%
DB 9.34 9.98 +6.7%
GD43 9.62 10.25 +6.6%
GD53 10.40 11.04 +6.2%
PS 4.58 5.08 +10.9%
cSIL 15.83 17.79 +12.4%
rCIP 73.24 75.51 +3.1%

This is not quite "within noise". The remaining cost is Python bookkeeping in plan/apply: about 0.3–0.7 µs per update, and about 2 µs for cSIL. Run-to-run noise was about 2–3%. For comparison, the attempt in #147 cost about 10 µs (+85% for CH). The figures above already include a separate commit that makes centroid_distances call np.add.reduce instead of np.sum. That gives identical results and saves about 0.5 µs per call. Without it the overhead would be roughly 0.5 µs higher. Closing the rest would probably take compiling the one-label update (Numba). The issue lists that as an option, but I left it out of scope here.

Trials did not regress, except cSIL vectorized at about +4%. Per-call trial times (develop → this PR), with 41 candidates / a single label:

  • CH 68→65 / 36→34
  • XB 205→176 / 52→50
  • DB 228→201 / 72→69
  • GD43 220→192 / 57→55
  • GD53 147→92 / 57→56 (rows are now built in one vectorized step)
  • PS 513→459 / 55→54
  • cSIL 423→437 / 62→57
  • rCIP about equal

Testing

  • python -m pytest tests: 1807 passed, 149 skipped (numba installed; JAX tests skipped locally).
  • Bitwise state comparison against develop, as described above.

Each index now has one add implementation. The base _param_inc plans
the single label and applies it, so plain commits, trials and memoized
commits share the same formulas:

- Counts, centroids and compactness come from shared helpers in _trial
  that take one vector or one row per slot.
- Index-specific statistics needed only for scoring (_plan_add_index)
  are computed only when a plan is scored. Applying a slot uses its
  scored row, or computes that one row with the same _add_row.
- One-label plans keep per-slot values in one-element lists, converted
  to arrays only before scoring, to avoid the 1-row array overhead that
  blocked this in #147.
- Inner products use np.inner for one vector and np.vecdot for rows.
  These run the same inner loop, so plain streaming commits stay bit
  for bit identical to 0.7.2 and to memoized commits.

The trial tests now require exact agreement between plain and memoized
commits; the cross-path tolerance is gone. The test that plain commits
avoid the planner is replaced by one that they skip scoring.
np.add.reduce gives identical results without np.sum's Python wrapper, which dominates on the short rows of one streaming update (about 0.5 us per call at K=40, d=8).
Times plain incremental updates per index, optionally against another checkout, alternating subprocess rounds so machine drift affects both.
@codecov

codecov Bot commented Nov 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.93%. Comparing base (2c9c428) to head (b5bf756).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #150      +/-   ##
===========================================
- Coverage    96.34%   95.93%   -0.42%     
===========================================
  Files           22       22              
  Lines         3313     2975     -338     
===========================================
- Hits          3192     2854     -338     
  Misses         121      121              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@NiklasMelton
NiklasMelton requested a review from AP6YC November 17, 2026 22:18

This branch has not been deployed

No deployments
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.

Unify the plain-commit and trial add paths

1 participant