|
| 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/nn.h |
| 22 | + * \brief Auxiliary attributes for nn operators. |
| 23 | + */ |
| 24 | +#ifndef TVM_RELAY_ATTRS_NN_QUANTIZE_H_ |
| 25 | +#define TVM_RELAY_ATTRS_NN_QUANTIZE_H_ |
| 26 | + |
| 27 | +#include <tvm/attrs.h> |
| 28 | +#include <string> |
| 29 | + |
| 30 | +namespace tvm { |
| 31 | +namespace relay { |
| 32 | + |
| 33 | +/*! \brief Attribute for quantized conv2d operator */ |
| 34 | +struct QConv2DAttrs : public tvm::AttrsNode<QConv2DAttrs> { |
| 35 | + // Traditional conv2d attributes. |
| 36 | + Array<IndexExpr> strides; |
| 37 | + Array<IndexExpr> padding; |
| 38 | + Array<IndexExpr> dilation; |
| 39 | + int groups; |
| 40 | + IndexExpr channels; |
| 41 | + Array<IndexExpr> kernel_size; |
| 42 | + std::string data_layout; |
| 43 | + std::string kernel_layout; |
| 44 | + std::string out_layout; |
| 45 | + DataType out_dtype; |
| 46 | + |
| 47 | + // Quantization related attributes. |
| 48 | + int32_t input_zero_point; |
| 49 | + int32_t kernel_zero_point; |
| 50 | + |
| 51 | + TVM_DECLARE_ATTRS(QConv2DAttrs, "relay.attrs.QConv2DAttrs") { |
| 52 | + TVM_ATTR_FIELD(strides).set_default(Array<IndexExpr>({1, 1})) |
| 53 | + .describe("Specifies the strides of the convolution."); |
| 54 | + TVM_ATTR_FIELD(padding).set_default(Array<IndexExpr>({0, 0})) |
| 55 | + .describe("If padding is non-zero, then the input is implicitly zero-padded" |
| 56 | + "on both sides for padding number of points"); |
| 57 | + TVM_ATTR_FIELD(dilation).set_default(Array<IndexExpr>({1, 1})) |
| 58 | + .describe("Specifies the dilation rate to use for dilated convolution."); |
| 59 | + TVM_ATTR_FIELD(groups).set_default(1) |
| 60 | + .describe("Controls the connections between inputs and outputs." |
| 61 | + "At groups=1, all inputs are convolved to all outputs." |
| 62 | + "At groups=2, the operation becomes equivalent to having two convolution" |
| 63 | + "layers side by side, each seeing half the input channels, and producing" |
| 64 | + "half the output channels, and both subsequently concatenated."); |
| 65 | + TVM_ATTR_FIELD(channels) |
| 66 | + .describe("The number of output channels in the convolution." |
| 67 | + " If it is not set, inferred by shape of the weight.") |
| 68 | + .set_default(NullValue<IndexExpr>()); |
| 69 | + TVM_ATTR_FIELD(kernel_size) |
| 70 | + .describe("Specifies the dimensions of the convolution window.") |
| 71 | + .set_default(NullValue<Array<IndexExpr> >()); |
| 72 | + TVM_ATTR_FIELD(data_layout).set_default("NCHW") |
| 73 | + .describe("Dimension ordering of input data. Can be 'NCHW', 'NHWC', etc." |
| 74 | + "'N', 'C', 'H', 'W' stands for batch, channel, height, and width" |
| 75 | + "dimensions respectively. Convolution is applied on the 'H' and" |
| 76 | + "'W' dimensions."); |
| 77 | + TVM_ATTR_FIELD(kernel_layout).set_default("OIHW") |
| 78 | + .describe("Dimension ordering of weight. Can be 'OIHW', 'OIHW16o16i', etc." |
| 79 | + "'O', 'I', 'H', 'W' stands for num_filter, input_channel, height, and width" |
| 80 | + "dimensions respectively."); |
| 81 | + TVM_ATTR_FIELD(out_layout).set_default("") |
| 82 | + .describe("Dimension ordering of output. Can be 'NCHW', 'NHWC', etc." |
| 83 | + "'N', 'C', 'H', 'W' stands for batch, channel, height, and width" |
| 84 | + "dimensions respectively. Default to be same as input layout."); |
| 85 | + TVM_ATTR_FIELD(out_dtype) |
| 86 | + .set_default(NullValue<DataType>()) |
| 87 | + .describe("Output data type, set to explicit type under mixed precision setting"); |
| 88 | + TVM_ATTR_FIELD(input_zero_point) |
| 89 | + .describe("The zero point of the input tensor."); |
| 90 | + TVM_ATTR_FIELD(kernel_zero_point) |
| 91 | + .describe("The zero point of the kernel tensor."); |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | + |
| 96 | +/*! \brief Attribute for requantize operator */ |
| 97 | +struct RequantizeAttrs : public tvm::AttrsNode<RequantizeAttrs> { |
| 98 | + double input_scale; |
| 99 | + int32_t input_zero_point; |
| 100 | + double output_scale; |
| 101 | + int32_t output_zero_point; |
| 102 | + bool use_int_compute; |
| 103 | + DataType out_dtype; |
| 104 | + |
| 105 | + TVM_DECLARE_ATTRS(RequantizeAttrs, "relay.attrs.RequantizeAttrs") { |
| 106 | + TVM_ATTR_FIELD(input_zero_point) |
| 107 | + .describe("The zero point of the input tensor."); |
| 108 | + TVM_ATTR_FIELD(output_zero_point) |
| 109 | + .describe("The zero point of the output tensor."); |
| 110 | + TVM_ATTR_FIELD(input_scale) |
| 111 | + .describe("The scale of the input tensor."); |
| 112 | + TVM_ATTR_FIELD(output_scale) |
| 113 | + .describe("The scale of the output tensor."); |
| 114 | + TVM_ATTR_FIELD(use_int_compute).set_default(false) |
| 115 | + .describe("When true, the integer computation is used to handle output scale"); |
| 116 | + TVM_ATTR_FIELD(out_dtype) |
| 117 | + .set_default(NullValue<DataType>()) |
| 118 | + .describe("Output data type, set to explicit type under mixed precision setting"); |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | + |
| 123 | +} // namespace relay |
| 124 | +} // namespace tvm |
| 125 | +#endif // TVM_RELAY_ATTRS_NN_QUANTIZE_H_ |
0 commit comments