From 93bcd9beb6b616f33f723548fa8c180c2bd2239e Mon Sep 17 00:00:00 2001 From: Ashwini Khade Date: Fri, 1 Feb 2019 07:59:45 -0800 Subject: [PATCH] Type and Shape inference for QuantizeLinear and DeQuantizeLinear Ops (#408) * Type and Shape inference for QuantizeeLinear and DeQuantizeLinear Ops * removing redundant type checking for some inputs and outputs * remove unnecessary type check deom type inference --- .../core/graph/contrib_ops/contrib_defs.cc | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/onnxruntime/core/graph/contrib_ops/contrib_defs.cc b/onnxruntime/core/graph/contrib_ops/contrib_defs.cc index 16704c46b4..0644ffdd5e 100644 --- a/onnxruntime/core/graph/contrib_ops/contrib_defs.cc +++ b/onnxruntime/core/graph/contrib_ops/contrib_defs.cc @@ -522,7 +522,16 @@ activation and leaky_relu_alpha.)DOC") .SetDoc(R"DOC( The linear quantization operator. It consumes a full precision data, a scale, a zero point and computes the quantized data. The quantization formula is y = (x / y_scale) + y_zero_point. For (x / y_scale), it computes the nearest integer value to arg (in floating-point format), - rounding halfway cases away from zero. Scale and zero point must have same shape. They must be either scalar (per tensor) or 1-D tensor (per 'axis').)DOC"); + rounding halfway cases away from zero. Scale and zero point must have same shape. They must be either scalar (per tensor) or 1-D tensor (per 'axis').)DOC") + .TypeAndShapeInferenceFunction([](ONNX_NAMESPACE::InferenceContext& ctx) { + propagateElemTypeFromInputToOutput(ctx, 2, 0); + + if (!hasInputShape(ctx, 0)) + return; + + auto& input_shape = getInputShape(ctx, 0); + updateOutputShape(ctx, 0, input_shape); + }); ONNX_CONTRIB_OPERATOR_SCHEMA(DequantizeLinear) .SetDomain(kMSDomain) @@ -543,7 +552,18 @@ The quantization formula is y = (x / y_scale) + y_zero_point. For (x / y_scale), .SetDoc(R"DOC( The linear de-quantization operator. It consumes a quantized data, a scale, a zero point and computes the full precision data. The dequantization formula is y = (x - x_zero_point) * x_scale. - Scale and zero point must have same shape. They must be either scalar (per tensor) or 1-D tensor (per 'axis').)DOC"); +Scale and zero point must have same shape. They must be either scalar (per tensor) or 1-D tensor (per 'axis').)DOC") + .TypeAndShapeInferenceFunction([](ONNX_NAMESPACE::InferenceContext& ctx) { + auto y_type = ctx.getOutputType(0); + // only float is supported + y_type->mutable_tensor_type()->set_elem_type(ONNX_NAMESPACE::TensorProto::FLOAT); + + if (!hasInputShape(ctx, 0)) + return; + + auto& input_shape = getInputShape(ctx, 0); + updateOutputShape(ctx, 0, input_shape); + }); ONNX_CONTRIB_OPERATOR_SCHEMA(QLinearMatMul) .SetDomain(kMSDomain)