Skip to content

Commit 61953b9

Browse files
author
zhangkaihuo
authored
[cherry-pick]Fix english documents of sparse api (#47496)
Fix english documents of sparse api
1 parent 601626a commit 61953b9

File tree

8 files changed

+558
-534
lines changed

8 files changed

+558
-534
lines changed

python/paddle/sparse/binary.py

Lines changed: 57 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
@dygraph_only
3434
def matmul(x, y, name=None):
3535
"""
36-
Note:
36+
Note:
3737
This API is only supported from ``CUDA 11.0`` .
3838
39-
Applies matrix multiplication of two Tensors.
40-
39+
Applies matrix multiplication of two Tensors.
40+
4141
The supported input/output Tensor layout are as follows:
42-
42+
4343
Note:
4444
x[SparseCsrTensor] @ y[SparseCsrTensor] -> out[SparseCsrTensor]
4545
x[SparseCsrTensor] @ y[DenseTensor] -> out[DenseTensor]
@@ -49,14 +49,14 @@ def matmul(x, y, name=None):
4949
It supports backward propagation.
5050
5151
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 `*`
5353
is zero or more batch dimensions.
5454
5555
Args:
5656
x (Tensor): The input tensor. It can be SparseCooTensor/SparseCsrTensor. The data type can be float32 or float64.
5757
y (Tensor): The input tensor. It can be SparseCooTensor/SparseCsrTensor/DenseTensor. The data type can be float32 or float64.
5858
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
59-
59+
6060
Returns:
6161
Tensor: Its layout is determined by that of `x` and `y` .
6262
@@ -72,9 +72,9 @@ def matmul(x, y, name=None):
7272
cols = [1, 2, 0]
7373
values = [1., 2., 3.]
7474
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],
7878
# values=[1., 2., 3.])
7979
dense = paddle.ones([3, 2])
8080
out = paddle.sparse.matmul(csr, dense)
@@ -87,9 +87,9 @@ def matmul(x, y, name=None):
8787
indices = [[0, 1, 2], [1, 2, 0]]
8888
values = [1., 2., 3.]
8989
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,
9191
# indices=[[0, 1, 2],
92-
# [1, 2, 0]],
92+
# [1, 2, 0]],
9393
# values=[1., 2., 3.])
9494
dense = paddle.ones([3, 2])
9595
out = paddle.sparse.matmul(coo, dense)
@@ -104,13 +104,13 @@ def matmul(x, y, name=None):
104104
@dygraph_only
105105
def masked_matmul(x, y, mask, name=None):
106106
"""
107-
Note:
107+
Note:
108108
This API is only supported from ``CUDA 11.3`` .
109109
110-
Applies matrix multiplication of two Dense Tensors.
111-
110+
Applies matrix multiplication of two Dense Tensors.
111+
112112
The supported input/output Tensor layout are as follows:
113-
113+
114114
Note:
115115
x[DenseTensor] @ y[DenseTensor] * mask[SparseCooTensor] -> out[SparseCooTensor]
116116
x[DenseTensor] @ y[DenseTensor] * mask[SparseCsrTensor] -> out[SparseCsrTensor]
@@ -153,9 +153,9 @@ def masked_matmul(x, y, mask, name=None):
153153
y = paddle.rand([5, 4])
154154
155155
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],
159159
# values=[0.98986477, 0.97800624, 1.14591956, 0.68561077, 0.94714981])
160160
161161
"""
@@ -165,11 +165,11 @@ def masked_matmul(x, y, mask, name=None):
165165
@dygraph_only
166166
def mv(x, vec, name=None):
167167
"""
168-
Note:
168+
Note:
169169
This API is only supported from ``CUDA 11.0`` .
170170
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+
173173
The supported input/output Tensor layout are as follows:
174174
175175
Note:
@@ -178,39 +178,39 @@ def mv(x, vec, name=None):
178178
179179
It supports backward propagation.
180180
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]` ,
182182
and the shape of `out` will be `[M]` .
183183
184184
Args:
185185
x (Tensor): The input 2D tensor. It must be SparseCooTensor/SparseCsrTensor. The data type can be float32 or float64.
186186
y (Tensor): The input 1D tensor. It must be DenseTensor vector. The data type can be float32 or float64.
187187
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
188-
188+
189189
Returns:
190190
Tensor: 1D Tensor.
191191
192192
Examples:
193193
194194
.. code-block:: python
195-
195+
196196
# required: gpu
197197
import paddle
198-
from paddle.fluid.framework import _test_eager_guard
198+
from paddle.fluid.framework import _test_eager_guard
199199
paddle.seed(100)
200200
201201
# csr @ dense -> dense
202-
with _test_eager_guard():
202+
with _test_eager_guard():
203203
crows = [0, 2, 3, 5]
204204
cols = [1, 3, 2, 0, 1]
205205
values = [1., 2., 3., 4., 5.]
206206
dense_shape = [3, 4]
207207
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],
211211
# values=[1., 2., 3., 4., 5.])
212212
vec = paddle.randn([4])
213-
213+
214214
out = paddle.sparse.mv(csr, vec)
215215
# Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
216216
# [-3.85499096, -2.42975140, -1.75087738])
@@ -241,17 +241,15 @@ def add(x, y, name=None):
241241
.. code-block:: python
242242
243243
import paddle
244-
from paddle.fluid.framework import _test_eager_guard
245244
246245
paddle.device.set_device("cpu")
247246
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())
255253
256254
# [[ 0., -1., 0., 0.],
257255
# [ 0., 2., -6., 0.],
@@ -268,10 +266,9 @@ def add(x, y, name=None):
268266
inputs = {'x': x, 'y': y}
269267
helper = LayerHelper(op_type)
270268
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+
)
275272
return out
276273

277274

@@ -298,17 +295,15 @@ def subtract(x, y, name=None):
298295
.. code-block:: python
299296
300297
import paddle
301-
from paddle.fluid.framework import _test_eager_guard
302298
303299
paddle.device.set_device("cpu")
304300
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())
312307
313308
# [[ 0., -1., 0., 4.],
314309
# [ 0., -2., 0., 0.],
@@ -343,17 +338,15 @@ def multiply(x, y, name=None):
343338
.. code-block:: python
344339
345340
import paddle
346-
from paddle.fluid.framework import _test_eager_guard
347341
348342
paddle.device.set_device("cpu")
349343
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())
357350
358351
# [[ 0., 0., 0., -4.],
359352
# [ 0., 0., 9., 0.],
@@ -391,17 +384,15 @@ def divide(x, y, name=None):
391384
.. code-block:: python
392385
393386
import paddle
394-
from paddle.fluid.framework import _test_eager_guard
395387
396388
paddle.device.set_device("cpu")
397389
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())
405396
406397
# [[ nan , -inf. , nan , -1. ],
407398
# [ nan , 0. , 1. , nan ],

0 commit comments

Comments
 (0)