Lev Proleev | 5a7b67a | 2019-08-08 14:08:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.3; |
| 18 | |
| 19 | import @1.0::ErrorStatus; |
| 20 | import @1.1::ExecutionPreference; |
| 21 | import @1.2::Constant; |
| 22 | import @1.2::DeviceType; |
| 23 | import @1.2::Extension; |
| 24 | import @1.2::IDevice; |
Xusong Wang | 931d5a1 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 25 | import BufferDesc; |
| 26 | import BufferRole; |
| 27 | import Capabilities; |
| 28 | import Model; |
| 29 | import IBuffer; |
| 30 | import IPreparedModel; |
Xusong Wang | 68c3234 | 2019-10-23 10:35:07 -0700 | [diff] [blame] | 31 | import IPreparedModelCallback; |
Lev Proleev | 5a7b67a | 2019-08-08 14:08:31 +0100 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * This interface represents a device driver. |
| 35 | */ |
| 36 | interface IDevice extends @1.2::IDevice { |
| 37 | /** |
| 38 | * Gets the capabilities of a driver. |
| 39 | * |
| 40 | * @return status Error status of the call, must be: |
| 41 | * - NONE if successful |
| 42 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 43 | * - GENERAL_FAILURE if there is an unspecified error |
| 44 | * @return capabilities Capabilities of the driver. |
| 45 | */ |
| 46 | getCapabilities_1_3() generates (ErrorStatus status, Capabilities capabilities); |
| 47 | |
| 48 | /** |
| 49 | * Gets the supported operations in a model. |
| 50 | * |
| 51 | * getSupportedOperations indicates which operations of a model are fully |
| 52 | * supported by the vendor driver. If an operation may not be supported for |
| 53 | * any reason, getSupportedOperations must return false for that operation. |
| 54 | * |
| 55 | * @param model A model whose operations--and their corresponding operands-- |
| 56 | * are to be verified by the driver. |
| 57 | * @return status Error status of the call, must be: |
| 58 | * - NONE if successful |
| 59 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 60 | * - GENERAL_FAILURE if there is an unspecified error |
| 61 | * - INVALID_ARGUMENT if provided model is invalid |
| 62 | * @return supportedOperations A list of supported operations, where true |
| 63 | * indicates the operation is supported and false indicates the |
| 64 | * operation is not supported. The index of "supported" corresponds with |
| 65 | * the index of the operation it is describing. |
| 66 | */ |
| 67 | getSupportedOperations_1_3(Model model) |
| 68 | generates (ErrorStatus status, vec<bool> supportedOperations); |
| 69 | |
| 70 | /** |
| 71 | * Asynchronously creates a prepared model for execution and optionally |
| 72 | * saves it into cache files. |
| 73 | * |
| 74 | * prepareModel is used to make any necessary transformations to or |
| 75 | * alternative representations to a model for execution, possibly including |
| 76 | * transformations on the constant data, optimization on the model's graph, |
| 77 | * or compilation into the device's native binary format. The model itself |
| 78 | * is not changed. |
| 79 | * |
| 80 | * Optionally, caching information may be provided for the driver to save |
| 81 | * the prepared model to cache files for faster model compilation time when |
| 82 | * the same model preparation is requested in the future. There are two |
| 83 | * types of cache file handles provided to the driver: model cache and data |
| 84 | * cache. For more information on the two types of cache handles, refer to |
| 85 | * getNumberOfCacheFilesNeeded. |
| 86 | * |
| 87 | * The file descriptors must be opened with read and write permission. A |
| 88 | * file may have any size, and the corresponding file descriptor may have |
| 89 | * any offset. The driver must truncate a file to zero size before writing |
| 90 | * to that file. The file descriptors may be closed by the client once the |
| 91 | * asynchronous preparation has finished. The driver must dup a file |
| 92 | * descriptor if it wants to get access to the cache file later. |
| 93 | * |
| 94 | * The model is prepared asynchronously with respect to the caller. The |
| 95 | * prepareModel function must verify the inputs to the preparedModel |
| 96 | * function related to preparing the model (as opposed to saving the |
| 97 | * prepared model to cache) are correct. If there is an error, prepareModel |
| 98 | * must immediately invoke the callback with the appropriate ErrorStatus |
| 99 | * value and nullptr for the IPreparedModel, then return with the same |
| 100 | * ErrorStatus. If the inputs to the prepareModel function that are related |
| 101 | * to preparing the model are valid and there is no error, prepareModel must |
| 102 | * launch an asynchronous task to prepare the model in the background, and |
| 103 | * immediately return from prepareModel with ErrorStatus::NONE. If the |
| 104 | * asynchronous task fails to launch, prepareModel must immediately invoke |
| 105 | * the callback with ErrorStatus::GENERAL_FAILURE and nullptr for the |
| 106 | * IPreparedModel, then return with ErrorStatus::GENERAL_FAILURE. |
| 107 | * |
| 108 | * When the asynchronous task has finished preparing the model, it must |
| 109 | * immediately invoke the callback function provided as an input to |
| 110 | * prepareModel. If the model was prepared successfully, the callback object |
| 111 | * must be invoked with an error status of ErrorStatus::NONE and the |
| 112 | * produced IPreparedModel object. If an error occurred preparing the model, |
| 113 | * the callback object must be invoked with the appropriate ErrorStatus |
| 114 | * value and nullptr for the IPreparedModel. |
| 115 | * |
| 116 | * Optionally, the driver may save the prepared model to cache during the |
| 117 | * asynchronous preparation. Any error that occurs when saving to cache must |
| 118 | * not affect the status of preparing the model. Even if the input arguments |
| 119 | * related to the cache may be invalid, or the driver may fail to save to |
| 120 | * cache, the prepareModel function must finish preparing the model. The |
| 121 | * driver may choose not to save to cache even if the caching information is |
| 122 | * provided and valid. |
| 123 | * |
| 124 | * The only information that may be unknown to the model at this stage is |
| 125 | * the shape of the tensors, which may only be known at execution time. As |
| 126 | * such, some driver services may return partially prepared models, where |
| 127 | * the prepared model may only be finished when it is paired with a set of |
| 128 | * inputs to the model. Note that the same prepared model object may be used |
| 129 | * with different shapes of inputs on different (possibly concurrent) |
| 130 | * executions. |
| 131 | * |
| 132 | * Multiple threads may call prepareModel on the same model concurrently. |
| 133 | * |
| 134 | * @param model The model to be prepared for execution. |
| 135 | * @param preference Indicates the intended execution behavior of a prepared |
| 136 | * model. |
| 137 | * @param modelCache A vector of handles with each entry holding exactly one |
| 138 | * cache file descriptor for the security-sensitive cache. The length of |
| 139 | * the vector must either be 0 indicating that caching information is |
| 140 | * not provided, or match the numModelCache returned from |
| 141 | * getNumberOfCacheFilesNeeded. The cache handles will be provided in |
| 142 | * the same order when retrieving the preparedModel from cache files |
Xusong Wang | 68c3234 | 2019-10-23 10:35:07 -0700 | [diff] [blame] | 143 | * with prepareModelFromCache_1_3. |
Lev Proleev | 5a7b67a | 2019-08-08 14:08:31 +0100 | [diff] [blame] | 144 | * @param dataCache A vector of handles with each entry holding exactly one |
| 145 | * cache file descriptor for the constants' cache. The length of the |
| 146 | * vector must either be 0 indicating that caching information is not |
| 147 | * provided, or match the numDataCache returned from |
| 148 | * getNumberOfCacheFilesNeeded. The cache handles will be provided in |
| 149 | * the same order when retrieving the preparedModel from cache files |
Xusong Wang | 68c3234 | 2019-10-23 10:35:07 -0700 | [diff] [blame] | 150 | * with prepareModelFromCache_1_3. |
Lev Proleev | 5a7b67a | 2019-08-08 14:08:31 +0100 | [diff] [blame] | 151 | * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN |
| 152 | * identifying the prepared model. The same token will be provided when |
| 153 | * retrieving the prepared model from the cache files with |
Xusong Wang | 68c3234 | 2019-10-23 10:35:07 -0700 | [diff] [blame] | 154 | * prepareModelFromCache_1_3. Tokens should be chosen to have a low rate of |
Lev Proleev | 5a7b67a | 2019-08-08 14:08:31 +0100 | [diff] [blame] | 155 | * collision for a particular application. The driver cannot detect a |
| 156 | * collision; a collision will result in a failed execution or in a |
| 157 | * successful execution that produces incorrect output values. If both |
| 158 | * modelCache and dataCache are empty indicating that caching |
| 159 | * information is not provided, this token must be ignored. |
| 160 | * @param callback A callback object used to return the error status of |
| 161 | * preparing the model for execution and the prepared model if |
| 162 | * successful, nullptr otherwise. The callback object's notify function |
| 163 | * must be called exactly once, even if the model could not be prepared. |
| 164 | * @return status Error status of launching a task which prepares the model |
| 165 | * in the background; must be: |
| 166 | * - NONE if preparation task is successfully launched |
| 167 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 168 | * - GENERAL_FAILURE if there is an unspecified error |
| 169 | * - INVALID_ARGUMENT if one of the input arguments related to preparing |
| 170 | * the model is invalid |
| 171 | */ |
| 172 | prepareModel_1_3(Model model, ExecutionPreference preference, |
| 173 | vec<handle> modelCache, vec<handle> dataCache, |
| 174 | uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token, |
| 175 | IPreparedModelCallback callback) |
| 176 | generates (ErrorStatus status); |
Xusong Wang | 68c3234 | 2019-10-23 10:35:07 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * Creates a prepared model from cache files for execution. |
| 180 | * |
| 181 | * prepareModelFromCache_1_3 is used to retrieve a prepared model directly from |
| 182 | * cache files to avoid slow model compilation time. There are |
| 183 | * two types of cache file handles provided to the driver: model cache |
| 184 | * and data cache. For more information on the two types of cache handles, |
| 185 | * refer to getNumberOfCacheFilesNeeded. |
| 186 | * |
| 187 | * The file descriptors must be opened with read and write permission. A file may |
| 188 | * have any size, and the corresponding file descriptor may have any offset. The |
| 189 | * driver must truncate a file to zero size before writing to that file. The file |
| 190 | * descriptors may be closed by the client once the asynchronous preparation has |
| 191 | * finished. The driver must dup a file descriptor if it wants to get access to |
| 192 | * the cache file later. |
| 193 | * |
| 194 | * The model is prepared asynchronously with respect to the caller. The |
| 195 | * prepareModelFromCache_1_3 function must verify the inputs to the |
| 196 | * prepareModelFromCache_1_3 function are correct, and that the security-sensitive |
| 197 | * cache has not been modified since it was last written by the driver. |
| 198 | * If there is an error, or if compilation caching is not supported, or if the |
| 199 | * security-sensitive cache has been modified, prepareModelFromCache_1_3 must |
| 200 | * immediately invoke the callback with the appropriate ErrorStatus value and |
| 201 | * nullptr for the IPreparedModel, then return with the same ErrorStatus. If |
| 202 | * the inputs to the prepareModelFromCache_1_3 function are valid, the security-sensitive |
| 203 | * cache is not modified, and there is no error, prepareModelFromCache_1_3 must launch an |
| 204 | * asynchronous task to prepare the model in the background, and immediately return |
| 205 | * from prepareModelFromCache_1_3 with ErrorStatus::NONE. If the asynchronous task |
| 206 | * fails to launch, prepareModelFromCache_1_3 must immediately invoke the callback |
| 207 | * with ErrorStatus::GENERAL_FAILURE and nullptr for the IPreparedModel, then |
| 208 | * return with ErrorStatus::GENERAL_FAILURE. |
| 209 | * |
| 210 | * When the asynchronous task has finished preparing the model, it must |
| 211 | * immediately invoke the callback function provided as an input to |
| 212 | * prepareModelFromCache_1_3. If the model was prepared successfully, the |
| 213 | * callback object must be invoked with an error status of ErrorStatus::NONE |
| 214 | * and the produced IPreparedModel object. If an error occurred preparing |
| 215 | * the model, the callback object must be invoked with the appropriate |
| 216 | * ErrorStatus value and nullptr for the IPreparedModel. |
| 217 | * |
| 218 | * The only information that may be unknown to the model at this stage is |
| 219 | * the shape of the tensors, which may only be known at execution time. As |
| 220 | * such, some driver services may return partially prepared models, where |
| 221 | * the prepared model may only be finished when it is paired with a set of |
| 222 | * inputs to the model. Note that the same prepared model object may be |
| 223 | * used with different shapes of inputs on different (possibly concurrent) |
| 224 | * executions. |
| 225 | * |
| 226 | * @param modelCache A vector of handles with each entry holding exactly one |
| 227 | * cache file descriptor for the security-sensitive cache. The length of |
| 228 | * the vector must match the numModelCache returned from getNumberOfCacheFilesNeeded. |
| 229 | * The cache handles will be provided in the same order as with prepareModel_1_3. |
| 230 | * @param dataCache A vector of handles with each entry holding exactly one |
| 231 | * cache file descriptor for the constants' cache. The length of the vector |
| 232 | * must match the numDataCache returned from getNumberOfCacheFilesNeeded. |
| 233 | * The cache handles will be provided in the same order as with prepareModel_1_3. |
| 234 | * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN |
| 235 | * identifying the prepared model. It is the same token provided when saving |
| 236 | * the cache files with prepareModel_1_3. Tokens should be chosen |
| 237 | * to have a low rate of collision for a particular application. The driver |
| 238 | * cannot detect a collision; a collision will result in a failed execution |
| 239 | * or in a successful execution that produces incorrect output values. |
| 240 | * @param callback A callback object used to return the error status of |
| 241 | * preparing the model for execution and the prepared model if |
| 242 | * successful, nullptr otherwise. The callback object's notify function |
| 243 | * must be called exactly once, even if the model could not be prepared. |
| 244 | * @return status Error status of launching a task which prepares the model |
| 245 | * in the background; must be: |
| 246 | * - NONE if preparation task is successfully launched |
| 247 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 248 | * - GENERAL_FAILURE if caching is not supported or if there is an |
| 249 | * unspecified error |
| 250 | * - INVALID_ARGUMENT if one of the input arguments is invalid |
| 251 | */ |
| 252 | prepareModelFromCache_1_3(vec<handle> modelCache, vec<handle> dataCache, |
| 253 | uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token, |
| 254 | IPreparedModelCallback callback) |
| 255 | generates (ErrorStatus status); |
Xusong Wang | 931d5a1 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 256 | |
| 257 | /** |
| 258 | * Allocates a driver-managed buffer with the properties specified by the buffer descriptor |
| 259 | * as well as the input and output roles. |
| 260 | * |
| 261 | * The allocate function must verify its inputs are correct. If there is an error, or if a |
| 262 | * certain role or property is not supported by the driver, the allocate |
| 263 | * function must return with an appropriate ErrorStatus, a nullptr as the IBuffer, and 0 as the |
| 264 | * buffer token. If the allocation is successful, this method must return with ErrorStatus::NONE |
| 265 | * and the produced IBuffer with a positive token identifying the allocated buffer. A successful |
| 266 | * allocation must accommodate all of the specified roles and buffer properties. |
| 267 | * |
| 268 | * The buffer is allocated to an uninitialized state. An uninitialized buffer may only be used |
| 269 | * in ways that are specified by outputRoles. A buffer is initialized after it is used as an |
| 270 | * output in a successful execution, or after a successful invocation of IBuffer::copyFrom on |
| 271 | * the buffer. An initialized buffer may be used according to all roles specified in inputRoles |
| 272 | * and outputRoles. A buffer will return to the uninitialized state if it is used as an output |
| 273 | * in a failed execution, or after a failed invocation of IBuffer::copyFrom on the buffer. |
| 274 | * |
| 275 | * The dimensions of the buffer can be deduced from the buffer descriptor as well as the |
| 276 | * dimensions of the corresponding model operands of the input and output roles. The dimensions |
| 277 | * or rank of the buffer may be unknown at this stage. As such, some driver services may only |
| 278 | * create a placeholder and defer the actual allocation until execution time. Note that the |
| 279 | * same buffer may be used for different shapes of outputs on different executions. When the |
| 280 | * buffer is used as an input, the input shape must be the same as the output shape from the |
| 281 | * last execution using this buffer as an output. |
| 282 | * |
| 283 | * The driver must apply proper validatation upon every usage of the buffer, and must fail the |
| 284 | * execution immediately if the usage is illegal. |
| 285 | * |
| 286 | * @param desc A buffer descriptor specifying the properties of the buffer to allocate. |
| 287 | * @param preparedModels A vector of IPreparedModel objects. Must only contain IPreparedModel |
| 288 | * objects from the same IDevice as this method is being invoked on. |
| 289 | * @param inputRoles A vector of roles with each specifying an input to a prepared model. |
| 290 | * @param outputRoles A vector of roles with each specifying an output to a prepared model. |
| 291 | * Each role specified in inputRoles and outputRoles must be unique. The corresponding |
| 292 | * model operands of the roles must have the same OperandType, scale, zero point, and |
| 293 | * ExtraParams. The dimensions of the operands and the dimensions specified in the buffer |
| 294 | * descriptor must be compatible with each other. Two dimensions are incompatible if there |
| 295 | * is at least one axis that is fully specified in both but has different values. |
| 296 | * @return status Error status of the buffer allocation. Must be: |
| 297 | * - NONE if successful |
| 298 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 299 | * - GENERAL_FAILURE if a certain buffer property or a certain role is not supported, |
| 300 | * or if there is an unspecified error |
| 301 | * - INVALID_ARGUMENT if one of the input arguments is invalid |
| 302 | * @return buffer The allocated IBuffer object. If the buffer was unable to be allocated |
| 303 | * due to an error, nullptr must be returned. |
| 304 | * @return token A positive token identifying the allocated buffer. The same token will be |
| 305 | * provided when referencing the buffer as one of the memory pools in the request of an |
| 306 | * execution. The token must not collide with the tokens of other IBuffer objects that are |
| 307 | * currently alive in the same driver service. If the buffer was unable to be allocated |
| 308 | * due to an error, the token must be 0. |
| 309 | */ |
| 310 | allocate(BufferDesc desc, vec<IPreparedModel> preparedModels, vec<BufferRole> inputRoles, |
| 311 | vec<BufferRole> outputRoles) |
| 312 | generates (ErrorStatus status, IBuffer buffer, int32_t token); |
Lev Proleev | 5a7b67a | 2019-08-08 14:08:31 +0100 | [diff] [blame] | 313 | }; |