🆕 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.

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.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()

Applies row-wise softmax to the non-zero elements of the sparse matrix.

Diagonal matrix class

class dgl.sparse.DiagMatrix[source]

Creators

diag(val[, shape])

Creates a diagonal matrix based on the diagonal values.

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

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

Attributes and methods

DiagMatrix.shape

Returns the shape of the diagonal matrix.

DiagMatrix.nnz

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

DiagMatrix.dtype

Returns the data type of the diagonal matrix.

DiagMatrix.device

Returns the device the diagonal matrix is on.

DiagMatrix.val

Returns the values of the non-zero elements.

DiagMatrix.to_sparse()

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

DiagMatrix.to_dense()

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

DiagMatrix.to([device, dtype])

Performs matrix dtype and/or device conversion.

DiagMatrix.cuda()

Moves the matrix to GPU.

DiagMatrix.cpu()

Moves the matrix to CPU.

DiagMatrix.float()

Converts the matrix values to float32 data type.

DiagMatrix.double()

Converts the matrix values to double data type.

DiagMatrix.int()

Converts the matrix values to int32 data type.

DiagMatrix.long()

Converts the matrix values to long data type.

DiagMatrix.transpose()

Returns a matrix that is a transposed version of the diagonal matrix.

DiagMatrix.t()

Alias of transpose()

DiagMatrix.T

Alias of transpose()

DiagMatrix.neg()

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

DiagMatrix.inv()

Returns the inverse of the diagonal matrix.

Operators

Elementwise Operators

add(A, B)

Elementwise addition for DiagMatrix and SparseMatrix, equivalent to A + B.

sub(A, B)

Elementwise subtraction for DiagMatrix and SparseMatrix, equivalent to A - B.

mul(A, B)

Elementwise multiplication for DiagMatrix and SparseMatrix, equivalent to A * B.

div(A, B)

Elementwise division for DiagMatrix and SparseMatrix, equivalent to A / B.

power(A, scalar)

Elementwise exponentiation for DiagMatrix and SparseMatrix, equivalent to A ** scalar.

Matrix Multiplication

matmul(A, B)

Multiplies two dense/sparse/diagonal 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)

Applies row-wise softmax to the non-zero elements of the sparse matrix.