Skip to content

Add an optional Numba cosine backend - #141

Closed
afermg wants to merge 1 commit into
perf/cosine-pairsfrom
perf/numba-cosine
Closed

afermg wants to merge 1 commit into
perf/cosine-pairsfrom
perf/numba-cosine

Conversation

@afermg

@afermg afermg commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

Stack

Summary

  • add copairs[numba] as an optional extra
  • add backend="numpy" | "numba" to regular and multilabel AP, defaulting to NumPy
  • lazily import Numba only when explicitly selected
  • run serial @njit(nogil=True, fastmath=False, cache=True) range kernels through the existing Python thread pool, avoiding nested native pools
  • reproduce NumPy's exact fast-axis pairwise reduction tree, including 8-way accumulation and 128-element recursive blocks
  • provide actionable missing-extra errors without masking incompatible/transitive import failures

The Numba backend initially supports only exact distance="cosine"; unsupported combinations fail explicitly rather than silently falling back.

Benchmark

Synthetic 5,000 × 196, 100,000 pairs, four workers, exact output digest:

Stage Time
Shared cosine preparation 15.82 s
NumPy pair kernel, median 68.14 s
Numba first compile 0.681 s
Numba warm median 0.00713 s
Fresh-process disk-cache hit 0.144 s

Warm kernel speedup was ~9,556× on the heavily contended benchmark host. Including the shared preparation stage, the measured first-call pipeline portion improved about 5.1×, and the warm portion about 5.3×. Absolute times are noisy because the host was simultaneously running two 95-worker JUMP-lite jobs.

Numerical contract

  • bitwise-exact float32/float64 output across reduction boundaries and adversarial cancellation cases
  • verified across NumPy 1.26.4, 2.0.2 source, and 2.3.5
  • exact regular and multilabel AP/tie output
  • established float32 similarity boundary retained

Validation

  • 47 focused Numba/AP tests passed after rebasing onto Precompute cosine normalization for indexed pairs #138
  • 6 default/no-Numba tests passed; 17 optional tests skipped appropriately
  • direct boundary/randomized widths through 4,097 features
  • cold and warm disk-cache process checks
  • Ruff, lock, and diff checks passed
  • independent follow-up review: OK

@afermg

afermg commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

Closing this alternate optional Numba backend in favour of Alex’s #154 (split from #150).

@afermg afermg closed this Nov 1, 2026
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.

1 participant