1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_UTILS_H_ 17 #define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_UTILS_H_ 18 19 #include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h" 20 #include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" 21 22 namespace mlir { 23 namespace TFL { 24 namespace tac { 25 IsTFLDialectNonConstOp(Operation * op)26inline bool IsTFLDialectNonConstOp(Operation* op) { 27 if (op->getDialect() == nullptr) return false; 28 if (op->getDialect()->getNamespace() != "tfl") return false; 29 if (llvm::isa<TFL::ConstOp, TFL::QConstOp>(op)) return false; 30 return true; 31 } 32 33 bool IsTFLNonQuantDequantizeOp(Operation* op); 34 35 // Returns true if it is a shaped type of f32 elements. IsF32ShapedType(Type t)36inline bool IsF32ShapedType(Type t) { 37 if (auto shaped_type = t.dyn_cast_or_null<ShapedType>()) { 38 return shaped_type.getElementType().isF32(); 39 } 40 return false; 41 } 42 43 // Return true when the given element_type is QI8. IsQI8Type(Type t)44inline bool IsQI8Type(Type t) { 45 auto quantized_type = quant::QuantizedType::getQuantizedElementType(t); 46 return quantized_type != nullptr && 47 quantized_type.getStorageTypeIntegralWidth() == 8 && 48 quantized_type.isSigned(); 49 } 50 51 // Return true when the given element_type is QUI8. IsQUI8Type(Type t)52inline bool IsQUI8Type(Type t) { 53 auto quantized_type = quant::QuantizedType::getQuantizedElementType(t); 54 return quantized_type != nullptr && 55 quantized_type.getStorageTypeIntegralWidth() == 8 && 56 !quantized_type.isSigned(); 57 } 58 59 // Return true when the given element_type is QI32. IsQI32Type(Type t)60inline bool IsQI32Type(Type t) { 61 auto quantized_type = quant::QuantizedType::getQuantizedElementType(t); 62 return quantized_type != nullptr && 63 quantized_type.getStorageTypeIntegralWidth() == 32 && 64 quantized_type.isSigned(); 65 } 66 67 // Try to guess the inference type of the op. 68 InferenceType GetInferenceType(Operation* op); 69 70 } // namespace tac 71 } // namespace TFL 72 } // namespace mlir 73 74 #endif // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_UTILS_H_ 75