Module ml::sparse
sparse tensors (CSR) with autograd and devices.
A SparseTensor is a rows x cols matrix that stores only its nonzeros, in compressed sparse row form: for row r, entries rowPtr[r] .. rowPtr[r+1]-1 of colIdx / values. The values are an ordinary FloatTensor of length nnz, so a sparse matrix can be a trained parameter (a sparse layer, a graph's learned edge weights): gradients reach the values and nothing else -- the sparsity pattern is fixed. The index arrays are float32 tensors too (indices exact up to 2^24), so the whole matrix moves to a device like any tensor and its ops run there.
struct SparseTensor A = SparseTensor::from_dense(adj, (float)0.0, 1); // learned edge weights
&FloatTensor H = tensor_spmm(&A, X); // [rows, n] = A [rows, cols] . X [cols, n]
... loss ... loss.backward(); // A.values.grad, X.grad
A.place(DEVICE_ROCM); // the values and indices on the GPU
Conforms to trait Tensor (element access, fill, backward, zero_grad, free) and trait DeviceTensor (device, place, sync), so generic code written against those traits takes it next to a DenseTensor.
Structs
| SparseTensor |
Functions
| tensor_spmm | out [rows, n] = S [rows, cols] . X [cols, n], with gradients to S's values and to X. Runs on the device of S's values / X when either is on one. |