Skip to content

Commit

Permalink
Fix unittest error (PaddlePaddle#393)
Browse files Browse the repository at this point in the history
* fix unittest error

* fix quant_post unittest error
  • Loading branch information
baiyfbupt committed Jul 22, 2020
1 parent a596e2b commit 115bbda
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 13 deletions.
2 changes: 2 additions & 0 deletions tests/test_darts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# 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 paddle
import unittest
import paddle.fluid as fluid
Expand Down
2 changes: 2 additions & 0 deletions tests/test_earlystop.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# 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 unittest
import paddle
from paddleslim.nas import SANAS
Expand Down
15 changes: 15 additions & 0 deletions tests/test_quant_embedding.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Copyright (c) 2019 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 paddle.fluid as fluid
import paddleslim.quant as quant
import unittest
Expand Down
7 changes: 3 additions & 4 deletions tests/test_quant_post_only_weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ def test(program, outputs=[avg_cost, acc_top1, acc_top5]):
fetch_list=outputs)
iter += 1
if iter % 100 == 0:
print(
'eval iter={}, avg loss {}, acc_top1 {}, acc_top5 {}'.
format(iter, cost, top1, top5))
print('eval iter={}, avg loss {}, acc_top1 {}, acc_top5 {}'.
format(iter, cost, top1, top5))
result[0].append(cost)
result[1].append(top1)
result[2].append(top5)
Expand All @@ -99,7 +98,7 @@ def test(program, outputs=[avg_cost, acc_top1, acc_top5]):
params_filename='params')

quant_post_dynamic(
model_dir='./test_quant_post',
model_dir='./test_quant_post_dynamic',
save_model_dir='./test_quant_post_inference',
model_filename='model',
params_filename='params',
Expand Down
2 changes: 2 additions & 0 deletions tests/test_rl_nas.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# 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 unittest
import paddle.fluid as fluid
from paddleslim.nas import RLNAS
Expand Down
36 changes: 27 additions & 9 deletions tests/test_sa_nas.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# 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 sys
import unittest
Expand All @@ -19,24 +21,27 @@
from paddleslim.analysis import flops
import numpy as np


def compute_op_num(program):
params = {}
ch_list = []
for block in program.blocks:
for param in block.all_parameters():
if len(param.shape) == 4:
if len(param.shape) == 4:
params[param.name] = param.shape
ch_list.append(int(param.shape[0]))
return params, ch_list


class TestSANAS(unittest.TestCase):
def setUp(self):
self.init_test_case()
port = np.random.randint(8337, 8773)
self.sanas = SANAS(configs=self.configs, server_addr=("", port), save_checkpoint=None)
self.sanas = SANAS(
configs=self.configs, server_addr=("", port), save_checkpoint=None)

def init_test_case(self):
self.configs=[('MobileNetV2BlockSpace', {'block_mask':[0]})]
self.configs = [('MobileNetV2BlockSpace', {'block_mask': [0]})]
self.filter_num = np.array([
3, 4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 144, 160, 192, 224,
256, 320, 384, 512
Expand All @@ -53,7 +58,10 @@ def check_chnum_convnum(self, program):

conv_list, ch_pro = compute_op_num(program)
### assert conv number
self.assertTrue((repeat_num * 3) == len(conv_list), "the number of conv is NOT match, the number compute from token: {}, actual conv number: {}".format(repeat_num * 3, len(conv_list)))
self.assertTrue((repeat_num * 3) == len(
conv_list
), "the number of conv is NOT match, the number compute from token: {}, actual conv number: {}".
format(repeat_num * 3, len(conv_list)))

### assert number of channels
ch_token = []
Expand All @@ -64,7 +72,10 @@ def check_chnum_convnum(self, program):
ch_token.append(filter_num)
init_ch_num = filter_num

self.assertTrue(str(ch_token) == str(ch_pro), "channel num is WRONG, channel num from token is {}, channel num come fom program is {}".format(str(ch_token), str(ch_pro)))
self.assertTrue(
str(ch_token) == str(ch_pro),
"channel num is WRONG, channel num from token is {}, channel num come fom program is {}".
format(str(ch_token), str(ch_pro)))

def test_all_function(self):
### unittest for next_archs
Expand All @@ -73,7 +84,8 @@ def test_all_function(self):
token2arch_program = fluid.Program()

with fluid.program_guard(next_program, startup_program):
inputs = fluid.data(name='input', shape=[None, 3, 32, 32], dtype='float32')
inputs = fluid.data(
name='input', shape=[None, 3, 32, 32], dtype='float32')
archs = self.sanas.next_archs()
for arch in archs:
output = arch(inputs)
Expand All @@ -85,16 +97,22 @@ def test_all_function(self):

### uniitest for tokens2arch
with fluid.program_guard(token2arch_program, startup_program):
inputs = fluid.data(name='input', shape=[None, 3, 32, 32], dtype='float32')
arch = self.sanas.tokens2arch(self.sanas.current_info()['current_tokens'])
inputs = fluid.data(
name='input', shape=[None, 3, 32, 32], dtype='float32')
arch = self.sanas.tokens2arch(self.sanas.current_info()[
'current_tokens'])
for arch in archs:
output = arch(inputs)
inputs = output
self.check_chnum_convnum(token2arch_program)

### unittest for current_info
current_info = self.sanas.current_info()
self.assertTrue(isinstance(current_info, dict), "the type of current info must be dict, but now is {}".format(type(current_info)))
self.assertTrue(
isinstance(current_info, dict),
"the type of current info must be dict, but now is {}".format(
type(current_info)))


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

0 comments on commit 115bbda

Please sign in to comment.