Skip to content

Commit e9b6845

Browse files
authored
[microNPU] Adding rounding mode attribute to operators (#9514)
* [microNPU] Adding rounding mode attribute to operators Allows rounding mode to be specified for each supported operator. By default "TFL" is used, which matches that of the behavior of TFLite. Other rounding mode options include "NATURAL" which rounds to the nearest value and "TRUNCATE" which rounds towards zero.
1 parent ee23013 commit e9b6845

29 files changed

+371
-114
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def _extract_ethosu_binary_elementwise_params(attrs, args):
4545
activation = attrs.activation
4646
clip_min = attrs.clip_min
4747
clip_max = attrs.clip_max
48+
rounding_mode = attrs.rounding_mode
4849
ifm_layout = attrs.ifm_layout
4950
ifm2_layout = attrs.ifm2_layout
5051
ofm_layout = attrs.ofm_layout
@@ -67,6 +68,7 @@ def _extract_ethosu_binary_elementwise_params(attrs, args):
6768
activation,
6869
clip_min,
6970
clip_max,
71+
rounding_mode,
7072
ifm_layout,
7173
ifm2_layout,
7274
ofm_layout,
@@ -111,6 +113,7 @@ def ethosu_binary_elementwise(
111113
activation: Optional[str] = "NONE",
112114
clip_min: Optional[int] = 0,
113115
clip_max: Optional[int] = 0,
116+
rounding_mode: Optional[str] = "TFL",
114117
ifm_layout: Optional[str] = "NHWC",
115118
ifm2_layout: Optional[str] = "NHWC",
116119
ofm_layout: Optional[str] = "NHWC",
@@ -179,6 +182,11 @@ def ethosu_binary_elementwise(
179182
The minimum clipping value if activation = "CLIP".
180183
clip_max : int, optional
181184
The maximum clipping value if activation = "CLIP".
185+
rounding_mode : str, optional
186+
The rounding mode to apply to the Output Feature Map tensor.
187+
"TFL" - Tensorflow Lite rounding scheme.
188+
"TRUNCATE" - Truncate towards zero.
189+
"NATURAL" - Round to nearest value, with x.5 rounded up towards +infinity.
182190
ifm_layout : str, optional
183191
The layout of the Input Feature Map tensor. Can be "NHWC" or "NHCWB16".
184192
ifm2_layout : str, optional
@@ -208,6 +216,7 @@ def ethosu_binary_elementwise(
208216
activation,
209217
clip_min,
210218
clip_max,
219+
rounding_mode,
211220
ifm_layout,
212221
ifm2_layout,
213222
ofm_layout,

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def _extract_ethosu_conv2d_params(attrs, args):
4545
activation = attrs.activation
4646
clip_min = attrs.clip_min
4747
clip_max = attrs.clip_max
48+
rounding_mode = attrs.rounding_mode
4849
upscale = attrs.upscale
4950
ifm_layout = attrs.ifm_layout
5051
ofm_layout = attrs.ofm_layout
@@ -65,6 +66,7 @@ def _extract_ethosu_conv2d_params(attrs, args):
6566
activation,
6667
clip_min,
6768
clip_max,
69+
rounding_mode,
6870
upscale,
6971
ifm_layout,
7072
ofm_layout,
@@ -108,6 +110,7 @@ def ethosu_conv2d(
108110
activation: str = "NONE",
109111
clip_min: int = 0,
110112
clip_max: int = 0,
113+
rounding_mode: str = "TFL",
111114
upscale: str = "NONE",
112115
ifm_layout: str = "NHWC",
113116
ofm_layout: str = "NHWC",
@@ -164,6 +167,11 @@ def ethosu_conv2d(
164167
The minimum clipping value if activation = "CLIP"
165168
clip_max : int, optional,
166169
The maximum clipping value if activation = "CLIP"
170+
rounding_mode : str, optional
171+
The rounding mode to apply to the Output Feature Map tensor.
172+
"TFL" - Tensorflow Lite rounding scheme.
173+
"TRUNCATE" - Truncate towards zero.
174+
"NATURAL" - Round to nearest value, with x.5 rounded up towards +infinity.
167175
upscale : str, optional
168176
The 2x2 upscaling mode to apply to the Input Feature Map tensor.
169177
"NONE" - no upscaling.
@@ -198,6 +206,7 @@ def ethosu_conv2d(
198206
activation,
199207
clip_min,
200208
clip_max,
209+
rounding_mode,
201210
upscale,
202211
ifm_layout,
203212
ofm_layout,

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def _extract_ethosu_depthwise_conv2d_params(attrs, args):
4646
activation = attrs.activation
4747
clip_min = attrs.clip_min
4848
clip_max = attrs.clip_max
49+
rounding_mode = attrs.rounding_mode
4950
upscale = attrs.upscale
5051
ifm_layout = attrs.ifm_layout
5152
ofm_layout = attrs.ofm_layout
@@ -66,6 +67,7 @@ def _extract_ethosu_depthwise_conv2d_params(attrs, args):
6667
activation,
6768
clip_min,
6869
clip_max,
70+
rounding_mode,
6971
upscale,
7072
ifm_layout,
7173
ofm_layout,
@@ -109,6 +111,7 @@ def ethosu_depthwise_conv2d(
109111
activation: str = "NONE",
110112
clip_min: int = 0,
111113
clip_max: int = 0,
114+
rounding_mode: str = "TFL",
112115
upscale: str = "NONE",
113116
ifm_layout: str = "NHWC",
114117
ofm_layout: str = "NHWC",
@@ -166,6 +169,11 @@ def ethosu_depthwise_conv2d(
166169
The minimum clipping value if activation = "CLIP"
167170
clip_max : int, optional,
168171
The maximum clipping value if activation = "CLIP"
172+
rounding_mode : str, optional
173+
The rounding mode to apply to the Output Feature Map tensor.
174+
"TFL" - Tensorflow Lite rounding scheme.
175+
"TRUNCATE" - Truncate towards zero.
176+
"NATURAL" - Round to nearest value, with x.5 rounded up towards +infinity.
169177
upscale : str, optional
170178
The 2x2 upscaling mode to apply to the Input Feature Map tensor.
171179
"NONE" - no upscaling.
@@ -200,6 +208,7 @@ def ethosu_depthwise_conv2d(
200208
activation,
201209
clip_min,
202210
clip_max,
211+
rounding_mode,
203212
upscale,
204213
ifm_layout,
205214
ofm_layout,

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def _extract_ethosu_pooling_params(attrs, args):
4444
activation = attrs.activation
4545
clip_min = attrs.clip_min
4646
clip_max = attrs.clip_max
47+
rounding_mode = attrs.rounding_mode
4748
upscale = attrs.upscale
4849
ifm_layout = attrs.ifm_layout
4950
ofm_layout = attrs.ofm_layout
@@ -63,6 +64,7 @@ def _extract_ethosu_pooling_params(attrs, args):
6364
activation,
6465
clip_min,
6566
clip_max,
67+
rounding_mode,
6668
upscale,
6769
ifm_layout,
6870
ofm_layout,
@@ -103,6 +105,7 @@ def ethosu_pooling(
103105
activation: str = "NONE",
104106
clip_min: int = 0,
105107
clip_max: int = 0,
108+
rounding_mode: str = "TFL",
106109
upscale: str = "NONE",
107110
ifm_layout: str = "NHWC",
108111
ofm_layout: str = "NHWC",
@@ -146,6 +149,11 @@ def ethosu_pooling(
146149
The minimum clipping value if activation = "CLIP".
147150
clip_max : int, optional
148151
The maximum clipping value if activation = "CLIP".
152+
rounding_mode : str, optional
153+
The rounding mode to apply to the Output Feature Map tensor.
154+
"TFL" - Tensorflow Lite rounding scheme.
155+
"TRUNCATE" - Truncate towards zero.
156+
"NATURAL" - Round to nearest value, with x.5 rounded up towards +infinity.
149157
upscale: str, optional
150158
The 2x2 upscaling mode to apply to the Input Feature Map tensor.
151159
"NONE" - no upscaling.
@@ -176,6 +184,7 @@ def ethosu_pooling(
176184
activation,
177185
clip_min,
178186
clip_max,
187+
rounding_mode,
179188
upscale,
180189
ifm_layout,
181190
ofm_layout,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def binary_elementwise_compute(
3838
activation: str,
3939
clip_min: int,
4040
clip_max: int,
41+
rounding_mode: str,
4142
ifm_layout: str,
4243
ifm2_layout: str,
4344
ofm_layout: str,
@@ -94,6 +95,11 @@ def binary_elementwise_compute(
9495
The minimum clipping value if activation = "CLIP".
9596
clip_max : int
9697
The maximum clipping value if activation = "CLIP".
98+
rounding_mode : str
99+
The rounding mode to apply to the Output Feature Map tensor.
100+
"TFL" - Tensorflow Lite rounding scheme.
101+
"TRUNCATE" - Truncate towards zero.
102+
"NATURAL" - Round to nearest value, with x.5 rounded up towards +infinity.
97103
ifm_layout : str, optional
98104
The layout of the Input Feature Map tensor. Can be "NHWC" or "NHCWB16".
99105
ifm2_layout : str, optional
@@ -136,6 +142,7 @@ def binary_elementwise_compute(
136142
"activation": activation,
137143
"clip_min": clip_min,
138144
"clip_max": clip_max,
145+
"rounding_mode": rounding_mode,
139146
}
140147

141148
operators = {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def conv2d_compute(
3838
activation: str,
3939
clip_min: int,
4040
clip_max: int,
41+
rounding_mode: str,
4142
upscale: str,
4243
ifm_layout: str,
4344
ofm_layout: str,
@@ -81,11 +82,17 @@ def conv2d_compute(
8182
The minimum clipping value if activation = "CLIP".
8283
clip_max : int
8384
The maximum clipping value if activation = "CLIP".
85+
rounding_mode : str
86+
The rounding mode to apply to the Output Feature Map tensor.
87+
"TFL" - Tensorflow Lite rounding scheme.
88+
"TRUNCATE" - Truncate towards zero.
89+
"NATURAL" - Round to nearest value, with x.5 rounded up towards +infinity.
8490
upscale : str
8591
The 2x2 upscaling mode to apply to the Input Feature Map tensor.
8692
"NONE" - no upscaling.
8793
"NEAREST" - upscale using nearest neighbour.
8894
"ZEROS" - upscale using zeros.
95+
"NATURAL" - Round to nearest value, with x.5 rounded up towards +infinity.
8996
ifm_layout : str
9097
The layout of the Input Feature Map tensor. Can be "NHWC" or "NHCWB16".
9198
ofm_layout : str
@@ -126,6 +133,7 @@ def conv2d_compute(
126133
"upscale": upscale,
127134
"clip_min": clip_min,
128135
"clip_max": clip_max,
136+
"rounding_mode": rounding_mode,
129137
"stride_h": stride_h,
130138
"stride_w": stride_w,
131139
"dilation_h": dilation_h,

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def depthwise_conv2d_compute(
3838
activation: str,
3939
clip_min: int,
4040
clip_max: int,
41+
rounding_mode: str,
4142
upscale: str,
4243
ifm_layout: str,
4344
ofm_layout: str,
@@ -81,6 +82,11 @@ def depthwise_conv2d_compute(
8182
The minimum clipping value if activation = "CLIP".
8283
clip_max : int
8384
The maximum clipping value if activation = "CLIP".
85+
rounding_mode : str
86+
The rounding mode to apply to the Output Feature Map tensor.
87+
"TFL" - Tensorflow Lite rounding scheme.
88+
"TRUNCATE" - Truncate towards zero.
89+
"NATURAL" - Round to nearest value, with x.5 rounded up towards +infinity.
8490
upscale : str
8591
The 2x2 upscaling mode to apply to the Input Feature Map tensor.
8692
"NONE" - no upscaling.
@@ -120,9 +126,10 @@ def depthwise_conv2d_compute(
120126
"op": "ethosu_depthwise_conv2d",
121127
"weight_zero_point": weight_zero_point,
122128
"activation": activation,
123-
"upscale": upscale,
124129
"clip_min": clip_min,
125130
"clip_max": clip_max,
131+
"rounding_mode": rounding_mode,
132+
"upscale": upscale,
126133
"stride_h": stride_h,
127134
"stride_w": stride_w,
128135
"dilation_h": dilation_h,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def pooling_compute(
3737
activation: str,
3838
clip_min: int,
3939
clip_max: int,
40+
rounding_mode: str,
4041
upscale: str,
4142
ifm_layout: str,
4243
ofm_layout: str,
@@ -78,6 +79,11 @@ def pooling_compute(
7879
The minimum clipping value if activation = "CLIP".
7980
clip_max : int
8081
The maximum clipping value if activation = "CLIP".
82+
rounding_mode : str
83+
The rounding mode to apply to the Output Feature Map tensor.
84+
"TFL" - Tensorflow Lite rounding scheme.
85+
"TRUNCATE" - Truncate towards zero.
86+
"NATURAL" - Round to nearest value, with x.5 rounded up towards +infinity.
8187
upscale : str
8288
The 2x2 upscaling mode to apply to the Input Feature Map tensor.
8389
"NONE" - no upscaling.
@@ -113,6 +119,7 @@ def pooling_compute(
113119
"activation": activation,
114120
"clip_min": clip_min,
115121
"clip_max": clip_max,
122+
"rounding_mode": rounding_mode,
116123
"upscale": upscale,
117124
}
118125

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def get_binary_elementwise_params(
9696
operator_type=attrs["operator_type"],
9797
reversed_operands=reversed_operands,
9898
activation=serial_activation,
99+
rounding_mode=attrs["rounding_mode"],
99100
),
100101
output_pointer,
101102
replace_pointer,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def get_conv2d_params(stmt, producers, consumers):
9999
scale_bias=serial_scale_bias,
100100
padding=serial_padding,
101101
activation=serial_activation,
102+
rounding_mode=attrs["rounding_mode"],
102103
upscale="NONE",
103104
),
104105
output_pointer,

0 commit comments

Comments
 (0)