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