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
I was playing around with some visualization on a large dataset, when I noticed some surprisingly high memory usage. I think I've narrowed it down to unexpected memory growth from taking views:
My assumption here being: taking a view shouldn't cause noticeable growth in memory usage. I'm pretty sure it's not just how memory_profiler is counting objects, since top and ActivityMonitor pick this up as well.
Thank you for pointing this out! It's surprising to me. I extensively memory-profiled when introducing the functionality about a year ago.
Right now, I can only think of one thing that would cause this behavior as a result of the design of AnnData being not mature enough: the .uns annotations are deep copied... everything else uses numpy's views in the background.
The other reason for views, besides saving memory (if they work properly) is that one should be able to write to the data matrix of the underlying object, especially in backed mode, that is
In the example I gave, I don't think there was anything in the uns field. Just to be sure, I've run it again with a AnnData object that definitely doesn't have anything in the uns field and gotten similar results.
Question about the syntax for views. What should happen in the following code?
CPU times: user 35.7 ms, sys: 61.5 ms, total: 97.2 ms
Wall time: 95.5 ms
Object size: 396.298382M
CPU times: user 132 ms, sys: 147 ms, total: 278 ms
Wall time: 277 ms
Object size: 592.445935M
CPU times: user 165 ms, sys: 203 ms, total: 369 ms
Wall time: 368 ms
Object size: 788.593488M
CPU times: user 201 ms, sys: 261 ms, total: 462 ms
Wall time: 462 ms
Object size: 984.741041M
CPU times: user 265 ms, sys: 276 ms, total: 542 ms
Wall time: 542 ms
Object size: 1180.888594M
CPU times: user 313 ms, sys: 342 ms, total: 655 ms
Wall time: 656 ms
Object size: 1377.036147M
CPU times: user 356 ms, sys: 378 ms, total: 734 ms
Wall time: 733 ms
Object size: 1573.1837M
CPU times: user 380 ms, sys: 463 ms, total: 843 ms
Wall time: 841 ms
Object size: 1769.331253M
CPU times: user 432 ms, sys: 485 ms, total: 917 ms
Wall time: 917 ms
Object size: 1965.478806M
CPU times: user 496 ms, sys: 683 ms, total: 1.18 s
Wall time: 1.18 s
Object size: 2161.626359M
Ok, something really shady is going on, which I was not aware of. I'll need to take a deeper look at views of AnnData. Or, @Koncopd, do you have some bandwidth to shed some light on this?
I've been doing a little bit of digging on this, and have some suspicious about what's causing it.
First, a view of a view stores it's parent view in _adata_ref. This means those intermediates won't get gc-ed leading to the linear increase in memory usage in the example above. Demo:
Second, I think that each view can be getting a copy of the expression matrix. In particular, line 752 will make a copy when the array is accessed with a boolean array
importnumpyasnpimportscanpyassca=sc.AnnData(np.ones((5000, 5000)))
# When view is taken with a slice, no additional memory is usedsc.logging.print_memory_usage() # Memory usage: current 0.28 GB, difference +0.28 GBv1=a[0:5000, 0:5000]
sc.logging.print_memory_usage() # Memory usage: current 0.28 GB, difference +0.00 GBv2=v1[0:5000, 0:5000]
sc.logging.print_memory_usage() # Memory usage: current 0.28 GB, difference +0.00 GB# Taken with a boolean array, memory use increasesv1=a[np.ones(5000, dtype=bool), 0:5000]
sc.logging.print_memory_usage() # Memory usage: current 0.38 GB, difference +0.09 GBv2=v1[np.ones(5000, dtype=bool), 0:5000]
sc.logging.print_memory_usage() # Memory usage: current 0.47 GB, difference +0.09 GB
My idea for how this could be solved is not to bother actually subsetting X until it's accessed, and just storing the subsetting index until then. This would be updated on every subsequent subset, and should always be an index into the "actual" AnnData.
Great. I'm pretty sure what needs to be done is write out how to resolve all the different indexing types (i.e. slice of a slice should be a slice, int array of a slice should be an int array, etc.).
Activity
falexwolf commentedon Sep 17, 2018
Thank you for pointing this out! It's surprising to me. I extensively memory-profiled when introducing the functionality about a year ago.
Right now, I can only think of one thing that would cause this behavior as a result of the design of AnnData being not mature enough: the
.unsannotations are deep copied... everything else uses numpy's views in the background.The other reason for views, besides saving memory (if they work properly) is that one should be able to write to the data matrix of the underlying object, especially in backed mode, that is
should modify the original data (see here).
Anyways, this is a severe issue and I'll definitely fix it soon if it persists.
ivirshup commentedon Oct 4, 2018
In the example I gave, I don't think there was anything in the
unsfield. Just to be sure, I've run it again with a AnnData object that definitely doesn't have anything in theunsfield and gotten similar results.Question about the syntax for views. What should happen in the following code?
My assumption is the change on the view should be replicated in the base object. That is, both print statements should return 0.
grst commentedon Oct 29, 2018
Observed the same when filtering an adata object. For demonstration purposies, here, the same filter is applied over and over again:
falexwolf commentedon Nov 5, 2018
Ok, something really shady is going on, which I was not aware of. I'll need to take a deeper look at views of AnnData. Or, @Koncopd, do you have some bandwidth to shed some light on this?
ivirshup commentedon May 10, 2019
I've been doing a little bit of digging on this, and have some suspicious about what's causing it.
First, a view of a view stores it's parent view in
_adata_ref. This means those intermediates won't get gc-ed leading to the linear increase in memory usage in the example above. Demo:Second, I think that each view can be getting a copy of the expression matrix. In particular, line 752 will make a copy when the array is accessed with a boolean array
https://github.com/theislab/anndata/blob/fc7f3cb06c191aff4fcad6af88f0c3012d7d1a65/anndata/base.py#L748-L752
Here's an example of the memory increasing:
My idea for how this could be solved is not to bother actually subsetting X until it's accessed, and just storing the subsetting index until then. This would be updated on every subsequent subset, and should always be an index into the "actual" AnnData.
falexwolf commentedon Jun 4, 2019
I don't know whether we discussed this on Slack already or not, Isaac. But your idea is perfect. I think we also discussed on Slack that we don't want
_adata_refto ever be a view, right? So that we don't get this recursion (https://scanpy.slack.com/archives/CHB1M6X5H/p1557405440002600?thread_ts=1557396576.001600&cid=CHB1M6X5H). Then the memory increase should be gone.ivirshup commentedon Jun 6, 2019
Great. I'm pretty sure what needs to be done is write out how to resolve all the different indexing types (i.e. slice of a slice should be a slice, int array of a slice should be an int array, etc.).