blob: 1295d6ac23b20a37e641ba2dd479872c9fef2e18 [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 Wang68c32342019-10-23 10:35:07 -070025import IPreparedModelCallback;
Lev Proleev5a7b67a2019-08-08 14:08:31 +010026
27/**
28 * This interface represents a device driver.
29 */
30interface IDevice extends @1.2::IDevice {
31 /**
32 * Gets the capabilities of a driver.
33 *
34 * @return status Error status of the call, must be:
35 * - NONE if successful
36 * - DEVICE_UNAVAILABLE if driver is offline or busy
37 * - GENERAL_FAILURE if there is an unspecified error
38 * @return capabilities Capabilities of the driver.
39 */
40 getCapabilities_1_3() generates (ErrorStatus status, Capabilities capabilities);
41
42 /**
43 * Gets the supported operations in a model.
44 *
45 * getSupportedOperations indicates which operations of a model are fully
46 * supported by the vendor driver. If an operation may not be supported for
47 * any reason, getSupportedOperations must return false for that operation.
48 *
49 * @param model A model whose operations--and their corresponding operands--
50 * are to be verified by the driver.
51 * @return status Error status of the call, must be:
52 * - NONE if successful
53 * - DEVICE_UNAVAILABLE if driver is offline or busy
54 * - GENERAL_FAILURE if there is an unspecified error
55 * - INVALID_ARGUMENT if provided model is invalid
56 * @return supportedOperations A list of supported operations, where true
57 * indicates the operation is supported and false indicates the
58 * operation is not supported. The index of "supported" corresponds with
59 * the index of the operation it is describing.
60 */
61 getSupportedOperations_1_3(Model model)
62 generates (ErrorStatus status, vec<bool> supportedOperations);
63
64 /**
65 * Asynchronously creates a prepared model for execution and optionally
66 * saves it into cache files.
67 *
68 * prepareModel is used to make any necessary transformations to or
69 * alternative representations to a model for execution, possibly including
70 * transformations on the constant data, optimization on the model's graph,
71 * or compilation into the device's native binary format. The model itself
72 * is not changed.
73 *
74 * Optionally, caching information may be provided for the driver to save
75 * the prepared model to cache files for faster model compilation time when
76 * the same model preparation is requested in the future. There are two
77 * types of cache file handles provided to the driver: model cache and data
78 * cache. For more information on the two types of cache handles, refer to
79 * getNumberOfCacheFilesNeeded.
80 *
81 * The file descriptors must be opened with read and write permission. A
82 * file may have any size, and the corresponding file descriptor may have
83 * any offset. The driver must truncate a file to zero size before writing
84 * to that file. The file descriptors may be closed by the client once the
85 * asynchronous preparation has finished. The driver must dup a file
86 * descriptor if it wants to get access to the cache file later.
87 *
88 * The model is prepared asynchronously with respect to the caller. The
89 * prepareModel function must verify the inputs to the preparedModel
90 * function related to preparing the model (as opposed to saving the
91 * prepared model to cache) are correct. If there is an error, prepareModel
92 * must immediately invoke the callback with the appropriate ErrorStatus
93 * value and nullptr for the IPreparedModel, then return with the same
94 * ErrorStatus. If the inputs to the prepareModel function that are related
95 * to preparing the model are valid and there is no error, prepareModel must
96 * launch an asynchronous task to prepare the model in the background, and
97 * immediately return from prepareModel with ErrorStatus::NONE. If the
98 * asynchronous task fails to launch, prepareModel must immediately invoke
99 * the callback with ErrorStatus::GENERAL_FAILURE and nullptr for the
100 * IPreparedModel, then return with ErrorStatus::GENERAL_FAILURE.
101 *
102 * When the asynchronous task has finished preparing the model, it must
103 * immediately invoke the callback function provided as an input to
104 * prepareModel. If the model was prepared successfully, the callback object
105 * must be invoked with an error status of ErrorStatus::NONE and the
106 * produced IPreparedModel object. If an error occurred preparing the model,
107 * the callback object must be invoked with the appropriate ErrorStatus
108 * value and nullptr for the IPreparedModel.
109 *
110 * Optionally, the driver may save the prepared model to cache during the
111 * asynchronous preparation. Any error that occurs when saving to cache must
112 * not affect the status of preparing the model. Even if the input arguments
113 * related to the cache may be invalid, or the driver may fail to save to
114 * cache, the prepareModel function must finish preparing the model. The
115 * driver may choose not to save to cache even if the caching information is
116 * provided and valid.
117 *
118 * The only information that may be unknown to the model at this stage is
119 * the shape of the tensors, which may only be known at execution time. As
120 * such, some driver services may return partially prepared models, where
121 * the prepared model may only be finished when it is paired with a set of
122 * inputs to the model. Note that the same prepared model object may be used
123 * with different shapes of inputs on different (possibly concurrent)
124 * executions.
125 *
126 * Multiple threads may call prepareModel on the same model concurrently.
127 *
128 * @param model The model to be prepared for execution.
129 * @param preference Indicates the intended execution behavior of a prepared
130 * model.
131 * @param modelCache A vector of handles with each entry holding exactly one
132 * cache file descriptor for the security-sensitive cache. The length of
133 * the vector must either be 0 indicating that caching information is
134 * not provided, or match the numModelCache returned from
135 * getNumberOfCacheFilesNeeded. The cache handles will be provided in
136 * the same order when retrieving the preparedModel from cache files
Xusong Wang68c32342019-10-23 10:35:07 -0700137 * with prepareModelFromCache_1_3.
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100138 * @param dataCache A vector of handles with each entry holding exactly one
139 * cache file descriptor for the constants' cache. The length of the
140 * vector must either be 0 indicating that caching information is not
141 * provided, or match the numDataCache returned from
142 * getNumberOfCacheFilesNeeded. The cache handles will be provided in
143 * the same order when retrieving the preparedModel from cache files
Xusong Wang68c32342019-10-23 10:35:07 -0700144 * with prepareModelFromCache_1_3.
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100145 * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN
146 * identifying the prepared model. The same token will be provided when
147 * retrieving the prepared model from the cache files with
Xusong Wang68c32342019-10-23 10:35:07 -0700148 * prepareModelFromCache_1_3. Tokens should be chosen to have a low rate of
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100149 * collision for a particular application. The driver cannot detect a
150 * collision; a collision will result in a failed execution or in a
151 * successful execution that produces incorrect output values. If both
152 * modelCache and dataCache are empty indicating that caching
153 * information is not provided, this token must be ignored.
154 * @param callback A callback object used to return the error status of
155 * preparing the model for execution and the prepared model if
156 * successful, nullptr otherwise. The callback object's notify function
157 * must be called exactly once, even if the model could not be prepared.
158 * @return status Error status of launching a task which prepares the model
159 * in the background; must be:
160 * - NONE if preparation task is successfully launched
161 * - DEVICE_UNAVAILABLE if driver is offline or busy
162 * - GENERAL_FAILURE if there is an unspecified error
163 * - INVALID_ARGUMENT if one of the input arguments related to preparing
164 * the model is invalid
165 */
166 prepareModel_1_3(Model model, ExecutionPreference preference,
167 vec<handle> modelCache, vec<handle> dataCache,
168 uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token,
169 IPreparedModelCallback callback)
170 generates (ErrorStatus status);
Xusong Wang68c32342019-10-23 10:35:07 -0700171
172 /**
173 * Creates a prepared model from cache files for execution.
174 *
175 * prepareModelFromCache_1_3 is used to retrieve a prepared model directly from
176 * cache files to avoid slow model compilation time. There are
177 * two types of cache file handles provided to the driver: model cache
178 * and data cache. For more information on the two types of cache handles,
179 * refer to getNumberOfCacheFilesNeeded.
180 *
181 * The file descriptors must be opened with read and write permission. A file may
182 * have any size, and the corresponding file descriptor may have any offset. The
183 * driver must truncate a file to zero size before writing to that file. The file
184 * descriptors may be closed by the client once the asynchronous preparation has
185 * finished. The driver must dup a file descriptor if it wants to get access to
186 * the cache file later.
187 *
188 * The model is prepared asynchronously with respect to the caller. The
189 * prepareModelFromCache_1_3 function must verify the inputs to the
190 * prepareModelFromCache_1_3 function are correct, and that the security-sensitive
191 * cache has not been modified since it was last written by the driver.
192 * If there is an error, or if compilation caching is not supported, or if the
193 * security-sensitive cache has been modified, prepareModelFromCache_1_3 must
194 * immediately invoke the callback with the appropriate ErrorStatus value and
195 * nullptr for the IPreparedModel, then return with the same ErrorStatus. If
196 * the inputs to the prepareModelFromCache_1_3 function are valid, the security-sensitive
197 * cache is not modified, and there is no error, prepareModelFromCache_1_3 must launch an
198 * asynchronous task to prepare the model in the background, and immediately return
199 * from prepareModelFromCache_1_3 with ErrorStatus::NONE. If the asynchronous task
200 * fails to launch, prepareModelFromCache_1_3 must immediately invoke the callback
201 * with ErrorStatus::GENERAL_FAILURE and nullptr for the IPreparedModel, then
202 * return with ErrorStatus::GENERAL_FAILURE.
203 *
204 * When the asynchronous task has finished preparing the model, it must
205 * immediately invoke the callback function provided as an input to
206 * prepareModelFromCache_1_3. If the model was prepared successfully, the
207 * callback object must be invoked with an error status of ErrorStatus::NONE
208 * and the produced IPreparedModel object. If an error occurred preparing
209 * the model, the callback object must be invoked with the appropriate
210 * ErrorStatus value and nullptr for the IPreparedModel.
211 *
212 * The only information that may be unknown to the model at this stage is
213 * the shape of the tensors, which may only be known at execution time. As
214 * such, some driver services may return partially prepared models, where
215 * the prepared model may only be finished when it is paired with a set of
216 * inputs to the model. Note that the same prepared model object may be
217 * used with different shapes of inputs on different (possibly concurrent)
218 * executions.
219 *
220 * @param modelCache A vector of handles with each entry holding exactly one
221 * cache file descriptor for the security-sensitive cache. The length of
222 * the vector must match the numModelCache returned from getNumberOfCacheFilesNeeded.
223 * The cache handles will be provided in the same order as with prepareModel_1_3.
224 * @param dataCache A vector of handles with each entry holding exactly one
225 * cache file descriptor for the constants' cache. The length of the vector
226 * must match the numDataCache returned from getNumberOfCacheFilesNeeded.
227 * The cache handles will be provided in the same order as with prepareModel_1_3.
228 * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN
229 * identifying the prepared model. It is the same token provided when saving
230 * the cache files with prepareModel_1_3. Tokens should be chosen
231 * to have a low rate of collision for a particular application. The driver
232 * cannot detect a collision; a collision will result in a failed execution
233 * or in a successful execution that produces incorrect output values.
234 * @param callback A callback object used to return the error status of
235 * preparing the model for execution and the prepared model if
236 * successful, nullptr otherwise. The callback object's notify function
237 * must be called exactly once, even if the model could not be prepared.
238 * @return status Error status of launching a task which prepares the model
239 * in the background; must be:
240 * - NONE if preparation task is successfully launched
241 * - DEVICE_UNAVAILABLE if driver is offline or busy
242 * - GENERAL_FAILURE if caching is not supported or if there is an
243 * unspecified error
244 * - INVALID_ARGUMENT if one of the input arguments is invalid
245 */
246 prepareModelFromCache_1_3(vec<handle> modelCache, vec<handle> dataCache,
247 uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token,
248 IPreparedModelCallback callback)
249 generates (ErrorStatus status);
Lev Proleev5a7b67a2019-08-08 14:08:31 +0100250};