1%% template file for generating NeuralNetworks.h. 2%% see README.md. 3/* 4 * Copyright (C) 2017 The Android Open Source Project 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19/** 20 * @addtogroup NeuralNetworks 21 * @{ 22 */ 23 24/** 25 * @file NeuralNetworks.h 26 */ 27 28#ifndef ANDROID_FRAMEWORKS_ML_NN_RUNTIME_NEURAL_NETWORKS_H 29#define ANDROID_FRAMEWORKS_ML_NN_RUNTIME_NEURAL_NETWORKS_H 30 31/****************************************************************** 32 * 33 * IMPORTANT NOTICE: 34 * 35 * This file is part of Android's set of stable system headers 36 * exposed by the Android NDK (Native Development Kit). 37 * 38 * Third-party source AND binary code relies on the definitions 39 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES. 40 * 41 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES) 42 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS 43 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY 44 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES 45 */ 46 47#include <android/hardware_buffer.h> 48#include <stddef.h> 49#include <stdint.h> 50#include <sys/cdefs.h> 51 52__BEGIN_DECLS 53 54%insert Operand_1.0_Comment 55typedef enum { 56%insert Operand_1.0 57%insert Operand_1.2 58%insert Operand_1.3 59} OperandCode; 60 61%insert Operation_1.0_Comment 62typedef enum { 63 // Operations below are available since API level 27. 64 65%insert Operation_1.0 66 67 // Operations below are available since API level 28. 68 69%insert Operation_1.1 70 71 // Operations below are available since API level 29. 72 73%insert Operation_1.2 74 75 // Operations below are available since API level 30. 76 77%insert Operation_1.3 78} OperationCode; 79 80/** 81 * Fused activation function types. 82 * 83 * 84 * Available since API level 27. 85 */ 86typedef enum { 87 /** NO fused activation function. */ 88 ANEURALNETWORKS_FUSED_NONE = 0, 89 /** Fused ReLU activation function. */ 90 ANEURALNETWORKS_FUSED_RELU = 1, 91 /** Fused ReLU1 activation function. */ 92 ANEURALNETWORKS_FUSED_RELU1 = 2, 93 /** Fused ReLU6 activation function. */ 94 ANEURALNETWORKS_FUSED_RELU6 = 3, 95} FuseCode; 96 97/** 98 * Implicit padding algorithms. 99 * 100 * 101 * Available since API level 27. 102 */ 103typedef enum { 104 /** 105 * SAME padding. 106 * Padding on both ends are the "same": 107 * padding_to_beginning = total_padding / 2 108 * padding_to_end = (total_padding + 1)/2. 109 * i.e., for even number of padding, padding to both ends are exactly 110 * the same; for odd number of padding, padding to the ending is bigger 111 * than the padding to the beginning by 1. 112 * 113 * total_padding is a function of input, stride, dilation and filter size. 114 * It could be computed as follows: 115 * out_size = (input + stride - 1) / stride 116 * effective_filter_size = (filter_size - 1) * dilation + 1 117 * needed_input = (out_size - 1) * stride + effective_filter_size 118 * total_padding = max(0, needed_input - input_size) 119 * The computation is the same for the horizontal and vertical directions. 120 */ 121 ANEURALNETWORKS_PADDING_SAME = 1, 122 123 /** 124 * VALID padding. 125 * No padding. When the input size is not evenly divisible by 126 * the filter size, the input at the end that could not fill 127 * the whole filter tile will simply be ignored. 128 */ 129 ANEURALNETWORKS_PADDING_VALID = 2, 130} PaddingCode; 131 132/** 133 * Execution preferences. 134 * 135 * Available since API level 27. 136 */ 137typedef enum { 138 /** 139 * Prefer executing in a way that minimizes battery drain. 140 * This is desirable for compilations that will be executed often. 141 */ 142 ANEURALNETWORKS_PREFER_LOW_POWER = 0, 143 /** 144 * Prefer returning a single answer as fast as possible, even if this causes 145 * more power consumption. 146 */ 147 ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER = 1, 148 /** 149 * Prefer maximizing the throughput of successive frames, for example when 150 * processing successive frames coming from the camera. 151 */ 152 ANEURALNETWORKS_PREFER_SUSTAINED_SPEED = 2, 153} PreferenceCode; 154 155/** 156 * Device types. 157 * 158 * The type of NNAPI device. 159 */ 160typedef enum { 161 /** The device type cannot be provided. */ 162 ANEURALNETWORKS_DEVICE_UNKNOWN = 0, 163 /** The device does not fall into any category below. */ 164 ANEURALNETWORKS_DEVICE_OTHER = 1, 165 /** The device runs NNAPI models on single or multi-core CPU. */ 166 ANEURALNETWORKS_DEVICE_CPU = 2, 167 /** The device can run NNAPI models and also accelerate graphics APIs such 168 * as OpenGL ES and Vulkan. */ 169 ANEURALNETWORKS_DEVICE_GPU = 3, 170 /** Dedicated accelerator for Machine Learning workloads. */ 171 ANEURALNETWORKS_DEVICE_ACCELERATOR = 4, 172} DeviceTypeCode; 173 174/** 175 * Result codes. 176 * 177 * <p>Any NNAPI function can return any result code, including result codes not 178 * currently documented. Any value other than {@link ANEURALNETWORKS_NO_ERROR} 179 * indicates a failure of some kind.</p> 180 * 181 * <p>Additional information about the nature of a failure can be obtained from 182 * the device log after enabling NNAPI debugging by setting the debug.nn.vlog 183 * property to 1, e.g., by calling "adb shell setprop debug.nn.vlog 1".</p> 184 * 185 * Available since API level 27. 186 */ 187typedef enum { 188 /** 189 * Operation was succesful. 190 */ 191 ANEURALNETWORKS_NO_ERROR = 0, 192 193 /** 194 * Failure caused by not enough available memory. 195 */ 196 ANEURALNETWORKS_OUT_OF_MEMORY = 1, 197 198 ANEURALNETWORKS_INCOMPLETE = 2, 199 200 /** 201 * Failure caused by unexpected null argument. 202 */ 203 ANEURALNETWORKS_UNEXPECTED_NULL = 3, 204 205 /** 206 * Failure caused by invalid function arguments, invalid model definition, 207 * invalid execution definition or invalid data at execution time. 208 */ 209 ANEURALNETWORKS_BAD_DATA = 4, 210 211 /** 212 * Failure caused by failed model execution. 213 */ 214 ANEURALNETWORKS_OP_FAILED = 5, 215 216 /** 217 * Failure caused by object being in the wrong state. 218 */ 219 ANEURALNETWORKS_BAD_STATE = 6, 220 221 /** 222 * Failure caused by not being able to map a file into memory. 223 * This may be caused by a file descriptor not being mappable, or an AHardwareBuffer 224 * not supported by the device. 225 * Mitigate by reading its content into memory. 226 */ 227 ANEURALNETWORKS_UNMAPPABLE = 7, 228 229 /** 230 * Failure caused by insufficient buffer size provided to a model output. 231 */ 232 ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE = 8, 233 234 /** 235 * Failure caused by a device not being available. 236 */ 237 ANEURALNETWORKS_UNAVAILABLE_DEVICE = 9, 238 239 /** 240 * Failure because a deadline could not be met for a task, but future 241 * deadlines may still be met for the same task after a short delay. 242 * 243 * Available since API level 30. 244 */ 245 ANEURALNETWORKS_MISSED_DEADLINE_TRANSIENT = 10, 246 247 /** 248 * Failure because a deadline could not be met for a task, and future 249 * deadlines will likely also not be met for the same task even after a 250 * short delay. 251 * 252 * Available since API level 30. 253 */ 254 ANEURALNETWORKS_MISSED_DEADLINE_PERSISTENT = 11, 255 256 /** 257 * Failure because of a resource limitation within the driver, but future 258 * calls for the same task may still succeed after a short delay. 259 * 260 * Available since API level 30. 261 */ 262 ANEURALNETWORKS_RESOURCE_EXHAUSTED_TRANSIENT = 12, 263 264 /** 265 * Failure because of a resource limitation within the driver, and future 266 * calls for the same task will likely also fail even after a short 267 * delay. 268 * 269 * Available since API level 30. 270 */ 271 ANEURALNETWORKS_RESOURCE_EXHAUSTED_PERSISTENT = 13, 272 273 /** 274 * Failure indicating an object is in a dead state. 275 * 276 * Available since API level 30. 277 */ 278 ANEURALNETWORKS_DEAD_OBJECT = 14, 279} ResultCode; 280 281/** 282 * For {@link ANeuralNetworksModel_setOperandValue}, values with a 283 * length smaller or equal to this will be immediately copied into 284 * the model. The size is in bytes. 285 * 286 * Available since API level 27. 287 */ 288enum { ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES = 128 }; 289 290/** 291 * For {@link ANeuralNetworksCompilation_setCaching}, specify the size 292 * of the cache token required from the application. The size is in bytes. 293 * 294 * Available since API level 29. 295 */ 296enum { ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN = 32 }; 297 298/** 299 * Different duration measurements. 300 * 301 * Durations are measured in nanoseconds. 302 * 303 * Available since API level 29. 304 */ 305typedef enum { 306 // Execution time on hardware (not driver, which runs on host processor). 307 ANEURALNETWORKS_DURATION_ON_HARDWARE = 0, 308 // Execution time in driver (including time on hardware). Excludes overhead 309 // such as that of the runtime itself and the IPC needed for the runtime to 310 // communicate with the driver. 311 ANEURALNETWORKS_DURATION_IN_DRIVER = 1, 312 // Execution time on hardware, after all dependencies have been signaled. 313 // If no dependencies specified (for example, if the execution was scheduled other 314 // than with {@link ANeuralNetworksExecution_startComputeWithDependencies}), the 315 // reported time will be the same as ANEURALNETWORKS_DURATION_ON_HARDWARE. 316 // Available since API level 30. 317 ANEURALNETWORKS_FENCED_DURATION_ON_HARDWARE = 2, 318 // Execution time in driver, after all dependencies have been signaled. Excludes 319 // overhead such as that of the runtime itself and the IPC needed for the runtime 320 // to communicate with the driver. 321 // If no dependencies specified (for example, if the execution was scheduled other 322 // than with {@link ANeuralNetworksExecution_startComputeWithDependencies}), the 323 // reported time will be the same as ANEURALNETWORKS_DURATION_IN_DRIVER. 324 // Available since API level 30. 325 ANEURALNETWORKS_FENCED_DURATION_IN_DRIVER = 3, 326} DurationCode; 327 328/** 329 * Relative execution priority. 330 * 331 * Available since API level 30. 332 */ 333typedef enum { 334 ANEURALNETWORKS_PRIORITY_LOW = 90, 335 ANEURALNETWORKS_PRIORITY_MEDIUM = 100, 336 ANEURALNETWORKS_PRIORITY_HIGH = 110, 337 ANEURALNETWORKS_PRIORITY_DEFAULT = ANEURALNETWORKS_PRIORITY_MEDIUM, 338} PriorityCode; 339 340/** 341 * ANeuralNetworksMemory is an opaque type that represents memory. 342 * 343 * This type is used to represent shared memory, memory mapped files, 344 * and similar memories. 345 * 346 * By using shared memory, a program can efficiently communicate to the 347 * runtime and drivers the tensors that define a model. See 348 * {@link ANeuralNetworksModel_setOperandValueFromMemory}. An application 349 * should typically create one shared memory object that contains every constant tensor 350 * needed to define a model. {@link ANeuralNetworksMemory_createFromFd} can be used to 351 * create shared memory from a file handle. 352 * {@link ANeuralNetworksMemory_createFromAHardwareBuffer} can be used to 353 * create shared memory from an AHardwareBuffer handle. 354 * 355 * Memory objects can also be used to specify the input and output arguments of 356 * an execution. See {@link ANeuralNetworksExecution_setInputFromMemory} 357 * and {@link ANeuralNetworksExecution_setOutputFromMemory}. 358 * 359 * When calling {@link ANeuralNetworksModel_setOperandValueFromMemory}, 360 * {@link ANeuralNetworksExecution_setInputFromMemory} and 361 * {@link ANeuralNetworksExecution_setOutputFromMemory}, each operand in the shared 362 * memory object must be aligned on a boundary of a byte size that is a multiple 363 * of the element type byte size, e.g., a tensor with 364 * {@link ANEURALNETWORKS_TENSOR_FLOAT32} type must be aligned on 4-byte boundary. 365 * 366 * It is the application's responsibility to ensure that there are no uses of 367 * the memory after calling {@link ANeuralNetworksMemory_free}. This includes 368 * any model which references this memory because of a call to 369 * {@link ANeuralNetworksModel_setOperandValueFromMemory}, any compilation 370 * created using such a model, any execution object or burst object created 371 * using such a compilation, or any execution which references this memory 372 * because of a call to {@link ANeuralNetworksExecution_setInputFromMemory} or 373 * {@link ANeuralNetworksExecution_setOutputFromMemory}. 374 * 375 * Available since API level 27. 376 * 377 * Starting at API level 30, the application may request creation of device native memory from 378 * {@link ANeuralNetworksMemoryDesc} to avoid potential memory copying and transformation 379 * overhead between executions. See also {@link ANeuralNetworksMemoryDesc} and 380 * {@link ANeuralNetworksMemory_createFromDesc}. 381 */ 382typedef struct ANeuralNetworksMemory ANeuralNetworksMemory; 383 384/** 385 * ANeuralNetworksModel is an opaque type that contains a description of the 386 * mathematical operations that constitute the model. 387 * 388 * <p>Build the model by calling<ul> 389 * <li>{@link ANeuralNetworksModel_create}</li> 390 * <li>{@link ANeuralNetworksModel_addOperation}</li> 391 * <li>{@link ANeuralNetworksModel_addOperand}</li> 392 * </ul> 393 * 394 * This forms a graph in which each operation and operand is a node, a 395 * directed edge from an operand to an operation indicates that the 396 * operand is an input to the operation, and a directed edge from an 397 * operation to an operand indicates that the operand is an output 398 * from the operation. This graph must be acyclic. 399 * 400 * A model is completed by calling {@link ANeuralNetworksModel_finish}. 401 * A model is destroyed by calling {@link ANeuralNetworksModel_free}. 402 * 403 * <p>A model cannot be modified once {@link ANeuralNetworksModel_finish} 404 * has been called on it.</p> 405 * 406 * <p>It is the application's responsibility to make sure that only one thread 407 * modifies a model at a given time. It is however safe for more than one 408 * thread to use the model once {@link ANeuralNetworksModel_finish} has returned.</p> 409 * 410 * <p>It is also the application's responsibility to ensure that there are no 411 * other uses of the model after calling {@link ANeuralNetworksModel_free}. 412 * This includes any compilation, execution object or burst object created using 413 * the model.</p> 414 * 415 * Available since API level 27. 416 */ 417typedef struct ANeuralNetworksModel ANeuralNetworksModel; 418 419/** 420 * ANeuralNetworksCompilation is an opaque type that can be used to compile 421 * a machine learning model. 422 * 423 * <p>To use:<ul> 424 * <li>Create a new compilation instance by calling the 425 * {@link ANeuralNetworksCompilation_create} function or 426 * {@link ANeuralNetworksCompilation_createForDevices}.</li> 427 * <li>Set any desired properties on the compilation (for example, 428 * {@link ANeuralNetworksCompilation_setPreference}).</li> 429 * <li>Optionally, set the caching signature and the cache directory on the 430 * compilation by calling {@link ANeuralNetworksCompilation_setCaching}.</li> 431 * <li>Complete the compilation with {@link ANeuralNetworksCompilation_finish}.</li> 432 * <li>Use the compilation as many times as needed 433 * with {@link ANeuralNetworksExecution_create} and 434 * {@link ANeuralNetworksBurst_create}.</li> 435 * <li>Destroy the compilation with {@link ANeuralNetworksCompilation_free} 436 * once all executions using the compilation have completed.</li></ul></p> 437 * 438 * A compilation is completed by calling {@link ANeuralNetworksCompilation_finish}. 439 * A compilation is destroyed by calling {@link ANeuralNetworksCompilation_free}. 440 * 441 * <p>A compilation cannot be modified once {@link ANeuralNetworksCompilation_finish} 442 * has been called on it.</p> 443 * 444 * <p>It is the application's responsibility to make sure that only 445 * one thread modifies a compilation at a given time. It is however 446 * safe for more than one thread to use the compilation once 447 * {@link ANeuralNetworksCompilation_finish} has returned.</p> 448 * 449 * <p>It is also the application's responsibility to ensure that there are no other 450 * uses of the compilation after calling {@link ANeuralNetworksCompilation_free}. 451 * This includes any execution object or burst object created using the compilation, 452 * or any memory descriptor with the compilation as part of one of the roles specified by 453 * {@link ANeuralNetworksMemoryDesc_addInputRole} or 454 * {@link ANeuralNetworksMemoryDesc_addOutputRole}.</p> 455 * 456 * Available since API level 27. 457 */ 458typedef struct ANeuralNetworksCompilation ANeuralNetworksCompilation; 459 460/** 461 * ANeuralNetworksExecution is an opaque type that can be used to apply a machine 462 * learning model to a set of inputs. 463 * 464 * <p>To use:<ul> 465 * <li>Create a new execution instance by calling the 466 * {@link ANeuralNetworksExecution_create} function.</li> 467 * <li>Associate input buffers or memory regions to the model inputs with 468 * {@link ANeuralNetworksExecution_setInput} or 469 * {@link ANeuralNetworksExecution_setInputFromMemory}.</li> 470 * <li>Associate output buffers or memory regions to the model outputs with 471 * {@link ANeuralNetworksExecution_setOutput} or 472 * {@link ANeuralNetworksExecution_setOutputFromMemory}.</li> 473 * <li>Apply the model with one of the following:</li><ul> 474 * <li>Asynchronously with {@link ANeuralNetworksExecution_startCompute} 475 * or with {@link ANeuralNetworksExecution_startComputeWithDependencies}, 476 * waiting for the execution to complete with 477 * {@link ANeuralNetworksEvent_wait}.</li> 478 * <li>Synchronously with {@link ANeuralNetworksExecution_compute}.</li> 479 * <li>Synchronously as part of an execution burst with 480 * {@link ANeuralNetworksExecution_burstCompute}.</li></ul> 481 * <li>Destroy the execution with 482 * {@link ANeuralNetworksExecution_free}.</li></ul></p> 483 * 484 * <p>An output buffer or memory region must not overlap with any 485 * other output buffer or memory region, with an input buffer or 486 * memory region, or with an operand value in a memory object 487 * ({@link ANeuralNetworksModel_setOperandValueFromMemory}).</p> 488 * 489 * <p>An execution cannot be modified once 490 * {@link ANeuralNetworksExecution_burstCompute}, 491 * {@link ANeuralNetworksExecution_compute}, 492 * {@link ANeuralNetworksExecution_startCompute} or 493 * {@link ANeuralNetworksExecution_startComputeWithDependencies} has been called on it.</p> 494 * 495 * <p>An execution can be applied to a model with 496 * {@link ANeuralNetworksExecution_burstCompute}, 497 * {@link ANeuralNetworksExecution_compute}, 498 * {@link ANeuralNetworksExecution_startCompute} or 499 * {@link ANeuralNetworksExecution_startComputeWithDependencies} only once. Create new 500 * executions to do new evaluations of the model.</p> 501 * 502 * <p>It is the application's responsibility to make sure that only one thread 503 * modifies an execution at a given time. It is however safe for more than one 504 * thread to use {@link ANeuralNetworksEvent_wait} at the same time.</p> 505 * 506 * <p>It is also the application's responsibility to ensure that the execution 507 * either has never been scheduled or has completed (i.e., that 508 * {@link ANeuralNetworksExecution_burstCompute}, 509 * {@link ANeuralNetworksExecution_compute}, or 510 * {@link ANeuralNetworksEvent_wait} has returned) before calling 511 * {@link ANeuralNetworksExecution_free}.</p>. 512 * 513 * <p>It is also the application's responsibility to ensure that there are no other 514 * uses of the execution after calling {@link ANeuralNetworksExecution_free}.</p> 515 * 516 * <p>Multiple executions can be scheduled and evaluated concurrently, either by 517 * means of {@link ANeuralNetworksExecution_compute} or 518 * {@link ANeuralNetworksExecution_burstCompute} (which are synchronous) in 519 * different threads, or by means of 520 * {@link ANeuralNetworksExecution_startCompute} or 521 * {@link ANeuralNetworksExecution_startComputeWithDependencies} (which are asynchronous). 522 * (Concurrent uses of {@link ANeuralNetworksExecution_burstCompute} must be on 523 * different burst objects.) The runtime makes no guarantee on the ordering of 524 * completion of executions. If it's important to the application, the 525 * application should enforce the ordering by ensuring that one execution 526 * completes before the next is scheduled (for example, by scheduling all 527 * executions synchronously within a single thread, or by scheduling all 528 * executions asynchronously and using {@link ANeuralNetworksEvent_wait} between 529 * calls to {@link ANeuralNetworksExecution_startCompute}); or by using 530 * {@link ANeuralNetworksExecution_startComputeWithDependencies} to make the execution wait for a 531 * list of events to be signaled before starting the actual evaluation.</p> 532 * 533 * Available since API level 27. 534 */ 535typedef struct ANeuralNetworksExecution ANeuralNetworksExecution; 536 537#if __ANDROID_API__ >= 29 538/** 539 * Parameters for ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL operand. 540 */ 541typedef struct ANeuralNetworksSymmPerChannelQuantParams { 542 /* The index of the channel dimension. */ 543 uint32_t channelDim; 544 /** The size of the scale array. Should be equal to dimension[channelDim] of the Operand. */ 545 uint32_t scaleCount; 546 /** The array of scaling values for each channel. Each value must be greater than zero. */ 547 const float* scales; 548} ANeuralNetworksSymmPerChannelQuantParams; 549 550/** 551 * ANeuralNetworksBurst is an opaque type that can be used to reduce the latency 552 * of a rapid sequence of executions. It will likely cause overhead if only used 553 * for a single execution. 554 * 555 * ANeuralNetworksBurst serves as a context object for any number of inferences 556 * using {@link ANeuralNetworksExecution} objects. An ANeuralNetworksBurst 557 * object and the {@link ANeuralNetworksExecution} objects used with it must all 558 * have been created from the same {@link ANeuralNetworksCompilation} object. 559 * 560 * This object is also used as a hint to drivers, providing insight to the 561 * lifetime of a rapid sequence of executions. For example, a driver may choose 562 * to increase the clock frequency of its accelerator for the lifetime of a 563 * burst object. 564 * 565 * <p>To use:<ul> 566 * <li>Create a new burst object by calling the 567 * {@link ANeuralNetworksBurst_create} function.</li> 568 * <li>For each execution:</li><ul> 569 * <li>Create {@link ANeuralNetworksExecution} and configure its 570 * properties (see {@link ANeuralNetworksExecution} for details).</li> 571 * <li>Apply the model synchronously with 572 * {@link ANeuralNetworksExecution_burstCompute}, reusing the same 573 * {@link ANeuralNetworksBurst} with the new 574 * {@link ANeuralNetworksExecution}.</li> 575 * <li>Use and free the {@link ANeuralNetworksExecution}.</li></ul> 576 * <li>Destroy the burst with 577 * {@link ANeuralNetworksBurst_free}.</li></ul></p> 578 * 579 * Available since API level 29. 580 */ 581typedef struct ANeuralNetworksBurst ANeuralNetworksBurst; 582#endif // __ANDROID_API__ >= 29 583 584/** 585 * ANeuralNetworksOperandType describes the type of an operand. 586 * 587 * This structure is used to describe both scalars and tensors. 588 * 589 * A tensor operand type with all dimensions specified is "fully 590 * specified". Whenever possible (i.e., whenever the dimensions are 591 * known at model construction time), a tensor operand type should be 592 * (but is not required to be) fully specified, in order to enable the 593 * best possible performance. 594 * 595 * If a tensor operand's type is not fully specified, the dimensions 596 * of the operand are deduced from the operand types and values of the 597 * operation for which that operand is an output or from the corresponding 598 * {@link ANEURALNETWORKS_IF} or {@link ANEURALNETWORKS_WHILE} operation input 599 * operand type in the case of referenced model input operands. 600 * 601 * <p>In the following situations, a tensor operand type must be fully 602 * specified:<ul> 603 * <li>The operand has a constant value, set by 604 * {@link ANeuralNetworksModel_setOperandValue} (with a 605 * non-nullptr buffer) or 606 * {@link ANeuralNetworksModel_setOperandValueFromMemory}.</li> 607 * <li>The operand is a model input (see 608 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}) of the main 609 * model within a compilation. A fully specified tensor operand type 610 * must either be provided to {@link ANeuralNetworksModel_addOperand}; 611 * or it must be provided to the corresponding 612 * {@link ANeuralNetworksExecution_setInput}, or 613 * {@link ANeuralNetworksExecution_setInputFromMemory}. 614 * EXCEPTION: If the input is optional and omitted 615 * (by passing nullptr for buffer to 616 * {@link ANeuralNetworksExecution_setInput}) then it need 617 * not have a fully specified tensor operand type.</li> 618 * <li>The operand is a model output (see 619 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}) of the main 620 * model within a compilation and is to be used with {@link 621 * ANeuralNetworksExecution_startComputeWithDependencies}. 622 * A fully specified tensor operand type must either be provided 623 * to {@link ANeuralNetworksModel_addOperand}; or it must be 624 * provided to the corresponding 625 * {@link ANeuralNetworksExecution_setOutput}, or 626 * {@link ANeuralNetworksExecution_setOutputFromMemory}.</li></ul> 627 * 628 * A tensor operand type of specified rank but some number of 629 * unspecified dimensions is represented by setting dimensionCount to 630 * the rank and each unspecified dimension to 0. 631 * 632 * Available since API level 27. 633 * 634 * Starting at API level 29, a tensor operand type of unspecified rank is 635 * represented by setting dimensionCount to 0 and dimensions to NULL (just as if 636 * it were a scalar operand type). 637 */ 638typedef struct ANeuralNetworksOperandType { 639 /** 640 * The data type, e.g ANEURALNETWORKS_FLOAT32. 641 */ 642 int32_t type; 643 644 /** 645 * The number of dimensions (rank). 646 * 647 * Must be 0 for scalars. 648 */ 649 uint32_t dimensionCount; 650 651 /** 652 * The dimensions of the tensor. 653 * 654 * Must be nullptr for scalars. 655 */ 656 const uint32_t* dimensions; 657 658 /** 659 * The quantization scale. 660 * 661 * Must be 0 when not applicable to an operand type. 662 * 663 * See {@link OperandCode}. 664 */ 665 float scale; 666 667 /** 668 * The quantization zero point. 669 * 670 * Must be 0 when not applicable to an operand type. 671 * 672 * See {@link OperandCode}. 673 */ 674 int32_t zeroPoint; 675} ANeuralNetworksOperandType; 676 677typedef int32_t ANeuralNetworksOperationType; 678 679/** 680 * ANeuralNetworksEvent is an opaque type that represents an event 681 * that will be signaled once an execution completes. 682 * 683 * Available since API level 27. 684 */ 685typedef struct ANeuralNetworksEvent ANeuralNetworksEvent; 686 687#if __ANDROID_API__ >= 29 688 689/** 690 * ANeuralNetworksDevice is an opaque type that represents a device. 691 * 692 * This type is used to query basic properties and supported operations of the corresponding 693 * device, and control which device(s) a model is to be run on. 694 * 695 * Available since API level 29. 696 */ 697typedef struct ANeuralNetworksDevice ANeuralNetworksDevice; 698 699#endif // __ANDROID_API__ >= 29 700 701#if __ANDROID_API__ >= 30 702 703/** 704 * ANeuralNetworksMemoryDesc is an opaque type that represents a memory descriptor. 705 * 706 * A memory descriptor describes the properties of a memory object, and is used by 707 * {@link ANeuralNetworksMemory_createFromDesc}. 708 * 709 * To use: 710 * - Create a new memory descriptor by calling {@link ANeuralNetworksMemoryDesc_create}. 711 * - Specify all of the intended input and output roles by calling 712 * {@link ANeuralNetworksMemoryDesc_addInputRole} and 713 * {@link ANeuralNetworksMemoryDesc_addOutputRole}. 714 * - Optionally, specify the memory dimensions by calling 715 * {@link ANeuralNetworksMemoryDesc_setDimensions}. 716 * - Complete the memory descriptor with {@link ANeuralNetworksMemoryDesc_finish}. 717 * - Use the memory descriptor as many times as needed with 718 * {@link ANeuralNetworksMemory_createFromDesc}. 719 * - Destroy the memory descriptor with {@link ANeuralNetworksMemoryDesc_free}. 720 * 721 * A memory descriptor is completed by calling {@link ANeuralNetworksMemoryDesc_finish}. 722 * A memory descriptor is destroyed by calling {@link ANeuralNetworksMemoryDesc_free}. 723 * 724 * A memory descriptor must not be modified once {@link ANeuralNetworksMemoryDesc_finish} 725 * has been called on it. 726 * 727 * It is the application's responsibility to make sure that only 728 * one thread modifies a memory descriptor at a given time. It is however 729 * safe for more than one thread to use the memory descriptor once 730 * {@link ANeuralNetworksMemoryDesc_finish} has returned. 731 * 732 * It is also the application's responsibility to ensure that there are no other 733 * uses of the memory descriptor after calling {@link ANeuralNetworksMemoryDesc_free}. 734 * It is however safe to continue using a {@link ANeuralNetworksMemory} object created 735 * from the memory descriptor. 736 * 737 * Available since API level 30. 738 */ 739typedef struct ANeuralNetworksMemoryDesc ANeuralNetworksMemoryDesc; 740 741/** 742 * Create a {@link ANeuralNetworksMemoryDesc} with no properties. 743 * 744 * This only creates the memory descriptor. Its properties should be set with calls to 745 * {@link ANeuralNetworksMemoryDesc_addInputRole}, 746 * {@link ANeuralNetworksMemoryDesc_addOutputRole}, and 747 * {@link ANeuralNetworksMemoryDesc_setDimensions}. 748 * 749 * {@link ANeuralNetworksMemoryDesc_finish} must be called once all properties have been set. 750 * 751 * {@link ANeuralNetworksMemoryDesc_free} must be called once the memory descriptor 752 * is no longer needed. 753 * 754 * Available since API level 30. 755 * 756 * @param desc The {@link ANeuralNetworksMemoryDesc} to be created. 757 * Set to NULL if unsuccessful. 758 * 759 * @return ANEURALNETWORKS_NO_ERROR if successful. 760 */ 761int ANeuralNetworksMemoryDesc_create(ANeuralNetworksMemoryDesc** desc) __INTRODUCED_IN(30); 762 763/** 764 * Destroy a memory descriptor. 765 * 766 * The memory descriptor need not have been finished by a call to 767 * {@link ANeuralNetworksMemoryDesc_finish}. 768 * 769 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded usage. 770 * 771 * Available since API level 30. 772 * 773 * @param desc The memory descriptor to be destroyed. Passing NULL is acceptable and 774 * results in no operation. 775 */ 776void ANeuralNetworksMemoryDesc_free(ANeuralNetworksMemoryDesc* desc) __INTRODUCED_IN(30); 777 778/** 779 * Specify that a memory object will be playing the role of an input to an execution created from a 780 * particular compilation. 781 * 782 * The compilation and the input index fully specify an input operand. This function 783 * may be invoked multiple times on the same memory descriptor with different input operands, 784 * and the same input operand may be specified on multiple memory descriptors. However, 785 * specifying the same input operand on the same memory descriptor more than once will 786 * return an error. 787 * 788 * The dimensions of the corresponding model operands of all the roles specified by 789 * {@link ANeuralNetworksMemoryDesc_addInputRole} and 790 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be compatible with each other. Two 791 * dimensions are incompatible if both ranks are fully specified but have different values, or if 792 * there is at least one axis that is fully specified in both but has different values. 793 * 794 * At least one of {@link ANeuralNetworksMemoryDesc_addInputRole} and 795 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be called on a memory descriptor 796 * before invoking {@link ANeuralNetworksMemoryDesc_finish}. 797 * 798 * Attempting to modify a memory descriptor once {@link ANeuralNetworksMemoryDesc_finish} has been 799 * called will return an error. 800 * 801 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded usage. 802 * 803 * Available since API level 30. 804 * 805 * @param desc The memory descriptor to be modified. 806 * @param compilation The compilation object. It must already have been finished by calling 807 * {@link ANeuralNetworksCompilation_finish}, and must outlive the memory 808 * descriptor. 809 * @param index The index of the input argument we are referencing from the compilation. It is 810 * an index into the inputs list passed to 811 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not 812 * the index associated with {@link ANeuralNetworksModel_addOperand}. 813 * @param frequency A floating-point value within the range (0.0, 1.0]. Describes how likely the 814 * memory is to be used in the specified role. This is provided as a hint to 815 * optimize the case when different roles prefer different memory locations or data 816 * layouts. 817 * 818 * @return ANEURALNETWORKS_NO_ERROR if successful. 819 */ 820int ANeuralNetworksMemoryDesc_addInputRole(ANeuralNetworksMemoryDesc* desc, 821 const ANeuralNetworksCompilation* compilation, 822 uint32_t index, float frequency) __INTRODUCED_IN(30); 823 824/** 825 * Specify that a memory object will be playing the role of an output to an execution created from a 826 * particular compilation. 827 * 828 * The compilation and the output index fully specify an output operand. This function 829 * may be invoked multiple times on the same memory descriptor with different output operands, 830 * and the same output operand may be specified on multiple memory descriptors. However, 831 * specifying the same output operand on the same memory descriptor object more than once will 832 * return an error. 833 * 834 * The dimensions of the corresponding model operands of all the roles specified by 835 * {@link ANeuralNetworksMemoryDesc_addInputRole} and 836 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be compatible with each other. Two 837 * dimensions are incompatible if both ranks are fully specified but have different values, or if 838 * there is at least one axis that is fully specified in both but has different values. 839 * 840 * At least one of {@link ANeuralNetworksMemoryDesc_addInputRole} and 841 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be called on the memory descriptor 842 * before invoking {@link ANeuralNetworksMemoryDesc_finish}. 843 * 844 * Attempting to modify a memory descriptor once {@link ANeuralNetworksMemoryDesc_finish} has been 845 * called will return an error. 846 * 847 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded usage. 848 * 849 * Available since API level 30. 850 * 851 * @param desc The memory descriptor to be modified. 852 * @param compilation The compilation object. It must already have been finished by calling 853 * {@link ANeuralNetworksCompilation_finish}, and must outlive the memory 854 * descriptor. 855 * @param index The index of the output argument we are referencing from the compilation. It is 856 * an index into the outputs list passed to 857 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not 858 * the index associated with {@link ANeuralNetworksModel_addOperand}. 859 * @param frequency A floating-point value within the range (0.0, 1.0]. Describes how likely the 860 * memory is to be used in the specified role. This is provided as a hint to 861 * optimize the case when multiple roles prefer different memory locations or data 862 * layouts. 863 * 864 * @return ANEURALNETWORKS_NO_ERROR if successful. 865 */ 866int ANeuralNetworksMemoryDesc_addOutputRole(ANeuralNetworksMemoryDesc* desc, 867 const ANeuralNetworksCompilation* compilation, 868 uint32_t index, float frequency) __INTRODUCED_IN(30); 869 870/** 871 * Set the dimensional information of the memory descriptor. 872 * 873 * The specified dimensions must be compatible with the dimensions of the corresponding model 874 * operands of all the roles specified by {@link ANeuralNetworksMemoryDesc_addInputRole} and 875 * {@link ANeuralNetworksMemoryDesc_addOutputRole}. Two dimensions are incompatible if both ranks 876 * are fully specified but have different values, or if there is at least one axis that is fully 877 * specified in both but has different values. 878 * 879 * Attempting to modify a memory descriptor once {@link ANeuralNetworksMemoryDesc_finish} has been 880 * called will return an error. 881 * 882 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded usage. 883 * 884 * Available since API level 30. 885 * 886 * @param desc The memory descriptor to be modified. 887 * @param rank The number of dimensions. Must be 0 for scalars. 888 * @param dimensions An array of dimensions. An entry with the value 0 indicates that the 889 * corresponding axis has an unknown size. 890 * 891 * @return ANEURALNETWORKS_NO_ERROR if successful. 892 */ 893int ANeuralNetworksMemoryDesc_setDimensions(ANeuralNetworksMemoryDesc* desc, uint32_t rank, 894 const uint32_t* dimensions) __INTRODUCED_IN(30); 895 896/** 897 * Indicate that we have finished modifying a memory descriptor. Required before calling 898 * {@link ANeuralNetworksMemory_createFromDesc}. 899 * 900 * This function must only be called once for a given memory descriptor. 901 * 902 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded usage. 903 * 904 * Available since API level 30. 905 * 906 * @param desc The memory descriptor to be finished. 907 * 908 * @return ANEURALNETWORKS_NO_ERROR if successful. 909 */ 910int ANeuralNetworksMemoryDesc_finish(ANeuralNetworksMemoryDesc* desc) __INTRODUCED_IN(30); 911 912/** 913 * Creates a memory object from a memory descriptor. 914 * 915 * The memory object is created with an uninitialized buffer. A memory object with an uninitialized 916 * buffer may only be used according to the roles specified by {@link 917 * ANeuralNetworksMemoryDesc_addOutputRole}, or as the destination memory in {@link 918 * ANeuralNetworksMemory_copy}. The buffer of a memory object is initialized after the memory object 919 * is used as an output in a successful execution, or used as the destination memory in a successful 920 * {@link ANeuralNetworksMemory_copy}. A memory object with an initialized buffer may be used 921 * according to all roles specified in {@link ANeuralNetworksMemoryDesc}, or as the source or 922 * destination memory in {@link ANeuralNetworksMemory_copy}. The buffer of a memory object will 923 * return to the uninitialized state if the memory object is used as an output in a failed 924 * execution, or used as the destination memory in a failed {@link ANeuralNetworksMemory_copy}. 925 * 926 * The dimensions of the memory descriptor are deduced from the dimensions of the corresponding 927 * model operands of all the roles specified by {@link ANeuralNetworksMemoryDesc_addInputRole} and 928 * {@link ANeuralNetworksMemoryDesc_addOutputRole}, as well as the dimensions set by the call to 929 * {@link ANeuralNetworksMemoryDesc_setDimensions}, if any. The memory descriptor may have 930 * unspecified dimensions or rank. In such a case, the same memory object may be used with different 931 * shapes of outputs in different executions. When the memory is used as an input, the input shape 932 * must be the same as the output shape from the last execution using this memory object as an 933 * output, or the last {@link ANeuralNetworkMemory_copy} using this memory object as the destination 934 * memory. Creating a memory object with unspecified dimensions or rank may fail for certain sets of 935 * roles. 936 * 937 * Using the memory in roles or shapes that are not compatible with the rules specified above will 938 * return an error. 939 * 940 * When calling {@link ANeuralNetworksExecution_setInputFromMemory} or 941 * {@link ANeuralNetworksExecution_setOutputFromMemory} with the memory object, 942 * both offset and length must be set to zero and the entire memory region will be 943 * associated with the specified input or output operand. 944 * 945 * Calling {@link ANeuralNetworksModel_setOperandValueFromMemory} with the memory created from this 946 * function will return an error. 947 * 948 * {@link ANeuralNetworksMemory_free} must be called once the memory is no longer needed. 949 * 950 * Attempting to create memory from an unfinished memory descriptor will return an error. 951 * 952 * The provided {@link ANeuralNetworksMemoryDesc} need not outlive the {@link ANeuralNetworksMemory} 953 * object. 954 * 955 * Available since API level 30. 956 * 957 * @param desc The memory descriptor. 958 * @param memory The memory object to be created. 959 * Set to NULL if unsuccessful. 960 * 961 * @return ANEURALNETWORKS_NO_ERROR if successful; ANEURALNETWORKS_OP_FAILED if the memory is 962 * created with unspecified dimensions or rank and it is not supported for this set of 963 * roles. 964 */ 965int ANeuralNetworksMemory_createFromDesc(const ANeuralNetworksMemoryDesc* desc, 966 ANeuralNetworksMemory** memory) __INTRODUCED_IN(30); 967 968/** 969 * Copies data from one memory object to another. 970 * 971 * If at most one of the src and dst is created from {@link ANeuralNetworksMemory_createFromDesc}, 972 * the src and dst must have the same logical size: 973 * - If the memory is created from {@link ANeuralNetworksMemory_createFromFd}, or if it is created 974 * from {@link ANeuralNetworksMemory_createFromAHardwareBuffer} with format of 975 * AHARDWAREBUFFER_FORMAT_BLOB, the logical size equals the size of the memory. 976 * - If the memory is created from {@link ANeuralNetworksMemory_createFromAHardwareBuffer} with a 977 * format other than AHARDWAREBUFFER_FORMAT_BLOB, the logical size equals the size when there is 978 * no padding and the data is tightly packed. This function may fail if the AHardwareBuffer 979 * cannot be accessed. 980 * - If the memory is created from {@link ANeuralNetworksMemory_createFromDesc}, the logical size 981 * equals the size indicated by the {@link OperandCode} multiplied by the number of elements. This 982 * function will fail if the number of elements is unknown. 983 * 984 * If both src and dst are created from {@link ANeuralNetworksMemory_createFromDesc}, they must have 985 * compatible dimensions. Two dimensions are incompatible if both ranks are fully specified but 986 * have different values, or if there is at least one axis that is fully specified in both but has 987 * different values. The dst may have unspecified dimensions or rank. In such a case, the dimensions 988 * of dst will get updated according to the dimensions of the src. 989 * 990 * In both cases, if the src is created from {@link ANeuralNetworksMemory_createFromDesc}, it must 991 * have been used as an output in a successful execution, or used as the destination memory in a 992 * successful {@link ANeuralNetworksMemory_copy}. 993 * 994 * The src and dst may have different data layout, in which case the data copying is performed 995 * logically with data layout transformation. 996 * 997 * Available since API level 30. 998 * 999 * @param src The source memory object. 1000 * @param dst The destination memory object. 1001 * 1002 * @return ANEURALNETWORKS_NO_ERROR if successful. 1003 */ 1004int ANeuralNetworksMemory_copy(const ANeuralNetworksMemory* src, const ANeuralNetworksMemory* dst) 1005 __INTRODUCED_IN(30); 1006 1007#endif // __ANDROID_API__ >= 30 1008 1009#if __ANDROID_API__ >= 29 1010 1011/** 1012 * Get the number of available devices. 1013 * 1014 * @param numDevices Used to return the number of devices. 1015 * 1016 * @return ANEURALNETWORKS_NO_ERROR if successful. 1017 * 1018 * Available since API level 29. 1019 */ 1020int ANeuralNetworks_getDeviceCount(uint32_t* numDevices) __INTRODUCED_IN(29); 1021 1022/** 1023 * Get the representation of the specified device. 1024 * 1025 * @param devIndex The index of the specified device. Must be less than the 1026 number of available devices. 1027 * @param device The representation of the specified device. 1028 * The same representation will always be returned for the specified 1029 * device. 1030 * 1031 * @return ANEURALNETWORKS_NO_ERROR if successful. 1032 * 1033 * Available since API level 29. 1034 */ 1035int ANeuralNetworks_getDevice(uint32_t devIndex, ANeuralNetworksDevice** device) 1036 __INTRODUCED_IN(29); 1037 1038/** 1039 * Get the name of the specified device. 1040 * 1041 * @param device The representation of the specified device. 1042 * @param name The returned name of the specified device. The name will be in UTF-8 1043 * and will be null-terminated. It will be recognizable as a known device name 1044 * rather than a cryptic string. For devices with feature level reported by 1045 * {@link ANeuralNetworksDevice_getFeatureLevel} that is 29 and above, the 1046 * format of the name is {VENDOR}-{DEVICE}. For devices with feature level 28 1047 * or lower, the format of the name is undefined. 1048 * The name will remain valid for the duration of the application. 1049 * 1050 * @return ANEURALNETWORKS_NO_ERROR if successful. 1051 * 1052 * Available since API level 29. 1053 */ 1054int ANeuralNetworksDevice_getName(const ANeuralNetworksDevice* device, const char** name) 1055 __INTRODUCED_IN(29); 1056 1057/** 1058 * Get the type of a given device. 1059 * 1060 * The device type can be used to help application developers to distribute Machine Learning 1061 * workloads and other workloads such as graphical rendering. 1062 * E.g., for an app which renders AR scenes based on real time object detection results, 1063 * the developer could choose an ACCELERATOR type device for ML workloads, and reserve GPU 1064 * for graphical rendering. 1065 * 1066 * @param device The representation of the specified device. 1067 * @param type The returned {@link DeviceTypeCode} of the specified device. 1068 * 1069 * @return ANEURALNETWORKS_NO_ERROR if successful. 1070 * 1071 * Available since API level 29. 1072 */ 1073int ANeuralNetworksDevice_getType(const ANeuralNetworksDevice* device, int32_t* type) 1074 __INTRODUCED_IN(29); 1075 1076/** 1077 * Get the version of the driver implementation of the specified device. 1078 * 1079 * It’s the responsibility of the driver implementor to insure that this version string 1080 * uniquely distinguishes this implementation from all previous implementations. 1081 * 1082 * This version string must not be confused with the feature level which is solely defined 1083 * by {@link ANeuralNetworksDevice_getFeatureLevel}. There is no implicit ordering of the versions. 1084 * For example, it is not possible to filter all drivers older than a certain version. 1085 * 1086 * Application developers may use this version string to avoid or prefer specific driver 1087 * implementations. For example, an application may want to do so because: 1088 * - A specific version of the driver does not provide the required performance, 1089 * perhaps because of a performance regression. 1090 * - A specific version of the driver has a bug or returns results that don’t match 1091 * the minimum precision requirement for the application. 1092 * 1093 * @param device The representation of the specified device. 1094 * @param version The returned version string of the driver for the specified device. The 1095 * string will be in UTF-8 and will be null-terminated. For devices with feature 1096 * level 28 or lower, "UNKNOWN" will be returned. The version string will remain 1097 * valid for the duration of the application. 1098 * 1099 * @return ANEURALNETWORKS_NO_ERROR if successful. 1100 * 1101 * Available since API level 29. 1102 */ 1103int ANeuralNetworksDevice_getVersion(const ANeuralNetworksDevice* device, const char** version) 1104 __INTRODUCED_IN(29); 1105 1106/** 1107 * Get the supported NNAPI version of the specified device. 1108 * 1109 * Each device has a supported feature level, which is the most advanced feature this driver 1110 * implements. For example, if the driver implements the features introduced in Android P, 1111 * but does not implement the features introduced after Android P, the value would be 28. 1112 * Developers could decide whether or not the specified device should be used for a Model that 1113 * has certain feature requirements. 1114 * 1115 * @param device The representation of the specified device. 1116 * @param featureLevel The API level of the most advanced feature this driver implements. 1117 * 1118 * @return ANEURALNETWORKS_NO_ERROR if successful. 1119 * 1120 * Available since API level 29. 1121 */ 1122int ANeuralNetworksDevice_getFeatureLevel(const ANeuralNetworksDevice* device, 1123 int64_t* featureLevel) __INTRODUCED_IN(29); 1124 1125#if __ANDROID_API__ >= 30 1126 1127/** 1128 * Wait until the device is in a live state. 1129 * 1130 * A device may encounter internal errors and temporarily enter a dead state. A 1131 * call that uses a device in such a state will return with the error 1132 * {@link ANEURALNETWORKS_DEAD_OBJECT}. ANeuralNetworksDevice_wait will block until 1133 * the device is in a live state. 1134 * 1135 * @param device The representation of the specified device. 1136 * 1137 * @return ANEURALNETWORKS_NO_ERROR if successful. 1138 * 1139 * Available since API level 30. 1140 */ 1141int ANeuralNetworksDevice_wait(const ANeuralNetworksDevice* device) __INTRODUCED_IN(30); 1142 1143#endif // __ANDROID_API__ >= 30 1144 1145/** 1146 * Get the supported operations for a specified set of devices. If multiple devices 1147 * are selected, the supported operation list is a union of supported operations of all 1148 * selected devices. 1149 * 1150 * @param model The model to be queried. 1151 * @param devices The set of devices. Must not contain duplicates. 1152 * @param numDevices The number of devices in the set. 1153 * @param supportedOps The boolean array to be filled. True means supported. The size of the 1154 * boolean array must be at least as large as the number of operations 1155 * in the model. The order of elements in the supportedOps array matches 1156 * the order in which the corresponding operations were added to the model. 1157 * 1158 * @return ANEURALNETWORKS_NO_ERROR if successful. 1159 * 1160 * Available since API level 29. 1161 */ 1162int ANeuralNetworksModel_getSupportedOperationsForDevices( 1163 const ANeuralNetworksModel* model, const ANeuralNetworksDevice* const* devices, 1164 uint32_t numDevices, bool* supportedOps) __INTRODUCED_IN(29); 1165 1166/** 1167 * Create a {@link ANeuralNetworksCompilation} to compile the given model for a specified set 1168 * of devices. If more than one device is specified, the compilation will 1169 * distribute the workload automatically across the devices. The model must be fully 1170 * supported by the specified set of devices. This means that 1171 * ANeuralNetworksModel_getSupportedOperationsForDevices() must have returned true for every 1172 * operation for that model/devices pair. 1173 * 1174 * The user must handle all compilation and execution failures from the 1175 * specified set of devices. This is in contrast to a use of {@link 1176 * ANeuralNetworksCompilation_create}, where the runtime will attempt to recover 1177 * from such failures. 1178 * 1179 * The model passed to this function is termed the "main model" of the 1180 * compilation, to distinguish it from other models referred to by an Operand 1181 * of type {@link ANEURALNETWORKS_MODEL} within this compilation. 1182 * 1183 * @param model The {@link ANeuralNetworksModel} to be compiled. 1184 * @param devices The set of devices. Must not contain duplicates. 1185 * @param numDevices The number of devices in the set. 1186 * @param compilation The newly created object or NULL if unsuccessful. 1187 * 1188 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA 1189 * if the model is invalid. 1190 * 1191 * Available since API level 29. 1192 */ 1193int ANeuralNetworksCompilation_createForDevices(ANeuralNetworksModel* model, 1194 const ANeuralNetworksDevice* const* devices, 1195 uint32_t numDevices, 1196 ANeuralNetworksCompilation** compilation) 1197 __INTRODUCED_IN(29); 1198 1199/** 1200 * Sets the compilation caching signature and the cache directory. 1201 * 1202 * Provides optional caching information to the runtime for faster repeated 1203 * compilation. 1204 * 1205 * See {@link ANeuralNetworksCompilation} for information on multithreaded usage. 1206 * 1207 * @param compilation The compilation to be modified. 1208 * @param cacheDir The cache directory for the runtime to store and retrieve caching 1209 * data. It is recommended to use the code cache directory provided 1210 * by the Android runtime. If not using the code cache directory, the 1211 * user should choose a directory local to the application, and is 1212 * responsible for managing the cache entries. 1213 * @param token The token provided by the user to specify a model must be of length 1214 * ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN. The user should ensure that 1215 * the token is unique to a model within the application. The NNAPI 1216 * runtime cannot detect token collisions; a collision will result in a 1217 * failed execution or in a successful execution that produces incorrect 1218 * output values. 1219 * 1220 * @return ANEURALNETWORKS_NO_ERROR if successful. 1221 * 1222 * Available since API level 29. 1223 */ 1224int ANeuralNetworksCompilation_setCaching(ANeuralNetworksCompilation* compilation, 1225 const char* cacheDir, const uint8_t* token) 1226 __INTRODUCED_IN(29); 1227 1228/** 1229 * Schedule synchronous evaluation of the execution. 1230 * 1231 * <p>Schedules synchronous evaluation of the execution. Returns once the 1232 * execution has completed and the outputs are ready to be consumed. 1233 * </p> 1234 * 1235 * If {@link ANeuralNetworksExecution_setTimeout} was called on this execution, 1236 * and the execution is not able to complete before the timeout duration is 1237 * exceeded, then execution may be aborted, in which case 1238 * {@link ANEURALNETWORKS_MISSED_DEADLINE_*} will be returned. If the device has 1239 * a feature level reported by {@link ANeuralNetworksDevice_getFeatureLevel} 1240 * that is lower than 30, then the timeout duration hint will be ignored. 1241 * 1242 * If this execution contains a {@link ANEURALNETWORKS_WHILE} operation, and 1243 * the condition model does not output false within the loop timeout duration, 1244 * then execution will be aborted and {@link ANEURALNETWORKS_MISSED_DEADLINE_*} 1245 * will be returned. 1246 * 1247 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 1248 * 1249 * See {@link ANeuralNetworksExecution_burstCompute} for burst synchronous execution. 1250 * See {@link ANeuralNetworksExecution_startCompute} for regular asynchronous execution. 1251 * See {@link ANeuralNetworksExecution_startComputeWithDependencies} for 1252 * asynchronous execution with dependencies. 1253 * 1254 * Available since API level 29. 1255 * 1256 * @param execution The execution to be scheduled and executed. 1257 * 1258 * @return ANEURALNETWORKS_NO_ERROR if the execution completed normally. 1259 * ANEURALNETWORKS_UNMAPPABLE if the execution input or output memory cannot 1260 * be properly mapped. 1261 */ 1262int ANeuralNetworksExecution_compute(ANeuralNetworksExecution* execution) __INTRODUCED_IN(29); 1263 1264/** 1265 * Get the dimensional information of the specified output operand of the model of the 1266 * {@link ANeuralNetworksExecution}. 1267 * 1268 * The execution must have completed. On asynchronous execution initiated by 1269 * {@link ANeuralNetworksExecution_startCompute} or 1270 * {@link ANeuralNetworksExecution_startComputeWithDependencies}, 1271 * {@link ANeuralNetworksEvent_wait} must be called prior to this function. 1272 * 1273 * @param execution The execution to be queried. 1274 * @param index The index of the output argument we are querying. It is 1275 * an index into the lists passed to 1276 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not 1277 * the index associated with {@link ANeuralNetworksModel_addOperand}. 1278 * @param rank The rank of the output operand. 1279 * 1280 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE 1281 * if the target output is provided an insufficient buffer at execution time, 1282 * ANEURALNETWORKS_BAD_DATA if the index is invalid. 1283 * 1284 * Available since API level 29. 1285 */ 1286int ANeuralNetworksExecution_getOutputOperandRank(ANeuralNetworksExecution* execution, 1287 int32_t index, uint32_t* rank) 1288 __INTRODUCED_IN(29); 1289 1290/** 1291 * Get the dimensional information of the specified output operand of the model of the 1292 * {@link ANeuralNetworksExecution}. The target output operand cannot be a scalar. 1293 * 1294 * The execution must have completed. On asynchronous execution initiated by 1295 * {@link ANeuralNetworksExecution_startCompute} or 1296 * {@link ANeuralNetworksExecution_startComputeWithDependencies}, 1297 * {@link ANeuralNetworksEvent_wait} must be called prior to this function. 1298 * 1299 * @param execution The execution to be queried. 1300 * @param index The index of the output argument we are querying. It is an index into the lists 1301 * passed to {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not 1302 * the index associated with {@link ANeuralNetworksModel_addOperand}. 1303 * @param dimensions The dimension array to be filled. The size of the array must be exactly as 1304 * large as the rank of the output operand to be queried in the model. 1305 * 1306 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE 1307 * if the target output is provided an insufficient buffer at execution time, 1308 * ANEURALNETWORKS_BAD_DATA if the index is invalid or if the target is a scalar. 1309 * 1310 * Available since API level 29. 1311 */ 1312int ANeuralNetworksExecution_getOutputOperandDimensions(ANeuralNetworksExecution* execution, 1313 int32_t index, uint32_t* dimensions) 1314 __INTRODUCED_IN(29); 1315 1316/** 1317 * Create a {@link ANeuralNetworksBurst} to apply the given compilation. 1318 * This only creates the burst object. Computation is only performed once 1319 * {@link ANeuralNetworksExecution_burstCompute} is invoked with a valid 1320 * {@link ANeuralNetworksExecution} and {@link ANeuralNetworksBurst}. 1321 * 1322 * <p>The provided compilation must outlive the burst object.</p> 1323 * 1324 * Available since API level 29. 1325 * 1326 * @param compilation The {@link ANeuralNetworksCompilation} to be evaluated. 1327 * @param burst The newly created object or NULL if unsuccessful. 1328 * 1329 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA 1330 * if the compilation is invalid. 1331 */ 1332int ANeuralNetworksBurst_create(ANeuralNetworksCompilation* compilation, 1333 ANeuralNetworksBurst** burst) __INTRODUCED_IN(29); 1334 1335/** 1336 * Destroys the burst object. 1337 * 1338 * Available since API level 29. 1339 * 1340 * @param burst The burst object to be destroyed. Passing NULL is acceptable and 1341 * results in no operation. 1342 */ 1343void ANeuralNetworksBurst_free(ANeuralNetworksBurst* burst) __INTRODUCED_IN(29); 1344 1345/** 1346 * Schedule synchronous evaluation of the execution on a burst object. 1347 * 1348 * <p>Schedules synchronous evaluation of the execution. Returns once the 1349 * execution has completed and the outputs are ready to be consumed.</p> 1350 * 1351 * If {@link ANeuralNetworksExecution_setTimeout} was called on the execution, 1352 * and the execution is not able to complete before the timeout duration is 1353 * exceeded, then execution may be aborted, in which case 1354 * {@link ANEURALNETWORKS_MISSED_DEADLINE_*} will be returned. 1355 * 1356 * If the execution contains a {@link ANEURALNETWORKS_WHILE} operation, and 1357 * the condition model does not output false within the loop timeout duration, 1358 * then execution will be aborted and {@link ANEURALNETWORKS_MISSED_DEADLINE_*} 1359 * will be returned. If the device has a feature level reported by 1360 * {@link ANeuralNetworksDevice_getFeatureLevel} that is lower than 30, then the 1361 * timeout duration hint will be ignored. 1362 * 1363 * <p>There must be at most one {@link ANeuralNetworksExecution} processing at 1364 * any given time for any given burst object. Any 1365 * {@link ANeuralNetworksExecution} launched before the previous has finished 1366 * will result in ANEURALNETWORKS_BAD_STATE.</p> 1367 * 1368 * See {@link ANeuralNetworksExecution_compute} for synchronous execution. 1369 * See {@link ANeuralNetworksExecution_startCompute} for regular asynchronous execution. 1370 * See {@link ANeuralNetworksExecution_startComputeWithDependencies} for 1371 * asynchronous execution with dependencies. 1372 * 1373 * Available since API level 29. 1374 * 1375 * @param burst The burst object to execute on. 1376 * @param execution The execution to be scheduled and executed. The execution 1377 * must be created from the same {@link 1378 * ANeuralNetworksCompilation} as the burst object. 1379 * 1380 * @return ANEURALNETWORKS_NO_ERROR if the execution completed normally. 1381 */ 1382int ANeuralNetworksExecution_burstCompute(ANeuralNetworksExecution* execution, 1383 ANeuralNetworksBurst* burst) __INTRODUCED_IN(29); 1384 1385/** 1386 * Creates a shared memory object from an AHardwareBuffer handle. 1387 * 1388 * If the shared memory is backed by an AHardwareBuffer of AHARDWAREBUFFER_FORMAT_BLOB 1389 * format, it can be used the same way as shared memory created from a file handle. See 1390 * {@link ANeuralNetworksMemory} for a description on how to use this shared memory. 1391 * 1392 * If the shared memory is backed by an AHardwareBuffer of a format other than 1393 * AHARDWAREBUFFER_FORMAT_BLOB, it can only be used for Model inputs and outputs. 1394 * When calling {@link ANeuralNetworksExecution_setInputFromMemory} or 1395 * {@link ANeuralNetworksExecution_setOutputFromMemory} with the shared memory, both 1396 * offset and length must be set to zero and the entire memory region will be 1397 * associated with the specified input or output operand. There is no guarantee 1398 * that an arbitrary AHardwareBuffer_Format and AHardwareBuffer_UsageFlags combination 1399 * can be used by arbitrary devices. The execution will fail if the selected set of 1400 * devices cannot consume the buffer. 1401 * 1402 * Calling {@link ANeuralNetworksModel_setOperandValueFromMemory} with shared memory 1403 * backed by an AHardwareBuffer of a format other than AHARDWAREBUFFER_FORMAT_BLOB is 1404 * disallowed. 1405 * 1406 * The provided AHardwareBuffer must outlive the ANeuralNetworksMemory object. 1407 * 1408 * Available since API level 29. 1409 * 1410 * @param ahwb The AHardwareBuffer handle. 1411 * @param memory The memory object to be created. 1412 * Set to NULL if unsuccessful. 1413 * 1414 * @return ANEURALNETWORKS_NO_ERROR if the request completed normally. 1415 * 1416 * @see AHardwareBuffer 1417 */ 1418int ANeuralNetworksMemory_createFromAHardwareBuffer(const AHardwareBuffer* ahwb, 1419 ANeuralNetworksMemory** memory) 1420 __INTRODUCED_IN(29); 1421 1422/** 1423 1424 * Specifies whether duration of the {@link ANeuralNetworksExecution} is to be 1425 * measured. Evaluation of the execution must not have been scheduled. 1426 * 1427 * By default, duration is not measured. 1428 * 1429 * The {@link ANeuralNetworksExecution} must have been created from an 1430 * {@link ANeuralNetworksCompilation} which in turn was created from 1431 * {@link ANeuralNetworksCompilation_createForDevices} with numDevices = 1. 1432 * If the device has a feature level reported by 1433 * {@link ANeuralNetworksDevice_getFeatureLevel} that is lower than 29, then the 1434 * duration will not be measured. 1435 * 1436 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 1437 * 1438 * Available since API level 29. 1439 * 1440 * @param execution The execution to be modified. 1441 * @param measure 'true' if duration is to be measured, 'false' if not. 1442 * 1443 * @return ANEURALNETWORKS_NO_ERROR if successful. 1444 */ 1445int ANeuralNetworksExecution_setMeasureTiming(ANeuralNetworksExecution* execution, bool measure) 1446 __INTRODUCED_IN(29); 1447 1448/** 1449 * Get the time spent in the specified {@link ANeuralNetworksExecution}, in nanoseconds. 1450 * 1451 * The execution must have completed. On asynchronous execution initiated by 1452 * {@link ANeuralNetworksExecution_startCompute} or 1453 * {@link ANeuralNetworksExecution_startComputeWithDependencies}, 1454 * {@link ANeuralNetworksEvent_wait} must be called prior to this function. 1455 * 1456 * @param execution The execution to be queried. 1457 * @param durationCode The measurement to be queried, specified by {@link DurationCode}. 1458 * @param duration The returned duration. If no measurement was requested by 1459 * {@link ANeuralNetworksExecution_setMeasureTiming}, if the 1460 * device is has a feature level reported by 1461 * {@link ANeuralNetworksDevice_getFeatureLevel} that is lower 1462 * than 29, or for some other reason the duration is not 1463 * available, UINT64_MAX will be returned. A particular device 1464 * need not support any given measurement. 1465 * 1466 * @return ANEURALNETWORKS_NO_ERROR if successful. 1467 * 1468 * Available since API level 29. 1469 */ 1470int ANeuralNetworksExecution_getDuration(const ANeuralNetworksExecution* execution, 1471 int32_t durationCode, uint64_t* duration) 1472 __INTRODUCED_IN(29); 1473 1474#endif // __ANDROID_API__ >= 29 1475 1476#if __ANDROID_API__ >= 27 1477 1478/** 1479 * Creates a shared memory object from a file descriptor. 1480 * 1481 * The shared memory is backed by a file descriptor via mmap. 1482 * See {@link ANeuralNetworksMemory} for a description on how to use 1483 * this shared memory. 1484 * 1485 * Available since API level 27. 1486 * 1487 * @param size The requested size in bytes. 1488 * Must not be larger than the file size. 1489 * @param prot The desired memory protection for the mapping. 1490 * It is either PROT_NONE or the bitwise OR of one or 1491 * more of the following flags: PROT_READ, PROT_WRITE. 1492 * @param fd The requested file descriptor. 1493 * The file descriptor has to be mmap-able. The file 1494 * descriptor will be duplicated. 1495 * @param offset The offset to the beginning of the file of the area to map. 1496 * The offset has to be aligned to a page size. 1497 * @param memory The memory object to be created. 1498 * Set to NULL if unsuccessful. 1499 * 1500 * @return ANEURALNETWORKS_NO_ERROR if the request completed normally. 1501 */ 1502int ANeuralNetworksMemory_createFromFd(size_t size, int protect, int fd, size_t offset, 1503 ANeuralNetworksMemory** memory) __INTRODUCED_IN(27); 1504 1505/** 1506 * Delete a memory object. 1507 * 1508 * Destroys the object used by the run time to keep track of the memory. 1509 * This will free the underlying actual memory if no other code has open 1510 * handles to this memory. 1511 * 1512 * Available since API level 27. 1513 * 1514 * @param memory The memory object to be freed. Passing NULL is acceptable and 1515 * results in no operation. 1516 */ 1517void ANeuralNetworksMemory_free(ANeuralNetworksMemory* memory) __INTRODUCED_IN(27); 1518 1519/** 1520 * Create an empty {@link ANeuralNetworksModel}. 1521 * 1522 * <p>This only creates the object. Computation is performed once 1523 * {@link ANeuralNetworksExecution_burstCompute}, 1524 * {@link ANeuralNetworksExecution_compute}, 1525 * {@link ANeuralNetworksExecution_startCompute} or 1526 * {@link ANeuralNetworksExecution_startComputeWithDependencies} is invoked. 1527 * 1528 * The model should be constructed with calls to 1529 * {@link ANeuralNetworksModel_addOperation} and 1530 * {@link ANeuralNetworksModel_addOperand} 1531 * 1532 * <p>{@link ANeuralNetworksModel_finish} should be called once the model 1533 * has been fully constructed.</p> 1534 * 1535 * <p>{@link ANeuralNetworksModel_free} should be called once the model 1536 * is no longer needed.</p> 1537 * 1538 * Available since API level 27. 1539 * 1540 * @param model The {@link ANeuralNetworksModel} to be created. 1541 * Set to NULL if unsuccessful. 1542 * 1543 * @return ANEURALNETWORKS_NO_ERROR if successful. 1544 */ 1545int ANeuralNetworksModel_create(ANeuralNetworksModel** model) __INTRODUCED_IN(27); 1546 1547/** 1548 * Destroy a model. 1549 * 1550 * The model need not have been finished by a call to 1551 * {@link ANeuralNetworksModel_finish}. 1552 * 1553 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 1554 * 1555 * Available since API level 27. 1556 * 1557 * @param model The model to be destroyed. Passing NULL is acceptable and 1558 * results in no operation. 1559 */ 1560void ANeuralNetworksModel_free(ANeuralNetworksModel* model) __INTRODUCED_IN(27); 1561 1562/** 1563 * Indicate that we have finished modifying a model. Required before 1564 * calling {@link ANeuralNetworksCompilation_create} and 1565 * {@link ANeuralNetworksCompilation_createForDevices}. 1566 * 1567 * An application must ensure that no other thread uses the model at the same 1568 * time. 1569 * 1570 * This function must only be called once for a given model. 1571 * 1572 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 1573 * 1574 * Available since API level 27. 1575 * 1576 * @param model The model to be finished. 1577 * 1578 * @return ANEURALNETWORKS_NO_ERROR if successful. 1579 */ 1580int ANeuralNetworksModel_finish(ANeuralNetworksModel* model) __INTRODUCED_IN(27); 1581 1582/** 1583 * Add an operand to a model. 1584 * 1585 * The order in which the operands are added is important. The first one added 1586 * to a model will have the index value 0, the second 1, etc. These indexes are 1587 * used as operand identifiers in 1588 * {@link ANeuralNetworksModel_addOperation}, 1589 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}, 1590 * {@link ANeuralNetworksModel_setOperandValue}, 1591 * {@link ANeuralNetworksModel_setOperandValueFromMemory}, 1592 * {@link ANeuralNetworksExecution_setInput}, 1593 * {@link ANeuralNetworksExecution_setInputFromMemory}, 1594 * {@link ANeuralNetworksExecution_setOutput}, 1595 * {@link ANeuralNetworksExecution_setOutputFromMemory} and 1596 * {@link ANeuralNetworksExecution_setOperandValue}. 1597 * 1598 * <p>Every operand must be referenced in exactly one of the following 1599 * ways:<ul> 1600 * <li>It is identified as a model input with 1601 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}.</li> 1602 * <li>It is identified as a constant with 1603 * {@link ANeuralNetworksModel_setOperandValue} or 1604 * {@link ANeuralNetworksModel_setOperandValueFromMemory}.</li> 1605 * <li>It is identified as an output of exactly one operation with 1606 * {@link ANeuralNetworksModel_addOperation}.</li></p> 1607 * <p>An operand that is identified as a model input or as a constant 1608 * must not also be identified as a model output with 1609 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}.</p> 1610 * 1611 * To build a model that can accommodate inputs of various sizes, as 1612 * you may want to do for a CNN, leave unspecified the dimensions that 1613 * will vary at run time. If you do so, fully specify dimensions 1614 * when calling {@link ANeuralNetworksExecution_setInput} or 1615 * {@link ANeuralNetworksExecution_setInputFromMemory}. 1616 * 1617 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has been 1618 * called will return an error. 1619 * 1620 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 1621 * 1622 * Available since API level 27. 1623 * 1624 * @param model The model to be modified. 1625 * @param type The {@link ANeuralNetworksOperandType} that describes the shape 1626 * of the operand. Neither the {@link ANeuralNetworksOperandType} 1627 * nor the dimensions it points to need to outlive the call to 1628 * {@link ANeuralNetworksModel_addOperand}. 1629 * 1630 * @return ANEURALNETWORKS_NO_ERROR if successful. 1631 */ 1632int ANeuralNetworksModel_addOperand(ANeuralNetworksModel* model, 1633 const ANeuralNetworksOperandType* type) __INTRODUCED_IN(27); 1634 1635/** 1636 * Sets an operand to a constant value. 1637 * 1638 * Values of length smaller or equal to 1639 * {@link ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES} 1640 * are immediately copied into the model. 1641 * 1642 * For values of length greater than 1643 * {@link ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES}, a pointer to 1644 * the buffer is stored within the model. The application must not change the 1645 * content of this region until all executions using this model have 1646 * completed. As the data may be copied during processing, modifying the data 1647 * after this call yields undefined results. The provided buffer must outlive 1648 * this model. 1649 * 1650 * For large tensors, using {@link ANeuralNetworksModel_setOperandValueFromMemory} 1651 * is likely to be more efficient. 1652 * 1653 * To indicate that an optional operand should be considered missing, 1654 * pass nullptr for buffer and 0 for length. 1655 * 1656 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has been 1657 * called will return an error. 1658 * 1659 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 1660 * 1661 * Available since API level 27. 1662 * 1663 * @param model The model to be modified. 1664 * @param index The index of the model operand we're setting. 1665 * @param buffer A pointer to the data to use. 1666 * @param length The size in bytes of the data value. 1667 * 1668 * @return ANEURALNETWORKS_NO_ERROR if successful. 1669 */ 1670int ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel* model, int32_t index, 1671 const void* buffer, size_t length) __INTRODUCED_IN(27); 1672 1673#if __ANDROID_API__ >= 29 1674 1675/** 1676 * Sets an operand's per channel quantization parameters. 1677 * 1678 * Sets parameters required by a tensor of type 1679 * {@link ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL}. 1680 * This function must be called for every tensor of type 1681 * {@link ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL} before 1682 * calling {@link ANeuralNetworksModel_finish}. 1683 * 1684 * Available since API level 29. 1685 * 1686 * @param model The model to be modified. 1687 * @param index The index of the model operand we're setting. 1688 * @param channelQuant The per channel quantization parameters for the operand. 1689 * No memory in this struct needs to outlive the call to 1690 * this function. 1691 * 1692 * @return ANEURALNETWORKS_NO_ERROR if successful. 1693 */ 1694int ANeuralNetworksModel_setOperandSymmPerChannelQuantParams( 1695 ANeuralNetworksModel* model, int32_t index, 1696 const ANeuralNetworksSymmPerChannelQuantParams* channelQuant) __INTRODUCED_IN(29); 1697 1698#endif // __ANDROID_API__ >= 29 1699 1700/** 1701 * Sets an operand to a value stored in a memory object. 1702 * 1703 * The content of the memory is not copied. A reference to that memory is stored 1704 * inside the model. The application must not change the content of the memory 1705 * region until all executions using this model have completed. As the data may 1706 * be copied during processing, modifying the data after this call yields 1707 * undefined results. 1708 * 1709 * <p>The provided memory must outlive this model.</p> 1710 * 1711 * To indicate that an optional operand should be considered missing, 1712 * use {@link ANeuralNetworksModel_setOperandValue} instead, passing nullptr for buffer. 1713 * 1714 * It is disallowed to set an operand value with shared memory backed by an AHardwareBuffer 1715 * of a format other than AHARDWAREBUFFER_FORMAT_BLOB. 1716 * 1717 * It is disallowed to set an operand value with memory created from 1718 * {@link ANeuralNetworksMemory_createFromDesc}. 1719 * 1720 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has been 1721 * called will return an error. 1722 * 1723 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 1724 * See {@link ANeuralNetworksMemory_createFromAHardwareBuffer} for information on 1725 * AHardwareBuffer usage. 1726 * 1727 * Available since API level 27. 1728 * 1729 * @param model The model to be modified. 1730 * @param index The index of the model operand we're setting. 1731 * @param buffer A pointer to the data to use. 1732 * @param memory The memory containing the data. 1733 * @param offset This specifies the location of the data within the memory. 1734 * The offset is in bytes from the start of memory. 1735 * @param length The size in bytes of the data value. 1736 * 1737 * @return ANEURALNETWORKS_NO_ERROR if successful. 1738 */ 1739int ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel* model, int32_t index, 1740 const ANeuralNetworksMemory* memory, 1741 size_t offset, size_t length) 1742 __INTRODUCED_IN(27); 1743 1744#if __ANDROID_API__ >= 30 1745 1746/** 1747 * Sets an operand to a value that is a reference to another NNAPI model. 1748 * 1749 * The referenced model must already have been finished by a call to 1750 * {@link ANeuralNetworksModel_finish}. 1751 * 1752 * The {@link ANeuralNetworksModel_relaxComputationFloat32toFloat16} setting of 1753 * referenced models is overridden by that setting of the main model of a 1754 * compilation. 1755 * 1756 * The referenced model must outlive the model referring to it. 1757 * 1758 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has 1759 * been called will return an error. 1760 * 1761 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 1762 * 1763 * Available since API level 30. 1764 * 1765 * @param model The model to be modified. 1766 * @param index The index of the model operand we're setting. 1767 * @param value The model to be referenced. 1768 * 1769 * @return ANEURALNETWORKS_NO_ERROR if successful. 1770 */ 1771int ANeuralNetworksModel_setOperandValueFromModel(ANeuralNetworksModel* model, int32_t index, 1772 const ANeuralNetworksModel* value) 1773 __INTRODUCED_IN(30); 1774 1775#endif // __ANDROID_API__ >= 30 1776 1777/** 1778 * Add an operation to a model. 1779 * 1780 * @param model The model to be modified. 1781 * @param type The {@link ANeuralNetworksOperationType} of the operation. 1782 * @param inputCount The number of entries in the inputs array. 1783 * @param inputs An array of indexes identifying each operand. 1784 * @param outputCount The number of entries in the outputs array. 1785 * @param outputs An array of indexes identifying each operand. 1786 * 1787 * The operands specified by inputs and outputs must have been 1788 * previously added by calls to {@link ANeuralNetworksModel_addOperand}. 1789 * 1790 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has been 1791 * called will return an error. 1792 * 1793 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 1794 * 1795 * Available since API level 27. 1796 * 1797 * @return ANEURALNETWORKS_NO_ERROR if successful. 1798 */ 1799int ANeuralNetworksModel_addOperation(ANeuralNetworksModel* model, 1800 ANeuralNetworksOperationType type, uint32_t inputCount, 1801 const uint32_t* inputs, uint32_t outputCount, 1802 const uint32_t* outputs) __INTRODUCED_IN(27); 1803 1804/** 1805 * Specifies which operands will be the model's inputs and 1806 * outputs. Every model must have at least one input and one output. 1807 * 1808 * An operand cannot be used for both input and output. Doing so will 1809 * return an error. 1810 * 1811 * @param model The model to be modified. 1812 * @param inputCount The number of entries in the inputs array. 1813 * @param inputs An array of indexes identifying the input operands. 1814 * @param outputCount The number of entries in the outputs array. 1815 * @param outputs An array of indexes identifying the output operands. 1816 * 1817 * The operands specified by inputs and outputs must have been 1818 * previously added by calls to {@link ANeuralNetworksModel_addOperand}. 1819 * 1820 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has been 1821 * called will return an error. 1822 * 1823 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 1824 * 1825 * Available since API level 27. 1826 * 1827 */ 1828int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel* model, uint32_t inputCount, 1829 const uint32_t* inputs, uint32_t outputCount, 1830 const uint32_t* outputs) __INTRODUCED_IN(27); 1831 1832#if __ANDROID_API__ >= 28 1833 1834/** 1835 * Specifies whether {@link ANEURALNETWORKS_TENSOR_FLOAT32} is allowed to be 1836 * calculated with range and/or precision as low as that of the IEEE 754 16-bit 1837 * floating-point format. By default, {@link ANEURALNETWORKS_TENSOR_FLOAT32} 1838 * must be calculated using at least the range and precision of the IEEE 754 1839 * 32-bit floating-point format. 1840 * 1841 * The relaxComputationFloat32toFloat16 setting of the main model of 1842 * a compilation overrides the values of the referenced models. 1843 * 1844 * @param model The model to be modified. 1845 * @param allow 'true' indicates {@link ANEURALNETWORKS_TENSOR_FLOAT32} may be 1846 * calculated with range and/or precision as low as that of the 1847 * IEEE 754 16-bit floating point format. 'false' indicates 1848 * {@link ANEURALNETWORKS_TENSOR_FLOAT32} must be calculated using 1849 * at least the range and precision of the IEEE 754 32-bit floating 1850 * point format. 1851 * 1852 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has been 1853 * called will return an error. 1854 * 1855 * Available since API level 28. 1856 * 1857 * See {@link ANeuralNetworksModel} for information on multithreaded usage. 1858 */ 1859int ANeuralNetworksModel_relaxComputationFloat32toFloat16(ANeuralNetworksModel* model, bool allow) 1860 __INTRODUCED_IN(28); 1861 1862#endif // __ANDROID_API__ >= 28 1863 1864/** 1865 * Create a {@link ANeuralNetworksCompilation} to compile the given model. 1866 * 1867 * The model passed to this function is termed the "main model" of the 1868 * compilation, to distinguish it from other models referred to by an Operand 1869 * of type {@link ANEURALNETWORKS_MODEL} within this compilation. 1870 * 1871 * <p>This function only creates the object. Compilation is only performed once 1872 * {@link ANeuralNetworksCompilation_finish} is invoked.</p> 1873 * 1874 * <p>{@link ANeuralNetworksCompilation_finish} should be called once 1875 * all desired properties have been set on the compilation.</p> 1876 * 1877 * <p>{@link ANeuralNetworksModel_free} should be called once the compilation 1878 * is no longer needed.</p> 1879 * 1880 * <p>The provided model must outlive the compilation.</p> 1881 * 1882 * The model must already have been finished by a call to 1883 * {@link ANeuralNetworksModel_finish}. 1884 * 1885 * See {@link ANeuralNetworksCompilation} for information on multithreaded usage. 1886 * 1887 * Available since API level 27. 1888 * 1889 * @param model The {@link ANeuralNetworksModel} to be compiled. 1890 * @param compilation The newly created object or NULL if unsuccessful. 1891 * 1892 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA 1893 * if the model is invalid. 1894 */ 1895int ANeuralNetworksCompilation_create(ANeuralNetworksModel* model, 1896 ANeuralNetworksCompilation** compilation) __INTRODUCED_IN(27); 1897 1898/** 1899 * Destroy a compilation. 1900 * 1901 * The compilation need not have been finished by a call to 1902 * {@link ANeuralNetworksCompilation_finish}. 1903 * 1904 * See {@link ANeuralNetworksCompilation} for information on multithreaded usage. 1905 * 1906 * Available since API level 27. 1907 * 1908 * @param compilation The compilation to be destroyed. Passing NULL is acceptable and 1909 * results in no operation. 1910 */ 1911void ANeuralNetworksCompilation_free(ANeuralNetworksCompilation* compilation) __INTRODUCED_IN(27); 1912 1913/** 1914 * Sets the execution preference. 1915 * 1916 * <p>Provides guidance to the runtime when trade-offs are possible. By default the runtime 1917 * uses PREFER_SINGLE_FAST_ANSWER</p> 1918 * 1919 * See {@link ANeuralNetworksCompilation} for information on multithreaded usage. 1920 * 1921 * Available since API level 27. 1922 * 1923 * @param compilation The compilation to be modified. 1924 * @param preference Either {@link PREFER_LOW_POWER}, 1925 * {@link PREFER_SINGLE_FAST_ANSWER}, or 1926 * {@link PREFER_SUSTAINED_SPEED}. 1927 * 1928 * @return ANEURALNETWORKS_NO_ERROR if successful. 1929 */ 1930int ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation* compilation, 1931 int32_t preference) __INTRODUCED_IN(27); 1932 1933/** 1934 * Indicate that we have finished modifying a compilation. Required before 1935 * calling {@link ANeuralNetworksBurst_create} or 1936 * {@link ANeuralNetworksExecution_create}. 1937 * 1938 * An application must ensure that no other thread uses the compilation at the 1939 * same time. 1940 * 1941 * This function must only be called once for a given compilation. 1942 * 1943 * If {@link ANeuralNetworksCompilation_setTimeout} was called on this 1944 * compilation, and the compilation is not able to be finished before the 1945 * timeout duration is exceeded, then compilation may be aborted, in which case 1946 * {@link ANEURALNETWORKS_MISSED_DEADLINE_*} will be returned. 1947 * 1948 * See {@link ANeuralNetworksCompilation} for information on multithreaded usage. 1949 * 1950 * Available since API level 27. 1951 * 1952 * @param compilation The compilation to be finished. 1953 * 1954 * @return ANEURALNETWORKS_NO_ERROR if successful. 1955 */ 1956int ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation* compilation) __INTRODUCED_IN(27); 1957 1958#if __ANDROID_API__ >= 30 1959 1960/** 1961 * Set the execution priority. 1962 * 1963 * Execution priorities are relative to other executions created by the same 1964 * application (specifically same uid) for the same device. Specifically, 1965 * priorities of executions from one application will not affect executions from 1966 * another application. Similarly, priorities of executions on one device will 1967 * not affect executions on another device. 1968 * 1969 * Higher priority executions may use more compute resources than lower priority 1970 * executions, and may preempt or starve lower priority executions. 1971 * 1972 * See {@link ANeuralNetworksCompilation} for information on multithreaded usage. 1973 * 1974 * Available since API level 30. 1975 * 1976 * @param compilation The compilation to be modified. 1977 * @param priority The relative priority of the execution compared to other 1978 * executions created by the application. Must be one of 1979 * ANEURALNETWORKS_PRIORITY_*. 1980 * 1981 * @return ANEURALNETWORKS_NO_ERROR if successful. 1982 */ 1983int ANeuralNetworksCompilation_setPriority(ANeuralNetworksCompilation* compilation, int priority) 1984 __INTRODUCED_IN(30); 1985 1986/** 1987 * Set the maximum expected duration for compiling the model. 1988 * 1989 * If the device is not able to complete the compilation within the specified 1990 * duration, the compilation may be aborted. The timeout duration begins at the 1991 * call to {@link ANeuralNetworksCompilation_finish}. 1992 * 1993 * This timeout duration acts as a hint to drivers, and can be used to both free 1994 * up compute resources within the driver and return control back to the 1995 * application quicker than is possible without the hint. It enables drivers 1996 * that are able to estimate how long a compilation will take to abort the 1997 * compilation before it has even started if the driver believes the compilation 1998 * cannot be completed within the timeout duration. Similarly, it enables 1999 * drivers to abort an ongoing compilation if it is taking too long. However, 2000 * this call does not guarantee that the compilation will complete or abort 2001 * within the timeout duration. 2002 * 2003 * By default (i.e., unless ANeuralNetworksCompilation_setTimeout is called), 2004 * the timeout duration for compiling the model is considered infinite. 2005 * 2006 * The {@link ANeuralNetworksCompilation} must have been created with 2007 * {@link ANeuralNetworksCompilation_createForDevices} with numDevices = 1, 2008 * otherwise this function will fail with ANEURALNETWORKS_BAD_DATA. If the 2009 * device has a feature level reported by 2010 * {@link ANeuralNetworksDevice_getFeatureLevel} that is lower than 30, then the 2011 * timeout duration hint will be ignored. 2012 * 2013 * See {@link ANeuralNetworksCompilation} for information on multithreaded usage. 2014 * 2015 * @param compilation The compilation to be modified. 2016 * @param duration The maximum amount of time in nanoseconds that is expected to 2017 * be spent finishing a compilation. If this duration is exceeded, the 2018 * compilation may be aborted. If set to 0, the timeout duration is 2019 * considered infinite. 2020 * 2021 * @return ANEURALNETWORKS_NO_ERROR if successful. 2022 * 2023 * Available since API level 30. 2024 */ 2025int ANeuralNetworksCompilation_setTimeout(ANeuralNetworksCompilation* compilation, 2026 uint64_t duration) __INTRODUCED_IN(30); 2027 2028#endif // __ANDROID_API__ >= 30 2029 2030/** 2031 * Create a {@link ANeuralNetworksExecution} to apply the given compilation. 2032 * This only creates the object. Computation is only performed once 2033 * {@link ANeuralNetworksExecution_burstCompute}, 2034 * {@link ANeuralNetworksExecution_compute}, 2035 * {@link ANeuralNetworksExecution_startCompute} or 2036 * {@link ANeuralNetworksExecution_startComputeWithDependencies} is invoked. 2037 * 2038 * <p>The provided compilation must outlive the execution.</p> 2039 * 2040 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2041 * 2042 * Available since API level 27. 2043 * 2044 * @param compilation The {@link ANeuralNetworksCompilation} to be evaluated. 2045 * @param execution The newly created object or NULL if unsuccessful. 2046 * 2047 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA 2048 * if the compilation is invalid. 2049 */ 2050int ANeuralNetworksExecution_create(ANeuralNetworksCompilation* compilation, 2051 ANeuralNetworksExecution** execution) __INTRODUCED_IN(27); 2052 2053/** 2054 * Destroy an execution. 2055 * 2056 * <p>The execution need not have been scheduled by a call to 2057 * {@link ANeuralNetworksExecution_burstCompute}, 2058 * {@link ANeuralNetworksExecution_compute}, 2059 * {@link ANeuralNetworksExecution_startCompute} or 2060 * {@link ANeuralNetworksExecution_startComputeWithDependencies}; but if it has been scheduled, 2061 * then the application must not call {@link ANeuralNetworksExecution_free} 2062 * until the execution has completed (i.e., 2063 * {@link ANeuralNetworksExecution_burstCompute}, 2064 * {@link ANeuralNetworksExecution_compute}, or 2065 * {@link ANeuralNetworksEvent_wait} has returned). 2066 * 2067 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2068 * 2069 * Available since API level 27. 2070 * 2071 * @param execution The execution to be destroyed. Passing NULL is acceptable and 2072 * results in no operation. 2073 */ 2074void ANeuralNetworksExecution_free(ANeuralNetworksExecution* execution) __INTRODUCED_IN(27); 2075 2076/** 2077 * Associate a user buffer with an input of the model of the 2078 * {@link ANeuralNetworksExecution}. Evaluation of the execution must not have 2079 * been scheduled. Once evaluation of the execution has been scheduled, the 2080 * application must not change the content of the buffer until the execution has 2081 * completed. Evaluation of the execution will not change the content of the 2082 * buffer. 2083 * 2084 * <p>The provided buffer must outlive the execution.</p> 2085 * 2086 * If the input is optional, you can indicate that it is omitted by 2087 * passing nullptr for buffer and 0 for length. 2088 * 2089 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2090 * 2091 * Available since API level 27. 2092 * 2093 * @param execution The execution to be modified. 2094 * @param index The index of the input argument we are setting. It is 2095 * an index into the lists passed to 2096 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not 2097 * the index associated with 2098 * {@link ANeuralNetworksModel_addOperand}. 2099 * @param type The {@link ANeuralNetworksOperandType} of the 2100 * operand. Unless the input is omitted, this should be 2101 * used to specify the dimensions that were left 2102 * unspecified when the operand was added to the 2103 * model. All other properties of the type must be the 2104 * same as specified in the model. If the type is the same 2105 * as specified when the model was built, NULL can be 2106 * passed. Neither the {@link ANeuralNetworksOperandType} 2107 * nor the dimensions it points to need to outlive the call 2108 * to {@link ANeuralNetworksExecution_setInput}. 2109 * @param buffer The buffer containing the data. 2110 * @param length The length in bytes of the buffer. 2111 * 2112 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if the 2113 * name is not recognized or the buffer is too small for the input. 2114 */ 2115int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution* execution, int32_t index, 2116 const ANeuralNetworksOperandType* type, const void* buffer, 2117 size_t length) __INTRODUCED_IN(27); 2118 2119/** 2120 * Associate a region of a memory object with an input of the model of the 2121 * {@link ANeuralNetworksExecution}. Evaluation of the execution must not have 2122 * been scheduled. Once evaluation of the execution has been scheduled, the 2123 * application must not change the content of the region until the execution has 2124 * completed. Evaluation of the execution will not change the content of the 2125 * region. 2126 * 2127 * <p>The provided memory must outlive the execution.</p> 2128 * 2129 * If the input is optional, you can indicate that it is omitted by 2130 * using {@link ANeuralNetworksExecution_setInput} instead, passing nullptr for 2131 * buffer and 0 for length. 2132 * 2133 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2134 * See {@link ANeuralNetworksMemory_createFromAHardwareBuffer} for information on 2135 * AHardwareBuffer usage. 2136 * See {@link ANeuralNetworksMemory_createFromDesc} for information on usage of memory objects 2137 * created from memory descriptors. 2138 * 2139 * Available since API level 27. 2140 * 2141 * @param execution The execution to be modified. 2142 * @param index The index of the input argument we are setting. It is 2143 * an index into the lists passed to 2144 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not 2145 * the index associated with {@link ANeuralNetworksModel_addOperand}. 2146 * @param type The {@link ANeuralNetworksOperandType} of the 2147 * operand. This should be used to specify the dimensions 2148 * that were left unspecified when the operand was added 2149 * to the model. All other properties of the type must be 2150 * the same as specified in the model. If the type is the 2151 * same as specified when the model was built, NULL can be 2152 * passed. Neither the {@link ANeuralNetworksOperandType} 2153 * nor the dimensions it points to need to outlive the call 2154 * to {@link ANeuralNetworksExecution_setInputFromMemory}. 2155 * @param memory The memory containing the data. 2156 * @param offset This specifies the location of the data within the memory. 2157 * The offset is in bytes from the start of memory. 2158 * @param length The size in bytes of the data value. 2159 * 2160 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if the 2161 * name is not recognized or the buffer is too small for the input. 2162 */ 2163int ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution* execution, int32_t index, 2164 const ANeuralNetworksOperandType* type, 2165 const ANeuralNetworksMemory* memory, size_t offset, 2166 size_t length) __INTRODUCED_IN(27); 2167 2168/** 2169 * Associate a user buffer with an output of the model of the 2170 * {@link ANeuralNetworksExecution}. Evaluation of the execution must not have 2171 * been scheduled. Once evaluation of the execution has been scheduled, the 2172 * application must not change the content of the buffer until the execution has 2173 * completed. 2174 * 2175 * If the output is optional, you can indicate that it is omitted by 2176 * passing nullptr for buffer and 0 for length. 2177 * 2178 * <p>The provided buffer must outlive the execution.</p> 2179 * 2180 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2181 * 2182 * Available since API level 27. 2183 * 2184 * @param execution The execution to be modified. 2185 * @param index The index of the output argument we are setting. It is 2186 * an index into the lists passed to 2187 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not 2188 * the index associated with {@link ANeuralNetworksModel_addOperand}. 2189 * @param type The {@link ANeuralNetworksOperandType} of the 2190 * operand. Unless the output is omitted, this should be 2191 * used to specify the dimensions that were left 2192 * unspecified when the operand was added to the 2193 * model. All other properties of the type must be the 2194 * same as specified in the model. If the type is the same 2195 * as specified when the model was built, NULL can be 2196 * passed. Neither the {@link ANeuralNetworksOperandType} 2197 * nor the dimensions it points to need to outlive the call 2198 * to {@link ANeuralNetworksExecution_setOutput}. 2199 * Since API level 29, the output operand can have unspecified 2200 * dimensions or rank to be deduced dynamically during the execution. 2201 * However, the user must provide a large enough buffer. The user 2202 * can retrieve the output dimensional information after the execution 2203 * by {@link ANeuralNetworksExecution_getOutputOperandRank} and 2204 * {@link ANeuralNetworksExecution_getOutputOperandDimensions}. 2205 * @param buffer The buffer where the data is to be written. 2206 * @param length The length in bytes of the buffer. 2207 * 2208 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if the 2209 * name is not recognized or the buffer is too small for the output. 2210 */ 2211int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution* execution, int32_t index, 2212 const ANeuralNetworksOperandType* type, void* buffer, 2213 size_t length) __INTRODUCED_IN(27); 2214 2215/** 2216 * Associate a region of a memory object with an output of the model of the 2217 * {@link ANeuralNetworksExecution}. Evaluation of the execution must not have 2218 * been scheduled. Once evaluation of the execution has been scheduled, the 2219 * application must not change the content of the region until the execution has 2220 * completed. 2221 * 2222 * If the output is optional, you can indicate that it is omitted by 2223 * using {@link ANeuralNetworksExecution_setOutput} instead, passing nullptr for 2224 * buffer and 0 for length. 2225 * 2226 * <p>The provided memory must outlive the execution.</p> 2227 * 2228 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2229 * See {@link ANeuralNetworksMemory_createFromAHardwareBuffer} for information on 2230 * AHardwareBuffer usage. 2231 * See {@link ANeuralNetworksMemory_createFromDesc} for information on usage of memory objects 2232 * created from memory descriptors. 2233 * 2234 * Available since API level 27. 2235 * 2236 * @param execution The execution to be modified. 2237 * @param index The index of the output argument we are setting. It is 2238 * an index into the lists passed to 2239 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not 2240 * the index associated with {@link ANeuralNetworksModel_addOperand}. 2241 * @param type The {@link ANeuralNetworksOperandType} of the operand. This should be 2242 * used to specify the dimensions that were left 2243 * unspecified when the operand was added to the 2244 * model. All other properties of the type must be the 2245 * same as specified in the model. If the type is the same 2246 * as specified when the model was built, NULL can be 2247 * passed. Neither the {@link ANeuralNetworksOperandType} 2248 * nor the dimensions it points to need to outlive the call 2249 * to {@link ANeuralNetworksExecution_setOutputFromMemory}. 2250 * Since API level 29, the output operand can have unspecified 2251 * dimensions or rank to be deduced dynamically during the execution. 2252 * However, the user must provide a large enough memory. The user 2253 * can retrieve the output dimensional information after the execution 2254 * by {@link ANeuralNetworksExecution_getOutputOperandRank} and 2255 * {@link ANeuralNetworksExecution_getOutputOperandDimensions}. 2256 * @param memory The memory where the data is to be stored. 2257 * @param offset This specifies the location of the data within the memory. 2258 * The offset is in bytes from the start of memory. 2259 * @param length The length in bytes of the data value. 2260 * 2261 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if the 2262 * name is not recognized or the buffer is too small for the output. 2263 */ 2264int ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution* execution, int32_t index, 2265 const ANeuralNetworksOperandType* type, 2266 const ANeuralNetworksMemory* memory, size_t offset, 2267 size_t length) __INTRODUCED_IN(27); 2268 2269/** 2270 * Schedule asynchronous evaluation of the execution. 2271 * 2272 * <p>Schedules asynchronous evaluation of the execution. Once the execution 2273 * has completed and the outputs are ready to be consumed, the returned event 2274 * will be signaled. Use {@link ANeuralNetworksEvent_wait} to wait for that 2275 * event. 2276 * </p> 2277 * 2278 * ANeuralNetworksEvent_wait must be called to recuperate the resources used 2279 * by the execution. 2280 * 2281 * If {@link ANeuralNetworksExecution_setTimeout} was called on this execution, 2282 * and the execution is not able to complete before the timeout duration is 2283 * exceeded, then execution may be aborted, in which case 2284 * {@link ANEURALNETWORKS_MISSED_DEADLINE_*} will be returned through 2285 * {@link ANeuralNetworksExecution_startCompute} or 2286 * {@link ANeuralNetworksEvent_wait} on the event object. If the device has a 2287 * feature level reported by {@link ANeuralNetworksDevice_getFeatureLevel} that 2288 * is lower than 30, then the timeout duration hint will be ignored. 2289 * 2290 * If this execution contains a {@link ANEURALNETWORKS_WHILE} operation, and 2291 * the condition model does not output false within the loop timeout duration, 2292 * then execution will be aborted and {@link ANEURALNETWORKS_MISSED_DEADLINE_*} 2293 * will be returned through {@link ANeuralNetworksEvent_wait} on the event 2294 * object. 2295 * 2296 * If the device can detect before the execution has started that the execution 2297 * will not complete within the timeout duration, the device may choose to skip 2298 * the execution and instead return {@link ANEURALNETWORKS_MISSED_DEADLINE_*}. 2299 * 2300 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2301 * 2302 * See {@link ANeuralNetworksExecution_compute} for synchronous execution. 2303 * See {@link ANeuralNetworksExecution_burstCompute} for burst synchronous execution. 2304 * See {@link ANeuralNetworksExecution_startComputeWithDependencies} for 2305 * asynchronous execution with dependencies. 2306 * 2307 * Available since API level 27. 2308 * 2309 * @param execution The execution to be scheduled and executed. 2310 * @param event The event that will be signaled on completion. event is set to 2311 * NULL if there's an error. 2312 * 2313 * @return ANEURALNETWORKS_NO_ERROR if the evaluation is successfully scheduled. 2314 */ 2315int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution* execution, 2316 ANeuralNetworksEvent** event) __INTRODUCED_IN(27); 2317 2318#if __ANDROID_API__ >= 30 2319 2320/** 2321 * Set the maximum expected duration of the specified execution. 2322 * 2323 * If the device is not able to complete the execution within the specified 2324 * duration, the execution may be aborted. The timeout duration begins at a 2325 * call to one of: 2326 * - {@link ANeuralNetworksExecution_burstCompute} 2327 * - {@link ANeuralNetworksExecution_compute} 2328 * - {@link ANeuralNetworksExecution_startCompute} 2329 * - {@link ANeuralNetworksExecution_startComputeWithDependencies} 2330 * 2331 * This timeout duration acts as a hint to drivers, and can be used to both free 2332 * up compute resources within the driver and return control back to the 2333 * application quicker than is possible without the hint. It enables drivers 2334 * that are able to estimate how long an execution will take to abort the 2335 * execution before it has even started if the driver believes the execution 2336 * cannot be completed within the timeout duration. Similarly, it enables 2337 * drivers to abort an ongoing execution if it is taking too long. However, this 2338 * call does not guarantee that the execution will complete or abort within the 2339 * timeout duration. 2340 * 2341 * By default (i.e., unless ANeuralNetworksExecution_setTimeout is called), 2342 * the timeout duration for execution is considered infinite. 2343 * 2344 * The {@link ANeuralNetworksExecution} must have been created from an 2345 * {@link ANeuralNetworksCompilation} which in turn was created from 2346 * {@link ANeuralNetworksCompilation_createForDevices} with numDevices = 1, 2347 * otherwise this function will fail with ANEURALNETWORKS_BAD_DATA. If the 2348 * device has a feature level reported by 2349 * {@link ANeuralNetworksDevice_getFeatureLevel} that is lower than 30, then the 2350 * timeout duration hint will be ignored. 2351 * 2352 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2353 * 2354 * @param execution The execution to be modified. 2355 * @param duration The maximum amount of time in nanoseconds that is expected to 2356 * be spent executing a model. If this duration is exceeded, the execution 2357 * may be aborted. If set to 0, the timeout duration is considered infinite. 2358 * 2359 * @return ANEURALNETWORKS_NO_ERROR if successful. 2360 * 2361 * Available since API level 30. 2362 */ 2363int ANeuralNetworksExecution_setTimeout(ANeuralNetworksExecution* execution, uint64_t duration) 2364 __INTRODUCED_IN(30); 2365 2366/** 2367 * Set the maximum duration of WHILE loops in the specified execution. 2368 * 2369 * This is a fuzzy per-loop timeout intended to prevent infinite loops. 2370 * 2371 * If a WHILE loop condition model does not output false within the specified 2372 * duration, the execution will be aborted. 2373 * 2374 * See {@link ANeuralNetworks_getDefaultLoopTimeout} and 2375 * {@link ANeuralNetworks_getMaximumLoopTimeout} for the default 2376 * and maximum timeout values. 2377 * 2378 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2379 * 2380 * @param execution The execution to be modified. 2381 * @param duration The maximum amount of time in nanoseconds that can be spent 2382 * executing a WHILE loop. If the specified duration value exceeds the value 2383 * produced by {@link ANeuralNetworks_getMaximumLoopTimeout}, it will be 2384 * overridden by that value. 2385 * 2386 * @return ANEURALNETWORKS_NO_ERROR if successful. 2387 * ANEURALNETWORKS_BAD_STATE if execution has started. 2388 * ANEURALNETWORKS_UNEXPECTED_NULL if execution is NULL. 2389 * 2390 * Available since API level 30. 2391 */ 2392int ANeuralNetworksExecution_setLoopTimeout(ANeuralNetworksExecution* execution, uint64_t duration) 2393 __INTRODUCED_IN(30); 2394 2395/** 2396 * Get the default timeout value for WHILE loops. 2397 * 2398 * @return The default timeout value in nanoseconds. 2399 * 2400 * Available since API level 30. 2401 */ 2402uint64_t ANeuralNetworks_getDefaultLoopTimeout() __INTRODUCED_IN(30); 2403 2404/** 2405 * Get the maximum timeout value for WHILE loops. 2406 * 2407 * @return The maximum timeout value in nanoseconds. 2408 * 2409 * Available since API level 30. 2410 */ 2411uint64_t ANeuralNetworks_getMaximumLoopTimeout() __INTRODUCED_IN(30); 2412 2413#endif // __ANDROID_API__ >= 30 2414 2415/** 2416 * Waits until the execution completes. 2417 * 2418 * More than one thread can wait on an event. When the execution completes, 2419 * all threads will be released. 2420 * 2421 * If {@link ANeuralNetworksExecution_setTimeout} was called on the execution 2422 * corresponding to this event, and the execution is not able to complete 2423 * before the duration is exceeded, the execution may be aborted, in which case 2424 * {@link ANEURALNETWORKS_MISSED_DEADLINE_*} will be returned here. 2425 * 2426 * If the execution contains a {@link ANEURALNETWORKS_WHILE} operation, and 2427 * the condition model does not output false within the loop timeout duration, 2428 * the execution will be aborted, and {@link ANEURALNETWORKS_MISSED_DEADLINE_*} 2429 * will be returned here. 2430 * 2431 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2432 * 2433 * Available since API level 27. 2434 * 2435 * @param event The event that will be signaled on completion. 2436 * @return ANEURALNETWORKS_NO_ERROR if the execution completed normally. 2437 * ANEURALNETWORKS_UNMAPPABLE if the execution input or output memory cannot 2438 * be properly mapped. 2439 */ 2440int ANeuralNetworksEvent_wait(ANeuralNetworksEvent* event) __INTRODUCED_IN(27); 2441 2442/** 2443 * Destroys the event. 2444 * 2445 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2446 * 2447 * Available since API level 27. 2448 * 2449 * @param event The event object to be destroyed. Passing NULL is acceptable and 2450 * results in no operation. 2451 */ 2452void ANeuralNetworksEvent_free(ANeuralNetworksEvent* event) __INTRODUCED_IN(27); 2453 2454#endif // __ANDROID_API__ >= 27 2455 2456#if __ANDROID_API__ >= 30 2457/** 2458 * Create a {@link ANeuralNetworksEvent} from a sync_fence file descriptor. 2459 * 2460 * The newly created ANeuralNetworksEvent does not take ownership of the provided sync_fence_fd, 2461 * it will instead dup the provided sync_fence_fd and own the duplicate. 2462 * 2463 * @param sync_fence_fd The sync_fence file descriptor. 2464 * @param event The newly created object or NULL if unsuccessful. 2465 * 2466 * @return ANEURALNETWORKS_NO_ERROR if successful. 2467 * 2468 * Available since API level 30. 2469 */ 2470int ANeuralNetworksEvent_createFromSyncFenceFd(int sync_fence_fd, ANeuralNetworksEvent** event) 2471 __INTRODUCED_IN(30); 2472 2473/** 2474 * Get sync_fence file descriptor from the event. 2475 * 2476 * If the ANeuralNetworksEvent is not backed by a sync fence, the sync_fence_fd 2477 * will be set to -1, and ANEURALNETWORKS_BAD_DATA will be returned. 2478 * 2479 * See {@link ANeuralNetworksEvent_createFromSyncFenceFd} and 2480 * {@link ANeuralNetworksExecution_startComputeWithDependencies} to see how to create 2481 * an event backed by a sync fence. 2482 * 2483 * The user takes ownership of the returned fd, and must close the returned file descriptor when 2484 * it is no longer needed. 2485 * 2486 * @param event An event that is backed by a sync fence. 2487 * @param sync_fence_fd The sync_fence file descriptor. The file descriptor will 2488 * be set to -1 if there is an error. 2489 * 2490 * @return ANEURALNETWORKS_NO_ERROR if successful. 2491 * 2492 * Available since API level 30. 2493 */ 2494int ANeuralNetworksEvent_getSyncFenceFd(const ANeuralNetworksEvent* event, int* sync_fence_fd) 2495 __INTRODUCED_IN(30); 2496 2497/** 2498 * Schedule asynchronous evaluation of the execution with dependencies. 2499 * 2500 * The execution will wait for all the depending events to be signaled before 2501 * starting the evaluation. Once the execution has completed and the outputs 2502 * are ready to be consumed, the returned event will be signaled. Depending on which 2503 * devices are handling the execution, the event could be backed by a sync fence. 2504 * Use {@link ANeuralNetworksEvent_wait} to wait for that event. 2505 * 2506 * ANeuralNetworksEvent_wait must be called to recurperate the resources used 2507 * by the execution. 2508 * 2509 * If parts of the execution are scheduled on devices that do not support fenced execution, 2510 * the function call may wait for such parts to finish before returning. 2511 * 2512 * The function will return an error if any of the events in dependencies is already in a bad 2513 * state. After the execution is scheduled, if any of the events in dependencies does not complete 2514 * normally, the execution will fail, and {@link ANeuralNetworksEvent_wait} on the returned 2515 * event will return an error. 2516 * 2517 * The function will return an error if any of the execution outputs has a tensor operand type 2518 * that is not fully specified. 2519 * 2520 * The function can be passed a timeout duration in nanoseconds. This timeout 2521 * duration acts as a hint to drivers in the same way that the timeout durations 2522 * in {@link ANeuralNetworksCompilation_setTimeout} and {@link 2523 * ANeuralNetworksExecution_setTimeout} act as hints to drivers. The duration 2524 * begins when all waitFor sync fences have been signaled, and can be used 2525 * together with {@link ANeuralNetworksExecution_setTimeout} which specifies the 2526 * maximum timeout duration beginning at the call to 2527 * {@link ANeuralNetworksExecution_startComputeWithDependencies}. 2528 * If the duration is non-zero, the {@link ANeuralNetworksExecution} must have been created 2529 * from an {@link ANeuralNetworksCompilation} which in turn was created from 2530 * {@link ANeuralNetworksCompilation_createForDevices} with numDevices = 1, 2531 * otherwise this function will fail with ANEURALNETWORKS_BAD_DATA. If either 2532 * the timeout duration from {@link ANeuralNetworksExecution_setTimeout} or the 2533 * timeout duration passed to this call is exceeded, the execution may be 2534 * aborted, in which case {@link ANEURALNETWORKS_MISSED_DEADLINE_*} will be 2535 * returned through {@link ANeuralNetworksExecution_startComputeWithDependencies} 2536 * or {@link ANeuralNetworksEvent_wait} on the event object. If the device has a 2537 * feature level reported by {@link ANeuralNetworksDevice_getFeatureLevel} that 2538 * is lower than 30, then the timeout duration hints will be ignored. 2539 * 2540 * If this execution contains a {@link ANEURALNETWORKS_WHILE} operation, and 2541 * the condition model does not output false within the loop timeout duration, 2542 * then execution will be aborted and {@link ANEURALNETWORKS_MISSED_DEADLINE_*} 2543 * will be returned through {@link ANeuralNetworksEvent_wait} on the event 2544 * object. 2545 * 2546 * See {@link ANeuralNetworksExecution} for information on multithreaded usage. 2547 * 2548 * See {@link ANeuralNetworksExecution_compute} for synchronous execution. 2549 * See {@link ANeuralNetworksExecution_burstCompute} for burst synchronous execution. 2550 * See {@link ANeuralNetworksExecution_startCompute} for regular asynchronous execution. 2551 * 2552 * @param execution The execution to be scheduled and executed. 2553 * @param dependencies A set of depending events. The actual evaluation will not start 2554 * until all the events are signaled. 2555 * @param num_dependencies The number of events in the dependencies set. 2556 * @param duration The maximum amount of time in nanoseconds that is expected to 2557 * be spent executing the model after all dependencies are 2558 * signaled. If set to 0, the timeout duration is considered 2559 * infinite. 2560 * @param event The event that will be signaled on completion. event is set to 2561 * NULL if there's an error. 2562 * 2563 * @return ANEURALNETWORKS_NO_ERROR if the evaluation is successfully scheduled. 2564 * 2565 * Available since API level 30. 2566 */ 2567int ANeuralNetworksExecution_startComputeWithDependencies( 2568 ANeuralNetworksExecution* execution, const ANeuralNetworksEvent* const* dependencies, 2569 uint32_t num_dependencies, uint64_t duration, ANeuralNetworksEvent** event) 2570 __INTRODUCED_IN(30); 2571 2572#endif // __ANDROID_API__ >= 30 2573 2574__END_DECLS 2575 2576#endif // ANDROID_FRAMEWORKS_ML_NN_RUNTIME_NEURAL_NETWORKS_H 2577 2578/** @} */ 2579