Skip to content

Commit 221215b

Browse files
authored
[ETHOSN] Remove requantize dependency on resize (#14422)
A following requantize operation can instead be offloaded as a standalone operation, leaving to the support library to optimize. Change-Id: I573d8d67bcdf54f23d4c73ea70dbbe3f0c9be9cb
1 parent 683e7a4 commit 221215b

File tree

3 files changed

+18
-57
lines changed

3 files changed

+18
-57
lines changed

python/tvm/relay/op/contrib/ethosn.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,7 @@ def qnn_requantize_pattern():
206206
return pattern
207207

208208
def qnn_resize_pattern():
209-
pattern = is_op("image.resize2d")(wildcard()).has_attr({"method": "nearest_neighbor"})
210-
pattern = is_op("qnn.requantize")(
211-
pattern, is_constant(), is_constant(), is_constant(), is_constant()
212-
)
213-
return pattern
209+
return is_op("image.resize2d")(wildcard()).has_attr({"method": "nearest_neighbor"})
214210

215211
def qnn_mul_pattern():
216212
"""

src/relay/backend/contrib/ethosn/ethosn_api.cc

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -838,40 +838,27 @@ EthosnError EthosnAPI::ReinterpretQuantize(const Expr& expr,
838838
}
839839

840840
EthosnError EthosnAPI::Resize(const Expr& expr, ResizeParams* params) {
841-
Call requantize = Downcast<Call>(expr);
842-
Call resize = Downcast<Call>(requantize->args[0]);
841+
Call resize = Downcast<Call>(expr);
842+
const auto* input_ttype = resize->args[0]->checked_type().as<TensorTypeNode>();
843+
844+
const auto* attrs = resize->attrs.as<Resize2DAttrs>();
845+
uint32_t height, width;
846+
EthosnError err = Tvm2Npu(attrs->size, &height, &width);
847+
params->resize_info = sl::ResizeInfo{sl::ResizeAlgorithm::NEAREST_NEIGHBOUR, height, width,
848+
params->input_info.m_QuantizationInfo};
843849

844-
const auto* input_dtype = resize->args[0]->checked_type().as<TensorTypeNode>();
845850
sl::TensorShape input_tensor_shape = {1, 1, 1, 1};
846-
EthosnError err = Tvm2Npu(input_dtype->shape, &input_tensor_shape);
847851
sl::DataType input_tensor_dtype;
848-
err += Tvm2Npu(input_dtype->dtype, &input_tensor_dtype);
849-
float input_sc;
850-
int input_zp;
851-
err += AsConstant(requantize->args[2], &input_zp);
852-
err += AsConstant(requantize->args[1], &input_sc);
853-
sl::QuantizationInfo input_q_info;
854-
err += Tvm2Npu(input_zp, input_sc, &input_q_info);
852+
err = Tvm2Npu(input_ttype->shape, &input_tensor_shape);
853+
err += Tvm2Npu(input_ttype->dtype, &input_tensor_dtype);
855854
params->input_info =
856-
sl::TensorInfo(input_tensor_shape, input_tensor_dtype, sl::DataFormat::NHWC, input_q_info);
855+
sl::TensorInfo(input_tensor_shape, input_tensor_dtype, params->input_info.m_DataFormat,
856+
params->input_info.m_QuantizationInfo);
857857

858-
float output_sc;
859-
int output_zp;
860-
err += AsConstant(requantize->args[3], &output_sc);
861-
err += AsConstant(requantize->args[4], &output_zp);
862-
sl::QuantizationInfo resize_q_info;
863-
err += Tvm2Npu(output_zp, output_sc, &resize_q_info);
864-
const auto* attrs = resize->attrs.as<Resize2DAttrs>();
865-
uint32_t height, width;
866-
err += Tvm2Npu(attrs->size, &height, &width);
867-
params->resize_info =
868-
sl::ResizeInfo{sl::ResizeAlgorithm::NEAREST_NEIGHBOUR, height, width, resize_q_info};
869-
870-
sl::TensorInfo output_info = params->input_info;
871-
output_info.m_Dimensions[1] = params->resize_info.m_NewHeight;
872-
output_info.m_Dimensions[2] = params->resize_info.m_NewWidth;
873-
output_info.m_QuantizationInfo = params->resize_info.m_OutputQuantizationInfo;
874-
params->output_info = output_info;
858+
sl::TensorInfo output_tensor_info;
859+
err += Tvm2Npu(resize->checked_type(), &output_tensor_info);
860+
output_tensor_info.m_QuantizationInfo = params->input_info.m_QuantizationInfo;
861+
params->output_info = output_tensor_info;
875862

876863
return err;
877864
}

tests/python/contrib/test_ethosn/test_resize.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,18 @@ def _get_model(
2929
shape,
3030
dtype,
3131
size,
32-
input_zp,
33-
input_sc,
34-
output_zp,
35-
output_sc,
3632
coordinate_transformation_mode,
3733
rounding_method,
3834
):
3935
x = relay.var("x", shape=shape, dtype=dtype)
40-
resize = relay.image.resize2d(
36+
return relay.image.resize2d(
4137
data=x,
4238
size=size,
4339
layout="NHWC",
4440
method="nearest_neighbor",
4541
coordinate_transformation_mode=coordinate_transformation_mode,
4642
rounding_method=rounding_method,
4743
)
48-
model = relay.qnn.op.requantize(
49-
resize,
50-
input_scale=relay.const(input_sc, "float32"),
51-
input_zero_point=relay.const(input_zp, "int32"),
52-
output_scale=relay.const(output_sc, "float32"),
53-
output_zero_point=relay.const(output_zp, "int32"),
54-
out_dtype=dtype,
55-
)
56-
return model
5744

5845

5946
@requires_ethosn
@@ -82,10 +69,6 @@ def test_resize(dtype, shape, size, coordinate_transformation_mode, rounding_met
8269
shape=shape,
8370
dtype=dtype,
8471
size=size,
85-
input_zp=zp_min + 128,
86-
input_sc=0.0784314,
87-
output_zp=zp_min + 128,
88-
output_sc=0.0784314,
8972
coordinate_transformation_mode=coordinate_transformation_mode,
9073
rounding_method=rounding_method,
9174
)
@@ -113,16 +96,11 @@ def test_resize(dtype, shape, size, coordinate_transformation_mode, rounding_met
11396
def test_resize_failure(size, err_msg):
11497
"""Check Resize error messages."""
11598
dtype = "int8"
116-
zp_min = np.iinfo(dtype).min
11799

118100
model = _get_model(
119101
shape=(1, 10, 10, 1),
120102
dtype=dtype,
121103
size=size,
122-
input_zp=zp_min + 128,
123-
input_sc=0.0784314,
124-
output_zp=zp_min + 128,
125-
output_sc=0.0784314,
126104
coordinate_transformation_mode="half_pixel",
127105
rounding_method="round_prefer_ceil",
128106
)

0 commit comments

Comments
 (0)