dgl.sparse_v0.rst 3.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
.. _apibackend:

dgl.mock_sparse
=================================

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

.. warning::
    This is an experimental package. The sparse operators provided in this library do not guarantee the same performance as their message-passing api counterparts.

Sparse matrix class
-------------------------
.. currentmodule:: dgl.mock_sparse

Israt Nisa's avatar
Israt Nisa committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
.. class:: SparseMatrix

    Class for creating a sparse matrix representation. The row and column indices of the sparse matrix can be the source
    (row) and destination (column) indices of a homogeneous or heterogeneous graph.

    There are a few ways to create a sparse matrix:

    * In COO format using row and col indices, use :func:`create_from_coo`.
    * In CSR format using row pointers and col indices, use :func:`create_from_csr`.
    * In CSC format using col pointers and row indices, use :func:`create_from_csc`.

    For example, we can create COO matrix as follows:

    Case1: Sparse matrix with row and column indices without values.

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

    Case2: Sparse matrix with scalar/vector values. Following example is with
    vector data.

        >>> val = torch.tensor([[1, 1], [2, 2], [3, 3]])
        >>> A = create_from_coo(src, dst, val)
        SparseMatrix(indices=tensor([[1, 1, 2],
                                     [2, 4, 3]]),
                     values=tensor([[1, 1],
                                    [2, 2],
                                    [3, 3]]),
                     shape=(3, 5), nnz=3)

    Similarly, we can create CSR matrix as follows:

        >>> indptr = torch.tensor([0, 1, 2, 5])
        >>> indices = torch.tensor([1, 2, 0, 1, 2])
        >>> val = torch.tensor([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]])
        >>> A = create_from_csr(indptr, indices, val)
        >>> A
        SparseMatrix(indices=tensor([[0, 1, 2, 2, 2],
                [1, 2, 0, 1, 2]]),
        values=tensor([[1, 1],
                [2, 2],
                [3, 3],
                [4, 4],
                [5, 5]]),
        shape=(3, 3), nnz=5)

Sparse matrix class attributes
------------------------------

.. autosummary::

    SparseMatrix.shape
    SparseMatrix.nnz
    SparseMatrix.dtype
    SparseMatrix.device
    SparseMatrix.row
    SparseMatrix.col
    SparseMatrix.val
    __call__
    SparseMatrix.indices
    SparseMatrix.coo
    SparseMatrix.csr
    SparseMatrix.csc
    SparseMatrix.dense
    SparseMatrix.t
    SparseMatrix.T
    SparseMatrix.transpose
    SparseMatrix.reduce
    SparseMatrix.sum
    SparseMatrix.smax
    SparseMatrix.smin
    SparseMatrix.smean
    SparseMatrix.__neg__
    SparseMatrix.inv
    SparseMatrix.softmax
    SparseMatrix.__matmul__
97
98
99
100
101
102
103
104
105
106
107
108
109

.. autosummary::
    :toctree: ../../generated/

    create_from_coo
    create_from_csr
    create_from_csc

Diagonal matrix class
-------------------------
.. currentmodule:: dgl.mock_sparse

.. autoclass:: DiagMatrix
Israt Nisa's avatar
Israt Nisa committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

Diagonal matrix class attributes
--------------------------------

.. autosummary::

    DiagMatrix.val
    DiagMatrix.shape
    DiagMatrix.__call__
    DiagMatrix.nnz
    DiagMatrix.dtype
    DiagMatrix.device
    DiagMatrix.as_sparse
    DiagMatrix.t
    DiagMatrix.T
    DiagMatrix.transpose
    DiagMatrix.reduce
    DiagMatrix.sum
    DiagMatrix.smax
    DiagMatrix.smin
    DiagMatrix.smean
    DiagMatrix.__neg__
    DiagMatrix.inv
    DiagMatrix.softmax
    DiagMatrix.__matmul__
135
136
137
138
139
140

.. autosummary::
    :toctree: ../../generated/

    diag
    identity
141
142
143
144
145
146
147
148
149
150
151
152

Operators
---------
.. currentmodule:: dgl.mock_sparse

.. autosummary::
    :toctree: ../../generated/

    spmm
    spspmm
    bspmm
    bspspmm
153
    sddmm