ciyongch commented on a change in pull request #17147: [WIP] Quantized Elemwise
Mul Operator
URL: https://github.com/apache/incubator-mxnet/pull/17147#discussion_r361237265
##########
File path: tests/python/quantization/test_quantization.py
##########
@@ -341,6 +341,68 @@ def check_quantized_elemwise_add(data_shape, qtype):
check_quantized_elemwise_add((3, 4, 56, 56), qtype)
check_quantized_elemwise_add((32, 56, 64, 11), qtype)
+@with_seed()
+def test_quantized_elemwise_mul():
+ def check_quantized_elemwise_mul(data_shape, qtype):
+ if is_test_for_native_cpu():
+ print('skipped testing quantized_elemwise_mul for native cpu since
it is not supported yet')
+ return
+ elif qtype != 'int8':
+ print('skipped testing quantized_elemwise_mul for not supported
data type')
+ return
+ elif is_test_for_gpu():
+ print('skipped testing quantized_elemwise_mul 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_mul_fp32 = mx.sym.elemwise_mul(dataA, dataB)
+ arg_names = elemwise_mul_fp32.list_arguments()
+ elemwise_mul_fp32_exe =
elemwise_mul_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_mul_fp32_exe.arg_dict[arg_names[0]][:] = dataA_val
+
+ elemwise_mul_fp32_exe.arg_dict[arg_names[1]][:] = dataB_val
+
+ output = elemwise_mul_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_mul = mx.sym.contrib.quantized_elemwise_mul(qdataA,
qdataB, min_dataA, max_dataA, min_dataB, max_dataB)
+ elemwise_mul_int8_exe =
quantized_elemwise_mul.simple_bind(ctx=mx.current_context(), grad_req='null')
+ qarg_names = quantized_elemwise_mul.list_arguments()
+ elemwise_mul_int8_exe.arg_dict[qarg_names[0]][:] =
elemwise_mul_fp32_exe.arg_dict[arg_names[0]].astype(qtype)
+ elemwise_mul_int8_exe.arg_dict[qarg_names[1]][:] =
elemwise_mul_fp32_exe.arg_dict[arg_names[1]].astype(qtype)
+ quantized_range = 127.0
+ elemwise_mul_int8_exe.arg_dict[qarg_names[2]][:] = data_low
+ elemwise_mul_int8_exe.arg_dict[qarg_names[3]][:] = data_high
+ elemwise_mul_int8_exe.arg_dict[qarg_names[4]][:] = data_low
+ elemwise_mul_int8_exe.arg_dict[qarg_names[5]][:] = data_high
+ qoutput, min_range, max_range = elemwise_mul_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
Review comment:
The `min_val` and `max_val` is not the scale range for `qoutput` when its
dtype is `int32`, if so, then the `int8_rslt` will be incorrect.
----------------------------------------------------------------
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