Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 39 | namespace android::hardware::neuralnetworks::V1_2::utils { |
| 40 | namespace { |
| 41 | |
| 42 | nn::GeneralResult<nn::SharedPreparedModel> convertPreparedModel( |
| 43 | const sp<V1_0::IPreparedModel>& preparedModel) { |
| 44 | return NN_TRY(V1_0::utils::PreparedModel::create(preparedModel)); |
| 45 | } |
| 46 | |
| 47 | nn::GeneralResult<nn::SharedPreparedModel> convertPreparedModel( |
| 48 | const sp<IPreparedModel>& preparedModel) { |
| 49 | return NN_TRY(utils::PreparedModel::create(preparedModel)); |
| 50 | } |
| 51 | |
| 52 | nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 53 | convertExecutionGeneralResultsHelper(const hidl_vec<OutputShape>& outputShapes, |
| 54 | const Timing& timing) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame^] | 55 | return std::make_pair(NN_TRY(nn::convert(outputShapes)), NN_TRY(nn::convert(timing))); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 59 | convertExecutionGeneralResults(const hidl_vec<OutputShape>& outputShapes, const Timing& timing) { |
| 60 | return hal::utils::makeExecutionFailure( |
| 61 | convertExecutionGeneralResultsHelper(outputShapes, timing)); |
| 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | |
| 66 | Return<void> PreparedModelCallback::notify(V1_0::ErrorStatus status, |
| 67 | const sp<V1_0::IPreparedModel>& preparedModel) { |
| 68 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame^] | 69 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 70 | 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 | |
| 80 | Return<void> PreparedModelCallback::notify_1_2(V1_0::ErrorStatus status, |
| 81 | const sp<IPreparedModel>& preparedModel) { |
| 82 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame^] | 83 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 84 | 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 | |
| 94 | void PreparedModelCallback::notifyAsDeadObject() { |
| 95 | notifyInternal(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object"); |
| 96 | } |
| 97 | |
| 98 | PreparedModelCallback::Data PreparedModelCallback::get() { |
| 99 | return mData.take(); |
| 100 | } |
| 101 | |
| 102 | void PreparedModelCallback::notifyInternal(PreparedModelCallback::Data result) { |
| 103 | mData.put(std::move(result)); |
| 104 | } |
| 105 | |
| 106 | // ExecutionCallback methods begin here |
| 107 | |
| 108 | Return<void> ExecutionCallback::notify(V1_0::ErrorStatus status) { |
| 109 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame^] | 110 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 111 | notifyInternal(NN_ERROR(canonical) << "execute failed with " << toString(status)); |
| 112 | } else { |
| 113 | notifyInternal({}); |
| 114 | } |
| 115 | return Void(); |
| 116 | } |
| 117 | |
| 118 | Return<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 Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame^] | 122 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 123 | notifyInternal(NN_ERROR(canonical) << "execute failed with " << toString(status)); |
| 124 | } else { |
| 125 | notifyInternal(convertExecutionGeneralResults(outputShapes, timing)); |
| 126 | } |
| 127 | return Void(); |
| 128 | } |
| 129 | |
| 130 | void ExecutionCallback::notifyAsDeadObject() { |
| 131 | notifyInternal(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object"); |
| 132 | } |
| 133 | |
| 134 | ExecutionCallback::Data ExecutionCallback::get() { |
| 135 | return mData.take(); |
| 136 | } |
| 137 | |
| 138 | void ExecutionCallback::notifyInternal(ExecutionCallback::Data result) { |
| 139 | mData.put(std::move(result)); |
| 140 | } |
| 141 | |
| 142 | } // namespace android::hardware::neuralnetworks::V1_2::utils |