Issue description
When converting a pretrained quantized GluonCV model to a TVM Relay graph a range parameter related to quantization is set to zero causing a RuntimeWarning about division by zero.
Steps to reproduce the issue
- Prepare hardware and environment that meet the requirements for TVM
- Install MXNet 1.5.1 or 1.6.0, GluonCV 0.7.0, and the latest MKL-DNN library
- Build TVM with USE_MKLDNN ON
- Download a pretrained INT8 model from GluonCV with
gluoncv.model_zoo_get_model()
- Convert the model to a TVM Relay graph with
tvm.relay.frontend.from_mxnet()
What’s the expected result?
- Graph conversion yields no RuntimeWarning
What’s the actual result?
-
Graph conversion yields the following RuntimeWarning:
/usr/tvm/python/tvm/relay/frontend/mxnet_qnn_op_utils.py:53: RuntimeWarning: divide by zero encountered in true_divide scale = np.divide(quantized_range, real_range)
Additional details
-
Debugging the code pointed to by the warning message reveals that the argument
real_range
is zero and the RuntimeWarning is thrown by the NumPy functiondivide()
-
The argument
real_range
is zero because the parametersmin_calib_range
andmax_calib_range
in_qnn_quantize()
inrelay/frontend/mxnet.py
are set to their default values of0.0
. -
The parameters
min_calib_range
andmax_calib_range
are set to their default values because the relevant operator, e.g.,_contrib_quantize_v2
, has no attributesmin_calib_range
andmax_calib_range
-
When compiling the model
ssd_512_vgg16_atrous_voc_int8
withtvm.relay.build()
atopt_level=3
after graph conversion is complete, the following error occurs and compilation fails, as described in TVMError Check failed Divide by zero when compiling INT8 model :TVMError: Check failed: fb->value != 0 (0 vs. 0) : Divide by zero
-
Hard-coding the default value of
max_calib_range
to20.0
(arbitrarily picked) in_qnn_quantize()
removes both the RuntimeWarning and the above compilation error, and inference is completed successfully with the modelssd_512_vgg16_atrous_voc_int8
Suggested solutions
- Fix TVM to properly handle the case where an operator of a quantized model has no attributes
min_calib_range
ormax_calib_range