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

Dynamic Graph Support to ASP #38517

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

import paddle.jit # noqa: F401
import paddle.amp # noqa: F401
import paddle.sparsity # noqa: F401
import paddle.dataset # noqa: F401
import paddle.inference # noqa: F401
import paddle.io # noqa: F401
Expand Down
3 changes: 2 additions & 1 deletion python/paddle/fluid/contrib/sparsity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
from .asp import prune_model
from .asp import set_excluded_layers
from .asp import reset_excluded_layers
from .supported_layer_list import add_supported_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'
]
480 changes: 380 additions & 100 deletions python/paddle/fluid/contrib/sparsity/asp.py

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions python/paddle/fluid/contrib/sparsity/supported_layer_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import paddle
from paddle.fluid.contrib import sparsity
import threading

__all__ = ['add_supported_layer']


def _default_pruning(weight_nparray, m, n, func_name, param_name):

checked_func_name = sparsity.CheckMethod.get_checking_method(func_name)

# The double transpose ops here make sure pruning direction consistent with cuSparseLt.
# SPMMA in cuSparseLt: D = (AxB) + C, where matrix A (mxk) is sparse matrix.
# cuSparseLt would prune matrix A along k dimension.
# In sparse training, layer weight matriices is viewed sparse matrix A, so
# the math fomula should be 'Act(WX + b)'. However, default fomula in PaddlePaddle
# is 'Act(XW + b)'. For enabling SPMMA, weights and inputs should be transposed
# for computing, Act( (W^T X^T)^T + b). Therefore, we have to prune alog k dimension
# of W^T, which is m dimension of W. Moreove, all mask generating functions in
# sparsity/utils is row-major pruning. That is the reason we have to transpose weight
# matrices beforce invoking create_mask. Then we transpose the result maks to make
# sure its shape to be the same as the input weight.
weight_sparse_mask = sparsity.create_mask(
weight_nparray.T, func_name=func_name, n=n, m=m).T
weight_pruned_nparray = np.multiply(weight_nparray, weight_sparse_mask)
assert sparsity.check_sparsity(weight_pruned_nparray.T, n=n, m=m, func_name=checked_func_name), \
'Pruning {} weight matrix failure!!!'.format(param_name)
return weight_pruned_nparray, weight_sparse_mask


# When value of given key in this DICT is None,
# ASP will call default pruning function in pruning stage.
_supported_layers_and_prune_func_map_lock = threading.Lock()
supported_layers_and_prune_func_map = {}


def add_supported_layer(layer, pruning_func=None):
r"""
Add supported layers and its corresponding pruning functino.

Args:
name (string|Layer): The name or type of layer, needed to support. If layer is `Layer` then
it would be turn to string internally. ASP would use this name to match parameter's name and call
its the corresponding pruning function.
pruning_func (function, optional): a function type which receives five argument (weight_nparray,
m, n, func_name, param_name), weight_nparray is a nparray of weight, param_name is the name of weight,
m, n, and func_name, please see `prune_model` for details.
"""
name = None
if isinstance(layer, str):
name = layer
elif isinstance(layer, paddle.fluid.dygraph.layers.Layer):
name = paddle.fluid.dygraph.layers._convert_camel_to_snake(
type(layer).__name__)
elif issubclass(layer, paddle.fluid.dygraph.layers.Layer):
name = paddle.fluid.dygraph.layers._convert_camel_to_snake(
layer.__name__)
else:
assert "The type of layer should be string of Layer, but got {}!".format(
type(layer))
if pruning_func is None:
pruning_func = _default_pruning
_supported_layers_and_prune_func_map_lock.acquire()
supported_layers_and_prune_func_map.update({name: pruning_func})
_supported_layers_and_prune_func_map_lock.release()


add_supported_layer('fc')
add_supported_layer('linear')
add_supported_layer('conv2d')
Copy link
Contributor

Choose a reason for hiding this comment

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

小疑问:conv2d 是把核函数 weight 4:2 吗 ?

8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/asp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
file(GLOB TEST_OPS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_*.py")
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")

list(REMOVE_ITEM TEST_OPS "test_fleet_with_asp")
list(REMOVE_ITEM TEST_OPS "test_fleet_with_asp_amp")
list(REMOVE_ITEM TEST_OPS "test_fleet_with_asp_static")
list(REMOVE_ITEM TEST_OPS "test_fleet_with_asp_dynamic")

foreach(TEST_OP ${TEST_OPS})
py_test_modules(${TEST_OP} MODULES ${TEST_OP})
endforeach(TEST_OP)

if(WITH_DISTRIBUTE)
py_test_modules(test_fleet_with_asp MODULES test_fleet_with_asp ENVS ${dist_ENVS})
py_test_modules(test_fleet_with_asp_amp MODULES test_fleet_with_asp_amp ENVS ${dist_ENVS})
py_test_modules(test_fleet_with_asp_dynamic MODULES test_fleet_with_asp_dynamic ENVS ${dist_ENVS})
py_test_modules(test_fleet_with_asp_static MODULES test_fleet_with_asp_static ENVS ${dist_ENVS})
endif()
270 changes: 270 additions & 0 deletions python/paddle/fluid/tests/unittests/asp/test_asp_customized_pruning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2021 NVIDIA Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import unittest
import numpy as np
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.contrib import sparsity
from paddle.fluid.contrib.sparsity.supported_layer_list import supported_layers_and_prune_func_map
from paddle.fluid.dygraph.layers import Layer, _convert_camel_to_snake


class MyOwnLayer(Layer):
def __init__(self):
super(MyOwnLayer, self).__init__()

def forward(self, x):
return x


static_tensor = None
static_tensor_mask = None


def my_own_pruning(tensor, m, n, mask_algo, param_name):
global static_tensor
global static_tensor_mask
if static_tensor is None:
static_tensor = np.random.rand(*tensor.shape).astype(np.float32)
if static_tensor_mask is None:
static_tensor_mask = np.random.rand(*tensor.shape).astype(np.float32)
return static_tensor, static_tensor_mask


class TestASPAddSupportedLayer(unittest.TestCase):
def test_add_supported_layer_via_name(self):
sparsity.add_supported_layer("test_supported_1")
sparsity.add_supported_layer("test_supported_2", my_own_pruning)
sparsity.add_supported_layer(MyOwnLayer)
my_own_layer_name = _convert_camel_to_snake(MyOwnLayer.__name__)

self.assertTrue(
"test_supported_1" in supported_layers_and_prune_func_map)
self.assertTrue(
"test_supported_2" in supported_layers_and_prune_func_map)
self.assertTrue(
"test_supported_2" in supported_layers_and_prune_func_map)
self.assertTrue(supported_layers_and_prune_func_map["test_supported_2"]
== my_own_pruning)
self.assertTrue(
my_own_layer_name in supported_layers_and_prune_func_map)


class TestASPDynamicCustomerizedPruneFunc(unittest.TestCase):
def setUp(self):
paddle.disable_static()

class CustomerLayer(paddle.nn.Layer):
def __init__(self):
super(CustomerLayer, self).__init__()

self.weight = self.create_parameter(
shape=[32, 32], attr=None, dtype='float32', is_bias=False)
self.linear1 = paddle.nn.Linear(32, 32)
self.linear2 = paddle.nn.Linear(32, 10)

def forward(self, input_):
hidden = paddle.nn.functional.linear(
x=input_, weight=self.weight)
hidden = self.linear1(hidden)
out = self.linear2(hidden)
return out

sparsity.add_supported_layer(CustomerLayer, my_own_pruning)

self.layer = CustomerLayer()
self.customer_prefix = paddle.fluid.dygraph.layers._convert_camel_to_snake(
CustomerLayer.__name__)
self.supported_layer_count_ref = 3

def test_inference_pruning(self):

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

supported_layer_count = 0
for param in self.layer.parameters():
mat = param.numpy()

if sparsity.asp.ASPHelper._is_supported_layer(
paddle.static.default_main_program(), param.name):
supported_layer_count += 1
if (self.customer_prefix in param.name):
self.assertLessEqual(
np.sum(mat.flatten() - static_tensor.flatten()), 1e-4)
else:
self.assertTrue(
sparsity.check_sparsity(
mat.T,
func_name=sparsity.CheckMethod.CHECK_1D,
n=2,
m=4))
self.assertEqual(supported_layer_count, self.supported_layer_count_ref)

def test_training_pruning(self):
optimizer = paddle.optimizer.SGD(learning_rate=0.01,
parameters=self.layer.parameters())
optimizer = sparsity.decorate(optimizer)

sparsity.prune_model(self.layer, mask_algo="mask_1d", with_mask=True)

supported_layer_count = 0
for param in self.layer.parameters():
mat = param.numpy()

if sparsity.asp.ASPHelper._is_supported_layer(
paddle.static.default_main_program(), param.name):

mat_mask = sparsity.asp.ASPHelper._get_program_asp_info(
paddle.static.default_main_program()).mask_vars[
param.name].numpy()

supported_layer_count += 1
if (self.customer_prefix in param.name):
self.assertLessEqual(
np.sum(mat.flatten() - static_tensor.flatten()), 1e-4)
self.assertLessEqual(
np.sum(mat_mask.flatten() - static_tensor_mask.flatten(
)), 1e-4)
else:
self.assertTrue(
sparsity.check_sparsity(
mat.T,
func_name=sparsity.CheckMethod.CHECK_1D,
n=2,
m=4))
self.assertTrue(
sparsity.check_sparsity(
mat_mask.T,
func_name=sparsity.CheckMethod.CHECK_1D,
n=2,
m=4))
self.assertEqual(supported_layer_count, self.supported_layer_count_ref)


class TestASPStaticCustomerizedPruneFunc(unittest.TestCase):
def setUp(self):
paddle.enable_static()

self.main_program = fluid.Program()
self.startup_program = fluid.Program()

self.customer_prefix = "customer_layer"

def build_model():
img = fluid.data(
name='img', shape=[None, 3, 32, 32], dtype='float32')
label = fluid.data(name='label', shape=[None, 1], dtype='int64')
hidden = fluid.layers.conv2d(
input=img, num_filters=4, filter_size=3, padding=2, act="relu")
hidden = fluid.layers.fc(input=hidden,
size=32,
act='relu',
name=self.customer_prefix)
hidden = fluid.layers.fc(input=hidden,
size=32,
act='relu',
name=self.customer_prefix)
hidden = fluid.layers.fc(input=hidden, size=32, act='relu')
prediction = fluid.layers.fc(input=hidden, size=10, act='softmax')
return img, label, prediction

with fluid.program_guard(self.main_program, self.startup_program):
self.img, self.label, self.predict = build_model()
self.supported_layer_count_ref = 5

self.place = paddle.CPUPlace()
if core.is_compiled_with_cuda():
self.place = paddle.CUDAPlace(0)
self.exe = fluid.Executor(self.place)

sparsity.add_supported_layer(self.customer_prefix, my_own_pruning)

def test_inference_pruning(self):
self.exe.run(self.startup_program)

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

supported_layer_count = 0
for param in self.main_program.global_block().all_parameters():
mat = np.array(fluid.global_scope().find_var(param.name).get_tensor(
))
if sparsity.asp.ASPHelper._is_supported_layer(self.main_program,
param.name):
supported_layer_count += 1
if (self.customer_prefix in param.name):
self.assertLessEqual(
np.sum(mat.flatten() - static_tensor.flatten()), 1e-4)
else:
self.assertTrue(
sparsity.check_sparsity(
mat.T,
func_name=sparsity.CheckMethod.CHECK_1D,
n=2,
m=4))
self.assertEqual(supported_layer_count, self.supported_layer_count_ref)

def test_training_pruning(self):
with fluid.program_guard(self.main_program, self.startup_program):
loss = fluid.layers.mean(
fluid.layers.cross_entropy(
input=self.predict, label=self.label))
optimizer = sparsity.decorate(
fluid.optimizer.SGD(learning_rate=0.01))
optimizer.minimize(loss, self.startup_program)

self.exe.run(self.startup_program)

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

supported_layer_count = 0
for param in self.main_program.global_block().all_parameters():
mat = np.array(fluid.global_scope().find_var(param.name).get_tensor(
))
if sparsity.asp.ASPHelper._is_supported_layer(self.main_program,
param.name):
mat_mask = np.array(fluid.global_scope().find_var(
sparsity.asp.ASPHelper._get_mask_name(param.name))
.get_tensor())
supported_layer_count += 1
if (self.customer_prefix in param.name):
self.assertLessEqual(
np.sum(mat.flatten() - static_tensor.flatten()), 1e-4)
self.assertLessEqual(
np.sum(mat_mask.flatten() - static_tensor_mask.flatten(
)), 1e-4)
else:
self.assertTrue(
sparsity.check_sparsity(
mat.T,
func_name=sparsity.CheckMethod.CHECK_1D,
n=2,
m=4))
self.assertTrue(
sparsity.check_sparsity(
mat_mask.T,
func_name=sparsity.CheckMethod.CHECK_1D,
n=2,
m=4))
self.assertEqual(supported_layer_count, self.supported_layer_count_ref)


if __name__ == '__main__':
unittest.main()
Loading