Rate this Page

torch.Tensor.to_sparse_bsr#

Tensor.to_sparse_bsr(blocksize,dense_dim)Tensor#

Convert a tensor to a block sparse row (BSR) storage format of givenblocksize. If theself is strided, then the number of densedimensions could be specified, and a hybrid BSR tensor will becreated, withdense_dim dense dimensions andself.dim() - 2 -dense_dim batch dimension.

Parameters
  • blocksize (list, tuple,torch.Size, optional) – Block sizeof the resulting BSR tensor. A block size must be a tuple oflength two such that its items evenly divide the two sparsedimensions.

  • dense_dim (int,optional) – Number of dense dimensions of theresulting BSR tensor. This argument should be used only ifself is a strided tensor, and must be a value between 0and dimension ofself tensor minus two.

Example:

>>>dense=torch.randn(10,10)>>>sparse=dense.to_sparse_csr()>>>sparse_bsr=sparse.to_sparse_bsr((5,5))>>>sparse_bsr.col_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_bsr((2,1),1)tensor(crow_indices=tensor([0, 2, 3]),       col_indices=tensor([0, 2, 1]),       values=tensor([[[[1.]],                       [[1.]]],                      [[[1.]],                       [[1.]]],                      [[[1.]],                       [[1.]]]]), size=(4, 3, 1), nnz=3,       layout=torch.sparse_bsr)