1 /* Copyright 2019 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 16 // This file defines common C types and APIs for implementing operations, 17 // delegates and other constructs in TensorFlow Lite. The actual operations and 18 // delegates can be defined using C++, but the interface between the interpreter 19 // and the operations are C. 20 // 21 // Summary of abstractions 22 // TF_LITE_ENSURE - Self-sufficient error checking 23 // TfLiteStatus - Status reporting 24 // TfLiteIntArray - stores tensor shapes (dims), 25 // TfLiteContext - allows an op to access the tensors 26 // TfLiteTensor - tensor (a multidimensional array) 27 // TfLiteNode - a single node or operation 28 // TfLiteRegistration - the implementation of a conceptual operation. 29 // TfLiteDelegate - allows delegation of nodes to alternative backends. 30 // 31 // Some abstractions in this file are created and managed by Interpreter. 32 // 33 // NOTE: The order of values in these structs are "semi-ABI stable". New values 34 // should be added only to the end of structs and never reordered. 35 36 #ifndef TENSORFLOW_LITE_C_COMMON_H_ 37 #define TENSORFLOW_LITE_C_COMMON_H_ 38 39 #include <stdbool.h> 40 #include <stddef.h> 41 #include <stdint.h> 42 43 #include "tensorflow/lite/c/c_api_types.h" // IWYU pragma: export 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif // __cplusplus 48 49 // The list of external context types known to TF Lite. This list exists solely 50 // to avoid conflicts and to ensure ops can share the external contexts they 51 // need. Access to the external contexts is controlled by one of the 52 // corresponding support files. 53 typedef enum TfLiteExternalContextType { 54 kTfLiteEigenContext = 0, // include eigen_support.h to use. 55 kTfLiteGemmLowpContext = 1, // include gemm_support.h to use. 56 kTfLiteEdgeTpuContext = 2, // Placeholder for Edge TPU support. 57 kTfLiteCpuBackendContext = 3, // include cpu_backend_context.h to use. 58 kTfLiteMaxExternalContexts = 4 59 } TfLiteExternalContextType; 60 61 // Forward declare so dependent structs and methods can reference these types 62 // prior to the struct definitions. 63 struct TfLiteContext; 64 struct TfLiteDelegate; 65 struct TfLiteRegistration; 66 67 // An external context is a collection of information unrelated to the TF Lite 68 // framework, but useful to a subset of the ops. TF Lite knows very little 69 // about the actual contexts, but it keeps a list of them, and is able to 70 // refresh them if configurations like the number of recommended threads 71 // change. 72 typedef struct TfLiteExternalContext { 73 TfLiteExternalContextType type; 74 TfLiteStatus (*Refresh)(struct TfLiteContext* context); 75 } TfLiteExternalContext; 76 77 #define kTfLiteOptionalTensor (-1) 78 79 // Fixed size list of integers. Used for dimensions and inputs/outputs tensor 80 // indices 81 typedef struct TfLiteIntArray { 82 int size; 83 // gcc 6.1+ have a bug where flexible members aren't properly handled 84 // https://github.com/google/re2/commit/b94b7cd42e9f02673cd748c1ac1d16db4052514c 85 #if (!defined(__clang__) && defined(__GNUC__) && __GNUC__ == 6 && \ 86 __GNUC_MINOR__ >= 1) || \ 87 defined(HEXAGON) || (__clang_major__ == 7 && __clang_minor__ == 1) 88 int data[0]; 89 #else 90 int data[]; 91 #endif 92 } TfLiteIntArray; 93 94 // Given the size (number of elements) in a TfLiteIntArray, calculate its size 95 // in bytes. 96 int TfLiteIntArrayGetSizeInBytes(int size); 97 98 #ifndef TF_LITE_STATIC_MEMORY 99 // Create a array of a given `size` (uninitialized entries). 100 // This returns a pointer, that you must free using TfLiteIntArrayFree(). 101 TfLiteIntArray* TfLiteIntArrayCreate(int size); 102 #endif 103 104 // Check if two intarrays are equal. Returns 1 if they are equal, 0 otherwise. 105 int TfLiteIntArrayEqual(const TfLiteIntArray* a, const TfLiteIntArray* b); 106 107 // Check if an intarray equals an array. Returns 1 if equals, 0 otherwise. 108 int TfLiteIntArrayEqualsArray(const TfLiteIntArray* a, int b_size, 109 const int b_data[]); 110 111 #ifndef TF_LITE_STATIC_MEMORY 112 // Create a copy of an array passed as `src`. 113 // You are expected to free memory with TfLiteIntArrayFree 114 TfLiteIntArray* TfLiteIntArrayCopy(const TfLiteIntArray* src); 115 116 // Free memory of array `a`. 117 void TfLiteIntArrayFree(TfLiteIntArray* a); 118 #endif // TF_LITE_STATIC_MEMORY 119 120 // Fixed size list of floats. Used for per-channel quantization. 121 typedef struct TfLiteFloatArray { 122 int size; 123 // gcc 6.1+ have a bug where flexible members aren't properly handled 124 // https://github.com/google/re2/commit/b94b7cd42e9f02673cd748c1ac1d16db4052514c 125 // This also applies to the toolchain used for Qualcomm Hexagon DSPs. 126 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ == 6 && \ 127 __GNUC_MINOR__ >= 1 128 float data[0]; 129 #else 130 float data[]; 131 #endif 132 } TfLiteFloatArray; 133 134 // Given the size (number of elements) in a TfLiteFloatArray, calculate its size 135 // in bytes. 136 int TfLiteFloatArrayGetSizeInBytes(int size); 137 138 #ifndef TF_LITE_STATIC_MEMORY 139 // Create a array of a given `size` (uninitialized entries). 140 // This returns a pointer, that you must free using TfLiteFloatArrayFree(). 141 TfLiteFloatArray* TfLiteFloatArrayCreate(int size); 142 143 // Free memory of array `a`. 144 void TfLiteFloatArrayFree(TfLiteFloatArray* a); 145 #endif // TF_LITE_STATIC_MEMORY 146 147 // Since we must not depend on any libraries, define a minimal subset of 148 // error macros while avoiding names that have pre-conceived meanings like 149 // assert and check. 150 151 // Try to make all reporting calls through TF_LITE_KERNEL_LOG rather than 152 // calling the context->ReportError function directly, so that message strings 153 // can be stripped out if the binary size needs to be severely optimized. 154 #ifndef TF_LITE_STRIP_ERROR_STRINGS 155 #define TF_LITE_KERNEL_LOG(context, ...) \ 156 do { \ 157 (context)->ReportError((context), __VA_ARGS__); \ 158 } while (false) 159 160 #define TF_LITE_MAYBE_KERNEL_LOG(context, ...) \ 161 do { \ 162 if ((context) != nullptr) { \ 163 (context)->ReportError((context), __VA_ARGS__); \ 164 } \ 165 } while (false) 166 #else // TF_LITE_STRIP_ERROR_STRINGS 167 #define TF_LITE_KERNEL_LOG(context, ...) 168 #define TF_LITE_MAYBE_KERNEL_LOG(context, ...) 169 #endif // TF_LITE_STRIP_ERROR_STRINGS 170 171 // Check whether value is true, and if not return kTfLiteError from 172 // the current function (and report the error string msg). 173 #define TF_LITE_ENSURE_MSG(context, value, msg) \ 174 do { \ 175 if (!(value)) { \ 176 TF_LITE_KERNEL_LOG((context), __FILE__ " " msg); \ 177 return kTfLiteError; \ 178 } \ 179 } while (0) 180 181 // Check whether the value `a` is true, and if not return kTfLiteError from 182 // the current function, while also reporting the location of the error. 183 #define TF_LITE_ENSURE(context, a) \ 184 do { \ 185 if (!(a)) { \ 186 TF_LITE_KERNEL_LOG((context), "%s:%d %s was not true.", __FILE__, \ 187 __LINE__, #a); \ 188 return kTfLiteError; \ 189 } \ 190 } while (0) 191 192 #define TF_LITE_ENSURE_STATUS(a) \ 193 do { \ 194 const TfLiteStatus s = (a); \ 195 if (s != kTfLiteOk) { \ 196 return s; \ 197 } \ 198 } while (0) 199 200 // Check whether the value `a == b` is true, and if not return kTfLiteError from 201 // the current function, while also reporting the location of the error. 202 // `a` and `b` may be evaluated more than once, so no side effects or 203 // extremely expensive computations should be done. 204 // NOTE: Use TF_LITE_ENSURE_TYPES_EQ if comparing TfLiteTypes. 205 #define TF_LITE_ENSURE_EQ(context, a, b) \ 206 do { \ 207 if ((a) != (b)) { \ 208 TF_LITE_KERNEL_LOG((context), "%s:%d %s != %s (%d != %d)", __FILE__, \ 209 __LINE__, #a, #b, (a), (b)); \ 210 return kTfLiteError; \ 211 } \ 212 } while (0) 213 214 #define TF_LITE_ENSURE_TYPES_EQ(context, a, b) \ 215 do { \ 216 if ((a) != (b)) { \ 217 TF_LITE_KERNEL_LOG((context), "%s:%d %s != %s (%s != %s)", __FILE__, \ 218 __LINE__, #a, #b, TfLiteTypeGetName(a), \ 219 TfLiteTypeGetName(b)); \ 220 return kTfLiteError; \ 221 } \ 222 } while (0) 223 224 #define TF_LITE_ENSURE_NEAR(context, a, b, epsilon) \ 225 do { \ 226 auto delta = ((a) > (b)) ? ((a) - (b)) : ((b) - (a)); \ 227 if (delta > epsilon) { \ 228 TF_LITE_KERNEL_LOG((context), "%s:%d %s not near %s (%f != %f)", \ 229 __FILE__, __LINE__, #a, #b, static_cast<double>(a), \ 230 static_cast<double>(b)); \ 231 return kTfLiteError; \ 232 } \ 233 } while (0) 234 235 #define TF_LITE_ENSURE_OK(context, status) \ 236 do { \ 237 const TfLiteStatus s = (status); \ 238 if ((s) != kTfLiteOk) { \ 239 return s; \ 240 } \ 241 } while (0) 242 243 // Single-precision complex data type compatible with the C99 definition. 244 typedef struct TfLiteComplex64 { 245 float re, im; // real and imaginary parts, respectively. 246 } TfLiteComplex64; 247 248 // Double-precision complex data type compatible with the C99 definition. 249 typedef struct TfLiteComplex128 { 250 double re, im; // real and imaginary parts, respectively. 251 } TfLiteComplex128; 252 253 // Half precision data type compatible with the C99 definition. 254 typedef struct TfLiteFloat16 { 255 uint16_t data; 256 } TfLiteFloat16; 257 258 // Return the name of a given type, for error reporting purposes. 259 const char* TfLiteTypeGetName(TfLiteType type); 260 261 // SupportedQuantizationTypes. 262 typedef enum TfLiteQuantizationType { 263 // No quantization. 264 kTfLiteNoQuantization = 0, 265 // Affine quantization (with support for per-channel quantization). 266 // Corresponds to TfLiteAffineQuantization. 267 kTfLiteAffineQuantization = 1, 268 } TfLiteQuantizationType; 269 270 // Structure specifying the quantization used by the tensor, if-any. 271 typedef struct TfLiteQuantization { 272 // The type of quantization held by params. 273 TfLiteQuantizationType type; 274 // Holds an optional reference to a quantization param structure. The actual 275 // type depends on the value of the `type` field (see the comment there for 276 // the values and corresponding types). 277 void* params; 278 } TfLiteQuantization; 279 280 // Parameters for asymmetric quantization across a dimension (i.e per output 281 // channel quantization). 282 // quantized_dimension specifies which dimension the scales and zero_points 283 // correspond to. 284 // For a particular value in quantized_dimension, quantized values can be 285 // converted back to float using: 286 // real_value = scale * (quantized_value - zero_point) 287 typedef struct TfLiteAffineQuantization { 288 TfLiteFloatArray* scale; 289 TfLiteIntArray* zero_point; 290 int32_t quantized_dimension; 291 } TfLiteAffineQuantization; 292 293 /* A union of pointers that points to memory for a given tensor. */ 294 typedef union TfLitePtrUnion { 295 /* Do not access these members directly, if possible, use 296 * GetTensorData<TYPE>(tensor) instead, otherwise only access .data, as other 297 * members are deprecated. */ 298 int32_t* i32; 299 uint32_t* u32; 300 int64_t* i64; 301 uint64_t* u64; 302 float* f; 303 TfLiteFloat16* f16; 304 double* f64; 305 char* raw; 306 const char* raw_const; 307 uint8_t* uint8; 308 bool* b; 309 int16_t* i16; 310 TfLiteComplex64* c64; 311 TfLiteComplex128* c128; 312 int8_t* int8; 313 /* Only use this member. */ 314 void* data; 315 } TfLitePtrUnion; 316 317 // Memory allocation strategies. 318 // * kTfLiteMmapRo: Read-only memory-mapped data, or data externally allocated. 319 // * kTfLiteArenaRw: Arena allocated with no guarantees about persistence, 320 // and available during eval. 321 // * kTfLiteArenaRwPersistent: Arena allocated but persistent across eval, and 322 // only available during eval. 323 // * kTfLiteDynamic: Allocated during eval, or for string tensors. 324 // * kTfLitePersistentRo: Allocated and populated during prepare. This is 325 // useful for tensors that can be computed during prepare and treated 326 // as constant inputs for downstream ops (also in prepare). 327 // * kTfLiteCustom: Custom memory allocation provided by the user. See 328 // TfLiteCustomAllocation below. 329 typedef enum TfLiteAllocationType { 330 kTfLiteMemNone = 0, 331 kTfLiteMmapRo, 332 kTfLiteArenaRw, 333 kTfLiteArenaRwPersistent, 334 kTfLiteDynamic, 335 kTfLitePersistentRo, 336 kTfLiteCustom, 337 } TfLiteAllocationType; 338 339 // The delegates should use zero or positive integers to represent handles. 340 // -1 is reserved from unallocated status. 341 typedef int TfLiteBufferHandle; 342 enum { 343 kTfLiteNullBufferHandle = -1, 344 }; 345 346 // Storage format of each dimension in a sparse tensor. 347 typedef enum TfLiteDimensionType { 348 kTfLiteDimDense = 0, 349 kTfLiteDimSparseCSR, 350 } TfLiteDimensionType; 351 352 // Metadata to encode each dimension in a sparse tensor. 353 typedef struct TfLiteDimensionMetadata { 354 TfLiteDimensionType format; 355 int dense_size; 356 TfLiteIntArray* array_segments; 357 TfLiteIntArray* array_indices; 358 } TfLiteDimensionMetadata; 359 360 // Parameters used to encode a sparse tensor. For detailed explanation of each 361 // field please refer to lite/schema/schema.fbs. 362 typedef struct TfLiteSparsity { 363 TfLiteIntArray* traversal_order; 364 TfLiteIntArray* block_map; 365 TfLiteDimensionMetadata* dim_metadata; 366 int dim_metadata_size; 367 } TfLiteSparsity; 368 369 // Defines a custom memory allocation not owned by the runtime. 370 // `data` should be aligned to kDefaultTensorAlignment defined in 371 // lite/util.h. (Currently 64 bytes) 372 // NOTE: See Interpreter.SetCustomAllocationForTensor for details on usage. 373 typedef struct TfLiteCustomAllocation { 374 void* data; 375 size_t bytes; 376 } TfLiteCustomAllocation; 377 378 // A tensor in the interpreter system which is a wrapper around a buffer of 379 // data including a dimensionality (or NULL if not currently defined). 380 #ifndef TF_LITE_STATIC_MEMORY 381 typedef struct TfLiteTensor { 382 // The data type specification for data stored in `data`. This affects 383 // what member of `data` union should be used. 384 TfLiteType type; 385 // A union of data pointers. The appropriate type should be used for a typed 386 // tensor based on `type`. 387 TfLitePtrUnion data; 388 // A pointer to a structure representing the dimensionality interpretation 389 // that the buffer should have. NOTE: the product of elements of `dims` 390 // and the element datatype size should be equal to `bytes` below. 391 TfLiteIntArray* dims; 392 // Quantization information. 393 TfLiteQuantizationParams params; 394 // How memory is mapped 395 // kTfLiteMmapRo: Memory mapped read only. 396 // i.e. weights 397 // kTfLiteArenaRw: Arena allocated read write memory 398 // (i.e. temporaries, outputs). 399 TfLiteAllocationType allocation_type; 400 // The number of bytes required to store the data of this Tensor. I.e. 401 // (bytes of each element) * dims[0] * ... * dims[n-1]. For example, if 402 // type is kTfLiteFloat32 and dims = {3, 2} then 403 // bytes = sizeof(float) * 3 * 2 = 4 * 3 * 2 = 24. 404 size_t bytes; 405 406 // An opaque pointer to a tflite::MMapAllocation 407 const void* allocation; 408 409 // Null-terminated name of this tensor. 410 const char* name; 411 412 // The delegate which knows how to handle `buffer_handle`. 413 // WARNING: This is an experimental interface that is subject to change. 414 struct TfLiteDelegate* delegate; 415 416 // An integer buffer handle that can be handled by `delegate`. 417 // The value is valid only when delegate is not null. 418 // WARNING: This is an experimental interface that is subject to change. 419 TfLiteBufferHandle buffer_handle; 420 421 // If the delegate uses its own buffer (e.g. GPU memory), the delegate is 422 // responsible to set data_is_stale to true. 423 // `delegate->CopyFromBufferHandle` can be called to copy the data from 424 // delegate buffer. 425 // WARNING: This is an // experimental interface that is subject to change. 426 bool data_is_stale; 427 428 // True if the tensor is a variable. 429 bool is_variable; 430 431 // Quantization information. Replaces params field above. 432 TfLiteQuantization quantization; 433 434 // Parameters used to encode a sparse tensor. 435 // This is optional. The field is NULL if a tensor is dense. 436 // WARNING: This is an experimental interface that is subject to change. 437 TfLiteSparsity* sparsity; 438 439 // Optional. Encodes shapes with unknown dimensions with -1. This field is 440 // only populated when unknown dimensions exist in a read-write tensor (i.e. 441 // an input or output tensor). (e.g. `dims` contains [1, 1, 1, 3] and 442 // `dims_signature` contains [1, -1, -1, 3]). 443 const TfLiteIntArray* dims_signature; 444 } TfLiteTensor; 445 446 // A structure representing an instance of a node. 447 // This structure only exhibits the inputs, outputs and user defined data, not 448 // other features like the type. 449 typedef struct TfLiteNode { 450 // Inputs to this node expressed as indices into the simulator's tensors. 451 TfLiteIntArray* inputs; 452 453 // Outputs to this node expressed as indices into the simulator's tensors. 454 TfLiteIntArray* outputs; 455 456 // intermediate tensors to this node expressed as indices into the simulator's 457 // tensors. 458 TfLiteIntArray* intermediates; 459 460 // Temporary tensors uses during the computations. This usually contains no 461 // tensors, but ops are allowed to change that if they need scratch space of 462 // any sort. 463 TfLiteIntArray* temporaries; 464 465 // Opaque data provided by the node implementer through `Registration.init`. 466 void* user_data; 467 468 // Opaque data provided to the node if the node is a builtin. This is usually 469 // a structure defined in builtin_op_data.h 470 void* builtin_data; 471 472 // Custom initial data. This is the opaque data provided in the flatbuffer. 473 // WARNING: This is an experimental interface that is subject to change. 474 const void* custom_initial_data; 475 int custom_initial_data_size; 476 477 // The pointer to the delegate. This is non-null only when the node is 478 // created by calling `interpreter.ModifyGraphWithDelegate`. 479 // WARNING: This is an experimental interface that is subject to change. 480 struct TfLiteDelegate* delegate; 481 } TfLiteNode; 482 #else // defined(TF_LITE_STATIC_MEMORY)? 483 // NOTE: This flag is opt-in only at compile time. 484 // 485 // Specific reduced TfLiteTensor struct for TF Micro runtime. This struct 486 // contains only the minimum fields required to initialize and prepare a micro 487 // inference graph. The fields in this struct have been ordered from 488 // largest-to-smallest for optimal struct sizeof. 489 // 490 // This struct does not use: 491 // - allocation 492 // - buffer_handle 493 // - data_is_stale 494 // - delegate 495 // - dims_signature 496 // - name 497 // - sparsity 498 typedef struct TfLiteTensor { 499 // TODO(b/155784997): Consider consolidating these quantization fields: 500 // Quantization information. Replaces params field above. 501 TfLiteQuantization quantization; 502 503 // Quantization information. 504 TfLiteQuantizationParams params; 505 506 // A union of data pointers. The appropriate type should be used for a typed 507 // tensor based on `type`. 508 TfLitePtrUnion data; 509 510 // A pointer to a structure representing the dimensionality interpretation 511 // that the buffer should have. NOTE: the product of elements of `dims` 512 // and the element datatype size should be equal to `bytes` below. 513 TfLiteIntArray* dims; 514 515 // The number of bytes required to store the data of this Tensor. I.e. 516 // (bytes of each element) * dims[0] * ... * dims[n-1]. For example, if 517 // type is kTfLiteFloat32 and dims = {3, 2} then 518 // bytes = sizeof(float) * 3 * 2 = 4 * 3 * 2 = 24. 519 size_t bytes; 520 521 // The data type specification for data stored in `data`. This affects 522 // what member of `data` union should be used. 523 TfLiteType type; 524 525 // How memory is mapped 526 // kTfLiteMmapRo: Memory mapped read only. 527 // i.e. weights 528 // kTfLiteArenaRw: Arena allocated read write memory 529 // (i.e. temporaries, outputs). 530 TfLiteAllocationType allocation_type; 531 532 // True if the tensor is a variable. 533 bool is_variable; 534 } TfLiteTensor; 535 536 // Specific reduced TfLiteNode struct for TF Micro runtime. This struct contains 537 // only the minimum fields required to represent a node. 538 // 539 // This struct does not use: 540 // - delegate 541 // - intermediates 542 // - temporaries 543 typedef struct TfLiteNode { 544 // Inputs to this node expressed as indices into the simulator's tensors. 545 TfLiteIntArray* inputs; 546 547 // Outputs to this node expressed as indices into the simulator's tensors. 548 TfLiteIntArray* outputs; 549 550 // Opaque data provided by the node implementer through `Registration.init`. 551 void* user_data; 552 553 // Opaque data provided to the node if the node is a builtin. This is usually 554 // a structure defined in builtin_op_data.h 555 void* builtin_data; 556 557 // Custom initial data. This is the opaque data provided in the flatbuffer. 558 // WARNING: This is an experimental interface that is subject to change. 559 const void* custom_initial_data; 560 int custom_initial_data_size; 561 } TfLiteNode; 562 #endif // TF_LITE_STATIC_MEMORY 563 564 // Light-weight tensor struct for TF Micro runtime. Provides the minimal amount 565 // of information required for a kernel to run during TfLiteRegistration::Eval. 566 // TODO(b/160955687): Move this field into TF_LITE_STATIC_MEMORY when TFLM 567 // builds with this flag by default internally. 568 typedef struct TfLiteEvalTensor { 569 // A union of data pointers. The appropriate type should be used for a typed 570 // tensor based on `type`. 571 TfLitePtrUnion data; 572 573 // A pointer to a structure representing the dimensionality interpretation 574 // that the buffer should have. 575 TfLiteIntArray* dims; 576 577 // The data type specification for data stored in `data`. This affects 578 // what member of `data` union should be used. 579 TfLiteType type; 580 } TfLiteEvalTensor; 581 582 #ifndef TF_LITE_STATIC_MEMORY 583 // Free data memory of tensor `t`. 584 void TfLiteTensorDataFree(TfLiteTensor* t); 585 586 // Free quantization data. 587 void TfLiteQuantizationFree(TfLiteQuantization* quantization); 588 589 // Free sparsity parameters. 590 void TfLiteSparsityFree(TfLiteSparsity* sparsity); 591 592 // Free memory of tensor `t`. 593 void TfLiteTensorFree(TfLiteTensor* t); 594 595 // Set all of a tensor's fields (and free any previously allocated data). 596 void TfLiteTensorReset(TfLiteType type, const char* name, TfLiteIntArray* dims, 597 TfLiteQuantizationParams quantization, char* buffer, 598 size_t size, TfLiteAllocationType allocation_type, 599 const void* allocation, bool is_variable, 600 TfLiteTensor* tensor); 601 602 // Resize the allocated data of a (dynamic) tensor. Tensors with allocation 603 // types other than kTfLiteDynamic will be ignored. 604 void TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor); 605 #endif // TF_LITE_STATIC_MEMORY 606 607 // WARNING: This is an experimental interface that is subject to change. 608 // 609 // Currently, TfLiteDelegateParams has to be allocated in a way that it's 610 // trivially destructable. It will be stored as `builtin_data` field in 611 // `TfLiteNode` of the delegate node. 612 // 613 // See also the `CreateDelegateParams` function in `interpreter.cc` details. 614 typedef struct TfLiteDelegateParams { 615 struct TfLiteDelegate* delegate; 616 TfLiteIntArray* nodes_to_replace; 617 TfLiteIntArray* input_tensors; 618 TfLiteIntArray* output_tensors; 619 } TfLiteDelegateParams; 620 621 typedef struct TfLiteContext { 622 // Number of tensors in the context. 623 size_t tensors_size; 624 625 // The execution plan contains a list of the node indices in execution 626 // order. execution_plan->size is the current number of nodes. And, 627 // execution_plan->data[0] is the first node that needs to be run. 628 // TfLiteDelegates can traverse the current execution plan by iterating 629 // through each member of this array and using GetNodeAndRegistration() to 630 // access details about a node. i.e. 631 // TfLiteIntArray* execution_plan; 632 // TF_LITE_ENSURE_STATUS(context->GetExecutionPlan(context, &execution_plan)); 633 // for (int exec_index = 0; exec_index < execution_plan->size; exec_index++) { 634 // int node_index = execution_plan->data[exec_index]; 635 // TfLiteNode* node; 636 // TfLiteRegistration* reg; 637 // context->GetNodeAndRegistration(context, node_index, &node, ®); 638 // } 639 // WARNING: This is an experimental interface that is subject to change. 640 TfLiteStatus (*GetExecutionPlan)(struct TfLiteContext* context, 641 TfLiteIntArray** execution_plan); 642 643 // An array of tensors in the interpreter context (of length `tensors_size`) 644 TfLiteTensor* tensors; 645 646 // opaque full context ptr (an opaque c++ data structure) 647 void* impl_; 648 649 // Request memory pointer be resized. Updates dimensions on the tensor. 650 // NOTE: ResizeTensor takes ownership of newSize. 651 TfLiteStatus (*ResizeTensor)(struct TfLiteContext*, TfLiteTensor* tensor, 652 TfLiteIntArray* new_size); 653 // Request that an error be reported with format string msg. 654 void (*ReportError)(struct TfLiteContext*, const char* msg, ...); 655 656 // Add `tensors_to_add` tensors, preserving pre-existing Tensor entries. If 657 // non-null, the value pointed to by `first_new_tensor_index` will be set to 658 // the index of the first new tensor. 659 TfLiteStatus (*AddTensors)(struct TfLiteContext*, int tensors_to_add, 660 int* first_new_tensor_index); 661 662 // Get a Tensor node by node_index. 663 // WARNING: This is an experimental interface that is subject to change. 664 TfLiteStatus (*GetNodeAndRegistration)( 665 struct TfLiteContext*, int node_index, TfLiteNode** node, 666 struct TfLiteRegistration** registration); 667 668 // Replace ops with one or more stub delegate operations. This function 669 // does not take ownership of `nodes_to_replace`. 670 TfLiteStatus (*ReplaceNodeSubsetsWithDelegateKernels)( 671 struct TfLiteContext*, struct TfLiteRegistration registration, 672 const TfLiteIntArray* nodes_to_replace, struct TfLiteDelegate* delegate); 673 674 // Number of threads that are recommended to subsystems like gemmlowp and 675 // eigen. 676 int recommended_num_threads; 677 678 // Access external contexts by type. 679 // WARNING: This is an experimental interface that is subject to change. 680 TfLiteExternalContext* (*GetExternalContext)(struct TfLiteContext*, 681 TfLiteExternalContextType); 682 // Set the value of a external context. Does not take ownership of the 683 // pointer. 684 // WARNING: This is an experimental interface that is subject to change. 685 void (*SetExternalContext)(struct TfLiteContext*, TfLiteExternalContextType, 686 TfLiteExternalContext*); 687 688 // Flag for allowing float16 precision for FP32 calculation. 689 // default: false. 690 // WARNING: This is an experimental API and subject to change. 691 bool allow_fp32_relax_to_fp16; 692 693 // Pointer to the op-level profiler, if set; nullptr otherwise. 694 void* profiler; 695 696 // Allocate persistent buffer which has the same life time as the interpreter. 697 // Returns nullptr on failure. 698 // The memory is allocated from heap for TFL, and from tail in TFLM. 699 // This method is only available in Init or Prepare stage. 700 // WARNING: This is an experimental interface that is subject to change. 701 void* (*AllocatePersistentBuffer)(struct TfLiteContext* ctx, size_t bytes); 702 703 // Allocate a buffer which will be deallocated right after invoke phase. 704 // The memory is allocated from heap in TFL, and from volatile arena in TFLM. 705 // This method is only available in invoke stage. 706 // NOTE: If possible use RequestScratchBufferInArena method to avoid memory 707 // allocation during inference time. 708 // WARNING: This is an experimental interface that is subject to change. 709 TfLiteStatus (*AllocateBufferForEval)(struct TfLiteContext* ctx, size_t bytes, 710 void** ptr); 711 712 // Request a scratch buffer in the arena through static memory planning. 713 // This method is only available in Prepare stage and the buffer is allocated 714 // by the interpreter between Prepare and Eval stage. In Eval stage, 715 // GetScratchBuffer API can be used to fetch the address. 716 // WARNING: This is an experimental interface that is subject to change. 717 TfLiteStatus (*RequestScratchBufferInArena)(struct TfLiteContext* ctx, 718 size_t bytes, int* buffer_idx); 719 720 // Get the scratch buffer pointer. 721 // This method is only available in Eval stage. 722 // WARNING: This is an experimental interface that is subject to change. 723 void* (*GetScratchBuffer)(struct TfLiteContext* ctx, int buffer_idx); 724 725 // Resize the memory pointer of the `tensor`. This method behaves the same as 726 // `ResizeTensor`, except that it makes a copy of the shape array internally 727 // so the shape array could be deallocated right afterwards. 728 // WARNING: This is an experimental interface that is subject to change. 729 TfLiteStatus (*ResizeTensorExplicit)(struct TfLiteContext* ctx, 730 TfLiteTensor* tensor, int dims, 731 const int* shape); 732 733 // This method provides a preview of post-delegation partitioning. Each 734 // TfLiteDelegateParams in the referenced array corresponds to one instance of 735 // the delegate kernel. 736 // Example usage: 737 // 738 // TfLiteIntArray* nodes_to_replace = ...; 739 // TfLiteDelegateParams* params_array; 740 // int num_partitions = 0; 741 // TF_LITE_ENSURE_STATUS(context->PreviewDelegatePartitioning( 742 // context, delegate, nodes_to_replace, ¶ms_array, &num_partitions)); 743 // for (int idx = 0; idx < num_partitions; idx++) { 744 // const auto& partition_params = params_array[idx]; 745 // ... 746 // } 747 // 748 // NOTE: The context owns the memory referenced by partition_params_array. It 749 // will be cleared with another call to PreviewDelegateParitioning, or after 750 // TfLiteDelegateParams::Prepare returns. 751 // 752 // WARNING: This is an experimental interface that is subject to change. 753 TfLiteStatus (*PreviewDelegatePartitioning)( 754 struct TfLiteContext* context, const TfLiteIntArray* nodes_to_replace, 755 TfLiteDelegateParams** partition_params_array, int* num_partitions); 756 757 // Returns a TfLiteTensor struct for a given index. 758 // WARNING: This is an experimental interface that is subject to change. 759 // WARNING: This method may not be available on all platforms. 760 TfLiteTensor* (*GetTensor)(const struct TfLiteContext* context, 761 int tensor_idx); 762 763 // Returns a TfLiteEvalTensor struct for a given index. 764 // WARNING: This is an experimental interface that is subject to change. 765 // WARNING: This method may not be available on all platforms. 766 TfLiteEvalTensor* (*GetEvalTensor)(const struct TfLiteContext* context, 767 int tensor_idx); 768 } TfLiteContext; 769 770 typedef struct TfLiteRegistration { 771 // Initializes the op from serialized data. 772 // If a built-in op: 773 // `buffer` is the op's params data (TfLiteLSTMParams*). 774 // `length` is zero. 775 // If custom op: 776 // `buffer` is the op's `custom_options`. 777 // `length` is the size of the buffer. 778 // 779 // Returns a type-punned (i.e. void*) opaque data (e.g. a primitive pointer 780 // or an instance of a struct). 781 // 782 // The returned pointer will be stored with the node in the `user_data` field, 783 // accessible within prepare and invoke functions below. 784 // NOTE: if the data is already in the desired format, simply implement this 785 // function to return `nullptr` and implement the free function to be a no-op. 786 void* (*init)(TfLiteContext* context, const char* buffer, size_t length); 787 788 // The pointer `buffer` is the data previously returned by an init invocation. 789 void (*free)(TfLiteContext* context, void* buffer); 790 791 // prepare is called when the inputs this node depends on have been resized. 792 // context->ResizeTensor() can be called to request output tensors to be 793 // resized. 794 // 795 // Returns kTfLiteOk on success. 796 TfLiteStatus (*prepare)(TfLiteContext* context, TfLiteNode* node); 797 798 // Execute the node (should read node->inputs and output to node->outputs). 799 // Returns kTfLiteOk on success. 800 TfLiteStatus (*invoke)(TfLiteContext* context, TfLiteNode* node); 801 802 // profiling_string is called during summarization of profiling information 803 // in order to group executions together. Providing a value here will cause a 804 // given op to appear multiple times is the profiling report. This is 805 // particularly useful for custom ops that can perform significantly 806 // different calculations depending on their `user-data`. 807 const char* (*profiling_string)(const TfLiteContext* context, 808 const TfLiteNode* node); 809 810 // Builtin codes. If this kernel refers to a builtin this is the code 811 // of the builtin. This is so we can do marshaling to other frameworks like 812 // NN API. 813 // Note: It is the responsibility of the registration binder to set this 814 // properly. 815 int32_t builtin_code; 816 817 // Custom op name. If the op is a builtin, this will be null. 818 // Note: It is the responsibility of the registration binder to set this 819 // properly. 820 // WARNING: This is an experimental interface that is subject to change. 821 const char* custom_name; 822 823 // The version of the op. 824 // Note: It is the responsibility of the registration binder to set this 825 // properly. 826 int version; 827 } TfLiteRegistration; 828 829 // The flags used in `TfLiteDelegate`. Note that this is a bitmask, so the 830 // values should be 1, 2, 4, 8, ...etc. 831 typedef enum TfLiteDelegateFlags { 832 kTfLiteDelegateFlagsNone = 0, 833 // The flag is set if the delegate can handle dynamic sized tensors. 834 // For example, the output shape of a `Resize` op with non-constant shape 835 // can only be inferred when the op is invoked. 836 // In this case, the Delegate is responsible for calling 837 // `SetTensorToDynamic` to mark the tensor as a dynamic tensor, and calling 838 // `ResizeTensor` when invoking the op. 839 // 840 // If the delegate isn't capable to handle dynamic tensors, this flag need 841 // to be set to false. 842 kTfLiteDelegateFlagsAllowDynamicTensors = 1, 843 844 // This flag can be used by delegates (that allow dynamic tensors) to ensure 845 // applicable tensor shapes are automatically propagated in the case of tensor 846 // resizing. 847 // This means that non-dynamic (allocation_type != kTfLiteDynamic) I/O tensors 848 // of a delegate kernel will have correct shapes before its Prepare() method 849 // is called. The runtime leverages TFLite builtin ops in the original 850 // execution plan to propagate shapes. 851 // 852 // A few points to note: 853 // 1. This requires kTfLiteDelegateFlagsAllowDynamicTensors. If that flag is 854 // false, this one is redundant since the delegate kernels are re-initialized 855 // every time tensors are resized. 856 // 2. Enabling this flag adds some overhead to AllocateTensors(), since extra 857 // work is required to prepare the original execution plan. 858 // 3. This flag requires that the original execution plan only have ops with 859 // valid registrations (and not 'dummy' custom ops like with Flex). 860 // WARNING: This feature is experimental and subject to change. 861 kTfLiteDelegateFlagsRequirePropagatedShapes = 2 862 } TfLiteDelegateFlags; 863 864 // WARNING: This is an experimental interface that is subject to change. 865 typedef struct TfLiteDelegate { 866 // Data that delegate needs to identify itself. This data is owned by the 867 // delegate. The delegate is owned in the user code, so the delegate is 868 // responsible for doing this when it is destroyed. 869 void* data_; 870 871 // Invoked by ModifyGraphWithDelegate. This prepare is called, giving the 872 // delegate a view of the current graph through TfLiteContext*. It typically 873 // will look at the nodes and call ReplaceNodeSubsetsWithDelegateKernels() 874 // to ask the TensorFlow lite runtime to create macro-nodes to represent 875 // delegated subgraphs of the original graph. 876 TfLiteStatus (*Prepare)(TfLiteContext* context, 877 struct TfLiteDelegate* delegate); 878 879 // Copy the data from delegate buffer handle into raw memory of the given 880 // 'tensor'. Note that the delegate is allowed to allocate the raw bytes as 881 // long as it follows the rules for kTfLiteDynamic tensors, in which case this 882 // cannot be null. 883 TfLiteStatus (*CopyFromBufferHandle)(TfLiteContext* context, 884 struct TfLiteDelegate* delegate, 885 TfLiteBufferHandle buffer_handle, 886 TfLiteTensor* tensor); 887 888 // Copy the data from raw memory of the given 'tensor' to delegate buffer 889 // handle. This can be null if the delegate doesn't use its own buffer. 890 TfLiteStatus (*CopyToBufferHandle)(TfLiteContext* context, 891 struct TfLiteDelegate* delegate, 892 TfLiteBufferHandle buffer_handle, 893 TfLiteTensor* tensor); 894 895 // Free the Delegate Buffer Handle. Note: This only frees the handle, but 896 // this doesn't release the underlying resource (e.g. textures). The 897 // resources are either owned by application layer or the delegate. 898 // This can be null if the delegate doesn't use its own buffer. 899 void (*FreeBufferHandle)(TfLiteContext* context, 900 struct TfLiteDelegate* delegate, 901 TfLiteBufferHandle* handle); 902 903 // Bitmask flags. See the comments in `TfLiteDelegateFlags`. 904 int64_t flags; 905 } TfLiteDelegate; 906 907 // Build a 'null' delegate, with all the fields properly set to their default 908 // values. 909 TfLiteDelegate TfLiteDelegateCreate(); 910 911 #ifdef __cplusplus 912 } // extern "C" 913 #endif // __cplusplus 914 #endif // TENSORFLOW_LITE_C_COMMON_H_ 915