blob: 01b5e123874da5e2a0897a64104a4520e1fa959e [file] [log] [blame]
Michael Butler4b276a72020-08-06 23:22:35 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Callbacks.h"
18
19#include "Conversions.h"
20#include "PreparedModel.h"
21#include "Utils.h"
22
23#include <android/hardware/neuralnetworks/1.0/types.h>
24#include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h>
25#include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h>
26#include <android/hardware/neuralnetworks/1.2/types.h>
27#include <nnapi/IPreparedModel.h>
28#include <nnapi/Result.h>
29#include <nnapi/Types.h>
Michael Butler7fd03c22020-12-06 21:50:59 -080030#include <nnapi/hal/1.0/Callbacks.h>
Michael Butler4b276a72020-08-06 23:22:35 -070031#include <nnapi/hal/1.0/Conversions.h>
32#include <nnapi/hal/1.0/PreparedModel.h>
33#include <nnapi/hal/CommonUtils.h>
34#include <nnapi/hal/HandleError.h>
35#include <nnapi/hal/ProtectCallback.h>
36#include <nnapi/hal/TransferValue.h>
37
38#include <utility>
39
Michael Butleraad934b2020-12-13 23:06:06 -080040// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
41// lifetimes across processes and for protecting asynchronous calls across HIDL.
42
Michael Butler4b276a72020-08-06 23:22:35 -070043namespace android::hardware::neuralnetworks::V1_2::utils {
44namespace {
45
Michael Butlere5e67022021-02-01 18:16:14 -080046nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback(
47 V1_0::ErrorStatus status, const sp<V1_0::IPreparedModel>& preparedModel) {
48 if (const auto dynamicPreparedModel =
49 V1_2::IPreparedModel::castFrom(preparedModel).withDefault(nullptr)) {
50 return V1_2::utils::prepareModelCallback(status, dynamicPreparedModel);
51 }
52 return V1_0::utils::prepareModelCallback(status, preparedModel);
53}
54
Michael Butler4b276a72020-08-06 23:22:35 -070055nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
56convertExecutionGeneralResultsHelper(const hidl_vec<OutputShape>& outputShapes,
57 const Timing& timing) {
Michael Butler6547b2a2020-11-22 19:36:30 -080058 return std::make_pair(NN_TRY(nn::convert(outputShapes)), NN_TRY(nn::convert(timing)));
Michael Butler4b276a72020-08-06 23:22:35 -070059}
60
Michael Butler7fd03c22020-12-06 21:50:59 -080061} // namespace
62
63nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback(
64 V1_0::ErrorStatus status, const sp<IPreparedModel>& preparedModel) {
65 HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
66 return NN_TRY(PreparedModel::create(preparedModel, /*executeSynchronously=*/true));
67}
68
69nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> executionCallback(
70 V1_0::ErrorStatus status, const hidl_vec<OutputShape>& outputShapes, const Timing& timing) {
71 if (status == V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
72 auto canonicalOutputShapes =
73 nn::convert(outputShapes).value_or(std::vector<nn::OutputShape>{});
74 return NN_ERROR(nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, std::move(canonicalOutputShapes))
75 << "execution failed with " << toString(status);
76 }
77 HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
Michael Butlerff9a5a52021-10-15 16:23:20 -070078 return convertExecutionGeneralResultsHelper(outputShapes, timing);
Michael Butler4b276a72020-08-06 23:22:35 -070079}
80
Michael Butler4b276a72020-08-06 23:22:35 -070081Return<void> PreparedModelCallback::notify(V1_0::ErrorStatus status,
82 const sp<V1_0::IPreparedModel>& preparedModel) {
Michael Butlere5e67022021-02-01 18:16:14 -080083 mData.put(prepareModelCallback(status, preparedModel));
Michael Butler4b276a72020-08-06 23:22:35 -070084 return Void();
85}
86
87Return<void> PreparedModelCallback::notify_1_2(V1_0::ErrorStatus status,
88 const sp<IPreparedModel>& preparedModel) {
Michael Butler7fd03c22020-12-06 21:50:59 -080089 mData.put(prepareModelCallback(status, preparedModel));
Michael Butler4b276a72020-08-06 23:22:35 -070090 return Void();
91}
92
93void PreparedModelCallback::notifyAsDeadObject() {
Michael Butler7fd03c22020-12-06 21:50:59 -080094 mData.put(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object");
Michael Butler4b276a72020-08-06 23:22:35 -070095}
96
97PreparedModelCallback::Data PreparedModelCallback::get() {
98 return mData.take();
99}
100
Michael Butler4b276a72020-08-06 23:22:35 -0700101// ExecutionCallback methods begin here
102
103Return<void> ExecutionCallback::notify(V1_0::ErrorStatus status) {
Michael Butler7fd03c22020-12-06 21:50:59 -0800104 mData.put(V1_0::utils::executionCallback(status));
Michael Butler4b276a72020-08-06 23:22:35 -0700105 return Void();
106}
107
108Return<void> ExecutionCallback::notify_1_2(V1_0::ErrorStatus status,
109 const hidl_vec<OutputShape>& outputShapes,
110 const Timing& timing) {
Michael Butler7fd03c22020-12-06 21:50:59 -0800111 mData.put(executionCallback(status, outputShapes, timing));
Michael Butler4b276a72020-08-06 23:22:35 -0700112 return Void();
113}
114
115void ExecutionCallback::notifyAsDeadObject() {
Michael Butler7fd03c22020-12-06 21:50:59 -0800116 mData.put(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object");
Michael Butler4b276a72020-08-06 23:22:35 -0700117}
118
119ExecutionCallback::Data ExecutionCallback::get() {
120 return mData.take();
121}
122
Michael Butler4b276a72020-08-06 23:22:35 -0700123} // namespace android::hardware::neuralnetworks::V1_2::utils