torch.sparse_compressed_tensor — PyTorch 2.7 documentation (original) (raw)

torch.sparse_compressed_tensor(compressed_indices, plain_indices, values, size=None, *, dtype=None, layout=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None) → Tensor

Constructs a sparse tensor in Compressed Sparse format - CSR, CSC, BSR, or BSC - with specified values at the given compressed_indices and plain_indices. Sparse matrix multiplication operations in Compressed Sparse format are typically faster than that for sparse tensors in COO format. Make you have a look at the note on the data type of the indices.

Note

If the device argument is not specified the device of the givenvalues and indices tensor(s) must match. If, however, the argument is specified the input Tensors will be converted to the given device and in turn determine the device of the constructed sparse tensor.

Parameters

Keyword Arguments

Example::

compressed_indices = [0, 2, 4] plain_indices = [0, 1, 0, 1] values = [1, 2, 3, 4] torch.sparse_compressed_tensor(torch.tensor(compressed_indices, dtype=torch.int64), ... torch.tensor(plain_indices, dtype=torch.int64), ... torch.tensor(values), dtype=torch.double, layout=torch.sparse_csr) tensor(crow_indices=tensor([0, 2, 4]), col_indices=tensor([0, 1, 0, 1]), values=tensor([1., 2., 3., 4.]), size=(2, 2), nnz=4, dtype=torch.float64, layout=torch.sparse_csr)