Repository navigation
CPU matmul kernels: balance work by nonzeros and evaluate g once per value group - #103
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 @ VCSCcould 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_boundsis one serial pass over the pointers, with no allocation beyond the bounds.Evaluate
gonce 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 VCSRX @ BandB @ Xnow 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 withgene_scale == 1, so results are unchanged.Measurements
Single thread, the
cp10k_log1precipe, 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):X @ VCSR(30,000 Lupus cells)B @ VCSR(misaligned)B @ VCSC,X @ VCSCrawis unchanged within noise.Tests
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.ruff checkandruff formatclean; no function ranked worse than B (radon); no newvulturefindings.Not changed
log1pis still a scalar libm call; a vectorized approximation (as BPCells uses) would change results by about 1e-7 and is left out.accumulator_threads) is unchanged, soX @ VCSCandB @ VCSRat large output sizes still run on few threads.