anijain2305 commented on a change in pull request #6782: URL: https://github.com/apache/incubator-tvm/pull/6782#discussion_r513775161
########## File path: python/tvm/relay/frontend/qnn_torch.py ########## @@ -826,6 +826,74 @@ def _impl(inputs, _): return _impl +def _linear_dynamic(): + def _calculate_qparam(inp): + # reference ATen/native/quantized/cpu/qlinear_dynamic.cpp + # ChooseQuantizationParams function + mn = _op.min(inp) + mx = _op.max(inp) + + # Ensure that the interval contains 0 + mn = _op.minimum(mn, _op.const(0.0, dtype="float32")) + mx = _op.maximum(mx, _op.const(0.0, dtype="float32")) + + qmax = 255 + + # reduce_range became True in v1.6 + if is_version_greater_than("1.5.1"): + qmax = 127 + + scale = (mx - mn) / _expr.const(qmax, dtype="float32") + + zero_point_from_min = -(mn / scale) + zero_point = _op.cast(_op.round(_op.clip(zero_point_from_min, 0.0, qmax)), "int32") + + return scale, zero_point + + def _impl(inputs, _): + weight = inputs[1][0] + weight_scale = inputs[1][1] + weight_zero_point = inputs[1][2] + + inp = inputs[0] + + input_scale, input_zero_point = _calculate_qparam(inp) + qinp = relay.qnn.op.quantize(inp, input_scale, input_zero_point, out_dtype="uint8") + + data_shape = infer_shape(inp) + + if len(data_shape) > 2: + qinp = _op.reverse_reshape(qinp, [-1, 0]) + + weight_shape = infer_shape(weight) + units = weight_shape[0] + dense = relay.qnn.op.dense( + qinp, + weight, + input_zero_point, + weight_zero_point, + input_scale, + weight_scale, + units=units, + ) + bias_var = inputs[1][3] + + dequant_scale = input_scale * weight_scale + dense_out = _op.cast(dense, "float32") * dequant_scale Review comment: It might be better to call qnn.dequantize operation here. ---------------------------------------------------------------- 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: us...@infra.apache.org