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 | |
Xusong Wang | 11f30c8 | 2021-10-05 14:10:41 -0700 | [diff] [blame] | 38 | nn::GeneralResult<std::shared_ptr<const ExecutionWithCachedRequest>> |
| 39 | ExecutionWithCachedRequest::create(std::shared_ptr<const PreparedModel> preparedModel, |
| 40 | Request request, hal::utils::RequestRelocation relocation, |
| 41 | bool measure, int64_t loopTimeoutDuration) { |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 42 | if (preparedModel == nullptr) { |
Xusong Wang | 11f30c8 | 2021-10-05 14:10:41 -0700 | [diff] [blame] | 43 | return NN_ERROR() << "aidl::utils::ExecutionWithCachedRequest::create must have non-null " |
| 44 | "preparedModel"; |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Xusong Wang | 11f30c8 | 2021-10-05 14:10:41 -0700 | [diff] [blame] | 47 | return std::make_shared<const ExecutionWithCachedRequest>( |
| 48 | PrivateConstructorTag{}, std::move(preparedModel), std::move(request), |
| 49 | std::move(relocation), measure, loopTimeoutDuration); |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Xusong Wang | 11f30c8 | 2021-10-05 14:10:41 -0700 | [diff] [blame] | 52 | ExecutionWithCachedRequest::ExecutionWithCachedRequest( |
| 53 | PrivateConstructorTag /*tag*/, std::shared_ptr<const PreparedModel> preparedModel, |
| 54 | Request request, hal::utils::RequestRelocation relocation, bool measure, |
| 55 | int64_t loopTimeoutDuration) |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 56 | : kPreparedModel(std::move(preparedModel)), |
| 57 | kRequest(std::move(request)), |
| 58 | kRelocation(std::move(relocation)), |
| 59 | kMeasure(measure), |
| 60 | kLoopTimeoutDuration(loopTimeoutDuration) {} |
| 61 | |
Xusong Wang | 11f30c8 | 2021-10-05 14:10:41 -0700 | [diff] [blame] | 62 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 63 | ExecutionWithCachedRequest::compute(const nn::OptionalTimePoint& deadline) const { |
Michael Butler | ff9a5a5 | 2021-10-15 16:23:20 -0700 | [diff] [blame] | 64 | const auto aidlDeadline = NN_TRY(convert(deadline)); |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 65 | return kPreparedModel->executeInternal(kRequest, kMeasure, aidlDeadline, kLoopTimeoutDuration, |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame^] | 66 | {}, {}, kRelocation); |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Xusong Wang | 11f30c8 | 2021-10-05 14:10:41 -0700 | [diff] [blame] | 69 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> |
| 70 | ExecutionWithCachedRequest::computeFenced( |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 71 | const std::vector<nn::SyncFence>& waitFor, const nn::OptionalTimePoint& deadline, |
| 72 | const nn::OptionalDuration& timeoutDurationAfterFence) const { |
| 73 | const auto aidlWaitFor = NN_TRY(convert(waitFor)); |
| 74 | const auto aidlDeadline = NN_TRY(convert(deadline)); |
| 75 | const auto aidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence)); |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame^] | 76 | return kPreparedModel->executeFencedInternal( |
| 77 | kRequest, aidlWaitFor, kMeasure, aidlDeadline, kLoopTimeoutDuration, |
| 78 | aidlTimeoutDurationAfterFence, {}, {}, kRelocation); |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Xusong Wang | 11f30c8 | 2021-10-05 14:10:41 -0700 | [diff] [blame] | 81 | nn::GeneralResult<std::shared_ptr<const Execution>> Execution::create( |
| 82 | std::shared_ptr<aidl_hal::IExecution> execution, hal::utils::RequestRelocation relocation) { |
| 83 | if (execution == nullptr) { |
| 84 | return NN_ERROR() << "aidl::utils::Execution::create must have non-null execution"; |
| 85 | } |
| 86 | |
| 87 | return std::make_shared<const Execution>(PrivateConstructorTag{}, std::move(execution), |
| 88 | std::move(relocation)); |
| 89 | } |
| 90 | |
| 91 | Execution::Execution(PrivateConstructorTag /*tag*/, std::shared_ptr<aidl_hal::IExecution> execution, |
| 92 | hal::utils::RequestRelocation relocation) |
| 93 | : kExecution(std::move(execution)), kRelocation(std::move(relocation)) {} |
| 94 | |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 95 | } // namespace aidl::android::hardware::neuralnetworks::utils |