Shortcuts

torch.Tensor.to_sparse_bsc

Tensor.to_sparse_bsc(blocksize, dense_dim) Tensor

Convert a tensor to a block sparse column (BSC) storage format of given blocksize. If the self is strided, then the number of dense dimensions could be specified, and a hybrid BSC tensor will be created, with dense_dim dense dimensions and self.dim() - 2 - dense_dim batch dimension.

Parameters:
  • blocksize (list, tuple, torch.Size, optional) – Block size of the resulting BSC tensor. A block size must be a tuple of length two such that its items evenly divide the two sparse dimensions.

  • dense_dim (int, optional) – Number of dense dimensions of the resulting BSC tensor. This argument should be used only if self is a strided tensor, and must be a value between 0 and dimension of self tensor minus two.

Example:

>>> dense = torch.randn(10, 10)
>>> sparse = dense.to_sparse_csr()
>>> sparse_bsc = sparse.to_sparse_bsc((5, 5))
>>> sparse_bsc.row_indices()
tensor([0, 1, 0, 1])

>>> dense = torch.zeros(4, 3, 1)
>>> dense[0:2, 0] = dense[0:2, 2] = dense[2:4, 1] = 1
>>> dense.to_sparse_bsc((2, 1), 1)
tensor(ccol_indices=tensor([0, 1, 2, 3]),
       row_indices=tensor([0, 1, 0]),
       values=tensor([[[[1.]],

                       [[1.]]],


                      [[[1.]],

                       [[1.]]],


                      [[[1.]],

                       [[1.]]]]), size=(4, 3, 1), nnz=3,
       layout=torch.sparse_bsc)

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources