Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.hardware.neuralnetworks@1.2; |
| 18 | |
| 19 | import @1.0::ErrorStatus; |
| 20 | import @1.0::IPreparedModel; |
| 21 | import @1.0::Request; |
Michael Butler | 7e91e24 | 2019-01-15 10:52:41 -0800 | [diff] [blame] | 22 | import IBurstCallback; |
| 23 | import IBurstContext; |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 24 | import IExecutionCallback; |
| 25 | |
| 26 | /** |
| 27 | * IPreparedModel describes a model that has been prepared for execution and |
| 28 | * is used to launch executions. |
| 29 | */ |
| 30 | interface IPreparedModel extends @1.0::IPreparedModel { |
| 31 | /** |
| 32 | * Launches an asynchronous execution on a prepared model. |
| 33 | * |
| 34 | * The execution is performed asynchronously with respect to the caller. |
| 35 | * execute_1_2 must verify the inputs to the function are correct. If there is |
| 36 | * an error, execute_1_2 must immediately invoke the callback with the |
| 37 | * appropriate ErrorStatus value, then return with the same ErrorStatus. If |
| 38 | * the inputs to the function are valid and there is no error, execute_1_2 must |
| 39 | * launch an asynchronous task to perform the execution in the background, |
| 40 | * and immediately return with ErrorStatus::NONE. If the asynchronous task |
| 41 | * fails to launch, execute_1_2 must immediately invoke the callback with |
| 42 | * ErrorStatus::GENERAL_FAILURE, then return with |
| 43 | * ErrorStatus::GENERAL_FAILURE. |
| 44 | * |
| 45 | * When the asynchronous task has finished its execution, it must |
| 46 | * immediately invoke the callback object provided as an input to the |
| 47 | * execute_1_2 function. This callback must be provided with the ErrorStatus of |
| 48 | * the execution. |
| 49 | * |
| 50 | * If the prepared model was prepared from a model wherein all |
| 51 | * tensor operands have fully specified dimensions, and the inputs |
| 52 | * to the function are valid, then the execution should launch |
| 53 | * and complete successfully (ErrorStatus::NONE). There must be |
| 54 | * no failure unless the device itself is in a bad state. |
| 55 | * |
David Gross | 49e4167 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 56 | * Any number of calls to the execute, execute_1_2, and executeSynchronously |
| 57 | * functions, in any combination, may be made concurrently, even on the same |
| 58 | * IPreparedModel object. |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 59 | * |
| 60 | * @param request The input and output information on which the prepared |
| 61 | * model is to be executed. |
David Gross | e301349 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 62 | * @param measure Specifies whether or not to measure duration of the execution. |
| 63 | * The duration runs from the time the driver sees the call |
| 64 | * to the execute_1_2 function to the time the driver invokes |
| 65 | * the callback. |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 66 | * @param callback A callback object used to return the error status of |
| 67 | * the execution. The callback object's notify function must |
| 68 | * be called exactly once, even if the execution was |
| 69 | * unsuccessful. |
| 70 | * @return status Error status of the call, must be: |
| 71 | * - NONE if task is successfully launched |
| 72 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 73 | * - GENERAL_FAILURE if there is an unspecified error |
| 74 | * - OUTPUT_INSUFFICIENT_SIZE if provided output buffer is |
| 75 | * not large enough to store the resultant values |
| 76 | * - INVALID_ARGUMENT if one of the input arguments is |
| 77 | * invalid |
| 78 | */ |
David Gross | e301349 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 79 | execute_1_2(Request request, MeasureTiming measure, IExecutionCallback callback) |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 80 | generates (ErrorStatus status); |
David Gross | 49e4167 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Performs a synchronous execution on a prepared model. |
| 84 | * |
| 85 | * The execution is performed synchronously with respect to the caller. |
| 86 | * executeSynchronously must verify the inputs to the function are |
| 87 | * correct. If there is an error, executeSynchronously must immediately |
| 88 | * return with the appropriate ErrorStatus value. If the inputs to the |
| 89 | * function are valid and there is no error, executeSynchronously must |
| 90 | * perform the execution, and must not return until the execution is |
| 91 | * complete. |
| 92 | * |
| 93 | * If the prepared model was prepared from a model wherein all tensor |
| 94 | * operands have fully specified dimensions, and the inputs to the function |
| 95 | * are valid, then the execution should complete successfully |
| 96 | * (ErrorStatus::NONE). There must be no failure unless the device itself is |
| 97 | * in a bad state. |
| 98 | * |
| 99 | * Any number of calls to the execute, execute_1_2, and executeSynchronously |
| 100 | * functions, in any combination, may be made concurrently, even on the same |
| 101 | * IPreparedModel object. |
| 102 | * |
| 103 | * @param request The input and output information on which the prepared |
| 104 | * model is to be executed. |
David Gross | e301349 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 105 | * @param measure Specifies whether or not to measure duration of the execution. |
| 106 | * The duration runs from the time the driver sees the call |
| 107 | * to the executeSynchronously function to the time the driver |
| 108 | * returns from the function. |
David Gross | 49e4167 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 109 | * @return status Error status of the execution, must be: |
| 110 | * - NONE if execution is performed successfully |
| 111 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 112 | * - GENERAL_FAILURE if there is an unspecified error |
Xusong Wang | 187c597 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 113 | * - OUTPUT_INSUFFICIENT_SIZE if at least one output |
| 114 | * operand buffer is not large enough to store the |
| 115 | * corresponding output |
David Gross | 49e4167 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 116 | * - INVALID_ARGUMENT if one of the input arguments is |
| 117 | * invalid |
Xusong Wang | 187c597 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 118 | * @return outputShapes A list of shape information of model output operands. |
| 119 | * The index into "outputShapes" corresponds to the index |
| 120 | * of the output operand in the Request outputs vector. |
| 121 | * outputShapes must be empty unless the status is either |
| 122 | * NONE or OUTPUT_INSUFFICIENT_SIZE. |
David Gross | e301349 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 123 | * @return Timing Duration of execution. Unless measure is YES and status is |
| 124 | * NONE, all times must be reported as UINT64_MAX. A driver may |
| 125 | * choose to report any time as UINT64_MAX, indicating that |
| 126 | * measurement is not available. |
David Gross | 49e4167 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 127 | */ |
David Gross | e301349 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 128 | executeSynchronously(Request request, MeasureTiming measure) |
| 129 | generates (ErrorStatus status, vec<OutputShape> outputShapes, Timing timing); |
Michael Butler | 7e91e24 | 2019-01-15 10:52:41 -0800 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * Configure a Burst object used to execute multiple inferences on a |
| 133 | * prepared model in rapid succession. |
| 134 | * |
| 135 | * @param callback A callback object used to retrieve memory resources |
| 136 | * corresponding to a unique identifiers ("slots"). |
| 137 | * @param requestChannel Used by the client to send a serialized Request to |
| 138 | * the Burst for execution. requestChannel must not be |
| 139 | * used to pass a second Request object until a result |
| 140 | * has been received from resultChannel. |
| 141 | * @param resultChannel Used by the service to return the results of an |
| 142 | * execution to the client: the status of the execution |
| 143 | * and OutputShape of all output tensors. resultChannel |
| 144 | * must be used to return the results if a Request was |
| 145 | * sent through the requestChannel. |
| 146 | * @return status Error status of configuring the execution burst, must be: |
| 147 | * - NONE if the burst is successfully configured |
| 148 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 149 | * - GENERAL_FAILURE if there is an unspecified error |
| 150 | * - INVALID_ARGUMENT if one of the input arguments is |
| 151 | * invalid |
| 152 | * @return context Object containing all resources (such as cached |
| 153 | * hidl_memory) related to a Burst if successful, otherwise |
| 154 | * nullptr. |
| 155 | */ |
| 156 | configureExecutionBurst(IBurstCallback callback, |
| 157 | fmq_sync<FmqRequestDatum> requestChannel, |
| 158 | fmq_sync<FmqResultDatum> resultChannel) |
| 159 | generates (ErrorStatus status, IBurstContext context); |
Xusong Wang | 89dfafb | 2019-01-11 17:41:11 -0800 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * Saves the prepared model to cache files. |
| 163 | * |
| 164 | * saveToCache is used to save a prepared model to cache files for faster |
| 165 | * model compilation time when the same model preparation is requested in |
| 166 | * the future. There are exactly two cache file descriptors provided to the |
| 167 | * driver: modelCache and dataCache. |
| 168 | * |
| 169 | * The dataCache is for caching constant data, possibly including preprocessed |
| 170 | * and transformed tensor buffers. Any modification to the dataCache should |
| 171 | * have no worse effect than generating bad output values at execution time. |
| 172 | * |
| 173 | * The modelCache is for caching security-sensitive data such as compiled |
| 174 | * executable machine code in the device's native binary format. A modification |
| 175 | * to the modelCache may affect the driver's execution behavior, and a malicious |
| 176 | * client could make use of this to execute beyond the granted permission. Thus, |
| 177 | * the driver must always check whether the modelCache is corrupted before preparing |
| 178 | * the model from cache. |
| 179 | * |
| 180 | * The two file descriptors must point to two zero-length files with offset |
| 181 | * positioned at the beginning of the file. The file descriptors may be closed |
| 182 | * by the client once the method has returned. |
| 183 | * |
| 184 | * If the driver decides not to save the prepared model without looking at the |
| 185 | * input arguments to the saveToCache function, saveToCache must return with |
| 186 | * ErrorStatus::GENERAL_FAILURE. Otherwise, the saveToCache function must verify |
| 187 | * the input arguments to the saveToCache function are valid, and return with |
| 188 | * ErrorStatus::INVALID_ARGUMENT if not. If the inputs are valid but the driver |
| 189 | * could not save the prepared model, saveToCache must return with the appropriate |
| 190 | * ErrorStatus. Otherwise, it must write the cache files and return |
| 191 | * ErrorStatus::NONE. Unless saveToCache returns ErrorStatus::NONE, the contents |
| 192 | * of the cache files are undefined. |
| 193 | * |
| 194 | * @param modelCache A handle holding exactly one cache file descriptor for the |
| 195 | * security-sensitive cache. |
| 196 | * @param dataCache A handle holding exactly one cache file descriptor for the |
| 197 | * constants' cache. |
| 198 | * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN |
| 199 | * identifying the prepared model. The same token will be provided |
| 200 | * when retrieving the prepared model from cache files with |
| 201 | * IDevice::prepareModelFromCache. Tokens should be chosen to have |
| 202 | * a low rate of collision for a particular application. The driver |
| 203 | * cannot detect a collision; a collision will result in a failed |
| 204 | * execution or in a successful execution that produces incorrect |
| 205 | * output values. |
| 206 | * @return status Error status of saveToCache, must be: |
| 207 | * - NONE if saveToCache is performed successfully |
| 208 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 209 | * - GENERAL_FAILURE if the driver could not save the |
| 210 | * prepared model or if there is an unspecified error |
| 211 | * - INVALID_ARGUMENT if one of the input arguments is invalid, |
| 212 | * unless the driver decides not to save the prepared model |
| 213 | * without looking at the input arguments |
| 214 | */ |
| 215 | saveToCache(handle modelCache, handle dataCache, |
| 216 | uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token) |
| 217 | generates (ErrorStatus status); |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 218 | }; |