1 /* Copyright 2020 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 #ifndef TENSORFLOW_COMPILER_MLIR_LITE_UTILS_ARITHMETIC_COUNT_UTIL_H_ 16 #define TENSORFLOW_COMPILER_MLIR_LITE_UTILS_ARITHMETIC_COUNT_UTIL_H_ 17 18 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project 19 #include "mlir/IR/Operation.h" // from @llvm-project 20 21 namespace mlir { 22 namespace TFL { 23 24 // For add/mul/div/sub and other broadcastable ops. 25 class ArithmeticCountUtilHelper { 26 public: GetFirstOutputCount(mlir::Operation * op,int64_t * count)27 static bool GetFirstOutputCount(mlir::Operation* op, int64_t* count) { 28 auto output = op->getResult(0); 29 auto output_type = 30 output.getType().dyn_cast_or_null<mlir::RankedTensorType>(); 31 if (!output_type || !output_type.hasStaticShape()) return false; 32 33 *count = output_type.getNumElements(); 34 return true; 35 } 36 GetInputTensorTotalSize(mlir::Operation * op,int64_t * count)37 static bool GetInputTensorTotalSize(mlir::Operation* op, int64_t* count) { 38 int64_t total_count = 0; 39 for (auto input : op->getOperands()) { 40 auto input_type = 41 input.getType().dyn_cast_or_null<mlir::RankedTensorType>(); 42 if (!input_type || !input_type.hasStaticShape()) { 43 return false; 44 } 45 total_count += input_type.getNumElements(); 46 } 47 *count = total_count; 48 return true; 49 } 50 51 // For conv2d/depthwise_conv/fully_connected ops. 52 // This algorithm actually comes from TOCO tooling_util.cc GetArithmeticCountForConvAndFullyconnectedOp(mlir::Operation * op,int64_t * count)53 static bool GetArithmeticCountForConvAndFullyconnectedOp(mlir::Operation* op, 54 int64_t* count) { 55 auto weight = op->getOperand(1); 56 auto weight_type = 57 weight.getType().dyn_cast_or_null<mlir::RankedTensorType>(); 58 if (weight_type == nullptr || !weight_type.hasStaticShape()) return false; 59 60 auto output = op->getResult(0); 61 auto output_type = 62 output.getType().dyn_cast_or_null<mlir::RankedTensorType>(); 63 if (output_type == nullptr || !output_type.hasStaticShape()) return false; 64 65 int64_t cols = 1; 66 for (int i = 0; i < output_type.getRank() - 1; ++i) { 67 cols *= output_type.getDimSize(i); 68 } 69 const int64_t cost_per_col = 2 * weight_type.getNumElements(); 70 71 *count = cost_per_col * cols; 72 73 auto bias = op->getOperand(2); 74 if (bias) { 75 auto bias_type = 76 bias.getType().dyn_cast_or_null<mlir::RankedTensorType>(); 77 if (bias_type && bias_type.hasStaticShape()) { 78 *count += output_type.getNumElements(); 79 } 80 } 81 82 return true; 83 } 84 }; 85 86 } // namespace TFL 87 } // namespace mlir 88 89 #endif // TENSORFLOW_COMPILER_MLIR_LITE_UTILS_ARITHMETIC_COUNT_UTIL_H_ 90