未验证 提交 fc1e1b77 编写于 作者: 张春乔 提交者: GitHub

[xdoctest] reformat example code with google style in No. 240 (#56474)

* 240

* fix bugs

* fix bugs
上级 daac3829
...@@ -57,17 +57,14 @@ class CheckMethod(Enum): ...@@ -57,17 +57,14 @@ class CheckMethod(Enum):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
from paddle.incubate.asp import CheckMethod, MaskAlgo >>> from paddle.incubate.asp import CheckMethod, MaskAlgo
>>> print(CheckMethod.get_checking_method(MaskAlgo.MASK_1D))
CheckMethod.get_checking_method(MaskAlgo.MASK_1D) CheckMethod.CHECK_1D
# CheckMethod.CHECK_1D >>> print(CheckMethod.get_checking_method(MaskAlgo.MASK_2D_GREEDY))
CheckMethod.CHECK_2D
CheckMethod.get_checking_method(MaskAlgo.MASK_2D_GREEDY) >>> print(CheckMethod.get_checking_method(MaskAlgo.MASK_2D_BEST))
# CheckMethod.CHECK_2D CheckMethod.CHECK_2D
CheckMethod.get_checking_method(MaskAlgo.MASK_2D_BEST)
# CheckMethod.CHECK_2D
""" """
assert isinstance( assert isinstance(
mask_algo, MaskAlgo mask_algo, MaskAlgo
...@@ -92,12 +89,14 @@ def calculate_density(x): ...@@ -92,12 +89,14 @@ def calculate_density(x):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import numpy as np >>> import numpy as np
x = np.array([[0, 1, 3, 0], >>> x = np.array([[0, 1, 3, 0],
[1, 1, 0, 1]]) ... [1, 1, 0, 1]])
paddle.incubate.asp.calculate_density(x) # 0.625 >>> out = paddle.incubate.asp.calculate_density(x)
>>> print(out)
0.625
""" """
x_flattened = x.flatten() x_flattened = x.flatten()
...@@ -149,21 +148,27 @@ def check_mask_1d(mat, n, m): ...@@ -149,21 +148,27 @@ def check_mask_1d(mat, n, m):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
import paddle.incubate.asp as sparsity >>> import paddle.incubate.asp as sparsity
x = np.array([[0, 1, 3, 0], >>> x = np.array([[0, 1, 3, 0],
[1, 0, 0, 1]]) ... [1, 0, 0, 1]])
sparsity.check_mask_1d(x, 2, 4) # True >>> y = sparsity.check_mask_1d(x, 2, 4)
>>> print(y)
x = np.array([[0, 1, 5, 4], True
[1, 0, 0, 1]])
sparsity.check_mask_1d(x, 2, 4) # False >>> x = np.array([[0, 1, 5, 4],
... [1, 0, 0, 1]])
# x would be padded to shape (2, 8) >>> y = sparsity.check_mask_1d(x, 2, 4)
x = np.array([[0, 1, 0, 4, 6], >>> print(y)
[1, 0, 0, 1, 7]]) False
sparsity.check_mask_1d(x, 2, 4) # True
>>> # x would be padded to shape (2, 8)
>>> x = np.array([[0, 1, 0, 4, 6],
... [1, 0, 0, 1, 7]])
>>> y = sparsity.check_mask_1d(x, 2, 4)
>>> print(y)
True
""" """
if len(mat.shape) <= 1: if len(mat.shape) <= 1:
mat_flattern, shape = _reshape_1d(mat.reshape(1, mat.shape[0]), m) mat_flattern, shape = _reshape_1d(mat.reshape(1, mat.shape[0]), m)
...@@ -193,15 +198,17 @@ def get_mask_1d(mat, n, m): ...@@ -193,15 +198,17 @@ def get_mask_1d(mat, n, m):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
import paddle.incubate.asp as sparsity >>> import paddle.incubate.asp as sparsity
>>> mat = np.array([[0, 1, 5, 4],
mat = np.array([[0, 1, 5, 4], ... [2, 7, 3, 6]])
[2, 7, 3, 6]]) >>> mask = sparsity.get_mask_1d(mat, 2, 4)
mask = sparsity.get_mask_1d(mat, 2, 4) >>> print(mask)
# nparray([[0, 0, 1, 1], [[0 0 1 1]
# [0, 1, 0, 1]]) [0 1 0 1]]
sparsity.check_mask_1d(mask, 2, 4) # True >>> y = sparsity.check_mask_1d(mask, 2, 4)
>>> print(y)
True
""" """
mat_flattern, shape = _reshape_1d(mat, m) mat_flattern, shape = _reshape_1d(mat, m)
...@@ -277,28 +284,34 @@ def check_mask_2d(mat, n, m): ...@@ -277,28 +284,34 @@ def check_mask_2d(mat, n, m):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
import paddle.incubate.asp as sparsity >>> import paddle.incubate.asp as sparsity
x = np.array([[0, 8, 9, 0], >>> x = np.array([[0, 8, 9, 0],
[9, 0, 0, 10], ... [9, 0, 0, 10],
[5, 0, 0, 6], ... [5, 0, 0, 6],
[0, 4, 6, 0]]) ... [0, 4, 6, 0]])
sparsity.check_mask_2d(x, 2, 4) # True >>> y = sparsity.check_mask_2d(x, 2, 4)
>>> print(y)
x = np.array([[0, 8, 0, 9], True
[9, 0, 0, 10],
[0, 5, 0, 6], >>> x = np.array([[0, 8, 0, 9],
[0, 4, 6, 0]]) ... [9, 0, 0, 10],
sparsity.check_mask_2d(x, 2, 4) # False ... [0, 5, 0, 6],
... [0, 4, 6, 0]])
# x would be padded to shape (8, 8) >>> y = sparsity.check_mask_2d(x, 2, 4)
x = np.array([[0, 8, 0, 9], >>> print(y)
[9, 0, 7, 0], True
[0, 5, 0, 6],
[3, 0, 6, 0], >>> # x would be padded to shape (8, 8)
[1, 1, 0, 1]]) >>> x = np.array([[0, 8, 0, 9],
sparsity.check_mask_2d(x, 2, 4) # True ... [9, 0, 7, 0],
... [0, 5, 0, 6],
... [3, 0, 6, 0],
... [1, 1, 0, 1]])
>>> y = sparsity.check_mask_2d(x, 2, 4)
>>> print(y)
True
""" """
mat_padded, shape = _reshape_2d(mat, m) mat_padded, shape = _reshape_2d(mat, m)
for sub_mat in mat_padded: for sub_mat in mat_padded:
...@@ -328,19 +341,22 @@ def get_mask_2d_greedy(mat, n, m): ...@@ -328,19 +341,22 @@ def get_mask_2d_greedy(mat, n, m):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
import paddle.incubate.asp as sparsity >>> import paddle.incubate.asp as sparsity
mat = np.array([[9, 8, 3, 7], >>> mat = np.array([[9, 8, 3, 7],
[9, 2, 1, 10], ... [9, 2, 1, 10],
[5, 1, 3, 6], ... [5, 1, 3, 6],
[2, 4, 6, 1]]) ... [2, 4, 6, 1]])
mask = sparsity.get_mask_2d_greedy(mat, 2, 4) >>> mask = sparsity.get_mask_2d_greedy(mat, 2, 4)
# nparray([[1. 1. 0. 0.] >>> print(mask)
# [1. 0. 0. 1.] [[1. 1. 0. 0.]
# [0. 0. 1. 1.] [1. 0. 0. 1.]
# [0. 1. 1. 0.]]) [0. 0. 1. 1.]
sparsity.check_mask_2d(mask, 2, 4) # True [0. 1. 1. 0.]]
>>> y = sparsity.check_mask_2d(mask, 2, 4)
>>> print(y)
True
""" """
mat_padded, shape = _reshape_2d(mat, m) mat_padded, shape = _reshape_2d(mat, m)
mask_padded = np.zeros_like(mat_padded).reshape(-1, m, m) mask_padded = np.zeros_like(mat_padded).reshape(-1, m, m)
...@@ -443,17 +459,19 @@ def get_mask_2d_best(mat, n, m): ...@@ -443,17 +459,19 @@ def get_mask_2d_best(mat, n, m):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
import paddle.incubate.asp as sparsity >>> import paddle.incubate.asp as sparsity
mat = np.array([[2, 8, 9, 9], >>> mat = np.array([[2, 8, 9, 9],
[9, 1, 3, 9], ... [9, 1, 3, 9],
[5, 6, 3, 9], ... [5, 6, 3, 9],
[2, 4, 6, 9]]) ... [2, 4, 6, 9]])
mask_greedy = sparsity.get_mask_2d_greedy(mat, 2, 4) >>> mask_greedy = sparsity.get_mask_2d_greedy(mat, 2, 4)
mask_best = sparsity.get_mask_2d_best(mat, 2, 4) >>> mask_best = sparsity.get_mask_2d_best(mat, 2, 4)
print("L1 norm of `greedy` sparse matrix", np.multiply(mat, mask_greedy).sum()) # 56 >>> print("L1 norm of `greedy` sparse matrix", np.multiply(mat, mask_greedy).sum())
print("L1 norm of `best` sparse matrix", np.multiply(mat, mask_best).sum()) # 61 L1 norm of `greedy` sparse matrix 56.0
>>> print("L1 norm of `best` sparse matrix", np.multiply(mat, mask_best).sum())
L1 norm of `best` sparse matrix 61.0
""" """
patterns = _compute_valid_2d_patterns(n, m) patterns = _compute_valid_2d_patterns(n, m)
...@@ -492,23 +510,25 @@ def create_mask(tensor, func_name=MaskAlgo.MASK_1D, n=2, m=4): ...@@ -492,23 +510,25 @@ def create_mask(tensor, func_name=MaskAlgo.MASK_1D, n=2, m=4):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
import paddle.incubate.asp as sparsity >>> import paddle.incubate.asp as sparsity
tensor = np.array([[2, 8, 9, 9], >>> tensor = np.array([[2, 8, 9, 9],
[9, 1, 3, 9], ... [9, 1, 3, 9],
[5, 6, 3, 9], ... [5, 6, 3, 9],
[2, 4, 6, 9]]) ... [2, 4, 6, 9]])
mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D) >>> mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D)
# nparray([[0 0 1 1], >>> print(mask_1d)
# [1 0 0 1], [[0 0 1 1]
# [0 1 0 1], [1 0 0 1]
# [0 0 1 1]]) [0 1 0 1]
mask_2d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_2D_BEST) [0 0 1 1]]
# nparray([[0 1 1 0], >>> mask_2d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_2D_BEST)
# [1 0 0 1], >>> print(mask_2d)
# [1 1 0 0], [[0 1 1 0]
# [0 0 1 1]]) [1 0 0 1]
[1 1 0 0]
[0 0 1 1]]
""" """
shape = tensor.shape shape = tensor.shape
dtype = tensor.dtype dtype = tensor.dtype
...@@ -561,20 +581,25 @@ def check_sparsity(tensor, func_name=CheckMethod.CHECK_1D, n=2, m=4): ...@@ -561,20 +581,25 @@ def check_sparsity(tensor, func_name=CheckMethod.CHECK_1D, n=2, m=4):
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
import paddle.incubate.asp as sparsity >>> import paddle.incubate.asp as sparsity
tensor = np.array([[2, 8, 9, 9], >>> tensor = np.array([[2, 8, 9, 9],
[9, 1, 3, 9], ... [9, 1, 3, 9],
[5, 6, 3, 9], ... [5, 6, 3, 9],
[2, 4, 6, 9]]) ... [2, 4, 6, 9]])
mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D) >>> mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D)
# nparray([[0 0 1 1], >>> print(mask_1d)
# [1 0 0 1], [[0 0 1 1]
# [0 1 0 1], [1 0 0 1]
# [0 0 1 1]]) [0 1 0 1]
sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_1D) # True [0 0 1 1]]
sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_2D) # False >>> y = sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_1D)
>>> print(y)
True
>>> y = sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_2D)
>>> print(y)
True
""" """
shape = tensor.shape shape = tensor.shape
t = tensor.astype(float) t = tensor.astype(float)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册