dgl.sparse_v0.rst 4.07 KB
Newer Older
1
2
.. _apibackend:

3
🆕 dgl.sparse
4
5
=================================

6
`dgl.sparse` is a library for sparse operators that are commonly used in GNN models.
7
8
9

Sparse matrix class
-------------------------
10
.. currentmodule:: dgl.sparse
11

Israt Nisa's avatar
Israt Nisa committed
12
13
.. class:: SparseMatrix

14
    Class for creating a sparse matrix representation
Israt Nisa's avatar
Israt Nisa committed
15
16
17

    There are a few ways to create a sparse matrix:

18
19
20
    * In COO format using row and col indices, use :func:`from_coo`.
    * In CSR format using row pointers and col indices, use :func:`from_csr`.
    * In CSC format using col pointers and row indices, use :func:`from_csc`.
Israt Nisa's avatar
Israt Nisa committed
21

22
    For example, one can create COO matrices as follows:
Israt Nisa's avatar
Israt Nisa committed
23

24
    Case1: Sparse matrix with row and column indices without values
Israt Nisa's avatar
Israt Nisa committed
25

26
27
        >>> row = torch.tensor([1, 1, 2])
        >>> col = torch.tensor([2, 4, 3])
28
        >>> A = from_coo(row, col)
Israt Nisa's avatar
Israt Nisa committed
29
30
31
32
33
34
        >>> A
        SparseMatrix(indices=tensor([[1, 1, 2],
                                     [2, 4, 3]]),
                     values=tensor([1., 1., 1.]),
                     shape=(3, 5), nnz=3)

35
    Case2: Sparse matrix with scalar/vector values
Israt Nisa's avatar
Israt Nisa committed
36

37
        >>> # vector values
Israt Nisa's avatar
Israt Nisa committed
38
        >>> val = torch.tensor([[1, 1], [2, 2], [3, 3]])
39
        >>> A = from_coo(row, col, val)
Israt Nisa's avatar
Israt Nisa committed
40
41
42
43
44
45
46
        SparseMatrix(indices=tensor([[1, 1, 2],
                                     [2, 4, 3]]),
                     values=tensor([[1, 1],
                                    [2, 2],
                                    [3, 3]]),
                     shape=(3, 5), nnz=3)

47
    Similarly, one can create a CSR matrix as follows:
Israt Nisa's avatar
Israt Nisa committed
48
49
50
51

        >>> 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]])
52
        >>> A = from_csr(indptr, indices, val)
Israt Nisa's avatar
Israt Nisa committed
53
54
        >>> A
        SparseMatrix(indices=tensor([[0, 1, 2, 2, 2],
55
56
57
58
59
60
61
                                     [1, 2, 0, 1, 2]]),
                     values=tensor([[1, 1],
                                    [2, 2],
                                    [3, 3],
                                    [4, 4],
                                    [5, 5]]),
                     shape=(3, 3), nnz=5)
Israt Nisa's avatar
Israt Nisa committed
62

63
Creation Ops
64
````````
Israt Nisa's avatar
Israt Nisa committed
65
66

.. autosummary::
67
68
    :toctree: ../../generated/

69
70
    spmatrix
    val_like
71
72
73
    from_coo
    from_csr
    from_csc
74
75
76
77
78
79

Attributes and methods
``````````````````````

.. autosummary::
    :toctree: ../../generated/
Israt Nisa's avatar
Israt Nisa committed
80
81
82
83
84

    SparseMatrix.shape
    SparseMatrix.nnz
    SparseMatrix.dtype
    SparseMatrix.device
85
    SparseMatrix.val
Israt Nisa's avatar
Israt Nisa committed
86
87
88
89
90
    SparseMatrix.row
    SparseMatrix.col
    SparseMatrix.coo
    SparseMatrix.csr
    SparseMatrix.csc
91
92
    SparseMatrix.coalesce
    SparseMatrix.has_duplicate
93
    SparseMatrix.to_dense
94
95
96
97
98
99
100
    SparseMatrix.to
    SparseMatrix.cuda
    SparseMatrix.cpu
    SparseMatrix.float
    SparseMatrix.double
    SparseMatrix.int
    SparseMatrix.long
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
101
102
103
104
    SparseMatrix.transpose
    SparseMatrix.t
    SparseMatrix.T
    SparseMatrix.neg
Israt Nisa's avatar
Israt Nisa committed
105
106
107
108
109
110
    SparseMatrix.reduce
    SparseMatrix.sum
    SparseMatrix.smax
    SparseMatrix.smin
    SparseMatrix.smean
    SparseMatrix.softmax
111
112
113

Diagonal matrix class
-------------------------
114
.. currentmodule:: dgl.sparse
115

116
.. class:: DiagMatrix
Israt Nisa's avatar
Israt Nisa committed
117

118
119
Creators
````````
Israt Nisa's avatar
Israt Nisa committed
120
121

.. autosummary::
122
123
124
125
126
127
128
129
130
131
    :toctree: ../../generated/

    diag
    identity

Attributes and methods
``````````````````````

.. autosummary::
    :toctree: ../../generated/
Israt Nisa's avatar
Israt Nisa committed
132
133
134
135
136

    DiagMatrix.shape
    DiagMatrix.nnz
    DiagMatrix.dtype
    DiagMatrix.device
137
    DiagMatrix.val
138
139
    DiagMatrix.to_sparse
    DiagMatrix.to_dense
140
141
142
143
144
145
146
    DiagMatrix.to
    DiagMatrix.cuda
    DiagMatrix.cpu
    DiagMatrix.float
    DiagMatrix.double
    DiagMatrix.int
    DiagMatrix.long
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
147
148
149
    DiagMatrix.transpose
    DiagMatrix.t
    DiagMatrix.T
150
    DiagMatrix.neg
Israt Nisa's avatar
Israt Nisa committed
151
    DiagMatrix.inv
152

153
154
Operators
---------
155
.. currentmodule:: dgl.sparse
156

157
158
159
160
161
162
163
164
Elementwise Operators
````````

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

    add
    sub
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
165
166
    mul
    div
167
168
169
170
171
    power

Matrix Multiplication
````````

172
173
174
.. autosummary::
    :toctree: ../../generated/

175
    matmul
176
177
    spmm
    bspmm
178
    spspmm
179
    sddmm
180
    bsddmm
181
182
183
184
185
186
187

Non-linear activation functions
````````

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

188
    softmax