Skip to content

perf: welford's algorithm for mean-var aggregation#4147

Open
ilan-gold wants to merge 4 commits into
mainfrom
ig/welford
Open

perf: welford's algorithm for mean-var aggregation#4147
ilan-gold wants to merge 4 commits into
mainfrom
ig/welford

Conversation

@ilan-gold

Copy link
Copy Markdown
Contributor

From discussions with @zboldyga.

This should then in theory be reused with #4143 instead of its custom moments calculation

  • Closes #
  • Tests included or not required because:

@ilan-gold ilan-gold added this to the 1.12.2 milestone Jun 8, 2026
@ilan-gold ilan-gold changed the title perf: welford's algorithm for mean-var perf: welford's algorithm for mean-var aggregation Jun 8, 2026
out[cat, col] += data.data[j]


@njit

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We should make these nogil or provide an option for fau to provide nogil njit

@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.60%. Comparing base (0ac3337) to head (48230af).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4147      +/-   ##
==========================================
- Coverage   79.61%   79.60%   -0.02%     
==========================================
  Files         120      120              
  Lines       12786    12780       -6     
==========================================
- Hits        10180    10173       -7     
- Misses       2606     2607       +1     
Flag Coverage Δ
hatch-test.low-vers 78.84% <100.00%> (-0.01%) ⬇️
hatch-test.pre 79.43% <100.00%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/scanpy/get/_aggregated.py 92.66% <100.00%> (-0.65%) ⬇️
src/scanpy/get/_kernels.py 100.00% <ø> (ø)

@ilan-gold ilan-gold requested a review from flying-sheep June 8, 2026 12:24

@zboldyga zboldyga left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ilan-gold I reviewed since this plays into our other work

I don't see any issues with the welfords implementation, lgtm!

and overall I'm new with njit, OMP, TBB. But I see your point about nogil, and was able to create some scenarios that trigger that path. So agreed on the suggestion... I also found that it does also slightly improve the chan njit work when fau falls back on the serial build, and I guess it would generally fix any of these types of things across scanpy, so maybe that does belong in fau?

There's probably some threading stuff I'm not fully grasping with scanpy, fau, those libraries yet though; I will be more confident in that in a few weeks. I figure I will revisit threading and numba as a whole as part of building a good understanding of these libraries, and if I find any thread issues throughout scanpy/fau I will raise them at that point.

@ilan-gold

Copy link
Copy Markdown
Contributor Author

so maybe that does belong in fau?

Yes the issue with nogil is that it means your code is no longer threadsafe with respect to its inputs (in a certain mental model of things, I guess). So introducing this change needs some though - I don't think anything in FAU actually alters its inputs though so we should be good.

There's probably some threading stuff I'm not fully grasping with scanpy, fau, those libraries yet though; I will be more confident in that in a few weeks. I figure I will revisit threading and numba as a whole as part of building a good understanding of these libraries, and if I find any thread issues throughout scanpy/fau I will raise them at that point.

That would be amazing because it is something we have (clearly) struggled with

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.

2 participants