graph-spmm

Sparse-dense matrix products for message passing, loadable through kernels: a CSR adjacency applied to a dense feature matrix with sum, mean, or max aggregation. The reference baseline is torch.sparse.mm (cuSPARSE), matched to fp32 summation order and beaten up to 2.1x on the sparse, narrow-feature shapes graph networks actually run. Companion to graph-reduce for unsorted destinations.

A graph convolution is a sparse adjacency times a dense feature matrix, applied once per layer over a graph that does not change. General sparse libraries pay for generality, and atomic-based scatter alternatives return different bits on every run. This kernel keeps one warp per output row with no atomics anywhere, so message passing over million-node graphs is both faster than cuSPARSE in its home regime and bitwise reproducible, and max-aggregation returns the winning neighbour's index in the same sweep, the piece a max-pool backward needs.

Heat from four sources diffusing through an 8,000-node kNN graph, one spmm mean-aggregation per step (log-scale coloring). On 1M nodes and 10M edges at 16 channels the product measures 0.39 ms against cuSPARSE's 0.85 ms, bitwise reproducible with no atomics.

Usage

import torch
from kernels import get_kernel

tsp = get_kernel("phanerozoic/graph-spmm", version=1, trust_remote_code=True)

# build the CSR once for a fixed graph, then apply it per layer
adj = tsp.SparseAdj.from_edge_index(edge_index, num_nodes, edge_weight)
h = adj @ x                                  # sum aggregation
h = adj.reduce(x, "mean")                    # mean
h, arg = adj.reduce(x, "max")                # max, plus the winning neighbour

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark.

API

Symbol Purpose
spmm_sum(rowptr, col, val, mat) A @ mat; val=None is an unweighted adjacency
spmm_mean(rowptr, col, val, mat) row-wise mean; empty rows are 0
spmm_max(rowptr, col, val, mat) (values, argmax); empty rows are (0, -1)
spmm(rowptr, col, val, mat, reduce) dispatch by name
SparseAdj(rowptr, col, val) a CSR adjacency held once and applied many times
SparseAdj.from_edge_index(edge_index, num_nodes, val) sort a [2, E] edge list into CSR

rowptr is [S+1] int64, col is [nnz] int64, mat is [K, C] fp32.

Method

One warp per output row. The row's nonzeros are walked cooperatively with each lane holding a strided slice of the feature columns, so dense loads coalesce across the warp and every output element reduces privately in a register. Nothing is shared between warps, so there are no atomics: the result is bitwise reproducible and independent of scheduling. spmm_max resolves the argmax during the same sweep. val=None treats every stored entry as 1 and skips the value load.

Measured

fp32, median of 30, against torch.sparse.mm on a CSR tensor (cuSPARSE):

nodes nnz C avg degree torch.sparse.mm this speedup
10,000 200,000 32 20 0.028 ms 0.023 ms 1.23x
10,000 200,000 128 20 0.035 ms 0.032 ms 1.10x
100,000 1,000,000 32 10 0.081 ms 0.038 ms 2.11x
100,000 4,000,000 64 40 0.301 ms 0.245 ms 1.23x
1,000,000 10,000,000 16 10 0.894 ms 0.494 ms 1.81x

The margin is largest on many nodes, low degree, modest channel count; at high degree and wide features both are bandwidth-bound on the dense operand and converge.

Correctness

  • Sum agrees with torch.sparse.mm to 1.5e-5 absolute across 10^4 to 10^6 nodes (fp32 summation order); mean to 3.0e-7 against the row-normalized product.
  • Max and its arg are checked against a per-row reference built from the CSR entries (a dense scatter silently drops duplicate pairs, which a real CSR can contain and this kernel sums).
  • Unweighted adjacency matches an explicit all-ones value array; empty rows reduce to 0 for sum and mean and (0, -1) for max.
  • Deterministic: repeated products are bitwise identical.
  • SparseAdj.from_edge_index sorts an unsorted edge list into CSR and reproduces the same product.

Requirements and limits

  • NVIDIA GPU with compute capability 8.0+; fp32 only.
  • One warp per row: power-law graphs with extreme hubs load-imbalance and want a segmented schedule this kernel does not implement.
  • Forward only; the backward of a sum aggregation is the transposed product, composed from a second SparseAdj on the reversed edge list.

References

Merrill and Garland, "Merge-based parallel sparse matrix-vector multiplication" (2016); Bell and Garland, "Implementing sparse matrix-vector multiplication on throughput-oriented processors" (2009).

License

Apache-2.0.

Downloads last month
-
apache-2.0
Supported hardwares new
CUDA
8.08.68.99.010.012.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64
Kernel Builder
19aaa64