1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*!
21 * \file tvm/relay/attrs/image.h
22 * \brief Auxiliary attributes for image operators.
23 */
24#ifndef TVM_RELAY_ATTRS_IMAGE_H_
25#define TVM_RELAY_ATTRS_IMAGE_H_
26
27#include <tvm/ir/attrs.h>
28#include <tvm/relay/base.h>
29
30#include <string>
31
32namespace tvm {
33namespace relay {
34
35/*! \brief Attributes used in image resize1d operator */
36struct Resize1DAttrs : public tvm::AttrsNode<Resize1DAttrs> {
37 Array<IndexExpr> size;
38 Array<FloatImm> roi;
39 std::string layout;
40 std::string method;
41 std::string coordinate_transformation_mode;
42 std::string rounding_method;
43 double cubic_alpha;
44 int cubic_exclude;
45 double extrapolation_value;
46 DataType out_dtype;
47
48 TVM_DECLARE_ATTRS(Resize1DAttrs, "relay.attrs.Resize1DAttrs") {
49 TVM_ATTR_FIELD(size).set_default(NullValue<Array<IndexExpr>>()).describe("Output Size.");
50 TVM_ATTR_FIELD(roi)
51 .set_default(NullValue<Array<FloatImm>>())
52 .describe("Region of Interest for coordinate transformation mode 'tf_crop_and_resize'");
53 TVM_ATTR_FIELD(layout).set_default("NCW").describe(
54 "Dimension ordering of input data. Can be 'NCW', 'NWC', etc."
55 "'N', 'C', 'W' stands for batch, channel and width"
56 "dimensions respectively. Resize is applied on the"
57 "'W' dimension.");
58 TVM_ATTR_FIELD(method).set_default("linear").describe(
59 "Specify the mode to use for scaling."
60 "nearest_neighbor - Nearest Neighbor"
61 "linear - Linear Interpolation"
62 "cubic - Cubic Interpolation");
63 TVM_ATTR_FIELD(coordinate_transformation_mode)
64 .set_default("half_pixel")
65 .describe(
66 "Describes how to transform the coordinate in the resized tensor"
67 "to the coordinate in the original tensor."
68 "Refer to the ONNX Resize operator specification for details"
69 "Available options are half_pixel, align_corners and asymmetric");
70 TVM_ATTR_FIELD(rounding_method)
71 .set_default("round")
72 .describe(
73 "indicates how to find the \"nearest\" pixel in nearest_neighbor method"
74 "Available options are round, floor, and ceil.");
75 TVM_ATTR_FIELD(cubic_alpha)
76 .set_default(-0.5)
77 .describe("Spline Coefficient for cubic interpolation");
78 TVM_ATTR_FIELD(cubic_exclude)
79 .set_default(0)
80 .describe("Flag to exclude exterior of the image during cubic interpolation");
81 TVM_ATTR_FIELD(extrapolation_value)
82 .set_default(0.0)
83 .describe("Value to return when roi is outside of the image");
84 TVM_ATTR_FIELD(out_dtype).set_default(NullValue<DataType>()).describe("Output data type.");
85 }
86};
87
88/*! \brief Attributes used in image resize2d operator */
89struct Resize2DAttrs : public tvm::AttrsNode<Resize2DAttrs> {
90 Array<IndexExpr> size;
91 Array<FloatImm> roi;
92 std::string layout;
93 std::string method;
94 std::string coordinate_transformation_mode;
95 std::string rounding_method;
96 double cubic_alpha;
97 int cubic_exclude;
98 double extrapolation_value;
99 DataType out_dtype;
100
101 TVM_DECLARE_ATTRS(Resize2DAttrs, "relay.attrs.Resize2DAttrs") {
102 TVM_ATTR_FIELD(size).set_default(NullValue<Array<IndexExpr>>()).describe("Output Size.");
103 TVM_ATTR_FIELD(roi)
104 .set_default(NullValue<Array<FloatImm>>())
105 .describe("Region of Interest for coordinate transformation mode 'tf_crop_and_resize'");
106 TVM_ATTR_FIELD(layout).set_default("NCHW").describe(
107 "Dimension ordering of input data. Can be 'NCHW', 'NHWC', etc."
108 "'N', 'C', 'H', 'W' stands for batch, channel, height, and width"
109 "dimensions respectively. Resize is applied on the 'H' and"
110 "'W' dimensions.");
111 TVM_ATTR_FIELD(method).set_default("linear").describe(
112 "Specify the mode to use for scaling."
113 "nearest_neighbor - Nearest Neighbor"
114 "linear - Bilinear Interpolation"
115 "cubic - Bicubic Interpolation");
116 TVM_ATTR_FIELD(coordinate_transformation_mode)
117 .set_default("half_pixel")
118 .describe(
119 "Describes how to transform the coordinate in the resized tensor"
120 "to the coordinate in the original tensor."
121 "Refer to the ONNX Resize operator specification for details"
122 "Available options are half_pixel, align_corners and asymmetric");
123 TVM_ATTR_FIELD(rounding_method)
124 .set_default("round")
125 .describe(
126 "indicates how to find the \"nearest\" pixel in nearest_neighbor method"
127 "Available options are round, floor, and ceil.");
128 TVM_ATTR_FIELD(cubic_alpha)
129 .set_default(-0.5)
130 .describe("Spline Coefficient for Bicubic Interpolation");
131 TVM_ATTR_FIELD(cubic_exclude)
132 .set_default(0)
133 .describe("Flag to exclude exterior of the image during bicubic interpolation");
134 TVM_ATTR_FIELD(extrapolation_value)
135 .set_default(0.0)
136 .describe("Value to return when roi is outside of the image");
137 TVM_ATTR_FIELD(out_dtype).set_default(NullValue<DataType>()).describe("Output data type.");
138 }
139};
140
141/*! \brief Attributes used in image resize3d operator */
142struct Resize3DAttrs : public tvm::AttrsNode<Resize3DAttrs> {
143 Array<IndexExpr> size;
144 Array<FloatImm> roi;
145 std::string layout;
146 std::string method;
147 std::string coordinate_transformation_mode;
148 std::string rounding_method;
149 double cubic_alpha;
150 int cubic_exclude;
151 double extrapolation_value;
152 DataType out_dtype;
153
154 TVM_DECLARE_ATTRS(Resize3DAttrs, "relay.attrs.Resize3DAttrs") {
155 TVM_ATTR_FIELD(size).set_default(NullValue<Array<IndexExpr>>()).describe("Output Size.");
156 TVM_ATTR_FIELD(roi)
157 .set_default(NullValue<Array<FloatImm>>())
158 .describe("Region of Interest for coordinate transformation mode 'tf_crop_and_resize'");
159 TVM_ATTR_FIELD(layout).set_default("NCDHW").describe(
160 "Dimension ordering of input data. Can be 'NCDHW', 'NDHWC', etc."
161 "'N', 'C', 'D', 'H', 'W' stands for batch, channel, depth, height, and width"
162 "dimensions respectively. Resize3d is applied on the 'D', 'H' and"
163 "'W' dimensions.");
164 TVM_ATTR_FIELD(method).set_default("linear").describe(
165 "Specify the mode to use for scaling."
166 "nearest_neighbor - Nearest Neighbor"
167 "linear - Trilinear Interpolation"
168 "cubic - Tricubic Interpolation");
169 TVM_ATTR_FIELD(coordinate_transformation_mode)
170 .set_default("half_pixel")
171 .describe(
172 "Describes how to transform the coordinate in the resized tensor"
173 "to the coordinate in the original tensor."
174 "Refer to the ONNX Resize operator specification for details"
175 "Available options are half_pixel, align_corners and asymmetric");
176 TVM_ATTR_FIELD(rounding_method)
177 .set_default("round")
178 .describe(
179 "indicates how to find the \"nearest\" pixel in nearest_neighbor method"
180 "Available options are round, floor, and ceil.");
181 TVM_ATTR_FIELD(cubic_alpha)
182 .set_default(-0.5)
183 .describe("Spline Coefficient for Tricubic Interpolation");
184 TVM_ATTR_FIELD(cubic_exclude)
185 .set_default(0)
186 .describe("Flag to exclude exterior of the image during tricubic interpolation");
187 TVM_ATTR_FIELD(extrapolation_value)
188 .set_default(0.0)
189 .describe("Value to return when roi is outside of the image");
190 TVM_ATTR_FIELD(out_dtype).set_default(NullValue<DataType>()).describe("Output data type.");
191 }
192};
193
194/*! \brief Attributes used in image crop_and_resize operator */
195struct CropAndResizeAttrs : public tvm::AttrsNode<CropAndResizeAttrs> {
196 Array<IndexExpr> crop_size;
197 std::string layout;
198 std::string method;
199 double extrapolation_value;
200 DataType out_dtype;
201
202 TVM_DECLARE_ATTRS(CropAndResizeAttrs, "relay.attrs.CropAndResizeAttrs") {
203 TVM_ATTR_FIELD(crop_size).set_default(NullValue<Array<IndexExpr>>()).describe("Target Size.");
204 TVM_ATTR_FIELD(layout).set_default("NCHW").describe(
205 "Dimension ordering of input data. Can be 'NCHW', 'NHWC', etc."
206 "'N', 'C', 'H', 'W' stands for batch, channel, height, and width"
207 "dimensions respectively. Resize is applied on the 'H' and"
208 "'W' dimensions.");
209 TVM_ATTR_FIELD(method)
210 .set_default("bilinear")
211 .describe(
212 "Specify the mode to use for scaling."
213 "nearest_neighbor - Nearest Neighbor"
214 "bilinear - Bilinear Interpolation");
215 TVM_ATTR_FIELD(extrapolation_value)
216 .set_default(0.0)
217 .describe("Specify value for extrapolation.");
218 TVM_ATTR_FIELD(out_dtype).set_default(NullValue<DataType>()).describe("Output data type.");
219 }
220};
221
222/*! \brief Attributes used in dilation operators */
223struct Dilation2DAttrs : public tvm::AttrsNode<Dilation2DAttrs> {
224 Array<IndexExpr> strides;
225 Array<IndexExpr> padding;
226 Array<IndexExpr> dilations;
227 std::string data_layout;
228 std::string kernel_layout;
229 DataType out_dtype;
230
231 TVM_DECLARE_ATTRS(Dilation2DAttrs, "relay.attrs.Dilation2DAttrs") {
232 TVM_ATTR_FIELD(strides)
233 .set_default(Array<IndexExpr>({1, 1}))
234 .describe("Specifies the strides of the sliding window. [stride_height, stride_width].");
235 TVM_ATTR_FIELD(padding)
236 .set_default(Array<IndexExpr>({0, 0}))
237 .describe(
238 "If padding is non-zero, then the input is implicitly zero-padded"
239 "Padding support both symmetric and asymmetric as"
240 "one int : same padding used on all sides"
241 "two int : bottom, right will use same padding as top, left"
242 "four int : padding width in the order of (top, left, bottom, right)");
243 TVM_ATTR_FIELD(dilations)
244 .set_default(Array<IndexExpr>({1, 1}))
245 .describe("Specifies the dilation rate to use. [dilation_height, dilation_width]");
246 TVM_ATTR_FIELD(data_layout)
247 .set_default("NCHW")
248 .describe(
249 "Dimension ordering of input data. Can be 'NCHW', 'NHWC', etc."
250 "'N', 'C', 'H', 'W' stands for batch, channel, height, and width"
251 "dimensions respectively. Convolution is applied on the 'H' and"
252 "'W' dimensions.");
253 TVM_ATTR_FIELD(kernel_layout)
254 .set_default("IHW")
255 .describe(
256 "Dimension ordering of weight. Can be 'IHW', 'HWI', etc."
257 "'I', 'H', 'W' stands for input_channel, height, and width"
258 "dimensions respectively.");
259 TVM_ATTR_FIELD(out_dtype)
260 .set_default(NullValue<DataType>())
261 .describe("Output data type, set to explicit type under mixed precision setting");
262 }
263};
264
265/*! \brief Attributes used in image affine_grid operator */
266struct AffineGridAttrs : public tvm::AttrsNode<AffineGridAttrs> {
267 Array<IndexExpr> target_shape;
268
269 TVM_DECLARE_ATTRS(AffineGridAttrs, "relay.attrs.AffineGridAttrs") {
270 TVM_ATTR_FIELD(target_shape).describe("Specifies the output shape (H, W).");
271 }
272};
273
274/*! \brief Attributes used in image grid_sample operator */
275struct GridSampleAttrs : public tvm::AttrsNode<GridSampleAttrs> {
276 String method;
277 String layout;
278 String padding_mode;
279 bool align_corners;
280
281 TVM_DECLARE_ATTRS(GridSampleAttrs, "relay.attrs.GridSampleAttrs") {
282 TVM_ATTR_FIELD(method)
283 .set_default("bilinear")
284 .describe(
285 "Specify the mode to use for scaling."
286 "nearest - 2D or 3D Nearest Interpolation."
287 "bilinear - '2D Bilinear' or '3D Trilinear' Interpolation."
288 "bicubic - 2D Bicubic Interpolation.");
289 TVM_ATTR_FIELD(layout).set_default("NCHW").describe(
290 "Dimension ordering of input data. Can be 'NCHW', 'NCDHW', etc."
291 "'N', 'C', 'D', 'H', 'W' stands for batch, channel, depth, height, and width"
292 "dimensions respectively."
293 "2D Resize is applied on the 'H' and 'W' dimensions."
294 "3D Resize is applied on the 'D' and 'H' and 'W' dimensions.");
295 TVM_ATTR_FIELD(padding_mode)
296 .set_default("zeros")
297 .describe(
298 "If :attr:'grid' has values outside the range of '[-1, 1]', the corresponding"
299 "outputs are handled as defined by padding_mode. Options are"
300 "padding_mode='zeros': use '0' for out-of-bound grid locations,"
301 "padding_mode='border': use border values for out-of-bound grid locations"
302 "padding_mode='reflection': use values at locations reflected by"
303 "the border for out-of-bound grid locations. For location far away"
304 "from the border, it will keep being reflected until becoming in bound,"
305 "e.g., (normalized) pixel location 'x = -3.5' reflects by border '-1'"
306 "and becomes 'x' = 1.5, then reflects by border '1' and becomes"
307 "'x' = -0.5");
308 TVM_ATTR_FIELD(align_corners)
309 .set_default(true)
310 .describe(
311 "Geometrically, we consider the pixels of the"
312 "input as squares rather than points."
313 "If set to True, the extrema (-1 and 1) are considered as referring"
314 "to the center points of the input's corner pixels. If set to False, they"
315 "are instead considered as referring to the corner points of the input's corner"
316 "pixels, making the sampling more resolution agnostic.");
317 }
318};
319
320} // namespace relay
321} // namespace tvm
322#endif // TVM_RELAY_ATTRS_IMAGE_H_
323