Skip to content

Speed up common median reference on channel-sized axes - #4836

Open
JESUSROYETH wants to merge 2 commits into
SpikeInterface:mainfrom
JESUSROYETH:radar/perf-common-median-reference
Open

JESUSROYETH wants to merge 2 commits into
SpikeInterface:mainfrom
JESUSROYETH:radar/perf-common-median-reference

Conversation

@JESUSROYETH

Copy link
Copy Markdown
Contributor

Summary

Common median reference currently calls np.median for each trace chunk. For the short channel axis used here, NumPy's full sort path turns out faster than its partition-based median path.

This adds a private reduction helper: one or two numeric channels use their equivalent mean, finite numeric arrays with at least four channels sort and average the middle value(s), and three-channel, NaN, empty, or non-numeric inputs keep np.median. No public API changes.

Benchmark

I ran a complete cold-process run_sorter("tridesclous2"), 20-minute/32-channel recording, n_jobs=1, on an Intel Sapphire Rapids box, five alternating pairs:

before after change
full sorter 170.2 s 150.5 s −11.6% (19.7 s)

Each of the five paired runs improved, 10.02–18.97%. On SpikeInterface's public 384-channel NHP snippet, the CMR reduction itself comes out 3.89× faster, with identical output hashes.

I ran the common-reference tests too: 16 passed, 1 skipped, Black/style clean. This is complementary to #4564 — it cuts default single-thread work without adding threading.

@alejoe91 alejoe91 added this to the 0.106.0 milestone Oct 7, 2026
@alejoe91 alejoe91 added the preprocessing Related to preprocessing module label Oct 7, 2026
@alejoe91 alejoe91 modified the milestones: 0.106.0, 0.105.1 Oct 7, 2026
galenlynch added a commit to galenlynch/spikeinterface that referenced this pull request Oct 13, 2026
np.sort vectorises 32- and 64-bit sorts, and on the default chain's
float32 output it beats the counting kernel (29 vs 39 ms per 1 s of
384 channels) and np.median (100 ms). This is the approach of SpikeInterface#4836.
It also covers float64 and needs no numba. The int16 histogram
kernel stays, since np.sort is 2.6x slower than np.median there.
galenlynch added a commit to galenlynch/spikeinterface that referenced this pull request Oct 14, 2026
Sorting float rows for the median, in beec896, follows @JESUSROYETH's
approach in SpikeInterface#4836.

Co-authored-by: JESUSROYETH <[email protected]>
@alejoe91

Copy link
Copy Markdown
Member

Partly: float32 traces are ~55% faster (384 ch 3.29 s to 1.46 s, 32 ch 0.39 s to 0.18 s; the author reported 3.9x on 384 ch), outputs identical, but int16 traces, which is what raw Neuropixels data is, are slower: +165% on 384 ch (2.5 s to 6.6 s) and +32% on 32 ch. A micro-benchmark on this machine (Ryzen 9 5950X, numpy 2.5.5) shows why: np.sort of int16 (30000x384, axis=1) takes 0.21 s vs 0.068 s for np.median, whereas float32 sorts in 0.033 s vs 0.107 s, so the sort path wins only for float; consider using it for floats only, or checking int16 on the CPUs the author used. Not covered: the 3-channel, NaN and 1-2 channel branches (3 changed lines missed in smoke mode, all module-level or fallback), and the author's full tridesclous2 run.

A/B vs main: PR #4836 head 012e0893f6 vs its base on main 1d7bacaa2a, 3 alternating run(s) per side, values are medians (the base is 3 commits behind current main).

main this PR change
whole run() (4 variants, 30 s slice, 5 timed passes each) (s) 33.46 44.78 +33.9%
peak memory, PSS of process tree (GB) 0.3563 0.3502 -1.7%
float32_32ch, best of 5 passes over 30 x 1 s chunks (s) 0.3863 0.1808 -53.2%
float32_384ch, time (s) 3.288 1.461 -55.6%
int16_32ch, time (s) 0.3072 0.4066 +32.4%
int16_384ch, time (s) 2.508 6.642 +164.8%

Outputs identical to main for all 8 compared arrays (floats within rtol=1e-05, atol=1e-06).

@JESUSROYETH did you use float or ints in your tests?

System
  • CPU: AMD Ryzen 9 5950X 16-Core Processor, 32 logical cores, 63 GiB RAM
  • OS: Linux-6.8.0-149-generic-x86_64-with-glibc2.39, Python 3.13.2
  • numpy 2.5.5, scipy 1.18.1, numba 0.67.0, zarr 3.4.2, numcodecs 0.17.1, probeinterface 0.5.1, neo 0.15.0.dev0, torch 2.14.0

@JESUSROYETH

Copy link
Copy Markdown
Contributor Author

Hi @alejoe91, thanks for checking this. The reported timings were for float32 at the common-reference stage. The public 384-channel snippet was bandpass-filtered with dtype="float32", and the full tridesclous2 run also uses float32 before common referencing through its default preprocessing.
int16 was included in the correctness tests, but those tests do not establish a performance gain. Given your results, I agree that integer inputs should keep np.median. The PR description should also make the float32 benchmark scope clear.

@alejoe91

Copy link
Copy Markdown
Member

Hi @alejoe91, thanks for checking this. The reported timings were for float32 at the common-reference stage. The public 384-channel snippet was bandpass-filtered with dtype="float32", and the full tridesclous2 run also uses float32 before common referencing through its default preprocessing. int16 was included in the correctness tests, but those tests do not establish a performance gain. Given your results, I agree that integer inputs should keep np.median. The PR description should also make the float32 benchmark scope clear.

Or use the numba kernel from #4854 :)

@alejoe91

Copy link
Copy Markdown
Member

To wrap this up, let's use the new function for float types only then!

@alejoe91

Copy link
Copy Markdown
Member

@JESUSROYETH maybe we could simply go with #4854 , since it's basically the same implementation for float and a numba version for ints. Is it ok for you?

@JESUSROYETH

Copy link
Copy Markdown
Contributor Author

Yes @alejoe91, that's fine with me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preprocessing Related to preprocessing module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants