Consider these two examples
from mxnet.ndarray.contrib import quantize, requantize, dequantize
from mxnet import random
# Dequantizing directly the quantization results
data_shape = (5, 5)
data_low = 0.0
data_high = 127.0
data = mx.nd.random.uniform(low=data_low, high=data_high, shape=data_shape).astype('float32')
print(data)
quantized_data, quantized_data_min, quantized_data_max = quantize(data, data.min(), data.max())
dequantized_data = dequantize(quantized_data, quantized_data_min, quantized_data_max)
print(dequantized_data)
# output of quantization
[[ 18.81389 99.11418 124.69234 125.0991 60.75303 ]
[ 95.70334 63.168705 0.5141094 81.21301 34.22389 ]
[ 46.810246 52.132504 17.386335 54.384426 104.40895 ]
[ 37.825912 24.110685 50.94381 64.93751 15.323439 ]
[ 28.488262 124.54886 12.42625 51.5773 109.49832 ]]
<NDArray 5x5 @cpu(0)>
# output of dequantization
[[ 18.64222 99.09811 124.60851 125.0991 60.832504 ]
[ 95.66402 63.285427 0.4905847 81.43706 34.34093 ]
[ 46.605545 52.00198 17.170465 54.454903 104.494545 ]
[ 37.77502 24.03865 51.02081 64.75718 15.208126 ]
[ 28.453913 124.60851 12.264618 51.511395 109.40039 ]]
<NDArray 5x5 @cpu(0)>
Now consider the example of dequantization
data_shape = (5, 5)
data_low = 0.0
data_high = 127.0
data = mx.nd.random.uniform(low=data_low, high=data_high, shape=data_shape).astype('float32')
print(data)
quantized_data, quantized_data_min, quantized_data_max = quantize(data, data.min(), data.max())
requantized_data, requantized_data_min, requantized_data_max = requantize(
quantized_data.astype('int32'), quantized_data_min, quantized_data_max)
dequantized_data = dequantize(requantized_data, requantized_data_min, requantized_data_max)
print(dequantized_data)
# output of quantization
[[ 72.289764 123.560776 43.637897 122.026 100.186844 ]
[115.132545 52.244297 98.30401 45.62747 42.309433 ]
[ 50.736362 10.299877 38.332523 51.719627 98.45291 ]
[ 29.493734 117.629036 16.82593 41.31441 6.7852516]
[121.01457 92.15048 1.7714465 1.4512868 67.750145 ]]
<NDArray 5x5 @cpu(0)>
# output of dequantization with requantization in between
[[8.5490710e-06 1.4672055e-05 5.1987595e-06 1.4556526e-05 1.1899383e-05]
[1.3747831e-05 6.2385116e-06 1.1668327e-05 5.4298157e-06 4.9677037e-06]
[6.0074553e-06 1.1552798e-06 4.5055917e-06 6.1229835e-06 1.1668327e-05]
[3.4658397e-06 1.3978886e-05 1.9639758e-06 4.8521756e-06 8.0869592e-07]
[1.4440999e-05 1.0975159e-05 2.3105598e-07 1.1552799e-07 8.0869595e-06]]
<NDArray 5x5 @cpu(0)>
As you can see, with the introduction of mx.ndarray.contrib.requntize
produces wildly more error than should be acceptable. Am i doing something wrong here, or there is some issue with the implementation?