Before working on this tutorial, please make sure to review our
MaskedTensor Overview tutorial <https://pytorch.org/tutorials/prototype/maskedtensor_overview.html>.
Introduction
Sparsity has been an area of rapid growth and importance within PyTorch; if any sparsity terms are confusing below,
please refer to the for additional details.
Sparse storage formats have been proven to be powerful in a variety of ways. As a primer, the first use case
most practitioners think about is when the majority of elements are equal to zero (a high degree of sparsity),
but even in cases of lower sparsity, certain formats (e.g. BSR) can take advantage of substructures within a matrix.
Principles
When creating a MaskedTensor with sparse tensors, there are a few principles that must be observed:
data and mask must have the same storage format, whether that’s torch.strided, torch.sparse_coo, or torch.sparse_csr
data and mask must have the same size, indicated by size()
Sparse COO tensors
In accordance with Principle #1, a sparse COO MaskedTensor is created by passing in two sparse COO tensors,
which can be initialized by any of its constructors, for example torch.sparse_coo_tensor().
As a recap of sparse COO tensors, the COO format
stands for “coordinate format”, where the specified elements are stored as tuples of their indices and the
corresponding values. That is, the following are provided:
indices: array of size (ndim,nse) and dtype torch.int64
values: array of size (nse,) with any integer or floating point dtype
where ndim is the dimensionality of the tensor and nse is the number of specified elements.
For both sparse COO and CSR tensors, you can construct a MaskedTensor by doing either:
dense_masked_tensor.to_sparse_coo() or dense_masked_tensor.to_sparse_csr()
The second method is easier to illustrate so we’ve shown that below, but for more on the first and the nuances behind
the approach, please read the Sparse COO Appendix.
importtorchfromtorch.maskedimportmasked_tensorimportwarnings# Disable prototype warnings and suchwarnings.filterwarnings(action='ignore',category=UserWarning)values=torch.tensor([[
mt:MaskedTensor([[--,--,3],[--,--,5]])
mt (sparse coo):MaskedTensor([[--,--,3],[--,--,5]])
mt data sparse coo
Sparse CSR tensors
Similarly, MaskedTensor also supports the
CSR (Compressed Sparse Row)
sparse tensor format. Instead of storing the tuples of the indices like sparse COO tensors, sparse CSR tensors
aim to decrease the memory requirements by storing compressed row indices.
In particular, a CSR sparse tensor consists of three 1-D tensors:
crow_indices: array of compressed row indices with size (size[0]+1,). This array indicates which row
a given entry in values lives in. The last element is the number of specified elements,
while crow_indices[i+1] - crow_indices[i] indicates the number of specified elements in row i.
col_indices: array of size (nnz,). Indicates the column indices for each value.
values: array of size (nnz,). Contains the values of the CSR tensor.
Of note, both sparse COO and CSR tensors are in a beta state.
By way of example:
mt_sparse_csr=mt.to_sparse_csr()print("mt (sparse csr):\n",mt_sparse_csr)print("mt data (sparse csr):\n",mt_sparse_csr.get_data())
mt (sparse csr):MaskedTensor([[--,--,3],[--,--,5]])
mt data (sparse csr):tensor(crow_indices=tensor([0,1,2]),
col_indices=tensor([2,
Note that mt and mt2 look identical on the surface, and in the vast majority of operations, will yield the same
result. But this brings us to a detail on the implementation:
data and mask – only for sparse MaskedTensors – can have a different number of elements (nnz())
at creation, but the indices of mask must then be a subset of the indices of data. In this case,
data will assume the shape of mask by data=data.sparse_mask(mask); in other words, any of the elements
in data that are not True in mask (that is, not specified) will be thrown away.
Therefore, under the hood, the data looks slightly different; mt2 has the “4” value masked out and mt
is completely without it. Their underlying data has different shapes,
which would make operations like mt+mt2 invalid.
We can also construct a sparse CSR MaskedTensor using sparse CSR tensors,
and like the example above, this results in a similar treatment under the hood.
In this tutorial, we have introduced how to use MaskedTensor with sparse COO and CSR formats and
discussed some of the subtleties under the hood in case users decide to access the underlying data structures
directly. Sparse storage formats and masked semantics indeed have strong synergies, so much so that they are
sometimes used as proxies for each other (as we will see in the next tutorial). In the future, we certainly plan
to invest and continue developing in this direction.