blob: 6c7aa882e8884e40230441760fc4f7e8fe7b46a2 [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>
Michael Butler7a9d6092021-03-10 21:57:13 -080029
30#include <memory>
31#include <mutex>
32#include <optional>
33#include <utility>
34
35namespace aidl::android::hardware::neuralnetworks::utils {
36namespace {
37
Xusong Wangb2e80852021-03-23 15:07:10 -070038class BurstExecution final : public nn::IExecution,
39 public std::enable_shared_from_this<BurstExecution> {
40 struct PrivateConstructorTag {};
41
42 public:
43 static nn::GeneralResult<std::shared_ptr<const BurstExecution>> create(
44 std::shared_ptr<const Burst> burst, Request request,
45 std::vector<int64_t> memoryIdentifierTokens, bool measure, int64_t loopTimeoutDuration,
Miao Wangb5c8a822021-10-26 20:03:05 +000046 const std::vector<nn::TokenValuePair>& hints,
47 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix,
Xusong Wangb2e80852021-03-23 15:07:10 -070048 hal::utils::RequestRelocation relocation,
49 std::vector<Burst::OptionalCacheHold> cacheHolds);
50
51 BurstExecution(PrivateConstructorTag tag, std::shared_ptr<const Burst> burst, Request request,
52 std::vector<int64_t> memoryIdentifierTokens, bool measure,
Miao Wangb5c8a822021-10-26 20:03:05 +000053 int64_t loopTimeoutDuration, const std::vector<nn::TokenValuePair>& hints,
54 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix,
55 hal::utils::RequestRelocation relocation,
Xusong Wangb2e80852021-03-23 15:07:10 -070056 std::vector<Burst::OptionalCacheHold> cacheHolds);
57
58 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> compute(
59 const nn::OptionalTimePoint& deadline) const override;
60
61 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> computeFenced(
62 const std::vector<nn::SyncFence>& waitFor, const nn::OptionalTimePoint& deadline,
63 const nn::OptionalDuration& timeoutDurationAfterFence) const override;
64
65 private:
66 const std::shared_ptr<const Burst> kBurst;
67 const Request kRequest;
Xusong Wang5e045952021-05-18 13:54:11 -070068 const std::vector<int64_t> kMemoryIdentifierTokens;
Xusong Wangb2e80852021-03-23 15:07:10 -070069 const bool kMeasure;
70 const int64_t kLoopTimeoutDuration;
Miao Wangb5c8a822021-10-26 20:03:05 +000071 const std::vector<nn::TokenValuePair> kHints;
72 const std::vector<nn::ExtensionNameAndPrefix> kExtensionNameToPrefix;
Xusong Wangb2e80852021-03-23 15:07:10 -070073 const hal::utils::RequestRelocation kRelocation;
74 const std::vector<Burst::OptionalCacheHold> kCacheHolds;
75};
76
Michael Butler7a9d6092021-03-10 21:57:13 -080077nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> convertExecutionResults(
78 const std::vector<OutputShape>& outputShapes, const Timing& timing) {
79 return std::make_pair(NN_TRY(nn::convert(outputShapes)), NN_TRY(nn::convert(timing)));
80}
81
82} // namespace
83
84Burst::MemoryCache::MemoryCache(std::shared_ptr<aidl_hal::IBurst> burst)
85 : kBurst(std::move(burst)) {}
86
87std::pair<int64_t, Burst::MemoryCache::SharedCleanup> Burst::MemoryCache::getOrCacheMemory(
88 const nn::SharedMemory& memory) {
89 std::lock_guard lock(mMutex);
90
91 // Get the cache payload or create it (with default values) if it does not exist.
92 auto& cachedPayload = mCache[memory];
93 {
94 const auto& [identifier, maybeCleaner] = cachedPayload;
95 // If cache payload already exists, reuse it.
96 if (auto cleaner = maybeCleaner.lock()) {
97 return std::make_pair(identifier, std::move(cleaner));
98 }
99 }
100
101 // If the code reaches this point, the cached payload either did not exist or expired prior to
102 // this call.
103
104 // Allocate a new identifier.
105 CHECK_LT(mUnusedIdentifier, std::numeric_limits<int64_t>::max());
106 const int64_t identifier = mUnusedIdentifier++;
107
108 // Create reference-counted self-cleaning cache object.
109 auto self = weak_from_this();
110 Task cleanup = [memory, identifier, maybeMemoryCache = std::move(self)] {
111 if (const auto memoryCache = maybeMemoryCache.lock()) {
112 memoryCache->tryFreeMemory(memory, identifier);
113 }
114 };
115 auto cleaner = std::make_shared<const Cleanup>(std::move(cleanup));
116
117 // Store the result in the cache and return it.
118 auto result = std::make_pair(identifier, std::move(cleaner));
119 cachedPayload = result;
120 return result;
121}
122
123std::optional<std::pair<int64_t, Burst::MemoryCache::SharedCleanup>>
124Burst::MemoryCache::getMemoryIfAvailable(const nn::SharedMemory& memory) {
125 std::lock_guard lock(mMutex);
126
127 // Get the existing cached entry if it exists.
128 const auto iter = mCache.find(memory);
129 if (iter != mCache.end()) {
130 const auto& [identifier, maybeCleaner] = iter->second;
131 if (auto cleaner = maybeCleaner.lock()) {
132 return std::make_pair(identifier, std::move(cleaner));
133 }
134 }
135
136 // If the code reaches this point, the cached payload did not exist or was actively being
137 // deleted.
138 return std::nullopt;
139}
140
141void Burst::MemoryCache::tryFreeMemory(const nn::SharedMemory& memory, int64_t identifier) {
142 {
143 std::lock_guard guard(mMutex);
144 // Remove the cached memory and payload if it is present but expired. Note that it may not
145 // be present or may not be expired because another thread may have removed or cached the
146 // same memory object before the current thread locked mMutex in tryFreeMemory.
147 const auto iter = mCache.find(memory);
148 if (iter != mCache.end()) {
149 if (std::get<WeakCleanup>(iter->second).expired()) {
150 mCache.erase(iter);
151 }
152 }
153 }
154 kBurst->releaseMemoryResource(identifier);
155}
156
157nn::GeneralResult<std::shared_ptr<const Burst>> Burst::create(
Miao Wangb5c8a822021-10-26 20:03:05 +0000158 std::shared_ptr<aidl_hal::IBurst> burst, nn::Version featureLevel) {
Michael Butler7a9d6092021-03-10 21:57:13 -0800159 if (burst == nullptr) {
160 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
161 << "aidl_hal::utils::Burst::create must have non-null burst";
162 }
163
Miao Wangb5c8a822021-10-26 20:03:05 +0000164 return std::make_shared<const Burst>(PrivateConstructorTag{}, std::move(burst), featureLevel);
Michael Butler7a9d6092021-03-10 21:57:13 -0800165}
166
Miao Wangb5c8a822021-10-26 20:03:05 +0000167Burst::Burst(PrivateConstructorTag /*tag*/, std::shared_ptr<aidl_hal::IBurst> burst,
168 nn::Version featureLevel)
169 : kBurst(std::move(burst)),
170 kMemoryCache(std::make_shared<MemoryCache>(kBurst)),
171 kFeatureLevel(featureLevel) {
Michael Butler7a9d6092021-03-10 21:57:13 -0800172 CHECK(kBurst != nullptr);
173}
174
175Burst::OptionalCacheHold Burst::cacheMemory(const nn::SharedMemory& memory) const {
176 auto [identifier, hold] = kMemoryCache->getOrCacheMemory(memory);
177 return hold;
178}
179
180nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Burst::execute(
181 const nn::Request& request, nn::MeasureTiming measure,
Miao Wangb5c8a822021-10-26 20:03:05 +0000182 const nn::OptionalTimePoint& deadline, const nn::OptionalDuration& loopTimeoutDuration,
183 const std::vector<nn::TokenValuePair>& hints,
184 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const {
Michael Butler7a9d6092021-03-10 21:57:13 -0800185 // Ensure that request is ready for IPC.
186 std::optional<nn::Request> maybeRequestInShared;
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800187 hal::utils::RequestRelocation relocation;
Michael Butlerff9a5a52021-10-15 16:23:20 -0700188 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
189 &request, nn::kDefaultRequestMemoryAlignment, nn::kDefaultRequestMemoryPadding,
190 &maybeRequestInShared, &relocation));
Michael Butler7a9d6092021-03-10 21:57:13 -0800191
Michael Butlerff9a5a52021-10-15 16:23:20 -0700192 const auto aidlRequest = NN_TRY(convert(requestInShared));
193 const auto aidlMeasure = NN_TRY(convert(measure));
194 const auto aidlDeadline = NN_TRY(convert(deadline));
195 const auto aidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
Michael Butler7a9d6092021-03-10 21:57:13 -0800196
197 std::vector<int64_t> memoryIdentifierTokens;
198 std::vector<OptionalCacheHold> holds;
Xusong Wangb2e80852021-03-23 15:07:10 -0700199 memoryIdentifierTokens.reserve(requestInShared.pools.size());
200 holds.reserve(requestInShared.pools.size());
201 for (const auto& memoryPool : requestInShared.pools) {
Michael Butler7a9d6092021-03-10 21:57:13 -0800202 if (const auto* memory = std::get_if<nn::SharedMemory>(&memoryPool)) {
203 if (auto cached = kMemoryCache->getMemoryIfAvailable(*memory)) {
204 auto& [identifier, hold] = *cached;
205 memoryIdentifierTokens.push_back(identifier);
206 holds.push_back(std::move(hold));
207 continue;
208 }
209 }
210 memoryIdentifierTokens.push_back(-1);
211 }
Xusong Wangb2e80852021-03-23 15:07:10 -0700212 CHECK_EQ(requestInShared.pools.size(), memoryIdentifierTokens.size());
Xusong Wangb2e80852021-03-23 15:07:10 -0700213 return executeInternal(aidlRequest, memoryIdentifierTokens, aidlMeasure, aidlDeadline,
Miao Wangb5c8a822021-10-26 20:03:05 +0000214 aidlLoopTimeoutDuration, hints, extensionNameToPrefix, relocation);
Xusong Wangb2e80852021-03-23 15:07:10 -0700215}
216
217nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Burst::executeInternal(
218 const Request& request, const std::vector<int64_t>& memoryIdentifierTokens, bool measure,
Miao Wangb5c8a822021-10-26 20:03:05 +0000219 int64_t deadline, int64_t loopTimeoutDuration, const std::vector<nn::TokenValuePair>& hints,
220 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix,
Xusong Wangb2e80852021-03-23 15:07:10 -0700221 const hal::utils::RequestRelocation& relocation) const {
222 // Ensure that at most one execution is in flight at any given time.
223 const bool alreadyInFlight = mExecutionInFlight.test_and_set();
224 if (alreadyInFlight) {
225 return NN_ERROR() << "IBurst already has an execution in flight";
226 }
227 const auto guard = ::android::base::make_scope_guard([this] { mExecutionInFlight.clear(); });
Michael Butler7a9d6092021-03-10 21:57:13 -0800228
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800229 if (relocation.input) {
230 relocation.input->flush();
231 }
232
Michael Butler7a9d6092021-03-10 21:57:13 -0800233 ExecutionResult executionResult;
Miao Wangb5c8a822021-10-26 20:03:05 +0000234 if (kFeatureLevel.level >= nn::Version::Level::FEATURE_LEVEL_8) {
235 auto aidlHints = NN_TRY(convert(hints));
236 auto aidlExtensionPrefix = NN_TRY(convert(extensionNameToPrefix));
237 const auto ret = kBurst->executeSynchronouslyWithConfig(
238 request, memoryIdentifierTokens,
239 {measure, loopTimeoutDuration, std::move(aidlHints),
240 std::move(aidlExtensionPrefix)},
241 deadline, &executionResult);
242 HANDLE_ASTATUS(ret) << "execute failed";
243 } else {
244 const auto ret =
245 kBurst->executeSynchronously(request, memoryIdentifierTokens, measure, deadline,
246 loopTimeoutDuration, &executionResult);
247 HANDLE_ASTATUS(ret) << "execute failed";
248 }
Michael Butler7a9d6092021-03-10 21:57:13 -0800249 if (!executionResult.outputSufficientSize) {
250 auto canonicalOutputShapes =
251 nn::convert(executionResult.outputShapes).value_or(std::vector<nn::OutputShape>{});
252 return NN_ERROR(nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, std::move(canonicalOutputShapes))
253 << "execution failed with " << nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE;
254 }
Michael Butlerff9a5a52021-10-15 16:23:20 -0700255 auto [outputShapes, timing] =
256 NN_TRY(convertExecutionResults(executionResult.outputShapes, executionResult.timing));
Michael Butler7a9d6092021-03-10 21:57:13 -0800257
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800258 if (relocation.output) {
259 relocation.output->flush();
260 }
Michael Butler7a9d6092021-03-10 21:57:13 -0800261 return std::make_pair(std::move(outputShapes), timing);
262}
263
Xusong Wangb2e80852021-03-23 15:07:10 -0700264nn::GeneralResult<nn::SharedExecution> Burst::createReusableExecution(
265 const nn::Request& request, nn::MeasureTiming measure,
Miao Wangb5c8a822021-10-26 20:03:05 +0000266 const nn::OptionalDuration& loopTimeoutDuration,
267 const std::vector<nn::TokenValuePair>& hints,
268 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const {
Xusong Wangb2e80852021-03-23 15:07:10 -0700269 // Ensure that request is ready for IPC.
270 std::optional<nn::Request> maybeRequestInShared;
271 hal::utils::RequestRelocation relocation;
272 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
Xusong Wange3d0dad2021-05-07 14:13:22 -0700273 &request, nn::kDefaultRequestMemoryAlignment, nn::kDefaultRequestMemoryPadding,
274 &maybeRequestInShared, &relocation));
Xusong Wangb2e80852021-03-23 15:07:10 -0700275
276 auto aidlRequest = NN_TRY(convert(requestInShared));
277 const auto aidlMeasure = NN_TRY(convert(measure));
278 const auto aidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
279
280 std::vector<int64_t> memoryIdentifierTokens;
281 std::vector<OptionalCacheHold> holds;
282 memoryIdentifierTokens.reserve(requestInShared.pools.size());
283 holds.reserve(requestInShared.pools.size());
284 for (const auto& memoryPool : requestInShared.pools) {
285 if (const auto* memory = std::get_if<nn::SharedMemory>(&memoryPool)) {
286 if (auto cached = kMemoryCache->getMemoryIfAvailable(*memory)) {
287 auto& [identifier, hold] = *cached;
288 memoryIdentifierTokens.push_back(identifier);
289 holds.push_back(std::move(hold));
290 continue;
291 }
292 }
293 memoryIdentifierTokens.push_back(-1);
294 }
295 CHECK_EQ(requestInShared.pools.size(), memoryIdentifierTokens.size());
296
297 return BurstExecution::create(shared_from_this(), std::move(aidlRequest),
298 std::move(memoryIdentifierTokens), aidlMeasure,
Miao Wangb5c8a822021-10-26 20:03:05 +0000299 aidlLoopTimeoutDuration, hints, extensionNameToPrefix,
300 std::move(relocation), std::move(holds));
Xusong Wangb2e80852021-03-23 15:07:10 -0700301}
302
303nn::GeneralResult<std::shared_ptr<const BurstExecution>> BurstExecution::create(
304 std::shared_ptr<const Burst> burst, Request request,
305 std::vector<int64_t> memoryIdentifierTokens, bool measure, int64_t loopTimeoutDuration,
Miao Wangb5c8a822021-10-26 20:03:05 +0000306 const std::vector<nn::TokenValuePair>& hints,
307 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix,
Xusong Wangb2e80852021-03-23 15:07:10 -0700308 hal::utils::RequestRelocation relocation,
309 std::vector<Burst::OptionalCacheHold> cacheHolds) {
310 if (burst == nullptr) {
311 return NN_ERROR() << "aidl::utils::BurstExecution::create must have non-null burst";
312 }
313
314 return std::make_shared<const BurstExecution>(
315 PrivateConstructorTag{}, std::move(burst), std::move(request),
Miao Wangb5c8a822021-10-26 20:03:05 +0000316 std::move(memoryIdentifierTokens), measure, loopTimeoutDuration, hints,
317 extensionNameToPrefix, std::move(relocation), std::move(cacheHolds));
Xusong Wangb2e80852021-03-23 15:07:10 -0700318}
319
320BurstExecution::BurstExecution(PrivateConstructorTag /*tag*/, std::shared_ptr<const Burst> burst,
321 Request request, std::vector<int64_t> memoryIdentifierTokens,
322 bool measure, int64_t loopTimeoutDuration,
Miao Wangb5c8a822021-10-26 20:03:05 +0000323 const std::vector<nn::TokenValuePair>& hints,
324 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix,
Xusong Wangb2e80852021-03-23 15:07:10 -0700325 hal::utils::RequestRelocation relocation,
326 std::vector<Burst::OptionalCacheHold> cacheHolds)
327 : kBurst(std::move(burst)),
328 kRequest(std::move(request)),
329 kMemoryIdentifierTokens(std::move(memoryIdentifierTokens)),
330 kMeasure(measure),
331 kLoopTimeoutDuration(loopTimeoutDuration),
Miao Wangb5c8a822021-10-26 20:03:05 +0000332 kHints(hints),
333 kExtensionNameToPrefix(extensionNameToPrefix),
Xusong Wangb2e80852021-03-23 15:07:10 -0700334 kRelocation(std::move(relocation)),
335 kCacheHolds(std::move(cacheHolds)) {}
336
337nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> BurstExecution::compute(
338 const nn::OptionalTimePoint& deadline) const {
Michael Butlerff9a5a52021-10-15 16:23:20 -0700339 const auto aidlDeadline = NN_TRY(convert(deadline));
Xusong Wangb2e80852021-03-23 15:07:10 -0700340 return kBurst->executeInternal(kRequest, kMemoryIdentifierTokens, kMeasure, aidlDeadline,
Miao Wangb5c8a822021-10-26 20:03:05 +0000341 kLoopTimeoutDuration, kHints, kExtensionNameToPrefix,
342 kRelocation);
Xusong Wangb2e80852021-03-23 15:07:10 -0700343}
344
345nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
346BurstExecution::computeFenced(const std::vector<nn::SyncFence>& /*waitFor*/,
347 const nn::OptionalTimePoint& /*deadline*/,
348 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
349 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
350 << "IExecution::computeFenced is not supported on burst object";
351}
352
Michael Butler7a9d6092021-03-10 21:57:13 -0800353} // namespace aidl::android::hardware::neuralnetworks::utils