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

Add Support Layer List to ASP #40253

Merged
merged 6 commits into from
Mar 16, 2022

Conversation

mingxu1067
Copy link
Collaborator

PR types

New features

PR changes

APIs

Describe

  1. Added a function to allow user to add their own layer and correspond pruning way to ASP support.
  2. Changed naming rule of asp masks to make it clear.
  3. Usage example
import numpy as np
import paddle
from paddle.static import sparsity

paddle.enable_static()

def special_pruning(tensor, m, n, mask_algo, param_name):
    static_tensor = np.random.rand(*tensor.shape).astype(np.float32)
    static_tensor_mask = np.random.rand(*tensor.shape).astype(np.float32)
    return static_tensor, static_tensor_mask

main_program = paddle.static.Program()
startup_program = paddle.static.Program()

with paddle.static.program_guard(main_program, startup_program):
    input_data = paddle.static.data(name='data', shape=[None, 128])
    label = paddle.static.data(name='label', shape=[None, 10])
    hidden = paddle.static.nn.fc(x=input_data, num_flatten_dims=-1, 
                                 size=32, activation=None, name="need_special_prune")
    hidden = paddle.static.nn.fc(x=hidden, num_flatten_dims=-1, size=32, activation=None)
    prob = paddle.static.nn.fc(x=hidden, num_flatten_dims=-1, size=10, activation=None)
    loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label))


sparsity.add_supported_layer("need_special_prune", special_pruning)

place = paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(startup_program)

sparsity.prune_model(main_program, mask_algo="mask_1d", with_mask=False)

1. Added a function to allow user to add their own layer and correspond
pruning way to ASP support.
2. Changed naming rule to asp masks.
@paddle-bot-old
Copy link

paddle-bot-old bot commented Mar 8, 2022

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2021 NVIDIA Corporation. All rights reserved.
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2022 NVIDIA Corporation. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

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

该文件不是本PR新增,不用修改CopyRight年份,下同。这是误改?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done, 2022 -> 2021


__all__ = [
'calculate_density', 'check_mask_1d', 'get_mask_1d', 'check_mask_2d',
'get_mask_2d_greedy', 'get_mask_2d_best', 'create_mask', 'check_sparsity',
'MaskAlgo', 'CheckMethod', 'decorate', 'prune_model', 'set_excluded_layers',
'reset_excluded_layers'
'reset_excluded_layers', 'add_supported_layer'
Copy link
Contributor

Choose a reason for hiding this comment

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

add_supported_layerset_excluded_layersreset_excluded_layers,这三个接口有点令人迷惑了。

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

這三個接口功能分別如下
add_supported_layer: 讓使用者增加自己客製化的nn.Layer的參數到ASP的支援清單,預設只會支援FC, Conv2D, Linear.
set_excluded_layers: 在支援的nn.Layer的參數中,針對特定的幾層不執行ASP, 比如 一個網絡有三層FC, 但其中一層不需要ASP,怎可以使用此API來將該層排除
reset_excluded_layers: 將 set_excluded_layers 所添加的Layer全部清空。


__all__ = [
'calculate_density', 'check_mask_1d', 'get_mask_1d', 'check_mask_2d',
'get_mask_2d_greedy', 'get_mask_2d_best', 'create_mask', 'check_sparsity',
'MaskAlgo', 'CheckMethod', 'decorate', 'prune_model', 'set_excluded_layers',
'reset_excluded_layers'
'reset_excluded_layers', 'add_supported_layer'
Copy link
Contributor

Choose a reason for hiding this comment

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

add_supported_layerset_excluded_layersreset_excluded_layers,这三个接口有点令人迷惑了。

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

同上

@@ -293,7 +295,7 @@ class ASPHelper(object):
"""

MASK_APPENDDED_NAME = '_asp_mask'
SUPPORTED_LAYERS = {'fc': 'w_0', 'linear': 'w_0', 'conv2d': 'w_0'}
PADDLE_WEIGHT_SUFFIX = "w_"
Copy link
Contributor

Choose a reason for hiding this comment

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

这样改的好处,是可以支持更多的layer,不限定于fclinearconv2d

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

是的,此次改動將支援的Layer改為統一由supported_layer_list來管理

@@ -293,7 +295,7 @@ class ASPHelper(object):
"""

MASK_APPENDDED_NAME = '_asp_mask'
SUPPORTED_LAYERS = {'fc': 'w_0', 'linear': 'w_0', 'conv2d': 'w_0'}
PADDLE_WEIGHT_SUFFIX = "w_"
Copy link
Contributor

Choose a reason for hiding this comment

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

这样改的好处,是可以支持更多的layer,不限定于fclinearconv2d

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

同上

@@ -384,7 +379,7 @@ def _get_mask_name(param_name):
Returns:
string: The mask name of :attr:`param_name`.
"""
return param_name + ASPHelper.MASK_APPENDDED_NAME
return param_name + "." + ASPHelper.MASK_APPENDDED_NAME
Copy link
Contributor

Choose a reason for hiding this comment

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

这里加了.,还需要保留_吗?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done, remoe _.

@@ -384,7 +379,7 @@ def _get_mask_name(param_name):
Returns:
string: The mask name of :attr:`param_name`.
"""
return param_name + ASPHelper.MASK_APPENDDED_NAME
return param_name + "." + ASPHelper.MASK_APPENDDED_NAME
Copy link
Contributor

Choose a reason for hiding this comment

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

这里加了.,还需要保留_吗?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

同上


__all__ = [ #noqa
'calculate_density',
'decorate',
'prune_model',
'set_excluded_layers',
'reset_excluded_layers'
'reset_excluded_layers',
'add_supported_layer'
Copy link
Contributor

Choose a reason for hiding this comment

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

这里后续PR再加?add_supported_layerset_excluded_layersreset_excluded_layers,这三个接口有点令人迷惑了。

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

同上解釋後,是否還是要暫緩對外公開呢?


__all__ = [ #noqa
'calculate_density',
'decorate',
'prune_model',
'set_excluded_layers',
'reset_excluded_layers'
'reset_excluded_layers',
'add_supported_layer'
Copy link
Contributor

Choose a reason for hiding this comment

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

这里后续PR再加?add_supported_layerset_excluded_layersreset_excluded_layers,这三个接口有点令人迷惑了。

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

同上解釋後,是否還是要暫緩對外公開呢?

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. Modified year of copyright.
2. Changed asp mask name from _asp_mask to asp_mask.
1. Hide add_support_layer and wait API confirmation to release.
2. Changed year of copyright.
Copy link
Contributor

@Xreki Xreki left a comment

Choose a reason for hiding this comment

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

LGTM

@Xreki Xreki requested a review from lanxianghit March 16, 2022 06:03
@Xreki Xreki merged commit c040bbd into PaddlePaddle:develop Mar 16, 2022
liqitong-a pushed a commit to liqitong-a/Paddle that referenced this pull request Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants