• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "PreparedModel.h"
18 
19 #include "Burst.h"
20 #include "BurstUtils.h"
21 #include "Callbacks.h"
22 #include "Conversions.h"
23 #include "Execution.h"
24 #include "Utils.h"
25 
26 #include <android/hardware/neuralnetworks/1.0/types.h>
27 #include <android/hardware/neuralnetworks/1.1/types.h>
28 #include <android/hardware/neuralnetworks/1.2/IPreparedModel.h>
29 #include <android/hardware/neuralnetworks/1.2/types.h>
30 #include <nnapi/IPreparedModel.h>
31 #include <nnapi/Result.h>
32 #include <nnapi/Types.h>
33 #include <nnapi/hal/1.0/Conversions.h>
34 #include <nnapi/hal/1.0/HandleError.h>
35 #include <nnapi/hal/1.0/ProtectCallback.h>
36 #include <nnapi/hal/CommonUtils.h>
37 
38 #include <chrono>
39 #include <memory>
40 #include <tuple>
41 #include <utility>
42 #include <vector>
43 
44 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
45 // lifetimes across processes and for protecting asynchronous calls across HIDL.
46 
47 namespace android::hardware::neuralnetworks::V1_2::utils {
48 
create(sp<V1_2::IPreparedModel> preparedModel,bool executeSynchronously)49 nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
50         sp<V1_2::IPreparedModel> preparedModel, bool executeSynchronously) {
51     if (preparedModel == nullptr) {
52         return NN_ERROR() << "V1_2::utils::PreparedModel::create must have non-null preparedModel";
53     }
54 
55     auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
56     return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously,
57                                                  std::move(preparedModel), std::move(deathHandler));
58 }
59 
PreparedModel(PrivateConstructorTag,bool executeSynchronously,sp<V1_2::IPreparedModel> preparedModel,hal::utils::DeathHandler deathHandler)60 PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously,
61                              sp<V1_2::IPreparedModel> preparedModel,
62                              hal::utils::DeathHandler deathHandler)
63     : kExecuteSynchronously(executeSynchronously),
64       kPreparedModel(std::move(preparedModel)),
65       kDeathHandler(std::move(deathHandler)) {}
66 
67 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
executeSynchronously(const V1_0::Request & request,MeasureTiming measure) const68 PreparedModel::executeSynchronously(const V1_0::Request& request, MeasureTiming measure) const {
69     auto cb = hal::utils::CallbackValue(executionCallback);
70 
71     const auto ret = kPreparedModel->executeSynchronously(request, measure, cb);
72     HANDLE_TRANSPORT_FAILURE(ret);
73 
74     return cb.take();
75 }
76 
77 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
executeAsynchronously(const V1_0::Request & request,MeasureTiming measure) const78 PreparedModel::executeAsynchronously(const V1_0::Request& request, MeasureTiming measure) const {
79     const auto cb = sp<ExecutionCallback>::make();
80     const auto scoped = kDeathHandler.protectCallback(cb.get());
81 
82     const auto ret = kPreparedModel->execute_1_2(request, measure, cb);
83     const auto status = HANDLE_TRANSPORT_FAILURE(ret);
84     if (status != V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
85         HANDLE_STATUS_HIDL(status) << "execution failed with " << toString(status);
86     }
87 
88     return cb->get();
89 }
90 
execute(const nn::Request & request,nn::MeasureTiming measure,const nn::OptionalTimePoint &,const nn::OptionalDuration &,const std::vector<nn::TokenValuePair> &,const std::vector<nn::ExtensionNameAndPrefix> &) const91 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
92         const nn::Request& request, nn::MeasureTiming measure,
93         const nn::OptionalTimePoint& /*deadline*/,
94         const nn::OptionalDuration& /*loopTimeoutDuration*/,
95         const std::vector<nn::TokenValuePair>& /*hints*/,
96         const std::vector<nn::ExtensionNameAndPrefix>& /*extensionNameToPrefix*/) const {
97     // Ensure that request is ready for IPC.
98     std::optional<nn::Request> maybeRequestInShared;
99     hal::utils::RequestRelocation relocation;
100     const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
101             &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
102             &maybeRequestInShared, &relocation));
103 
104     const auto hidlRequest = NN_TRY(convert(requestInShared));
105     const auto hidlMeasure = NN_TRY(convert(measure));
106 
107     return executeInternal(hidlRequest, hidlMeasure, relocation);
108 }
109 
110 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
executeInternal(const V1_0::Request & request,MeasureTiming measure,const hal::utils::RequestRelocation & relocation) const111 PreparedModel::executeInternal(const V1_0::Request& request, MeasureTiming measure,
112                                const hal::utils::RequestRelocation& relocation) const {
113     if (relocation.input) {
114         relocation.input->flush();
115     }
116 
117     auto result = kExecuteSynchronously ? executeSynchronously(request, measure)
118                                         : executeAsynchronously(request, measure);
119     auto [outputShapes, timing] = NN_TRY(std::move(result));
120 
121     if (relocation.output) {
122         relocation.output->flush();
123     }
124     return std::make_pair(std::move(outputShapes), timing);
125 }
126 
127 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
executeFenced(const nn::Request &,const std::vector<nn::SyncFence> &,nn::MeasureTiming,const nn::OptionalTimePoint &,const nn::OptionalDuration &,const nn::OptionalDuration &,const std::vector<nn::TokenValuePair> &,const std::vector<nn::ExtensionNameAndPrefix> &) const128 PreparedModel::executeFenced(
129         const nn::Request& /*request*/, const std::vector<nn::SyncFence>& /*waitFor*/,
130         nn::MeasureTiming /*measure*/, const nn::OptionalTimePoint& /*deadline*/,
131         const nn::OptionalDuration& /*loopTimeoutDuration*/,
132         const nn::OptionalDuration& /*timeoutDurationAfterFence*/,
133         const std::vector<nn::TokenValuePair>& /*hints*/,
134         const std::vector<nn::ExtensionNameAndPrefix>& /*extensionNameToPrefix*/) const {
135     return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
136            << "IPreparedModel::executeFenced is not supported on 1.2 HAL service";
137 }
138 
createReusableExecution(const nn::Request & request,nn::MeasureTiming measure,const nn::OptionalDuration &,const std::vector<nn::TokenValuePair> &,const std::vector<nn::ExtensionNameAndPrefix> &) const139 nn::GeneralResult<nn::SharedExecution> PreparedModel::createReusableExecution(
140         const nn::Request& request, nn::MeasureTiming measure,
141         const nn::OptionalDuration& /*loopTimeoutDuration*/,
142         const std::vector<nn::TokenValuePair>& /*hints*/,
143         const std::vector<nn::ExtensionNameAndPrefix>& /*extensionNameToPrefix*/) const {
144     // Ensure that request is ready for IPC.
145     std::optional<nn::Request> maybeRequestInShared;
146     hal::utils::RequestRelocation relocation;
147     const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
148             &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
149             &maybeRequestInShared, &relocation));
150 
151     auto hidlRequest = NN_TRY(convert(requestInShared));
152     auto hidlMeasure = NN_TRY(convert(measure));
153     return Execution::create(shared_from_this(), std::move(hidlRequest), std::move(relocation),
154                              hidlMeasure);
155 }
156 
configureExecutionBurst() const157 nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
158     const auto pollingTimeWindow = getBurstControllerPollingTimeWindow();
159     return Burst::create(shared_from_this(), kPreparedModel, pollingTimeWindow);
160 }
161 
getUnderlyingResource() const162 std::any PreparedModel::getUnderlyingResource() const {
163     sp<V1_2::IPreparedModel> resource = kPreparedModel;
164     return resource;
165 }
166 
167 }  // namespace android::hardware::neuralnetworks::V1_2::utils
168