Refine documentation for compilation caching.
Also let mToken in CompilationBuilder to use an array,
and add a check in NeuralNetworksWrapper on token size.
Bug: 119616526
Test: NeuralNetworksTest_static
Change-Id: Ic8d4a39cb28905bd7829ff8a39412855bb354e57
diff --git a/runtime/CompilationBuilder.cpp b/runtime/CompilationBuilder.cpp
index 290a263..ccd30d8 100644
--- a/runtime/CompilationBuilder.cpp
+++ b/runtime/CompilationBuilder.cpp
@@ -101,8 +101,8 @@
if (!mCacheDir.empty() && mCacheDir.back() != '/') {
mCacheDir.push_back('/');
}
- mToken.resize(ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN);
- std::copy(token, token + ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN, mToken.data());
+ std::copy(token, token + ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN, mToken);
+ mIsCacheInfoProvided = true;
return ANEURALNETWORKS_NO_ERROR;
}
diff --git a/runtime/CompilationBuilder.h b/runtime/CompilationBuilder.h
index 1711012..8f85ca0 100644
--- a/runtime/CompilationBuilder.h
+++ b/runtime/CompilationBuilder.h
@@ -72,7 +72,8 @@
// Compilation caching information.
std::string mCacheDir;
- std::vector<uint8_t> mToken;
+ uint8_t mToken[ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN];
+ bool mIsCacheInfoProvided = false;
};
} // namespace nn
diff --git a/runtime/include/NeuralNetworks.h b/runtime/include/NeuralNetworks.h
index aa87c07..7418cfe 100644
--- a/runtime/include/NeuralNetworks.h
+++ b/runtime/include/NeuralNetworks.h
@@ -4448,7 +4448,7 @@
/**
* For {@link ANeuralNetworksCompilation_setCaching}, specify the size
- * of the cache token expecting from the application. The size is in bytes.
+ * of the cache token required from the application. The size is in bytes.
*
* Available since API level 29.
*/
@@ -4521,6 +4521,8 @@
* {@link ANeuralNetworksCompilation_createForDevices}.</li>
* <li>Set any desired properties on the compilation (for example,
* {@link ANeuralNetworksCompilation_setPreference}).</li>
+ * <li>Optionally, set the caching signature and the cache directory on the
+ * compilation by calling {@link ANeuralNetworksCompilation_setCaching}.</li>
* <li>Complete the compilation with {@link ANeuralNetworksCompilation_finish}.</li>
* <li>Use the compilation as many times as needed
* with {@link ANeuralNetworksExecution_create} and
@@ -4896,16 +4898,17 @@
* See {@link ANeuralNetworksCompilation} for information on multithreaded usage.
*
* @param compilation The compilation to be modified.
- * @param cacheDir The cache directory to store and retrieve caching data. It is
- * recommended to use the code_cache provided by the Android runtime.
- * If not using the code_cache, the user should choose a directory
- * local to the application, and is responsible to manage and clean
- * the cache entries.
- * @param token The token provided by the user to specify a model, must be of length
+ * @param cacheDir The cache directory for the runtime to store and retrieve caching
+ * data. It is recommended to use the code cache directory provided
+ * by the Android runtime. If not using the code cache directory, the
+ * user should choose a directory local to the application, and is
+ * responsible to managing the cache entries.
+ * @param token The token provided by the user to specify a model must be of length
* ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN. The user should ensure that
* the token is unique to a model within the application. The NNAPI
- * runtime will not detected token collisions. If there is a collision,
- * the compilation outcome may be incorrect without notifying with error.
+ * runtime cannot detect token collisions; a collision will result in a
+ * failed execution or in a successful execution that produces incorrect
+ * output values.
*
* @return ANEURALNETWORKS_NO_ERROR if successful.
*
diff --git a/runtime/include/NeuralNetworksWrapper.h b/runtime/include/NeuralNetworksWrapper.h
index 0d6d815..0be6a25 100644
--- a/runtime/include/NeuralNetworksWrapper.h
+++ b/runtime/include/NeuralNetworksWrapper.h
@@ -332,6 +332,9 @@
}
Result setCaching(const std::string& cacheDir, const std::vector<uint8_t>& token) {
+ if (token.size() != ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN) {
+ return Result::BAD_DATA;
+ }
return static_cast<Result>(ANeuralNetworksCompilation_setCaching(
mCompilation, cacheDir.c_str(), token.data()));
}
diff --git a/runtime/test/TestNeuralNetworksWrapper.h b/runtime/test/TestNeuralNetworksWrapper.h
index cc6a331..1659d79 100644
--- a/runtime/test/TestNeuralNetworksWrapper.h
+++ b/runtime/test/TestNeuralNetworksWrapper.h
@@ -335,6 +335,9 @@
}
Result setCaching(const std::string& cacheDir, const std::vector<uint8_t>& token) {
+ if (token.size() != ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN) {
+ return Result::BAD_DATA;
+ }
return static_cast<Result>(ANeuralNetworksCompilation_setCaching(
mCompilation, cacheDir.c_str(), token.data()));
}