33
33
@dygraph_only
34
34
def matmul (x , y , name = None ):
35
35
"""
36
- Note:
36
+ Note:
37
37
This API is only supported from ``CUDA 11.0`` .
38
38
39
- Applies matrix multiplication of two Tensors.
40
-
39
+ Applies matrix multiplication of two Tensors.
40
+
41
41
The supported input/output Tensor layout are as follows:
42
-
42
+
43
43
Note:
44
44
x[SparseCsrTensor] @ y[SparseCsrTensor] -> out[SparseCsrTensor]
45
45
x[SparseCsrTensor] @ y[DenseTensor] -> out[DenseTensor]
@@ -49,14 +49,14 @@ def matmul(x, y, name=None):
49
49
It supports backward propagation.
50
50
51
51
Dimensions `x` and `y` must be >= 2D. Automatic broadcasting of Tensor is not supported.
52
- the shape of `x` should be `[*, M, K]` , and the shape of `y` should be `[*, K, N]` , where `*`
52
+ the shape of `x` should be `[*, M, K]` , and the shape of `y` should be `[*, K, N]` , where `*`
53
53
is zero or more batch dimensions.
54
54
55
55
Args:
56
56
x (Tensor): The input tensor. It can be SparseCooTensor/SparseCsrTensor. The data type can be float32 or float64.
57
57
y (Tensor): The input tensor. It can be SparseCooTensor/SparseCsrTensor/DenseTensor. The data type can be float32 or float64.
58
58
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
59
-
59
+
60
60
Returns:
61
61
Tensor: Its layout is determined by that of `x` and `y` .
62
62
@@ -72,9 +72,9 @@ def matmul(x, y, name=None):
72
72
cols = [1, 2, 0]
73
73
values = [1., 2., 3.]
74
74
csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, [3, 3])
75
- # Tensor(shape=[3, 3], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
76
- # crows=[0, 1, 2, 3],
77
- # cols=[1, 2, 0],
75
+ # Tensor(shape=[3, 3], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
76
+ # crows=[0, 1, 2, 3],
77
+ # cols=[1, 2, 0],
78
78
# values=[1., 2., 3.])
79
79
dense = paddle.ones([3, 2])
80
80
out = paddle.sparse.matmul(csr, dense)
@@ -87,9 +87,9 @@ def matmul(x, y, name=None):
87
87
indices = [[0, 1, 2], [1, 2, 0]]
88
88
values = [1., 2., 3.]
89
89
coo = paddle.sparse.sparse_coo_tensor(indices, values, [3, 3])
90
- # Tensor(shape=[3, 3], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
90
+ # Tensor(shape=[3, 3], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
91
91
# indices=[[0, 1, 2],
92
- # [1, 2, 0]],
92
+ # [1, 2, 0]],
93
93
# values=[1., 2., 3.])
94
94
dense = paddle.ones([3, 2])
95
95
out = paddle.sparse.matmul(coo, dense)
@@ -104,13 +104,13 @@ def matmul(x, y, name=None):
104
104
@dygraph_only
105
105
def masked_matmul (x , y , mask , name = None ):
106
106
"""
107
- Note:
107
+ Note:
108
108
This API is only supported from ``CUDA 11.3`` .
109
109
110
- Applies matrix multiplication of two Dense Tensors.
111
-
110
+ Applies matrix multiplication of two Dense Tensors.
111
+
112
112
The supported input/output Tensor layout are as follows:
113
-
113
+
114
114
Note:
115
115
x[DenseTensor] @ y[DenseTensor] * mask[SparseCooTensor] -> out[SparseCooTensor]
116
116
x[DenseTensor] @ y[DenseTensor] * mask[SparseCsrTensor] -> out[SparseCsrTensor]
@@ -153,9 +153,9 @@ def masked_matmul(x, y, mask, name=None):
153
153
y = paddle.rand([5, 4])
154
154
155
155
out = paddle.sparse.masked_matmul(x, y, mask)
156
- # Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
157
- # crows=[0, 2, 3, 5],
158
- # cols=[1, 3, 2, 0, 1],
156
+ # Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
157
+ # crows=[0, 2, 3, 5],
158
+ # cols=[1, 3, 2, 0, 1],
159
159
# values=[0.98986477, 0.97800624, 1.14591956, 0.68561077, 0.94714981])
160
160
161
161
"""
@@ -165,11 +165,11 @@ def masked_matmul(x, y, mask, name=None):
165
165
@dygraph_only
166
166
def mv (x , vec , name = None ):
167
167
"""
168
- Note:
168
+ Note:
169
169
This API is only supported from ``CUDA 11.0`` .
170
170
171
- Applies matrix-vector product of Sparse Matrix 'x' and Dense vector 'vec' .
172
-
171
+ Applies matrix-vector product of Sparse Matrix 'x' and Dense vector 'vec' .
172
+
173
173
The supported input/output Tensor layout are as follows:
174
174
175
175
Note:
@@ -178,39 +178,39 @@ def mv(x, vec, name=None):
178
178
179
179
It supports backward propagation.
180
180
181
- The shape of `x` should be `[M, N]` , and the shape of `y` should be `[N]` ,
181
+ The shape of `x` should be `[M, N]` , and the shape of `y` should be `[N]` ,
182
182
and the shape of `out` will be `[M]` .
183
183
184
184
Args:
185
185
x (Tensor): The input 2D tensor. It must be SparseCooTensor/SparseCsrTensor. The data type can be float32 or float64.
186
186
y (Tensor): The input 1D tensor. It must be DenseTensor vector. The data type can be float32 or float64.
187
187
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
188
-
188
+
189
189
Returns:
190
190
Tensor: 1D Tensor.
191
191
192
192
Examples:
193
193
194
194
.. code-block:: python
195
-
195
+
196
196
# required: gpu
197
197
import paddle
198
- from paddle.fluid.framework import _test_eager_guard
198
+ from paddle.fluid.framework import _test_eager_guard
199
199
paddle.seed(100)
200
200
201
201
# csr @ dense -> dense
202
- with _test_eager_guard():
202
+ with _test_eager_guard():
203
203
crows = [0, 2, 3, 5]
204
204
cols = [1, 3, 2, 0, 1]
205
205
values = [1., 2., 3., 4., 5.]
206
206
dense_shape = [3, 4]
207
207
csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape)
208
- # Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
209
- # crows=[0, 2, 3, 5],
210
- # cols=[1, 3, 2, 0, 1],
208
+ # Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
209
+ # crows=[0, 2, 3, 5],
210
+ # cols=[1, 3, 2, 0, 1],
211
211
# values=[1., 2., 3., 4., 5.])
212
212
vec = paddle.randn([4])
213
-
213
+
214
214
out = paddle.sparse.mv(csr, vec)
215
215
# Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
216
216
# [-3.85499096, -2.42975140, -1.75087738])
@@ -241,17 +241,15 @@ def add(x, y, name=None):
241
241
.. code-block:: python
242
242
243
243
import paddle
244
- from paddle.fluid.framework import _test_eager_guard
245
244
246
245
paddle.device.set_device("cpu")
247
246
248
- with _test_eager_guard():
249
- x = paddle.to_tensor([[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]], 'float32')
250
- y = paddle.to_tensor([[0, 0, 0, -2], [0, 2, -3, 0], [2, 3, 4, 8]], 'float32')
251
- sparse_x = x.to_sparse_csr()
252
- sparse_y = y.to_sparse_csr()
253
- sparse_z = paddle.sparse.add(sparse_x, sparse_y)
254
- print(sparse_z.to_dense())
247
+ x = paddle.to_tensor([[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]], 'float32')
248
+ y = paddle.to_tensor([[0, 0, 0, -2], [0, 2, -3, 0], [2, 3, 4, 8]], 'float32')
249
+ sparse_x = x.to_sparse_csr()
250
+ sparse_y = y.to_sparse_csr()
251
+ sparse_z = paddle.sparse.add(sparse_x, sparse_y)
252
+ print(sparse_z.to_dense())
255
253
256
254
# [[ 0., -1., 0., 0.],
257
255
# [ 0., 2., -6., 0.],
@@ -268,10 +266,9 @@ def add(x, y, name=None):
268
266
inputs = {'x' : x , 'y' : y }
269
267
helper = LayerHelper (op_type )
270
268
out = helper .create_sparse_variable_for_type_inference (x .dtype )
271
- helper .append_op (type = op_type ,
272
- inputs = inputs ,
273
- outputs = {'out' : out },
274
- attrs = {})
269
+ helper .append_op (
270
+ type = op_type , inputs = inputs , outputs = {'out' : out }, attrs = {}
271
+ )
275
272
return out
276
273
277
274
@@ -298,17 +295,15 @@ def subtract(x, y, name=None):
298
295
.. code-block:: python
299
296
300
297
import paddle
301
- from paddle.fluid.framework import _test_eager_guard
302
298
303
299
paddle.device.set_device("cpu")
304
300
305
- with _test_eager_guard():
306
- x = paddle.to_tensor([[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]], 'float32')
307
- y = paddle.to_tensor([[0, 0, 0, -2], [0, 2, -3, 0], [2, 3, 4, 8]], 'float32')
308
- sparse_x = x.to_sparse_csr()
309
- sparse_y = y.to_sparse_csr()
310
- sparse_z = paddle.sparse.subtract(sparse_x, sparse_y)
311
- print(sparse_z.to_dense())
301
+ x = paddle.to_tensor([[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]], 'float32')
302
+ y = paddle.to_tensor([[0, 0, 0, -2], [0, 2, -3, 0], [2, 3, 4, 8]], 'float32')
303
+ sparse_x = x.to_sparse_csr()
304
+ sparse_y = y.to_sparse_csr()
305
+ sparse_z = paddle.sparse.subtract(sparse_x, sparse_y)
306
+ print(sparse_z.to_dense())
312
307
313
308
# [[ 0., -1., 0., 4.],
314
309
# [ 0., -2., 0., 0.],
@@ -343,17 +338,15 @@ def multiply(x, y, name=None):
343
338
.. code-block:: python
344
339
345
340
import paddle
346
- from paddle.fluid.framework import _test_eager_guard
347
341
348
342
paddle.device.set_device("cpu")
349
343
350
- with _test_eager_guard():
351
- x = paddle.to_tensor([[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]], 'float32')
352
- y = paddle.to_tensor([[0, 0, 0, -2], [0, 2, -3, 0], [2, 3, 4, 8]], 'float32')
353
- sparse_x = x.to_sparse_csr()
354
- sparse_y = y.to_sparse_csr()
355
- sparse_z = paddle.sparse.multiply(sparse_x, sparse_y)
356
- print(sparse_z.to_dense())
344
+ x = paddle.to_tensor([[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]], 'float32')
345
+ y = paddle.to_tensor([[0, 0, 0, -2], [0, 2, -3, 0], [2, 3, 4, 8]], 'float32')
346
+ sparse_x = x.to_sparse_csr()
347
+ sparse_y = y.to_sparse_csr()
348
+ sparse_z = paddle.sparse.multiply(sparse_x, sparse_y)
349
+ print(sparse_z.to_dense())
357
350
358
351
# [[ 0., 0., 0., -4.],
359
352
# [ 0., 0., 9., 0.],
@@ -391,17 +384,15 @@ def divide(x, y, name=None):
391
384
.. code-block:: python
392
385
393
386
import paddle
394
- from paddle.fluid.framework import _test_eager_guard
395
387
396
388
paddle.device.set_device("cpu")
397
389
398
- with _test_eager_guard():
399
- x = paddle.to_tensor([[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]], 'float32')
400
- y = paddle.to_tensor([[0, 0, 0, -2], [0, 2, -3, 0], [2, 3, 4, 8]], 'float32')
401
- sparse_x = x.to_sparse_csr()
402
- sparse_y = y.to_sparse_csr()
403
- sparse_z = paddle.sparse.divide(sparse_x, sparse_y)
404
- print(sparse_z.to_dense())
390
+ x = paddle.to_tensor([[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]], 'float32')
391
+ y = paddle.to_tensor([[0, 0, 0, -2], [0, 2, -3, 0], [2, 3, 4, 8]], 'float32')
392
+ sparse_x = x.to_sparse_csr()
393
+ sparse_y = y.to_sparse_csr()
394
+ sparse_z = paddle.sparse.divide(sparse_x, sparse_y)
395
+ print(sparse_z.to_dense())
405
396
406
397
# [[ nan , -inf. , nan , -1. ],
407
398
# [ nan , 0. , 1. , nan ],
0 commit comments