提交 96138ca8 编写于 作者: D Dang Qingqing

Fix unit test.

上级 9e580631
...@@ -208,53 +208,60 @@ class TestQuantizeTranspiler(unittest.TestCase): ...@@ -208,53 +208,60 @@ class TestQuantizeTranspiler(unittest.TestCase):
paddle.dataset.mnist.test(), batch_size=batch_size) paddle.dataset.mnist.test(), batch_size=batch_size)
feeder = fluid.DataFeeder(feed_list=[img, label], place=place) feeder = fluid.DataFeeder(feed_list=[img, label], place=place)
for _ in range(iter): with fluid.program_guard(main):
data = train_reader().next() for _ in range(iter):
loss_v = exe.run(program=main, data = train_reader().next()
feed=feeder.feed(data), loss_v = exe.run(program=main,
fetch_list=[loss]) feed=feeder.feed(data),
test_data = test_reader().next() fetch_list=[loss])
f_var = fluid.framework.get_var('conv2d_1.tmp_0', test_program) with fluid.program_guard(test_program):
w_var = fluid.framework.get_var('conv2d_1.w_0.quantized', test_program) test_data = test_reader().next()
# Testing during training f_var = fluid.framework.get_var('conv2d_1.tmp_0', test_program)
test_loss1, f_v1, w_quant = exe.run(program=test_program, w_var = fluid.framework.get_var('conv2d_1.w_0.quantized',
feed=feeder.feed(test_data), test_program)
fetch_list=[loss, f_var, w_var]) # Testing during training
test_loss1, f_v1, w_quant = exe.run(
# Freeze program for inference, but the weight of fc/conv is still float type. program=test_program,
quant_transpiler.freeze_program(test_program, place) feed=feeder.feed(test_data),
fv2 = fluid.framework.get_var('conv2d_1.tmp_0.dequantized', fetch_list=[loss, f_var, w_var])
test_program)
test_loss2, f_v2 = exe.run(program=test_program, # Freeze program for inference, but the weight of fc/conv is still float type.
feed=feeder.feed(test_data), quant_transpiler.freeze_program(test_program, place)
fetch_list=[loss, fv2]) fv2 = fluid.framework.get_var('conv2d_1.tmp_0.dequantized',
self.assertAlmostEqual(test_loss1, test_loss2, delta=1e-5) test_program)
self.assertAlmostEqual(f_v1.all(), f_v2.all(), delta=1e-5) test_loss2, f_v2 = exe.run(program=test_program,
w_freeze = np.array(fluid.global_scope().find_var('conv2d_1.w_0') feed=feeder.feed(test_data),
.get_tensor()) fetch_list=[loss, fv2])
self.assertEqual(np.sum(w_freeze), np.sum(w_quant)) self.assertAlmostEqual(test_loss1, test_loss2, delta=1e-3)
self.assertTrue(np.allclose(f_v1, f_v2, rtol=1e-05, atol=1e-05))
# Convert parameter to 8-bit. w_freeze = np.array(fluid.global_scope().find_var('conv2d_1.w_0')
quant_transpiler.convert_to_int8(test_program, place) .get_tensor())
# Save the 8-bit parameter and model file. self.assertEqual(np.sum(w_freeze), np.sum(w_quant))
fluid.io.save_inference_model('model_8bit', ['image', 'label'], [loss],
exe, test_program) # Convert parameter to 8-bit.
# Test whether the 8-bit parameter and model file can be loaded successfully. quant_transpiler.convert_to_int8(test_program, place)
[infer, feed, fetch] = fluid.io.load_inference_model('model_8bit', exe) # Save the 8-bit parameter and model file.
# Check the loaded 8-bit weight. fluid.io.save_inference_model('model_8bit', ['image', 'label'],
w_8bit = np.array(fluid.global_scope().find_var('conv2d_1.w_0.int8') [loss], exe, test_program)
.get_tensor()) # Test whether the 8-bit parameter and model file can be loaded successfully.
[infer, feed, fetch] = fluid.io.load_inference_model('model_8bit',
self.assertEqual(w_8bit.dtype, np.int8) exe)
self.assertEqual(np.sum(w_8bit), np.sum(w_freeze)) # Check the loaded 8-bit weight.
w_8bit = np.array(fluid.global_scope().find_var('conv2d_1.w_0.int8')
.get_tensor())
self.assertEqual(w_8bit.dtype, np.int8)
self.assertEqual(np.sum(w_8bit), np.sum(w_freeze))
def test_freeze_program_cuda(self): def test_freeze_program_cuda(self):
if fluid.core.is_compiled_with_cuda(): if fluid.core.is_compiled_with_cuda():
self.freeze_program(True) with fluid.unique_name.guard():
self.freeze_program(True)
def test_freeze_program_cpu(self): def test_freeze_program_cpu(self):
self.freeze_program(False) with fluid.unique_name.guard():
self.freeze_program(False)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册