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 #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 ParseAssignVariable(const Operator* op, 88 ErrorReporter* error_reporter, 89 BuiltinDataAllocator* allocator, 90 void** builtin_data); 91 92 TfLiteStatus ParseBatchMatMul(const Operator* op, ErrorReporter* error_reporter, 93 BuiltinDataAllocator* allocator, 94 void** builtin_data); 95 96 TfLiteStatus ParseBatchToSpaceNd(const Operator* op, 97 ErrorReporter* error_reporter, 98 BuiltinDataAllocator* allocator, 99 void** builtin_data); 100 101 TfLiteStatus ParseBroadcastArgs(const Operator* op, 102 ErrorReporter* error_reporter, 103 BuiltinDataAllocator* allocator, 104 void** builtin_data); 105 106 TfLiteStatus ParseBroadcastTo(const Operator* op, ErrorReporter* error_reporter, 107 BuiltinDataAllocator* allocator, 108 void** builtin_data); 109 110 TfLiteStatus ParseCallOnce(const Operator* op, ErrorReporter* error_reporter, 111 BuiltinDataAllocator* allocator, 112 void** builtin_data); 113 114 TfLiteStatus ParseCeil(const Operator* op, ErrorReporter* error_reporter, 115 BuiltinDataAllocator* allocator, void** builtin_data); 116 117 TfLiteStatus ParseCast(const Operator* op, ErrorReporter* error_reporter, 118 BuiltinDataAllocator* allocator, void** builtin_data); 119 120 TfLiteStatus ParseConcatenation(const Operator* op, 121 ErrorReporter* error_reporter, 122 BuiltinDataAllocator* allocator, 123 void** builtin_data); 124 125 TfLiteStatus ParseConv2D(const Operator* op, ErrorReporter* error_reporter, 126 BuiltinDataAllocator* allocator, void** builtin_data); 127 128 TfLiteStatus ParseCos(const Operator* op, ErrorReporter* error_reporter, 129 BuiltinDataAllocator* allocator, void** builtin_data); 130 131 TfLiteStatus ParseCumsum(const Operator* op, ErrorReporter* error_reporter, 132 BuiltinDataAllocator* allocator, void** builtin_data); 133 134 TfLiteStatus ParseDepthToSpace(const Operator* op, 135 ErrorReporter* error_reporter, 136 BuiltinDataAllocator* allocator, 137 void** builtin_data); 138 139 TfLiteStatus ParseDepthwiseConv2D(const Operator* op, 140 ErrorReporter* error_reporter, 141 BuiltinDataAllocator* allocator, 142 void** builtin_data); 143 144 TfLiteStatus ParseDequantize(const Operator* op, ErrorReporter* error_reporter, 145 BuiltinDataAllocator* allocator, 146 void** builtin_data); 147 148 TfLiteStatus ParseDiv(const Operator* op, ErrorReporter* error_reporter, 149 BuiltinDataAllocator* allocator, void** builtin_data); 150 151 TfLiteStatus ParseElu(const Operator* op, ErrorReporter* error_reporter, 152 BuiltinDataAllocator* allocator, void** builtin_data); 153 154 TfLiteStatus ParseEqual(const Operator* op, ErrorReporter* error_reporter, 155 BuiltinDataAllocator* allocator, void** builtin_data); 156 157 TfLiteStatus ParseExp(const Operator* op, ErrorReporter* error_reporter, 158 BuiltinDataAllocator* allocator, void** builtin_data); 159 160 TfLiteStatus ParseExpandDims(const Operator* op, ErrorReporter* error_reporter, 161 BuiltinDataAllocator* allocator, 162 void** builtin_data); 163 164 TfLiteStatus ParseFill(const Operator* op, ErrorReporter* error_reporter, 165 BuiltinDataAllocator* allocator, void** builtin_data); 166 167 TfLiteStatus ParseFloor(const Operator* op, ErrorReporter* error_reporter, 168 BuiltinDataAllocator* allocator, void** builtin_data); 169 170 TfLiteStatus ParseFloorDiv(const Operator* op, ErrorReporter* error_reporter, 171 BuiltinDataAllocator* allocator, 172 void** builtin_data); 173 174 TfLiteStatus ParseFloorMod(const Operator* op, ErrorReporter* error_reporter, 175 BuiltinDataAllocator* allocator, 176 void** builtin_data); 177 178 TfLiteStatus ParseFullyConnected(const Operator* op, 179 ErrorReporter* error_reporter, 180 BuiltinDataAllocator* allocator, 181 void** builtin_data); 182 183 TfLiteStatus ParseGather(const Operator* op, ErrorReporter* error_reporter, 184 BuiltinDataAllocator* allocator, void** builtin_data); 185 186 TfLiteStatus ParseGatherNd(const Operator* op, ErrorReporter* error_reporter, 187 BuiltinDataAllocator* allocator, 188 void** builtin_data); 189 190 TfLiteStatus ParseGreater(const Operator* op, ErrorReporter* error_reporter, 191 BuiltinDataAllocator* allocator, void** builtin_data); 192 193 TfLiteStatus ParseGreaterEqual(const Operator* op, 194 ErrorReporter* error_reporter, 195 BuiltinDataAllocator* allocator, 196 void** builtin_data); 197 198 TfLiteStatus ParseHardSwish(const Operator* op, ErrorReporter* error_reporter, 199 BuiltinDataAllocator* allocator, 200 void** builtin_data); 201 202 TfLiteStatus ParseIf(const Operator* op, ErrorReporter* error_reporter, 203 BuiltinDataAllocator* allocator, void** builtin_data); 204 205 TfLiteStatus ParseL2Normalization(const Operator* op, 206 ErrorReporter* error_reporter, 207 BuiltinDataAllocator* allocator, 208 void** builtin_data); 209 210 TfLiteStatus ParseLeakyRelu(const Operator* op, ErrorReporter* error_reporter, 211 BuiltinDataAllocator* allocator, 212 void** builtin_data); 213 214 TfLiteStatus ParseLess(const Operator* op, ErrorReporter* error_reporter, 215 BuiltinDataAllocator* allocator, void** builtin_data); 216 217 TfLiteStatus ParseLessEqual(const Operator* op, ErrorReporter* error_reporter, 218 BuiltinDataAllocator* allocator, 219 void** builtin_data); 220 221 TfLiteStatus ParseLog(const Operator* op, ErrorReporter* error_reporter, 222 BuiltinDataAllocator* allocator, void** builtin_data); 223 224 TfLiteStatus ParseLogicalAnd(const Operator* op, ErrorReporter* error_reporter, 225 BuiltinDataAllocator* allocator, 226 void** builtin_data); 227 228 TfLiteStatus ParseLogicalNot(const Operator* op, ErrorReporter* error_reporter, 229 BuiltinDataAllocator* allocator, 230 void** builtin_data); 231 232 TfLiteStatus ParseLogicalOr(const Operator* op, ErrorReporter* error_reporter, 233 BuiltinDataAllocator* allocator, 234 void** builtin_data); 235 236 TfLiteStatus ParseLogistic(const Operator* op, ErrorReporter* error_reporter, 237 BuiltinDataAllocator* allocator, 238 void** builtin_data); 239 240 TfLiteStatus ParseLogSoftmax(const Operator* op, ErrorReporter* error_reporter, 241 BuiltinDataAllocator* allocator, 242 void** builtin_data); 243 244 TfLiteStatus ParseLSTM(const Operator* op, ErrorReporter* error_reporter, 245 BuiltinDataAllocator* allocator, void** builtin_data); 246 247 TfLiteStatus ParseMaximum(const Operator* op, ErrorReporter* error_reporter, 248 BuiltinDataAllocator* allocator, void** builtin_data); 249 250 TfLiteStatus ParseMinimum(const Operator* op, ErrorReporter* error_reporter, 251 BuiltinDataAllocator* allocator, void** builtin_data); 252 253 TfLiteStatus ParseMirrorPad(const Operator* op, ErrorReporter* error_reporter, 254 BuiltinDataAllocator* allocator, 255 void** builtin_data); 256 257 TfLiteStatus ParseMul(const Operator* op, ErrorReporter* error_reporter, 258 BuiltinDataAllocator* allocator, void** builtin_data); 259 260 TfLiteStatus ParseNeg(const Operator* op, ErrorReporter* error_reporter, 261 BuiltinDataAllocator* allocator, void** builtin_data); 262 263 TfLiteStatus ParseNotEqual(const Operator* op, ErrorReporter* error_reporter, 264 BuiltinDataAllocator* allocator, 265 void** builtin_data); 266 267 TfLiteStatus ParsePack(const Operator* op, ErrorReporter* error_reporter, 268 BuiltinDataAllocator* allocator, void** builtin_data); 269 270 TfLiteStatus ParsePad(const Operator* op, ErrorReporter* error_reporter, 271 BuiltinDataAllocator* allocator, void** builtin_data); 272 273 TfLiteStatus ParsePadV2(const Operator* op, ErrorReporter* error_reporter, 274 BuiltinDataAllocator* allocator, void** builtin_data); 275 276 TfLiteStatus ParsePool(const Operator* op, ErrorReporter* error_reporter, 277 BuiltinDataAllocator* allocator, void** builtin_data); 278 279 TfLiteStatus ParsePow(const Operator* op, ErrorReporter* error_reporter, 280 BuiltinDataAllocator* allocator, void** builtin_data); 281 282 TfLiteStatus ParsePrelu(const Operator* op, ErrorReporter* error_reporter, 283 BuiltinDataAllocator* allocator, void** builtin_data); 284 285 TfLiteStatus ParseQuantize(const Operator* op, ErrorReporter* error_reporter, 286 BuiltinDataAllocator* allocator, 287 void** builtin_data); 288 289 TfLiteStatus ParseReadVariable(const Operator* op, 290 ErrorReporter* error_reporter, 291 BuiltinDataAllocator* allocator, 292 void** builtin_data); 293 294 TfLiteStatus ParseReducer(const Operator* op, ErrorReporter* error_reporter, 295 BuiltinDataAllocator* allocator, void** builtin_data); 296 297 TfLiteStatus ParseRelu(const Operator* op, ErrorReporter* error_reporter, 298 BuiltinDataAllocator* allocator, void** builtin_data); 299 300 TfLiteStatus ParseRelu6(const Operator* op, ErrorReporter* error_reporter, 301 BuiltinDataAllocator* allocator, void** builtin_data); 302 303 TfLiteStatus ParseReshape(const Operator* op, ErrorReporter* error_reporter, 304 BuiltinDataAllocator* allocator, void** builtin_data); 305 306 TfLiteStatus ParseResizeBilinear(const Operator* op, 307 ErrorReporter* error_reporter, 308 BuiltinDataAllocator* allocator, 309 void** builtin_data); 310 311 TfLiteStatus ParseResizeNearestNeighbor(const Operator* op, 312 ErrorReporter* error_reporter, 313 BuiltinDataAllocator* allocator, 314 void** builtin_data); 315 316 TfLiteStatus ParseRound(const Operator* op, ErrorReporter* error_reporter, 317 BuiltinDataAllocator* allocator, void** builtin_data); 318 319 TfLiteStatus ParseRsqrt(const Operator* op, ErrorReporter* error_reporter, 320 BuiltinDataAllocator* allocator, void** builtin_data); 321 322 TfLiteStatus ParseSelectV2(const Operator* op, ErrorReporter* error_reporter, 323 BuiltinDataAllocator* allocator, 324 void** builtin_data); 325 326 TfLiteStatus ParseShape(const Operator* op, ErrorReporter* error_reporter, 327 BuiltinDataAllocator* allocator, void** builtin_data); 328 329 TfLiteStatus ParseSin(const Operator* op, ErrorReporter* error_reporter, 330 BuiltinDataAllocator* allocator, void** builtin_data); 331 332 TfLiteStatus ParseSlice(const Operator* op, ErrorReporter* error_reporter, 333 BuiltinDataAllocator* allocator, void** builtin_data); 334 335 TfLiteStatus ParseSoftmax(const Operator* op, ErrorReporter* error_reporter, 336 BuiltinDataAllocator* allocator, void** builtin_data); 337 338 TfLiteStatus ParseSpaceToBatchNd(const Operator* op, 339 ErrorReporter* error_reporter, 340 BuiltinDataAllocator* allocator, 341 void** builtin_data); 342 343 TfLiteStatus ParseSpaceToDepth(const Operator* op, 344 ErrorReporter* error_reporter, 345 BuiltinDataAllocator* allocator, 346 void** builtin_data); 347 348 TfLiteStatus ParseSplit(const Operator* op, ErrorReporter* error_reporter, 349 BuiltinDataAllocator* allocator, void** builtin_data); 350 351 TfLiteStatus ParseSplitV(const Operator* op, ErrorReporter* error_reporter, 352 BuiltinDataAllocator* allocator, void** builtin_data); 353 354 TfLiteStatus ParseSqueeze(const Operator* op, ErrorReporter* error_reporter, 355 BuiltinDataAllocator* allocator, void** builtin_data); 356 357 TfLiteStatus ParseSqrt(const Operator* op, ErrorReporter* error_reporter, 358 BuiltinDataAllocator* allocator, void** builtin_data); 359 360 TfLiteStatus ParseSquare(const Operator* op, ErrorReporter* error_reporter, 361 BuiltinDataAllocator* allocator, void** builtin_data); 362 363 TfLiteStatus ParseSquaredDifference(const Operator* op, 364 ErrorReporter* error_reporter, 365 BuiltinDataAllocator* allocator, 366 void** builtin_data); 367 368 TfLiteStatus ParseStridedSlice(const Operator* op, 369 ErrorReporter* error_reporter, 370 BuiltinDataAllocator* allocator, 371 void** builtin_data); 372 373 TfLiteStatus ParseSub(const Operator* op, ErrorReporter* error_reporter, 374 BuiltinDataAllocator* allocator, void** builtin_data); 375 376 TfLiteStatus ParseSvdf(const Operator* op, ErrorReporter* error_reporter, 377 BuiltinDataAllocator* allocator, void** builtin_data); 378 379 TfLiteStatus ParseTanh(const Operator* op, ErrorReporter* error_reporter, 380 BuiltinDataAllocator* allocator, void** builtin_data); 381 382 TfLiteStatus ParseTranspose(const Operator* op, ErrorReporter* error_reporter, 383 BuiltinDataAllocator* allocator, 384 void** builtin_data); 385 386 TfLiteStatus ParseTransposeConv(const Operator* op, 387 ErrorReporter* error_reporter, 388 BuiltinDataAllocator* allocator, 389 void** builtin_data); 390 391 TfLiteStatus ParseUnpack(const Operator* op, ErrorReporter* error_reporter, 392 BuiltinDataAllocator* allocator, void** builtin_data); 393 394 TfLiteStatus ParseUnidirectionalSequenceLSTM(const Operator* op, 395 ErrorReporter* error_reporter, 396 BuiltinDataAllocator* allocator, 397 void** builtin_data); 398 399 TfLiteStatus ParseVarHandle(const Operator* op, ErrorReporter* error_reporter, 400 BuiltinDataAllocator* allocator, 401 void** builtin_data); 402 403 TfLiteStatus ParseWhile(const Operator* op, ErrorReporter* error_reporter, 404 BuiltinDataAllocator* allocator, void** builtin_data); 405 406 TfLiteStatus ParseZerosLike(const Operator* op, ErrorReporter* error_reporter, 407 BuiltinDataAllocator* allocator, 408 void** builtin_data); 409 410 } // namespace tflite 411 412 #endif // TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_ 413