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

Asp movement #36525

merged 17 commits into from
Oct 29, 2021

Conversation

mingxu1067
Copy link
Collaborator

@mingxu1067 mingxu1067 commented Oct 19, 2021

PR types

Others

PR changes

APIs

Describe

Added Automatic SParsity (ASP)'s alias into paddel.static.

Since the folder organization changes in v2.2, the paddle.fluid.xxx is not the official way to use modules. We move ASP module to paddle.static to make its usage be consistent with official ways, also modified code examples and related unittests.

Example:

import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core

# Previous Usage
# from paddle.fluid.contrib import sparsity
# Current Usage
from paddle.static import sparsity

# Others are the same as previous.
paddle.enable_static()

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_sparse_fc")
	hidden = paddle.static.nn.fc(x=hidden, num_flatten_dims=-1, size=32, activation=None, name="need_dense_fc")
	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))

	# Setup exluded layers out from ASP workflow.
	# Please note, excluded_layers must be set before calling `optimizer.minimize()`.
	sparsity.set_excluded_layers(main_program, ["need_dense_fc"])

	optimizer = paddle.optimizer.SGD(learning_rate=0.1)
	optimizer = paddle.static.amp.decorator.decorate(optimizer )
	# Calling sparsity.decorate() to wrap minimize() in optimizer, which 
	# will insert necessary masking operations for ASP workflow.
	optimizer = sparsity.decorate(optimizer)
	optimizer.minimize(loss, startup_program)

device = paddle.device.get_device()
place  = paddle.set_device(device)

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

# Must call `exe.run(startup_program)` first before calling `sparsity.prune_model`
sparsity.prune_model(place, main_program, func_name=sparsity.MaskAlgo.MASK_2D_BEST)

# Do training loop

@@ -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.

好的 已經全面修正

@@ -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的裝置

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
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.

# limitations under the License.

from ...fluid.contrib.sparsity import calculate_density #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

@@ -114,7 +112,7 @@ def prune_model(place,
inference only. To obtain OptimizerWithSparsityGuarantee, please see `sparsity.decoreate()`.

Args:
place (fluid.CPUPlace()|fluid.CUDAPlace(N)): Device place for pruned parameter and mask Variables, and N means the GPU's id. It should be the same as created instance of Executor.
place (paddle.CPUPlace()|paddle.CUDAPlace(N)): Device place for pruned parameter and mask Variables, and N means the GPU's id. It should be the same as created instance of Executor.
Copy link
Contributor

Choose a reason for hiding this comment

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

确认下,place这个参数是否是必须的?
是否可以用这两组参数来修改全局的place信息?
paddle.device.get_device()
paddle.device.set_device()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

可以的,依照建議移除place argument 採用paddle.device.get_device(), paddle.device.set_device()來自動判斷

1. Do not expose MaskAlgo, instead using string as data type of func_name.
2. Removed 'place' arg in prune_model()
3. Added __all__ in paddle.static.sparsity
Xreki
Xreki previously approved these changes Oct 29, 2021
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

n=2,
m=4,
func_name=sparsity.MaskAlgo.MASK_1D,
func_name='mask_1d',
Copy link
Contributor

Choose a reason for hiding this comment

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

这里api的参数名称func_name是否指mask algorithm?建议再改一下,更清晰一些
func_name -> mask_algo

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

依照建議修改完成。

main_program (Program, optional): Program with model definition and its parameters. Default is `paddle.static.default_main_program()
n (int): n of `n:m` sparse pattern.
m (int): m of `n:m` sparse pattern.
func_name (MaskAlgo, optional): The function name to generate spase mask. Default is `MaskAlgo.MASK_1D`. All options please refer to `MaskAlgo`.
func_name (string, optional): The function name to generate spase mask. Default is `mask_1d`.
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
Contributor

@XiaoguangHu01 XiaoguangHu01 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 merged commit 113816d into PaddlePaddle:develop Oct 29, 2021
Xreki pushed a commit to Xreki/Paddle that referenced this pull request Oct 29, 2021
ghost pushed a commit to piotrekobi/Paddle that referenced this pull request Nov 3, 2021
@mingxu1067 mingxu1067 deleted the asp_movement branch December 17, 2021 09:31
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.

3 participants