From 917dab7f92735b3bf2ebd3c6888faa4aefbe17c0 Mon Sep 17 00:00:00 2001 From: Gayatri Panchapakesan Kumari Date: Tue, 21 Sep 2021 05:42:04 -0500 Subject: [PATCH 1/4] [ONNX] QLinearLeakyRelu contrib op --- python/tvm/relay/frontend/onnx.py | 20 ++++++++++++++++++++ tests/python/frontend/onnx/test_forward.py | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 5b5076a4997c..954795530ddf 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -3430,6 +3430,25 @@ def _impl_v10(cls, inputs, attr, params): return _qnn.op.quantize(out, y_scale, y_zero_point, out_dtype=dtype) +class QLinearLeakyRelu(OnnxOpConverter): + """Operator converter for QLinearLeakyRelu from Microsoft onnxruntime contrib opset.""" + + @classmethod + def _impl_v10(cls, inputs, attr, params): + + a_scale = get_scalar(inputs[1], params) + a_zero_point = get_scalar(inputs[2], params, "int32") + y_scale = fold_constant(get_scalar(inputs[3], params)) + y_zero_point = get_scalar(inputs[4], params, "int32") + alpha = float(attr.get("alpha", 1.0)) + + dtype = infer_type(inputs[0]).checked_type.dtype + + a = _qnn.op.dequantize(inputs[0], a_scale, a_zero_point) + out = _op.nn.leaky_relu(a, alpha) + return _qnn.op.quantize(out, y_scale, y_zero_point, out_dtype=dtype) + + class QLinearSigmoid(OnnxOpConverter): """Operator converter for QLinearSigmoid from Microsoft onnxruntime contrib opset.""" @@ -4114,6 +4133,7 @@ def _get_convert_map(opset): "ConvInteger": ConvInteger.get_converter(opset), "QLinearAveragePool": QLinearAveragePool.get_converter(opset), "QLinearGlobalAveragePool": QLinearGlobalAveragePool.get_converter(opset), + "QLinearLeakyRelu": QLinearLeakyRelu.get_converter(opset), # Random number generation. "RandomUniform": RandomUniform.get_converter(opset), # Loss functions / training diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index ae1f52215a19..d076e1a2e098 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -5557,6 +5557,27 @@ def verify_qlinearmul(a_shape, b_shape, c_shape): verify_qlinearmul([5, 1, 7], [2, 7], [5, 2, 7]) +@tvm.testing.parametrize_targets +def test_qlinearleakyrelu(target, dev): + def verify_qlinearleakyrelu(inshape, kwargs): + + in_array = np.random.random(inshape).astype("float32") + node = helper.make_node("LeakyRelu", ["X"], ["Y"], **kwargs) + + graph = helper.make_graph( + [node], + "qlinearRelu_test", + inputs=[helper.make_tensor_value_info("X", TensorProto.FLOAT, list(in_array.shape))], + outputs=[helper.make_tensor_value_info("Y", TensorProto.FLOAT, list(in_array.shape))], + ) + model = helper.make_model(graph, producer_name="qlinearRelu_test") + quantize_and_verify_with_ort(model, ["X"], [in_array.shape], target, dev) + + verify_qlinearleakyrelu([2, 4, 5, 6], {"alpha": 0.25}) + verify_qlinearleakyrelu([6, 5, 6, 7], {"alpha": 0.35}) + verify_qlinearleakyrelu([5, 1, 4, 6], {"alpha": 0.65}) + + @tvm.testing.parametrize_targets def test_qlinearsigmoid(target, dev): def verify_qlinearsigmoid(a_shape): From c6c5b764d7c67ea57c59e54ff1d56c64e354220c Mon Sep 17 00:00:00 2001 From: Gayatri Panchapakesan Kumari Date: Wed, 22 Sep 2021 10:06:25 -0500 Subject: [PATCH 2/4] Add comment --- python/tvm/relay/frontend/onnx.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 954795530ddf..7899f8e380ab 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -3444,6 +3444,9 @@ def _impl_v10(cls, inputs, attr, params): dtype = infer_type(inputs[0]).checked_type.dtype + # Onnxruntime doesn't actually do this op in integer, they dequantize to fp32 + # and then requantize afer (according to documentation below) + # https://github.com/microsoft/onnxruntime/blob/master/docs/ContribOperators.md#com.microsoft.QLinearLeakyRelu a = _qnn.op.dequantize(inputs[0], a_scale, a_zero_point) out = _op.nn.leaky_relu(a, alpha) return _qnn.op.quantize(out, y_scale, y_zero_point, out_dtype=dtype) From 6b6f042af916b39df4ad8be70fbc5065b19d8200 Mon Sep 17 00:00:00 2001 From: Gayatri Panchapakesan Kumari Date: Wed, 29 Sep 2021 03:21:43 -0500 Subject: [PATCH 3/4] jostle ci From 559bc32c964e503dbf4ab30989ae43f0b9f525db Mon Sep 17 00:00:00 2001 From: Gayatri Panchapakesan Kumari Date: Fri, 1 Oct 2021 01:44:20 -0500 Subject: [PATCH 4/4] jostle ci