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
40// Attributes used for encoding sparse tensors.
41// Please find detailed explanation of these parameters in the TFLite schema.
42def TFL_DT_Dense : StrEnumAttrCase<"DENSE", 0>;
43def TFL_DT_SparseCSR : StrEnumAttrCase<"SPARSE_CSR", 1>;
44
45def TFL_DimensionTypeAttr : StrEnumAttr<
46    "DimensionType", "dimension type", [TFL_DT_Dense, TFL_DT_SparseCSR]>;
47
48//===----------------------------------------------------------------------===//
49// TFL op interface for stateful operands.
50
51def TFL_StatefulOp : OpInterface<"StatefulOpInterface"> {
52  let description = [{
53    Interface for ops that are stateful and need to identify stateful operands.
54
55    Stateful operands correspond to TF's variables semantics. An op that has 1
56    or more stateful operands is a stateful op.
57  }];
58
59  let methods = [
60    InterfaceMethod<
61      [{Returns the indices of stateful operands.}],
62      "std::vector<int>", "GetStatefulOperands", (ins)
63    >,
64  ];
65}
66
67//===----------------------------------------------------------------------===//
68// TFL op interface for sparse operands.
69
70def TFL_SparseOp : OpInterface<"SparseOpInterface"> {
71  let description = [{
72    Interface for ops that support sparse computation.
73  }];
74
75  let methods = [
76    InterfaceMethod<
77      [{Returns the indices of sparse operands.}],
78      "std::vector<int>", "GetSparseOperands", (ins)
79    >,
80    InterfaceMethod<
81      [{Returns the supported block size of float sparse operands.}],
82      "std::vector<std::vector<int>>", "GetFloatBlockSize", (ins)
83    >,
84    InterfaceMethod<
85      [{Returns the supported block size of quantized sparse operands.}],
86      "std::vector<std::vector<int>>", "GetQuantizedBlockSize", (ins)
87    >,
88  ];
89}
90
91//===----------------------------------------------------------------------===//
92// TFL runtime type verification of operand/result types.
93
94def TFL_RuntimeVerification : OpInterface<"TflRuntimeVerifyOpInterface"> {
95  let description = [{
96    Interface to verify TFLite runtime op verification.
97
98    This verifies that the converted TFLite ops has operand/result type
99    supported by the TFLite runtime.
100  }];
101
102  let methods = [
103    StaticInterfaceMethod<
104      [{Returns whether the op's operands/results are supported by runtime.}],
105      "LogicalResult", "VerifyTflRuntimeConstraints", (ins "Operation*":$op)
106    >,
107  ];
108}
109
110#endif // TFL_OP_INTERFACES
111