Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions benchmarks/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
best_gpu_time,
best_time,
integer_counts_csr,
skewed_counts_csr,
ratio_vs_scipy,
)

Expand Down Expand Up @@ -437,7 +438,7 @@ def subset_load_vs_full_load() -> dict[str, float]:
_CUDA_ROWS, _CUDA_COLS, _CUDA_WIDTH = 60_000, 2_000, 16


def _cuda_setup(cls_name: str, recipe: str):
def _cuda_setup(cls_name: str, recipe: str, *, skewed: bool = False):
"""``(gpu_view, csr, means, mat)`` for a CUDA case.

The baseline is deliberately the *best* materialized option, not the
Expand All @@ -450,7 +451,11 @@ def _cuda_setup(cls_name: str, recipe: str):
"""
import vsparse

mat = integer_counts_csr(_CUDA_ROWS, _CUDA_COLS, density=0.05)
mat = (
skewed_counts_csr(_CUDA_ROWS, _CUDA_COLS, mean_nnz_per_row=100)
if skewed
else integer_counts_csr(_CUDA_ROWS, _CUDA_COLS, density=0.05)
)
nv = getattr(vsparse, cls_name).from_scipy(mat).normalized(recipe)
gpu = nv.to_gpu()
return gpu, gpu.to_cupy_sparse(format="csr"), gpu.means, mat
Expand All @@ -474,38 +479,42 @@ def _cuda_metrics(gpu, csr, via_kernel, via_csr) -> dict[str, float]:
}


def _cuda_matmul_case(cls_name: str, recipe: str) -> Callable[[], dict[str, float]]:
def _cuda_matmul_case(
cls_name: str, recipe: str, *, skewed: bool = False
) -> Callable[[], dict[str, float]]:
"""``gpu @ B`` against ``csr @ B`` plus the same rank-1 correction."""

def bench() -> dict[str, float]:
import cupy as cp

gpu, csr, means, mat = _cuda_setup(cls_name, recipe)
gpu, csr, means, mat = _cuda_setup(cls_name, recipe, skewed=skewed)
B = cp.asarray(
np.random.default_rng(0).normal(size=(mat.shape[1], _CUDA_WIDTH)), dtype=cp.float32
)
return _cuda_metrics(gpu, csr, lambda: gpu @ B, lambda: csr @ B + (-means) @ B)

direction = "matmul" if cls_name == "VCSRArray" else "matmul_misaligned"
bench.__name__ = f"cuda_{recipe}_{direction}_vs_csr"
bench.__name__ = f"cuda_{recipe}_{direction}{'_skewed' if skewed else ''}_vs_csr"
return bench


def _cuda_rmatmul_case(cls_name: str, recipe: str) -> Callable[[], dict[str, float]]:
def _cuda_rmatmul_case(
cls_name: str, recipe: str, *, skewed: bool = False
) -> Callable[[], dict[str, float]]:
"""``B @ gpu`` against ``B @ csr`` plus the same rank-1 correction."""

def bench() -> dict[str, float]:
import cupy as cp

gpu, csr, means, mat = _cuda_setup(cls_name, recipe)
gpu, csr, means, mat = _cuda_setup(cls_name, recipe, skewed=skewed)
B = cp.asarray(
np.random.default_rng(0).normal(size=(_CUDA_WIDTH, mat.shape[0])), dtype=cp.float32
)
correction = B.sum(axis=1)[:, None] * (-means)[None, :]
return _cuda_metrics(gpu, csr, lambda: B @ gpu, lambda: B @ csr + correction)

direction = "rmatmul" if cls_name == "VCSCArray" else "rmatmul_misaligned"
bench.__name__ = f"cuda_{recipe}_{direction}_vs_csr"
bench.__name__ = f"cuda_{recipe}_{direction}{'_skewed' if skewed else ''}_vs_csr"
return bench


Expand All @@ -521,6 +530,10 @@ def _register_cuda_benchmarks() -> None:
cuda(_cuda_rmatmul_case("VCSCArray", "parafac2"))
cuda(_cuda_matmul_case("VCSCArray", "parafac2"))
cuda(_cuda_rmatmul_case("VCSRArray", "parafac2"))
# Real count matrices are skewed (a few genes in most cells), which leaves a
# VCSC with a few giant slices; the uniform matrix above cannot show that.
for skewed_case in (_cuda_rmatmul_case, _cuda_matmul_case):
cuda(skewed_case("VCSCArray", "parafac2", skewed=True))


_register_cuda_benchmarks()
Expand Down
20 changes: 20 additions & 0 deletions benchmarks/harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ def integer_counts_csr(n_rows: int, n_cols: int, density: float, seed: int = 0)
return mat


def skewed_counts_csr(
n_rows: int, n_cols: int, mean_nnz_per_row: int, seed: int = 0
) -> sp.csr_array:
"""Integer counts shaped like a real single-cell matrix, unlike the uniform one above.

Gene popularity is Zipf-distributed (a few genes in most cells, most in
few), cell depth is log-normal (heavy-tailed row lengths), and counts are
drawn by multinomial-style sampling, so most nonzeros equal 1.
"""
rng = np.random.default_rng(seed)
gene_p = 1.0 / np.arange(1, n_cols + 1) ** 1.1
cell_p = rng.lognormal(0.0, 0.8, n_rows)
draws = int(n_rows * mean_nnz_per_row * 1.3)
rows = rng.choice(n_rows, size=draws, p=cell_p / cell_p.sum())
cols = rng.choice(n_cols, size=draws, p=gene_p / gene_p.sum())
mat = sp.coo_array((np.ones(draws), (rows, cols)), shape=(n_rows, n_cols)).tocsr()
mat.sum_duplicates()
return mat


def best_gpu_time(fn: Callable[[], Any], repeat: int = 7) -> float:
"""Best wall-clock time over ``repeat`` runs of a CUDA call, in seconds.

Expand Down
Loading
Loading