dgl.sparse_v0.rst 4.04 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
64
Creators
````````
Israt Nisa's avatar
Israt Nisa committed
65
66

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

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

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

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

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

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

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

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

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

    diag
    identity

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

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

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

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

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

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

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

Matrix Multiplication
````````

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

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

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

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

187
    softmax