Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
fc1e1b77
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
fc1e1b77
编写于
8月 29, 2023
作者:
张
张春乔
提交者:
GitHub
8月 29, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[xdoctest] reformat example code with google style in No. 240 (#56474)
* 240 * fix bugs * fix bugs
上级
daac3829
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
142 addition
and
117 deletion
+142
-117
python/paddle/incubate/asp/utils.py
python/paddle/incubate/asp/utils.py
+142
-117
未找到文件。
python/paddle/incubate/asp/utils.py
浏览文件 @
fc1e1b77
...
...
@@ -57,17 +57,14 @@ class CheckMethod(Enum):
Examples:
.. code-block:: python
import numpy as np
from paddle.incubate.asp import CheckMethod, MaskAlgo
CheckMethod.get_checking_method(MaskAlgo.MASK_1D)
# CheckMethod.CHECK_1D
CheckMethod.get_checking_method(MaskAlgo.MASK_2D_GREEDY)
# CheckMethod.CHECK_2D
CheckMethod.get_checking_method(MaskAlgo.MASK_2D_BEST)
# CheckMethod.CHECK_2D
>>> import numpy as np
>>> from paddle.incubate.asp import CheckMethod, MaskAlgo
>>> print(CheckMethod.get_checking_method(MaskAlgo.MASK_1D))
CheckMethod.CHECK_1D
>>> print(CheckMethod.get_checking_method(MaskAlgo.MASK_2D_GREEDY))
CheckMethod.CHECK_2D
>>> print(CheckMethod.get_checking_method(MaskAlgo.MASK_2D_BEST))
CheckMethod.CHECK_2D
"""
assert
isinstance
(
mask_algo
,
MaskAlgo
...
...
@@ -92,12 +89,14 @@ def calculate_density(x):
Examples:
.. code-block:: python
import paddle
import numpy as np
>>>
import paddle
>>>
import numpy as np
x = np.array([[0, 1, 3, 0],
[1, 1, 0, 1]])
paddle.incubate.asp.calculate_density(x) # 0.625
>>> x = np.array([[0, 1, 3, 0],
... [1, 1, 0, 1]])
>>> out = paddle.incubate.asp.calculate_density(x)
>>> print(out)
0.625
"""
x_flattened
=
x
.
flatten
()
...
...
@@ -149,21 +148,27 @@ def check_mask_1d(mat, n, m):
Examples:
.. code-block:: python
import numpy as np
import paddle.incubate.asp as sparsity
x = np.array([[0, 1, 3, 0],
[1, 0, 0, 1]])
sparsity.check_mask_1d(x, 2, 4) # True
x = np.array([[0, 1, 5, 4],
[1, 0, 0, 1]])
sparsity.check_mask_1d(x, 2, 4) # False
# x would be padded to shape (2, 8)
x = np.array([[0, 1, 0, 4, 6],
[1, 0, 0, 1, 7]])
sparsity.check_mask_1d(x, 2, 4) # True
>>> import numpy as np
>>> import paddle.incubate.asp as sparsity
>>> x = np.array([[0, 1, 3, 0],
... [1, 0, 0, 1]])
>>> y = sparsity.check_mask_1d(x, 2, 4)
>>> print(y)
True
>>> x = np.array([[0, 1, 5, 4],
... [1, 0, 0, 1]])
>>> y = sparsity.check_mask_1d(x, 2, 4)
>>> print(y)
False
>>> # 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
:
mat_flattern
,
shape
=
_reshape_1d
(
mat
.
reshape
(
1
,
mat
.
shape
[
0
]),
m
)
...
...
@@ -193,15 +198,17 @@ def get_mask_1d(mat, n, m):
Examples:
.. code-block:: python
import numpy as np
import paddle.incubate.asp as sparsity
mat = np.array([[0, 1, 5, 4],
[2, 7, 3, 6]])
mask = sparsity.get_mask_1d(mat, 2, 4)
# nparray([[0, 0, 1, 1],
# [0, 1, 0, 1]])
sparsity.check_mask_1d(mask, 2, 4) # True
>>> import numpy as np
>>> import paddle.incubate.asp as sparsity
>>> mat = np.array([[0, 1, 5, 4],
... [2, 7, 3, 6]])
>>> mask = sparsity.get_mask_1d(mat, 2, 4)
>>> print(mask)
[[0 0 1 1]
[0 1 0 1]]
>>> y = sparsity.check_mask_1d(mask, 2, 4)
>>> print(y)
True
"""
mat_flattern
,
shape
=
_reshape_1d
(
mat
,
m
)
...
...
@@ -277,28 +284,34 @@ def check_mask_2d(mat, n, m):
Examples:
.. code-block:: python
import numpy as np
import paddle.incubate.asp as sparsity
x = np.array([[0, 8, 9, 0],
[9, 0, 0, 10],
[5, 0, 0, 6],
[0, 4, 6, 0]])
sparsity.check_mask_2d(x, 2, 4) # True
x = np.array([[0, 8, 0, 9],
[9, 0, 0, 10],
[0, 5, 0, 6],
[0, 4, 6, 0]])
sparsity.check_mask_2d(x, 2, 4) # False
# x would be padded to shape (8, 8)
x = np.array([[0, 8, 0, 9],
[9, 0, 7, 0],
[0, 5, 0, 6],
[3, 0, 6, 0],
[1, 1, 0, 1]])
sparsity.check_mask_2d(x, 2, 4) # True
>>> import numpy as np
>>> import paddle.incubate.asp as sparsity
>>> x = np.array([[0, 8, 9, 0],
... [9, 0, 0, 10],
... [5, 0, 0, 6],
... [0, 4, 6, 0]])
>>> y = sparsity.check_mask_2d(x, 2, 4)
>>> print(y)
True
>>> x = np.array([[0, 8, 0, 9],
... [9, 0, 0, 10],
... [0, 5, 0, 6],
... [0, 4, 6, 0]])
>>> y = sparsity.check_mask_2d(x, 2, 4)
>>> print(y)
True
>>> # x would be padded to shape (8, 8)
>>> x = np.array([[0, 8, 0, 9],
... [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
)
for
sub_mat
in
mat_padded
:
...
...
@@ -328,19 +341,22 @@ def get_mask_2d_greedy(mat, n, m):
Examples:
.. code-block:: python
import numpy as np
import paddle.incubate.asp as sparsity
mat = np.array([[9, 8, 3, 7],
[9, 2, 1, 10],
[5, 1, 3, 6],
[2, 4, 6, 1]])
mask = sparsity.get_mask_2d_greedy(mat, 2, 4)
# nparray([[1. 1. 0. 0.]
# [1. 0. 0. 1.]
# [0. 0. 1. 1.]
# [0. 1. 1. 0.]])
sparsity.check_mask_2d(mask, 2, 4) # True
>>> import numpy as np
>>> import paddle.incubate.asp as sparsity
>>> mat = np.array([[9, 8, 3, 7],
... [9, 2, 1, 10],
... [5, 1, 3, 6],
... [2, 4, 6, 1]])
>>> mask = sparsity.get_mask_2d_greedy(mat, 2, 4)
>>> print(mask)
[[1. 1. 0. 0.]
[1. 0. 0. 1.]
[0. 0. 1. 1.]
[0. 1. 1. 0.]]
>>> y = sparsity.check_mask_2d(mask, 2, 4)
>>> print(y)
True
"""
mat_padded
,
shape
=
_reshape_2d
(
mat
,
m
)
mask_padded
=
np
.
zeros_like
(
mat_padded
).
reshape
(
-
1
,
m
,
m
)
...
...
@@ -443,17 +459,19 @@ def get_mask_2d_best(mat, n, m):
Examples:
.. code-block:: python
import numpy as np
import paddle.incubate.asp as sparsity
mat = np.array([[2, 8, 9, 9],
[9, 1, 3, 9],
[5, 6, 3, 9],
[2, 4, 6, 9]])
mask_greedy = sparsity.get_mask_2d_greedy(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 `best` sparse matrix", np.multiply(mat, mask_best).sum()) # 61
>>> import numpy as np
>>> import paddle.incubate.asp as sparsity
>>> mat = np.array([[2, 8, 9, 9],
... [9, 1, 3, 9],
... [5, 6, 3, 9],
... [2, 4, 6, 9]])
>>> mask_greedy = sparsity.get_mask_2d_greedy(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())
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
)
...
...
@@ -492,23 +510,25 @@ def create_mask(tensor, func_name=MaskAlgo.MASK_1D, n=2, m=4):
Examples:
.. code-block:: python
import numpy as np
import paddle.incubate.asp as sparsity
tensor = np.array([[2, 8, 9, 9],
[9, 1, 3, 9],
[5, 6, 3, 9],
[2, 4, 6, 9]])
mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D)
# nparray([[0 0 1 1],
# [1 0 0 1],
# [0 1 0 1],
# [0 0 1 1]])
mask_2d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_2D_BEST)
# nparray([[0 1 1 0],
# [1 0 0 1],
# [1 1 0 0],
# [0 0 1 1]])
>>> import numpy as np
>>> import paddle.incubate.asp as sparsity
>>> tensor = np.array([[2, 8, 9, 9],
... [9, 1, 3, 9],
... [5, 6, 3, 9],
... [2, 4, 6, 9]])
>>> mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D)
>>> print(mask_1d)
[[0 0 1 1]
[1 0 0 1]
[0 1 0 1]
[0 0 1 1]]
>>> mask_2d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_2D_BEST)
>>> print(mask_2d)
[[0 1 1 0]
[1 0 0 1]
[1 1 0 0]
[0 0 1 1]]
"""
shape
=
tensor
.
shape
dtype
=
tensor
.
dtype
...
...
@@ -561,20 +581,25 @@ def check_sparsity(tensor, func_name=CheckMethod.CHECK_1D, n=2, m=4):
Examples:
.. code-block:: python
import numpy as np
import paddle.incubate.asp as sparsity
tensor = np.array([[2, 8, 9, 9],
[9, 1, 3, 9],
[5, 6, 3, 9],
[2, 4, 6, 9]])
mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D)
# nparray([[0 0 1 1],
# [1 0 0 1],
# [0 1 0 1],
# [0 0 1 1]])
sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_1D) # True
sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_2D) # False
>>> import numpy as np
>>> import paddle.incubate.asp as sparsity
>>> tensor = np.array([[2, 8, 9, 9],
... [9, 1, 3, 9],
... [5, 6, 3, 9],
... [2, 4, 6, 9]])
>>> mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D)
>>> print(mask_1d)
[[0 0 1 1]
[1 0 0 1]
[0 1 0 1]
[0 0 1 1]]
>>> 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
t
=
tensor
.
astype
(
float
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录