jinhuang415 commented on a change in pull request #10433: [MXNET-290] MKLDNN
support for model quantization
URL: https://github.com/apache/incubator-mxnet/pull/10433#discussion_r193410022
##########
File path: tests/python/quantization/test_quantization.py
##########
@@ -120,114 +128,126 @@ def check_requantize(shape, min_calib_range=None,
max_calib_range=None):
@with_seed()
def test_quantized_conv():
- if mx.current_context().device_type != 'gpu':
- print('skipped testing quantized_conv on cpu since it is not
implemented yet')
+ if is_test_for_naive_cpu():
+ print('skipped testing quantized_conv for naive cpu since it is not
implemented yet')
return
- def check_quantized_conv(data_shape, kernel, num_filter, pad, stride,
no_bias):
- with mx.Context('gpu', 0):
- # run fp32 conv
- data = mx.sym.Variable(name='data', shape=data_shape,
dtype='float32')
- conv2d = mx.sym.Convolution(data=data, kernel=kernel,
num_filter=num_filter, pad=pad, stride=stride,
- no_bias=no_bias, cudnn_off=False,
name='conv2d')
- arg_shapes, _, _ = conv2d.infer_shape(data=data_shape)
- arg_names = conv2d.list_arguments()
- conv_exe_fp32 = conv2d.simple_bind(ctx=mx.current_context(),
grad_req='null')
- conv_exe_fp32.arg_dict[arg_names[0]][:] =
mx.nd.random.uniform(low=-127.0, high=127.0,
-
shape=data_shape).astype('int32')
- conv_exe_fp32.arg_dict[arg_names[1]][:] =
mx.nd.random.uniform(low=-127.0, high=127.0,
-
shape=arg_shapes[1]).astype('int32')
- if not no_bias:
- conv_exe_fp32.arg_dict[arg_names[2]][:] =
mx.nd.random.uniform(low=-127.0, high=127.0,
-
shape=arg_shapes[2]).astype('int32')
- output = conv_exe_fp32.forward()[0]
-
- # run quantized conv
- qdata = mx.sym.Variable(name='qdata', shape=data_shape,
dtype='int8')
- qweight = mx.sym.Variable(name='qweight', dtype='int8')
- min_data = mx.sym.Variable(name='min_data')
- max_data = mx.sym.Variable(name='max_data')
- min_weight = mx.sym.Variable(name='min_weight')
- max_weight = mx.sym.Variable(name='max_weight')
- quantized_conv2d = mx.sym.contrib.quantized_conv(data=qdata,
weight=qweight, min_data=min_data,
-
max_data=max_data, min_weight=min_weight,
-
max_weight=max_weight, kernel=kernel,
-
num_filter=num_filter, pad=pad, stride=stride,
- no_bias=no_bias)
- qarg_names = quantized_conv2d.list_arguments()
- type_dict = None
- if not no_bias:
- type_dict = {qarg_names[2]: 'int8'}
- conv_exe_int8 =
quantized_conv2d.simple_bind(ctx=mx.current_context(), type_dict=type_dict,
grad_req='null')
- conv_exe_int8.arg_dict[qarg_names[0]][:] =
conv_exe_fp32.arg_dict[arg_names[0]].astype('int8')
- conv_exe_int8.arg_dict[qarg_names[1]][:] =
conv_exe_fp32.arg_dict[arg_names[1]].astype('int8')
- quantized_range = 127.0
- if no_bias:
- conv_exe_int8.arg_dict[qarg_names[2]][:] = -quantized_range
- conv_exe_int8.arg_dict[qarg_names[3]][:] = quantized_range
- conv_exe_int8.arg_dict[qarg_names[4]][:] = -quantized_range
- conv_exe_int8.arg_dict[qarg_names[5]][:] = quantized_range
- else:
- conv_exe_int8.arg_dict[qarg_names[2]][:] =
conv_exe_fp32.arg_dict[arg_names[2]].astype('int8')
- conv_exe_int8.arg_dict[qarg_names[3]][:] = -quantized_range
- conv_exe_int8.arg_dict[qarg_names[4]][:] = quantized_range
- conv_exe_int8.arg_dict[qarg_names[5]][:] = -quantized_range
- conv_exe_int8.arg_dict[qarg_names[6]][:] = quantized_range
- conv_exe_int8.arg_dict[qarg_names[7]][:] = -quantized_range
- conv_exe_int8.arg_dict[qarg_names[8]][:] = quantized_range
- qoutput, min_range, max_range = conv_exe_int8.forward()
+ if is_test_for_mkldnn():
+ dtype = 'uint8'
Review comment:
Currently MKLDNN quantization only support uint8 as input data so we made
separate logic for mkldnn.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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