1/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16// This is the operation interface definition file for TensorFlow Lite. 17 18#ifndef TFL_OP_INTERFACES 19#define TFL_OP_INTERFACES 20 21include "mlir/IR/OpBase.td" 22 23def TFL_Dialect : Dialect { 24 let name = "tfl"; 25 26 let description = [{ 27 The TensorFlow Lite dialect. 28 29 This dialect maps to TensorFlow Lite operations. 30 31 Invariants: 32 33 * All values are of Tensor type (in particular, scalars are 34 represented using zero-dimensional tensors); 35 }]; 36 37 let cppNamespace = "::mlir::TFL"; 38 39 let emitAccessorPrefix = kEmitAccessorPrefix_Raw; 40 41 let useDefaultAttributePrinterParser = 1; 42 let useDefaultTypePrinterParser = 1; 43 44 let extraClassDeclaration = [{ 45 ParseResult parseOneResultSameOperandTypeOp(OpAsmParser &parser, 46 OperationState &result); 47 48 void printOneResultOp(Operation *op, OpAsmPrinter &p); 49 50 // Registered hook to materialize a constant operation from a given 51 // attribute value with the desired resultant type. 52 Operation *materializeConstant(OpBuilder &builder, Attribute value, 53 Type type, Location loc) override; 54 }]; 55} 56 57//===----------------------------------------------------------------------===// 58// TFL op interface for stateful operands. 59 60def TFL_StatefulOp : OpInterface<"StatefulOpInterface"> { 61 let description = [{ 62 Interface for ops that are stateful and need to identify stateful operands. 63 64 Stateful operands correspond to TF's variables semantics. An op that has 1 65 or more stateful operands is a stateful op. 66 }]; 67 68 let methods = [ 69 InterfaceMethod< 70 [{Returns the indices of stateful operands.}], 71 "std::vector<int>", "GetStatefulOperands", (ins) 72 >, 73 ]; 74} 75 76//===----------------------------------------------------------------------===// 77// TFL op interface for sparse operands. 78 79def TFL_SparseOp : OpInterface<"SparseOpInterface"> { 80 let description = [{ 81 Interface for ops that support sparse computation. 82 }]; 83 84 let methods = [ 85 InterfaceMethod< 86 [{Returns the indices of sparse operands.}], 87 "std::vector<int>", "GetSparseOperands", (ins) 88 >, 89 InterfaceMethod< 90 [{Returns the supported block size of float sparse operands.}], 91 "std::vector<std::vector<int>>", "GetFloatBlockSize", (ins) 92 >, 93 InterfaceMethod< 94 [{Returns the supported block size of quantized sparse operands.}], 95 "std::vector<std::vector<int>>", "GetQuantizedBlockSize", (ins) 96 >, 97 ]; 98} 99 100//===----------------------------------------------------------------------===// 101// TFL runtime type verification of operand/result types. 102 103def TFL_RuntimeVerification : OpInterface<"TflRuntimeVerifyOpInterface"> { 104 let description = [{ 105 Interface to verify TFLite runtime op verification. 106 107 This verifies that the converted TFLite ops has operand/result type 108 supported by the TFLite runtime. 109 }]; 110 111 let methods = [ 112 StaticInterfaceMethod< 113 [{Returns whether the op's operands/results are supported by runtime.}], 114 "LogicalResult", "VerifyTflRuntimeConstraints", 115 (ins "Operation*":$op, "bool":$emit_error_on_verify_fail) 116 >, 117 ]; 118} 119 120//===----------------------------------------------------------------------===// 121// TFL arithmetic count interface. 122 123def TFL_ArithmeticCount : OpInterface<"TflArithmeticCountOpInterface"> { 124 let description = [{ 125 Interface for TFLite ops to calculate arithmetic count (Multiply-Add Count). 126 }]; 127 128 let methods = [ 129 StaticInterfaceMethod< 130 [{Returns an integer representing the op's arithmetic count (Multiply-Add 131 Count), return -1 if the arithmetic count cannot be determined.}], 132 "int64_t", "GetArithmeticCount", (ins "Operation*":$op) 133 >, 134 ]; 135} 136#endif // TFL_OP_INTERFACES 137