|
| 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 nnvm/compiler/quantize_util.h |
| 22 | + * \brief Utility methods needs for quantized ops that can be shared |
| 23 | + */ |
| 24 | + |
| 25 | +#ifndef TVM_QUANTIZE_UTIL_H |
| 26 | +#define TVM_QUANTIZE_UTIL_H |
| 27 | + |
| 28 | +#include <tvm/expr.h> |
| 29 | +#include "./base.h" |
| 30 | + |
| 31 | +namespace tvm { |
| 32 | +namespace relay { |
| 33 | + |
| 34 | +inline bool is_Int8(const DataType& dtype) { |
| 35 | + return dtype == Int(8); |
| 36 | +} |
| 37 | + |
| 38 | +inline bool is_UInt8(const DataType& dtype) { |
| 39 | + return dtype == UInt(8); |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +inline bool is_Int16(const DataType& dtype) { |
| 44 | + return dtype == Int(16); |
| 45 | +} |
| 46 | + |
| 47 | +inline bool is_UInt16(const DataType& dtype) { |
| 48 | + return dtype == UInt(16); |
| 49 | +} |
| 50 | + |
| 51 | +inline bool is_Int32(const DataType& dtype) { |
| 52 | + return dtype == Int(32); |
| 53 | +} |
| 54 | + |
| 55 | +inline bool is_UInt32(const DataType& dtype) { |
| 56 | + return dtype == UInt(32); |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +inline bool is_Float32(const DataType& dtype) { |
| 62 | + return dtype == Float(32); |
| 63 | +} |
| 64 | + |
| 65 | +inline bool is_quantized_type(const DataType& dtype) { |
| 66 | + return is_Int8(dtype) || is_UInt8(dtype) |
| 67 | + || is_Int16(dtype) || is_UInt16(dtype); |
| 68 | +} |
| 69 | + |
| 70 | +enum class QuantizeOpType : uint8_t { |
| 71 | + Quantize_Requantize, |
| 72 | + Dequantize |
| 73 | +}; |
| 74 | + |
| 75 | +inline bool is_valid_quantized_op_input_type(const QuantizeOpType &op_type, const DataType &in_dtype) { |
| 76 | + switch(op_type) { |
| 77 | + case QuantizeOpType::Quantize_Requantize: |
| 78 | + return is_Float32(in_dtype) || is_quantized_type(in_dtype); |
| 79 | + case QuantizeOpType ::Dequantize: |
| 80 | + return is_quantized_type(in_dtype); |
| 81 | + default: |
| 82 | + return false; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +inline bool is_valid_quantized_op_output_type(const QuantizeOpType &op_type, const DataType &in_dtype) { |
| 87 | + switch(op_type) { |
| 88 | + case QuantizeOpType::Quantize_Requantize: |
| 89 | + return is_quantized_type(in_dtype); |
| 90 | + case QuantizeOpType::Dequantize: |
| 91 | + return is_Float32(in_dtype); |
| 92 | + default: |
| 93 | + return false; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +inline const int32_t get_qmin(const DataType& dtype) { |
| 98 | + if (is_Int8(dtype)) { |
| 99 | + return std::numeric_limits<int8_t>::min(); |
| 100 | + } else if (is_UInt8(dtype)) { |
| 101 | + return std::numeric_limits<uint8_t>::min(); |
| 102 | + } else if (is_Int16(dtype)) { |
| 103 | + return std::numeric_limits<int16_t>::min(); |
| 104 | + } else if (is_UInt16(dtype)) { |
| 105 | + return std::numeric_limits<uint16_t>::min(); |
| 106 | + } else if (is_Int32(dtype)) { |
| 107 | + return std::numeric_limits<int32_t>::min(); |
| 108 | + } else if (is_UInt32(dtype)) { |
| 109 | + return std::numeric_limits<uint32_t>::min(); |
| 110 | + } |
| 111 | + LOG(FATAL) << "Type not supported\n"; |
| 112 | + return -1; |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +inline const int32_t get_qmax(const DataType& dtype) { |
| 117 | + if (is_Int8(dtype)) { |
| 118 | + return std::numeric_limits<int8_t>::max(); |
| 119 | + } else if (is_UInt8(dtype)) { |
| 120 | + return std::numeric_limits<uint8_t>::max(); |
| 121 | + } else if (is_Int16(dtype)) { |
| 122 | + return std::numeric_limits<int16_t>::max(); |
| 123 | + } else if (is_UInt16(dtype)) { |
| 124 | + return std::numeric_limits<uint16_t>::max(); |
| 125 | + } else if (is_Int32(dtype)) { |
| 126 | + return std::numeric_limits<int32_t>::max(); |
| 127 | + } else if (is_UInt32(dtype)) { |
| 128 | + return std::numeric_limits<uint32_t>::max(); |
| 129 | + } |
| 130 | + LOG(FATAL) << "Type not supported\n"; |
| 131 | + return -1; |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace relay |
| 135 | +} // namespace tvm |
| 136 | +#endif //TVM_QUANTIZE_UTIL_H |
0 commit comments