From 9c10e3e5f305a1d30479592c929a5ae9adba16cd Mon Sep 17 00:00:00 2001 From: baiyfbupt Date: Mon, 3 Aug 2020 11:45:30 +0800 Subject: [PATCH] add unittest --- tests/test_analyze_helper.py | 67 ++++++++++++++++++++++++++++++++++++ tests/test_quant_utility.py | 67 ++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 tests/test_analyze_helper.py create mode 100755 tests/test_quant_utility.py diff --git a/tests/test_analyze_helper.py b/tests/test_analyze_helper.py new file mode 100644 index 00000000..773c3471 --- /dev/null +++ b/tests/test_analyze_helper.py @@ -0,0 +1,67 @@ +# Copyright (c) 2020 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 sys +sys.path.append("../") +import os +import unittest +import paddle +import paddle.fluid as fluid +import paddle.dataset.mnist as reader +from paddleslim.common import get_distribution, pdf +sys.path.append("../demo") +from models import MobileNet + + +class TestAnalysisHelper(unittest.TestCase): + def get_model(self): + image = fluid.layers.data( + name='image', shape=[1, 28, 28], dtype='float32') + label = fluid.layers.data(name='label', shape=[1], dtype='int64') + train_loader = fluid.io.DataLoader.from_generator( + feed_list=[image, label], + capacity=512, + use_double_buffer=True, + iterable=True) + model = MobileNet() + out = model.net(input=image, class_dim=10) + cost = fluid.layers.cross_entropy(input=out, label=label) + avg_cost = fluid.layers.mean(x=cost) + startup_prog = fluid.default_startup_program() + train_prog = fluid.default_main_program() + return startup_prog, train_prog, train_loader + + def test_analysishelper(self): + startup_prog, train_prog, train_loader = self.get_model() + place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda( + ) else fluid.CPUPlace() + train_reader = reader.train() + train_reader = paddle.fluid.io.batch(train_reader, batch_size=64) + train_loader.set_sample_list_generator(train_reader, place) + exe = fluid.Executor(place) + exe.run(startup_prog) + var_names = [ + var.name for var in list(train_prog.list_vars()) + if 'generated_var' not in var.name and '@GRAD' not in var.name + ][:50] + var_dist = get_distribution(train_prog, var_names, exe, train_loader) + pdf(var_dist, pdf_save_dir='var_dist_pdf') + + # test get_distribution + self.assertTrue(isinstance(var_dist, dict)) + # test pdf + self.assertTrue(os.path.exists('./var_dist_pdf')) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_quant_utility.py b/tests/test_quant_utility.py new file mode 100755 index 00000000..2b54d5f5 --- /dev/null +++ b/tests/test_quant_utility.py @@ -0,0 +1,67 @@ +# Copyright (c) 2020 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 sys +sys.path.append("../") +import os +import unittest +import paddle +import paddle.fluid as fluid +import paddle.dataset.mnist as reader +from paddleslim.common import get_distribution +from paddleslim.quant import pact_thres +sys.path.append("../demo") +from models import MobileNet + + +class TestQuantUtility(unittest.TestCase): + def get_model(self): + image = fluid.layers.data( + name='image', shape=[1, 28, 28], dtype='float32') + label = fluid.layers.data(name='label', shape=[1], dtype='int64') + train_loader = fluid.io.DataLoader.from_generator( + feed_list=[image, label], + capacity=512, + use_double_buffer=True, + iterable=True) + model = MobileNet() + out = model.net(input=image, class_dim=10) + cost = fluid.layers.cross_entropy(input=out, label=label) + avg_cost = fluid.layers.mean(x=cost) + optimizer = fluid.optimizer.Momentum(learning_rate=0.1, momentum=0.9) + optimizer.minimize(avg_cost) + startup_prog = fluid.default_startup_program() + train_prog = fluid.default_main_program() + return startup_prog, train_prog, train_loader + + def test_pact_thres(self): + startup_prog, train_prog, train_loader = self.get_model() + place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda( + ) else fluid.CPUPlace() + train_reader = reader.train() + train_reader = paddle.fluid.io.batch(train_reader, batch_size=64) + train_loader.set_sample_list_generator(train_reader, place) + exe = fluid.Executor(place) + exe.run(startup_prog) + var_names = [ + var.name for var in list(train_prog.list_vars()) + if 'generated_var' not in var.name and '@GRAD' not in var.name + ] + var_dist = get_distribution(train_prog, var_names, exe, train_loader) + pact_alphas = pact_thres(var_dist) + self.assertTrue(isinstance(pact_alphas, dict)) + self.assertTrue(isinstance(list(pact_alphas.values())[0], float)) + + +if __name__ == '__main__': + unittest.main() -- GitLab