Skip to content
Merged
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 test/contrib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ py_test_modules(
FLAGS_conv_workspace_size_limit=1000)

set_tests_properties(test_multi_precision_fp16_train PROPERTIES TIMEOUT 120)
set_tests_properties(test_image_classification_fp16 PROPERTIES TIMEOUT 120)
111 changes: 111 additions & 0 deletions test/contrib/test_bf16_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# 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 copy
import unittest

import paddle
from paddle.static import amp

paddle.enable_static()


class AMPTest(unittest.TestCase):
def setUp(self):
self.bf16_list = copy.copy(amp.bf16.amp_lists.bf16_list)
self.fp32_list = copy.copy(amp.bf16.amp_lists.fp32_list)
self.gray_list = copy.copy(amp.bf16.amp_lists.gray_list)
self.amp_lists_ = None

def tearDown(self):
self.assertEqual(self.amp_lists_.bf16_list, self.bf16_list)
self.assertEqual(self.amp_lists_.fp32_list, self.fp32_list)
self.assertEqual(self.amp_lists_.gray_list, self.gray_list)

def test_amp_lists(self):
self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16()

def test_amp_lists_1(self):
# 1. w={'exp}, b=None
self.bf16_list.add('exp')
self.fp32_list.remove('exp')

self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16({'exp'})

def test_amp_lists_2(self):
# 2. w={'tanh'}, b=None
self.fp32_list.remove('tan')
self.bf16_list.add('tan')

self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16({'tan'})

def test_amp_lists_3(self):
# 3. w={'lstm'}, b=None
self.bf16_list.add('lstm')

self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16({'lstm'})

def test_amp_lists_4(self):
# 4. w=None, b={'matmul_v2'}
self.bf16_list.remove('matmul_v2')
self.fp32_list.add('matmul_v2')

self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16(
custom_fp32_list={'matmul_v2'}
)

def test_amp_lists_5(self):
# 5. w=None, b={'matmul_v2'}
self.fp32_list.add('matmul_v2')
self.bf16_list.remove('matmul_v2')

self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16(
custom_fp32_list={'matmul_v2'}
)

def test_amp_lists_6(self):
# 6. w=None, b={'lstm'}
self.fp32_list.add('lstm')

self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16(
custom_fp32_list={'lstm'}
)

def test_amp_lists_7(self):
self.fp32_list.add('reshape2')
self.gray_list.remove('reshape2')

self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16(
custom_fp32_list={'reshape2'}
)

def test_amp_list_8(self):
self.bf16_list.add('reshape2')
self.gray_list.remove('reshape2')

self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16(
custom_bf16_list={'reshape2'}
)


class AMPTest2(unittest.TestCase):
def test_amp_lists_(self):
# 7. w={'lstm'} b={'lstm'}
# raise ValueError
self.assertRaises(
ValueError, amp.bf16.AutoMixedPrecisionListsBF16, {'lstm'}, {'lstm'}
)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import numpy

# TODO: remove sys.path.append
sys.path.append("../../legacy_test")
sys.path.append("../legacy_test")
import nets

import paddle
Expand Down Expand Up @@ -649,44 +649,5 @@ def scope_prog_guard(self):
yield


class TestAmpWithNonIterableDataLoader(unittest.TestCase):
def decorate_with_data_loader(self):
main_prog = paddle.static.Program()
start_prog = paddle.static.Program()
with paddle.static.program_guard(main_prog, start_prog):
with paddle.base.unique_name.guard():
image = paddle.static.data(
name='image', shape=[-1, 3, 224, 224], dtype='float32'
)
label = paddle.static.data(
name='label', shape=[-1, 1], dtype='int64'
)

net = vgg16_bn_drop(image)
logits = paddle.static.nn.fc(
x=net, size=10, activation="softmax"
)
cost, predict = paddle.nn.functional.softmax_with_cross_entropy(
logits, label, return_softmax=True
)
avg_cost = paddle.mean(cost)

optimizer = paddle.optimizer.Lamb(learning_rate=0.001)
amp_lists = paddle.static.amp.AutoMixedPrecisionLists(
custom_black_varnames={"loss", "conv2d_0.w_0"}
)
mp_optimizer = decorate(
optimizer=optimizer,
amp_lists=amp_lists,
init_loss_scaling=8.0,
use_dynamic_loss_scaling=True,
)

mp_optimizer.minimize(avg_cost)

def test_non_iterable_dataloader(self):
self.decorate_with_data_loader()


if __name__ == '__main__':
unittest.main()
4 changes: 2 additions & 2 deletions test/deprecated/asp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ foreach(TEST_OP ${TEST_OPS})
endforeach()

set_tests_properties(test_asp_pruning_dynamic PROPERTIES TIMEOUT 30)
set_tests_properties(test_asp_pruning_static PROPERTIES TIMEOUT 30)
set_tests_properties(test_asp_pruning_static_deprecated PROPERTIES TIMEOUT 30)
set_tests_properties(test_asp_optimize_dynamic PROPERTIES TIMEOUT 30)
set_tests_properties(test_asp_optimize_static PROPERTIES TIMEOUT 30)
set_tests_properties(test_asp_optimize_static_deprecated PROPERTIES TIMEOUT 30)
157 changes: 0 additions & 157 deletions test/deprecated/asp/test_asp_customized_pruning.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import numpy as np

import paddle
from paddle import base
from paddle.base import core
from paddle.incubate import asp as sparsity
from paddle.incubate.asp.supported_layer_list import (
supported_layers_and_prune_func_map,
Expand Down Expand Up @@ -184,160 +182,5 @@ def test_training_pruning(self):
self.assertEqual(supported_layer_count, self.supported_layer_count_ref)


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

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

self.customer_prefix = "customer_layer"

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

with base.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 = base.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(
base.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:
if (len(param.shape) == 4 and param.shape[1] < 4) or (
len(param.shape) == 2 and param.shape[0] < 4
):
self.assertFalse(
paddle.incubate.asp.check_sparsity(mat.T, n=2, m=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 base.program_guard(self.main_program, self.startup_program):
loss = paddle.mean(
paddle.nn.functional.cross_entropy(
input=self.predict,
label=self.label,
reduction='none',
use_softmax=False,
)
)
optimizer = sparsity.decorate(
paddle.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(
base.global_scope().find_var(param.name).get_tensor()
)
if sparsity.asp.ASPHelper._is_supported_layer(
self.main_program, param.name
):
mat_mask = np.array(
base.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:
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:
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