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

Automatic sparsity Utilities #32995

Merged
merged 2 commits into from
May 25, 2021

Conversation

mingxu1067
Copy link
Collaborator

PR types

New features

PR changes

APIs

Describe

Added sparse mask related utilities for further ASP training:

  1. Added sparse mask generating functions, including 1D and 2Ds.
  2. Added sparse pattern checking functions, including 1D and 2Ds.

1. Added sparse mask generating functions, including 1D and 2Ds.
2. Added sparse pattern checking functions, including 1D and 2Ds.
@paddle-bot-old
Copy link

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

@mingxu1067 mingxu1067 mentioned this pull request May 19, 2021
@qingqing01 qingqing01 self-requested a review May 20, 2021 02:56
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. 一些小的代码上的review建议,可以下个PR再改。

CheckMethod.get_checking_method(MaskAlgo.MASK_2D_BEST)
# CheckMethod.CHECK_2D
"""
assert type(mask_algo) == MaskAlgo, \
Copy link
Contributor

Choose a reason for hiding this comment

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

类型检查,是否用isinstance()更好?

return CheckMethod.CHECK_2D


def density(x):
Copy link
Contributor

Choose a reason for hiding this comment

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

函数名用calculate_density这样的命名方式,比较有助于理解函数的功能。

tuple: A pair of the reshaped and padded matrix and the shape of padded matrix (non-reshaping).
"""
remainder = mat.shape[1] % m
if mat.shape[1] % m > 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

这个要求输入mat是2-D的吗?

return True


def get_mask_1d(mat, n, m):
Copy link
Contributor

Choose a reason for hiding this comment

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

如果我没理解错的话,1d是指沿着1个维度?那会不会命名成get_mask_along_1d会好一点?

dtype = tensor.dtype
t = tensor.astype(float)

assert type(func_name) == MaskAlgo, \
Copy link
Contributor

Choose a reason for hiding this comment

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

建议用isinstance()函数来检查。

elif len(shape) == 4:
t = t.reshape(shape[0], shape[1] * shape[2] * shape[3])
mask = func(t, n=n, m=m)
return mask.reshape(shape).astype(dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

L511 - L527代码段可以简化一下?

mask = func(t, n=n, m=m)
return mask.reshape(shape).astype(dtype)
else:
assert True, "The dimension of input tensor is not supported in create_mask, " \
Copy link
Contributor

Choose a reason for hiding this comment

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

raise ValueError报错?

return func(t, n=n, m=m)
else:
assert True, "The dimension of input tensor is not supported in check_sparsity, " \
"Only dimension < 4 is supported but got {}".format(len(shape))
Copy link
Contributor

Choose a reason for hiding this comment

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

同上。

'density', 'check_mask_1d', 'get_mask_1d', 'check_mask_2d',
'get_mask_2d_greedy', 'get_mask_2d_best', 'create_mask', 'check_sparsity',
'MaskAlgo', 'CheckMethod'
]
Copy link
Contributor

Choose a reason for hiding this comment

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

一些函数没有必要暴露给用户,可以设置为文件内私有的?

from __future__ import print_function

from . import utils
from .utils import *
Copy link
Contributor

Choose a reason for hiding this comment

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

若API不需要建立上层alias,则没有必要import *

@Xreki Xreki merged commit f91e0f4 into PaddlePaddle:develop May 25, 2021
@mingxu1067 mingxu1067 deleted the automatic_sparsity_utils branch October 15, 2021 06:11
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.

2 participants