Skip to content

Commit 7315c9d

Browse files
[cherry-pick][microNPU][ETHOSU] Fix rounding mode in requantize operation (#15929) (#15946)
[microNPU][ETHOSU] Fix rounding mode in requantize operation (#15929) Before the fix, TFL rounding was used in the requantize operation and this led to a discrepancy in the results in some cases Co-authored-by: Aleksei-grovety <113356454+Aleksei-grovety@users.noreply.github.com>
1 parent b94bfe2 commit 7315c9d

File tree

8 files changed

+37
-5
lines changed

8 files changed

+37
-5
lines changed

python/tvm/relay/backend/contrib/ethosu/legalize.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,7 @@ def callback(
12161216
ifm_zero_point=int(params.ifm.q_params.zero_point),
12171217
ofm_scale=float(params.ofm.q_params.scale_f32),
12181218
ofm_zero_point=int(params.ofm.q_params.zero_point),
1219+
rounding_mode="NATURAL",
12191220
)
12201221

12211222

python/tvm/relay/backend/contrib/ethosu/op/identity.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@ def create_ethosu_identity_compute(attrs, args, out_type):
3636
ofm_scale = attrs.ofm_scale
3737
ofm_zero_point = attrs.ofm_zero_point
3838
activation = attrs.activation
39+
rounding_mode = attrs.rounding_mode
3940
op = identity_compute(
40-
ifm, lut, ifm_scale, ifm_zero_point, ofm_scale, ofm_zero_point, activation
41+
ifm,
42+
lut,
43+
ifm_scale,
44+
ifm_zero_point,
45+
ofm_scale,
46+
ofm_zero_point,
47+
activation,
48+
rounding_mode,
4149
)
4250
return [op]
4351

@@ -61,6 +69,7 @@ def ethosu_identity(
6169
ofm_scale: float = 1,
6270
ofm_zero_point: int = 0,
6371
activation: str = "NONE",
72+
rounding_mode: str = "TFL",
6473
) -> tvm.relay.Call:
6574
"""The Identity operator that runs on the NPU.
6675
@@ -87,12 +96,17 @@ def ethosu_identity(
8796
"TANH" - tanh activation function.
8897
"SIGMOID" - sigmoid activation function.
8998
"LUT" - use a look-up table to perform the activation function.
99+
rounding_mode : str, optional
100+
The rounding mode to apply to the Output Feature Map tensor.
101+
"TFL" - Tensorflow Lite rounding scheme.
102+
"TRUNCATE" - Truncate towards zero.
103+
"NATURAL" - Round to nearest value, with x.5 rounded up towards +infinity.
90104
91105
Returns
92106
-------
93107
out : tvm.relay.Call
94108
A call to the ethosu_identity op.
95109
"""
96110
return _make.ethosu_identity(
97-
ifm, lut, ifm_scale, ifm_zero_point, ofm_scale, ofm_zero_point, activation
111+
ifm, lut, ifm_scale, ifm_zero_point, ofm_scale, ofm_zero_point, activation, rounding_mode
98112
)

python/tvm/relay/backend/contrib/ethosu/te/identity.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def identity_compute(
3131
ofm_scale: float,
3232
ofm_zero_point: int,
3333
activation: str,
34+
rounding_mode: str,
3435
) -> te.Tensor:
3536
"""A compute operator for the NPU identity operator.
3637
@@ -54,14 +55,19 @@ def identity_compute(
5455
"TANH" - tanh activation function.
5556
"SIGMOID" - sigmoid activation function.
5657
"LUT" - use a look-up table to perform the activation function.
58+
rounding_mode : str
59+
The rounding mode to apply to the Output Feature Map tensor.
60+
"TFL" - Tensorflow Lite rounding scheme.
61+
"TRUNCATE" - Truncate towards zero.
62+
"NATURAL" - Round to nearest value, with x.5 rounded up towards +infinity.
5763
5864
Returns
5965
-------
6066
te.Tensor
6167
The Output Feature Map tensor.
6268
"""
6369
dmaed_ifm = read_compute(ifm, ifm_zero_point, ifm_scale)
64-
id_attrs = {"op": "ethosu_identity", "activation": activation}
70+
id_attrs = {"op": "ethosu_identity", "activation": activation, "rounding_mode": rounding_mode}
6571

6672
has_lut = activation in ("TANH", "LUT", "SIGMOID")
6773

python/tvm/relay/backend/contrib/ethosu/tir/identity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def get_identity_params(
166166
padding=SerialPadding(0, 0, 0, 0),
167167
activation=serial_activation,
168168
upscale="NONE",
169-
rounding_mode="TFL",
169+
rounding_mode=attrs["rounding_mode"],
170170
block_config=SerialBlockConfig(0, 0, 0),
171171
),
172172
output_pointer,

src/relay/op/contrib/ethosu/identity.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ bool EthosuIdentityRel(const Array<Type>& types, int num_inputs, const Attrs& at
6363
}
6464

6565
Expr MakeEthosuIdentity(Expr ifm, Expr lut, double ifm_scale, int ifm_zero_point, double ofm_scale,
66-
int ofm_zero_point, String activation) {
66+
int ofm_zero_point, String activation, String rounding_mode) {
6767
auto attrs = make_object<EthosuIdentityAttrs>();
6868
attrs->ifm_scale = ifm_scale;
6969
attrs->ifm_zero_point = ifm_zero_point;
7070
attrs->ofm_scale = ofm_scale;
7171
attrs->ofm_zero_point = ofm_zero_point;
7272
attrs->activation = std::move(activation);
73+
attrs->rounding_mode = std::move(rounding_mode);
7374
static const Op& op = Op::Get("contrib.ethosu.identity");
7475
return Call(op, {ifm, lut}, Attrs(attrs), {});
7576
}

src/relay/op/contrib/ethosu/op_attrs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ struct EthosuIdentityAttrs : public tvm::AttrsNode<EthosuIdentityAttrs> {
319319
double ofm_scale;
320320
int ofm_zero_point;
321321
String activation;
322+
String rounding_mode;
322323

323324
TVM_DECLARE_ATTRS(EthosuIdentityAttrs, "relay.attrs.EthosuIdentityAttrs") {
324325
TVM_ATTR_FIELD(ifm_scale).describe("The quantization scale for the Input Feature Map tensor.");
@@ -335,6 +336,13 @@ struct EthosuIdentityAttrs : public tvm::AttrsNode<EthosuIdentityAttrs> {
335336
"'SIGMOID' - sigmoid activation function. "
336337
"'LUT' - use a look-up table to perform the activation function.")
337338
.set_default("NONE");
339+
TVM_ATTR_FIELD(rounding_mode)
340+
.describe(
341+
"The rounding mode to apply to the Output Feature Map tensor. "
342+
"'TFL' - Tensorflow Lite rounding scheme. "
343+
"'TRUNCATE' - Truncate towards zero."
344+
"'NATURAL' - Round to nearest value, with x.5 rounded up towards +infinity.")
345+
.set_default("TFL");
338346
}
339347
};
340348

tests/python/contrib/test_ethosu/cascader/test_ethosu_identity_matcher.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_ethosu_identity_matcher():
3939
ofm_scale=1,
4040
ofm_zero_point=0,
4141
activation="NONE",
42+
rounding_mode="TFL",
4243
)
4344

4445
length = len(ifm.shape)

tests/python/contrib/test_ethosu/test_codegen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,7 @@ def split_func(x):
12331233
[
12341234
[(1, 8, 8, 3), 1.0, 0, 1.0, 0],
12351235
[(1, 20, 30, 3), 1.345, 34, 0.32, -23],
1236+
[(1, 1, 4, 8), 0.0078125, 0, 0.00997, -30],
12361237
],
12371238
)
12381239
def test_ethosu_requantize(accel_type, ifm_shape, ifm_scale, ifm_zp, ofm_scale, ofm_zp):

0 commit comments

Comments
 (0)