Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asp movement #36525

Merged
merged 17 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions python/paddle/fluid/contrib/sparsity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
from .utils import check_sparsity
from .utils import MaskAlgo
from .utils import CheckMethod
from .asp import decorate, prune_model
from .asp import set_excluded_layers, reset_excluded_layers
from .asp import decorate
from .asp import prune_model
from .asp import set_excluded_layers
from .asp import reset_excluded_layers

__all__ = [
'calculate_density', 'check_mask_1d', 'get_mask_1d', 'check_mask_2d',
Expand Down
12 changes: 5 additions & 7 deletions python/paddle/fluid/contrib/sparsity/asp.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def decorate(optimizer):

import paddle
import paddle.fluid as fluid
from paddle.fluid.contrib import sparsity
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要对用户暴露的接口的示例代码中,不要再import fluid,也不要用任何fluid的API。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的 已經全面修正

from paddle.static import sparsity

main_program = fluid.Program()
startup_program = fluid.Program()
Expand Down Expand Up @@ -128,17 +128,13 @@ def prune_model(place,
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.contrib import sparsity
from paddle.static import sparsity

paddle.enable_static()

main_program = fluid.Program()
startup_program = fluid.Program()

place = paddle.CPUPlace()
if core.is_compiled_with_cuda():
place = paddle.CUDAPlace(0)

with fluid.program_guard(main_program, startup_program):
input_data = fluid.layers.data(name='data', shape=[None, 128])
label = fluid.layers.data(name='label', shape=[None, 10])
Expand All @@ -158,6 +154,8 @@ def prune_model(place,
optimizer = sparsity.decorate(optimizer)
optimizer.minimize(loss, startup_program)

place = paddle.CPUPlace()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要设置CPUPlace

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修改成使用paddle.get_device來判斷Place的裝置


exe = fluid.Executor(place)
exe.run(startup_program)

Expand Down Expand Up @@ -348,7 +346,7 @@ def _is_supported_layer(cls, main_program, param_name):
.. code-block:: python

import paddle.fluid as fluid
from paddle.fluid.contrib.sparsity.asp import ASPHelper
from paddle.static.sparsity.asp import ASPHelper

main_program = fluid.Program()
startup_program = fluid.Program()
Expand Down
20 changes: 10 additions & 10 deletions python/paddle/fluid/contrib/sparsity/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_checking_method(mask_algo):
.. code-block:: python

import numpy as np
from paddle.fluid.contrib.sparsity import MaskAlgo, CheckMethod
from paddle.static.sparsity import MaskAlgo, CheckMethod

CheckMethod.get_checking_method(MaskAlgo.MASK_1D)
# CheckMethod.CHECK_1D
Expand Down Expand Up @@ -95,7 +95,7 @@ def calculate_density(x):
.. code-block:: python

import numpy as np
import paddle.fluid.contrib.sparsity as sparsity
import paddle.static.sparsity as sparsity

x = np.array([[0, 1, 3, 0],
[1, 1, 0, 1]])
Expand Down Expand Up @@ -151,7 +151,7 @@ def check_mask_1d(mat, n, m):
.. code-block:: python

import numpy as np
import paddle.fluid.contrib.sparsity as sparsity
import paddle.static.sparsity as sparsity

x = np.array([[0, 1, 3, 0],
[1, 0, 0, 1]])
Expand Down Expand Up @@ -195,7 +195,7 @@ def get_mask_1d(mat, n, m):
.. code-block:: python

import numpy as np
import paddle.fluid.contrib.sparsity as sparsity
import paddle.static.sparsity as sparsity

mat = np.array([[0, 1, 5, 4],
[2, 7, 3, 6]])
Expand Down Expand Up @@ -279,7 +279,7 @@ def check_mask_2d(mat, n, m):
.. code-block:: python

import numpy as np
import paddle.fluid.contrib.sparsity as sparsity
import paddle.static.sparsity as sparsity

x = np.array([[0, 8, 9, 0],
[9, 0, 0, 10],
Expand Down Expand Up @@ -329,7 +329,7 @@ def get_mask_2d_greedy(mat, n, m):
.. code-block:: python

import numpy as np
import paddle.fluid.contrib.sparsity as sparsity
import paddle.static.sparsity as sparsity

mat = np.array([[9, 8, 3, 7],
[9, 2, 1, 10],
Expand Down Expand Up @@ -439,14 +439,14 @@ def get_mask_2d_best(mat, n, m):
.. code-block:: python

import numpy as np
import paddle.fluid.contrib.sparsity as sparsity
import paddle.static.sparsity 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_greedy = 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 `best` sparse matrix", np.multiply(mat, mask_best).sum()) # 61
"""
Expand Down Expand Up @@ -487,7 +487,7 @@ def create_mask(tensor, func_name=MaskAlgo.MASK_1D, n=2, m=4):
.. code-block:: python

import numpy as np
import paddle.fluid.contrib.sparsity as sparsity
import paddle.static.sparsity as sparsity

tensor = np.array([[2, 8, 9, 9],
[9, 1, 3, 9],
Expand Down Expand Up @@ -549,7 +549,7 @@ def check_sparsity(tensor, func_name=CheckMethod.CHECK_1D, n=2, m=4):
.. code-block:: python

import numpy as np
import paddle.fluid.contrib.sparsity as sparsity
import paddle.static.sparsity as sparsity

tensor = np.array([[2, 8, 9, 9],
[9, 1, 3, 9],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.contrib import sparsity
from paddle.static import sparsity
from paddle.fluid.contrib.sparsity.asp import ASPHelper
import numpy as np

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.contrib import sparsity
from paddle.static import sparsity
from paddle.fluid.contrib.sparsity.asp import ASPHelper
import numpy as np

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import unittest
import paddle
from paddle.fluid.contrib import sparsity
from paddle.static import sparsity
from paddle.fluid.tests.unittests.asp.asp_pruning_base import TestASPHelperPruningBase

paddle.enable_static()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import paddle
import unittest
from paddle.fluid.contrib import sparsity
from paddle.static import sparsity
from paddle.fluid.tests.unittests.asp.asp_pruning_base import TestASPHelperPruningBase

paddle.enable_static()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import unittest
import paddle
from paddle.fluid.contrib import sparsity
from paddle.static import sparsity
from paddle.fluid.tests.unittests.asp.asp_pruning_base import TestASPHelperPruningBase

paddle.enable_static()
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/fluid/tests/unittests/asp/test_asp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import unittest
import threading, time
import paddle
from paddle.fluid.contrib import sparsity
from paddle.static import sparsity
import numpy as np


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import paddle.fluid as fluid
import paddle.fluid.core as core
import os
from paddle.fluid.contrib import sparsity
from paddle.static import sparsity
from paddle.fluid.contrib.sparsity.asp import ASPHelper
import numpy as np
cuda_visible_devices = os.getenv('CUDA_VISIBLE_DEVICES')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import paddle.fluid as fluid
import paddle.fluid.core as core
import os
from paddle.fluid.contrib import sparsity
from paddle.static import sparsity
from paddle.fluid.contrib.sparsity.asp import ASPHelper
import numpy as np
cuda_visible_devices = os.getenv('CUDA_VISIBLE_DEVICES')
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/static/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2021 NVIDIA Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,6 +14,7 @@
# limitations under the License.

from . import amp # noqa: F401
from . import sparsity # noqa: F401
from . import nn # noqa: F401
from .io import save_inference_model # noqa: F401
from .io import load_inference_model # noqa: F401
Expand Down
29 changes: 29 additions & 0 deletions python/paddle/static/sparsity/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2021 NVIDIA Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ...fluid.contrib.sparsity import calculate_density #noqa: F401
from ...fluid.contrib.sparsity import check_mask_1d #noqa: F401
from ...fluid.contrib.sparsity import get_mask_1d #noqa: F401
from ...fluid.contrib.sparsity import check_mask_2d #noqa: F401
from ...fluid.contrib.sparsity import get_mask_2d_greedy #noqa: F401
from ...fluid.contrib.sparsity import get_mask_2d_best #noqa: F401
from ...fluid.contrib.sparsity import create_mask #noqa: F401
from ...fluid.contrib.sparsity import check_sparsity #noqa: F401
from ...fluid.contrib.sparsity import MaskAlgo #noqa: F401
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MaskAlgo这个类型可以作为内部使用的数据类型使用,不建议对外公开。
比如这种写法暴露了较多内部实现的细节,书写和阅读都不是很方便
prune_model(... func_name=sparsity.MaskAlgo.MASK_1D)
建议改成这种形式更简洁,减少对外公开的数据类型,采用小写方便输入
prune_model(... mask_algo='mask_1d')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

採用建議使用小寫string作為輸入選項,並隱藏 MaskAlgo

from ...fluid.contrib.sparsity import CheckMethod #noqa: F401
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

再确认一下,以上API都需要向用户暴露吗?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一定需要暴露給用戶的API僅為MaskAlgo,目的是要讓用戶能夠決定Pruning的方式 (1D, 2D_greedy, 2D_best)等。
其餘依照建議移除

from ...fluid.contrib.sparsity import decorate #noqa: F401
from ...fluid.contrib.sparsity import prune_model #noqa: F401
from ...fluid.contrib.sparsity import set_excluded_layers #noqa: F401
from ...fluid.contrib.sparsity import reset_excluded_layers #noqa: F401
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

将需要对外公开的API 放到__all__列表,paddle会根据__all__列表区别公开API和内部使用API,
公开API需要提供使用文档和示例

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 change: 1 addition & 0 deletions python/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ packages=['paddle',
'paddle.static',
'paddle.static.nn',
'paddle.static.amp',
'paddle.static.sparsity',
'paddle.tensor',
'paddle.onnx',
'paddle.autograd',
Expand Down