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> |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame^] | 30 | #include <nnapi/hal/1.0/Callbacks.h> |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 31 | #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 Butler | aad934b | 2020-12-13 23:06:06 -0800 | [diff] [blame] | 40 | // 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 Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 43 | namespace android::hardware::neuralnetworks::V1_2::utils { |
| 44 | namespace { |
| 45 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 46 | nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 47 | convertExecutionGeneralResultsHelper(const hidl_vec<OutputShape>& outputShapes, |
| 48 | const Timing& timing) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 49 | 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] | 50 | } |
| 51 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame^] | 52 | } // namespace |
| 53 | |
| 54 | nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback( |
| 55 | V1_0::ErrorStatus status, const sp<IPreparedModel>& preparedModel) { |
| 56 | HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status); |
| 57 | return NN_TRY(PreparedModel::create(preparedModel, /*executeSynchronously=*/true)); |
| 58 | } |
| 59 | |
| 60 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> executionCallback( |
| 61 | V1_0::ErrorStatus status, const hidl_vec<OutputShape>& outputShapes, const Timing& timing) { |
| 62 | if (status == V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) { |
| 63 | auto canonicalOutputShapes = |
| 64 | nn::convert(outputShapes).value_or(std::vector<nn::OutputShape>{}); |
| 65 | return NN_ERROR(nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, std::move(canonicalOutputShapes)) |
| 66 | << "execution failed with " << toString(status); |
| 67 | } |
| 68 | HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 69 | return hal::utils::makeExecutionFailure( |
| 70 | convertExecutionGeneralResultsHelper(outputShapes, timing)); |
| 71 | } |
| 72 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 73 | Return<void> PreparedModelCallback::notify(V1_0::ErrorStatus status, |
| 74 | const sp<V1_0::IPreparedModel>& preparedModel) { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame^] | 75 | mData.put(V1_0::utils::prepareModelCallback(status, preparedModel)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 76 | return Void(); |
| 77 | } |
| 78 | |
| 79 | Return<void> PreparedModelCallback::notify_1_2(V1_0::ErrorStatus status, |
| 80 | const sp<IPreparedModel>& preparedModel) { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame^] | 81 | mData.put(prepareModelCallback(status, preparedModel)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 82 | return Void(); |
| 83 | } |
| 84 | |
| 85 | void PreparedModelCallback::notifyAsDeadObject() { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame^] | 86 | mData.put(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object"); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | PreparedModelCallback::Data PreparedModelCallback::get() { |
| 90 | return mData.take(); |
| 91 | } |
| 92 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 93 | // ExecutionCallback methods begin here |
| 94 | |
| 95 | Return<void> ExecutionCallback::notify(V1_0::ErrorStatus status) { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame^] | 96 | mData.put(V1_0::utils::executionCallback(status)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 97 | return Void(); |
| 98 | } |
| 99 | |
| 100 | Return<void> ExecutionCallback::notify_1_2(V1_0::ErrorStatus status, |
| 101 | const hidl_vec<OutputShape>& outputShapes, |
| 102 | const Timing& timing) { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame^] | 103 | mData.put(executionCallback(status, outputShapes, timing)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 104 | return Void(); |
| 105 | } |
| 106 | |
| 107 | void ExecutionCallback::notifyAsDeadObject() { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame^] | 108 | mData.put(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object"); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | ExecutionCallback::Data ExecutionCallback::get() { |
| 112 | return mData.take(); |
| 113 | } |
| 114 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 115 | } // namespace android::hardware::neuralnetworks::V1_2::utils |