Implement NN HAL for compilation caching.
Add three methods
- IDevice::isCachingSupported
- IDevice::prepareModelFromCache
- IPreparedModel::saveToCache
Bug: 119616526
Test: NeuralNetworksTest_static
Test: VtsHalNeuralnetworksV1_xTargetTest with 1.2 sample driver
Change-Id: If28ffe0be48bcb9f4715293fc1201c8d2dbeb946
diff --git a/neuralnetworks/1.2/IPreparedModel.hal b/neuralnetworks/1.2/IPreparedModel.hal
index 5d2d80f..757d5f1 100644
--- a/neuralnetworks/1.2/IPreparedModel.hal
+++ b/neuralnetworks/1.2/IPreparedModel.hal
@@ -157,4 +157,62 @@
fmq_sync<FmqRequestDatum> requestChannel,
fmq_sync<FmqResultDatum> resultChannel)
generates (ErrorStatus status, IBurstContext context);
+
+ /*
+ * Saves the prepared model to cache files.
+ *
+ * saveToCache is used to save a prepared model to cache files for faster
+ * model compilation time when the same model preparation is requested in
+ * the future. There are exactly two cache file descriptors provided to the
+ * driver: modelCache and dataCache.
+ *
+ * The dataCache is for caching constant data, possibly including preprocessed
+ * and transformed tensor buffers. Any modification to the dataCache should
+ * have no worse effect than generating bad output values at execution time.
+ *
+ * The modelCache is for caching security-sensitive data such as compiled
+ * executable machine code in the device's native binary format. A modification
+ * to the modelCache may affect the driver's execution behavior, and a malicious
+ * client could make use of this to execute beyond the granted permission. Thus,
+ * the driver must always check whether the modelCache is corrupted before preparing
+ * the model from cache.
+ *
+ * The two file descriptors must point to two zero-length files with offset
+ * positioned at the beginning of the file. The file descriptors may be closed
+ * by the client once the method has returned.
+ *
+ * If the driver decides not to save the prepared model without looking at the
+ * input arguments to the saveToCache function, saveToCache must return with
+ * ErrorStatus::GENERAL_FAILURE. Otherwise, the saveToCache function must verify
+ * the input arguments to the saveToCache function are valid, and return with
+ * ErrorStatus::INVALID_ARGUMENT if not. If the inputs are valid but the driver
+ * could not save the prepared model, saveToCache must return with the appropriate
+ * ErrorStatus. Otherwise, it must write the cache files and return
+ * ErrorStatus::NONE. Unless saveToCache returns ErrorStatus::NONE, the contents
+ * of the cache files are undefined.
+ *
+ * @param modelCache A handle holding exactly one cache file descriptor for the
+ * security-sensitive cache.
+ * @param dataCache A handle holding exactly one cache file descriptor for the
+ * constants' cache.
+ * @param token A caching token of length Constant::BYTE_SIZE_OF_CACHE_TOKEN
+ * identifying the prepared model. The same token will be provided
+ * when retrieving the prepared model from cache files with
+ * IDevice::prepareModelFromCache. Tokens should be chosen to have
+ * a low rate of collision for a particular application. The driver
+ * cannot detect a collision; a collision will result in a failed
+ * execution or in a successful execution that produces incorrect
+ * output values.
+ * @return status Error status of saveToCache, must be:
+ * - NONE if saveToCache is performed successfully
+ * - DEVICE_UNAVAILABLE if driver is offline or busy
+ * - GENERAL_FAILURE if the driver could not save the
+ * prepared model or if there is an unspecified error
+ * - INVALID_ARGUMENT if one of the input arguments is invalid,
+ * unless the driver decides not to save the prepared model
+ * without looking at the input arguments
+ */
+ saveToCache(handle modelCache, handle dataCache,
+ uint8_t[Constant:BYTE_SIZE_OF_CACHE_TOKEN] token)
+ generates (ErrorStatus status);
};