🆕 dgl.sparse

dgl.sparse is a library for sparse operators that are commonly used in GNN models.

Sparse matrix class

class dgl.sparse.SparseMatrix[source]

A SparseMatrix can be created from Coordinate format indices using the spmatrix() constructor:

>>> indices = torch.tensor([[1, 1, 2],
>>>                         [2, 4, 3]])
>>> A = dglsp.spmatrix(indices)
SparseMatrix(indices=tensor([[1, 1, 2],
                             [2, 4, 3]]),
             values=tensor([1., 1., 1.]),
             shape=(3, 5), nnz=3)

Creation Ops

spmatrix(indices[, val, shape])

Creates a sparse matrix from Coordinate format indices.

val_like(mat, val)

Creates a sparse matrix from an existing sparse matrix using new values.

from_coo(row, col[, val, shape])

Creates a sparse matrix from a coordinate list (COO), which stores a list of (row, column, value) tuples.

from_csr(indptr, indices[, val, shape])

Creates a sparse matrix from compress sparse row (CSR) format.

from_csc(indptr, indices[, val, shape])

Creates a sparse matrix from compress sparse column (CSC) format.

diag(val[, shape])

Creates a sparse matrix based on the diagonal values.

identity(shape[, d, dtype, device])

Creates a sparse matrix with ones on the diagonal and zeros elsewhere.

Attributes and methods

SparseMatrix.shape

Returns the shape of the sparse matrix.

SparseMatrix.nnz

Returns the number of non-zero elements in the sparse matrix.

SparseMatrix.dtype

Returns the data type of the sparse matrix.

SparseMatrix.device

Returns the device the sparse matrix is on.

SparseMatrix.val

Returns the values of the non-zero elements.

SparseMatrix.row

Returns the row indices of the non-zero elements.

SparseMatrix.col

Returns the column indices of the non-zero elements.

SparseMatrix.indices()

Returns the coordinate list (COO) representation in one tensor with shape (2, nnz).

SparseMatrix.coo()

Returns the coordinate list (COO) representation of the sparse matrix.

SparseMatrix.csr()

Returns the compressed sparse row (CSR) representation of the sparse matrix.

SparseMatrix.csc()

Returns the compressed sparse column (CSC) representation of the sparse matrix.

SparseMatrix.coalesce()

Returns a coalesced sparse matrix.

SparseMatrix.has_duplicate()

Returns True if the sparse matrix contains duplicate indices.

SparseMatrix.to_dense()

Returns a copy in dense matrix format of the sparse matrix.

SparseMatrix.to([device, dtype])

Performs matrix dtype and/or device conversion.

SparseMatrix.cuda()

Moves the matrix to GPU.

SparseMatrix.cpu()

Moves the matrix to CPU.

SparseMatrix.float()

Converts the matrix values to float32 data type.

SparseMatrix.double()

Converts the matrix values to double data type.

SparseMatrix.int()

Converts the matrix values to int32 data type.

SparseMatrix.long()

Converts the matrix values to long data type.

SparseMatrix.transpose()

Returns the transpose of this sparse matrix.

SparseMatrix.t()

Alias of transpose()

SparseMatrix.T

Alias of transpose()

SparseMatrix.neg()

Returns a new sparse matrix with the negation of the original nonzero values, equivalent to -A.

SparseMatrix.reduce([dim, rtype])

Computes the reduction of non-zero values of the input sparse matrix along the given dimension dim.

SparseMatrix.sum([dim])

Computes the sum of non-zero values of the input sparse matrix along the given dimension dim.

SparseMatrix.smax([dim])

Computes the maximum of non-zero values of the input sparse matrix along the given dimension dim.

SparseMatrix.smin([dim])

Computes the minimum of non-zero values of the input sparse matrix along the given dimension dim.

SparseMatrix.smean([dim])

Computes the mean of non-zero values of the input sparse matrix along the given dimension dim.

SparseMatrix.softmax([dim])

Applies softmax to the non-zero elements of the sparse matrix on the dimension :attr:dim.

Operators

Elementwise Operators

add(A, B)

Elementwise addition for SparseMatrix, equivalent to A + B.

sub(A, B)

Elementwise subtraction for SparseMatrix, equivalent to A - B.

mul(A, B)

Elementwise multiplication for SparseMatrix, equivalent to A * B.

div(A, B)

Elementwise division for SparseMatrix, equivalent to A / B.

power(A, scalar)

Elementwise exponentiation SparseMatrix, equivalent to A ** scalar.

Matrix Multiplication

matmul(A, B)

Multiplies two dense/sparse matrices, equivalent to A @ B.

spmm(A, X)

Multiplies a sparse matrix by a dense matrix, equivalent to A @ X.

bspmm(A, X)

Multiplies a sparse matrix by a dense matrix by batches, equivalent to A @ X.

spspmm(A, B)

Multiplies a sparse matrix by a sparse matrix, equivalent to A @ B.

sddmm(A, X1, X2)

Sampled-Dense-Dense Matrix Multiplication (SDDMM).

bsddmm(A, X1, X2)

Sampled-Dense-Dense Matrix Multiplication (SDDMM) by batches.

Non-linear activation functions

softmax(input[, dim])

Applies softmax to the non-zero elements of the sparse matrix on the dimension :attr:dim.

Broadcast operators

sp_broadcast_v(A, v, op)

Broadcast operator for sparse matrix and vector.

sp_add_v(A, v)

Broadcast addition for sparse matrix and vector.

sp_sub_v(A, v)

Broadcast substraction for sparse matrix and vector.

sp_mul_v(A, v)

Broadcast multiply for sparse matrix and vector.

sp_div_v(A, v)

Broadcast division for sparse matrix and vector.