diff --git a/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/PrepareModelConfig.aidl b/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/PrepareModelConfig.aidl index 85c924fe31..1f44ba724f 100644 --- a/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/PrepareModelConfig.aidl +++ b/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/PrepareModelConfig.aidl @@ -39,7 +39,8 @@ parcelable PrepareModelConfig { long deadlineNs; ParcelFileDescriptor[] modelCache; ParcelFileDescriptor[] dataCache; - byte[] cacheToken; + byte[32] cacheToken; android.hardware.neuralnetworks.TokenValuePair[] compilationHints; android.hardware.neuralnetworks.ExtensionNameAndPrefix[] extensionNameToPrefix; + const int BYTE_SIZE_OF_CACHE_TOKEN = 32; } diff --git a/neuralnetworks/aidl/android/hardware/neuralnetworks/IDevice.aidl b/neuralnetworks/aidl/android/hardware/neuralnetworks/IDevice.aidl index 821b9febfe..7808fc2b73 100644 --- a/neuralnetworks/aidl/android/hardware/neuralnetworks/IDevice.aidl +++ b/neuralnetworks/aidl/android/hardware/neuralnetworks/IDevice.aidl @@ -39,7 +39,7 @@ interface IDevice { /** * The byte size of the cache token. */ - const int BYTE_SIZE_OF_CACHE_TOKEN = 32; + const int BYTE_SIZE_OF_CACHE_TOKEN = PrepareModelConfig.BYTE_SIZE_OF_CACHE_TOKEN; /** * The maximum number of files for each type of cache in compilation caching. */ diff --git a/neuralnetworks/aidl/android/hardware/neuralnetworks/IPreparedModel.aidl b/neuralnetworks/aidl/android/hardware/neuralnetworks/IPreparedModel.aidl index 949804ea59..f752750514 100644 --- a/neuralnetworks/aidl/android/hardware/neuralnetworks/IPreparedModel.aidl +++ b/neuralnetworks/aidl/android/hardware/neuralnetworks/IPreparedModel.aidl @@ -204,6 +204,12 @@ interface IPreparedModel { * appropriate ErrorStatus value. If the inputs to the function are valid and there is no error, * createReusableExecution must construct a reusable execution. * + * This method will be called when a client requests a reusable execution with consistent + * request and execution config. For single-time execution, + * {@link IPreparedModel::executeSynchronouslyWithConfig} or + * {@link IPreparedModel::executeFencedWithConfig} is preferred, because the overhead of + * setting up a reusable execution can be avoided. + * * @param request The input and output information on which the prepared model is to be * executed. * @param config Specifies the execution configuration parameters. @@ -223,6 +229,10 @@ interface IPreparedModel { * ExecutionConfig} instead of a list of configuration parameters, and ExecutionConfig contains * more configuration parameters than are passed to executeSynchronously. * + * This method is preferred when a client requests a single-time synchronous execution. + * For reusable execution with consistent request and execution config, + * {@link IPreparedModel::createReusableExecution} must be called. + * * @param request The input and output information on which the prepared model is to be * executed. * @param config Specifies the execution configuration parameters. @@ -246,6 +256,10 @@ interface IPreparedModel { * ExecutionConfig} instead of a list of configuration parameters, and ExecutionConfig contains * more configuration parameters than are passed to executeFenced. * + * This method is preferred when a client requests a single-time fenced execution. + * For reusable execution with consistent request and execution config, + * {@link IPreparedModel::createReusableExecution} must be called. + * * @param request The input and output information on which the prepared model is to be * executed. The outputs in the request must have fully specified dimensions. * @param waitFor A vector of sync fence file descriptors. Execution must not start until all diff --git a/neuralnetworks/aidl/android/hardware/neuralnetworks/PrepareModelConfig.aidl b/neuralnetworks/aidl/android/hardware/neuralnetworks/PrepareModelConfig.aidl index 96df968464..55bd291be9 100644 --- a/neuralnetworks/aidl/android/hardware/neuralnetworks/PrepareModelConfig.aidl +++ b/neuralnetworks/aidl/android/hardware/neuralnetworks/PrepareModelConfig.aidl @@ -27,6 +27,11 @@ import android.hardware.neuralnetworks.TokenValuePair; */ @VintfStability parcelable PrepareModelConfig { + /** + * The byte size of the cache token. + */ + const int BYTE_SIZE_OF_CACHE_TOKEN = 32; + /** * Indicates the intended execution behavior of a prepared model. */ @@ -66,7 +71,7 @@ parcelable PrepareModelConfig { */ ParcelFileDescriptor[] dataCache; /** - * A caching token of length IDevice::BYTE_SIZE_OF_CACHE_TOKEN identifying + * A caching token of length BYTE_SIZE_OF_CACHE_TOKEN identifying * the prepared model. The same token will be provided when * retrieving the prepared model from the cache files with * IDevice::prepareModelFromCache. Tokens should be chosen to have a low @@ -77,7 +82,7 @@ parcelable PrepareModelConfig { * indicating that caching information is not provided, this * token must be ignored. */ - byte[] cacheToken; + byte[BYTE_SIZE_OF_CACHE_TOKEN] cacheToken; /** * A vector of token / value pairs represent vendor specific * compilation hints or metadata. The provided TokenValuePairs must not diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Conversions.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Conversions.h index af587150b1..71a28effdf 100644 --- a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Conversions.h +++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Conversions.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -219,6 +220,7 @@ nn::GeneralResult> convert( #endif // NN_AIDL_V4_OR_ABOVE nn::GeneralResult> toSigned(const std::vector& vec); +std::vector toVec(const std::array& token); } // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/src/Conversions.cpp b/neuralnetworks/aidl/utils/src/Conversions.cpp index 9b897c43ac..83fda10ce2 100644 --- a/neuralnetworks/aidl/utils/src/Conversions.cpp +++ b/neuralnetworks/aidl/utils/src/Conversions.cpp @@ -614,7 +614,7 @@ struct overloaded : Ts... { using Ts::operator()...; }; template -overloaded(Ts...)->overloaded; +overloaded(Ts...) -> overloaded; #ifdef __ANDROID__ nn::GeneralResult aidlHandleFromNativeHandle( @@ -1190,4 +1190,8 @@ nn::GeneralResult> toSigned(const std::vector& ve return std::vector(vec.begin(), vec.end()); } +std::vector toVec(const std::array& token) { + return std::vector(token.begin(), token.end()); +} + } // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/src/Device.cpp b/neuralnetworks/aidl/utils/src/Device.cpp index f3f4fdbba1..b64a40dcaa 100644 --- a/neuralnetworks/aidl/utils/src/Device.cpp +++ b/neuralnetworks/aidl/utils/src/Device.cpp @@ -229,7 +229,6 @@ nn::GeneralResult Device::prepareModel( const auto aidlDeadline = NN_TRY(convert(deadline)); auto aidlModelCache = NN_TRY(convert(modelCache)); auto aidlDataCache = NN_TRY(convert(dataCache)); - const auto aidlToken = NN_TRY(convert(token)); const auto cb = ndk::SharedRefBase::make(kFeatureLevel); const auto scoped = kDeathHandler.protectCallback(cb.get()); @@ -240,12 +239,13 @@ nn::GeneralResult Device::prepareModel( const auto ret = kDevice->prepareModelWithConfig( aidlModel, {aidlPreference, aidlPriority, aidlDeadline, std::move(aidlModelCache), - std::move(aidlDataCache), aidlToken, std::move(aidlHints), + std::move(aidlDataCache), token, std::move(aidlHints), std::move(aidlExtensionPrefix)}, cb); HANDLE_ASTATUS(ret) << "prepareModel failed"; return cb->get(); } + const auto aidlToken = NN_TRY(convert(token)); const auto ret = kDevice->prepareModel(aidlModel, aidlPreference, aidlPriority, aidlDeadline, aidlModelCache, aidlDataCache, aidlToken, cb); HANDLE_ASTATUS(ret) << "prepareModel failed"; diff --git a/neuralnetworks/aidl/utils/src/InvalidDevice.cpp b/neuralnetworks/aidl/utils/src/InvalidDevice.cpp index 33270ff381..44f8ea9ff8 100644 --- a/neuralnetworks/aidl/utils/src/InvalidDevice.cpp +++ b/neuralnetworks/aidl/utils/src/InvalidDevice.cpp @@ -189,7 +189,8 @@ ndk::ScopedAStatus InvalidDevice::prepareModelWithConfig( } } return prepareModel(model, config.preference, config.priority, config.deadlineNs, - config.modelCache, config.dataCache, config.cacheToken, callback); + config.modelCache, config.dataCache, utils::toVec(config.cacheToken), + callback); } ndk::ScopedAStatus InvalidDevice::prepareModelFromCache( diff --git a/neuralnetworks/aidl/vts/functional/Utils.h b/neuralnetworks/aidl/vts/functional/Utils.h index 4e0a4aafa3..ccb0778ac5 100644 --- a/neuralnetworks/aidl/vts/functional/Utils.h +++ b/neuralnetworks/aidl/vts/functional/Utils.h @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -47,6 +48,7 @@ inline constexpr int64_t kNoDeadline = -1; inline constexpr int64_t kOmittedTimeoutDuration = -1; inline constexpr int64_t kNoDuration = -1; inline const std::vector kEmptyCacheToken(IDevice::BYTE_SIZE_OF_CACHE_TOKEN); +inline const std::array kEmptyCacheTokenArray{}; // Returns the amount of space needed to store a value of the specified type. // diff --git a/neuralnetworks/aidl/vts/functional/ValidateModel.cpp b/neuralnetworks/aidl/vts/functional/ValidateModel.cpp index 931ba258b3..060434ed08 100644 --- a/neuralnetworks/aidl/vts/functional/ValidateModel.cpp +++ b/neuralnetworks/aidl/vts/functional/ValidateModel.cpp @@ -85,7 +85,7 @@ static void validatePrepareModelWithConfig(const std::shared_ptr& devic std::shared_ptr preparedModelCallback = ndk::SharedRefBase::make(); const auto prepareLaunchStatus = device->prepareModelWithConfig( - model, {preference, priority, kNoDeadline, {}, {}, kEmptyCacheToken, {}, {}}, + model, {preference, priority, kNoDeadline, {}, {}, kEmptyCacheTokenArray, {}, {}}, preparedModelCallback); ASSERT_FALSE(prepareLaunchStatus.isOk()); ASSERT_EQ(prepareLaunchStatus.getExceptionCode(), EX_SERVICE_SPECIFIC); diff --git a/neuralnetworks/aidl/vts/functional/VtsHalNeuralnetworks.cpp b/neuralnetworks/aidl/vts/functional/VtsHalNeuralnetworks.cpp index 51b4805134..bf87f15aa5 100644 --- a/neuralnetworks/aidl/vts/functional/VtsHalNeuralnetworks.cpp +++ b/neuralnetworks/aidl/vts/functional/VtsHalNeuralnetworks.cpp @@ -72,7 +72,7 @@ void createPreparedModel(const std::shared_ptr& device, const Model& mo kNoDeadline, {}, {}, - kEmptyCacheToken, + kEmptyCacheTokenArray, {}, {}}, preparedModelCallback); diff --git a/neuralnetworks/utils/adapter/aidl/src/Device.cpp b/neuralnetworks/utils/adapter/aidl/src/Device.cpp index 84aaddbe9d..1b90a1ab4b 100644 --- a/neuralnetworks/utils/adapter/aidl/src/Device.cpp +++ b/neuralnetworks/utils/adapter/aidl/src/Device.cpp @@ -312,8 +312,8 @@ ndk::ScopedAStatus Device::prepareModelWithConfig( const std::shared_ptr& callback) { const auto result = adapter::prepareModel( kDevice, kExecutor, model, config.preference, config.priority, config.deadlineNs, - config.modelCache, config.dataCache, config.cacheToken, config.compilationHints, - config.extensionNameToPrefix, callback); + config.modelCache, config.dataCache, utils::toVec(config.cacheToken), + config.compilationHints, config.extensionNameToPrefix, callback); if (!result.has_value()) { const auto& [message, code] = result.error(); const auto aidlCode = utils::convert(code).value_or(ErrorStatus::GENERAL_FAILURE);