1 /* Copyright 2018 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_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_ 16 #define TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_ 17 18 // These functions transform codes and data structures that are defined in the 19 // flatbuffer serialization format into in-memory values that are used by the 20 // runtime API and interpreter. 21 22 #include <cstddef> 23 #include <new> 24 #include <type_traits> 25 26 #include "tensorflow/lite/c/common.h" 27 #include "tensorflow/lite/core/api/error_reporter.h" 28 #include "tensorflow/lite/schema/schema_generated.h" 29 30 namespace tflite { 31 32 // Interface class for builtin data allocations. 33 class BuiltinDataAllocator { 34 public: 35 virtual void* Allocate(size_t size, size_t alignment_hint) = 0; 36 virtual void Deallocate(void* data) = 0; 37 38 // Allocate a structure, but make sure it is a POD structure that doesn't 39 // require constructors to run. The reason we do this, is that Interpreter's C 40 // extension part will take ownership so destructors will not be run during 41 // deallocation. 42 template <typename T> AllocatePOD()43 T* AllocatePOD() { 44 // TODO(b/154346074): Change this to is_trivially_destructible when all 45 // platform targets support that properly. 46 static_assert(std::is_pod<T>::value, "Builtin data structure must be POD."); 47 void* allocated_memory = this->Allocate(sizeof(T), alignof(T)); 48 return new (allocated_memory) T(); 49 } 50 ~BuiltinDataAllocator()51 virtual ~BuiltinDataAllocator() {} 52 }; 53 54 // Parse the appropriate data out of the op. 55 // 56 // This handles builtin data explicitly as there are flatbuffer schemas. 57 // If it returns kTfLiteOk, it passes the data out with `builtin_data`. The 58 // calling function has to pass in an allocator object, and this allocator 59 // will be called to reserve space for the output data. If the calling 60 // function's allocator reserves memory on the heap, then it's the calling 61 // function's responsibility to free it. 62 // If it returns kTfLiteError, `builtin_data` will be `nullptr`. 63 TfLiteStatus ParseOpData(const Operator* op, BuiltinOperator op_type, 64 ErrorReporter* error_reporter, 65 BuiltinDataAllocator* allocator, void** builtin_data); 66 67 // Converts the tensor data type used in the flat buffer to the representation 68 // used by the runtime. 69 TfLiteStatus ConvertTensorType(TensorType tensor_type, TfLiteType* type, 70 ErrorReporter* error_reporter); 71 72 TfLiteStatus ParseAbs(const Operator* op, ErrorReporter* error_reporter, 73 BuiltinDataAllocator* allocator, void** builtin_data); 74 75 TfLiteStatus ParseAdd(const Operator* op, ErrorReporter* error_reporter, 76 BuiltinDataAllocator* allocator, void** builtin_data); 77 78 TfLiteStatus ParseAddN(const Operator* op, ErrorReporter* error_reporter, 79 BuiltinDataAllocator* allocator, void** builtin_data); 80 81 TfLiteStatus ParseArgMax(const Operator* op, ErrorReporter* error_reporter, 82 BuiltinDataAllocator* allocator, void** builtin_data); 83 84 TfLiteStatus ParseArgMin(const Operator* op, ErrorReporter* error_reporter, 85 BuiltinDataAllocator* allocator, void** builtin_data); 86 87 TfLiteStatus ParseBatchMatMul(const Operator* op, ErrorReporter* error_reporter, 88 BuiltinDataAllocator* allocator, 89 void** builtin_data); 90 91 TfLiteStatus ParseBatchToSpaceNd(const Operator* op, 92 ErrorReporter* error_reporter, 93 BuiltinDataAllocator* allocator, 94 void** builtin_data); 95 96 TfLiteStatus ParseCeil(const Operator* op, ErrorReporter* error_reporter, 97 BuiltinDataAllocator* allocator, void** builtin_data); 98 99 TfLiteStatus ParseCast(const Operator* op, ErrorReporter* error_reporter, 100 BuiltinDataAllocator* allocator, void** builtin_data); 101 102 TfLiteStatus ParseConcatenation(const Operator* op, 103 ErrorReporter* error_reporter, 104 BuiltinDataAllocator* allocator, 105 void** builtin_data); 106 107 TfLiteStatus ParseConv2D(const Operator* op, ErrorReporter* error_reporter, 108 BuiltinDataAllocator* allocator, void** builtin_data); 109 110 TfLiteStatus ParseCos(const Operator* op, ErrorReporter* error_reporter, 111 BuiltinDataAllocator* allocator, void** builtin_data); 112 113 TfLiteStatus ParseDepthToSpace(const Operator* op, 114 ErrorReporter* error_reporter, 115 BuiltinDataAllocator* allocator, 116 void** builtin_data); 117 118 TfLiteStatus ParseDepthwiseConv2D(const Operator* op, 119 ErrorReporter* error_reporter, 120 BuiltinDataAllocator* allocator, 121 void** builtin_data); 122 123 TfLiteStatus ParseDequantize(const Operator* op, ErrorReporter* error_reporter, 124 BuiltinDataAllocator* allocator, 125 void** builtin_data); 126 127 TfLiteStatus ParseDiv(const Operator* op, ErrorReporter* error_reporter, 128 BuiltinDataAllocator* allocator, void** builtin_data); 129 130 TfLiteStatus ParseElu(const Operator* op, ErrorReporter* error_reporter, 131 BuiltinDataAllocator* allocator, void** builtin_data); 132 133 TfLiteStatus ParseEqual(const Operator* op, ErrorReporter* error_reporter, 134 BuiltinDataAllocator* allocator, void** builtin_data); 135 136 TfLiteStatus ParseExp(const Operator* op, ErrorReporter* error_reporter, 137 BuiltinDataAllocator* allocator, void** builtin_data); 138 139 TfLiteStatus ParseExpandDims(const Operator* op, ErrorReporter* error_reporter, 140 BuiltinDataAllocator* allocator, 141 void** builtin_data); 142 143 TfLiteStatus ParseFill(const Operator* op, ErrorReporter* error_reporter, 144 BuiltinDataAllocator* allocator, void** builtin_data); 145 146 TfLiteStatus ParseFloor(const Operator* op, ErrorReporter* error_reporter, 147 BuiltinDataAllocator* allocator, void** builtin_data); 148 149 TfLiteStatus ParseFloorDiv(const Operator* op, ErrorReporter* error_reporter, 150 BuiltinDataAllocator* allocator, 151 void** builtin_data); 152 153 TfLiteStatus ParseFloorMod(const Operator* op, ErrorReporter* error_reporter, 154 BuiltinDataAllocator* allocator, 155 void** builtin_data); 156 157 TfLiteStatus ParseFullyConnected(const Operator* op, 158 ErrorReporter* error_reporter, 159 BuiltinDataAllocator* allocator, 160 void** builtin_data); 161 162 TfLiteStatus ParseGather(const Operator* op, ErrorReporter* error_reporter, 163 BuiltinDataAllocator* allocator, void** builtin_data); 164 165 TfLiteStatus ParseGatherNd(const Operator* op, ErrorReporter* error_reporter, 166 BuiltinDataAllocator* allocator, 167 void** builtin_data); 168 169 TfLiteStatus ParseGreater(const Operator* op, ErrorReporter* error_reporter, 170 BuiltinDataAllocator* allocator, void** builtin_data); 171 172 TfLiteStatus ParseGreaterEqual(const Operator* op, 173 ErrorReporter* error_reporter, 174 BuiltinDataAllocator* allocator, 175 void** builtin_data); 176 177 TfLiteStatus ParseHardSwish(const Operator* op, ErrorReporter* error_reporter, 178 BuiltinDataAllocator* allocator, 179 void** builtin_data); 180 181 TfLiteStatus ParseL2Normalization(const Operator* op, 182 ErrorReporter* error_reporter, 183 BuiltinDataAllocator* allocator, 184 void** builtin_data); 185 186 TfLiteStatus ParseLeakyRelu(const Operator* op, ErrorReporter* error_reporter, 187 BuiltinDataAllocator* allocator, 188 void** builtin_data); 189 190 TfLiteStatus ParseLess(const Operator* op, ErrorReporter* error_reporter, 191 BuiltinDataAllocator* allocator, void** builtin_data); 192 193 TfLiteStatus ParseLessEqual(const Operator* op, ErrorReporter* error_reporter, 194 BuiltinDataAllocator* allocator, 195 void** builtin_data); 196 197 TfLiteStatus ParseLog(const Operator* op, ErrorReporter* error_reporter, 198 BuiltinDataAllocator* allocator, void** builtin_data); 199 200 TfLiteStatus ParseLogicalAnd(const Operator* op, ErrorReporter* error_reporter, 201 BuiltinDataAllocator* allocator, 202 void** builtin_data); 203 204 TfLiteStatus ParseLogicalNot(const Operator* op, ErrorReporter* error_reporter, 205 BuiltinDataAllocator* allocator, 206 void** builtin_data); 207 208 TfLiteStatus ParseLogicalOr(const Operator* op, ErrorReporter* error_reporter, 209 BuiltinDataAllocator* allocator, 210 void** builtin_data); 211 212 TfLiteStatus ParseLogistic(const Operator* op, ErrorReporter* error_reporter, 213 BuiltinDataAllocator* allocator, 214 void** builtin_data); 215 216 TfLiteStatus ParseMaximum(const Operator* op, ErrorReporter* error_reporter, 217 BuiltinDataAllocator* allocator, void** builtin_data); 218 219 TfLiteStatus ParseMinimum(const Operator* op, ErrorReporter* error_reporter, 220 BuiltinDataAllocator* allocator, void** builtin_data); 221 222 TfLiteStatus ParseMul(const Operator* op, ErrorReporter* error_reporter, 223 BuiltinDataAllocator* allocator, void** builtin_data); 224 225 TfLiteStatus ParseNeg(const Operator* op, ErrorReporter* error_reporter, 226 BuiltinDataAllocator* allocator, void** builtin_data); 227 228 TfLiteStatus ParseNotEqual(const Operator* op, ErrorReporter* error_reporter, 229 BuiltinDataAllocator* allocator, 230 void** builtin_data); 231 232 TfLiteStatus ParsePack(const Operator* op, ErrorReporter* error_reporter, 233 BuiltinDataAllocator* allocator, void** builtin_data); 234 235 TfLiteStatus ParsePad(const Operator* op, ErrorReporter* error_reporter, 236 BuiltinDataAllocator* allocator, void** builtin_data); 237 238 TfLiteStatus ParsePadV2(const Operator* op, ErrorReporter* error_reporter, 239 BuiltinDataAllocator* allocator, void** builtin_data); 240 241 TfLiteStatus ParsePool(const Operator* op, ErrorReporter* error_reporter, 242 BuiltinDataAllocator* allocator, void** builtin_data); 243 244 TfLiteStatus ParsePow(const Operator* op, ErrorReporter* error_reporter, 245 BuiltinDataAllocator* allocator, void** builtin_data); 246 247 TfLiteStatus ParsePrelu(const Operator* op, ErrorReporter* error_reporter, 248 BuiltinDataAllocator* allocator, void** builtin_data); 249 250 TfLiteStatus ParseQuantize(const Operator* op, ErrorReporter* error_reporter, 251 BuiltinDataAllocator* allocator, 252 void** builtin_data); 253 254 TfLiteStatus ParseReducer(const Operator* op, ErrorReporter* error_reporter, 255 BuiltinDataAllocator* allocator, void** builtin_data); 256 257 TfLiteStatus ParseRelu(const Operator* op, ErrorReporter* error_reporter, 258 BuiltinDataAllocator* allocator, void** builtin_data); 259 260 TfLiteStatus ParseRelu6(const Operator* op, ErrorReporter* error_reporter, 261 BuiltinDataAllocator* allocator, void** builtin_data); 262 263 TfLiteStatus ParseReshape(const Operator* op, ErrorReporter* error_reporter, 264 BuiltinDataAllocator* allocator, void** builtin_data); 265 266 TfLiteStatus ParseResizeBilinear(const Operator* op, 267 ErrorReporter* error_reporter, 268 BuiltinDataAllocator* allocator, 269 void** builtin_data); 270 271 TfLiteStatus ParseResizeNearestNeighbor(const Operator* op, 272 ErrorReporter* error_reporter, 273 BuiltinDataAllocator* allocator, 274 void** builtin_data); 275 276 TfLiteStatus ParseRound(const Operator* op, ErrorReporter* error_reporter, 277 BuiltinDataAllocator* allocator, void** builtin_data); 278 279 TfLiteStatus ParseRsqrt(const Operator* op, ErrorReporter* error_reporter, 280 BuiltinDataAllocator* allocator, void** builtin_data); 281 282 TfLiteStatus ParseShape(const Operator* op, ErrorReporter* error_reporter, 283 BuiltinDataAllocator* allocator, void** builtin_data); 284 285 TfLiteStatus ParseSin(const Operator* op, ErrorReporter* error_reporter, 286 BuiltinDataAllocator* allocator, void** builtin_data); 287 288 TfLiteStatus ParseSoftmax(const Operator* op, ErrorReporter* error_reporter, 289 BuiltinDataAllocator* allocator, void** builtin_data); 290 291 TfLiteStatus ParseSpaceToBatchNd(const Operator* op, 292 ErrorReporter* error_reporter, 293 BuiltinDataAllocator* allocator, 294 void** builtin_data); 295 296 TfLiteStatus ParseSpaceToDepth(const Operator* op, 297 ErrorReporter* error_reporter, 298 BuiltinDataAllocator* allocator, 299 void** builtin_data); 300 301 TfLiteStatus ParseSplit(const Operator* op, ErrorReporter* error_reporter, 302 BuiltinDataAllocator* allocator, void** builtin_data); 303 304 TfLiteStatus ParseSplitV(const Operator* op, ErrorReporter* error_reporter, 305 BuiltinDataAllocator* allocator, void** builtin_data); 306 307 TfLiteStatus ParseSqrt(const Operator* op, ErrorReporter* error_reporter, 308 BuiltinDataAllocator* allocator, void** builtin_data); 309 310 TfLiteStatus ParseSquare(const Operator* op, ErrorReporter* error_reporter, 311 BuiltinDataAllocator* allocator, void** builtin_data); 312 313 TfLiteStatus ParseStridedSlice(const Operator* op, 314 ErrorReporter* error_reporter, 315 BuiltinDataAllocator* allocator, 316 void** builtin_data); 317 318 TfLiteStatus ParseSub(const Operator* op, ErrorReporter* error_reporter, 319 BuiltinDataAllocator* allocator, void** builtin_data); 320 321 TfLiteStatus ParseSvdf(const Operator* op, ErrorReporter* error_reporter, 322 BuiltinDataAllocator* allocator, void** builtin_data); 323 324 TfLiteStatus ParseTanh(const Operator* op, ErrorReporter* error_reporter, 325 BuiltinDataAllocator* allocator, void** builtin_data); 326 327 TfLiteStatus ParseTranspose(const Operator* op, ErrorReporter* error_reporter, 328 BuiltinDataAllocator* allocator, 329 void** builtin_data); 330 331 TfLiteStatus ParseTransposeConv(const Operator* op, 332 ErrorReporter* error_reporter, 333 BuiltinDataAllocator* allocator, 334 void** builtin_data); 335 336 TfLiteStatus ParseUnpack(const Operator* op, ErrorReporter* error_reporter, 337 BuiltinDataAllocator* allocator, void** builtin_data); 338 339 TfLiteStatus ParseZerosLike(const Operator* op, ErrorReporter* error_reporter, 340 BuiltinDataAllocator* allocator, 341 void** builtin_data); 342 343 } // namespace tflite 344 345 #endif // TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_ 346