Repository navigation
Conversation
Creates PyTorch-Sparse-Roadmap.md (1st draft)
|
One very basic newbie question: is there a generalization of CSR format that works for higher dimensions? Or is it a 2D only kind of deal? |
|
I did a quick read, thanks for the thorough writeup! Some questions that come to mind:
|
There is GCRS (https://ieeexplore.ieee.org/document/7237032) and CSF (https://dl.acm.org/doi/10.1145/2833179.2833183). If you want to go completely-bonkers-general-all-formats-are-one there's TACO (https://arxiv.org/abs/1804.10112). |
|
Hi, Thanks for the proposal. |
|
Excellent point. Yes, this has the potential to break existing parts of the codebase, so an additional point needs to be added to the timeline to verify this (concerns about |
Important disclaimer there is that those >2-D generalizations are not supported by cuSPARSE, MKL et al. CSR is the best-supported format by those libraries, followed by COO. So it's unclear to me whether it makes sense to have more than one >2-D format. |
pearu
left a comment
There was a problem hiding this comment.
Overall LGTM. Added some suggestions and questions regarding CSR implementation details.
|
I think we've already gotten here in the convo, but just to state it explicitly: if csr unambiguously refers to 2d only, we don't need to qualify the name with 2d; and we don't have to work on generalized versions if it's not clear, 2d only is fine. |
|
Wouldn't a batch of sparse matrix be an important usecase as well? |
|
This is great proposal! Interesting thought: CSR is effectively equivalent to I'd also vote for extending representation to >2 dimensions if possible (or at least not baking it in API names now). Batch use case Alban mentioned is one example. Another point is that several embedding_bags' input can be thought as sparse 3d tensor (e.g. batch X num_features X feature_dim for sparse features). So it might be interesting to explore whether sparse tensor api is a good representation for it. |
@albanD yes, batches of sparse matrices is obviously important. I have modified the proposal to describe how CSR extends to tensors of arbitrary rank (and added references to papers that describe other extensions in greater detail). I believe this category of generalization is sufficient (even if the indexing sparse dimension in |
|
@dhavide could you also rename |
|
@rgommers pointed me here from the CuPy issue–glad to see progress being made! To add some comments from an end-user (domain is computational biology).
I think this would have a major benefit in my area of research. I work a lot with single-cell RNA sequencing data (scRNAseq), and the raw data is a large, sparse count matrix. By "large": on the order of 10k rows for one experiment, easily 100k to 1M+ for very large projects or meta-analyses, and 10s of thousands of features. By "sparse": typically 1-2% nonzero values. Not "big data" so much as wide, complex data. This type of data is becoming more prevalent and datasets are growing as methods improve, see this google sheet showing the trend over time (credit to @vals who might drop in later). When analyzing scRNA data, the difference between sparse and dense representations can be the difference between fitting the data in GPU memory or not, which can greatly impact the speed of training. The rest of the model could be dense but just using sparse tensors for the input would allow for larger batches and less memory-copying. It's hard to know exactly how big a benefit that would provide but I think it'd be considerable. One thing I would add as something to think about (but out of scope here, I guess): I'd love to be able to pass a sparse tensor in to That's kind of a niche thing to do but it might be doable with the |
pearu
left a comment
There was a problem hiding this comment.
The proposed CSR construction API is incorrect (RO and CO have different lengths and hence cannot be collected in single indices tensor) and proposed a fix.
I also suggest implementing arbitrary rank CSR construction function as it will be no different from the rank 2 case. However, the implementations of algorithms may restrict to supporting only rank 2 cases in short term.
| ```{python} | ||
| sparse_csr_tensor(indices, values, size=None, dtype=None, | ||
| device=None, requires_grad=False) -> Tensor | ||
| ``` | ||
|
|
||
| where `indices` would be a list of two lists (e.g., `RO` & `CO` | ||
| from the description of CSR format above) and `values` would be a tensor | ||
| containing the corresponding (presumably) nonzero entries of the sparse |
There was a problem hiding this comment.
This API for CSR is incorrect/inefficient: RO has a length size[0]+1 while CO has length nnz and so RO and CO lists should be separated.
I suggest the following API for sparse_csr_tensor:
sparse_csr_tensor(pointers, indices, values, size=None, dtype=None,
device=None, requires_grad=False) -> Tensorwhere the pointers argument is a 1-D array of length size[0] + 1 containing the cumulative number of nonzero elements in the sparse array computed for each index of the first sparse dimension.
|
|
||
| 2. Construct and test a prototype implementation of conversion functions | ||
| `torch.to_sparse_csr` and `torch.to_sparse_coo` to convert sparse PyTorch | ||
| tensors of sparse rank 2 between COO and CSR formats. |
There was a problem hiding this comment.
I think we can target immediately arbitrary rank (>=2) CSR sparse tensors in the constructor, including the hybrid feature, without any extra effort. The algorithms, however, may be restricted to 2-D in the short run.
|
So will block sparse be supported as some people ask for that maybe for sparse attention? (pytorch/pytorch#9222) |
|
Thanks for asking @npuichigo. We've discussed it briefly recently and it's clearly of interest. The first target for increasing sparse matrix performance at the moment is the GCS format: pytorch/pytorch#44190. Considering the block sparse use cases and interest in pytorch/pytorch#9222, it does seem like that's a good next candidate. I'll add a note to the tracking issue for sparse tensor improvements (pytorch/pytorch#44634) to reassess the priority of block-sparse. |
Suggestions for a strategy on how to go forward with
torch.sparsetorch.sparse.