blob: 79f9c325acaf2aa4f66e39e5643c590f13dc2710 [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
Lev Proleev5a7b67a2019-08-08 14:08:31 +010019import @1.1::ExecutionPreference;
20import @1.2::Constant;
21import @1.2::DeviceType;
22import @1.2::Extension;
23import @1.2::IDevice;
Xusong Wang931d5a12019-11-27 12:46:48 -080024import BufferDesc;
25import BufferRole;
26import Capabilities;
Michael Butlerc2499ec2019-12-11 18:31:12 -080027import ErrorStatus;
Xusong Wang931d5a12019-11-27 12:46:48 -080028import Model;
Michael Butlerc2499ec2019-12-11 18:31:12 -080029import OptionalTimePoint;
30import Priority;
Xusong Wang931d5a12019-11-27 12:46:48 -080031import IBuffer;
32import IPreparedModel;
Xusong Wang68c32342019-10-23 10:35:07 -070033import IPreparedModelCallback;
Lev Proleev5a7b67a2019-08-08 14:08:31 +010034
35/**
36 * This interface represents a device driver.
37 */
38interface IDevice extends @1.2::IDevice {
39 /**
40 * Gets the capabilities of a driver.
41 *
42 * @return status Error status of the call, must be:
43 * - NONE if successful
44 * - DEVICE_UNAVAILABLE if driver is offline or busy
45 * - GENERAL_FAILURE if there is an unspecified error
46 * @return capabilities Capabilities of the driver.
47 */
48 getCapabilities_1_3() generates (ErrorStatus status, Capabilities capabilities);
49
50 /**
Michael Butlerc2499ec2019-12-11 18:31:12 -080051 * Returns whether the device is able to complete or abort a task within a
52 * specified duration.
53 *
54 * @return prepareModelDeadline 'true' if the device supports completing or
55 * aborting model preparation by the deadline when the deadline is supplied,
56 * 'false' otherwise.
57 * @return executionDeadline 'true' if the device supports completing or
58 * aborting an execution by the deadline when the deadline is supplied,
59 * 'false' otherwise.
60 */
61 supportsDeadlines() generates (bool prepareModelDeadline, bool executionDeadline);
62
63 /**
Lev Proleev5a7b67a2019-08-08 14:08:31 +010064 * Gets the supported operations in a model.
65 *
Slava Shklyaeva785a3f2019-12-13 12:24:35 +000066 * getSupportedOperations indicates which operations of the top-level
67 * subgraph are fully supported by the vendor driver. If an operation may
68 * not be supported for any reason, getSupportedOperations must return
69 * false for that operation.
70 *
71 * The {@link OperationType::IF} and {@link OperationType::WHILE}
72 * operations may only be fully supported if the vendor driver fully
73 * supports all operations in the referenced subgraphs.
Lev Proleev5a7b67a2019-08-08 14:08:31 +010074 *
75 * @param model A model whose operations--and their corresponding operands--
76 * are to be verified by the driver.
77 * @return status Error status of the call, must be:
78 * - NONE if successful
79 * - DEVICE_UNAVAILABLE if driver is offline or busy
80 * - GENERAL_FAILURE if there is an unspecified error
81 * - INVALID_ARGUMENT if provided model is invalid
82 * @return supportedOperations A list of supported operations, where true
83 * indicates the operation is supported and false indicates the
84 * operation is not supported. The index of "supported" corresponds with
85 * the index of the operation it is describing.
86 */
87 getSupportedOperations_1_3(Model model)
88 generates (ErrorStatus status, vec<bool> supportedOperations);
89
90 /**
91 * Asynchronously creates a prepared model for execution and optionally
92 * saves it into cache files.
93 *
94 * prepareModel is used to make any necessary transformations to or
95 * alternative representations to a model for execution, possibly including
96 * transformations on the constant data, optimization on the model's graph,
97 * or compilation into the device's native binary format. The model itself
98 * is not changed.
99 *
100 * Optionally, caching information may be provided for the driver to save
101 * the prepared model to cache files for faster model compilation time when
102 * the same model preparation is requested in the future. There are two
103 * types of cache file handles provided to the driver: model cache and data
104 * cache. For more information on the two types of cache handles, refer to
105 * getNumberOfCacheFilesNeeded.
106 *
107 * The file descriptors must be opened with read and write permission. A
108 * file may have any size, and the corresponding file descriptor may have
109 * any offset. The driver must truncate a file to zero size before writing
110 * to that file. The file descriptors may be closed by the client once the
111 * asynchronous preparation has finished. The driver must dup a file
112 * descriptor if it wants to get access to the cache file later.
113 *
114 * The model is prepared asynchronously with respect to the caller. The
115 * prepareModel function must verify the inputs to the preparedModel
116 * function related to preparing the model (as opposed to saving the
117 * prepared model to cache) are correct. If there is an error, prepareModel
118 * must immediately invoke the callback with the appropriate ErrorStatus
119 * value and nullptr for the IPreparedModel, then return with the same
120 * ErrorStatus. If the inputs to the prepareModel function that are related
121 * to preparing the model are valid and there is no error, prepareModel must
122 * launch an asynchronous task to prepare the model in the background, and
123 * immediately return from prepareModel with ErrorStatus::NONE. If the
124 * asynchronous task fails to launch, prepareModel must immediately invoke
125 * the callback with ErrorStatus::GENERAL_FAILURE and nullptr for the
126 * IPreparedModel, then return with ErrorStatus::GENERAL_FAILURE.
127 *
128 * When the asynchronous task has finished preparing the model, it must
129 * immediately invoke the callback function provided as an input to
130 * prepareModel. If the model was prepared successfully, the callback object
131 * must be invoked with an error status of ErrorStatus::NONE and the
132 * produced IPreparedModel object. If an error occurred preparing the model,
133 * the callback object must be invoked with the appropriate ErrorStatus
134 * value and nullptr for the IPreparedModel.
135 *
Michael Butlerc2499ec2019-12-11 18:31:12 -0800136 * The model is prepared with a priority. This priority is relative to other
137 * prepared models owned by the same client. Higher priority executions may
138 * use more compute resources than lower priority executions, and may
139 * preempt or starve lower priority executions.
140 *
141 * prepareModel_1_3 can be called with an optional deadline. If the model
142 * is not able to be prepared before the provided deadline, the model
143 * preparation must be aborted, and either {@link
144 * ErrorStatus::MISSED_DEADLINE_TRANSIENT} or {@link
145 * ErrorStatus::MISSED_DEADLINE_PERSISTENT} must be returned. The error due
146 * to an abort must be sent the same way as other errors, described above.
147 * If the service reports that it does not support preparation deadlines via
148 * IDevice::supportsDeadlines, and prepareModel_1_3 is called with a
149 * deadline, then the argument is invalid, and {@link
150 * ErrorStatus::INVALID_ARGUMENT} must be returned.
151 *
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100152 * Optionally, the driver may save the prepared model to cache during the
153 * asynchronous preparation. Any error that occurs when saving to cache must
154 * not affect the status of preparing the model. Even if the input arguments
155 * related to the cache may be invalid, or the driver may fail to save to
156 * cache, the prepareModel function must finish preparing the model. The
157 * driver may choose not to save to cache even if the caching information is
158 * provided and valid.
159 *
160 * The only information that may be unknown to the model at this stage is
161 * the shape of the tensors, which may only be known at execution time. As
162 * such, some driver services may return partially prepared models, where
163 * the prepared model may only be finished when it is paired with a set of
164 * inputs to the model. Note that the same prepared model object may be used
165 * with different shapes of inputs on different (possibly concurrent)
166 * executions.
167 *
168 * Multiple threads may call prepareModel on the same model concurrently.
169 *
170 * @param model The model to be prepared for execution.
171 * @param preference Indicates the intended execution behavior of a prepared
172 * model.
Michael Butlerc2499ec2019-12-11 18:31:12 -0800173 * @param priority The priority of the prepared model relative to other
174 * prepared models owned by the client.
175 * @param deadline The time by which the model must be prepared. If the
176 * model cannot be prepared by the deadline, the preparation must be
177 * aborted.
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100178 * @param modelCache A vector of handles with each entry holding exactly one
179 * cache file descriptor for the security-sensitive cache. The length of
180 * the vector must either be 0 indicating that caching information is
181 * not provided, or match the numModelCache returned from
182 * getNumberOfCacheFilesNeeded. The cache handles will be provided in
183 * the same order when retrieving the preparedModel from cache files
Xusong Wang68c32342019-10-23 10:35:07 -0700184 * with prepareModelFromCache_1_3.
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100185 * @param dataCache A vector of handles with each entry holding exactly one
186 * cache file descriptor for the constants' cache. The length of the
187 * vector must either be 0 indicating that caching information is not
188 * provided, or match the numDataCache returned from
189 * getNumberOfCacheFilesNeeded. The cache handles will be provided in
190 * the same order when retrieving the preparedModel from cache files
Xusong Wang68c32342019-10-23 10:35:07 -0700191 * with prepareModelFromCache_1_3.
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100192 * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN
193 * identifying the prepared model. The same token will be provided when
194 * retrieving the prepared model from the cache files with
Xusong Wang68c32342019-10-23 10:35:07 -0700195 * prepareModelFromCache_1_3. Tokens should be chosen to have a low rate of
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100196 * collision for a particular application. The driver cannot detect a
197 * collision; a collision will result in a failed execution or in a
198 * successful execution that produces incorrect output values. If both
199 * modelCache and dataCache are empty indicating that caching
200 * information is not provided, this token must be ignored.
201 * @param callback A callback object used to return the error status of
202 * preparing the model for execution and the prepared model if
203 * successful, nullptr otherwise. The callback object's notify function
204 * must be called exactly once, even if the model could not be prepared.
205 * @return status Error status of launching a task which prepares the model
206 * in the background; must be:
207 * - NONE if preparation task is successfully launched
208 * - DEVICE_UNAVAILABLE if driver is offline or busy
209 * - GENERAL_FAILURE if there is an unspecified error
210 * - INVALID_ARGUMENT if one of the input arguments related to preparing
211 * the model is invalid
Michael Butlerc2499ec2019-12-11 18:31:12 -0800212 * - MISSED_DEADLINE_* if the deadline for preparing a model cannot be
213 * met
214 * - RESOURCE_EXHAUSTED_* if the task was aborted by the driver
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100215 */
216 prepareModel_1_3(Model model, ExecutionPreference preference,
Michael Butlerc2499ec2019-12-11 18:31:12 -0800217 Priority priority, OptionalTimePoint deadline,
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100218 vec<handle> modelCache, vec<handle> dataCache,
219 uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token,
220 IPreparedModelCallback callback)
221 generates (ErrorStatus status);
Xusong Wang68c32342019-10-23 10:35:07 -0700222
223 /**
224 * Creates a prepared model from cache files for execution.
225 *
226 * prepareModelFromCache_1_3 is used to retrieve a prepared model directly from
227 * cache files to avoid slow model compilation time. There are
228 * two types of cache file handles provided to the driver: model cache
229 * and data cache. For more information on the two types of cache handles,
230 * refer to getNumberOfCacheFilesNeeded.
231 *
232 * The file descriptors must be opened with read and write permission. A file may
233 * have any size, and the corresponding file descriptor may have any offset. The
234 * driver must truncate a file to zero size before writing to that file. The file
235 * descriptors may be closed by the client once the asynchronous preparation has
236 * finished. The driver must dup a file descriptor if it wants to get access to
237 * the cache file later.
238 *
239 * The model is prepared asynchronously with respect to the caller. The
240 * prepareModelFromCache_1_3 function must verify the inputs to the
241 * prepareModelFromCache_1_3 function are correct, and that the security-sensitive
242 * cache has not been modified since it was last written by the driver.
243 * If there is an error, or if compilation caching is not supported, or if the
244 * security-sensitive cache has been modified, prepareModelFromCache_1_3 must
245 * immediately invoke the callback with the appropriate ErrorStatus value and
246 * nullptr for the IPreparedModel, then return with the same ErrorStatus. If
247 * the inputs to the prepareModelFromCache_1_3 function are valid, the security-sensitive
248 * cache is not modified, and there is no error, prepareModelFromCache_1_3 must launch an
249 * asynchronous task to prepare the model in the background, and immediately return
250 * from prepareModelFromCache_1_3 with ErrorStatus::NONE. If the asynchronous task
251 * fails to launch, prepareModelFromCache_1_3 must immediately invoke the callback
252 * with ErrorStatus::GENERAL_FAILURE and nullptr for the IPreparedModel, then
253 * return with ErrorStatus::GENERAL_FAILURE.
254 *
255 * When the asynchronous task has finished preparing the model, it must
256 * immediately invoke the callback function provided as an input to
257 * prepareModelFromCache_1_3. If the model was prepared successfully, the
258 * callback object must be invoked with an error status of ErrorStatus::NONE
259 * and the produced IPreparedModel object. If an error occurred preparing
260 * the model, the callback object must be invoked with the appropriate
261 * ErrorStatus value and nullptr for the IPreparedModel.
262 *
Michael Butlerc2499ec2019-12-11 18:31:12 -0800263 * prepareModelFromCache_1_3 can be called with an optional deadline. If the
264 * model is not able to prepared before the provided deadline, the model
265 * preparation must be aborted, and either {@link
266 * ErrorStatus::MISSED_DEADLINE_TRANSIENT}
267 * or {@link ErrorStatus::MISSED_DEADLINE_PERSISTENT} must be returned. The
268 * error due to an abort must be sent the same way as other errors,
269 * described above. If the service reports that it does not support
270 * preparation deadlines via IDevice::supportsDeadlines, and
271 * prepareModelFromCache_1_3 is called with a deadline, then the argument is
272 * invalid, and {@link ErrorStatus::INVALID_ARGUMENT} must be returned.
273 *
Xusong Wang68c32342019-10-23 10:35:07 -0700274 * The only information that may be unknown to the model at this stage is
275 * the shape of the tensors, which may only be known at execution time. As
276 * such, some driver services may return partially prepared models, where
277 * the prepared model may only be finished when it is paired with a set of
278 * inputs to the model. Note that the same prepared model object may be
279 * used with different shapes of inputs on different (possibly concurrent)
280 * executions.
281 *
Michael Butlerc2499ec2019-12-11 18:31:12 -0800282 * @param deadline The time by which the model must be prepared. If the
283 * model cannot be prepared by the deadline, the preparation must be
284 * aborted.
Xusong Wang68c32342019-10-23 10:35:07 -0700285 * @param modelCache A vector of handles with each entry holding exactly one
286 * cache file descriptor for the security-sensitive cache. The length of
287 * the vector must match the numModelCache returned from getNumberOfCacheFilesNeeded.
288 * The cache handles will be provided in the same order as with prepareModel_1_3.
289 * @param dataCache A vector of handles with each entry holding exactly one
290 * cache file descriptor for the constants' cache. The length of the vector
291 * must match the numDataCache returned from getNumberOfCacheFilesNeeded.
292 * The cache handles will be provided in the same order as with prepareModel_1_3.
293 * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN
294 * identifying the prepared model. It is the same token provided when saving
295 * the cache files with prepareModel_1_3. Tokens should be chosen
296 * to have a low rate of collision for a particular application. The driver
297 * cannot detect a collision; a collision will result in a failed execution
298 * or in a successful execution that produces incorrect output values.
299 * @param callback A callback object used to return the error status of
300 * preparing the model for execution and the prepared model if
301 * successful, nullptr otherwise. The callback object's notify function
302 * must be called exactly once, even if the model could not be prepared.
303 * @return status Error status of launching a task which prepares the model
304 * in the background; must be:
305 * - NONE if preparation task is successfully launched
306 * - DEVICE_UNAVAILABLE if driver is offline or busy
307 * - GENERAL_FAILURE if caching is not supported or if there is an
308 * unspecified error
309 * - INVALID_ARGUMENT if one of the input arguments is invalid
Michael Butlerc2499ec2019-12-11 18:31:12 -0800310 * - MISSED_DEADLINE_* if the deadline for preparing a model cannot be
311 * met
312 * - RESOURCE_EXHAUSTED_* if the task was aborted by the driver
Xusong Wang68c32342019-10-23 10:35:07 -0700313 */
Michael Butlerc90250f2020-01-31 18:39:22 -0800314 prepareModelFromCache_1_3(OptionalTimePoint deadline,
Michael Butlerc2499ec2019-12-11 18:31:12 -0800315 vec<handle> modelCache, vec<handle> dataCache,
Xusong Wang68c32342019-10-23 10:35:07 -0700316 uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token,
317 IPreparedModelCallback callback)
318 generates (ErrorStatus status);
Xusong Wang931d5a12019-11-27 12:46:48 -0800319
320 /**
321 * Allocates a driver-managed buffer with the properties specified by the buffer descriptor
322 * as well as the input and output roles.
323 *
324 * The allocate function must verify its inputs are correct. If there is an error, or if a
325 * certain role or property is not supported by the driver, the allocate
326 * function must return with an appropriate ErrorStatus, a nullptr as the IBuffer, and 0 as the
327 * buffer token. If the allocation is successful, this method must return with ErrorStatus::NONE
328 * and the produced IBuffer with a positive token identifying the allocated buffer. A successful
329 * allocation must accommodate all of the specified roles and buffer properties.
330 *
331 * The buffer is allocated to an uninitialized state. An uninitialized buffer may only be used
332 * in ways that are specified by outputRoles. A buffer is initialized after it is used as an
333 * output in a successful execution, or after a successful invocation of IBuffer::copyFrom on
334 * the buffer. An initialized buffer may be used according to all roles specified in inputRoles
335 * and outputRoles. A buffer will return to the uninitialized state if it is used as an output
336 * in a failed execution, or after a failed invocation of IBuffer::copyFrom on the buffer.
337 *
338 * The dimensions of the buffer can be deduced from the buffer descriptor as well as the
339 * dimensions of the corresponding model operands of the input and output roles. The dimensions
340 * or rank of the buffer may be unknown at this stage. As such, some driver services may only
341 * create a placeholder and defer the actual allocation until execution time. Note that the
342 * same buffer may be used for different shapes of outputs on different executions. When the
343 * buffer is used as an input, the input shape must be the same as the output shape from the
344 * last execution using this buffer as an output.
345 *
346 * The driver must apply proper validatation upon every usage of the buffer, and must fail the
347 * execution immediately if the usage is illegal.
348 *
349 * @param desc A buffer descriptor specifying the properties of the buffer to allocate.
350 * @param preparedModels A vector of IPreparedModel objects. Must only contain IPreparedModel
351 * objects from the same IDevice as this method is being invoked on.
352 * @param inputRoles A vector of roles with each specifying an input to a prepared model.
353 * @param outputRoles A vector of roles with each specifying an output to a prepared model.
354 * Each role specified in inputRoles and outputRoles must be unique. The corresponding
355 * model operands of the roles must have the same OperandType, scale, zero point, and
356 * ExtraParams. The dimensions of the operands and the dimensions specified in the buffer
357 * descriptor must be compatible with each other. Two dimensions are incompatible if there
358 * is at least one axis that is fully specified in both but has different values.
359 * @return status Error status of the buffer allocation. Must be:
360 * - NONE if successful
361 * - DEVICE_UNAVAILABLE if driver is offline or busy
362 * - GENERAL_FAILURE if a certain buffer property or a certain role is not supported,
363 * or if there is an unspecified error
364 * - INVALID_ARGUMENT if one of the input arguments is invalid
365 * @return buffer The allocated IBuffer object. If the buffer was unable to be allocated
366 * due to an error, nullptr must be returned.
367 * @return token A positive token identifying the allocated buffer. The same token will be
368 * provided when referencing the buffer as one of the memory pools in the request of an
369 * execution. The token must not collide with the tokens of other IBuffer objects that are
370 * currently alive in the same driver service. If the buffer was unable to be allocated
371 * due to an error, the token must be 0.
372 */
373 allocate(BufferDesc desc, vec<IPreparedModel> preparedModels, vec<BufferRole> inputRoles,
374 vec<BufferRole> outputRoles)
Michael Butler6a4172c2020-02-04 16:15:04 -0800375 generates (ErrorStatus status, IBuffer buffer, uint32_t token);
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100376};