Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 "Execution.h" |
| 18 | |
| 19 | #include "Conversions.h" |
| 20 | #include "PreparedModel.h" |
| 21 | #include "Utils.h" |
| 22 | |
| 23 | #include <aidl/android/hardware/neuralnetworks/Request.h> |
| 24 | #include <nnapi/IExecution.h> |
| 25 | #include <nnapi/Result.h> |
| 26 | #include <nnapi/Types.h> |
| 27 | #include <nnapi/hal/CommonUtils.h> |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 28 | |
| 29 | #include <memory> |
| 30 | #include <utility> |
| 31 | #include <vector> |
| 32 | |
| 33 | // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface |
| 34 | // lifetimes across processes and for protecting asynchronous calls across HIDL. |
| 35 | |
| 36 | namespace aidl::android::hardware::neuralnetworks::utils { |
| 37 | |
| 38 | nn::GeneralResult<std::shared_ptr<const Execution>> Execution::create( |
| 39 | std::shared_ptr<const PreparedModel> preparedModel, Request request, |
| 40 | hal::utils::RequestRelocation relocation, bool measure, int64_t loopTimeoutDuration) { |
| 41 | if (preparedModel == nullptr) { |
| 42 | return NN_ERROR() << "aidl::utils::Execution::create must have non-null preparedModel"; |
| 43 | } |
| 44 | |
| 45 | return std::make_shared<const Execution>(PrivateConstructorTag{}, std::move(preparedModel), |
| 46 | std::move(request), std::move(relocation), measure, |
| 47 | loopTimeoutDuration); |
| 48 | } |
| 49 | |
| 50 | Execution::Execution(PrivateConstructorTag /*tag*/, |
| 51 | std::shared_ptr<const PreparedModel> preparedModel, Request request, |
| 52 | hal::utils::RequestRelocation relocation, bool measure, |
| 53 | int64_t loopTimeoutDuration) |
| 54 | : kPreparedModel(std::move(preparedModel)), |
| 55 | kRequest(std::move(request)), |
| 56 | kRelocation(std::move(relocation)), |
| 57 | kMeasure(measure), |
| 58 | kLoopTimeoutDuration(loopTimeoutDuration) {} |
| 59 | |
| 60 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Execution::compute( |
| 61 | const nn::OptionalTimePoint& deadline) const { |
Michael Butler | ff9a5a5 | 2021-10-15 16:23:20 -0700 | [diff] [blame] | 62 | const auto aidlDeadline = NN_TRY(convert(deadline)); |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 63 | return kPreparedModel->executeInternal(kRequest, kMeasure, aidlDeadline, kLoopTimeoutDuration, |
| 64 | kRelocation); |
| 65 | } |
| 66 | |
| 67 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> Execution::computeFenced( |
| 68 | const std::vector<nn::SyncFence>& waitFor, const nn::OptionalTimePoint& deadline, |
| 69 | const nn::OptionalDuration& timeoutDurationAfterFence) const { |
| 70 | const auto aidlWaitFor = NN_TRY(convert(waitFor)); |
| 71 | const auto aidlDeadline = NN_TRY(convert(deadline)); |
| 72 | const auto aidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence)); |
| 73 | return kPreparedModel->executeFencedInternal(kRequest, aidlWaitFor, kMeasure, aidlDeadline, |
| 74 | kLoopTimeoutDuration, |
| 75 | aidlTimeoutDurationAfterFence, kRelocation); |
| 76 | } |
| 77 | |
| 78 | } // namespace aidl::android::hardware::neuralnetworks::utils |