blob: 3cbba4d643246736738a7eb8e84bd0947cefc2c3 [file] [log] [blame]
Michael Butler7a9d6092021-03-10 21:57:13 -08001/*
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 "Burst.h"
18
19#include "Conversions.h"
20#include "Utils.h"
21
22#include <android-base/logging.h>
23#include <android/binder_auto_utils.h>
24#include <nnapi/IBurst.h>
Xusong Wangb2e80852021-03-23 15:07:10 -070025#include <nnapi/IExecution.h>
Michael Butler7a9d6092021-03-10 21:57:13 -080026#include <nnapi/Result.h>
27#include <nnapi/TypeUtils.h>
28#include <nnapi/Types.h>
29#include <nnapi/hal/HandleError.h>
30
31#include <memory>
32#include <mutex>
33#include <optional>
34#include <utility>
35
36namespace aidl::android::hardware::neuralnetworks::utils {
37namespace {
38
Xusong Wangb2e80852021-03-23 15:07:10 -070039class BurstExecution final : public nn::IExecution,
40 public std::enable_shared_from_this<BurstExecution> {
41 struct PrivateConstructorTag {};
42
43 public:
44 static nn::GeneralResult<std::shared_ptr<const BurstExecution>> create(
45 std::shared_ptr<const Burst> burst, Request request,
46 std::vector<int64_t> memoryIdentifierTokens, bool measure, int64_t loopTimeoutDuration,
47 hal::utils::RequestRelocation relocation,
48 std::vector<Burst::OptionalCacheHold> cacheHolds);
49
50 BurstExecution(PrivateConstructorTag tag, std::shared_ptr<const Burst> burst, Request request,
51 std::vector<int64_t> memoryIdentifierTokens, bool measure,
52 int64_t loopTimeoutDuration, hal::utils::RequestRelocation relocation,
53 std::vector<Burst::OptionalCacheHold> cacheHolds);
54
55 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> compute(
56 const nn::OptionalTimePoint& deadline) const override;
57
58 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> computeFenced(
59 const std::vector<nn::SyncFence>& waitFor, const nn::OptionalTimePoint& deadline,
60 const nn::OptionalDuration& timeoutDurationAfterFence) const override;
61
62 private:
63 const std::shared_ptr<const Burst> kBurst;
64 const Request kRequest;
65 const std::vector<int64_t>& kMemoryIdentifierTokens;
66 const bool kMeasure;
67 const int64_t kLoopTimeoutDuration;
68 const hal::utils::RequestRelocation kRelocation;
69 const std::vector<Burst::OptionalCacheHold> kCacheHolds;
70};
71
Michael Butler7a9d6092021-03-10 21:57:13 -080072nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> convertExecutionResults(
73 const std::vector<OutputShape>& outputShapes, const Timing& timing) {
74 return std::make_pair(NN_TRY(nn::convert(outputShapes)), NN_TRY(nn::convert(timing)));
75}
76
77} // namespace
78
79Burst::MemoryCache::MemoryCache(std::shared_ptr<aidl_hal::IBurst> burst)
80 : kBurst(std::move(burst)) {}
81
82std::pair<int64_t, Burst::MemoryCache::SharedCleanup> Burst::MemoryCache::getOrCacheMemory(
83 const nn::SharedMemory& memory) {
84 std::lock_guard lock(mMutex);
85
86 // Get the cache payload or create it (with default values) if it does not exist.
87 auto& cachedPayload = mCache[memory];
88 {
89 const auto& [identifier, maybeCleaner] = cachedPayload;
90 // If cache payload already exists, reuse it.
91 if (auto cleaner = maybeCleaner.lock()) {
92 return std::make_pair(identifier, std::move(cleaner));
93 }
94 }
95
96 // If the code reaches this point, the cached payload either did not exist or expired prior to
97 // this call.
98
99 // Allocate a new identifier.
100 CHECK_LT(mUnusedIdentifier, std::numeric_limits<int64_t>::max());
101 const int64_t identifier = mUnusedIdentifier++;
102
103 // Create reference-counted self-cleaning cache object.
104 auto self = weak_from_this();
105 Task cleanup = [memory, identifier, maybeMemoryCache = std::move(self)] {
106 if (const auto memoryCache = maybeMemoryCache.lock()) {
107 memoryCache->tryFreeMemory(memory, identifier);
108 }
109 };
110 auto cleaner = std::make_shared<const Cleanup>(std::move(cleanup));
111
112 // Store the result in the cache and return it.
113 auto result = std::make_pair(identifier, std::move(cleaner));
114 cachedPayload = result;
115 return result;
116}
117
118std::optional<std::pair<int64_t, Burst::MemoryCache::SharedCleanup>>
119Burst::MemoryCache::getMemoryIfAvailable(const nn::SharedMemory& memory) {
120 std::lock_guard lock(mMutex);
121
122 // Get the existing cached entry if it exists.
123 const auto iter = mCache.find(memory);
124 if (iter != mCache.end()) {
125 const auto& [identifier, maybeCleaner] = iter->second;
126 if (auto cleaner = maybeCleaner.lock()) {
127 return std::make_pair(identifier, std::move(cleaner));
128 }
129 }
130
131 // If the code reaches this point, the cached payload did not exist or was actively being
132 // deleted.
133 return std::nullopt;
134}
135
136void Burst::MemoryCache::tryFreeMemory(const nn::SharedMemory& memory, int64_t identifier) {
137 {
138 std::lock_guard guard(mMutex);
139 // Remove the cached memory and payload if it is present but expired. Note that it may not
140 // be present or may not be expired because another thread may have removed or cached the
141 // same memory object before the current thread locked mMutex in tryFreeMemory.
142 const auto iter = mCache.find(memory);
143 if (iter != mCache.end()) {
144 if (std::get<WeakCleanup>(iter->second).expired()) {
145 mCache.erase(iter);
146 }
147 }
148 }
149 kBurst->releaseMemoryResource(identifier);
150}
151
152nn::GeneralResult<std::shared_ptr<const Burst>> Burst::create(
153 std::shared_ptr<aidl_hal::IBurst> burst) {
154 if (burst == nullptr) {
155 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
156 << "aidl_hal::utils::Burst::create must have non-null burst";
157 }
158
159 return std::make_shared<const Burst>(PrivateConstructorTag{}, std::move(burst));
160}
161
162Burst::Burst(PrivateConstructorTag /*tag*/, std::shared_ptr<aidl_hal::IBurst> burst)
163 : kBurst(std::move(burst)), kMemoryCache(std::make_shared<MemoryCache>(kBurst)) {
164 CHECK(kBurst != nullptr);
165}
166
167Burst::OptionalCacheHold Burst::cacheMemory(const nn::SharedMemory& memory) const {
168 auto [identifier, hold] = kMemoryCache->getOrCacheMemory(memory);
169 return hold;
170}
171
172nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Burst::execute(
173 const nn::Request& request, nn::MeasureTiming measure,
174 const nn::OptionalTimePoint& deadline,
175 const nn::OptionalDuration& loopTimeoutDuration) const {
Michael Butler7a9d6092021-03-10 21:57:13 -0800176 // Ensure that request is ready for IPC.
177 std::optional<nn::Request> maybeRequestInShared;
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800178 hal::utils::RequestRelocation relocation;
179 const nn::Request& requestInShared =
180 NN_TRY(hal::utils::makeExecutionFailure(hal::utils::convertRequestFromPointerToShared(
181 &request, &maybeRequestInShared, &relocation)));
Michael Butler7a9d6092021-03-10 21:57:13 -0800182
183 const auto aidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
184 const auto aidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure)));
185 const auto aidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline)));
186 const auto aidlLoopTimeoutDuration =
187 NN_TRY(hal::utils::makeExecutionFailure(convert(loopTimeoutDuration)));
188
189 std::vector<int64_t> memoryIdentifierTokens;
190 std::vector<OptionalCacheHold> holds;
Xusong Wangb2e80852021-03-23 15:07:10 -0700191 memoryIdentifierTokens.reserve(requestInShared.pools.size());
192 holds.reserve(requestInShared.pools.size());
193 for (const auto& memoryPool : requestInShared.pools) {
Michael Butler7a9d6092021-03-10 21:57:13 -0800194 if (const auto* memory = std::get_if<nn::SharedMemory>(&memoryPool)) {
195 if (auto cached = kMemoryCache->getMemoryIfAvailable(*memory)) {
196 auto& [identifier, hold] = *cached;
197 memoryIdentifierTokens.push_back(identifier);
198 holds.push_back(std::move(hold));
199 continue;
200 }
201 }
202 memoryIdentifierTokens.push_back(-1);
203 }
Xusong Wangb2e80852021-03-23 15:07:10 -0700204 CHECK_EQ(requestInShared.pools.size(), memoryIdentifierTokens.size());
205
206 return executeInternal(aidlRequest, memoryIdentifierTokens, aidlMeasure, aidlDeadline,
207 aidlLoopTimeoutDuration, relocation);
208}
209
210nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Burst::executeInternal(
211 const Request& request, const std::vector<int64_t>& memoryIdentifierTokens, bool measure,
212 int64_t deadline, int64_t loopTimeoutDuration,
213 const hal::utils::RequestRelocation& relocation) const {
214 // Ensure that at most one execution is in flight at any given time.
215 const bool alreadyInFlight = mExecutionInFlight.test_and_set();
216 if (alreadyInFlight) {
217 return NN_ERROR() << "IBurst already has an execution in flight";
218 }
219 const auto guard = ::android::base::make_scope_guard([this] { mExecutionInFlight.clear(); });
Michael Butler7a9d6092021-03-10 21:57:13 -0800220
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800221 if (relocation.input) {
222 relocation.input->flush();
223 }
224
Michael Butler7a9d6092021-03-10 21:57:13 -0800225 ExecutionResult executionResult;
Xusong Wangb2e80852021-03-23 15:07:10 -0700226 const auto ret = kBurst->executeSynchronously(request, memoryIdentifierTokens, measure,
227 deadline, loopTimeoutDuration, &executionResult);
Michael Butler7a9d6092021-03-10 21:57:13 -0800228 HANDLE_ASTATUS(ret) << "execute failed";
229 if (!executionResult.outputSufficientSize) {
230 auto canonicalOutputShapes =
231 nn::convert(executionResult.outputShapes).value_or(std::vector<nn::OutputShape>{});
232 return NN_ERROR(nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, std::move(canonicalOutputShapes))
233 << "execution failed with " << nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE;
234 }
235 auto [outputShapes, timing] = NN_TRY(hal::utils::makeExecutionFailure(
236 convertExecutionResults(executionResult.outputShapes, executionResult.timing)));
237
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800238 if (relocation.output) {
239 relocation.output->flush();
240 }
Michael Butler7a9d6092021-03-10 21:57:13 -0800241 return std::make_pair(std::move(outputShapes), timing);
242}
243
Xusong Wangb2e80852021-03-23 15:07:10 -0700244nn::GeneralResult<nn::SharedExecution> Burst::createReusableExecution(
245 const nn::Request& request, nn::MeasureTiming measure,
246 const nn::OptionalDuration& loopTimeoutDuration) const {
247 // Ensure that request is ready for IPC.
248 std::optional<nn::Request> maybeRequestInShared;
249 hal::utils::RequestRelocation relocation;
250 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
251 &request, &maybeRequestInShared, &relocation));
252
253 auto aidlRequest = NN_TRY(convert(requestInShared));
254 const auto aidlMeasure = NN_TRY(convert(measure));
255 const auto aidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
256
257 std::vector<int64_t> memoryIdentifierTokens;
258 std::vector<OptionalCacheHold> holds;
259 memoryIdentifierTokens.reserve(requestInShared.pools.size());
260 holds.reserve(requestInShared.pools.size());
261 for (const auto& memoryPool : requestInShared.pools) {
262 if (const auto* memory = std::get_if<nn::SharedMemory>(&memoryPool)) {
263 if (auto cached = kMemoryCache->getMemoryIfAvailable(*memory)) {
264 auto& [identifier, hold] = *cached;
265 memoryIdentifierTokens.push_back(identifier);
266 holds.push_back(std::move(hold));
267 continue;
268 }
269 }
270 memoryIdentifierTokens.push_back(-1);
271 }
272 CHECK_EQ(requestInShared.pools.size(), memoryIdentifierTokens.size());
273
274 return BurstExecution::create(shared_from_this(), std::move(aidlRequest),
275 std::move(memoryIdentifierTokens), aidlMeasure,
276 aidlLoopTimeoutDuration, std::move(relocation), std::move(holds));
277}
278
279nn::GeneralResult<std::shared_ptr<const BurstExecution>> BurstExecution::create(
280 std::shared_ptr<const Burst> burst, Request request,
281 std::vector<int64_t> memoryIdentifierTokens, bool measure, int64_t loopTimeoutDuration,
282 hal::utils::RequestRelocation relocation,
283 std::vector<Burst::OptionalCacheHold> cacheHolds) {
284 if (burst == nullptr) {
285 return NN_ERROR() << "aidl::utils::BurstExecution::create must have non-null burst";
286 }
287
288 return std::make_shared<const BurstExecution>(
289 PrivateConstructorTag{}, std::move(burst), std::move(request),
290 std::move(memoryIdentifierTokens), measure, loopTimeoutDuration, std::move(relocation),
291 std::move(cacheHolds));
292}
293
294BurstExecution::BurstExecution(PrivateConstructorTag /*tag*/, std::shared_ptr<const Burst> burst,
295 Request request, std::vector<int64_t> memoryIdentifierTokens,
296 bool measure, int64_t loopTimeoutDuration,
297 hal::utils::RequestRelocation relocation,
298 std::vector<Burst::OptionalCacheHold> cacheHolds)
299 : kBurst(std::move(burst)),
300 kRequest(std::move(request)),
301 kMemoryIdentifierTokens(std::move(memoryIdentifierTokens)),
302 kMeasure(measure),
303 kLoopTimeoutDuration(loopTimeoutDuration),
304 kRelocation(std::move(relocation)),
305 kCacheHolds(std::move(cacheHolds)) {}
306
307nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> BurstExecution::compute(
308 const nn::OptionalTimePoint& deadline) const {
309 const auto aidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline)));
310 return kBurst->executeInternal(kRequest, kMemoryIdentifierTokens, kMeasure, aidlDeadline,
311 kLoopTimeoutDuration, kRelocation);
312}
313
314nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
315BurstExecution::computeFenced(const std::vector<nn::SyncFence>& /*waitFor*/,
316 const nn::OptionalTimePoint& /*deadline*/,
317 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
318 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
319 << "IExecution::computeFenced is not supported on burst object";
320}
321
Michael Butler7a9d6092021-03-10 21:57:13 -0800322} // namespace aidl::android::hardware::neuralnetworks::utils