blob: 39f88c2c5ef9062538fead24781c291adfe27d4d [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>
30#include <nnapi/hal/1.0/Conversions.h>
31#include <nnapi/hal/1.0/PreparedModel.h>
32#include <nnapi/hal/CommonUtils.h>
33#include <nnapi/hal/HandleError.h>
34#include <nnapi/hal/ProtectCallback.h>
35#include <nnapi/hal/TransferValue.h>
36
37#include <utility>
38
39namespace android::hardware::neuralnetworks::V1_2::utils {
40namespace {
41
42nn::GeneralResult<nn::SharedPreparedModel> convertPreparedModel(
43 const sp<V1_0::IPreparedModel>& preparedModel) {
44 return NN_TRY(V1_0::utils::PreparedModel::create(preparedModel));
45}
46
47nn::GeneralResult<nn::SharedPreparedModel> convertPreparedModel(
48 const sp<IPreparedModel>& preparedModel) {
49 return NN_TRY(utils::PreparedModel::create(preparedModel));
50}
51
52nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
53convertExecutionGeneralResultsHelper(const hidl_vec<OutputShape>& outputShapes,
54 const Timing& timing) {
Michael Butler6547b2a2020-11-22 19:36:30 -080055 return std::make_pair(NN_TRY(nn::convert(outputShapes)), NN_TRY(nn::convert(timing)));
Michael Butler4b276a72020-08-06 23:22:35 -070056}
57
58nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
59convertExecutionGeneralResults(const hidl_vec<OutputShape>& outputShapes, const Timing& timing) {
60 return hal::utils::makeExecutionFailure(
61 convertExecutionGeneralResultsHelper(outputShapes, timing));
62}
63
64} // namespace
65
66Return<void> PreparedModelCallback::notify(V1_0::ErrorStatus status,
67 const sp<V1_0::IPreparedModel>& preparedModel) {
68 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -080069 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -070070 notifyInternal(NN_ERROR(canonical) << "preparedModel failed with " << toString(status));
71 } else if (preparedModel == nullptr) {
72 notifyInternal(NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
73 << "Returned preparedModel is nullptr");
74 } else {
75 notifyInternal(convertPreparedModel(preparedModel));
76 }
77 return Void();
78}
79
80Return<void> PreparedModelCallback::notify_1_2(V1_0::ErrorStatus status,
81 const sp<IPreparedModel>& preparedModel) {
82 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -080083 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -070084 notifyInternal(NN_ERROR(canonical) << "preparedModel failed with " << toString(status));
85 } else if (preparedModel == nullptr) {
86 notifyInternal(NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
87 << "Returned preparedModel is nullptr");
88 } else {
89 notifyInternal(convertPreparedModel(preparedModel));
90 }
91 return Void();
92}
93
94void PreparedModelCallback::notifyAsDeadObject() {
95 notifyInternal(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object");
96}
97
98PreparedModelCallback::Data PreparedModelCallback::get() {
99 return mData.take();
100}
101
102void PreparedModelCallback::notifyInternal(PreparedModelCallback::Data result) {
103 mData.put(std::move(result));
104}
105
106// ExecutionCallback methods begin here
107
108Return<void> ExecutionCallback::notify(V1_0::ErrorStatus status) {
109 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800110 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700111 notifyInternal(NN_ERROR(canonical) << "execute failed with " << toString(status));
112 } else {
113 notifyInternal({});
114 }
115 return Void();
116}
117
118Return<void> ExecutionCallback::notify_1_2(V1_0::ErrorStatus status,
119 const hidl_vec<OutputShape>& outputShapes,
120 const Timing& timing) {
121 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800122 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700123 notifyInternal(NN_ERROR(canonical) << "execute failed with " << toString(status));
124 } else {
125 notifyInternal(convertExecutionGeneralResults(outputShapes, timing));
126 }
127 return Void();
128}
129
130void ExecutionCallback::notifyAsDeadObject() {
131 notifyInternal(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object");
132}
133
134ExecutionCallback::Data ExecutionCallback::get() {
135 return mData.take();
136}
137
138void ExecutionCallback::notifyInternal(ExecutionCallback::Data result) {
139 mData.put(std::move(result));
140}
141
142} // namespace android::hardware::neuralnetworks::V1_2::utils