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 ParseCumsum(const Operator* op, ErrorReporter* error_reporter, 114 BuiltinDataAllocator* allocator, void** builtin_data); 115 116 TfLiteStatus ParseDepthToSpace(const Operator* op, 117 ErrorReporter* error_reporter, 118 BuiltinDataAllocator* allocator, 119 void** builtin_data); 120 121 TfLiteStatus ParseDepthwiseConv2D(const Operator* op, 122 ErrorReporter* error_reporter, 123 BuiltinDataAllocator* allocator, 124 void** builtin_data); 125 126 TfLiteStatus ParseDequantize(const Operator* op, ErrorReporter* error_reporter, 127 BuiltinDataAllocator* allocator, 128 void** builtin_data); 129 130 TfLiteStatus ParseDiv(const Operator* op, ErrorReporter* error_reporter, 131 BuiltinDataAllocator* allocator, void** builtin_data); 132 133 TfLiteStatus ParseElu(const Operator* op, ErrorReporter* error_reporter, 134 BuiltinDataAllocator* allocator, void** builtin_data); 135 136 TfLiteStatus ParseEqual(const Operator* op, ErrorReporter* error_reporter, 137 BuiltinDataAllocator* allocator, void** builtin_data); 138 139 TfLiteStatus ParseExp(const Operator* op, ErrorReporter* error_reporter, 140 BuiltinDataAllocator* allocator, void** builtin_data); 141 142 TfLiteStatus ParseExpandDims(const Operator* op, ErrorReporter* error_reporter, 143 BuiltinDataAllocator* allocator, 144 void** builtin_data); 145 146 TfLiteStatus ParseFill(const Operator* op, ErrorReporter* error_reporter, 147 BuiltinDataAllocator* allocator, void** builtin_data); 148 149 TfLiteStatus ParseFloor(const Operator* op, ErrorReporter* error_reporter, 150 BuiltinDataAllocator* allocator, void** builtin_data); 151 152 TfLiteStatus ParseFloorDiv(const Operator* op, ErrorReporter* error_reporter, 153 BuiltinDataAllocator* allocator, 154 void** builtin_data); 155 156 TfLiteStatus ParseFloorMod(const Operator* op, ErrorReporter* error_reporter, 157 BuiltinDataAllocator* allocator, 158 void** builtin_data); 159 160 TfLiteStatus ParseFullyConnected(const Operator* op, 161 ErrorReporter* error_reporter, 162 BuiltinDataAllocator* allocator, 163 void** builtin_data); 164 165 TfLiteStatus ParseGather(const Operator* op, ErrorReporter* error_reporter, 166 BuiltinDataAllocator* allocator, void** builtin_data); 167 168 TfLiteStatus ParseGatherNd(const Operator* op, ErrorReporter* error_reporter, 169 BuiltinDataAllocator* allocator, 170 void** builtin_data); 171 172 TfLiteStatus ParseGreater(const Operator* op, ErrorReporter* error_reporter, 173 BuiltinDataAllocator* allocator, void** builtin_data); 174 175 TfLiteStatus ParseGreaterEqual(const Operator* op, 176 ErrorReporter* error_reporter, 177 BuiltinDataAllocator* allocator, 178 void** builtin_data); 179 180 TfLiteStatus ParseHardSwish(const Operator* op, ErrorReporter* error_reporter, 181 BuiltinDataAllocator* allocator, 182 void** builtin_data); 183 184 TfLiteStatus ParseIf(const Operator* op, ErrorReporter* error_reporter, 185 BuiltinDataAllocator* allocator, void** builtin_data); 186 187 TfLiteStatus ParseL2Normalization(const Operator* op, 188 ErrorReporter* error_reporter, 189 BuiltinDataAllocator* allocator, 190 void** builtin_data); 191 192 TfLiteStatus ParseLeakyRelu(const Operator* op, ErrorReporter* error_reporter, 193 BuiltinDataAllocator* allocator, 194 void** builtin_data); 195 196 TfLiteStatus ParseLess(const Operator* op, ErrorReporter* error_reporter, 197 BuiltinDataAllocator* allocator, void** builtin_data); 198 199 TfLiteStatus ParseLessEqual(const Operator* op, ErrorReporter* error_reporter, 200 BuiltinDataAllocator* allocator, 201 void** builtin_data); 202 203 TfLiteStatus ParseLog(const Operator* op, ErrorReporter* error_reporter, 204 BuiltinDataAllocator* allocator, void** builtin_data); 205 206 TfLiteStatus ParseLogicalAnd(const Operator* op, ErrorReporter* error_reporter, 207 BuiltinDataAllocator* allocator, 208 void** builtin_data); 209 210 TfLiteStatus ParseLogicalNot(const Operator* op, ErrorReporter* error_reporter, 211 BuiltinDataAllocator* allocator, 212 void** builtin_data); 213 214 TfLiteStatus ParseLogicalOr(const Operator* op, ErrorReporter* error_reporter, 215 BuiltinDataAllocator* allocator, 216 void** builtin_data); 217 218 TfLiteStatus ParseLogistic(const Operator* op, ErrorReporter* error_reporter, 219 BuiltinDataAllocator* allocator, 220 void** builtin_data); 221 222 TfLiteStatus ParseLogSoftmax(const Operator* op, ErrorReporter* error_reporter, 223 BuiltinDataAllocator* allocator, 224 void** builtin_data); 225 226 TfLiteStatus ParseMaximum(const Operator* op, ErrorReporter* error_reporter, 227 BuiltinDataAllocator* allocator, void** builtin_data); 228 229 TfLiteStatus ParseMinimum(const Operator* op, ErrorReporter* error_reporter, 230 BuiltinDataAllocator* allocator, void** builtin_data); 231 232 TfLiteStatus ParseMul(const Operator* op, ErrorReporter* error_reporter, 233 BuiltinDataAllocator* allocator, void** builtin_data); 234 235 TfLiteStatus ParseNeg(const Operator* op, ErrorReporter* error_reporter, 236 BuiltinDataAllocator* allocator, void** builtin_data); 237 238 TfLiteStatus ParseNotEqual(const Operator* op, ErrorReporter* error_reporter, 239 BuiltinDataAllocator* allocator, 240 void** builtin_data); 241 242 TfLiteStatus ParsePack(const Operator* op, ErrorReporter* error_reporter, 243 BuiltinDataAllocator* allocator, void** builtin_data); 244 245 TfLiteStatus ParsePad(const Operator* op, ErrorReporter* error_reporter, 246 BuiltinDataAllocator* allocator, void** builtin_data); 247 248 TfLiteStatus ParsePadV2(const Operator* op, ErrorReporter* error_reporter, 249 BuiltinDataAllocator* allocator, void** builtin_data); 250 251 TfLiteStatus ParsePool(const Operator* op, ErrorReporter* error_reporter, 252 BuiltinDataAllocator* allocator, void** builtin_data); 253 254 TfLiteStatus ParsePow(const Operator* op, ErrorReporter* error_reporter, 255 BuiltinDataAllocator* allocator, void** builtin_data); 256 257 TfLiteStatus ParsePrelu(const Operator* op, ErrorReporter* error_reporter, 258 BuiltinDataAllocator* allocator, void** builtin_data); 259 260 TfLiteStatus ParseQuantize(const Operator* op, ErrorReporter* error_reporter, 261 BuiltinDataAllocator* allocator, 262 void** builtin_data); 263 264 TfLiteStatus ParseReducer(const Operator* op, ErrorReporter* error_reporter, 265 BuiltinDataAllocator* allocator, void** builtin_data); 266 267 TfLiteStatus ParseRelu(const Operator* op, ErrorReporter* error_reporter, 268 BuiltinDataAllocator* allocator, void** builtin_data); 269 270 TfLiteStatus ParseRelu6(const Operator* op, ErrorReporter* error_reporter, 271 BuiltinDataAllocator* allocator, void** builtin_data); 272 273 TfLiteStatus ParseReshape(const Operator* op, ErrorReporter* error_reporter, 274 BuiltinDataAllocator* allocator, void** builtin_data); 275 276 TfLiteStatus ParseResizeBilinear(const Operator* op, 277 ErrorReporter* error_reporter, 278 BuiltinDataAllocator* allocator, 279 void** builtin_data); 280 281 TfLiteStatus ParseResizeNearestNeighbor(const Operator* op, 282 ErrorReporter* error_reporter, 283 BuiltinDataAllocator* allocator, 284 void** builtin_data); 285 286 TfLiteStatus ParseRound(const Operator* op, ErrorReporter* error_reporter, 287 BuiltinDataAllocator* allocator, void** builtin_data); 288 289 TfLiteStatus ParseRsqrt(const Operator* op, ErrorReporter* error_reporter, 290 BuiltinDataAllocator* allocator, void** builtin_data); 291 292 TfLiteStatus ParseShape(const Operator* op, ErrorReporter* error_reporter, 293 BuiltinDataAllocator* allocator, void** builtin_data); 294 295 TfLiteStatus ParseSin(const Operator* op, ErrorReporter* error_reporter, 296 BuiltinDataAllocator* allocator, void** builtin_data); 297 298 TfLiteStatus ParseSoftmax(const Operator* op, ErrorReporter* error_reporter, 299 BuiltinDataAllocator* allocator, void** builtin_data); 300 301 TfLiteStatus ParseSpaceToBatchNd(const Operator* op, 302 ErrorReporter* error_reporter, 303 BuiltinDataAllocator* allocator, 304 void** builtin_data); 305 306 TfLiteStatus ParseSpaceToDepth(const Operator* op, 307 ErrorReporter* error_reporter, 308 BuiltinDataAllocator* allocator, 309 void** builtin_data); 310 311 TfLiteStatus ParseSplit(const Operator* op, ErrorReporter* error_reporter, 312 BuiltinDataAllocator* allocator, void** builtin_data); 313 314 TfLiteStatus ParseSplitV(const Operator* op, ErrorReporter* error_reporter, 315 BuiltinDataAllocator* allocator, void** builtin_data); 316 317 TfLiteStatus ParseSqueeze(const Operator* op, ErrorReporter* error_reporter, 318 BuiltinDataAllocator* allocator, void** builtin_data); 319 320 TfLiteStatus ParseSqrt(const Operator* op, ErrorReporter* error_reporter, 321 BuiltinDataAllocator* allocator, void** builtin_data); 322 323 TfLiteStatus ParseSquare(const Operator* op, ErrorReporter* error_reporter, 324 BuiltinDataAllocator* allocator, void** builtin_data); 325 326 TfLiteStatus ParseStridedSlice(const Operator* op, 327 ErrorReporter* error_reporter, 328 BuiltinDataAllocator* allocator, 329 void** builtin_data); 330 331 TfLiteStatus ParseSub(const Operator* op, ErrorReporter* error_reporter, 332 BuiltinDataAllocator* allocator, void** builtin_data); 333 334 TfLiteStatus ParseSvdf(const Operator* op, ErrorReporter* error_reporter, 335 BuiltinDataAllocator* allocator, void** builtin_data); 336 337 TfLiteStatus ParseTanh(const Operator* op, ErrorReporter* error_reporter, 338 BuiltinDataAllocator* allocator, void** builtin_data); 339 340 TfLiteStatus ParseTranspose(const Operator* op, ErrorReporter* error_reporter, 341 BuiltinDataAllocator* allocator, 342 void** builtin_data); 343 344 TfLiteStatus ParseTransposeConv(const Operator* op, 345 ErrorReporter* error_reporter, 346 BuiltinDataAllocator* allocator, 347 void** builtin_data); 348 349 TfLiteStatus ParseUnpack(const Operator* op, ErrorReporter* error_reporter, 350 BuiltinDataAllocator* allocator, void** builtin_data); 351 352 TfLiteStatus ParseZerosLike(const Operator* op, ErrorReporter* error_reporter, 353 BuiltinDataAllocator* allocator, 354 void** builtin_data); 355 356 } // namespace tflite 357 358 #endif // TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_ 359