Slava Shklyaev | 060a9ac | 2018-09-07 15:27:24 +0100 | [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; |
Slava Shklyaev | 060a9ac | 2018-09-07 15:27:24 +0100 | [diff] [blame] | 20 | import @1.1::ExecutionPreference; |
| 21 | import @1.1::IDevice; |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 22 | import IPreparedModelCallback; |
Slava Shklyaev | 060a9ac | 2018-09-07 15:27:24 +0100 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * This interface represents a device driver. |
| 26 | */ |
| 27 | interface IDevice extends @1.1::IDevice { |
| 28 | /** |
Miao Wang | 44b029b | 2018-09-20 11:35:42 -0700 | [diff] [blame] | 29 | * Get the version string of the driver implementation. |
| 30 | * |
| 31 | * The version string must be a unique token among the set of version strings of |
| 32 | * drivers of a specific device. The token identifies the device driver's |
| 33 | * implementation. The token must not be confused with the feature level which is solely |
| 34 | * defined by the interface version. This API is opaque to the Android framework, but the |
| 35 | * Android framework may use the information for debugging or to pass on to NNAPI applications. |
| 36 | * |
| 37 | * Application developers sometimes have specific requirements to ensure good user experiences, |
| 38 | * and they need more information to make intelligent decisions when the Android framework cannot. |
| 39 | * For example, combined with the device name and other information, the token can help |
| 40 | * NNAPI applications filter devices based on their needs: |
| 41 | * - An application demands a certain level of performance, but a specific version of |
| 42 | * the driver cannot meet that requirement because of a performance regression. |
| 43 | * The application can blacklist the driver based on the version provided. |
| 44 | * - An application has a minimum precision requirement, but certain versions of |
| 45 | * the driver cannot meet that requirement because of bugs or certain optimizations. |
| 46 | * The application can filter out versions of these drivers. |
| 47 | * |
| 48 | * @return status Error status returned from querying the version string. Must be: |
| 49 | * - NONE if the query was successful |
| 50 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 51 | * - GENERAL_FAILURE if the query resulted in an |
| 52 | * unspecified error |
| 53 | * @return version The version string of the device implementation. |
| 54 | * Must have nonzero length |
| 55 | */ |
| 56 | getVersionString() generates (ErrorStatus status, string version); |
| 57 | |
| 58 | /** |
Miao Wang | e3b9353 | 2018-09-20 13:30:31 -0700 | [diff] [blame] | 59 | * Get the type of a given device. |
| 60 | * |
| 61 | * The device type can be used to help application developers to distribute |
| 62 | * Machine Learning workloads and other workloads such as graphical rendering. |
| 63 | * E.g., for an app which renders AR scenes based on real time object detection |
| 64 | * results, the developer could choose an ACCELERATOR type device for ML |
| 65 | * workloads, and reserve GPU for graphical rendering. |
| 66 | * |
| 67 | * @param status Error status returned from querying the device type. Must be: |
| 68 | * - NONE if the query was successful |
| 69 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 70 | * - GENERAL_FAILURE if the query resulted in an |
| 71 | * unspecified error |
| 72 | * @param type The DeviceType of the device. Please note, this is not a |
| 73 | * bitfield of DeviceTypes. Each device must only be of a |
| 74 | * single DeviceType. |
| 75 | */ |
| 76 | getType() generates (ErrorStatus status, DeviceType type); |
| 77 | |
| 78 | /** |
Slava Shklyaev | 060a9ac | 2018-09-07 15:27:24 +0100 | [diff] [blame] | 79 | * Gets the supported operations in a model. |
| 80 | * |
| 81 | * getSupportedOperations indicates which operations of a model are fully |
| 82 | * supported by the vendor driver. If an operation may not be supported for |
| 83 | * any reason, getSupportedOperations must return false for that operation. |
| 84 | * |
| 85 | * @param model A model whose operations--and their corresponding operands-- |
| 86 | * are to be verified by the driver. |
| 87 | * @return status Error status of the call, must be: |
| 88 | * - NONE if successful |
| 89 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 90 | * - GENERAL_FAILURE if there is an unspecified error |
| 91 | * - INVALID_ARGUMENT if provided model is invalid |
| 92 | * @return supportedOperations A list of supported operations, where true |
| 93 | * indicates the operation is supported and false indicates the |
| 94 | * operation is not supported. The index of "supported" corresponds with |
| 95 | * the index of the operation it is describing. |
| 96 | */ |
| 97 | getSupportedOperations_1_2(Model model) |
| 98 | generates (ErrorStatus status, vec<bool> supportedOperations); |
| 99 | |
| 100 | /** |
Xusong Wang | 89dfafb | 2019-01-11 17:41:11 -0800 | [diff] [blame] | 101 | * Gets whether the driver supports compilation caching. |
| 102 | * |
| 103 | * isCachingSupported indicates whether the driver supports compilation caching. |
| 104 | * Even if so, the driver may still choose not to cache certain compiled models. |
| 105 | * |
| 106 | * If the device reports the caching is not supported, the user may avoid calling |
| 107 | * IDevice::prepareModelFromCache and IPreparedModel::saveToCache. |
| 108 | * |
| 109 | * @return status Error status of the call, must be: |
| 110 | * - NONE if successful |
| 111 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 112 | * - GENERAL_FAILURE if there is an unspecified error |
| 113 | * @return supported A boolean indicating whether the driver supports compilation |
| 114 | * caching. Even on returning true, the driver may still choose |
| 115 | * not to cache certain compiled models. |
| 116 | */ |
| 117 | isCachingSupported() generates (ErrorStatus status, bool supported); |
| 118 | |
| 119 | /** |
Slava Shklyaev | 060a9ac | 2018-09-07 15:27:24 +0100 | [diff] [blame] | 120 | * Creates a prepared model for execution. |
| 121 | * |
| 122 | * prepareModel is used to make any necessary transformations or alternative |
| 123 | * representations to a model for execution, possibly including |
| 124 | * transformations on the constant data, optimization on the model's graph, |
| 125 | * or compilation into the device's native binary format. The model itself |
| 126 | * is not changed. |
| 127 | * |
| 128 | * The model is prepared asynchronously with respect to the caller. The |
| 129 | * prepareModel function must verify the inputs to the prepareModel function |
| 130 | * are correct. If there is an error, prepareModel must immediately invoke |
| 131 | * the callback with the appropriate ErrorStatus value and nullptr for the |
| 132 | * IPreparedModel, then return with the same ErrorStatus. If the inputs to |
| 133 | * the prepareModel function are valid and there is no error, prepareModel |
| 134 | * must launch an asynchronous task to prepare the model in the background, |
| 135 | * and immediately return from prepareModel with ErrorStatus::NONE. If the |
| 136 | * asynchronous task fails to launch, prepareModel must immediately invoke |
| 137 | * the callback with ErrorStatus::GENERAL_FAILURE and nullptr for the |
| 138 | * IPreparedModel, then return with ErrorStatus::GENERAL_FAILURE. |
| 139 | * |
| 140 | * When the asynchronous task has finished preparing the model, it must |
| 141 | * immediately invoke the callback function provided as an input to |
| 142 | * prepareModel. If the model was prepared successfully, the callback object |
| 143 | * must be invoked with an error status of ErrorStatus::NONE and the |
| 144 | * produced IPreparedModel object. If an error occurred preparing the model, |
| 145 | * the callback object must be invoked with the appropriate ErrorStatus |
| 146 | * value and nullptr for the IPreparedModel. |
| 147 | * |
| 148 | * The only information that may be unknown to the model at this stage is |
| 149 | * the shape of the tensors, which may only be known at execution time. As |
| 150 | * such, some driver services may return partially prepared models, where |
| 151 | * the prepared model may only be finished when it is paired with a set of |
| 152 | * inputs to the model. Note that the same prepared model object may be |
| 153 | * used with different shapes of inputs on different (possibly concurrent) |
| 154 | * executions. |
| 155 | * |
| 156 | * Multiple threads may call prepareModel on the same model concurrently. |
| 157 | * |
| 158 | * @param model The model to be prepared for execution. |
| 159 | * @param preference Indicates the intended execution behavior of a prepared |
| 160 | * model. |
| 161 | * @param callback A callback object used to return the error status of |
| 162 | * preparing the model for execution and the prepared model if |
| 163 | * successful, nullptr otherwise. The callback object's notify function |
| 164 | * must be called exactly once, even if the model could not be prepared. |
| 165 | * @return status Error status of launching a task which prepares the model |
| 166 | * in the background; must be: |
| 167 | * - NONE if preparation task is successfully launched |
| 168 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 169 | * - GENERAL_FAILURE if there is an unspecified error |
| 170 | * - INVALID_ARGUMENT if one of the input arguments is invalid |
| 171 | */ |
| 172 | prepareModel_1_2(Model model, ExecutionPreference preference, |
| 173 | IPreparedModelCallback callback) |
| 174 | generates (ErrorStatus status); |
Xusong Wang | 89dfafb | 2019-01-11 17:41:11 -0800 | [diff] [blame] | 175 | |
| 176 | /** |
| 177 | * Creates a prepared model from cache files for execution. |
| 178 | * |
| 179 | * prepareModelFromCache is used to retrieve a prepared model directly from |
| 180 | * cache files to avoid slow model compilation time. There are exactly two |
| 181 | * cache file descriptors provided to the driver: modelCache and dataCache. |
| 182 | * |
| 183 | * The dataCache is for caching constant data, possibly including preprocessed |
| 184 | * and transformed tensor buffers. Any modification to the dataCache should |
| 185 | * have no worse effect than generating bad output values at execution time. |
| 186 | * |
| 187 | * The modelCache is for caching security-sensitive data such as compiled |
| 188 | * executable machine code in the device's native binary format. A modification |
| 189 | * to the modelCache may affect the driver's execution behavior, and a malicious |
| 190 | * client could make use of this to execute beyond the granted permission. Thus, |
| 191 | * the driver must always check whether the modelCache is corrupted before preparing |
| 192 | * the model from cache. |
| 193 | * |
| 194 | * The two file descriptors may be closed by the client once the asynchronous |
| 195 | * preparation has finished. The driver has to copy all the data it needs. |
| 196 | * |
| 197 | * The model is prepared asynchronously with respect to the caller. The |
| 198 | * prepareModelFromCache function must verify the inputs to the |
| 199 | * prepareModelFromCache function are correct, and that the security-sensitive |
| 200 | * cache has not been modified since it was last written by the driver. |
| 201 | * If there is an error, or if compilation caching is not supported, or if the |
| 202 | * security-sensitive cache has been modified, prepareModelFromCache must |
| 203 | * immediately invoke the callback with the appropriate ErrorStatus value and |
| 204 | * nullptr for the IPreparedModel, then return with the same ErrorStatus. If |
| 205 | * the inputs to the prepareModelFromCache function are valid, the security-sensitive |
| 206 | * cache is not modified, and there is no error, prepareModelFromCache must launch an |
| 207 | * asynchronous task to prepare the model in the background, and immediately return |
| 208 | * from prepareModelFromCache with ErrorStatus::NONE. If the asynchronous task |
| 209 | * fails to launch, prepareModelFromCache must immediately invoke the callback |
| 210 | * with ErrorStatus::GENERAL_FAILURE and nullptr for the IPreparedModel, then |
| 211 | * return with ErrorStatus::GENERAL_FAILURE. |
| 212 | * |
| 213 | * When the asynchronous task has finished preparing the model, it must |
| 214 | * immediately invoke the callback function provided as an input to |
| 215 | * prepareModelFromCache. If the model was prepared successfully, the |
| 216 | * callback object must be invoked with an error status of ErrorStatus::NONE |
| 217 | * and the produced IPreparedModel object. If an error occurred preparing |
| 218 | * the model, the callback object must be invoked with the appropriate |
| 219 | * ErrorStatus value and nullptr for the IPreparedModel. |
| 220 | * |
| 221 | * The only information that may be unknown to the model at this stage is |
| 222 | * the shape of the tensors, which may only be known at execution time. As |
| 223 | * such, some driver services may return partially prepared models, where |
| 224 | * the prepared model may only be finished when it is paired with a set of |
| 225 | * inputs to the model. Note that the same prepared model object may be |
| 226 | * used with different shapes of inputs on different (possibly concurrent) |
| 227 | * executions. |
| 228 | * |
| 229 | * @param modelCache A handle holding exactly one cache file descriptor for the |
| 230 | * security-sensitive cache. |
| 231 | * @param dataCache A handle holding exactly one cache file descriptor for the |
| 232 | * constants' cache. |
| 233 | * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN |
| 234 | * identifying the prepared model. It is the same token provided when saving |
| 235 | * the cache files with IPreparedModel::saveToCache. Tokens should be chosen |
| 236 | * to have a low rate of collision for a particular application. The driver |
| 237 | * cannot detect a collision; a collision will result in a failed execution |
| 238 | * or in a successful execution that produces incorrect output values. |
| 239 | * @param callback A callback object used to return the error status of |
| 240 | * preparing the model for execution and the prepared model if |
| 241 | * successful, nullptr otherwise. The callback object's notify function |
| 242 | * must be called exactly once, even if the model could not be prepared. |
| 243 | * @return status Error status of launching a task which prepares the model |
| 244 | * in the background; must be: |
| 245 | * - NONE if preparation task is successfully launched |
| 246 | * - DEVICE_UNAVAILABLE if driver is offline or busy |
| 247 | * - GENERAL_FAILURE if caching is not supported or if there is an |
| 248 | * unspecified error |
| 249 | * - INVALID_ARGUMENT if one of the input arguments is invalid |
| 250 | */ |
| 251 | prepareModelFromCache(handle modelCache, handle dataCache, |
| 252 | uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token, |
| 253 | IPreparedModelCallback callback) |
| 254 | generates (ErrorStatus status); |
Slava Shklyaev | 060a9ac | 2018-09-07 15:27:24 +0100 | [diff] [blame] | 255 | }; |