xref: /aosp_15_r20/hardware/interfaces/neuralnetworks/1.3/IDevice.hal (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1*4d7e907cSAndroid Build Coastguard Worker/*
2*4d7e907cSAndroid Build Coastguard Worker * Copyright (C) 2019 The Android Open Source Project
3*4d7e907cSAndroid Build Coastguard Worker *
4*4d7e907cSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*4d7e907cSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*4d7e907cSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*4d7e907cSAndroid Build Coastguard Worker *
8*4d7e907cSAndroid Build Coastguard Worker *      http://www.apache.org/licenses/LICENSE-2.0
9*4d7e907cSAndroid Build Coastguard Worker *
10*4d7e907cSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*4d7e907cSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*4d7e907cSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4d7e907cSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*4d7e907cSAndroid Build Coastguard Worker * limitations under the License.
15*4d7e907cSAndroid Build Coastguard Worker */
16*4d7e907cSAndroid Build Coastguard Worker
17*4d7e907cSAndroid Build Coastguard Workerpackage [email protected];
18*4d7e907cSAndroid Build Coastguard Worker
19*4d7e907cSAndroid Build Coastguard Workerimport @1.1::ExecutionPreference;
20*4d7e907cSAndroid Build Coastguard Workerimport @1.2::Constant;
21*4d7e907cSAndroid Build Coastguard Workerimport @1.2::DeviceType;
22*4d7e907cSAndroid Build Coastguard Workerimport @1.2::Extension;
23*4d7e907cSAndroid Build Coastguard Workerimport @1.2::IDevice;
24*4d7e907cSAndroid Build Coastguard Workerimport BufferDesc;
25*4d7e907cSAndroid Build Coastguard Workerimport BufferRole;
26*4d7e907cSAndroid Build Coastguard Workerimport Capabilities;
27*4d7e907cSAndroid Build Coastguard Workerimport ErrorStatus;
28*4d7e907cSAndroid Build Coastguard Workerimport Model;
29*4d7e907cSAndroid Build Coastguard Workerimport OptionalTimePoint;
30*4d7e907cSAndroid Build Coastguard Workerimport Priority;
31*4d7e907cSAndroid Build Coastguard Workerimport IBuffer;
32*4d7e907cSAndroid Build Coastguard Workerimport IPreparedModel;
33*4d7e907cSAndroid Build Coastguard Workerimport IPreparedModelCallback;
34*4d7e907cSAndroid Build Coastguard Worker
35*4d7e907cSAndroid Build Coastguard Worker/**
36*4d7e907cSAndroid Build Coastguard Worker * This interface represents a device driver.
37*4d7e907cSAndroid Build Coastguard Worker */
38*4d7e907cSAndroid Build Coastguard Workerinterface IDevice extends @1.2::IDevice {
39*4d7e907cSAndroid Build Coastguard Worker    /**
40*4d7e907cSAndroid Build Coastguard Worker     * Gets the capabilities of a driver.
41*4d7e907cSAndroid Build Coastguard Worker     *
42*4d7e907cSAndroid Build Coastguard Worker     * @return status Error status of the call, must be:
43*4d7e907cSAndroid Build Coastguard Worker     *                - NONE if successful
44*4d7e907cSAndroid Build Coastguard Worker     *                - DEVICE_UNAVAILABLE if driver is offline or busy
45*4d7e907cSAndroid Build Coastguard Worker     *                - GENERAL_FAILURE if there is an unspecified error
46*4d7e907cSAndroid Build Coastguard Worker     * @return capabilities Capabilities of the driver.
47*4d7e907cSAndroid Build Coastguard Worker     */
48*4d7e907cSAndroid Build Coastguard Worker    getCapabilities_1_3() generates (ErrorStatus status, Capabilities capabilities);
49*4d7e907cSAndroid Build Coastguard Worker
50*4d7e907cSAndroid Build Coastguard Worker    /**
51*4d7e907cSAndroid Build Coastguard Worker     * Gets the supported operations in a model.
52*4d7e907cSAndroid Build Coastguard Worker     *
53*4d7e907cSAndroid Build Coastguard Worker     * getSupportedOperations indicates which operations of the top-level
54*4d7e907cSAndroid Build Coastguard Worker     * subgraph are fully supported by the vendor driver. If an operation may
55*4d7e907cSAndroid Build Coastguard Worker     * not be supported for any reason, getSupportedOperations must return
56*4d7e907cSAndroid Build Coastguard Worker     * false for that operation.
57*4d7e907cSAndroid Build Coastguard Worker     *
58*4d7e907cSAndroid Build Coastguard Worker     * The {@link OperationType::IF} and {@link OperationType::WHILE}
59*4d7e907cSAndroid Build Coastguard Worker     * operations may only be fully supported if the vendor driver fully
60*4d7e907cSAndroid Build Coastguard Worker     * supports all operations in the referenced subgraphs.
61*4d7e907cSAndroid Build Coastguard Worker     *
62*4d7e907cSAndroid Build Coastguard Worker     * @param model A model whose operations--and their corresponding operands--
63*4d7e907cSAndroid Build Coastguard Worker     *     are to be verified by the driver.
64*4d7e907cSAndroid Build Coastguard Worker     * @return status Error status of the call, must be:
65*4d7e907cSAndroid Build Coastguard Worker     *     - NONE if successful
66*4d7e907cSAndroid Build Coastguard Worker     *     - DEVICE_UNAVAILABLE if driver is offline or busy
67*4d7e907cSAndroid Build Coastguard Worker     *     - GENERAL_FAILURE if there is an unspecified error
68*4d7e907cSAndroid Build Coastguard Worker     *     - INVALID_ARGUMENT if provided model is invalid
69*4d7e907cSAndroid Build Coastguard Worker     * @return supportedOperations A list of supported operations, where true
70*4d7e907cSAndroid Build Coastguard Worker     *     indicates the operation is supported and false indicates the
71*4d7e907cSAndroid Build Coastguard Worker     *     operation is not supported. The index of "supported" corresponds with
72*4d7e907cSAndroid Build Coastguard Worker     *     the index of the operation it is describing.
73*4d7e907cSAndroid Build Coastguard Worker     */
74*4d7e907cSAndroid Build Coastguard Worker    getSupportedOperations_1_3(Model model)
75*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorStatus status, vec<bool> supportedOperations);
76*4d7e907cSAndroid Build Coastguard Worker
77*4d7e907cSAndroid Build Coastguard Worker    /**
78*4d7e907cSAndroid Build Coastguard Worker     * Asynchronously creates a prepared model for execution and optionally
79*4d7e907cSAndroid Build Coastguard Worker     * saves it into cache files.
80*4d7e907cSAndroid Build Coastguard Worker     *
81*4d7e907cSAndroid Build Coastguard Worker     * prepareModel is used to make any necessary transformations to or
82*4d7e907cSAndroid Build Coastguard Worker     * alternative representations to a model for execution, possibly including
83*4d7e907cSAndroid Build Coastguard Worker     * transformations on the constant data, optimization on the model's graph,
84*4d7e907cSAndroid Build Coastguard Worker     * or compilation into the device's native binary format. The model itself
85*4d7e907cSAndroid Build Coastguard Worker     * is not changed.
86*4d7e907cSAndroid Build Coastguard Worker     *
87*4d7e907cSAndroid Build Coastguard Worker     * Optionally, caching information may be provided for the driver to save
88*4d7e907cSAndroid Build Coastguard Worker     * the prepared model to cache files for faster model compilation time when
89*4d7e907cSAndroid Build Coastguard Worker     * the same model preparation is requested in the future. There are two
90*4d7e907cSAndroid Build Coastguard Worker     * types of cache file handles provided to the driver: model cache and data
91*4d7e907cSAndroid Build Coastguard Worker     * cache. For more information on the two types of cache handles, refer to
92*4d7e907cSAndroid Build Coastguard Worker     * getNumberOfCacheFilesNeeded.
93*4d7e907cSAndroid Build Coastguard Worker     *
94*4d7e907cSAndroid Build Coastguard Worker     * The file descriptors must be opened with read and write permission. A
95*4d7e907cSAndroid Build Coastguard Worker     * file may have any size, and the corresponding file descriptor may have
96*4d7e907cSAndroid Build Coastguard Worker     * any offset. The driver must truncate a file to zero size before writing
97*4d7e907cSAndroid Build Coastguard Worker     * to that file. The file descriptors may be closed by the client once the
98*4d7e907cSAndroid Build Coastguard Worker     * asynchronous preparation has finished. The driver must dup a file
99*4d7e907cSAndroid Build Coastguard Worker     * descriptor if it wants to get access to the cache file later.
100*4d7e907cSAndroid Build Coastguard Worker     *
101*4d7e907cSAndroid Build Coastguard Worker     * The model is prepared asynchronously with respect to the caller. The
102*4d7e907cSAndroid Build Coastguard Worker     * prepareModel function must verify the inputs to the preparedModel
103*4d7e907cSAndroid Build Coastguard Worker     * function related to preparing the model (as opposed to saving the
104*4d7e907cSAndroid Build Coastguard Worker     * prepared model to cache) are correct. If there is an error, prepareModel
105*4d7e907cSAndroid Build Coastguard Worker     * must immediately invoke the callback with the appropriate ErrorStatus
106*4d7e907cSAndroid Build Coastguard Worker     * value and nullptr for the IPreparedModel, then return with the same
107*4d7e907cSAndroid Build Coastguard Worker     * ErrorStatus. If the inputs to the prepareModel function that are related
108*4d7e907cSAndroid Build Coastguard Worker     * to preparing the model are valid and there is no error, prepareModel must
109*4d7e907cSAndroid Build Coastguard Worker     * launch an asynchronous task to prepare the model in the background, and
110*4d7e907cSAndroid Build Coastguard Worker     * immediately return from prepareModel with ErrorStatus::NONE. If the
111*4d7e907cSAndroid Build Coastguard Worker     * asynchronous task fails to launch, prepareModel must immediately invoke
112*4d7e907cSAndroid Build Coastguard Worker     * the callback with ErrorStatus::GENERAL_FAILURE and nullptr for the
113*4d7e907cSAndroid Build Coastguard Worker     * IPreparedModel, then return with ErrorStatus::GENERAL_FAILURE.
114*4d7e907cSAndroid Build Coastguard Worker     *
115*4d7e907cSAndroid Build Coastguard Worker     * When the asynchronous task has finished preparing the model, it must
116*4d7e907cSAndroid Build Coastguard Worker     * immediately invoke the callback function provided as an input to
117*4d7e907cSAndroid Build Coastguard Worker     * prepareModel. If the model was prepared successfully, the callback object
118*4d7e907cSAndroid Build Coastguard Worker     * must be invoked with an error status of ErrorStatus::NONE and the
119*4d7e907cSAndroid Build Coastguard Worker     * produced IPreparedModel object. If an error occurred preparing the model,
120*4d7e907cSAndroid Build Coastguard Worker     * the callback object must be invoked with the appropriate ErrorStatus
121*4d7e907cSAndroid Build Coastguard Worker     * value and nullptr for the IPreparedModel.
122*4d7e907cSAndroid Build Coastguard Worker     *
123*4d7e907cSAndroid Build Coastguard Worker     * The model is prepared with a priority. This priority is relative to other
124*4d7e907cSAndroid Build Coastguard Worker     * prepared models owned by the same client. Higher priority executions may
125*4d7e907cSAndroid Build Coastguard Worker     * use more compute resources than lower priority executions, and may
126*4d7e907cSAndroid Build Coastguard Worker     * preempt or starve lower priority executions.
127*4d7e907cSAndroid Build Coastguard Worker     *
128*4d7e907cSAndroid Build Coastguard Worker     * prepareModel_1_3 can be called with an optional deadline. If the model
129*4d7e907cSAndroid Build Coastguard Worker     * is not able to be prepared before the provided deadline, the model
130*4d7e907cSAndroid Build Coastguard Worker     * preparation may be aborted, and either {@link
131*4d7e907cSAndroid Build Coastguard Worker     * ErrorStatus::MISSED_DEADLINE_TRANSIENT} or {@link
132*4d7e907cSAndroid Build Coastguard Worker     * ErrorStatus::MISSED_DEADLINE_PERSISTENT} may be returned. The error due
133*4d7e907cSAndroid Build Coastguard Worker     * to an abort must be sent the same way as other errors, described above.
134*4d7e907cSAndroid Build Coastguard Worker     * The deadline is represented as nanoseconds since the epoch of the steady
135*4d7e907cSAndroid Build Coastguard Worker     * clock (as if from std::chrono::steady_clock::time_point), but the service
136*4d7e907cSAndroid Build Coastguard Worker     * may convert it to the nanoseconds since boot time (as if from
137*4d7e907cSAndroid Build Coastguard Worker     * clock_gettime(CLOCK_BOOTTIME, &ts) or
138*4d7e907cSAndroid Build Coastguard Worker     * android::base::boot_clock::time_point) to account for time when the
139*4d7e907cSAndroid Build Coastguard Worker     * system is suspended. This conversion can by done by finding the timeout
140*4d7e907cSAndroid Build Coastguard Worker     * duration remaining compared to the steady_clock and adding it to the
141*4d7e907cSAndroid Build Coastguard Worker     * current boot_clock time.
142*4d7e907cSAndroid Build Coastguard Worker     *
143*4d7e907cSAndroid Build Coastguard Worker     * Optionally, the driver may save the prepared model to cache during the
144*4d7e907cSAndroid Build Coastguard Worker     * asynchronous preparation. Any error that occurs when saving to cache must
145*4d7e907cSAndroid Build Coastguard Worker     * not affect the status of preparing the model. Even if the input arguments
146*4d7e907cSAndroid Build Coastguard Worker     * related to the cache may be invalid, or the driver may fail to save to
147*4d7e907cSAndroid Build Coastguard Worker     * cache, the prepareModel function must finish preparing the model. The
148*4d7e907cSAndroid Build Coastguard Worker     * driver may choose not to save to cache even if the caching information is
149*4d7e907cSAndroid Build Coastguard Worker     * provided and valid.
150*4d7e907cSAndroid Build Coastguard Worker     *
151*4d7e907cSAndroid Build Coastguard Worker     * The only information that may be unknown to the model at this stage is
152*4d7e907cSAndroid Build Coastguard Worker     * the shape of the tensors, which may only be known at execution time. As
153*4d7e907cSAndroid Build Coastguard Worker     * such, some driver services may return partially prepared models, where
154*4d7e907cSAndroid Build Coastguard Worker     * the prepared model may only be finished when it is paired with a set of
155*4d7e907cSAndroid Build Coastguard Worker     * inputs to the model. Note that the same prepared model object may be used
156*4d7e907cSAndroid Build Coastguard Worker     * with different shapes of inputs on different (possibly concurrent)
157*4d7e907cSAndroid Build Coastguard Worker     * executions.
158*4d7e907cSAndroid Build Coastguard Worker     *
159*4d7e907cSAndroid Build Coastguard Worker     * Multiple threads may call prepareModel on the same model concurrently.
160*4d7e907cSAndroid Build Coastguard Worker     *
161*4d7e907cSAndroid Build Coastguard Worker     * @param model The model to be prepared for execution.
162*4d7e907cSAndroid Build Coastguard Worker     * @param preference Indicates the intended execution behavior of a prepared
163*4d7e907cSAndroid Build Coastguard Worker     *     model.
164*4d7e907cSAndroid Build Coastguard Worker     * @param priority The priority of the prepared model relative to other
165*4d7e907cSAndroid Build Coastguard Worker     *     prepared models owned by the client.
166*4d7e907cSAndroid Build Coastguard Worker     * @param deadline The time by which the model is expected to be prepared.
167*4d7e907cSAndroid Build Coastguard Worker     *     If the model cannot be prepared by the deadline, the preparation may
168*4d7e907cSAndroid Build Coastguard Worker     *     be aborted.
169*4d7e907cSAndroid Build Coastguard Worker     * @param modelCache A vector of handles with each entry holding exactly one
170*4d7e907cSAndroid Build Coastguard Worker     *     cache file descriptor for the security-sensitive cache. The length of
171*4d7e907cSAndroid Build Coastguard Worker     *     the vector must either be 0 indicating that caching information is
172*4d7e907cSAndroid Build Coastguard Worker     *     not provided, or match the numModelCache returned from
173*4d7e907cSAndroid Build Coastguard Worker     *     getNumberOfCacheFilesNeeded. The cache handles will be provided in
174*4d7e907cSAndroid Build Coastguard Worker     *     the same order when retrieving the preparedModel from cache files
175*4d7e907cSAndroid Build Coastguard Worker     *     with prepareModelFromCache_1_3.
176*4d7e907cSAndroid Build Coastguard Worker     * @param dataCache A vector of handles with each entry holding exactly one
177*4d7e907cSAndroid Build Coastguard Worker     *     cache file descriptor for the constants' cache. The length of the
178*4d7e907cSAndroid Build Coastguard Worker     *     vector must either be 0 indicating that caching information is not
179*4d7e907cSAndroid Build Coastguard Worker     *     provided, or match the numDataCache returned from
180*4d7e907cSAndroid Build Coastguard Worker     *     getNumberOfCacheFilesNeeded. The cache handles will be provided in
181*4d7e907cSAndroid Build Coastguard Worker     *     the same order when retrieving the preparedModel from cache files
182*4d7e907cSAndroid Build Coastguard Worker     *     with prepareModelFromCache_1_3.
183*4d7e907cSAndroid Build Coastguard Worker     * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN
184*4d7e907cSAndroid Build Coastguard Worker     *     identifying the prepared model. The same token will be provided when
185*4d7e907cSAndroid Build Coastguard Worker     *     retrieving the prepared model from the cache files with
186*4d7e907cSAndroid Build Coastguard Worker     *     prepareModelFromCache_1_3.  Tokens should be chosen to have a low rate of
187*4d7e907cSAndroid Build Coastguard Worker     *     collision for a particular application. The driver cannot detect a
188*4d7e907cSAndroid Build Coastguard Worker     *     collision; a collision will result in a failed execution or in a
189*4d7e907cSAndroid Build Coastguard Worker     *     successful execution that produces incorrect output values. If both
190*4d7e907cSAndroid Build Coastguard Worker     *     modelCache and dataCache are empty indicating that caching
191*4d7e907cSAndroid Build Coastguard Worker     *     information is not provided, this token must be ignored.
192*4d7e907cSAndroid Build Coastguard Worker     * @param callback A callback object used to return the error status of
193*4d7e907cSAndroid Build Coastguard Worker     *     preparing the model for execution and the prepared model if
194*4d7e907cSAndroid Build Coastguard Worker     *     successful, nullptr otherwise. The callback object's notify function
195*4d7e907cSAndroid Build Coastguard Worker     *     must be called exactly once, even if the model could not be prepared.
196*4d7e907cSAndroid Build Coastguard Worker     * @return status Error status of launching a task which prepares the model
197*4d7e907cSAndroid Build Coastguard Worker     *     in the background; must be:
198*4d7e907cSAndroid Build Coastguard Worker     *     - NONE if preparation task is successfully launched
199*4d7e907cSAndroid Build Coastguard Worker     *     - DEVICE_UNAVAILABLE if driver is offline or busy
200*4d7e907cSAndroid Build Coastguard Worker     *     - GENERAL_FAILURE if there is an unspecified error
201*4d7e907cSAndroid Build Coastguard Worker     *     - INVALID_ARGUMENT if one of the input arguments related to preparing
202*4d7e907cSAndroid Build Coastguard Worker     *       the model is invalid
203*4d7e907cSAndroid Build Coastguard Worker     *     - MISSED_DEADLINE_* if the preparation is aborted because the model
204*4d7e907cSAndroid Build Coastguard Worker     *       cannot be prepared by the deadline
205*4d7e907cSAndroid Build Coastguard Worker     *     - RESOURCE_EXHAUSTED_* if the task was aborted by the driver
206*4d7e907cSAndroid Build Coastguard Worker     */
207*4d7e907cSAndroid Build Coastguard Worker    prepareModel_1_3(Model model, ExecutionPreference preference,
208*4d7e907cSAndroid Build Coastguard Worker                     Priority priority, OptionalTimePoint deadline,
209*4d7e907cSAndroid Build Coastguard Worker                     vec<handle> modelCache, vec<handle> dataCache,
210*4d7e907cSAndroid Build Coastguard Worker                     uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token,
211*4d7e907cSAndroid Build Coastguard Worker                     IPreparedModelCallback callback)
212*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorStatus status);
213*4d7e907cSAndroid Build Coastguard Worker
214*4d7e907cSAndroid Build Coastguard Worker    /**
215*4d7e907cSAndroid Build Coastguard Worker     * Creates a prepared model from cache files for execution.
216*4d7e907cSAndroid Build Coastguard Worker     *
217*4d7e907cSAndroid Build Coastguard Worker     * prepareModelFromCache_1_3 is used to retrieve a prepared model directly from
218*4d7e907cSAndroid Build Coastguard Worker     * cache files to avoid slow model compilation time. There are
219*4d7e907cSAndroid Build Coastguard Worker     * two types of cache file handles provided to the driver: model cache
220*4d7e907cSAndroid Build Coastguard Worker     * and data cache. For more information on the two types of cache handles,
221*4d7e907cSAndroid Build Coastguard Worker     * refer to getNumberOfCacheFilesNeeded.
222*4d7e907cSAndroid Build Coastguard Worker     *
223*4d7e907cSAndroid Build Coastguard Worker     * The file descriptors must be opened with read and write permission. A file may
224*4d7e907cSAndroid Build Coastguard Worker     * have any size, and the corresponding file descriptor may have any offset. The
225*4d7e907cSAndroid Build Coastguard Worker     * driver must truncate a file to zero size before writing to that file. The file
226*4d7e907cSAndroid Build Coastguard Worker     * descriptors may be closed by the client once the asynchronous preparation has
227*4d7e907cSAndroid Build Coastguard Worker     * finished. The driver must dup a file descriptor if it wants to get access to
228*4d7e907cSAndroid Build Coastguard Worker     * the cache file later.
229*4d7e907cSAndroid Build Coastguard Worker     *
230*4d7e907cSAndroid Build Coastguard Worker     * The model is prepared asynchronously with respect to the caller. The
231*4d7e907cSAndroid Build Coastguard Worker     * prepareModelFromCache_1_3 function must verify the inputs to the
232*4d7e907cSAndroid Build Coastguard Worker     * prepareModelFromCache_1_3 function are correct, and that the security-sensitive
233*4d7e907cSAndroid Build Coastguard Worker     * cache has not been modified since it was last written by the driver.
234*4d7e907cSAndroid Build Coastguard Worker     * If there is an error, or if compilation caching is not supported, or if the
235*4d7e907cSAndroid Build Coastguard Worker     * security-sensitive cache has been modified, prepareModelFromCache_1_3 must
236*4d7e907cSAndroid Build Coastguard Worker     * immediately invoke the callback with the appropriate ErrorStatus value and
237*4d7e907cSAndroid Build Coastguard Worker     * nullptr for the IPreparedModel, then return with the same ErrorStatus. If
238*4d7e907cSAndroid Build Coastguard Worker     * the inputs to the prepareModelFromCache_1_3 function are valid, the security-sensitive
239*4d7e907cSAndroid Build Coastguard Worker     * cache is not modified, and there is no error, prepareModelFromCache_1_3 must launch an
240*4d7e907cSAndroid Build Coastguard Worker     * asynchronous task to prepare the model in the background, and immediately return
241*4d7e907cSAndroid Build Coastguard Worker     * from prepareModelFromCache_1_3 with ErrorStatus::NONE. If the asynchronous task
242*4d7e907cSAndroid Build Coastguard Worker     * fails to launch, prepareModelFromCache_1_3 must immediately invoke the callback
243*4d7e907cSAndroid Build Coastguard Worker     * with ErrorStatus::GENERAL_FAILURE and nullptr for the IPreparedModel, then
244*4d7e907cSAndroid Build Coastguard Worker     * return with ErrorStatus::GENERAL_FAILURE.
245*4d7e907cSAndroid Build Coastguard Worker     *
246*4d7e907cSAndroid Build Coastguard Worker     * When the asynchronous task has finished preparing the model, it must
247*4d7e907cSAndroid Build Coastguard Worker     * immediately invoke the callback function provided as an input to
248*4d7e907cSAndroid Build Coastguard Worker     * prepareModelFromCache_1_3. If the model was prepared successfully, the
249*4d7e907cSAndroid Build Coastguard Worker     * callback object must be invoked with an error status of ErrorStatus::NONE
250*4d7e907cSAndroid Build Coastguard Worker     * and the produced IPreparedModel object. If an error occurred preparing
251*4d7e907cSAndroid Build Coastguard Worker     * the model, the callback object must be invoked with the appropriate
252*4d7e907cSAndroid Build Coastguard Worker     * ErrorStatus value and nullptr for the IPreparedModel.
253*4d7e907cSAndroid Build Coastguard Worker     *
254*4d7e907cSAndroid Build Coastguard Worker     * prepareModelFromCache_1_3 can be called with an optional deadline. If the
255*4d7e907cSAndroid Build Coastguard Worker     * model is not able to prepared before the provided deadline, the model
256*4d7e907cSAndroid Build Coastguard Worker     * preparation may be aborted, and either {@link
257*4d7e907cSAndroid Build Coastguard Worker     * ErrorStatus::MISSED_DEADLINE_TRANSIENT}
258*4d7e907cSAndroid Build Coastguard Worker     * or {@link ErrorStatus::MISSED_DEADLINE_PERSISTENT} may be returned. The
259*4d7e907cSAndroid Build Coastguard Worker     * error due to an abort must be sent the same way as other errors,
260*4d7e907cSAndroid Build Coastguard Worker     * described above. The deadline is represented as nanoseconds since the
261*4d7e907cSAndroid Build Coastguard Worker     * epoch of the steady clock (as if from
262*4d7e907cSAndroid Build Coastguard Worker     * std::chrono::steady_clock::time_point), but the service may convert it to
263*4d7e907cSAndroid Build Coastguard Worker     * the nanoseconds since boot time (as if from
264*4d7e907cSAndroid Build Coastguard Worker     * clock_gettime(CLOCK_BOOTTIME, &ts) or
265*4d7e907cSAndroid Build Coastguard Worker     * android::base::boot_clock::time_point) to account for time when the
266*4d7e907cSAndroid Build Coastguard Worker     * system is suspended. This conversion can by done by finding the timeout
267*4d7e907cSAndroid Build Coastguard Worker     * duration remaining compared to the steady_clock and adding it to the
268*4d7e907cSAndroid Build Coastguard Worker     * current boot_clock time.
269*4d7e907cSAndroid Build Coastguard Worker     *
270*4d7e907cSAndroid Build Coastguard Worker     * The only information that may be unknown to the model at this stage is
271*4d7e907cSAndroid Build Coastguard Worker     * the shape of the tensors, which may only be known at execution time. As
272*4d7e907cSAndroid Build Coastguard Worker     * such, some driver services may return partially prepared models, where
273*4d7e907cSAndroid Build Coastguard Worker     * the prepared model may only be finished when it is paired with a set of
274*4d7e907cSAndroid Build Coastguard Worker     * inputs to the model. Note that the same prepared model object may be
275*4d7e907cSAndroid Build Coastguard Worker     * used with different shapes of inputs on different (possibly concurrent)
276*4d7e907cSAndroid Build Coastguard Worker     * executions.
277*4d7e907cSAndroid Build Coastguard Worker     *
278*4d7e907cSAndroid Build Coastguard Worker     * @param deadline The time by which the model is expected to be prepared.
279*4d7e907cSAndroid Build Coastguard Worker     *     If the model cannot be prepared by the deadline, the preparation may
280*4d7e907cSAndroid Build Coastguard Worker     *     be aborted.
281*4d7e907cSAndroid Build Coastguard Worker     * @param modelCache A vector of handles with each entry holding exactly one
282*4d7e907cSAndroid Build Coastguard Worker     *     cache file descriptor for the security-sensitive cache. The length of
283*4d7e907cSAndroid Build Coastguard Worker     *     the vector must match the numModelCache returned from getNumberOfCacheFilesNeeded.
284*4d7e907cSAndroid Build Coastguard Worker     *     The cache handles will be provided in the same order as with prepareModel_1_3.
285*4d7e907cSAndroid Build Coastguard Worker     * @param dataCache A vector of handles with each entry holding exactly one
286*4d7e907cSAndroid Build Coastguard Worker     *     cache file descriptor for the constants' cache. The length of the vector
287*4d7e907cSAndroid Build Coastguard Worker     *     must match the numDataCache returned from getNumberOfCacheFilesNeeded.
288*4d7e907cSAndroid Build Coastguard Worker     *     The cache handles will be provided in the same order as with prepareModel_1_3.
289*4d7e907cSAndroid Build Coastguard Worker     * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN
290*4d7e907cSAndroid Build Coastguard Worker     *     identifying the prepared model. It is the same token provided when saving
291*4d7e907cSAndroid Build Coastguard Worker     *     the cache files with prepareModel_1_3. Tokens should be chosen
292*4d7e907cSAndroid Build Coastguard Worker     *     to have a low rate of collision for a particular application. The driver
293*4d7e907cSAndroid Build Coastguard Worker     *     cannot detect a collision; a collision will result in a failed execution
294*4d7e907cSAndroid Build Coastguard Worker     *     or in a successful execution that produces incorrect output values.
295*4d7e907cSAndroid Build Coastguard Worker     * @param callback A callback object used to return the error status of
296*4d7e907cSAndroid Build Coastguard Worker     *     preparing the model for execution and the prepared model if
297*4d7e907cSAndroid Build Coastguard Worker     *     successful, nullptr otherwise. The callback object's notify function
298*4d7e907cSAndroid Build Coastguard Worker     *     must be called exactly once, even if the model could not be prepared.
299*4d7e907cSAndroid Build Coastguard Worker     * @return status Error status of launching a task which prepares the model
300*4d7e907cSAndroid Build Coastguard Worker     *     in the background; must be:
301*4d7e907cSAndroid Build Coastguard Worker     *     - NONE if preparation task is successfully launched
302*4d7e907cSAndroid Build Coastguard Worker     *     - DEVICE_UNAVAILABLE if driver is offline or busy
303*4d7e907cSAndroid Build Coastguard Worker     *     - GENERAL_FAILURE if caching is not supported or if there is an
304*4d7e907cSAndroid Build Coastguard Worker     *       unspecified error
305*4d7e907cSAndroid Build Coastguard Worker     *     - INVALID_ARGUMENT if one of the input arguments is invalid
306*4d7e907cSAndroid Build Coastguard Worker     *     - MISSED_DEADLINE_* if the preparation is aborted because the model
307*4d7e907cSAndroid Build Coastguard Worker     *       cannot be prepared by the deadline
308*4d7e907cSAndroid Build Coastguard Worker     *     - RESOURCE_EXHAUSTED_* if the task was aborted by the driver
309*4d7e907cSAndroid Build Coastguard Worker     */
310*4d7e907cSAndroid Build Coastguard Worker    prepareModelFromCache_1_3(OptionalTimePoint deadline,
311*4d7e907cSAndroid Build Coastguard Worker                              vec<handle> modelCache, vec<handle> dataCache,
312*4d7e907cSAndroid Build Coastguard Worker                              uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token,
313*4d7e907cSAndroid Build Coastguard Worker                              IPreparedModelCallback callback)
314*4d7e907cSAndroid Build Coastguard Worker            generates (ErrorStatus status);
315*4d7e907cSAndroid Build Coastguard Worker
316*4d7e907cSAndroid Build Coastguard Worker    /**
317*4d7e907cSAndroid Build Coastguard Worker     * Allocates a driver-managed buffer with the properties specified by the buffer descriptor
318*4d7e907cSAndroid Build Coastguard Worker     * as well as the input and output roles.
319*4d7e907cSAndroid Build Coastguard Worker     *
320*4d7e907cSAndroid Build Coastguard Worker     * The allocate function must verify its inputs are correct. If there is an error, or if a
321*4d7e907cSAndroid Build Coastguard Worker     * certain role or property is not supported by the driver, the allocate
322*4d7e907cSAndroid Build Coastguard Worker     * function must return with an appropriate ErrorStatus, a nullptr as the IBuffer, and 0 as the
323*4d7e907cSAndroid Build Coastguard Worker     * buffer token. If the allocation is successful, this method must return with ErrorStatus::NONE
324*4d7e907cSAndroid Build Coastguard Worker     * and the produced IBuffer with a positive token identifying the allocated buffer. A successful
325*4d7e907cSAndroid Build Coastguard Worker     * allocation must accommodate all of the specified roles and buffer properties.
326*4d7e907cSAndroid Build Coastguard Worker     *
327*4d7e907cSAndroid Build Coastguard Worker     * The buffer is allocated to an uninitialized state. An uninitialized buffer may only be used
328*4d7e907cSAndroid Build Coastguard Worker     * in ways that are specified by outputRoles. A buffer is initialized after it is used as an
329*4d7e907cSAndroid Build Coastguard Worker     * output in a successful execution, or after a successful invocation of IBuffer::copyFrom on
330*4d7e907cSAndroid Build Coastguard Worker     * the buffer. An initialized buffer may be used according to all roles specified in inputRoles
331*4d7e907cSAndroid Build Coastguard Worker     * and outputRoles. A buffer will return to the uninitialized state if it is used as an output
332*4d7e907cSAndroid Build Coastguard Worker     * in a failed execution, or after a failed invocation of IBuffer::copyFrom on the buffer.
333*4d7e907cSAndroid Build Coastguard Worker     *
334*4d7e907cSAndroid Build Coastguard Worker     * The dimensions of the buffer can be deduced from the buffer descriptor as well as the
335*4d7e907cSAndroid Build Coastguard Worker     * dimensions of the corresponding model operands of the input and output roles. The dimensions
336*4d7e907cSAndroid Build Coastguard Worker     * or rank of the buffer may be unknown at this stage. As such, some driver services may only
337*4d7e907cSAndroid Build Coastguard Worker     * create a placeholder and defer the actual allocation until execution time. Note that the
338*4d7e907cSAndroid Build Coastguard Worker     * same buffer may be used for different shapes of outputs on different executions. When the
339*4d7e907cSAndroid Build Coastguard Worker     * buffer is used as an input, the input shape must be the same as the output shape from the
340*4d7e907cSAndroid Build Coastguard Worker     * last execution using this buffer as an output.
341*4d7e907cSAndroid Build Coastguard Worker     *
342*4d7e907cSAndroid Build Coastguard Worker     * The driver must apply proper validatation upon every usage of the buffer, and must fail the
343*4d7e907cSAndroid Build Coastguard Worker     * execution immediately if the usage is illegal.
344*4d7e907cSAndroid Build Coastguard Worker     *
345*4d7e907cSAndroid Build Coastguard Worker     * @param desc A buffer descriptor specifying the properties of the buffer to allocate.
346*4d7e907cSAndroid Build Coastguard Worker     * @param preparedModels A vector of IPreparedModel objects. Must only contain IPreparedModel
347*4d7e907cSAndroid Build Coastguard Worker     *     objects from the same IDevice as this method is being invoked on.
348*4d7e907cSAndroid Build Coastguard Worker     * @param inputRoles A vector of roles with each specifying an input to a prepared model.
349*4d7e907cSAndroid Build Coastguard Worker     * @param outputRoles A vector of roles with each specifying an output to a prepared model.
350*4d7e907cSAndroid Build Coastguard Worker     *     Each role specified in inputRoles and outputRoles must be unique. The corresponding
351*4d7e907cSAndroid Build Coastguard Worker     *     model operands of the roles must have the same OperandType, scale, zero point, and
352*4d7e907cSAndroid Build Coastguard Worker     *     ExtraParams. The dimensions of the operands and the dimensions specified in the buffer
353*4d7e907cSAndroid Build Coastguard Worker     *     descriptor must be compatible with each other. Two dimensions are incompatible if there
354*4d7e907cSAndroid Build Coastguard Worker     *     is at least one axis that is fully specified in both but has different values.
355*4d7e907cSAndroid Build Coastguard Worker     * @return status Error status of the buffer allocation. Must be:
356*4d7e907cSAndroid Build Coastguard Worker     *     - NONE if successful
357*4d7e907cSAndroid Build Coastguard Worker     *     - DEVICE_UNAVAILABLE if driver is offline or busy
358*4d7e907cSAndroid Build Coastguard Worker     *     - GENERAL_FAILURE if a certain buffer property or a certain role is not supported,
359*4d7e907cSAndroid Build Coastguard Worker     *       or if there is an unspecified error
360*4d7e907cSAndroid Build Coastguard Worker     *     - INVALID_ARGUMENT if one of the input arguments is invalid
361*4d7e907cSAndroid Build Coastguard Worker     * @return buffer The allocated IBuffer object. If the buffer was unable to be allocated
362*4d7e907cSAndroid Build Coastguard Worker     *     due to an error, nullptr must be returned.
363*4d7e907cSAndroid Build Coastguard Worker     * @return token A positive token identifying the allocated buffer. The same token will be
364*4d7e907cSAndroid Build Coastguard Worker     *     provided when referencing the buffer as one of the memory pools in the request of an
365*4d7e907cSAndroid Build Coastguard Worker     *     execution. The token must not collide with the tokens of other IBuffer objects that are
366*4d7e907cSAndroid Build Coastguard Worker     *     currently alive in the same driver service. If the buffer was unable to be allocated
367*4d7e907cSAndroid Build Coastguard Worker     *     due to an error, the token must be 0.
368*4d7e907cSAndroid Build Coastguard Worker     */
369*4d7e907cSAndroid Build Coastguard Worker    allocate(BufferDesc desc, vec<IPreparedModel> preparedModels, vec<BufferRole> inputRoles,
370*4d7e907cSAndroid Build Coastguard Worker             vec<BufferRole> outputRoles)
371*4d7e907cSAndroid Build Coastguard Worker            generates (ErrorStatus status, IBuffer buffer, uint32_t token);
372*4d7e907cSAndroid Build Coastguard Worker};
373