1*4d7e907cSAndroid Build Coastguard Worker/* 2*4d7e907cSAndroid Build Coastguard Worker * Copyright (C) 2018 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.0::ErrorStatus; 20*4d7e907cSAndroid Build Coastguard Workerimport @1.1::ExecutionPreference; 21*4d7e907cSAndroid Build Coastguard Workerimport @1.1::IDevice; 22*4d7e907cSAndroid Build Coastguard Workerimport IPreparedModelCallback; 23*4d7e907cSAndroid Build Coastguard Worker 24*4d7e907cSAndroid Build Coastguard Worker/** 25*4d7e907cSAndroid Build Coastguard Worker * This interface represents a device driver. 26*4d7e907cSAndroid Build Coastguard Worker */ 27*4d7e907cSAndroid Build Coastguard Workerinterface IDevice extends @1.1::IDevice { 28*4d7e907cSAndroid Build Coastguard Worker /** 29*4d7e907cSAndroid Build Coastguard Worker * Get the version string of the driver implementation. 30*4d7e907cSAndroid Build Coastguard Worker * 31*4d7e907cSAndroid Build Coastguard Worker * The version string must be a unique token among the set of version strings of 32*4d7e907cSAndroid Build Coastguard Worker * drivers of a specific device. The token identifies the device driver's 33*4d7e907cSAndroid Build Coastguard Worker * implementation. The token must not be confused with the feature level which is solely 34*4d7e907cSAndroid Build Coastguard Worker * defined by the interface version. This API is opaque to the Android framework, but the 35*4d7e907cSAndroid Build Coastguard Worker * Android framework may use the information for debugging or to pass on to NNAPI applications. 36*4d7e907cSAndroid Build Coastguard Worker * 37*4d7e907cSAndroid Build Coastguard Worker * Application developers sometimes have specific requirements to ensure good user experiences, 38*4d7e907cSAndroid Build Coastguard Worker * and they need more information to make intelligent decisions when the Android framework cannot. 39*4d7e907cSAndroid Build Coastguard Worker * For example, combined with the device name and other information, the token can help 40*4d7e907cSAndroid Build Coastguard Worker * NNAPI applications filter devices based on their needs: 41*4d7e907cSAndroid Build Coastguard Worker * - An application demands a certain level of performance, but a specific version of 42*4d7e907cSAndroid Build Coastguard Worker * the driver cannot meet that requirement because of a performance regression. 43*4d7e907cSAndroid Build Coastguard Worker * The application can disallow the driver based on the version provided. 44*4d7e907cSAndroid Build Coastguard Worker * - An application has a minimum precision requirement, but certain versions of 45*4d7e907cSAndroid Build Coastguard Worker * the driver cannot meet that requirement because of bugs or certain optimizations. 46*4d7e907cSAndroid Build Coastguard Worker * The application can filter out versions of these drivers. 47*4d7e907cSAndroid Build Coastguard Worker * 48*4d7e907cSAndroid Build Coastguard Worker * @return status Error status returned from querying the version string. Must be: 49*4d7e907cSAndroid Build Coastguard Worker * - NONE if the query was successful 50*4d7e907cSAndroid Build Coastguard Worker * - DEVICE_UNAVAILABLE if driver is offline or busy 51*4d7e907cSAndroid Build Coastguard Worker * - GENERAL_FAILURE if the query resulted in an 52*4d7e907cSAndroid Build Coastguard Worker * unspecified error 53*4d7e907cSAndroid Build Coastguard Worker * @return version The version string of the device implementation. 54*4d7e907cSAndroid Build Coastguard Worker * Must have nonzero length 55*4d7e907cSAndroid Build Coastguard Worker */ 56*4d7e907cSAndroid Build Coastguard Worker getVersionString() generates (ErrorStatus status, string version); 57*4d7e907cSAndroid Build Coastguard Worker 58*4d7e907cSAndroid Build Coastguard Worker /** 59*4d7e907cSAndroid Build Coastguard Worker * Get the type of a given device. 60*4d7e907cSAndroid Build Coastguard Worker * 61*4d7e907cSAndroid Build Coastguard Worker * The device type can be used to help application developers to distribute 62*4d7e907cSAndroid Build Coastguard Worker * Machine Learning workloads and other workloads such as graphical rendering. 63*4d7e907cSAndroid Build Coastguard Worker * E.g., for an app which renders AR scenes based on real time object detection 64*4d7e907cSAndroid Build Coastguard Worker * results, the developer could choose an ACCELERATOR type device for ML 65*4d7e907cSAndroid Build Coastguard Worker * workloads, and reserve GPU for graphical rendering. 66*4d7e907cSAndroid Build Coastguard Worker * 67*4d7e907cSAndroid Build Coastguard Worker * @return status Error status returned from querying the device type. Must be: 68*4d7e907cSAndroid Build Coastguard Worker * - NONE if the query was successful 69*4d7e907cSAndroid Build Coastguard Worker * - DEVICE_UNAVAILABLE if driver is offline or busy 70*4d7e907cSAndroid Build Coastguard Worker * - GENERAL_FAILURE if the query resulted in an 71*4d7e907cSAndroid Build Coastguard Worker * unspecified error 72*4d7e907cSAndroid Build Coastguard Worker * @return type The DeviceType of the device. Please note, this is not a 73*4d7e907cSAndroid Build Coastguard Worker * bitfield of DeviceTypes. Each device must only be of a 74*4d7e907cSAndroid Build Coastguard Worker * single DeviceType. 75*4d7e907cSAndroid Build Coastguard Worker */ 76*4d7e907cSAndroid Build Coastguard Worker getType() generates (ErrorStatus status, DeviceType type); 77*4d7e907cSAndroid Build Coastguard Worker 78*4d7e907cSAndroid Build Coastguard Worker /** 79*4d7e907cSAndroid Build Coastguard Worker * Gets the capabilities of a driver. 80*4d7e907cSAndroid Build Coastguard Worker * 81*4d7e907cSAndroid Build Coastguard Worker * @return status Error status of the call, must be: 82*4d7e907cSAndroid Build Coastguard Worker * - NONE if successful 83*4d7e907cSAndroid Build Coastguard Worker * - DEVICE_UNAVAILABLE if driver is offline or busy 84*4d7e907cSAndroid Build Coastguard Worker * - GENERAL_FAILURE if there is an unspecified error 85*4d7e907cSAndroid Build Coastguard Worker * @return capabilities Capabilities of the driver. 86*4d7e907cSAndroid Build Coastguard Worker */ 87*4d7e907cSAndroid Build Coastguard Worker getCapabilities_1_2() generates (ErrorStatus status, Capabilities capabilities); 88*4d7e907cSAndroid Build Coastguard Worker 89*4d7e907cSAndroid Build Coastguard Worker /** 90*4d7e907cSAndroid Build Coastguard Worker * Gets information about extensions supported by the driver implementation. 91*4d7e907cSAndroid Build Coastguard Worker * 92*4d7e907cSAndroid Build Coastguard Worker * All extension operations and operands must be fully supported for the 93*4d7e907cSAndroid Build Coastguard Worker * extension to appear in the list of supported extensions. 94*4d7e907cSAndroid Build Coastguard Worker * 95*4d7e907cSAndroid Build Coastguard Worker * @return status Error status of the call, must be: 96*4d7e907cSAndroid Build Coastguard Worker * - NONE if successful 97*4d7e907cSAndroid Build Coastguard Worker * - DEVICE_UNAVAILABLE if driver is offline or busy 98*4d7e907cSAndroid Build Coastguard Worker * - GENERAL_FAILURE if there is an unspecified error 99*4d7e907cSAndroid Build Coastguard Worker * @return extensions A list of supported extensions. 100*4d7e907cSAndroid Build Coastguard Worker */ 101*4d7e907cSAndroid Build Coastguard Worker getSupportedExtensions() 102*4d7e907cSAndroid Build Coastguard Worker generates (ErrorStatus status, vec<Extension> extensions); 103*4d7e907cSAndroid Build Coastguard Worker 104*4d7e907cSAndroid Build Coastguard Worker /** 105*4d7e907cSAndroid Build Coastguard Worker * Gets the supported operations in a model. 106*4d7e907cSAndroid Build Coastguard Worker * 107*4d7e907cSAndroid Build Coastguard Worker * getSupportedOperations indicates which operations of a model are fully 108*4d7e907cSAndroid Build Coastguard Worker * supported by the vendor driver. If an operation may not be supported for 109*4d7e907cSAndroid Build Coastguard Worker * any reason, getSupportedOperations must return false for that operation. 110*4d7e907cSAndroid Build Coastguard Worker * 111*4d7e907cSAndroid Build Coastguard Worker * @param model A model whose operations--and their corresponding operands-- 112*4d7e907cSAndroid Build Coastguard Worker * are to be verified by the driver. 113*4d7e907cSAndroid Build Coastguard Worker * @return status Error status of the call, must be: 114*4d7e907cSAndroid Build Coastguard Worker * - NONE if successful 115*4d7e907cSAndroid Build Coastguard Worker * - DEVICE_UNAVAILABLE if driver is offline or busy 116*4d7e907cSAndroid Build Coastguard Worker * - GENERAL_FAILURE if there is an unspecified error 117*4d7e907cSAndroid Build Coastguard Worker * - INVALID_ARGUMENT if provided model is invalid 118*4d7e907cSAndroid Build Coastguard Worker * @return supportedOperations A list of supported operations, where true 119*4d7e907cSAndroid Build Coastguard Worker * indicates the operation is supported and false indicates the 120*4d7e907cSAndroid Build Coastguard Worker * operation is not supported. The index of "supported" corresponds with 121*4d7e907cSAndroid Build Coastguard Worker * the index of the operation it is describing. 122*4d7e907cSAndroid Build Coastguard Worker */ 123*4d7e907cSAndroid Build Coastguard Worker getSupportedOperations_1_2(Model model) 124*4d7e907cSAndroid Build Coastguard Worker generates (ErrorStatus status, vec<bool> supportedOperations); 125*4d7e907cSAndroid Build Coastguard Worker 126*4d7e907cSAndroid Build Coastguard Worker /** 127*4d7e907cSAndroid Build Coastguard Worker * Gets the caching requirements of the driver implementation. 128*4d7e907cSAndroid Build Coastguard Worker * 129*4d7e907cSAndroid Build Coastguard Worker * There are two types of cache file descriptors provided to the driver: model cache 130*4d7e907cSAndroid Build Coastguard Worker * and data cache. 131*4d7e907cSAndroid Build Coastguard Worker * 132*4d7e907cSAndroid Build Coastguard Worker * The data cache is for caching constant data, possibly including preprocessed 133*4d7e907cSAndroid Build Coastguard Worker * and transformed tensor buffers. Any modification to the data cache should 134*4d7e907cSAndroid Build Coastguard Worker * have no worse effect than generating bad output values at execution time. 135*4d7e907cSAndroid Build Coastguard Worker * 136*4d7e907cSAndroid Build Coastguard Worker * The model cache is for caching security-sensitive data such as compiled 137*4d7e907cSAndroid Build Coastguard Worker * executable machine code in the device's native binary format. A modification 138*4d7e907cSAndroid Build Coastguard Worker * to the model cache may affect the driver's execution behavior, and a malicious 139*4d7e907cSAndroid Build Coastguard Worker * client could make use of this to execute beyond the granted permission. Thus, 140*4d7e907cSAndroid Build Coastguard Worker * the driver must always check whether the model cache is corrupted before 141*4d7e907cSAndroid Build Coastguard Worker * preparing the model from cache. 142*4d7e907cSAndroid Build Coastguard Worker * 143*4d7e907cSAndroid Build Coastguard Worker * getNumberOfCacheFilesNeeded returns how many of each type of cache files the driver 144*4d7e907cSAndroid Build Coastguard Worker * implementation needs to cache a single prepared model. Returning 0 for both types 145*4d7e907cSAndroid Build Coastguard Worker * indicates compilation caching is not supported by this driver. The driver may 146*4d7e907cSAndroid Build Coastguard Worker * still choose not to cache certain compiled models even if it reports that caching 147*4d7e907cSAndroid Build Coastguard Worker * is supported. 148*4d7e907cSAndroid Build Coastguard Worker * 149*4d7e907cSAndroid Build Coastguard Worker * If the device reports that caching is not supported, the user may avoid calling 150*4d7e907cSAndroid Build Coastguard Worker * IDevice::prepareModelFromCache or providing cache file descriptors to 151*4d7e907cSAndroid Build Coastguard Worker * IDevice::prepareModel_1_2. 152*4d7e907cSAndroid Build Coastguard Worker * 153*4d7e907cSAndroid Build Coastguard Worker * @return status Error status of the call, must be: 154*4d7e907cSAndroid Build Coastguard Worker * - NONE if successful 155*4d7e907cSAndroid Build Coastguard Worker * - DEVICE_UNAVAILABLE if driver is offline or busy 156*4d7e907cSAndroid Build Coastguard Worker * - GENERAL_FAILURE if there is an unspecified error 157*4d7e907cSAndroid Build Coastguard Worker * @return numModelCache An unsigned integer indicating how many files for model cache 158*4d7e907cSAndroid Build Coastguard Worker * the driver needs to cache a single prepared model. It must 159*4d7e907cSAndroid Build Coastguard Worker * be less than or equal to Constant::MAX_NUMBER_OF_CACHE_FILES. 160*4d7e907cSAndroid Build Coastguard Worker * @return numDataCache An unsigned integer indicating how many files for data cache 161*4d7e907cSAndroid Build Coastguard Worker * the driver needs to cache a single prepared model. It must 162*4d7e907cSAndroid Build Coastguard Worker * be less than or equal to Constant::MAX_NUMBER_OF_CACHE_FILES. 163*4d7e907cSAndroid Build Coastguard Worker */ 164*4d7e907cSAndroid Build Coastguard Worker getNumberOfCacheFilesNeeded() 165*4d7e907cSAndroid Build Coastguard Worker generates (ErrorStatus status, uint32_t numModelCache, uint32_t numDataCache); 166*4d7e907cSAndroid Build Coastguard Worker 167*4d7e907cSAndroid Build Coastguard Worker /** 168*4d7e907cSAndroid Build Coastguard Worker * Asynchronously creates a prepared model for execution and optionally saves it 169*4d7e907cSAndroid Build Coastguard Worker * into cache files. 170*4d7e907cSAndroid Build Coastguard Worker * 171*4d7e907cSAndroid Build Coastguard Worker * prepareModel is used to make any necessary transformations to or alternative 172*4d7e907cSAndroid Build Coastguard Worker * representations to a model for execution, possibly including 173*4d7e907cSAndroid Build Coastguard Worker * transformations on the constant data, optimization on the model's graph, 174*4d7e907cSAndroid Build Coastguard Worker * or compilation into the device's native binary format. The model itself 175*4d7e907cSAndroid Build Coastguard Worker * is not changed. 176*4d7e907cSAndroid Build Coastguard Worker * 177*4d7e907cSAndroid Build Coastguard Worker * Optionally, caching information may be provided for the driver to save 178*4d7e907cSAndroid Build Coastguard Worker * the prepared model to cache files for faster model compilation time 179*4d7e907cSAndroid Build Coastguard Worker * when the same model preparation is requested in the future. There are 180*4d7e907cSAndroid Build Coastguard Worker * two types of cache file handles provided to the driver: model cache 181*4d7e907cSAndroid Build Coastguard Worker * and data cache. For more information on the two types of cache handles, 182*4d7e907cSAndroid Build Coastguard Worker * refer to getNumberOfCacheFilesNeeded. 183*4d7e907cSAndroid Build Coastguard Worker * 184*4d7e907cSAndroid Build Coastguard Worker * The file descriptors must be opened with read and write permission. A file may 185*4d7e907cSAndroid Build Coastguard Worker * have any size, and the corresponding file descriptor may have any offset. The 186*4d7e907cSAndroid Build Coastguard Worker * driver must truncate a file to zero size before writing to that file. The file 187*4d7e907cSAndroid Build Coastguard Worker * descriptors may be closed by the client once the asynchronous preparation has 188*4d7e907cSAndroid Build Coastguard Worker * finished. The driver must dup a file descriptor if it wants to get access to 189*4d7e907cSAndroid Build Coastguard Worker * the cache file later. 190*4d7e907cSAndroid Build Coastguard Worker * 191*4d7e907cSAndroid Build Coastguard Worker * The model is prepared asynchronously with respect to the caller. The 192*4d7e907cSAndroid Build Coastguard Worker * prepareModel function must verify the inputs to the preparedModel function 193*4d7e907cSAndroid Build Coastguard Worker * related to preparing the model (as opposed to saving the prepared model to 194*4d7e907cSAndroid Build Coastguard Worker * cache) are correct. If there is an error, prepareModel must immediately invoke 195*4d7e907cSAndroid Build Coastguard Worker * the callback with the appropriate ErrorStatus value and nullptr for the 196*4d7e907cSAndroid Build Coastguard Worker * IPreparedModel, then return with the same ErrorStatus. If the inputs to the 197*4d7e907cSAndroid Build Coastguard Worker * prepareModel function that are related to preparing the model are valid and 198*4d7e907cSAndroid Build Coastguard Worker * there is no error, prepareModel must launch an asynchronous task 199*4d7e907cSAndroid Build Coastguard Worker * to prepare the model in the background, and immediately return from 200*4d7e907cSAndroid Build Coastguard Worker * prepareModel with ErrorStatus::NONE. If the asynchronous task fails to launch, 201*4d7e907cSAndroid Build Coastguard Worker * prepareModel must immediately invoke the callback with 202*4d7e907cSAndroid Build Coastguard Worker * ErrorStatus::GENERAL_FAILURE and nullptr for the IPreparedModel, then return 203*4d7e907cSAndroid Build Coastguard Worker * with ErrorStatus::GENERAL_FAILURE. 204*4d7e907cSAndroid Build Coastguard Worker * 205*4d7e907cSAndroid Build Coastguard Worker * When the asynchronous task has finished preparing the model, it must 206*4d7e907cSAndroid Build Coastguard Worker * immediately invoke the callback function provided as an input to 207*4d7e907cSAndroid Build Coastguard Worker * prepareModel. If the model was prepared successfully, the callback object 208*4d7e907cSAndroid Build Coastguard Worker * must be invoked with an error status of ErrorStatus::NONE and the 209*4d7e907cSAndroid Build Coastguard Worker * produced IPreparedModel object. If an error occurred preparing the model, 210*4d7e907cSAndroid Build Coastguard Worker * the callback object must be invoked with the appropriate ErrorStatus 211*4d7e907cSAndroid Build Coastguard Worker * value and nullptr for the IPreparedModel. 212*4d7e907cSAndroid Build Coastguard Worker * 213*4d7e907cSAndroid Build Coastguard Worker * Optionally, the driver may save the prepared model to cache during the 214*4d7e907cSAndroid Build Coastguard Worker * asynchronous preparation. Any error that occurs when saving to cache must 215*4d7e907cSAndroid Build Coastguard Worker * not affect the status of preparing the model. Even if the input arguments 216*4d7e907cSAndroid Build Coastguard Worker * related to the cache may be invalid, or the driver may fail to save to cache, 217*4d7e907cSAndroid Build Coastguard Worker * the prepareModel function must finish preparing the model. The driver 218*4d7e907cSAndroid Build Coastguard Worker * may choose not to save to cache even if the caching information is 219*4d7e907cSAndroid Build Coastguard Worker * provided and valid. 220*4d7e907cSAndroid Build Coastguard Worker * 221*4d7e907cSAndroid Build Coastguard Worker * The only information that may be unknown to the model at this stage is 222*4d7e907cSAndroid Build Coastguard Worker * the shape of the tensors, which may only be known at execution time. As 223*4d7e907cSAndroid Build Coastguard Worker * such, some driver services may return partially prepared models, where 224*4d7e907cSAndroid Build Coastguard Worker * the prepared model may only be finished when it is paired with a set of 225*4d7e907cSAndroid Build Coastguard Worker * inputs to the model. Note that the same prepared model object may be 226*4d7e907cSAndroid Build Coastguard Worker * used with different shapes of inputs on different (possibly concurrent) 227*4d7e907cSAndroid Build Coastguard Worker * executions. 228*4d7e907cSAndroid Build Coastguard Worker * 229*4d7e907cSAndroid Build Coastguard Worker * Multiple threads may call prepareModel on the same model concurrently. 230*4d7e907cSAndroid Build Coastguard Worker * 231*4d7e907cSAndroid Build Coastguard Worker * @param model The model to be prepared for execution. 232*4d7e907cSAndroid Build Coastguard Worker * @param preference Indicates the intended execution behavior of a prepared 233*4d7e907cSAndroid Build Coastguard Worker * model. 234*4d7e907cSAndroid Build Coastguard Worker * @param modelCache A vector of handles with each entry holding exactly one 235*4d7e907cSAndroid Build Coastguard Worker * cache file descriptor for the security-sensitive cache. The length of 236*4d7e907cSAndroid Build Coastguard Worker * the vector must either be 0 indicating that caching information is not provided, 237*4d7e907cSAndroid Build Coastguard Worker * or match the numModelCache returned from getNumberOfCacheFilesNeeded. The cache 238*4d7e907cSAndroid Build Coastguard Worker * handles will be provided in the same order when retrieving the 239*4d7e907cSAndroid Build Coastguard Worker * preparedModel from cache files with prepareModelFromCache. 240*4d7e907cSAndroid Build Coastguard Worker * @param dataCache A vector of handles with each entry holding exactly one 241*4d7e907cSAndroid Build Coastguard Worker * cache file descriptor for the constants' cache. The length of 242*4d7e907cSAndroid Build Coastguard Worker * the vector must either be 0 indicating that caching information is not provided, 243*4d7e907cSAndroid Build Coastguard Worker * or match the numDataCache returned from getNumberOfCacheFilesNeeded. The cache 244*4d7e907cSAndroid Build Coastguard Worker * handles will be provided in the same order when retrieving the 245*4d7e907cSAndroid Build Coastguard Worker * preparedModel from cache files with prepareModelFromCache. 246*4d7e907cSAndroid Build Coastguard Worker * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN 247*4d7e907cSAndroid Build Coastguard Worker * identifying the prepared model. The same token will be provided when retrieving 248*4d7e907cSAndroid Build Coastguard Worker * the prepared model from the cache files with prepareModelFromCache. 249*4d7e907cSAndroid Build Coastguard Worker * Tokens should be chosen to have a low rate of collision for a particular 250*4d7e907cSAndroid Build Coastguard Worker * application. The driver cannot detect a collision; a collision will result 251*4d7e907cSAndroid Build Coastguard Worker * in a failed execution or in a successful execution that produces incorrect 252*4d7e907cSAndroid Build Coastguard Worker * output values. If both modelCache and dataCache are empty indicating that 253*4d7e907cSAndroid Build Coastguard Worker * caching information is not provided, this token must be ignored. 254*4d7e907cSAndroid Build Coastguard Worker * @param callback A callback object used to return the error status of 255*4d7e907cSAndroid Build Coastguard Worker * preparing the model for execution and the prepared model if 256*4d7e907cSAndroid Build Coastguard Worker * successful, nullptr otherwise. The callback object's notify function 257*4d7e907cSAndroid Build Coastguard Worker * must be called exactly once, even if the model could not be prepared. 258*4d7e907cSAndroid Build Coastguard Worker * @return status Error status of launching a task which prepares the model 259*4d7e907cSAndroid Build Coastguard Worker * in the background; must be: 260*4d7e907cSAndroid Build Coastguard Worker * - NONE if preparation task is successfully launched 261*4d7e907cSAndroid Build Coastguard Worker * - DEVICE_UNAVAILABLE if driver is offline or busy 262*4d7e907cSAndroid Build Coastguard Worker * - GENERAL_FAILURE if there is an unspecified error 263*4d7e907cSAndroid Build Coastguard Worker * - INVALID_ARGUMENT if one of the input arguments related to preparing the 264*4d7e907cSAndroid Build Coastguard Worker * model is invalid 265*4d7e907cSAndroid Build Coastguard Worker */ 266*4d7e907cSAndroid Build Coastguard Worker prepareModel_1_2(Model model, ExecutionPreference preference, 267*4d7e907cSAndroid Build Coastguard Worker vec<handle> modelCache, vec<handle> dataCache, 268*4d7e907cSAndroid Build Coastguard Worker uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token, 269*4d7e907cSAndroid Build Coastguard Worker IPreparedModelCallback callback) 270*4d7e907cSAndroid Build Coastguard Worker generates (ErrorStatus status); 271*4d7e907cSAndroid Build Coastguard Worker 272*4d7e907cSAndroid Build Coastguard Worker /** 273*4d7e907cSAndroid Build Coastguard Worker * Creates a prepared model from cache files for execution. 274*4d7e907cSAndroid Build Coastguard Worker * 275*4d7e907cSAndroid Build Coastguard Worker * prepareModelFromCache is used to retrieve a prepared model directly from 276*4d7e907cSAndroid Build Coastguard Worker * cache files to avoid slow model compilation time. There are 277*4d7e907cSAndroid Build Coastguard Worker * two types of cache file handles provided to the driver: model cache 278*4d7e907cSAndroid Build Coastguard Worker * and data cache. For more information on the two types of cache handles, 279*4d7e907cSAndroid Build Coastguard Worker * refer to getNumberOfCacheFilesNeeded. 280*4d7e907cSAndroid Build Coastguard Worker * 281*4d7e907cSAndroid Build Coastguard Worker * The file descriptors must be opened with read and write permission. A file may 282*4d7e907cSAndroid Build Coastguard Worker * have any size, and the corresponding file descriptor may have any offset. The 283*4d7e907cSAndroid Build Coastguard Worker * driver must truncate a file to zero size before writing to that file. The file 284*4d7e907cSAndroid Build Coastguard Worker * descriptors may be closed by the client once the asynchronous preparation has 285*4d7e907cSAndroid Build Coastguard Worker * finished. The driver must dup a file descriptor if it wants to get access to 286*4d7e907cSAndroid Build Coastguard Worker * the cache file later. 287*4d7e907cSAndroid Build Coastguard Worker * 288*4d7e907cSAndroid Build Coastguard Worker * The model is prepared asynchronously with respect to the caller. The 289*4d7e907cSAndroid Build Coastguard Worker * prepareModelFromCache function must verify the inputs to the 290*4d7e907cSAndroid Build Coastguard Worker * prepareModelFromCache function are correct, and that the security-sensitive 291*4d7e907cSAndroid Build Coastguard Worker * cache has not been modified since it was last written by the driver. 292*4d7e907cSAndroid Build Coastguard Worker * If there is an error, or if compilation caching is not supported, or if the 293*4d7e907cSAndroid Build Coastguard Worker * security-sensitive cache has been modified, prepareModelFromCache must 294*4d7e907cSAndroid Build Coastguard Worker * immediately invoke the callback with the appropriate ErrorStatus value and 295*4d7e907cSAndroid Build Coastguard Worker * nullptr for the IPreparedModel, then return with the same ErrorStatus. If 296*4d7e907cSAndroid Build Coastguard Worker * the inputs to the prepareModelFromCache function are valid, the security-sensitive 297*4d7e907cSAndroid Build Coastguard Worker * cache is not modified, and there is no error, prepareModelFromCache must launch an 298*4d7e907cSAndroid Build Coastguard Worker * asynchronous task to prepare the model in the background, and immediately return 299*4d7e907cSAndroid Build Coastguard Worker * from prepareModelFromCache with ErrorStatus::NONE. If the asynchronous task 300*4d7e907cSAndroid Build Coastguard Worker * fails to launch, prepareModelFromCache must immediately invoke the callback 301*4d7e907cSAndroid Build Coastguard Worker * with ErrorStatus::GENERAL_FAILURE and nullptr for the IPreparedModel, then 302*4d7e907cSAndroid Build Coastguard Worker * return with ErrorStatus::GENERAL_FAILURE. 303*4d7e907cSAndroid Build Coastguard Worker * 304*4d7e907cSAndroid Build Coastguard Worker * When the asynchronous task has finished preparing the model, it must 305*4d7e907cSAndroid Build Coastguard Worker * immediately invoke the callback function provided as an input to 306*4d7e907cSAndroid Build Coastguard Worker * prepareModelFromCache. If the model was prepared successfully, the 307*4d7e907cSAndroid Build Coastguard Worker * callback object must be invoked with an error status of ErrorStatus::NONE 308*4d7e907cSAndroid Build Coastguard Worker * and the produced IPreparedModel object. If an error occurred preparing 309*4d7e907cSAndroid Build Coastguard Worker * the model, the callback object must be invoked with the appropriate 310*4d7e907cSAndroid Build Coastguard Worker * ErrorStatus value and nullptr for the IPreparedModel. 311*4d7e907cSAndroid Build Coastguard Worker * 312*4d7e907cSAndroid Build Coastguard Worker * The only information that may be unknown to the model at this stage is 313*4d7e907cSAndroid Build Coastguard Worker * the shape of the tensors, which may only be known at execution time. As 314*4d7e907cSAndroid Build Coastguard Worker * such, some driver services may return partially prepared models, where 315*4d7e907cSAndroid Build Coastguard Worker * the prepared model may only be finished when it is paired with a set of 316*4d7e907cSAndroid Build Coastguard Worker * inputs to the model. Note that the same prepared model object may be 317*4d7e907cSAndroid Build Coastguard Worker * used with different shapes of inputs on different (possibly concurrent) 318*4d7e907cSAndroid Build Coastguard Worker * executions. 319*4d7e907cSAndroid Build Coastguard Worker * 320*4d7e907cSAndroid Build Coastguard Worker * @param modelCache A vector of handles with each entry holding exactly one 321*4d7e907cSAndroid Build Coastguard Worker * cache file descriptor for the security-sensitive cache. The length of 322*4d7e907cSAndroid Build Coastguard Worker * the vector must match the numModelCache returned from getNumberOfCacheFilesNeeded. 323*4d7e907cSAndroid Build Coastguard Worker * The cache handles will be provided in the same order as with prepareModel_1_2. 324*4d7e907cSAndroid Build Coastguard Worker * @param dataCache A vector of handles with each entry holding exactly one 325*4d7e907cSAndroid Build Coastguard Worker * cache file descriptor for the constants' cache. The length of the vector 326*4d7e907cSAndroid Build Coastguard Worker * must match the numDataCache returned from getNumberOfCacheFilesNeeded. 327*4d7e907cSAndroid Build Coastguard Worker * The cache handles will be provided in the same order as with prepareModel_1_2. 328*4d7e907cSAndroid Build Coastguard Worker * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN 329*4d7e907cSAndroid Build Coastguard Worker * identifying the prepared model. It is the same token provided when saving 330*4d7e907cSAndroid Build Coastguard Worker * the cache files with prepareModel_1_2. Tokens should be chosen 331*4d7e907cSAndroid Build Coastguard Worker * to have a low rate of collision for a particular application. The driver 332*4d7e907cSAndroid Build Coastguard Worker * cannot detect a collision; a collision will result in a failed execution 333*4d7e907cSAndroid Build Coastguard Worker * or in a successful execution that produces incorrect output values. 334*4d7e907cSAndroid Build Coastguard Worker * @param callback A callback object used to return the error status of 335*4d7e907cSAndroid Build Coastguard Worker * preparing the model for execution and the prepared model if 336*4d7e907cSAndroid Build Coastguard Worker * successful, nullptr otherwise. The callback object's notify function 337*4d7e907cSAndroid Build Coastguard Worker * must be called exactly once, even if the model could not be prepared. 338*4d7e907cSAndroid Build Coastguard Worker * @return status Error status of launching a task which prepares the model 339*4d7e907cSAndroid Build Coastguard Worker * in the background; must be: 340*4d7e907cSAndroid Build Coastguard Worker * - NONE if preparation task is successfully launched 341*4d7e907cSAndroid Build Coastguard Worker * - DEVICE_UNAVAILABLE if driver is offline or busy 342*4d7e907cSAndroid Build Coastguard Worker * - GENERAL_FAILURE if caching is not supported or if there is an 343*4d7e907cSAndroid Build Coastguard Worker * unspecified error 344*4d7e907cSAndroid Build Coastguard Worker * - INVALID_ARGUMENT if one of the input arguments is invalid 345*4d7e907cSAndroid Build Coastguard Worker */ 346*4d7e907cSAndroid Build Coastguard Worker prepareModelFromCache(vec<handle> modelCache, vec<handle> dataCache, 347*4d7e907cSAndroid Build Coastguard Worker uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token, 348*4d7e907cSAndroid Build Coastguard Worker IPreparedModelCallback callback) 349*4d7e907cSAndroid Build Coastguard Worker generates (ErrorStatus status); 350*4d7e907cSAndroid Build Coastguard Worker}; 351