From d531ab22376218785cbf5344c47e61395fdbb6e1 Mon Sep 17 00:00:00 2001 From: Intron7 Date: Mon, 28 Aug 2023 15:57:28 +0200 Subject: [PATCH 1/4] forces move to CPU --- anndata/_core/raw.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/anndata/_core/raw.py b/anndata/_core/raw.py index f18f75bf8..f248b99be 100644 --- a/anndata/_core/raw.py +++ b/anndata/_core/raw.py @@ -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 From 5a8b5ecc78ba4efc0f1edcf7694cc987553a1cc1 Mon Sep 17 00:00:00 2001 From: Intron7 Date: Tue, 29 Aug 2023 12:33:45 +0200 Subject: [PATCH 2/4] added raw GPU test --- anndata/tests/test_gpu.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/anndata/tests/test_gpu.py b/anndata/tests/test_gpu.py index a5f820bd8..127b0a4ee 100644 --- a/anndata/tests/test_gpu.py +++ b/anndata/tests/test_gpu.py @@ -1,5 +1,6 @@ import pytest - +from anndata import AnnData, Raw +from scipy.sparse import isspmatrix_csr @pytest.mark.gpu def test_gpu(): @@ -9,3 +10,19 @@ 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.sparse import random + import cupy as cp + adata = AnnData(X = random(500,50,density=0.01, format='csr', dtype=cp.float32)) + adata.raw = adata + assert isspmatrix_csr(adata.raw.X) + +@pytest.mark.gpu +def test_raw_gpu(): + from cupyx.scipy.sparse import random + import cupy as cp + adata = AnnData(X = random(500,50,density=0.01, format='csr', dtype=cp.float32)) + araw = Raw(adata) + assert isspmatrix_csr(araw.X) From a65639cd1eafbb9966499205f60bec52bb07f69f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 29 Aug 2023 10:34:03 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- anndata/tests/test_gpu.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/anndata/tests/test_gpu.py b/anndata/tests/test_gpu.py index 127b0a4ee..f285a39c4 100644 --- a/anndata/tests/test_gpu.py +++ b/anndata/tests/test_gpu.py @@ -2,6 +2,7 @@ from anndata import AnnData, Raw from scipy.sparse import isspmatrix_csr + @pytest.mark.gpu def test_gpu(): """ @@ -11,18 +12,22 @@ def test_gpu(): cupy.ones(1) + @pytest.mark.gpu def test_adata_raw_gpu(): from cupyx.scipy.sparse import random import cupy as cp - adata = AnnData(X = random(500,50,density=0.01, format='csr', dtype=cp.float32)) + + adata = AnnData(X=random(500, 50, density=0.01, format="csr", dtype=cp.float32)) adata.raw = adata assert isspmatrix_csr(adata.raw.X) + @pytest.mark.gpu def test_raw_gpu(): from cupyx.scipy.sparse import random import cupy as cp - adata = AnnData(X = random(500,50,density=0.01, format='csr', dtype=cp.float32)) + + adata = AnnData(X=random(500, 50, density=0.01, format="csr", dtype=cp.float32)) araw = Raw(adata) assert isspmatrix_csr(araw.X) From 870e3261458375b2c05755f80dfe5a738e539ad0 Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Tue, 29 Aug 2023 13:34:08 +0200 Subject: [PATCH 4/4] Use qualified function names, plus avoid future scipy deprecations --- anndata/tests/test_gpu.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/anndata/tests/test_gpu.py b/anndata/tests/test_gpu.py index f285a39c4..434567ca8 100644 --- a/anndata/tests/test_gpu.py +++ b/anndata/tests/test_gpu.py @@ -1,6 +1,7 @@ import pytest +from scipy import sparse + from anndata import AnnData, Raw -from scipy.sparse import isspmatrix_csr @pytest.mark.gpu @@ -15,19 +16,23 @@ def test_gpu(): @pytest.mark.gpu def test_adata_raw_gpu(): - from cupyx.scipy.sparse import random + from cupyx.scipy import sparse as cupy_sparse import cupy as cp - adata = AnnData(X=random(500, 50, density=0.01, format="csr", dtype=cp.float32)) + adata = AnnData( + X=cupy_sparse.random(500, 50, density=0.01, format="csr", dtype=cp.float32) + ) adata.raw = adata - assert isspmatrix_csr(adata.raw.X) + assert isinstance(adata.raw.X, sparse.csr_matrix) @pytest.mark.gpu def test_raw_gpu(): - from cupyx.scipy.sparse import random + from cupyx.scipy import sparse as cupy_sparse import cupy as cp - adata = AnnData(X=random(500, 50, density=0.01, format="csr", dtype=cp.float32)) + adata = AnnData( + X=cupy_sparse.random(500, 50, density=0.01, format="csr", dtype=cp.float32) + ) araw = Raw(adata) - assert isspmatrix_csr(araw.X) + assert isinstance(araw.X, sparse.csr_matrix)