Merge "Make NN canonical->HIDL adapter execute* methods synchronous" am: f955569c8a am: c7d8c19823

Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/2005130

Change-Id: I09577f2bf81869613f9e61d15c68edd8a49e41ef
This commit is contained in:
Michael Butler 2022-03-04 01:22:10 +00:00 committed by Automerger Merge Worker
commit 277e31957b
5 changed files with 49 additions and 87 deletions

View file

@ -46,9 +46,6 @@ using Executor = std::function<void(Task, ::android::nn::OptionalTimePoint)>;
/** /**
* Adapt an NNAPI canonical interface object to a AIDL NN HAL interface object. * Adapt an NNAPI canonical interface object to a AIDL NN HAL interface object.
* *
* The IPreparedModel object created from IDevice::prepareModel or IDevice::preparedModelFromCache
* must return "const nn::Model*" from IPreparedModel::getUnderlyingResource().
*
* @param device NNAPI canonical IDevice interface object to be adapted. * @param device NNAPI canonical IDevice interface object to be adapted.
* @param executor Type-erased executor to handle executing tasks asynchronously. * @param executor Type-erased executor to handle executing tasks asynchronously.
* @return AIDL NN HAL IDevice interface object. * @return AIDL NN HAL IDevice interface object.
@ -58,9 +55,6 @@ std::shared_ptr<BnDevice> adapt(::android::nn::SharedDevice device, Executor exe
/** /**
* Adapt an NNAPI canonical interface object to a AIDL NN HAL interface object. * Adapt an NNAPI canonical interface object to a AIDL NN HAL interface object.
* *
* The IPreparedModel object created from IDevice::prepareModel or IDevice::preparedModelFromCache
* must return "const nn::Model*" from IPreparedModel::getUnderlyingResource().
*
* This function uses a default executor, which will execute tasks from a detached thread. * This function uses a default executor, which will execute tasks from a detached thread.
* *
* @param device NNAPI canonical IDevice interface object to be adapted. * @param device NNAPI canonical IDevice interface object to be adapted.

View file

@ -46,9 +46,6 @@ using Executor = std::function<void(Task, nn::OptionalTimePoint)>;
/** /**
* Adapt an NNAPI canonical interface object to a HIDL NN HAL interface object. * Adapt an NNAPI canonical interface object to a HIDL NN HAL interface object.
* *
* The IPreparedModel object created from IDevice::prepareModel or IDevice::preparedModelFromCache
* must return "const nn::Model*" from IPreparedModel::getUnderlyingResource().
*
* @param device NNAPI canonical IDevice interface object to be adapted. * @param device NNAPI canonical IDevice interface object to be adapted.
* @param executor Type-erased executor to handle executing tasks asynchronously. * @param executor Type-erased executor to handle executing tasks asynchronously.
* @return HIDL NN HAL IDevice interface object. * @return HIDL NN HAL IDevice interface object.
@ -58,9 +55,6 @@ sp<V1_3::IDevice> adapt(nn::SharedDevice device, Executor executor);
/** /**
* Adapt an NNAPI canonical interface object to a HIDL NN HAL interface object. * Adapt an NNAPI canonical interface object to a HIDL NN HAL interface object.
* *
* The IPreparedModel object created from IDevice::prepareModel or IDevice::preparedModelFromCache
* must return "const nn::Model*" from IPreparedModel::getUnderlyingResource().
*
* This function uses a default executor, which will execute tasks from a detached thread. * This function uses a default executor, which will execute tasks from a detached thread.
* *
* @param device NNAPI canonical IDevice interface object to be adapted. * @param device NNAPI canonical IDevice interface object to be adapted.

View file

@ -39,7 +39,7 @@ namespace android::hardware::neuralnetworks::adapter {
// Class that adapts nn::IPreparedModel to V1_3::IPreparedModel. // Class that adapts nn::IPreparedModel to V1_3::IPreparedModel.
class PreparedModel final : public V1_3::IPreparedModel { class PreparedModel final : public V1_3::IPreparedModel {
public: public:
PreparedModel(nn::SharedPreparedModel preparedModel, Executor executor); explicit PreparedModel(nn::SharedPreparedModel preparedModel);
Return<V1_0::ErrorStatus> execute(const V1_0::Request& request, Return<V1_0::ErrorStatus> execute(const V1_0::Request& request,
const sp<V1_0::IExecutionCallback>& callback) override; const sp<V1_0::IExecutionCallback>& callback) override;
@ -70,7 +70,6 @@ class PreparedModel final : public V1_3::IPreparedModel {
private: private:
const nn::SharedPreparedModel kPreparedModel; const nn::SharedPreparedModel kPreparedModel;
const Executor kExecutor;
}; };
} // namespace android::hardware::neuralnetworks::adapter } // namespace android::hardware::neuralnetworks::adapter

View file

@ -62,11 +62,11 @@ auto convertInput(const Type& object) -> decltype(nn::convert(std::declval<Type>
using PrepareModelResult = nn::GeneralResult<nn::SharedPreparedModel>; using PrepareModelResult = nn::GeneralResult<nn::SharedPreparedModel>;
sp<PreparedModel> adaptPreparedModel(nn::SharedPreparedModel preparedModel, Executor executor) { sp<PreparedModel> adaptPreparedModel(nn::SharedPreparedModel preparedModel) {
if (preparedModel == nullptr) { if (preparedModel == nullptr) {
return nullptr; return nullptr;
} }
return sp<PreparedModel>::make(std::move(preparedModel), std::move(executor)); return sp<PreparedModel>::make(std::move(preparedModel));
} }
void notify(V1_0::IPreparedModelCallback* callback, nn::ErrorStatus status, void notify(V1_0::IPreparedModelCallback* callback, nn::ErrorStatus status,
@ -105,14 +105,14 @@ void notify(V1_3::IPreparedModelCallback* callback, nn::ErrorStatus status,
} }
template <typename CallbackType> template <typename CallbackType>
void notify(CallbackType* callback, PrepareModelResult result, Executor executor) { void notify(CallbackType* callback, PrepareModelResult result) {
if (!result.has_value()) { if (!result.has_value()) {
const auto [message, status] = std::move(result).error(); const auto [message, status] = std::move(result).error();
LOG(ERROR) << message; LOG(ERROR) << message;
notify(callback, status, nullptr); notify(callback, status, nullptr);
} else { } else {
auto preparedModel = std::move(result).value(); auto preparedModel = std::move(result).value();
auto hidlPreparedModel = adaptPreparedModel(std::move(preparedModel), std::move(executor)); auto hidlPreparedModel = adaptPreparedModel(std::move(preparedModel));
notify(callback, nn::ErrorStatus::NONE, std::move(hidlPreparedModel)); notify(callback, nn::ErrorStatus::NONE, std::move(hidlPreparedModel));
} }
} }
@ -133,10 +133,10 @@ nn::GeneralResult<void> prepareModel(const nn::SharedDevice& device, const Execu
auto nnModel = NN_TRY(convertInput(model)); auto nnModel = NN_TRY(convertInput(model));
Task task = [device, nnModel = std::move(nnModel), executor, callback] { Task task = [device, nnModel = std::move(nnModel), callback] {
auto result = device->prepareModel(nnModel, nn::ExecutionPreference::DEFAULT, auto result = device->prepareModel(nnModel, nn::ExecutionPreference::DEFAULT,
nn::Priority::DEFAULT, {}, {}, {}, {}, {}, {}); nn::Priority::DEFAULT, {}, {}, {}, {}, {}, {});
notify(callback.get(), std::move(result), executor); notify(callback.get(), std::move(result));
}; };
executor(std::move(task), {}); executor(std::move(task), {});
@ -154,10 +154,10 @@ nn::GeneralResult<void> prepareModel_1_1(const nn::SharedDevice& device, const E
auto nnModel = NN_TRY(convertInput(model)); auto nnModel = NN_TRY(convertInput(model));
const auto nnPreference = NN_TRY(convertInput(preference)); const auto nnPreference = NN_TRY(convertInput(preference));
Task task = [device, nnModel = std::move(nnModel), nnPreference, executor, callback] { Task task = [device, nnModel = std::move(nnModel), nnPreference, callback] {
auto result = device->prepareModel(nnModel, nnPreference, nn::Priority::DEFAULT, {}, {}, {}, auto result = device->prepareModel(nnModel, nnPreference, nn::Priority::DEFAULT, {}, {}, {},
{}, {}, {}); {}, {}, {});
notify(callback.get(), std::move(result), executor); notify(callback.get(), std::move(result));
}; };
executor(std::move(task), {}); executor(std::move(task), {});
@ -183,10 +183,10 @@ nn::GeneralResult<void> prepareModel_1_2(const nn::SharedDevice& device, const E
Task task = [device, nnModel = std::move(nnModel), nnPreference, Task task = [device, nnModel = std::move(nnModel), nnPreference,
nnModelCache = std::move(nnModelCache), nnDataCache = std::move(nnDataCache), nnModelCache = std::move(nnModelCache), nnDataCache = std::move(nnDataCache),
nnToken, executor, callback] { nnToken, callback] {
auto result = device->prepareModel(nnModel, nnPreference, nn::Priority::DEFAULT, {}, auto result = device->prepareModel(nnModel, nnPreference, nn::Priority::DEFAULT, {},
nnModelCache, nnDataCache, nnToken, {}, {}); nnModelCache, nnDataCache, nnToken, {}, {});
notify(callback.get(), std::move(result), executor); notify(callback.get(), std::move(result));
}; };
executor(std::move(task), {}); executor(std::move(task), {});
@ -213,10 +213,10 @@ nn::GeneralResult<void> prepareModel_1_3(
Task task = [device, nnModel = std::move(nnModel), nnPreference, nnPriority, nnDeadline, Task task = [device, nnModel = std::move(nnModel), nnPreference, nnPriority, nnDeadline,
nnModelCache = std::move(nnModelCache), nnDataCache = std::move(nnDataCache), nnModelCache = std::move(nnModelCache), nnDataCache = std::move(nnDataCache),
nnToken, executor, callback] { nnToken, callback] {
auto result = device->prepareModel(nnModel, nnPreference, nnPriority, nnDeadline, auto result = device->prepareModel(nnModel, nnPreference, nnPriority, nnDeadline,
nnModelCache, nnDataCache, nnToken, {}, {}); nnModelCache, nnDataCache, nnToken, {}, {});
notify(callback.get(), std::move(result), executor); notify(callback.get(), std::move(result));
}; };
executor(std::move(task), nnDeadline); executor(std::move(task), nnDeadline);
@ -238,9 +238,9 @@ nn::GeneralResult<void> prepareModelFromCache(const nn::SharedDevice& device,
const auto nnToken = nn::CacheToken(token); const auto nnToken = nn::CacheToken(token);
Task task = [device, nnModelCache = std::move(nnModelCache), Task task = [device, nnModelCache = std::move(nnModelCache),
nnDataCache = std::move(nnDataCache), nnToken, executor, callback] { nnDataCache = std::move(nnDataCache), nnToken, callback] {
auto result = device->prepareModelFromCache({}, nnModelCache, nnDataCache, nnToken); auto result = device->prepareModelFromCache({}, nnModelCache, nnDataCache, nnToken);
notify(callback.get(), std::move(result), executor); notify(callback.get(), std::move(result));
}; };
executor(std::move(task), {}); executor(std::move(task), {});
@ -262,9 +262,9 @@ nn::GeneralResult<void> prepareModelFromCache_1_3(
const auto nnToken = nn::CacheToken(token); const auto nnToken = nn::CacheToken(token);
auto task = [device, nnDeadline, nnModelCache = std::move(nnModelCache), auto task = [device, nnDeadline, nnModelCache = std::move(nnModelCache),
nnDataCache = std::move(nnDataCache), nnToken, executor, callback] { nnDataCache = std::move(nnDataCache), nnToken, callback] {
auto result = device->prepareModelFromCache(nnDeadline, nnModelCache, nnDataCache, nnToken); auto result = device->prepareModelFromCache(nnDeadline, nnModelCache, nnDataCache, nnToken);
notify(callback.get(), std::move(result), executor); notify(callback.get(), std::move(result));
}; };
executor(std::move(task), nnDeadline); executor(std::move(task), nnDeadline);

View file

@ -55,15 +55,6 @@ auto convertInput(const Type& object) -> decltype(nn::convert(std::declval<Type>
return result; return result;
} }
nn::GeneralResult<nn::Version> validateRequestForModel(const nn::Request& request,
const nn::Model& model) {
nn::GeneralResult<nn::Version> version = nn::validateRequestForModel(request, model);
if (!version.ok()) {
version.error().code = nn::ErrorStatus::INVALID_ARGUMENT;
}
return version;
}
class FencedExecutionCallback final : public V1_3::IFencedExecutionCallback { class FencedExecutionCallback final : public V1_3::IFencedExecutionCallback {
public: public:
explicit FencedExecutionCallback(const nn::ExecuteFencedInfoCallback& callback) explicit FencedExecutionCallback(const nn::ExecuteFencedInfoCallback& callback)
@ -144,58 +135,48 @@ void notify(CallbackType* callback, ExecutionResult result) {
} }
nn::GeneralResult<void> execute(const nn::SharedPreparedModel& preparedModel, nn::GeneralResult<void> execute(const nn::SharedPreparedModel& preparedModel,
const Executor& executor, const V1_0::Request& request, const V1_0::Request& request,
const sp<V1_0::IExecutionCallback>& callback) { const sp<V1_0::IExecutionCallback>& callback) {
if (callback.get() == nullptr) { if (callback.get() == nullptr) {
return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) << "Invalid callback"; return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) << "Invalid callback";
} }
auto nnRequest = NN_TRY(convertInput(request)); const auto nnRequest = NN_TRY(convertInput(request));
const std::any resource = preparedModel->getUnderlyingResource(); auto result = preparedModel->execute(nnRequest, nn::MeasureTiming::NO, {}, {}, {}, {});
if (const auto* model = std::any_cast<const nn::Model*>(&resource)) {
CHECK(*model != nullptr); if (!result.ok() && result.error().code == nn::ErrorStatus::INVALID_ARGUMENT) {
NN_TRY(adapter::validateRequestForModel(nnRequest, **model)); const auto& [message, code, outputShapes] = result.error();
return nn::error(code) << message;
} }
Task task = [preparedModel, nnRequest = std::move(nnRequest), callback] { notify(callback.get(), std::move(result));
auto result = preparedModel->execute(nnRequest, nn::MeasureTiming::NO, {}, {}, {}, {});
notify(callback.get(), std::move(result));
};
executor(std::move(task), {});
return {}; return {};
} }
nn::GeneralResult<void> execute_1_2(const nn::SharedPreparedModel& preparedModel, nn::GeneralResult<void> execute_1_2(const nn::SharedPreparedModel& preparedModel,
const Executor& executor, const V1_0::Request& request, const V1_0::Request& request, V1_2::MeasureTiming measure,
V1_2::MeasureTiming measure,
const sp<V1_2::IExecutionCallback>& callback) { const sp<V1_2::IExecutionCallback>& callback) {
if (callback.get() == nullptr) { if (callback.get() == nullptr) {
return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) << "Invalid callback"; return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) << "Invalid callback";
} }
auto nnRequest = NN_TRY(convertInput(request)); const auto nnRequest = NN_TRY(convertInput(request));
const auto nnMeasure = NN_TRY(convertInput(measure)); const auto nnMeasure = NN_TRY(convertInput(measure));
const std::any resource = preparedModel->getUnderlyingResource(); auto result = preparedModel->execute(nnRequest, nnMeasure, {}, {}, {}, {});
if (const auto* model = std::any_cast<const nn::Model*>(&resource)) {
CHECK(*model != nullptr); if (!result.ok() && result.error().code == nn::ErrorStatus::INVALID_ARGUMENT) {
NN_TRY(adapter::validateRequestForModel(nnRequest, **model)); const auto& [message, code, outputShapes] = result.error();
return nn::error(code) << message;
} }
Task task = [preparedModel, nnRequest = std::move(nnRequest), nnMeasure, callback] { notify(callback.get(), std::move(result));
auto result = preparedModel->execute(nnRequest, nnMeasure, {}, {}, {}, {});
notify(callback.get(), std::move(result));
};
executor(std::move(task), {});
return {}; return {};
} }
nn::GeneralResult<void> execute_1_3(const nn::SharedPreparedModel& preparedModel, nn::GeneralResult<void> execute_1_3(const nn::SharedPreparedModel& preparedModel,
const Executor& executor, const V1_3::Request& request, const V1_3::Request& request, V1_2::MeasureTiming measure,
V1_2::MeasureTiming measure,
const V1_3::OptionalTimePoint& deadline, const V1_3::OptionalTimePoint& deadline,
const V1_3::OptionalTimeoutDuration& loopTimeoutDuration, const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
const sp<V1_3::IExecutionCallback>& callback) { const sp<V1_3::IExecutionCallback>& callback) {
@ -203,25 +184,20 @@ nn::GeneralResult<void> execute_1_3(const nn::SharedPreparedModel& preparedModel
return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) << "Invalid callback"; return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) << "Invalid callback";
} }
auto nnRequest = NN_TRY(convertInput(request)); const auto nnRequest = NN_TRY(convertInput(request));
const auto nnMeasure = NN_TRY(convertInput(measure)); const auto nnMeasure = NN_TRY(convertInput(measure));
const auto nnDeadline = NN_TRY(convertInput(deadline)); const auto nnDeadline = NN_TRY(convertInput(deadline));
const auto nnLoopTimeoutDuration = NN_TRY(convertInput(loopTimeoutDuration)); const auto nnLoopTimeoutDuration = NN_TRY(convertInput(loopTimeoutDuration));
const std::any resource = preparedModel->getUnderlyingResource(); auto result =
if (const auto* model = std::any_cast<const nn::Model*>(&resource)) { preparedModel->execute(nnRequest, nnMeasure, nnDeadline, nnLoopTimeoutDuration, {}, {});
CHECK(*model != nullptr);
NN_TRY(adapter::validateRequestForModel(nnRequest, **model)); if (!result.ok() && result.error().code == nn::ErrorStatus::INVALID_ARGUMENT) {
const auto& [message, code, outputShapes] = result.error();
return nn::error(code) << message;
} }
Task task = [preparedModel, nnRequest = std::move(nnRequest), nnMeasure, nnDeadline, notify(callback.get(), std::move(result));
nnLoopTimeoutDuration, callback] {
auto result = preparedModel->execute(nnRequest, nnMeasure, nnDeadline,
nnLoopTimeoutDuration, {}, {});
notify(callback.get(), std::move(result));
};
executor(std::move(task), nnDeadline);
return {}; return {};
} }
@ -304,10 +280,9 @@ nn::GeneralResult<std::pair<hidl_handle, sp<V1_3::IFencedExecutionCallback>>> ex
} // namespace } // namespace
PreparedModel::PreparedModel(nn::SharedPreparedModel preparedModel, Executor executor) PreparedModel::PreparedModel(nn::SharedPreparedModel preparedModel)
: kPreparedModel(std::move(preparedModel)), kExecutor(std::move(executor)) { : kPreparedModel(std::move(preparedModel)) {
CHECK(kPreparedModel != nullptr); CHECK(kPreparedModel != nullptr);
CHECK(kExecutor != nullptr);
} }
nn::SharedPreparedModel PreparedModel::getUnderlyingPreparedModel() const { nn::SharedPreparedModel PreparedModel::getUnderlyingPreparedModel() const {
@ -316,7 +291,7 @@ nn::SharedPreparedModel PreparedModel::getUnderlyingPreparedModel() const {
Return<V1_0::ErrorStatus> PreparedModel::execute(const V1_0::Request& request, Return<V1_0::ErrorStatus> PreparedModel::execute(const V1_0::Request& request,
const sp<V1_0::IExecutionCallback>& callback) { const sp<V1_0::IExecutionCallback>& callback) {
auto result = adapter::execute(kPreparedModel, kExecutor, request, callback); auto result = adapter::execute(kPreparedModel, request, callback);
if (!result.has_value()) { if (!result.has_value()) {
auto [message, code] = std::move(result).error(); auto [message, code] = std::move(result).error();
LOG(ERROR) << "adapter::PreparedModel::execute failed with " << code << ": " << message; LOG(ERROR) << "adapter::PreparedModel::execute failed with " << code << ": " << message;
@ -329,7 +304,7 @@ Return<V1_0::ErrorStatus> PreparedModel::execute(const V1_0::Request& request,
Return<V1_0::ErrorStatus> PreparedModel::execute_1_2(const V1_0::Request& request, Return<V1_0::ErrorStatus> PreparedModel::execute_1_2(const V1_0::Request& request,
V1_2::MeasureTiming measure, V1_2::MeasureTiming measure,
const sp<V1_2::IExecutionCallback>& callback) { const sp<V1_2::IExecutionCallback>& callback) {
auto result = adapter::execute_1_2(kPreparedModel, kExecutor, request, measure, callback); auto result = adapter::execute_1_2(kPreparedModel, request, measure, callback);
if (!result.has_value()) { if (!result.has_value()) {
auto [message, code] = std::move(result).error(); auto [message, code] = std::move(result).error();
LOG(ERROR) << "adapter::PreparedModel::execute_1_2 failed with " << code << ": " << message; LOG(ERROR) << "adapter::PreparedModel::execute_1_2 failed with " << code << ": " << message;
@ -344,7 +319,7 @@ Return<V1_3::ErrorStatus> PreparedModel::execute_1_3(
const V1_3::OptionalTimePoint& deadline, const V1_3::OptionalTimePoint& deadline,
const V1_3::OptionalTimeoutDuration& loopTimeoutDuration, const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
const sp<V1_3::IExecutionCallback>& callback) { const sp<V1_3::IExecutionCallback>& callback) {
auto result = adapter::execute_1_3(kPreparedModel, kExecutor, request, measure, deadline, auto result = adapter::execute_1_3(kPreparedModel, request, measure, deadline,
loopTimeoutDuration, callback); loopTimeoutDuration, callback);
if (!result.has_value()) { if (!result.has_value()) {
auto [message, code] = std::move(result).error(); auto [message, code] = std::move(result).error();
@ -405,8 +380,8 @@ Return<void> PreparedModel::configureExecutionBurst(
cb(V1_2::utils::convert(code).value(), nullptr); cb(V1_2::utils::convert(code).value(), nullptr);
return Void(); return Void();
} }
auto burstContext = std::move(result).value(); const auto burstContext = std::move(result).value();
cb(V1_0::ErrorStatus::NONE, std::move(burstContext)); cb(V1_0::ErrorStatus::NONE, burstContext);
return Void(); return Void();
} }