You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This always warns about memory usage then fails on my dev machine with 64 gb of memory. However, I'm able to do plenty of other operations with an array this size (e.g. PCA, simple reductions). To me, a tree reduction here should be more than capable of handling this size of array.
Is this just me and my compute being odd, or do I have an incorrect expectation here?
You're starting with 1.5GiB chunk sizes on X. I would reduce that to the 200MB range. The bottleneck is usually numpy_groupies here. You should see input_validation prominently in the dask flamegraph
So I would also try installing numbagg. It'll be a bit slow to compile but should be faster and make less memory copies.
Running this locally, I also spot a dask scheduling bug where it doesn't treat normal as a data generating task and runs way too many of them initially before reducing that data. Can you open a dask issue please?
I also spot a dask scheduling bug where it doesn't treat normal as a data generating task and runs way too many of them initially before reducing that data.
In my real world use case, I get this just loading data from a zarr store.
I think this is a dask scheduling issue.
Me too, but I'm not sure why flox seems to be triggering it. In the dask issue I show that other tree aggregations with this array (X.sum(axis=0)) seem fine.
you'll see it automatically chooses method="cohorts" and it just works albeit with spilling. There seems be a densification of each sparse block that could be improved. I'll have to look in to it.
EDIT: your choice of reindex=True was using way more memory than necessary to run this reduction. You have 2390 unique groups, but there are O(10) groups in the output chunks with cohorts. That means reindex=True was inflating each block to ~ 2390/~10 = 200X larger than it needed to be.
plt.hist(res.chunks[-1])
EDIT2: I guess you're densifying on your own...
EDIT3: Memory issues can be controlled by using numbagg and nansum. It's also 5X faster (approx). I guess we should look in to how to make flox just handle sparse matrices directly.
How would flox handle sparse matrices directly? graphblas sparse matrix product? We can construct the sparse matrix from by really quickly (we know where the ones are already).
graphblas can be nice, since we can pretty easily define a semi-ring for many tasks here (though, not so sure about median) and it's very fast. But it's always going to be only 2d, and is GPL.
In scanpy right now we are just doing matrix multiplication (code) for in-memory aggregation, but I suspect will end up using some numba kernels since we can get better performance and numerical stability.
@Intron7 may have some thoughts here as he wrote some CUDA kernels for these aggregations in memory.
graphblas can be nice, since we can pretty easily define a semi-ring for many tasks here (though, not so sure about median) and it's very fast. But it's always going to be only 2d, and is GPL.
I was thinking of using it through python-graphblas which is Apache-2.
n scanpy right now we are just doing matrix multiplication (code) for in-memory aggregation, but I suspect will end up using some numba kernels since we can get better performance and numerical stability.
Oh nice! We could move your code into flox as an "engine" or alternatively tie in to scanpy with engine="scanpy". I prefer the former, I think, it'd be nice to not need an optional dependency for fairly simple code.
For reference, "engine"s handle the in-memory part of the aggregation. And we already have the codes ready.
I was thinking of using it through python-graphblas which is Apache-2.
The wrapper is apache-2, but it pulls in graphblas which is GPL. So effectively same distribution issues is my understanding.
We could move your code into flox as an "engine"
That's how I've been thinking this sort of thing would work.
Would it be weird to have a "sparse" engine that only works with sparse chunks? Or would you want to add it to another engine?
your choice of reindex=True was using way more memory than necessary to run this reduction. You have 2390 unique groups, but there are O(10) groups in the output chunks with cohorts. That means reindex=True was inflating each block to ~ 2390/~10 = 200X larger than it needed to be.
I am trying to remember why I was using reindex=True. I think it may have just been for a memory usage I could estimate as I had a lot of trouble getting this to work without running out of memory at all.
FWIW, if there is sparse support, then we can increase the chunk sizes, which means we end up hitting more groups.
Also, we don't really have set expectations about the distribution of groups per chunk, so I'd like to be sure this works in the worst case where the labels are well shuffled.
Would it be weird to have a "sparse" engine that only works with sparse chunks?
Don't think so. In general, I'd like users to not set it at all (the default is None now). Constructing a sparse "weights" matrix is another way to do a groupby anyway (Ken Iversion stuck it in his "notation as a tool of thought" paper too!) and flox has scipy as a dependency already.
Also, we don't really have set expectations about the distribution of groups per chunk, so I'd like to be sure this works in the worst case where the labels are well shuffled.
Yeah that would be nice. But it would also be good to get a real example. We might instead look to speed up the reindex=False case to reduce memory usage, and have that be chosen by default.
My agents also whipped up a python-graphblas engine for flox. It's pretty fast and low memory, and looks like a good idea to me if there's interest. I can continue this part of the discussion in #443.
@ivirshup, given the amount of work you do around sparse things (such as scverse), I would kindly ask you to please not dissuade the use of GraphBLAS due to licensing. I think you are thinking of SuiteSparse (which is different from SuiteSparse:GraphBLAS!) from Tim Davis, which is GPL. Heh, if I may ask: where else would python-graphblas be a good fit that you know of but avoided due to misconceptions about the license? (I guess I should make the permissive licensing of python-graphblas and its dependencies more prominent).
fwiw, python-graphblas is becoming more active again (I'm even working on a Tensor class and an Array API namespace)
Activity
dcherian commentedon Mar 26, 2024
You're starting with 1.5GiB chunk sizes on
X. I would reduce that to the 200MB range. The bottleneck is usuallynumpy_groupieshere. You should seeinput_validationprominently in the dask flamegraphSo I would also try installing
numbagg. It'll be a bit slow to compile but should be faster and make less memory copies.dcherian commentedon Mar 26, 2024
Running this locally, I also spot a dask scheduling bug where it doesn't treat
normalas a data generating task and runs way too many of them initially before reducing that data. Can you open a dask issue please?dcherian commentedon Mar 26, 2024
Ah I keep forgetting this
numbaggonly helps with nan-skipping aggregations, so it won't really help here.I think this is a dask scheduling issue.
ivirshup commentedon Mar 26, 2024
In my real world use case, I get this just loading data from a zarr store.
Me too, but I'm not sure why flox seems to be triggering it. In the dask issue I show that other tree aggregations with this array (
X.sum(axis=0)) seem fine.dcherian commentedon Mar 26, 2024
Your last comment is important context! (the zarr bit in particular). I would add that to the other issue
dcherian commentedon Jun 6, 2024
Reproducer here: https://gist.github.com/ivirshup/eb4f5beb1bb33724b8c11bd0eacf03a6 from dask/dask#11026 (comment)
This works on my laptop with 32GB RAM with some spilling
If you turn on logging with
you'll see it automatically chooses
method="cohorts"and it just works albeit with spilling. There seems be a densification of each sparse block that could be improved. I'll have to look in to it.EDIT: your choice of
reindex=Truewas using way more memory than necessary to run this reduction. You have2390unique groups, but there are O(10) groups in the output chunks with cohorts. That meansreindex=Truewas inflating each block to ~2390/~10 = 200X larger than it needed to be.EDIT2: I guess you're densifying on your own...
EDIT3: Memory issues can be controlled by using
numbaggandnansum. It's also 5X faster (approx). I guess we should look in to how to makefloxjust handle sparse matrices directly.[-]Weirdly high memory usage with flox[/-][+]Handling sparse matrices[/+]dcherian commentedon Jun 6, 2024
How would flox handle sparse matrices directly?
graphblassparse matrix product? We can construct the sparse matrix frombyreally quickly (we know where the ones are already).ivirshup commentedon Jun 6, 2024
graphblascan be nice, since we can pretty easily define a semi-ring for many tasks here (though, not so sure aboutmedian) and it's very fast. But it's always going to be only 2d, and is GPL.In
scanpyright now we are just doing matrix multiplication (code) for in-memory aggregation, but I suspect will end up using some numba kernels since we can get better performance and numerical stability.@Intron7 may have some thoughts here as he wrote some CUDA kernels for these aggregations in memory.
dcherian commentedon Jun 6, 2024
I was thinking of using it through
python-graphblaswhich is Apache-2.Oh nice! We could move your code into flox as an "engine" or alternatively tie in to scanpy with
engine="scanpy". I prefer the former, I think, it'd be nice to not need an optional dependency for fairly simple code.For reference, "engine"s handle the in-memory part of the aggregation. And we already have the
codesready.ivirshup commentedon Jun 6, 2024
The wrapper is apache-2, but it pulls in graphblas which is GPL. So effectively same distribution issues is my understanding.
That's how I've been thinking this sort of thing would work.
Would it be weird to have a "sparse" engine that only works with sparse chunks? Or would you want to add it to another engine?
I am trying to remember why I was using reindex=True. I think it may have just been for a memory usage I could estimate as I had a lot of trouble getting this to work without running out of memory at all.
FWIW, if there is sparse support, then we can increase the chunk sizes, which means we end up hitting more groups.
Also, we don't really have set expectations about the distribution of groups per chunk, so I'd like to be sure this works in the worst case where the labels are well shuffled.
dcherian commentedon Jun 6, 2024
Don't think so. In general, I'd like users to not set it at all (the default is
Nonenow). Constructing a sparse "weights" matrix is another way to do a groupby anyway (Ken Iversion stuck it in his "notation as a tool of thought" paper too!) andfloxhasscipyas a dependency already.Yeah that would be nice. But it would also be good to get a real example. We might instead look to speed up the
reindex=Falsecase to reduce memory usage, and have that be chosen by default.Support aggregating by sparse arrays.
Support aggregating by sparse arrays.
Support aggregating by sparse arrays.
Support aggregating by sparse arrays.
Support aggregating sparse arrays.
Support aggregating sparse arrays.
eriknw commentedon Nov 29, 2026
Hi, I'm the primary maintainer of python-graphblas. My agents brought this issue to my attention.
The quoted statement above is incorrect. python-graphblas is not GPL nor does it have any GPL dependencies. It is Apache 2. SuiteSparse:GraphBLAS (what it wraps) has no GPL components. It is primarily Apache 2 with some BSD-2 and BSD-3 parts. See the license here: https://github.com/DrTimothyAldenDavis/GraphBLAS/blob/stable/LICENSE
My agents also whipped up a python-graphblas engine for flox. It's pretty fast and low memory, and looks like a good idea to me if there's interest. I can continue this part of the discussion in #443.
@ivirshup, given the amount of work you do around sparse things (such as scverse), I would kindly ask you to please not dissuade the use of GraphBLAS due to licensing. I think you are thinking of SuiteSparse (which is different from SuiteSparse:GraphBLAS!) from Tim Davis, which is GPL. Heh, if I may ask: where else would python-graphblas be a good fit that you know of but avoided due to misconceptions about the license? (I guess I should make the permissive licensing of python-graphblas and its dependencies more prominent).
fwiw, python-graphblas is becoming more active again (I'm even working on a Tensor class and an Array API namespace)