1 /* Copyright 2017 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_NNAPI_NNAPI_IMPLEMENTATION_H_ 16 #define TENSORFLOW_LITE_NNAPI_NNAPI_IMPLEMENTATION_H_ 17 18 #include <stdint.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 22 #include "tensorflow/lite/nnapi/NeuralNetworksTypes.h" 23 24 struct NnApi { 25 bool nnapi_exists; 26 int32_t android_sdk_version; 27 28 /** 29 * Creates a shared memory object from a file descriptor. 30 * 31 * The shared memory is backed by a file descriptor via mmap. 32 * See {@link ANeuralNetworksMemory} for a description on how to use 33 * this shared memory. 34 * 35 * @param size The requested size in bytes. 36 * Must not be larger than the file size. 37 * @param prot The desired memory protection for the mapping. 38 * It is either PROT_NONE or the bitwise OR of one or 39 * more of the following flags: PROT_READ, PROT_WRITE. 40 * @param fd The requested file descriptor. 41 * The file descriptor has to be mmap-able. The file 42 * descriptor will be duplicated. 43 * @param offset The offset to the beginning of the file of the area to map. 44 * The offset has to be aligned to a page size. 45 * @param memory The memory object to be created. 46 * Set to NULL if unsuccessful. 47 * 48 * @return ANEURALNETWORKS_NO_ERROR if the request completed normally. 49 */ 50 int (*ANeuralNetworksMemory_createFromFd)(size_t size, int protect, int fd, 51 size_t offset, 52 ANeuralNetworksMemory** memory); 53 54 /** 55 * Delete a memory object. 56 * 57 * Destroys the object used by the run time to keep track of the memory. 58 * This will free the underlying actual memory if no other code has open 59 * handles to this memory. 60 * 61 * @param memory The memory object to be freed. 62 */ 63 void (*ANeuralNetworksMemory_free)(ANeuralNetworksMemory* memory); 64 65 /** 66 * Create an empty {@link ANeuralNetworksModel}. 67 * 68 * <p>This only creates the object. Computation is performed once 69 * {@link ANeuralNetworksExecution_startCompute} is invoked. 70 * 71 * The model should be constructed with calls to 72 * {@link ANeuralNetworksModel_addOperation} and 73 * {@link ANeuralNetworksModel_addOperand} 74 * 75 * <p>{@link ANeuralNetworksModel_finish} should be called once the model 76 * has been fully constructed.</p> 77 * 78 * <p>{@link ANeuralNetworksModel_free} should be called once the model 79 * is no longer needed.</p> 80 * 81 * @param model The {@link ANeuralNetworksModel} to be created. 82 * Set to NULL if unsuccessful. 83 * 84 * @return ANEURALNETWORKS_NO_ERROR if successful. 85 */ 86 int (*ANeuralNetworksModel_create)(ANeuralNetworksModel** model); 87 88 /** 89 * Destroy a model. 90 * 91 * The model need not have been finished by a call to 92 * {@link ANeuralNetworksModel_finish}. 93 * 94 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 95 * 96 * @param model The model to be destroyed. Passing NULL is acceptable and 97 * results in no operation. 98 */ 99 void (*ANeuralNetworksModel_free)(ANeuralNetworksModel* model); 100 101 /** 102 * Indicate that we have finished modifying a model. Required before 103 * calling {@link ANeuralNetworksCompilation_compile}. 104 * 105 * An application is responsible to make sure that no other thread uses 106 * the model at the same time. 107 * 108 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 109 * 110 * @param model The model to be finished. 111 * 112 * @return ANEURALNETWORKS_NO_ERROR if successful. 113 */ 114 int (*ANeuralNetworksModel_finish)(ANeuralNetworksModel* model); 115 116 /** 117 * Add an operand to a model. 118 * 119 * The order in which the operands are added is important. The first one added 120 * to a model will have the index value 0, the second 1, etc. These indexes 121 * are used as operand identifiers in 122 * {@link ANeuralNetworksModel_addOperation}, 123 * {@link ANeuralNetworksExecution_setInput}, 124 * {@link ANeuralNetworksExecution_setInputFromMemory}, 125 * {@link ANeuralNetworksExecution_setOutput}, 126 * {@link ANeuralNetworksExecution_setOutputFromMemory} and 127 * {@link ANeuralNetworksExecution_setOperandValue}. 128 * 129 * To build a model that can accommodate inputs of various sizes, as you may 130 * want to do for a CNN, set the size of the dimensions that will vary at run 131 * time to 0. If you do so, provide the full dimensions when calling 132 * {@link ANeuralNetworksExecution_setInput} or {@link 133 * ANeuralNetworksExecution_setInputFromMemory}. 134 * 135 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has 136 * been called will return an error. 137 * 138 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 139 * 140 * @param model The model to be modified. 141 * @param type The {@link ANeuralNetworksOperandType} that describes the shape 142 * of the operand. 143 * 144 * @return ANEURALNETWORKS_NO_ERROR if successful. 145 */ 146 int (*ANeuralNetworksModel_addOperand)( 147 ANeuralNetworksModel* model, const ANeuralNetworksOperandType* type); 148 149 /** 150 * Sets an operand to a constant value. 151 * 152 * For scalar values, the content of buffer is copied into the model. 153 * 154 * For tensor values, a pointer to the buffer is stored within the model. 155 * The application is responsible for not changing the content of this region 156 * until all executions using this model have completed. As the data may 157 * be copied during processing, modifying the data after this call yields 158 * undefined results. 159 * 160 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has 161 * been called will return an error. 162 * 163 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 164 * 165 * @param model The model to be modified. 166 * @param index The index of the model operand we're setting. 167 * @param buffer A pointer to the data to use. 168 * @param length The size in bytes of the data value. 169 * 170 * @return ANEURALNETWORKS_NO_ERROR if successful. 171 */ 172 int (*ANeuralNetworksModel_setOperandValue)(ANeuralNetworksModel* model, 173 int32_t index, const void* buffer, 174 size_t length); 175 176 /** 177 * Sets an operand's per channel quantization parameters. 178 * 179 * Sets parameters required by a tensor of type 180 * {@link ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL}. 181 * This function must be called for every tensor of type 182 * {@link ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL} before 183 * calling {@link ANeuralNetworksModel_finish}. 184 * 185 * Available since API level 29. 186 * 187 * @param model The model to be modified. 188 * @param index The index of the model operand we're setting. 189 * @param channelQuant The per channel quantization parameters for the 190 * operand. No memory in this struct needs to outlive the 191 * call to this function. 192 * 193 * @return ANEURALNETWORKS_NO_ERROR if successful. 194 */ 195 int (*ANeuralNetworksModel_setOperandSymmPerChannelQuantParams)( 196 ANeuralNetworksModel* model, int32_t index, 197 const ANeuralNetworksSymmPerChannelQuantParams* channelQuant); 198 199 /** 200 * Sets an operand to a value stored in a memory object. 201 * 202 * The content of the memory is not copied. A reference to that memory is 203 * stored inside the model. The application is responsible for not changing 204 * the content of the memory region until all executions using this model have 205 * completed. 206 * As the data may be copied during processing, modifying the data after this 207 * call yields undefined results. 208 * 209 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has 210 * been called will return an error. 211 * 212 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 213 * 214 * @param model The model to be modified. 215 * @param index The index of the model operand we're setting. 216 * @param buffer A pointer to the data to use. 217 * @param memory The memory containing the data. 218 * @param offset This specifies the location of the data within the memory. 219 * The offset is in bytes from the start of memory. 220 * @param length The size in bytes of the data value. 221 * 222 * @return ANEURALNETWORKS_NO_ERROR if successful. 223 */ 224 int (*ANeuralNetworksModel_setOperandValueFromMemory)( 225 ANeuralNetworksModel* model, int32_t index, 226 const ANeuralNetworksMemory* memory, size_t offset, size_t length); 227 228 /** 229 * Add an operation to a model. 230 * 231 * @param model The model to be modified. 232 * @param type The type of the operation. 233 * @param inputCount The number of entries in the inputs array. 234 * @param inputs An array of indexes identifying each operand. 235 * @param outputCount The number of entries in the outputs array. 236 * @param outputs An array of indexes identifying each operand. 237 * 238 * The operands specified by inputs and outputs must have been 239 * previously added by calls to {@link ANeuralNetworksModel_addOperand}. 240 * 241 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has 242 * been called will return an error. 243 * 244 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 245 * 246 * @return ANEURALNETWORKS_NO_ERROR if successful. 247 */ 248 int (*ANeuralNetworksModel_addOperation)(ANeuralNetworksModel* model, 249 ANeuralNetworksOperationType type, 250 uint32_t inputCount, 251 const uint32_t* inputs, 252 uint32_t outputCount, 253 const uint32_t* outputs); 254 255 /** 256 * Specifies which operands will be the model's inputs and outputs. 257 * 258 * An operand cannot be used for both input and output. Doing so will 259 * return an error. 260 * 261 * @param model The model to be modified. 262 * @param inputCount The number of entries in the inputs array. 263 * @param inputs An array of indexes identifying the input operands. 264 * @param outputCount The number of entries in the outputs array. 265 * @param outputs An array of indexes identifying the output operands. 266 * 267 * The operands specified by inputs and outputs must have been 268 * previously added by calls to {@link ANeuralNetworksModel_addOperand}. 269 * 270 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has 271 * been called will return an error. 272 * 273 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 274 * 275 * @return ANEURALNETWORKS_NO_ERROR if successful. 276 */ 277 int (*ANeuralNetworksModel_identifyInputsAndOutputs)( 278 ANeuralNetworksModel* model, uint32_t inputCount, const uint32_t* inputs, 279 uint32_t outputCount, const uint32_t* outputs); 280 281 /** 282 * Specifies whether {@link ANEURALNETWORKS_TENSOR_FLOAT32} is allowed to be 283 * calculated with range and/or precision as low as that of the 284 * IEEE 754 16-bit floating-point format. By default, 285 * {@link ANEURALNETWORKS_TENSOR_FLOAT32} must be calculated using at least 286 * the range and precision of the IEEE 754 32-bit floating-point format. 287 * 288 * @param model The model to be modified. 289 * @param allow 'true' indicates {@link ANEURALNETWORKS_TENSOR_FLOAT32} may be 290 * calculated with range and/or precision as low as that of the 291 * IEEE 754 16-bit floating point format. 'false' indicates 292 * {@link ANEURALNETWORKS_TENSOR_FLOAT32} must be calculated 293 * using at least the range and precision of the IEEE 754 32-bit 294 * floating point format. 295 * 296 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has 297 * been called will return an error. 298 * 299 * Available since API level 28. 300 * 301 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 302 * 303 * @return ANEURALNETWORKS_NO_ERROR if successful. 304 */ 305 int (*ANeuralNetworksModel_relaxComputationFloat32toFloat16)( 306 ANeuralNetworksModel* model, bool allow); 307 308 /** 309 * Create a {@link ANeuralNetworksCompilation} to compile the given model. 310 * This only creates the object. Compilation is only performed once 311 * {@link ANeuralNetworksCompilation_start} is invoked. 312 * 313 * <p>The provided model must outlive the compilation.</p> 314 * 315 * The model must already have been finished by a call to 316 * {@link ANeuralNetworksModel_finish}. 317 * 318 * See {@link ANeuralNetworksCompilation} for information on multithreaded 319 * usage. 320 * 321 * @param model The {@link ANeuralNetworksModel} to be compiled. 322 * @param compilation The newly created object or NULL if unsuccessful. 323 * 324 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA 325 * if the model is invalid. 326 */ 327 int (*ANeuralNetworksCompilation_create)( 328 ANeuralNetworksModel* model, ANeuralNetworksCompilation** compilation); 329 330 /** 331 * Destroy a compilation. 332 * 333 * <p>If called on a compilation for which 334 * {@link ANeuralNetworksCompilation_start} has been called, the 335 * function will return immediately but will mark the compilation to be 336 * deleted once the compilation completes. The 337 * {@link ANeuralNetworksCompilation_wait} will return ERROR_DELETED. 338 * 339 * See {@link ANeuralNetworksCompilation} for information on multithreaded 340 * usage. 341 * 342 * @param compilation The compilation to be destroyed. Passing NULL is 343 * acceptable and results in no operation. 344 */ 345 void (*ANeuralNetworksCompilation_free)( 346 ANeuralNetworksCompilation* compilation); 347 348 /** 349 * Sets the execution preference. 350 * 351 * <p>Provides guidance to the runtime when trade-offs are possible.</p> 352 * 353 * See {@link ANeuralNetworksCompilation} for information on multithreaded 354 * usage. 355 * 356 * @param compilation The compilation to be modified. 357 * @param preference Either {@link PREFER_LOW_POWER}, 358 * {@link PREFER_SINGLE_FAST_ANSWER}, or 359 * {@link PREFER_SUSTAINED_SPEED}. 360 * 361 * @return ANEURALNETWORKS_NO_ERROR if successful. 362 */ 363 int (*ANeuralNetworksCompilation_setPreference)( 364 ANeuralNetworksCompilation* compilation, int32_t preference); 365 366 /** 367 * Waits until the compilation completes. 368 * 369 * More than one thread can wait on a compilation. When the compilation 370 * completes, all threads will be released. 371 * 372 * See {@link ANeuralNetworksCompilation} for information on multithreaded 373 * usage. 374 * 375 * @return ANEURALNETWORKS_NO_ERROR if the compilation completed normally. 376 */ 377 int (*ANeuralNetworksCompilation_finish)( 378 ANeuralNetworksCompilation* compilation); 379 380 /** 381 * Create a {@link ANeuralNetworksExecution} to apply the given compilation. 382 * This only creates the object. Computation is only performed once 383 * {@link ANeuralNetworksExecution_startCompute} is invoked. 384 * 385 * <p>The provided compilation must outlive the execution.</p> 386 * 387 * See {@link ANeuralNetworksExecution} for information on multithreaded 388 * usage. 389 * 390 * @param compilation The {@link ANeuralNetworksCompilation} to be evaluated. 391 * @param execution The newly created object or NULL if unsuccessful. 392 * 393 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA 394 * if the compilation is invalid. 395 */ 396 int (*ANeuralNetworksExecution_create)( 397 ANeuralNetworksCompilation* compilation, 398 ANeuralNetworksExecution** execution); 399 400 /** 401 * Destroy an execution. 402 * 403 * <p>If called on an execution for which 404 * {@link ANeuralNetworksExecution_startCompute} has been called, the 405 * function will return immediately but will mark the execution to be deleted 406 * once the computation completes. The {link ANeuralNetworksExecution_wait} 407 * will return ANEURALNETWORKS_ERROR_DELETED. 408 * 409 * See {@link ANeuralNetworksExecution} for information on multithreaded 410 * usage. 411 * 412 * @param execution The execution to be destroyed. Passing NULL is acceptable 413 * and results in no operation. 414 */ 415 void (*ANeuralNetworksExecution_free)(ANeuralNetworksExecution* execution); 416 417 /** 418 * Associate a user buffer with an input of the model of the 419 * {@link ANeuralNetworksExecution}. 420 * 421 * <p>The provided buffer must outlive the execution.</p> 422 * 423 * See {@link ANeuralNetworksExecution} for information on multithreaded 424 * usage. 425 * 426 * @param execution The execution to be modified. 427 * @param index The index of the input argument we are setting. It is 428 * an index into the lists passed to 429 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is 430 * not the index associated with {@link 431 * ANeuralNetworksModel_addOperand}. 432 * @param type The type of the operand. This should be used to specify the 433 * dimensions that were set to 0 when the operand was added to the 434 * model. All other properties of the type must be the same as 435 * specified in the model. If the type is the same as specified 436 * when the model was built, NULL can be passed. 437 * @param buffer The buffer containing the data. 438 * @param length The length in bytes of the buffer. 439 * 440 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if 441 * the name is not recognized or the buffer is too small for the input. 442 */ 443 int (*ANeuralNetworksExecution_setInput)( 444 ANeuralNetworksExecution* execution, int32_t index, 445 const ANeuralNetworksOperandType* type, const void* buffer, 446 size_t length); 447 448 /** 449 * Associate part of a memory object with an input of the model of the 450 * {@link ANeuralNetworksExecution}. 451 * 452 * <p>The provided memory must outlive the execution.</p> 453 * 454 * See {@link ANeuralNetworksExecution} for information on multithreaded 455 * usage. 456 * 457 * @param execution The execution to be modified. 458 * @param index The index of the input argument we are setting. It is 459 * an index into the lists passed to 460 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is 461 * not the index associated with {@link 462 * ANeuralNetworksModel_addOperand}. 463 * @param type The type of the operand. This can be used to specify the 464 * dimensions that were set to 0 when the operand was added to the 465 * model. All other values must be the same as specified in the 466 * model. If the type is the same as specified when the model 467 * was built, NULL can be passed. 468 * @param memory The memory containing the data. 469 * @param offset This specifies the location of the data within the memory. 470 * The offset is in bytes from the start of memory. 471 * @param length The size in bytes of the data value. 472 * 473 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if 474 * the name is not recognized or the buffer is too small for the input. 475 */ 476 int (*ANeuralNetworksExecution_setInputFromMemory)( 477 ANeuralNetworksExecution* execution, int32_t index, 478 const ANeuralNetworksOperandType* type, 479 const ANeuralNetworksMemory* memory, size_t offset, size_t length); 480 481 /** 482 * Associate a user buffer with an output of the model of the 483 * {@link ANeuralNetworksExecution}. 484 * 485 * <p>The provided buffer must outlive the execution.</p> 486 * 487 * See {@link ANeuralNetworksExecution} for information on multithreaded 488 * usage. 489 * 490 * @param execution The execution to be modified. 491 * @param index The index of the output argument we are setting. It is 492 * an index into the lists passed to 493 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is 494 * not the index associated with {@link 495 * ANeuralNetworksModel_addOperand}. 496 * @param type The type of the operand. This can be used to specify the 497 * dimensions that were set to 0 when the operand was added to the 498 * model. All other values must be the same as specified in the 499 * model. If the type is the same as specified when the model 500 * was built, NULL can be passed. 501 * @param buffer The buffer where the data is to be written. 502 * @param length The length in bytes of the buffer. 503 * 504 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if 505 * the name is not recognized or the buffer is too small for the output. 506 */ 507 int (*ANeuralNetworksExecution_setOutput)( 508 ANeuralNetworksExecution* execution, int32_t index, 509 const ANeuralNetworksOperandType* type, void* buffer, size_t length); 510 511 /** 512 * Associate part of a memory object with an output of the model of the 513 * {@link ANeuralNetworksExecution}. 514 * 515 * <p>The provided memory must outlive the execution.</p> 516 * 517 * See {@link ANeuralNetworksExecution} for information on multithreaded 518 * usage. 519 * 520 * @param execution The execution to be modified. 521 * @param index The index of the output argument we are setting. It is 522 * an index into the lists passed to 523 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is 524 * not the index associated with {@link 525 * ANeuralNetworksModel_addOperand}. 526 * @param type The type of the operand. This can be used to specify the 527 * dimensions that were set to 0 when the operand was added to the 528 * model. All other values must be the same as specified in the 529 * model. If the type is the same as specified when the model 530 * was built, NULL can be passed. 531 * @param memory The memory where the data is to be stored. 532 * @param offset This specifies the location of the data within the memory. 533 * The offset is in bytes from the start of memory. 534 * @param length The length in bytes of the data value. 535 * 536 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if 537 * the name is not recognized or the buffer is too small for the output. 538 */ 539 int (*ANeuralNetworksExecution_setOutputFromMemory)( 540 ANeuralNetworksExecution* execution, int32_t index, 541 const ANeuralNetworksOperandType* type, 542 const ANeuralNetworksMemory* memory, size_t offset, size_t length); 543 544 /** 545 * Schedule evaluation of the execution. 546 * 547 * <p>Schedules evaluation of the execution. Once the model has been 548 * applied and the outputs are ready to be consumed, the execution will be 549 * signaled. Use {@link ANeuralNetworksExecution_wait} to wait for that 550 * signal. 551 * </p> 552 * 553 * Multiple executions can be scheduled and evaluated concurrently, and 554 * compilations can be performed concurrently with executions. The runtime 555 * makes no guarantee on the ordering of the completion of compilations and 556 * executions. If it's important to the application, the application should 557 * enforce the ordering by using {@link ANeuralNetworksCompilation_wait} and 558 * {@link ANeuralNetworksExecution_wait}. 559 * 560 * ANeuralNetworksExecution_wait must be called to recuperate the resources 561 * used by the execution. 562 * 563 * See {@link ANeuralNetworksExecution} for information on multithreaded 564 * usage. 565 * 566 * @param execution The execution to be scheduled and executed. 567 * 568 * @return ANEURALNETWORKS_NO_ERROR if successful. 569 */ 570 int (*ANeuralNetworksExecution_startCompute)( 571 ANeuralNetworksExecution* execution, ANeuralNetworksEvent** event); 572 573 /** 574 * Waits until the execution completes. 575 * 576 * More than one thread can wait on an event. When the execution completes, 577 * all threads will be released. 578 * 579 * See {@link ANeuralNetworksExecution} for information on multithreaded 580 * usage. 581 * 582 * @return ANEURALNETWORKS_NO_ERROR if the execution completed normally. 583 */ 584 int (*ANeuralNetworksEvent_wait)(ANeuralNetworksEvent* event); 585 586 /** 587 * Destroys the event. 588 * 589 * See {@link ANeuralNetworksExecution} for information on multithreaded 590 * usage. 591 */ 592 void (*ANeuralNetworksEvent_free)(ANeuralNetworksEvent* event); 593 594 // ASharedMemory_create was added in Android 8.0, so safe to use with NNAPI 595 // which was added in 8.1. 596 int (*ASharedMemory_create)(const char* name, size_t size); 597 598 /** 599 * Get the number of available devices. 600 * 601 * @param numDevices Used to return the number of devices. 602 * 603 * @return ANEURALNETWORKS_NO_ERROR if successful. 604 * 605 * Available since API level 29. 606 */ 607 int (*ANeuralNetworks_getDeviceCount)(uint32_t* numDevices); 608 609 /** 610 * Get the representation of the specified device. 611 * 612 * @param devIndex The index of the specified device. Must be less than the 613 * number of available devices. 614 * @param device The representation of the specified device. 615 * The same representation will always be returned for the 616 * specified device. 617 * 618 * @return ANEURALNETWORKS_NO_ERROR if successful. 619 * 620 * Available since API level 29. 621 */ 622 623 int (*ANeuralNetworks_getDevice)(uint32_t devIndex, 624 ANeuralNetworksDevice** device); 625 626 /** 627 * Get the name of the specified device. 628 * 629 * @param device The representation of the specified device. 630 * @param name The returned name of the specified device. The name will be 631 * in UTF-8 and will be null-terminated. It will be recognizable 632 * as a known device name rather than a cryptic string. For 633 * devices with API level 29 and above, the format of the name is 634 * {VENDOR}-{DEVICE}, e.g. “google-ipu”. For devices with feature 635 * level 28 or lower, the name will always be “unknown-device”. 636 * The name will remain valid for the duration of the application. 637 * 638 * @return ANEURALNETWORKS_NO_ERROR if successful. 639 * 640 * Available since API level 29. 641 */ 642 int (*ANeuralNetworksDevice_getName)(const ANeuralNetworksDevice* device, 643 const char** name); 644 645 /** 646 * Get the version of the driver implementation of the specified device. 647 * 648 * It’s the responsibility of the driver implementor to insure that this 649 * version string uniquely distinguishes this implementation from all previous 650 * implementations. 651 * 652 * This version string must not be confused with the feature level which is 653 * solely defined by {@link ANeuralNetworksDevice_getFeatureLevel}. There is 654 * no implicit ordering of the versions. For example, it is not possible to 655 * filter all drivers older than a certain version. 656 * 657 * Application developers may use this version string to avoid or prefer 658 * specific driver implementations. For example, an application may want to do 659 * so because: 660 * - A specific version of the driver does not provide the required 661 * performance, perhaps because of a performance regression. 662 * - A specific version of the driver has a bug or returns results that 663 * don’t match the minimum precision requirement for the application. 664 * 665 * @param device The representation of the specified device. 666 * @param version The returned version string of the driver for the specified 667 * device. The string will be in UTF-8 and will be 668 * null-terminated. For devices with feature level 28 or lower, 669 * "UNKNOWN" will be returned. The version string will remain 670 * valid for the duration of the application. 671 * 672 * @return ANEURALNETWORKS_NO_ERROR if successful. 673 * 674 * Available since API level 29. 675 */ 676 int (*ANeuralNetworksDevice_getVersion)(const ANeuralNetworksDevice* device, 677 const char** version); 678 679 /** 680 * Get the supported NNAPI version of the specified device. 681 * 682 * Each device has a supported feature level, which is the most advanced 683 * feature this driver implements. For example, if the driver implements the 684 * features introduced in Android P, but does not implement the features 685 * introduced after Android P, the value would be 28. Developers could decide 686 * whether or not the specified device should be used for a Model that has 687 * certain feature requirements. 688 * 689 * @param device The representation of the specified device. 690 * @param featureLevel The API level of the most advanced feature this driver 691 * implements. 692 * 693 * @return ANEURALNETWORKS_NO_ERROR if successful. 694 * 695 * Available since API level 29. 696 */ 697 int (*ANeuralNetworksDevice_getFeatureLevel)( 698 const ANeuralNetworksDevice* device, int64_t* featureLevel); 699 700 /** 701 * Get the type of a given device. 702 * 703 * The device type can be used to help application developers to distribute 704 * Machine Learning workloads and other workloads such as graphical rendering. 705 * E.g., for an app which renders AR scenes based on real time object 706 * detection results, the developer could choose an ACCELERATOR type device 707 * for ML workloads, and reserve GPU for graphical rendering. 708 * 709 * @param device The representation of the specified device. 710 * @param type The returned {@link DeviceTypeCode} of the specified device. 711 * 712 * @return ANEURALNETWORKS_NO_ERROR if successful. 713 * 714 * Available since API level 29. 715 */ 716 int (*ANeuralNetworksDevice_getType)(const ANeuralNetworksDevice* device, 717 int32_t* type); 718 719 /** 720 * Get the supported operations for a specified set of devices. If multiple 721 * devices are selected, the supported operation list is a union of supported 722 * operations of all selected devices. 723 * 724 * @param model The model to be queried. 725 * @param devices The set of devices. Must not contain duplicates. 726 * @param numDevices The number of devices in the set. 727 * @param supportedOps The boolean array to be filled. True means supported. 728 * The size of the boolean array must be at least as large 729 * as the number of operations in the model. The order of 730 * elements in the supportedOps array matches the order in 731 * which the corresponding operations were added to the 732 * model. 733 * 734 * @return ANEURALNETWORKS_NO_ERROR if successful. 735 * 736 * Available since API level 29. 737 */ 738 int (*ANeuralNetworksModel_getSupportedOperationsForDevices)( 739 const ANeuralNetworksModel* model, 740 const ANeuralNetworksDevice* const* devices, uint32_t numDevices, 741 bool* supportedOps); 742 743 /** 744 * Create a {@link ANeuralNetworksCompilation} to compile the given model for 745 * a specified set of devices. If more than one device is specified, the 746 * compilation will distribute the workload automatically across the devices. 747 * The model must be fully supported by the specified set of devices. This 748 * means that ANeuralNetworksModel_getSupportedOperationsForDevices() must 749 * have returned true for every operation for that model/devices pair. 750 * 751 * @param model The {@link ANeuralNetworksModel} to be compiled. 752 * @param devices The set of devices. Must not contain duplicates. 753 * @param numDevices The number of devices in the set. 754 * @param compilation The newly created object or NULL if unsuccessful. 755 * 756 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA 757 * if the model is invalid. 758 * 759 * Available since API level 29. 760 */ 761 int (*ANeuralNetworksCompilation_createForDevices)( 762 ANeuralNetworksModel* model, const ANeuralNetworksDevice* const* devices, 763 uint32_t numDevices, ANeuralNetworksCompilation** compilation); 764 765 /** 766 * Sets the compilation caching signature and the cache directory. 767 * 768 * Provides optional caching information to the runtime for faster repeated 769 * compilation. 770 * 771 * See {@link ANeuralNetworksCompilation} for information on multithreaded 772 * usage. 773 * 774 * @param compilation The compilation to be modified. 775 * @param cacheDir The cache directory to store and retrieve caching data. It 776 * is recommended to use the code_cache provided by the 777 * Android runtime. If not using the code_cache, the user 778 * should choose a directory local to the application, and is 779 * responsible to manage and clean the cache entries. 780 * @param token The token provided by the user to specify a model, must be of 781 * length ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN. The user 782 * should ensure that the token is unique to a model within the 783 * application. The NNAPI runtime will not detected token 784 * collisions. If there is a collision, the compilation outcome 785 * may be incorrect without notifying with error. 786 * 787 * @return ANEURALNETWORKS_NO_ERROR if successful. 788 * 789 * Available since API level 29. 790 */ 791 int (*ANeuralNetworksCompilation_setCaching)( 792 ANeuralNetworksCompilation* compilation, const char* cacheDir, 793 const uint8_t* token); 794 795 /** 796 * Set the maximum expected duration for compiling the model. 797 * 798 * If the device is not able to complete the compilation within the specified 799 * duration, the compilation may be aborted. The timeout duration begins at 800 * the call to {@link ANeuralNetworksCompilation_finish}. 801 * 802 * This timeout duration acts as a hint to drivers, and can be used to both 803 * free up compute resources within the driver and return control back to the 804 * application quicker than is possible without the hint. It enables drivers 805 * that are able to estimate how long a compilation will take to abort the 806 * compilation before it has even started if the driver believes the 807 * compilation cannot be completed within the timeout duration. Similarly, it 808 * enables drivers to abort an ongoing compilation if it is taking too long. 809 * However, this call does not guarantee that the compilation will complete or 810 * abort within the timeout duration. 811 * 812 * By default (i.e., unless ANeuralNetworksCompilation_setTimeout is called), 813 * the timeout duration for compiling the model is considered infinite. 814 * 815 * The {@link ANeuralNetworksCompilation} must have been created with 816 * {@link ANeuralNetworksCompilation_createForDevices} with numDevices = 1, 817 * otherwise this function will fail with ANEURALNETWORKS_BAD_DATA. If the 818 * device has a feature level reported by 819 * {@link ANeuralNetworksDevice_getFeatureLevel} that is lower than 30, then 820 * the timeout duration hint will be ignored. 821 * 822 * See {@link ANeuralNetworksCompilation} for information on multithreaded 823 * usage. 824 * 825 * @param compilation The compilation to be modified. 826 * @param duration The maximum amount of time in nanoseconds that is expected 827 * to be spent finishing a compilation. If this duration is exceeded, the 828 * compilation may be aborted. If set to 0, the timeout duration is 829 * considered infinite. 830 * 831 * @return ANEURALNETWORKS_NO_ERROR if successful. 832 * 833 * Available since API level 30. 834 */ 835 int (*ANeuralNetworksCompilation_setTimeout)( 836 ANeuralNetworksCompilation* compilation, uint64_t duration); 837 838 /** 839 * Set the execution priority. 840 * 841 * Execution priorities are relative to other executions created by the same 842 * application (specifically same uid) for the same device. Specifically, 843 * priorities of executions from one application will not affect executions 844 * from another application. Similarly, priorities of executions on one device 845 * will not affect executions on another device. 846 * 847 * Higher priority executions may use more compute resources than lower 848 * priority executions, and may preempt or starve lower priority executions. 849 * 850 * See {@link ANeuralNetworksCompilation} for information on multithreaded 851 * usage. 852 * 853 * Available since API level 30. 854 * 855 * @param compilation The compilation to be modified. 856 * @param priority The relative priority of the execution compared to other 857 * executions created by the application. Must be one of 858 * ANEURALNETWORKS_PRIORITY_*. 859 * 860 * @return ANEURALNETWORKS_NO_ERROR if successful. 861 */ 862 int (*ANeuralNetworksCompilation_setPriority)( 863 ANeuralNetworksCompilation* compilation, int priority); 864 865 /** 866 * Schedule synchronous evaluation of the execution. 867 * 868 * <p>Schedules synchronous evaluation of the execution. Returns once the 869 * execution has completed and the outputs are ready to be consumed. 870 * </p> 871 * 872 * See {@link ANeuralNetworksExecution} for information on multithreaded 873 * usage. 874 * 875 * See {@link ANeuralNetworksExecution_startCompute} for asynchronous 876 * execution. Synchronous execution incurs lower overhead than asynchronous 877 * execution. 878 * 879 * Available since API level 29. 880 * 881 * @param execution The execution to be scheduled and executed. 882 * 883 * @return ANEURALNETWORKS_NO_ERROR if the execution completed normally. 884 * ANEURALNETWORKS_UNMAPPABLE if the execution input or output memory 885 * cannot be properly mapped. 886 */ 887 int (*ANeuralNetworksExecution_compute)(ANeuralNetworksExecution* execution); 888 889 /** 890 * Set the maximum expected duration of the specified execution. 891 * 892 * If the device is not able to complete the execution within the specified 893 * duration, the execution may be aborted. The timeout duration begins at a 894 * call to one of: 895 * - {@link ANeuralNetworksExecution_burstCompute} 896 * - {@link ANeuralNetworksExecution_compute} 897 * - {@link ANeuralNetworksExecution_startCompute} 898 * - {@link ANeuralNetworksExecution_startComputeWithDependencies} 899 * 900 * This timeout duration acts as a hint to drivers, and can be used to both 901 * free up compute resources within the driver and return control back to the 902 * application quicker than is possible without the hint. It enables drivers 903 * that are able to estimate how long an execution will take to abort the 904 * execution before it has even started if the driver believes the execution 905 * cannot be completed within the timeout duration. Similarly, it enables 906 * drivers to abort an ongoing execution if it is taking too long. However, 907 * this call does not guarantee that the execution will complete or abort 908 * within the timeout duration. 909 * 910 * By default (i.e., unless ANeuralNetworksExecution_setTimeout is called), 911 * the timeout duration for execution is considered infinite. 912 * 913 * The {@link ANeuralNetworksExecution} must have been created from an 914 * {@link ANeuralNetworksCompilation} which in turn was created from 915 * {@link ANeuralNetworksCompilation_createForDevices} with numDevices = 1, 916 * otherwise this function will fail with ANEURALNETWORKS_BAD_DATA. If the 917 * device has a feature level reported by 918 * {@link ANeuralNetworksDevice_getFeatureLevel} that is lower than 30, then 919 * the timeout duration hint will be ignored. 920 * 921 * See {@link ANeuralNetworksExecution} for information on multithreaded 922 * usage. 923 * 924 * @param execution The execution to be modified. 925 * @param duration The maximum amount of time in nanoseconds that is expected 926 * to be spent executing a model. If this duration is exceeded, the execution 927 * may be aborted. If set to 0, the timeout duration is considered 928 * infinite. 929 * 930 * @return ANEURALNETWORKS_NO_ERROR if successful. 931 * 932 * Available since API level 30. 933 */ 934 int (*ANeuralNetworksExecution_setTimeout)( 935 ANeuralNetworksExecution* execution, uint64_t duration); 936 937 /** 938 * Set the maximum duration of WHILE loops in the specified execution. 939 * 940 * This is a fuzzy per-loop timeout intended to prevent infinite loops. 941 * 942 * If a WHILE loop condition model does not output false within the specified 943 * duration, the execution will be aborted. 944 * 945 * See {@link ANeuralNetworks_getDefaultLoopTimeout} and 946 * {@link ANeuralNetworks_getMaximumLoopTimeout} for the default 947 * and maximum timeout values. 948 * 949 * See {@link ANeuralNetworksExecution} for information on multithreaded 950 * usage. 951 * 952 * @param execution The execution to be modified. 953 * @param duration The maximum amount of time in nanoseconds that can be spent 954 * executing a WHILE loop. If the specified duration value exceeds the 955 * value produced by {@link ANeuralNetworks_getMaximumLoopTimeout}, it will be 956 * overridden by that value. 957 * 958 * @return ANEURALNETWORKS_NO_ERROR if successful. 959 * ANEURALNETWORKS_BAD_STATE if execution has started. 960 * ANEURALNETWORKS_UNEXPECTED_NULL if execution is NULL. 961 * 962 * Available since API level 30. 963 */ 964 int (*ANeuralNetworksExecution_setLoopTimeout)( 965 ANeuralNetworksExecution* execution, uint64_t duration); 966 967 /** 968 * Get the dimensional information of the specified output operand of the 969 * model of the 970 * {@link ANeuralNetworksExecution}. 971 * 972 * On asynchronous execution initiated by {@link 973 * ANeuralNetworksExecution_startCompute}, 974 * {@link ANeuralNetworksEvent_wait} must be called prior to this function to 975 * recuperate the resources used by the execution. 976 * 977 * @param execution The execution to be queried. 978 * @param index The index of the output argument we are querying. It is 979 * an index into the lists passed to 980 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is 981 * not the index associated with 982 * {@link ANeuralNetworksModel_addOperand}. 983 * @param rank The rank of the output operand. 984 * 985 * @return ANEURALNETWORKS_NO_ERROR if successful, 986 * ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE if the target output is 987 * provided an insufficient buffer at execution time, 988 * ANEURALNETWORKS_BAD_DATA if the index is invalid. 989 * 990 * Available since API level 29. 991 */ 992 int (*ANeuralNetworksExecution_getOutputOperandRank)( 993 ANeuralNetworksExecution* execution, int32_t index, uint32_t* rank); 994 995 /** 996 * Get the dimensional information of the specified output operand of the 997 * model of the 998 * {@link ANeuralNetworksExecution}. The target output operand cannot be a 999 * scalar. 1000 * 1001 * On asynchronous execution initiated by {@link 1002 * ANeuralNetworksExecution_startCompute}, 1003 * {@link ANeuralNetworksEvent_wait} must be called prior to this function to 1004 * recuperate the resources used by the execution. 1005 * 1006 * @param execution The execution to be queried. 1007 * @param index The index of the output argument we are querying. It is an 1008 * index into the lists passed to 1009 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is 1010 * not the index associated with 1011 * {@link ANeuralNetworksModel_addOperand}. 1012 * @param dimensions The dimension array to be filled. The size of the array 1013 * must be exactly as large as the rank of the output 1014 * operand to be queried in the model. 1015 * 1016 * @return ANEURALNETWORKS_NO_ERROR if successful, 1017 * ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE if the target output is 1018 * provided an insufficient buffer at execution time, 1019 * ANEURALNETWORKS_BAD_DATA if the index is invalid or if the target 1020 * is a scalar. 1021 * 1022 * Available since API level 29. 1023 */ 1024 int (*ANeuralNetworksExecution_getOutputOperandDimensions)( 1025 ANeuralNetworksExecution* execution, int32_t index, uint32_t* dimensions); 1026 1027 /** 1028 * Create a {@link ANeuralNetworksBurst} to apply the given compilation. 1029 * This only creates the burst object. Computation is only performed once 1030 * {@link ANeuralNetworksExecution_burstCompute} is invoked with a valid 1031 * {@link ANeuralNetworksExecution} and {@link ANeuralNetworksBurst}. 1032 * 1033 * <p>The provided compilation must outlive the burst object.</p> 1034 * 1035 * Available since API level 29. 1036 * 1037 * @param compilation The {@link ANeuralNetworksCompilation} to be evaluated. 1038 * @param burst The newly created object or NULL if unsuccessful. 1039 * 1040 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA 1041 * if the compilation is invalid. 1042 */ 1043 int (*ANeuralNetworksBurst_create)(ANeuralNetworksCompilation* compilation, 1044 ANeuralNetworksBurst** burst); 1045 1046 /** 1047 * Destroys the burst object. 1048 * 1049 * Available since API level 29. 1050 * 1051 * @param burst The burst object to be destroyed. Passing NULL is acceptable 1052 * and results in no operation. 1053 */ 1054 void (*ANeuralNetworksBurst_free)(ANeuralNetworksBurst* burst); 1055 1056 /** 1057 * Schedule synchronous evaluation of the execution on a burst object. 1058 * 1059 * <p>Schedules synchronous evaluation of the execution. Returns once the 1060 * execution has completed and the outputs are ready to be consumed.</p> 1061 * 1062 * <p>There must be at most one {@link ANeuralNetworksExecution} processing at 1063 * any given time for any given burst object. Any 1064 * {@link ANeuralNetworksExecution} launched before the previous has finished 1065 * will result in ANEURALNETWORKS_BAD_STATE.</p> 1066 * 1067 * Available since API level 29. 1068 * 1069 * @param burst The burst object to execute on. 1070 * @param execution The execution to be scheduled and executed. The execution 1071 * must be created from the same {@link 1072 * ANeuralNetworksCompilation} as the burst object. 1073 * 1074 * @return ANEURALNETWORKS_NO_ERROR if the execution completed normally. 1075 */ 1076 int (*ANeuralNetworksExecution_burstCompute)( 1077 ANeuralNetworksExecution* execution, ANeuralNetworksBurst* burst); 1078 1079 /** 1080 * Creates a shared memory object from an AHardwareBuffer handle. 1081 * 1082 * If the shared memory is backed by an AHardwareBuffer of 1083 * AHARDWAREBUFFER_FORMAT_BLOB format, it can be used the same way as 1084 * shared memory created from a file handle. See 1085 * {@link ANeuralNetworksMemory} for a description on how to use this 1086 * shared memory. 1087 * 1088 * If the shared memory is backed by an AHardwareBuffer of a format other 1089 * than AHARDWAREBUFFER_FORMAT_BLOB, it can only be used for Model inputs 1090 * and outputs. When calling 1091 * {@link ANeuralNetworksExecution_setInputFromMemory} or 1092 * {@link ANeuralNetworksExecution_setOutputFromMemory} with the shared 1093 * memory, both offset and length must be set to zero and the entire 1094 * memory region will be associated with the specified input or output 1095 * operand. There is no guarantee that an arbitrary AHardwareBuffer_Format 1096 * and AHardwareBuffer_UsageFlags combination can be used by arbitrary 1097 * devices. The execution will fail if selected set of devices cannot 1098 * consume the buffer. 1099 * 1100 * Calling {@link ANeuralNetworksModel_setOperandValueFromMemory} with 1101 * shared memory backed by an AHardwareBuffer of a format other than 1102 * AHARDWAREBUFFER_FORMAT_BLOB is disallowed. 1103 * 1104 * TODO(miaowang): add documentation about intended usage with 1105 * introspection API. 1106 * 1107 * Available since API level 29. 1108 * 1109 * @param ahwb The AHardwareBuffer handle. 1110 * @param memory The memory object to be created. 1111 * Set to NULL if unsuccessful. 1112 * 1113 * @return ANEURALNETWORKS_NO_ERROR if the request completed normally. 1114 * 1115 * @see AHardwareBuffer 1116 */ 1117 int (*ANeuralNetworksMemory_createFromAHardwareBuffer)( 1118 const AHardwareBuffer* ahwb, ANeuralNetworksMemory** memory); 1119 1120 /** 1121 * Specifies whether duration of the {@link ANeuralNetworksExecution} is to be 1122 * measured. By default, duration is not measured. 1123 * 1124 * The {@link ANeuralNetworksExecution} must have been created with 1125 * {@link ANeuralNetworksCompilation_createForDevices} with numDevices = 1. 1126 * 1127 * See {@link ANeuralNetworksExecution} for information on multithreaded 1128 * usage. 1129 * 1130 * Available since API level 29. 1131 * 1132 * @param execution The execution to be modified. 1133 * @param measure 'true' if duration is to be measured, 'false' if not. 1134 * 1135 * @return ANEURALNETWORKS_NO_ERROR if successful. 1136 */ 1137 int (*ANeuralNetworksExecution_setMeasureTiming)( 1138 ANeuralNetworksExecution* execution, bool measure); 1139 1140 /** 1141 * Get the time spent in the specified {@link ANeuralNetworksExecution}, in 1142 * nanoseconds. The execution must have completed. 1143 * 1144 * @param execution The execution to be queried. 1145 * @param durationCode The measurement to be queried, specified by {@link 1146 * DurationCode}. 1147 * @param duration The returned duration. If no measurement was requested by 1148 * {@link ANeuralNetworksExecution_setMeasureTiming}, or for 1149 * some other reason the duration is not available, UINT64_MAX will be 1150 * returned. A particular device need not support any given measurement. 1151 * 1152 * @return ANEURALNETWORKS_NO_ERROR if successful. 1153 */ 1154 int (*ANeuralNetworksExecution_getDuration)( 1155 const ANeuralNetworksExecution* execution, int32_t durationCode, 1156 uint64_t* duration); 1157 1158 /** 1159 * Queries whether an extension is supported by the driver implementation of 1160 * the specified device. 1161 * 1162 * @param device The representation of the specified device. 1163 * @param extension The extension name. 1164 * @param isExtensionSupported The boolean value indicating whether the 1165 * extension is supported. 1166 * 1167 * @return ANEURALNETWORKS_NO_ERROR if successful. 1168 * 1169 * Available since API level 29. 1170 */ 1171 int (*ANeuralNetworksDevice_getExtensionSupport)( 1172 const ANeuralNetworksDevice* device, const char* extensionName, 1173 bool* isExtensionSupported); 1174 1175 /** 1176 * Creates an operand type from an extension name and an extension operand 1177 * code. 1178 * 1179 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 1180 * 1181 * Available since API level 29. 1182 * 1183 * @param model The model to contain the operand. 1184 * @param extensionName The extension name. 1185 * @param operandCodeWithinExtension The extension operand code. 1186 * @param type The operand type. 1187 * 1188 * @return ANEURALNETWORKS_NO_ERROR if successful. 1189 */ 1190 int (*ANeuralNetworksModel_getExtensionOperandType)( 1191 ANeuralNetworksModel* model, const char* extensionName, 1192 uint16_t operandCodeWithinExtension, int32_t* type); 1193 1194 /** 1195 * Creates an operation type from an extension name and an extension operation 1196 * code. 1197 * 1198 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 1199 * 1200 * Available since API level 29. 1201 * 1202 * @param model The model to contain the operation. 1203 * @param extensionName The extension name. 1204 * @param operationCodeWithinExtension The extension operation code. 1205 * @param type The operation type. 1206 * 1207 * @return ANEURALNETWORKS_NO_ERROR if successful. 1208 */ 1209 int (*ANeuralNetworksModel_getExtensionOperationType)( 1210 ANeuralNetworksModel* model, const char* extensionName, 1211 uint16_t operationCodeWithinExtension, 1212 ANeuralNetworksOperationType* type); 1213 1214 /** 1215 * Sets extension operand parameters. 1216 * 1217 * Available since API level 29. 1218 * 1219 * @param model The model to be modified. 1220 * @param index The index of the model operand we're setting. 1221 * @param data A pointer to the extension operand data. 1222 * The data does not have to outlive the call to this function. 1223 * @param length The size in bytes of the data value. 1224 * 1225 * @return ANEURALNETWORKS_NO_ERROR if successful. 1226 */ 1227 int (*ANeuralNetworksModel_setOperandExtensionData)( 1228 ANeuralNetworksModel* model, int32_t index, const void* data, 1229 size_t length); 1230 1231 /** 1232 * Create a {@link ANeuralNetworksMemoryDesc} with no properties. 1233 * 1234 * This only creates the memory descriptor. Its properties should be set with 1235 * calls to 1236 * {@link ANeuralNetworksMemoryDesc_addInputRole}, 1237 * {@link ANeuralNetworksMemoryDesc_addOutputRole}, and 1238 * {@link ANeuralNetworksMemoryDesc_setDimensions}. 1239 * 1240 * {@link ANeuralNetworksMemoryDesc_finish} must be called once all properties 1241 * have been set. 1242 * 1243 * {@link ANeuralNetworksMemoryDesc_free} must be called once the memory 1244 * descriptor is no longer needed. 1245 * 1246 * Available since API level 30. 1247 * 1248 * @param desc The {@link ANeuralNetworksMemoryDesc} to be created. 1249 * Set to NULL if unsuccessful. 1250 * 1251 * @return ANEURALNETWORKS_NO_ERROR if successful. 1252 */ 1253 int (*ANeuralNetworksMemoryDesc_create)(ANeuralNetworksMemoryDesc** desc); 1254 1255 /** 1256 * Destroy a memory descriptor. 1257 * 1258 * The memory descriptor need not have been finished by a call to 1259 * {@link ANeuralNetworksMemoryDesc_finish}. 1260 * 1261 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded 1262 * usage. 1263 * 1264 * Available since API level 30. 1265 * 1266 * @param desc The memory descriptor to be destroyed. Passing NULL is 1267 * acceptable and results in no operation. 1268 */ 1269 void (*ANeuralNetworksMemoryDesc_free)(ANeuralNetworksMemoryDesc* desc); 1270 1271 /** 1272 * Specify that a memory object will be playing the role of an input to an 1273 * execution created from a particular compilation. 1274 * 1275 * The compilation and the input index fully specify an input operand. This 1276 * function may be invoked multiple times on the same memory descriptor with 1277 * different input operands, and the same input operand may be specified on 1278 * multiple memory descriptors. However, specifying the same input operand on 1279 * the same memory descriptor more than once will return an error. 1280 * 1281 * The dimensions of the corresponding model operands of all the roles 1282 * specified by 1283 * {@link ANeuralNetworksMemoryDesc_addInputRole} and 1284 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be compatible with 1285 * each other. Two dimensions are incompatible if both ranks are fully 1286 * specified but have different values, or if there is at least one axis that 1287 * is fully specified in both but has different values. 1288 * 1289 * At least one of {@link ANeuralNetworksMemoryDesc_addInputRole} and 1290 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be called on a memory 1291 * descriptor before invoking {@link ANeuralNetworksMemoryDesc_finish}. 1292 * 1293 * Attempting to modify a memory descriptor once 1294 * {@link ANeuralNetworksMemoryDesc_finish} has been called will return an 1295 * error. 1296 * 1297 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded 1298 * usage. 1299 * 1300 * Available since API level 30. 1301 * 1302 * @param desc The memory descriptor to be modified. 1303 * @param compilation The compilation object. It must already have been 1304 * finished by calling {@link ANeuralNetworksCompilation_finish}, and must 1305 * outlive the memory descriptor. 1306 * @param index The index of the input argument we are referencing from the 1307 * compilation. It is an index into the inputs list passed to 1308 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is 1309 * not the index associated with {@link ANeuralNetworksModel_addOperand}. 1310 * @param frequency A floating-point value within the range (0.0, 1.0]. 1311 * Describes how likely the memory is to be used in the specified role. This 1312 * is provided as a hint to optimize the case when different roles prefer 1313 * different memory locations or data layouts. 1314 * 1315 * @return ANEURALNETWORKS_NO_ERROR if successful. 1316 */ 1317 int (*ANeuralNetworksMemoryDesc_addInputRole)( 1318 ANeuralNetworksMemoryDesc* desc, 1319 const ANeuralNetworksCompilation* compilation, int32_t index, 1320 float frequency); 1321 1322 /** 1323 * Specify that a memory object will be playing the role of an output to an 1324 * execution created from a particular compilation. 1325 * 1326 * The compilation and the output index fully specify an output operand. This 1327 * function may be invoked multiple times on the same memory descriptor with 1328 * different output operands, and the same output operand may be specified on 1329 * multiple memory descriptors. However, specifying the same output operand on 1330 * the same memory descriptor object more than once will return an error. 1331 * 1332 * The dimensions of the corresponding model operands of all the roles 1333 * specified by 1334 * {@link ANeuralNetworksMemoryDesc_addInputRole} and 1335 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be compatible with 1336 * each other. Two dimensions are incompatible if both ranks are fully 1337 * specified but have different values, or if there is at least one axis that 1338 * is fully specified in both but has different values. 1339 * 1340 * At least one of {@link ANeuralNetworksMemoryDesc_addInputRole} and 1341 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be called on the 1342 * memory descriptor before invoking {@link ANeuralNetworksMemoryDesc_finish}. 1343 * 1344 * Attempting to modify a memory descriptor once 1345 * {@link ANeuralNetworksMemoryDesc_finish} has been called will return an 1346 * error. 1347 * 1348 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded 1349 * usage. 1350 * 1351 * Available since API level 30. 1352 * 1353 * @param desc The memory descriptor to be modified. 1354 * @param compilation The compilation object. It must already have been 1355 * finished by calling {@link ANeuralNetworksCompilation_finish}, and must 1356 * outlive the memory descriptor. 1357 * @param index The index of the output argument we are referencing from the 1358 * compilation. It is an index into the outputs list passed to 1359 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is 1360 * not the index associated with {@link ANeuralNetworksModel_addOperand}. 1361 * @param frequency A floating-point value within the range (0.0, 1.0]. 1362 * Describes how likely the memory is to be used in the specified role. This 1363 * is provided as a hint to optimize the case when multiple roles prefer 1364 * different memory locations or data layouts. 1365 * 1366 * @return ANEURALNETWORKS_NO_ERROR if successful. 1367 */ 1368 int (*ANeuralNetworksMemoryDesc_addOutputRole)( 1369 ANeuralNetworksMemoryDesc* desc, 1370 const ANeuralNetworksCompilation* compilation, uint32_t index, 1371 float frequency); 1372 1373 /** 1374 * Set the dimensional information of the memory descriptor. 1375 * 1376 * The specified dimensions must be compatible with the dimensions of the 1377 * corresponding model operands of all the roles specified by 1378 * {@link ANeuralNetworksMemoryDesc_addInputRole} and 1379 * {@link ANeuralNetworksMemoryDesc_addOutputRole}. Two dimensions are 1380 * incompatible if both ranks are fully specified but have different values, 1381 * or if there is at least one axis that is fully specified in both but has 1382 * different values. 1383 * 1384 * Attempting to modify a memory descriptor once 1385 * {@link ANeuralNetworksMemoryDesc_finish} has been called will return an 1386 * error. 1387 * 1388 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded 1389 * usage. 1390 * 1391 * Available since API level 30. 1392 * 1393 * @param desc The memory descriptor to be modified. 1394 * @param rank The number of dimensions. Must be 0 for scalars. 1395 * @param dimensions An array of dimensions. An entry with the value 0 1396 * indicates that the corresponding axis has an unknown size. 1397 * 1398 * @return ANEURALNETWORKS_NO_ERROR if successful. 1399 */ 1400 int (*ANeuralNetworksMemoryDesc_setDimensions)( 1401 ANeuralNetworksMemoryDesc* desc, uint32_t rank, 1402 const uint32_t* dimensions); 1403 1404 /** 1405 * Indicate that we have finished modifying a memory descriptor. Required 1406 * before calling 1407 * {@link ANeuralNetworksMemory_createFromDesc}. 1408 * 1409 * This function must only be called once for a given memory descriptor. 1410 * 1411 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded 1412 * usage. 1413 * 1414 * Available since API level 30. 1415 * 1416 * @param desc The memory descriptor to be finished. 1417 * 1418 * @return ANEURALNETWORKS_NO_ERROR if successful. 1419 */ 1420 int (*ANeuralNetworksMemoryDesc_finish)(ANeuralNetworksMemoryDesc* desc); 1421 1422 /** 1423 * Creates a memory object from a memory descriptor. 1424 * 1425 * The memory object is created with an uninitialized buffer. A memory object 1426 * with an uninitialized buffer may only be used according to the roles 1427 * specified by 1428 * {@link ANeuralNetworksMemoryDesc_addOutputRole}, or as the destination 1429 * memory in 1430 * {@link ANeuralNetworksMemory_copy}. The buffer of a memory object is 1431 * initialized after the memory object is used as an output in a successful 1432 * execution, or used as the destination memory in a successful {@link 1433 * ANeuralNetworksMemory_copy}. A memory object with an initialized buffer may 1434 * be used according to all roles specified in 1435 * {@link ANeuralNetworksMemoryDesc}, or as the source or destination memory 1436 * in 1437 * {@link ANeuralNetworksMemory_copy}. The buffer of a memory object will 1438 * return to the uninitialized state if the memory object is used as an output 1439 * in a failed execution, or used as the destination memory in a failed {@link 1440 * ANeuralNetworksMemory_copy}. 1441 * 1442 * The dimensions of the memory descriptor are deduced from the dimensions of 1443 * the corresponding model operands of all the roles specified by 1444 * {@link ANeuralNetworksMemoryDesc_addInputRole} and 1445 * {@link ANeuralNetworksMemoryDesc_addOutputRole}, as well as the dimensions 1446 * set by the call to {@link ANeuralNetworksMemoryDesc_setDimensions}, if any. 1447 * The memory descriptor may have unspecified dimensions or rank. In such a 1448 * case, the same memory object may be used with different shapes of outputs 1449 * in different executions. When the memory is used as an input, the input 1450 * shape must be the same as the output shape from the last execution using 1451 * this memory object as an output, or the last 1452 * {@link ANeuralNetworkMemory_copy} using this memory object as the 1453 * destination memory. Creating a memory object with unspecified dimensions or 1454 * rank may fail for certain sets of roles. 1455 * 1456 * Using the memory in roles or shapes that are not compatible with the rules 1457 * specified above will return an error. 1458 * 1459 * When calling {@link ANeuralNetworksExecution_setInputFromMemory} or 1460 * {@link ANeuralNetworksExecution_setOutputFromMemory} with the memory 1461 * object, both offset and length must be set to zero and the entire memory 1462 * region will be associated with the specified input or output operand. 1463 * 1464 * Calling {@link ANeuralNetworksModel_setOperandValueFromMemory} with the 1465 * memory created from this function will return an error. 1466 * 1467 * {@link ANeuralNetworksMemory_free} must be called once the memory is no 1468 * longer needed. 1469 * 1470 * Attempting to create memory from an unfinished memory descriptor will 1471 * return an error. 1472 * 1473 * The provided {@link ANeuralNetworksMemoryDesc} need not outlive the 1474 * {@link ANeuralNetworksMemory} object. 1475 * 1476 * Available since API level 30. 1477 * 1478 * @param desc The memory descriptor. 1479 * @param memory The memory object to be created. 1480 * Set to NULL if unsuccessful. 1481 * 1482 * @return ANEURALNETWORKS_NO_ERROR if successful; ANEURALNETWORKS_OP_FAILED 1483 * if the memory is created with unspecified dimensions or rank and it is not 1484 * supported for this set of roles. 1485 */ 1486 int (*ANeuralNetworksMemory_createFromDesc)( 1487 const ANeuralNetworksMemoryDesc* desc, ANeuralNetworksMemory** memory); 1488 1489 /** 1490 * Copies data from one memory object to another. 1491 * 1492 * If at most one of the src and dst is created from 1493 * {@link ANeuralNetworksMemory_createFromDesc}, the src and dst must have the 1494 * same logical size: 1495 * - If the memory is created from {@link ANeuralNetworksMemory_createFromFd}, 1496 * or if it is created from {@link 1497 * ANeuralNetworksMemory_createFromAHardwareBuffer} with format of 1498 * AHARDWAREBUFFER_FORMAT_BLOB, the logical size equals the size of the 1499 * memory. 1500 * - If the memory is created from 1501 * {@link ANeuralNetworksMemory_createFromAHardwareBuffer} with a format 1502 * other than AHARDWAREBUFFER_FORMAT_BLOB, the logical size equals the size 1503 * when there is no padding and the data is tightly packed. This function may 1504 * fail if the AHardwareBuffer cannot be accessed. 1505 * - If the memory is created from {@link 1506 * ANeuralNetworksMemory_createFromDesc}, the logical size equals the size 1507 * indicated by the {@link OperandCode} multiplied by the number of elements. 1508 * This function will fail if the number of elements is unknown. 1509 * 1510 * If both src and dst are created from {@link 1511 * ANeuralNetworksMemory_createFromDesc}, they must have compatible 1512 * dimensions. Two dimensions are incompatible if both ranks are fully 1513 * specified but have different values, or if there is at least one axis that 1514 * is fully specified in both but has different values. The dst may have 1515 * unspecified dimensions or rank. In such a case, the dimensions of dst will 1516 * get updated according to the dimensions of the src. 1517 * 1518 * In both cases, if the src is created from 1519 * {@link ANeuralNetworksMemory_createFromDesc}, it must have been used as an 1520 * output in a successful execution, or used as the destination memory in a 1521 * successful 1522 * {@link ANeuralNetworksMemory_copy}. 1523 * 1524 * The src and dst may have different data layout, in which case the data 1525 * copying is performed logically with data layout transformation. 1526 * 1527 * Available since API level 30. 1528 * 1529 * @param src The source memory object. 1530 * @param dst The destination memory object. 1531 * 1532 * @return ANEURALNETWORKS_NO_ERROR if successful. 1533 */ 1534 int (*ANeuralNetworksMemory_copy)(const ANeuralNetworksMemory* src, 1535 const ANeuralNetworksMemory* dst); 1536 1537 /** 1538 * Create a {@link ANeuralNetworksEvent} from a sync_fence file descriptor. 1539 * 1540 * The newly created ANeuralNetworksEvent does not take ownership of the 1541 * provided sync_fence_fd, it will instead dup the provided sync_fence_fd and 1542 * own the duplicate. 1543 * 1544 * @param sync_fence_fd The sync_fence file descriptor. 1545 * @param event The newly created object or NULL if unsuccessful. 1546 * 1547 * @return ANEURALNETWORKS_NO_ERROR if successful. 1548 * 1549 * Available since API level 30. 1550 */ 1551 int (*ANeuralNetworksEvent_createFromSyncFenceFd)( 1552 int sync_fence_fd, ANeuralNetworksEvent** event); 1553 1554 /** 1555 * Get sync_fence file descriptor from the event. 1556 * 1557 * If the ANeuralNetworksEvent is not backed by a sync fence, the 1558 * sync_fence_fd will be set to -1, and ANEURALNETWORKS_BAD_DATA will be 1559 * returned. 1560 * 1561 * See {@link ANeuralNetworksEvent_createFromSyncFenceFd} and 1562 * {@link ANeuralNetworksExecution_startComputeWithDependencies} to see how to 1563 * create an event backed by a sync fence. 1564 * 1565 * The user takes ownership of the returned fd, and must close the returned 1566 * file descriptor when it is no longer needed. 1567 * 1568 * @param event An event that is backed by a sync fence. 1569 * @param sync_fence_fd The sync_fence file descriptor. The file descriptor 1570 * will be set to -1 if there is an error. 1571 * 1572 * @return ANEURALNETWORKS_NO_ERROR if successful. 1573 * 1574 * Available since API level 30. 1575 */ 1576 int (*ANeuralNetworksEvent_getSyncFenceFd)(const ANeuralNetworksEvent* event, 1577 int* sync_fence_fd); 1578 1579 /** 1580 * Schedule asynchronous evaluation of the execution with dependencies. 1581 * 1582 * The execution will wait for all the depending events to be signaled before 1583 * starting the evaluation. Once the execution has completed and the outputs 1584 * are ready to be consumed, the returned event will be signaled. Depending on 1585 * which devices are handling the execution, the event could be backed by a 1586 * sync fence. Use {@link ANeuralNetworksEvent_wait} to wait for that event. 1587 * 1588 * ANeuralNetworksEvent_wait must be called to recurperate the resources used 1589 * by the execution. 1590 * 1591 * If parts of the execution are scheduled on devices that do not support 1592 * fenced execution, the function call may wait for such parts to finish 1593 * before returning. 1594 * 1595 * The function will return an error if any of the events in dependencies is 1596 * already in a bad state. After the execution is scheduled, if any of the 1597 * events in dependencies does not complete normally, the execution will fail, 1598 * and {@link ANeuralNetworksEvent_wait} on the returned event will return an 1599 * error. 1600 * 1601 * The function will return an error if any of the execution outputs has a 1602 * tensor operand type that is not fully specified. 1603 * 1604 * The function can be passed a timeout duration in nanoseconds. This timeout 1605 * duration acts as a hint to drivers in the same way that the timeout 1606 * durations in {@link ANeuralNetworksCompilation_setTimeout} and {@link 1607 * ANeuralNetworksExecution_setTimeout} act as hints to drivers. The duration 1608 * begins when all waitFor sync fences have been signaled, and can be used 1609 * together with {@link ANeuralNetworksExecution_setTimeout} which specifies 1610 * the maximum timeout duration beginning at the call to 1611 * {@link ANeuralNetworksExecution_startComputeWithDependencies}. 1612 * If the duration is non-zero, the {@link ANeuralNetworksExecution} must have 1613 * been created from an {@link ANeuralNetworksCompilation} which in turn was 1614 * created from 1615 * {@link ANeuralNetworksCompilation_createForDevices} with numDevices = 1, 1616 * otherwise this function will fail with ANEURALNETWORKS_BAD_DATA. If either 1617 * the timeout duration from {@link ANeuralNetworksExecution_setTimeout} or 1618 * the timeout duration passed to this call is exceeded, the execution may be 1619 * aborted, in which case {@link ANEURALNETWORKS_MISSED_DEADLINE_*} will be 1620 * returned through {@link 1621 * ANeuralNetworksExecution_startComputeWithDependencies} or {@link 1622 * ANeuralNetworksEvent_wait} on the event object. If the device has a feature 1623 * level reported by {@link ANeuralNetworksDevice_getFeatureLevel} that is 1624 * lower than 30, then the timeout duration hints will be ignored. 1625 * 1626 * If this execution contains a {@link ANEURALNETWORKS_WHILE} operation, and 1627 * the condition model does not output false within the loop timeout duration, 1628 * then execution will be aborted and {@link 1629 * ANEURALNETWORKS_MISSED_DEADLINE_*} will be returned through {@link 1630 * ANeuralNetworksEvent_wait} on the event object. 1631 * 1632 * See {@link ANeuralNetworksExecution} for information on multithreaded 1633 * usage. 1634 * 1635 * See {@link ANeuralNetworksExecution_compute} for synchronous execution. 1636 * See {@link ANeuralNetworksExecution_burstCompute} for burst synchronous 1637 * execution. See {@link ANeuralNetworksExecution_startCompute} for regular 1638 * asynchronous execution. 1639 * 1640 * @param execution The execution to be scheduled and executed. 1641 * @param dependencies A set of depending events. The actual evaluation will 1642 * not start until all the events are signaled. 1643 * @param num_dependencies The number of events in the dependencies set. 1644 * @param duration The maximum amount of time in nanoseconds that is expected 1645 * to be spent executing the model after all dependencies are signaled. If set 1646 * to 0, the timeout duration is considered infinite. 1647 * @param event The event that will be signaled on completion. event is set to 1648 * NULL if there's an error. 1649 * 1650 * @return ANEURALNETWORKS_NO_ERROR if the evaluation is successfully 1651 * scheduled. 1652 * 1653 * Available since API level 30. 1654 */ 1655 int (*ANeuralNetworksExecution_startComputeWithDependencies)( 1656 ANeuralNetworksExecution* execution, 1657 const ANeuralNetworksEvent* const* dependencies, 1658 uint32_t num_dependencies, uint64_t duration, 1659 ANeuralNetworksEvent** event); 1660 }; 1661 1662 /** 1663 * Load the NNAPI implementation from the shared libraries. 1664 * The NnApi structure is filled with all the pointers. If one function doesn't 1665 * exist, a null pointer is stored. 1666 */ 1667 const NnApi* NnApiImplementation(); 1668 1669 #endif // TENSORFLOW_LITE_NNAPI_NNAPI_IMPLEMENTATION_H_ 1670