Skip to content

CPU matmul kernels: balance work by nonzeros and evaluate g once per value group - #103

Merged
aarmey merged 4 commits into
mainfrom
cpu-kernels
Nov 22, 2026
Merged

aarmey merged 4 commits into
mainfrom
cpu-kernels

Conversation

@aarmey

@aarmey aarmey commented Nov 22, 2026

Copy link
Copy Markdown
Member

Summary

Two changes to the CPU normalized-view products (src/vsparse/_vcs_matmul.py), found while benchmarking the CPU kernels for the paper (Fig 5 in meyer-lab/vsparse-paper).

Balance by nonzeros. Every kernel gave each thread the same number of major slices. Real counts have heavy-tailed slice lengths: on 250,000 Lupus cells, 28% of the nonzeros sit in 1% of the genes, the longest gene is in every cell, and the long genes are contiguous along the axis. An equal-count split therefore leaves one thread with up to 20x the mean work, and B @ VCSC could not use more than about 1.5 of 16 or 2.4 of 48 threads (measured: 1.4x and 1.9x). The aligned kernels now loop in parallel over nonzero-balanced chunks (four per thread), and the misaligned kernels give each private accumulator a nonzero-balanced range of slices. _nnz_balanced_bounds is one serial pass over the pointers, with no allocation beyond the bounds.

Evaluate g once per value group. When the recipe has no per-gene scaling (cp10k_log1p, scanpy, raw), g(v / row_scale / gene_scale) depends only on the row and the value, so VCSR X @ B and B @ X now evaluate it once per value group instead of once per stored entry (this is the TODO in the paper's Methods; it removes most of the logarithms, since most nonzeros in a cell are 1). VCSC cannot hoist it: the argument changes with the row of every index. The hoisted expression is the one evaluated before with gene_scale == 1, so results are unchanged.

Measurements

Single thread, the cp10k_log1p recipe, width 16, interleaved old/new runs on a heavily loaded shared machine (load ~21 from another user's jobs, so absolute times are noisy; the full 250,000-cell and thread-scaling numbers will follow when the machine is quiet):

product before after
X @ VCSR (30,000 Lupus cells) 0.50 s 0.22 s
B @ VCSR (misaligned) 0.55 s 0.22 s
B @ VCSC, X @ VCSC unchanged within noise (single thread, as expected)

raw is unchanged within noise.

Tests

  • New tests/test_matmul_balance.py: products against a dense reference on skewed matrices for every recipe, both layouts and 1, 3 and 8 threads (covers hoisted and unhoisted paths); the chunking helper (coverage, monotone, no chunk over its share by more than the longest slice, empty array, more chunks than slices); and a guard that the test matrix is unbalanced under an equal-count split.
  • Full suite: 1570 passed, 74 skipped (all-zero-matrix skips as before).
  • ruff check and ruff format clean; no function ranked worse than B (radon); no new vulture findings.

Not changed

  • log1p is still a scalar libm call; a vectorized approximation (as BPCells uses) would change results by about 1e-7 and is left out.
  • The misaligned kernels' thread cap from the accumulator budget (accumulator_threads) is unchanged, so X @ VCSC and B @ VCSR at large output sizes still run on few threads.

…value group

Two changes to the normalized-view products in _vcs_matmul.py.

Balance: every kernel gave each thread the same number of major slices. Real counts have heavy-tailed slice lengths
(on 250,000 Lupus cells, 28% of the nonzeros sit in 1% of the genes, and the long genes are contiguous), so for
B @ VCSC the busiest thread held up to 20x the mean and the product could not use more than about 2.4 of 48 threads.
The aligned kernels now loop over nonzero-balanced chunks (four per thread) and the misaligned ones give each
private accumulator a nonzero-balanced range of slices (_nnz_balanced_bounds, one serial pass over the pointers).

Hoist: when the recipe has no per-gene scaling, g(v / row_scale / gene_scale) depends only on the row and the value,
so VCSR X @ B and B @ X evaluate it once per value group and not once per stored entry. VCSC cannot, since the
argument changes with the row of every index. Results are unchanged: the hoisted expression is the one evaluated
before, with gene_scale == 1.

Tests cover skewed matrices for every recipe, direction and thread count, and the chunking helper.
numba.set_num_threads raises when asked for more threads than the process
launched with, which broke the threads=8 cases on 4-thread CI runners.
numba.config.NUMBA_NUM_THREADS is not visible to ty.
@aarmey
aarmey merged commit 3ff76db into main Nov 22, 2026
8 checks passed
@aarmey
aarmey deleted the cpu-kernels branch November 22, 2026 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant