Skip to content

Commit

Permalink
forces Raw move to CPU (#1107)
Browse files Browse the repository at this point in the history
* forces move to CPU

* added raw GPU test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Use qualified function names, plus avoid future scipy deprecations

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Isaac Virshup <[email protected]>
  • Loading branch information
3 people authored Aug 29, 2023
1 parent 87d363e commit 1eaa56c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion anndata/_core/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def __init__(
self._n_obs = adata.n_obs
# construct manually
if adata.isbacked == (X is None):
self._X = X
# Move from GPU to CPU since it's large and not always used
if isinstance(X, (CupyArray, CupySparseMatrix)):
self._X = X.get()
else:
self._X = X
self._var = _gen_dataframe(var, self.X.shape[1], ["var_names"])
self._varm = AxisArrays(self, 1, varm)
elif X is None: # construct from adata
Expand Down
27 changes: 27 additions & 0 deletions anndata/tests/test_gpu.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import pytest
from scipy import sparse

from anndata import AnnData, Raw


@pytest.mark.gpu
Expand All @@ -9,3 +12,27 @@ def test_gpu():
import cupy # This test shouldn't run if cupy isn't installed

cupy.ones(1)


@pytest.mark.gpu
def test_adata_raw_gpu():
from cupyx.scipy import sparse as cupy_sparse
import cupy as cp

adata = AnnData(
X=cupy_sparse.random(500, 50, density=0.01, format="csr", dtype=cp.float32)
)
adata.raw = adata
assert isinstance(adata.raw.X, sparse.csr_matrix)


@pytest.mark.gpu
def test_raw_gpu():
from cupyx.scipy import sparse as cupy_sparse
import cupy as cp

adata = AnnData(
X=cupy_sparse.random(500, 50, density=0.01, format="csr", dtype=cp.float32)
)
araw = Raw(adata)
assert isinstance(araw.X, sparse.csr_matrix)

0 comments on commit 1eaa56c

Please sign in to comment.