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