compile DynamicColoredNoise IIR loop with numba - #5
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The per-sample Kasdin IIR recursion in
DynamicColoredNoisewas a pure-Pythondouble loop (over input bins × output samples) and the dominant cost in the
velocity→ecephys encoder. This ports the hot loop verbatim to a numba
njitkernel. The recursion is inherently sequential (
output[i]feeds the delayline that produces
output[i+1]), so it can't vectorize over time — compilingthe scalar loop is the right tool.
No behavior change: the white-noise draw stays in NumPy (RNG sequence
preserved) and the MLX/array-api input path is untouched (the loop already ran
on a NumPy view). Channels are independent, so iterating over them inside the
sample loop is equivalent to the old vectorized-over-channels form; non-finite
β channels skip the coefficient update (an EMA-toward-self no-op).
Results
Per-stage encoder benchmark (
bench_cosine_encoder.py, 256ch @ 30 kHz):lfp.noisebeforeEnd-to-end (simulator over LSL, 50 Hz, 256ch):
PINK_NOISEdropped from~0.70 ms/msg to 0.088 ms/msg — no longer the pipeline's longest stage.
Correctness
(floating-point reassociation only: the old code's
np.einsumpole reductionsums in a different order than the scalar loop). Same-seed determinism holds.
tests/unit/). The compiled loop also cuts thesuite's runtime from ~28.5 s to ~2.5 s.
Dependency
Declares
numbaexplicitly (it was already present transitively viaezmsg-event).