TaoLv commented on a change in pull request #14614: [MKLDNN] add quantized sum
URL: https://github.com/apache/incubator-mxnet/pull/14614#discussion_r276585921
##########
File path: tests/python/quantization/test_quantization.py
##########
@@ -213,6 +213,71 @@ def check_quantized_conv(data_shape, kernel, num_filter,
pad, stride, no_bias, q
check_quantized_conv((3, 4, 28, 28), (3, 3), 128, (1, 1), (1, 1),
True, qdtype)
check_quantized_conv((3, 4, 28, 28), (3, 3), 128, (1, 1), (1, 1),
False, qdtype)
+
+@with_seed()
+def test_quantized_elemwise_add():
+ def check_quantized_elemwise_add(data_shape, qtype):
+ if is_test_for_native_cpu():
+ print('skipped testing quantized_elemwise_add for native cpu since
it is not supported yet')
+ return
+ elif qtype != 'uint8' and qtype != 'int8':
+ print('skipped testing quantized_elemwise_add for not supported
data type')
+ return
+ elif is_test_for_gpu():
+ print('skipped testing quantized_elemwise_add for gpu since it is
not supported yet')
+ return
+
+ dataA = mx.sym.Variable(name='dataA', shape=data_shape,
dtype='float32')
+ dataB = mx.sym.Variable(name='dataB', shape=data_shape,
dtype='float32')
+ elemwise_add_fp32 = mx.sym.elemwise_add(dataA, dataB)
+ arg_names = elemwise_add_fp32.list_arguments()
+ elemwise_add_fp32_exe =
elemwise_add_fp32.simple_bind(ctx=mx.current_context(), grad_req='null')
+ if qtype == 'uint8':
+ data_low = 0.0
+ data_high = 255.0
+ else:
+ data_low = -127.0
+ data_high = 127.0
+
+ dataA_val = mx.nd.random.uniform(low=data_low, high=data_high,
shape=data_shape).astype('int32')
+ dataB_val = mx.nd.random.uniform(low=data_low, high=data_high,
shape=data_shape).astype('int32')
+ elemwise_add_fp32_exe.arg_dict[arg_names[0]][:] = dataA_val
+
+ elemwise_add_fp32_exe.arg_dict[arg_names[1]][:] = dataB_val
+
+ output = elemwise_add_fp32_exe.forward()[0]
+
+ qdataA = mx.sym.Variable(name='qdataA', shape=data_shape, dtype=qtype)
+ qdataB = mx.sym.Variable(name='qdataB', shape=data_shape, dtype=qtype)
+ min_dataA = mx.sym.Variable(name='min_dataA')
+ max_dataA = mx.sym.Variable(name='max_dataA')
+ min_dataB = mx.sym.Variable(name='min_dataB')
+ max_dataB = mx.sym.Variable(name='max_dataB')
+ quantized_elemwise_add = mx.sym.contrib.quantized_elemwise_add(qdataA,
qdataB, min_dataA, max_dataA, min_dataB, max_dataB)
+ elemwise_add_int8_exe =
quantized_elemwise_add.simple_bind(ctx=mx.current_context(), grad_req='null')
+ qarg_names = quantized_elemwise_add.list_arguments()
+ elemwise_add_int8_exe.arg_dict[qarg_names[0]][:] =
elemwise_add_fp32_exe.arg_dict[arg_names[0]].astype(qtype)
+ elemwise_add_int8_exe.arg_dict[qarg_names[1]][:] =
elemwise_add_fp32_exe.arg_dict[arg_names[1]].astype(qtype)
+ quantized_range = 127.0
+ elemwise_add_int8_exe.arg_dict[qarg_names[2]][:] = data_low
+ elemwise_add_int8_exe.arg_dict[qarg_names[3]][:] = data_high
+ elemwise_add_int8_exe.arg_dict[qarg_names[4]][:] = data_low
+ elemwise_add_int8_exe.arg_dict[qarg_names[5]][:] = data_high
+ qoutput, min_range, max_range = elemwise_add_int8_exe.forward()
+ min_val = min_range.asnumpy().tolist()[0]
+ max_val = max_range.asnumpy().tolist()[0]
+
+ fp32_rslt = output.asnumpy()
+ int8_rslt = qoutput.asnumpy()*max_val/0x7fffffff
+ assert_almost_equal(int8_rslt, int8_rslt, atol = 1)
Review comment:
why choose atol=1?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services