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 "PreparedModel.h"
18 
19 #include "Burst.h"
20 #include "Callbacks.h"
21 #include "Conversions.h"
22 #include "Execution.h"
23 #include "ProtectCallback.h"
24 #include "Utils.h"
25 
26 #include <aidl/android/hardware/neuralnetworks/Request.h>
27 #include <android/binder_auto_utils.h>
28 #include <nnapi/IPreparedModel.h>
29 #include <nnapi/Result.h>
30 #include <nnapi/TypeUtils.h>
31 #include <nnapi/Types.h>
32 #include <nnapi/hal/CommonUtils.h>
33 #include <nnapi/hal/HandleError.h>
34 
35 #include <memory>
36 #include <tuple>
37 #include <utility>
38 #include <vector>
39 
40 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
41 // lifetimes across processes and for protecting asynchronous calls across AIDL.
42 
43 namespace aidl::android::hardware::neuralnetworks::utils {
44 namespace {
45 
convertExecutionResults(const std::vector<OutputShape> & outputShapes,const Timing & timing)46 nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> convertExecutionResults(
47         const std::vector<OutputShape>& outputShapes, const Timing& timing) {
48     return std::make_pair(NN_TRY(nn::convert(outputShapes)), NN_TRY(nn::convert(timing)));
49 }
50 
convertFencedExecutionResults(ErrorStatus status,const aidl_hal::Timing & timingLaunched,const aidl_hal::Timing & timingFenced)51 nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> convertFencedExecutionResults(
52         ErrorStatus status, const aidl_hal::Timing& timingLaunched,
53         const aidl_hal::Timing& timingFenced) {
54     HANDLE_HAL_STATUS(status) << "fenced execution callback info failed with " << toString(status);
55     return std::make_pair(NN_TRY(nn::convert(timingLaunched)), NN_TRY(nn::convert(timingFenced)));
56 }
57 
58 }  // namespace
59 
create(std::shared_ptr<aidl_hal::IPreparedModel> preparedModel)60 nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
61         std::shared_ptr<aidl_hal::IPreparedModel> preparedModel) {
62     if (preparedModel == nullptr) {
63         return NN_ERROR()
64                << "aidl_hal::utils::PreparedModel::create must have non-null preparedModel";
65     }
66 
67     return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, std::move(preparedModel));
68 }
69 
PreparedModel(PrivateConstructorTag,std::shared_ptr<aidl_hal::IPreparedModel> preparedModel)70 PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/,
71                              std::shared_ptr<aidl_hal::IPreparedModel> preparedModel)
72     : kPreparedModel(std::move(preparedModel)) {}
73 
execute(const nn::Request & request,nn::MeasureTiming measure,const nn::OptionalTimePoint & deadline,const nn::OptionalDuration & loopTimeoutDuration) const74 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
75         const nn::Request& request, nn::MeasureTiming measure,
76         const nn::OptionalTimePoint& deadline,
77         const nn::OptionalDuration& loopTimeoutDuration) const {
78     // Ensure that request is ready for IPC.
79     std::optional<nn::Request> maybeRequestInShared;
80     hal::utils::RequestRelocation relocation;
81     const nn::Request& requestInShared =
82             NN_TRY(hal::utils::makeExecutionFailure(hal::utils::convertRequestFromPointerToShared(
83                     &request, nn::kDefaultRequestMemoryAlignment, nn::kDefaultRequestMemoryPadding,
84                     &maybeRequestInShared, &relocation)));
85 
86     const auto aidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
87     const auto aidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure)));
88     const auto aidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline)));
89     const auto aidlLoopTimeoutDuration =
90             NN_TRY(hal::utils::makeExecutionFailure(convert(loopTimeoutDuration)));
91     return executeInternal(aidlRequest, aidlMeasure, aidlDeadline, aidlLoopTimeoutDuration,
92                            relocation);
93 }
94 
95 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
executeInternal(const Request & request,bool measure,int64_t deadline,int64_t loopTimeoutDuration,const hal::utils::RequestRelocation & relocation) const96 PreparedModel::executeInternal(const Request& request, bool measure, int64_t deadline,
97                                int64_t loopTimeoutDuration,
98                                const hal::utils::RequestRelocation& relocation) const {
99     if (relocation.input) {
100         relocation.input->flush();
101     }
102 
103     ExecutionResult executionResult;
104     const auto ret = kPreparedModel->executeSynchronously(request, measure, deadline,
105                                                           loopTimeoutDuration, &executionResult);
106     HANDLE_ASTATUS(ret) << "executeSynchronously failed";
107     if (!executionResult.outputSufficientSize) {
108         auto canonicalOutputShapes =
109                 nn::convert(executionResult.outputShapes).value_or(std::vector<nn::OutputShape>{});
110         return NN_ERROR(nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, std::move(canonicalOutputShapes))
111                << "execution failed with " << nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE;
112     }
113     auto [outputShapes, timing] = NN_TRY(hal::utils::makeExecutionFailure(
114             convertExecutionResults(executionResult.outputShapes, executionResult.timing)));
115 
116     if (relocation.output) {
117         relocation.output->flush();
118     }
119     return std::make_pair(std::move(outputShapes), timing);
120 }
121 
122 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
executeFenced(const nn::Request & request,const std::vector<nn::SyncFence> & waitFor,nn::MeasureTiming measure,const nn::OptionalTimePoint & deadline,const nn::OptionalDuration & loopTimeoutDuration,const nn::OptionalDuration & timeoutDurationAfterFence) const123 PreparedModel::executeFenced(const nn::Request& request, const std::vector<nn::SyncFence>& waitFor,
124                              nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline,
125                              const nn::OptionalDuration& loopTimeoutDuration,
126                              const nn::OptionalDuration& timeoutDurationAfterFence) const {
127     // Ensure that request is ready for IPC.
128     std::optional<nn::Request> maybeRequestInShared;
129     hal::utils::RequestRelocation relocation;
130     const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
131             &request, nn::kDefaultRequestMemoryAlignment, nn::kDefaultRequestMemoryPadding,
132             &maybeRequestInShared, &relocation));
133 
134     const auto aidlRequest = NN_TRY(convert(requestInShared));
135     const auto aidlWaitFor = NN_TRY(convert(waitFor));
136     const auto aidlMeasure = NN_TRY(convert(measure));
137     const auto aidlDeadline = NN_TRY(convert(deadline));
138     const auto aidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
139     const auto aidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
140     return executeFencedInternal(aidlRequest, aidlWaitFor, aidlMeasure, aidlDeadline,
141                                  aidlLoopTimeoutDuration, aidlTimeoutDurationAfterFence,
142                                  relocation);
143 }
144 
145 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
executeFencedInternal(const Request & request,const std::vector<ndk::ScopedFileDescriptor> & waitFor,bool measure,int64_t deadline,int64_t loopTimeoutDuration,int64_t timeoutDurationAfterFence,const hal::utils::RequestRelocation & relocation) const146 PreparedModel::executeFencedInternal(const Request& request,
147                                      const std::vector<ndk::ScopedFileDescriptor>& waitFor,
148                                      bool measure, int64_t deadline, int64_t loopTimeoutDuration,
149                                      int64_t timeoutDurationAfterFence,
150                                      const hal::utils::RequestRelocation& relocation) const {
151     if (relocation.input) {
152         relocation.input->flush();
153     }
154 
155     FencedExecutionResult result;
156     const auto ret =
157             kPreparedModel->executeFenced(request, waitFor, measure, deadline, loopTimeoutDuration,
158                                           timeoutDurationAfterFence, &result);
159     HANDLE_ASTATUS(ret) << "executeFenced failed";
160 
161     auto resultSyncFence = nn::SyncFence::createAsSignaled();
162     if (result.syncFence.get() != -1) {
163         resultSyncFence = NN_TRY(nn::convert(result.syncFence));
164     }
165 
166     auto callback = result.callback;
167     if (callback == nullptr) {
168         return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "callback is null";
169     }
170 
171     // If executeFenced required the request memory to be moved into shared memory, block here until
172     // the fenced execution has completed and flush the memory back.
173     if (relocation.output) {
174         const auto state = resultSyncFence.syncWait({});
175         if (state != nn::SyncFence::FenceState::SIGNALED) {
176             return NN_ERROR() << "syncWait failed with " << state;
177         }
178         relocation.output->flush();
179     }
180 
181     // Create callback which can be used to retrieve the execution error status and timings.
182     nn::ExecuteFencedInfoCallback resultCallback =
183             [callback]() -> nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> {
184         ErrorStatus errorStatus;
185         Timing timingLaunched;
186         Timing timingFenced;
187         const auto ret = callback->getExecutionInfo(&timingLaunched, &timingFenced, &errorStatus);
188         HANDLE_ASTATUS(ret) << "fenced execution callback getExecutionInfo failed";
189         return convertFencedExecutionResults(errorStatus, timingLaunched, timingFenced);
190     };
191 
192     return std::make_pair(std::move(resultSyncFence), std::move(resultCallback));
193 }
194 
createReusableExecution(const nn::Request & request,nn::MeasureTiming measure,const nn::OptionalDuration & loopTimeoutDuration) const195 nn::GeneralResult<nn::SharedExecution> PreparedModel::createReusableExecution(
196         const nn::Request& request, nn::MeasureTiming measure,
197         const nn::OptionalDuration& loopTimeoutDuration) const {
198     // Ensure that request is ready for IPC.
199     std::optional<nn::Request> maybeRequestInShared;
200     hal::utils::RequestRelocation relocation;
201     const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
202             &request, nn::kDefaultRequestMemoryAlignment, nn::kDefaultRequestMemoryPadding,
203             &maybeRequestInShared, &relocation));
204 
205     auto aidlRequest = NN_TRY(convert(requestInShared));
206     auto aidlMeasure = NN_TRY(convert(measure));
207     auto aidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
208     return Execution::create(shared_from_this(), std::move(aidlRequest), std::move(relocation),
209                              aidlMeasure, aidlLoopTimeoutDuration);
210 }
211 
configureExecutionBurst() const212 nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
213     std::shared_ptr<IBurst> burst;
214     const auto ret = kPreparedModel->configureExecutionBurst(&burst);
215     HANDLE_ASTATUS(ret) << "configureExecutionBurst failed";
216     return Burst::create(std::move(burst));
217 }
218 
getUnderlyingResource() const219 std::any PreparedModel::getUnderlyingResource() const {
220     std::shared_ptr<aidl_hal::IPreparedModel> resource = kPreparedModel;
221     return resource;
222 }
223 
224 }  // namespace aidl::android::hardware::neuralnetworks::utils
225