未验证 提交 6aeb60aa 编写于 作者: M minghaoBD 提交者: GitHub

[ASP] fix some bugs of asp (#43853)

上级 b34b54db
...@@ -722,6 +722,10 @@ class ASPHelper(object): ...@@ -722,6 +722,10 @@ class ASPHelper(object):
if param_name in supported_layers_and_prune_func_map: if param_name in supported_layers_and_prune_func_map:
return True return True
# The parameter's name is neither in *.* format nor added to supported_layers_and_prune_func_map, return False.
if len(param_name_list) == 1:
return False
param_name_no_weight_suffix = param_name_list[0] param_name_no_weight_suffix = param_name_list[0]
param_type_suffix = param_name_list[1] param_type_suffix = param_name_list[1]
layer_name = param_name_no_weight_suffix[:param_name_no_weight_suffix. layer_name = param_name_no_weight_suffix[:param_name_no_weight_suffix.
......
...@@ -15,14 +15,38 @@ ...@@ -15,14 +15,38 @@
import numpy as np import numpy as np
import paddle import paddle
import copy
from paddle.fluid.contrib import sparsity from paddle.fluid.contrib import sparsity
import threading import threading
import logging
from ...log_helper import get_logger
__all__ = ['add_supported_layer'] __all__ = ['add_supported_layer']
_logger = get_logger(__name__,
logging.INFO,
fmt='%(asctime)s-%(levelname)s: %(message)s')
def _default_pruning(weight_nparray, m, n, func_name, param_name): def _default_pruning(weight_nparray, m, n, func_name, param_name):
# if the to-be-pruned dimension's size is smaller than m, we don't prune it. This strong assertion is required by the inference from cuSparseLT.
shape = weight_nparray.shape
weight_pruned_nparray = copy.deepcopy(weight_nparray)
weight_sparse_mask = np.ones_like(weight_pruned_nparray)
exlude_cond_shape2 = len(shape) == 2 and shape[0] < m
exlude_cond_shape4 = len(shape) == 4 and shape[1] < m
if exlude_cond_shape2:
_logger.warning(
'{} is not pruned because the first dimension of {} is smaller than {}'
.format(param_name, shape, m))
return weight_pruned_nparray, weight_sparse_mask
if exlude_cond_shape4:
_logger.warning(
'{} is not pruned because the second dimension of {} is smaller than {}'
.format(param_name, shape, m))
return weight_pruned_nparray, weight_sparse_mask
checked_func_name = sparsity.CheckMethod.get_checking_method(func_name) checked_func_name = sparsity.CheckMethod.get_checking_method(func_name)
# The double transpose ops here make sure pruning direction consistent with cuSparseLt. # The double transpose ops here make sure pruning direction consistent with cuSparseLt.
......
...@@ -223,6 +223,14 @@ class TestASPStaticCustomerizedPruneFunc(unittest.TestCase): ...@@ -223,6 +223,14 @@ class TestASPStaticCustomerizedPruneFunc(unittest.TestCase):
if (self.customer_prefix in param.name): if (self.customer_prefix in param.name):
self.assertLessEqual( self.assertLessEqual(
np.sum(mat.flatten() - static_tensor.flatten()), 1e-4) np.sum(mat.flatten() - static_tensor.flatten()), 1e-4)
else:
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else: else:
self.assertTrue( self.assertTrue(
sparsity.check_sparsity( sparsity.check_sparsity(
...@@ -263,6 +271,14 @@ class TestASPStaticCustomerizedPruneFunc(unittest.TestCase): ...@@ -263,6 +271,14 @@ class TestASPStaticCustomerizedPruneFunc(unittest.TestCase):
self.assertLessEqual( self.assertLessEqual(
np.sum(mat_mask.flatten() - np.sum(mat_mask.flatten() -
static_tensor_mask.flatten()), 1e-4) static_tensor_mask.flatten()), 1e-4)
else:
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
sparsity.check_sparsity(mat.T, n=2, m=4))
self.assertFalse(
sparsity.check_sparsity(mat_mask.T, n=2, m=4))
else: else:
self.assertTrue( self.assertTrue(
sparsity.check_sparsity( sparsity.check_sparsity(
......
...@@ -131,6 +131,14 @@ class TestASPDynamicOptimize(unittest.TestCase): ...@@ -131,6 +131,14 @@ class TestASPDynamicOptimize(unittest.TestCase):
if ASPHelper._is_supported_layer( if ASPHelper._is_supported_layer(
paddle.static.default_main_program(), param.name): paddle.static.default_main_program(), param.name):
mat = param.numpy() mat = param.numpy()
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
...@@ -165,6 +173,15 @@ class TestASPDynamicOptimize(unittest.TestCase): ...@@ -165,6 +173,15 @@ class TestASPDynamicOptimize(unittest.TestCase):
if ASPHelper._is_supported_layer( if ASPHelper._is_supported_layer(
paddle.static.default_main_program(), param.name): paddle.static.default_main_program(), param.name):
mat = param.numpy() mat = param.numpy()
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
......
...@@ -143,6 +143,14 @@ class TestASPStaticOptimize(unittest.TestCase): ...@@ -143,6 +143,14 @@ class TestASPStaticOptimize(unittest.TestCase):
if ASPHelper._is_supported_layer(self.main_program, param.name): if ASPHelper._is_supported_layer(self.main_program, param.name):
mat = np.array(fluid.global_scope().find_var( mat = np.array(fluid.global_scope().find_var(
param.name).get_tensor()) param.name).get_tensor())
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
...@@ -172,6 +180,14 @@ class TestASPStaticOptimize(unittest.TestCase): ...@@ -172,6 +180,14 @@ class TestASPStaticOptimize(unittest.TestCase):
if ASPHelper._is_supported_layer(self.main_program, param.name): if ASPHelper._is_supported_layer(self.main_program, param.name):
mat = np.array(fluid.global_scope().find_var( mat = np.array(fluid.global_scope().find_var(
param.name).get_tensor()) param.name).get_tensor())
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
......
...@@ -85,6 +85,14 @@ class TestASPDynamicPruningBase(unittest.TestCase): ...@@ -85,6 +85,14 @@ class TestASPDynamicPruningBase(unittest.TestCase):
if ASPHelper._is_supported_layer( if ASPHelper._is_supported_layer(
paddle.static.default_main_program(), param.name): paddle.static.default_main_program(), param.name):
mat = param.numpy() mat = param.numpy()
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity( paddle.fluid.contrib.sparsity.check_sparsity(
mat.T, func_name=self.mask_check_func, n=2, m=4)) mat.T, func_name=self.mask_check_func, n=2, m=4))
......
...@@ -43,7 +43,8 @@ class TestASPStaticPruningBase(unittest.TestCase): ...@@ -43,7 +43,8 @@ class TestASPStaticPruningBase(unittest.TestCase):
padding=2, padding=2,
act="relu") act="relu")
hidden = fluid.layers.fc(input=hidden, size=32, act='softmax') hidden = fluid.layers.fc(input=hidden, size=32, act='softmax')
prediction = fluid.layers.fc(input=hidden, size=10, act='softmax') hidden = fluid.layers.fc(input=hidden, size=3, act='softmax')
prediction = fluid.layers.fc(input=hidden, size=3, act='softmax')
return img, label, prediction return img, label, prediction
with fluid.program_guard(self.main_program, self.startup_program): with fluid.program_guard(self.main_program, self.startup_program):
...@@ -88,6 +89,14 @@ class TestASPStaticPruningBase(unittest.TestCase): ...@@ -88,6 +89,14 @@ class TestASPStaticPruningBase(unittest.TestCase):
if ASPHelper._is_supported_layer(self.main_program, param.name): if ASPHelper._is_supported_layer(self.main_program, param.name):
mat = np.array(fluid.global_scope().find_var( mat = np.array(fluid.global_scope().find_var(
param.name).get_tensor()) param.name).get_tensor())
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity( paddle.fluid.contrib.sparsity.check_sparsity(
mat.T, func_name=self.mask_check_func, n=2, m=4)) mat.T, func_name=self.mask_check_func, n=2, m=4))
......
...@@ -103,6 +103,14 @@ class TestASPDynamicOptimize(unittest.TestCase): ...@@ -103,6 +103,14 @@ class TestASPDynamicOptimize(unittest.TestCase):
if ASPHelper._is_supported_layer( if ASPHelper._is_supported_layer(
paddle.static.default_main_program(), param.name): paddle.static.default_main_program(), param.name):
mat = param.numpy() mat = param.numpy()
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
...@@ -171,6 +179,14 @@ class TestASPStaticOptimize(unittest.TestCase): ...@@ -171,6 +179,14 @@ class TestASPStaticOptimize(unittest.TestCase):
if ASPHelper._is_supported_layer(prog, param.name): if ASPHelper._is_supported_layer(prog, param.name):
mat = np.array(fluid.global_scope().find_var( mat = np.array(fluid.global_scope().find_var(
param.name).get_tensor()) param.name).get_tensor())
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
......
...@@ -90,6 +90,14 @@ class TestFleetWithASPDynamic(unittest.TestCase): ...@@ -90,6 +90,14 @@ class TestFleetWithASPDynamic(unittest.TestCase):
if ASPHelper._is_supported_layer( if ASPHelper._is_supported_layer(
paddle.static.default_main_program(), param.name): paddle.static.default_main_program(), param.name):
mat = param.numpy() mat = param.numpy()
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
...@@ -146,6 +154,14 @@ class TestFleetWithASPAMPDynamic(unittest.TestCase): ...@@ -146,6 +154,14 @@ class TestFleetWithASPAMPDynamic(unittest.TestCase):
if ASPHelper._is_supported_layer( if ASPHelper._is_supported_layer(
paddle.static.default_main_program(), param.name): paddle.static.default_main_program(), param.name):
mat = param.numpy() mat = param.numpy()
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
......
...@@ -110,6 +110,14 @@ class TestFleetWithASPSharding(unittest.TestCase): ...@@ -110,6 +110,14 @@ class TestFleetWithASPSharding(unittest.TestCase):
if ASPHelper._is_supported_layer(train_prog, param.name): if ASPHelper._is_supported_layer(train_prog, param.name):
mat = np.array(fluid.global_scope().find_var( mat = np.array(fluid.global_scope().find_var(
param.name).get_tensor()) param.name).get_tensor())
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
......
...@@ -85,6 +85,14 @@ class TestFleetWithASPStatic(unittest.TestCase): ...@@ -85,6 +85,14 @@ class TestFleetWithASPStatic(unittest.TestCase):
if ASPHelper._is_supported_layer(train_prog, param.name): if ASPHelper._is_supported_layer(train_prog, param.name):
mat = np.array(fluid.global_scope().find_var( mat = np.array(fluid.global_scope().find_var(
param.name).get_tensor()) param.name).get_tensor())
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
...@@ -146,6 +154,14 @@ class TestFleetWithASPAMPStatic(unittest.TestCase): ...@@ -146,6 +154,14 @@ class TestFleetWithASPAMPStatic(unittest.TestCase):
if ASPHelper._is_supported_layer(train_prog, param.name): if ASPHelper._is_supported_layer(train_prog, param.name):
mat = np.array(fluid.global_scope().find_var( mat = np.array(fluid.global_scope().find_var(
param.name).get_tensor()) param.name).get_tensor())
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
...@@ -187,6 +203,14 @@ class TestFleetWithASPAMPStatic(unittest.TestCase): ...@@ -187,6 +203,14 @@ class TestFleetWithASPAMPStatic(unittest.TestCase):
if ASPHelper._is_supported_layer(train_prog, param.name): if ASPHelper._is_supported_layer(train_prog, param.name):
mat = np.array(fluid.global_scope().find_var( mat = np.array(fluid.global_scope().find_var(
param.name).get_tensor()) param.name).get_tensor())
if (len(param.shape) == 4
and param.shape[1] < 4) or (len(param.shape) == 2
and param.shape[0] < 4):
self.assertFalse(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2,
m=4))
else:
self.assertTrue( self.assertTrue(
paddle.fluid.contrib.sparsity.check_sparsity(mat.T, paddle.fluid.contrib.sparsity.check_sparsity(mat.T,
n=2, n=2,
......
...@@ -17,6 +17,7 @@ from ...fluid.contrib.sparsity import calculate_density #noqa: F401 ...@@ -17,6 +17,7 @@ from ...fluid.contrib.sparsity import calculate_density #noqa: F401
from ...fluid.contrib.sparsity import decorate #noqa: F401 from ...fluid.contrib.sparsity import decorate #noqa: F401
from ...fluid.contrib.sparsity import prune_model #noqa: F401 from ...fluid.contrib.sparsity import prune_model #noqa: F401
from ...fluid.contrib.sparsity import reset_excluded_layers #noqa: F401 from ...fluid.contrib.sparsity import reset_excluded_layers #noqa: F401
from ...fluid.contrib.sparsity import add_supported_layer #noqa: F401
from ...fluid.contrib import sparsity #noqa: F401 from ...fluid.contrib import sparsity #noqa: F401
...@@ -27,5 +28,5 @@ def set_excluded_layers(main_program, param_names): ...@@ -27,5 +28,5 @@ def set_excluded_layers(main_program, param_names):
__all__ = [ #noqa __all__ = [ #noqa
'calculate_density', 'decorate', 'prune_model', 'set_excluded_layers', 'calculate_density', 'decorate', 'prune_model', 'set_excluded_layers',
'reset_excluded_layers' 'reset_excluded_layers', 'add_supported_layer'
] ]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册