-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Automatic sparsity Utilities #32995
Conversation
1. Added sparse mask generating functions, including 1D and 2Ds. 2. Added sparse pattern checking functions, including 1D and 2Ds.
… automatic_sparsity_utils
Thanks for your contribution! |
There was a problem hiding this 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, \ |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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, \ |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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, " \ |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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' | ||
] |
There was a problem hiding this comment.
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 * |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
若API不需要建立上层alias,则没有必要import *
。
PR types
New features
PR changes
APIs
Describe
Added sparse mask related utilities for further ASP training: