blob: 8b2e1dd46529b62e826164be5425e299517e36ec [file] [log] [blame]
Michael Butlerf6b2d1a2020-12-19 14:44:35 -08001/*
2 * Copyright (C) 2019 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
Michael Butler137ee992021-11-01 16:40:31 -070017#include "Burst.h"
Michael Butlerf6b2d1a2020-12-19 14:44:35 -080018
19#include <android-base/logging.h>
Michael Butler76e491f2020-12-19 01:55:32 -080020#include <nnapi/IBurst.h>
21#include <nnapi/Result.h>
22#include <nnapi/TypeUtils.h>
23#include <nnapi/Types.h>
24#include <nnapi/Validation.h>
25#include <nnapi/hal/1.0/Conversions.h>
Michael Butler49d95e02021-10-15 18:52:52 -070026#include <nnapi/hal/1.0/HandleError.h>
Michael Butlere8645c32021-10-15 18:42:32 -070027#include <nnapi/hal/1.0/ProtectCallback.h>
Michael Butler137ee992021-11-01 16:40:31 -070028#include <nnapi/hal/1.2/BurstUtils.h>
29#include <nnapi/hal/1.2/Conversions.h>
Michael Butler76e491f2020-12-19 01:55:32 -080030#include <nnapi/hal/TransferValue.h>
Michael Butlerf6b2d1a2020-12-19 14:44:35 -080031
32#include <algorithm>
33#include <cstring>
34#include <limits>
35#include <map>
36#include <memory>
37#include <tuple>
38#include <utility>
39#include <vector>
40
Michael Butlerf6b2d1a2020-12-19 14:44:35 -080041#include "Tracing.h"
42
Michael Butler137ee992021-11-01 16:40:31 -070043namespace android::hardware::neuralnetworks::adapter {
Michael Butlerf6b2d1a2020-12-19 14:44:35 -080044namespace {
45
Michael Butler137ee992021-11-01 16:40:31 -070046constexpr V1_2::Timing kTiming = {std::numeric_limits<uint64_t>::max(),
47 std::numeric_limits<uint64_t>::max()};
Michael Butler76e491f2020-12-19 01:55:32 -080048
49nn::GeneralResult<std::vector<nn::SharedMemory>> getMemoriesCallback(
50 V1_0::ErrorStatus status, const hidl_vec<hidl_memory>& memories) {
Michael Butler49d95e02021-10-15 18:52:52 -070051 HANDLE_STATUS_HIDL(status) << "getting burst memories failed with " << toString(status);
Michael Butler76e491f2020-12-19 01:55:32 -080052 std::vector<nn::SharedMemory> canonicalMemories;
53 canonicalMemories.reserve(memories.size());
54 for (const auto& memory : memories) {
55 canonicalMemories.push_back(NN_TRY(nn::convert(memory)));
Michael Butlerf6b2d1a2020-12-19 14:44:35 -080056 }
Michael Butler76e491f2020-12-19 01:55:32 -080057 return canonicalMemories;
58}
Michael Butlerf6b2d1a2020-12-19 14:44:35 -080059
60} // anonymous namespace
61
Michael Butler137ee992021-11-01 16:40:31 -070062Burst::MemoryCache::MemoryCache(nn::SharedBurst burstExecutor,
63 sp<V1_2::IBurstCallback> burstCallback)
Michael Butler76e491f2020-12-19 01:55:32 -080064 : kBurstExecutor(std::move(burstExecutor)), kBurstCallback(std::move(burstCallback)) {
65 CHECK(kBurstExecutor != nullptr);
66 CHECK(kBurstCallback != nullptr);
67}
68
69nn::GeneralResult<std::vector<std::pair<nn::SharedMemory, nn::IBurst::OptionalCacheHold>>>
Michael Butler137ee992021-11-01 16:40:31 -070070Burst::MemoryCache::getCacheEntries(const std::vector<int32_t>& slots) {
Michael Butler76e491f2020-12-19 01:55:32 -080071 std::lock_guard guard(mMutex);
72 NN_TRY(ensureCacheEntriesArePresentLocked(slots));
73
74 std::vector<std::pair<nn::SharedMemory, nn::IBurst::OptionalCacheHold>> results;
75 results.reserve(slots.size());
76 for (int32_t slot : slots) {
77 results.push_back(NN_TRY(getCacheEntryLocked(slot)));
78 }
79
80 return results;
81}
82
Michael Butler137ee992021-11-01 16:40:31 -070083nn::GeneralResult<void> Burst::MemoryCache::ensureCacheEntriesArePresentLocked(
Michael Butler76e491f2020-12-19 01:55:32 -080084 const std::vector<int32_t>& slots) {
85 const auto slotIsKnown = [this](int32_t slot)
86 REQUIRES(mMutex) { return mCache.count(slot) > 0; };
87
88 // find unique unknown slots
89 std::vector<int32_t> unknownSlots = slots;
90 std::sort(unknownSlots.begin(), unknownSlots.end());
91 auto unknownSlotsEnd = std::unique(unknownSlots.begin(), unknownSlots.end());
92 unknownSlotsEnd = std::remove_if(unknownSlots.begin(), unknownSlotsEnd, slotIsKnown);
93 unknownSlots.erase(unknownSlotsEnd, unknownSlots.end());
94
95 // quick-exit if all slots are known
96 if (unknownSlots.empty()) {
97 return {};
98 }
99
100 auto cb = neuralnetworks::utils::CallbackValue(getMemoriesCallback);
101
102 const auto ret = kBurstCallback->getMemories(unknownSlots, cb);
103 HANDLE_TRANSPORT_FAILURE(ret);
104
105 auto returnedMemories = NN_TRY(cb.take());
106
107 if (returnedMemories.size() != unknownSlots.size()) {
Michael Butler137ee992021-11-01 16:40:31 -0700108 return NN_ERROR() << "Burst::MemoryCache::ensureCacheEntriesArePresentLocked: Error "
109 "retrieving memories -- count mismatch between requested memories ("
110 << unknownSlots.size() << ") and returned memories ("
111 << returnedMemories.size() << ")";
Michael Butler76e491f2020-12-19 01:55:32 -0800112 }
113
114 // add memories to unknown slots
115 for (size_t i = 0; i < unknownSlots.size(); ++i) {
116 addCacheEntryLocked(unknownSlots[i], std::move(returnedMemories[i]));
117 }
118
119 return {};
120}
121
122nn::GeneralResult<std::pair<nn::SharedMemory, nn::IBurst::OptionalCacheHold>>
Michael Butler137ee992021-11-01 16:40:31 -0700123Burst::MemoryCache::getCacheEntryLocked(int32_t slot) {
Michael Butler76e491f2020-12-19 01:55:32 -0800124 if (const auto iter = mCache.find(slot); iter != mCache.end()) {
125 return iter->second;
126 }
Michael Butler137ee992021-11-01 16:40:31 -0700127 return NN_ERROR() << "Burst::MemoryCache::getCacheEntryLocked failed because slot " << slot
128 << " is not present in the cache";
Michael Butler76e491f2020-12-19 01:55:32 -0800129}
130
Michael Butler137ee992021-11-01 16:40:31 -0700131void Burst::MemoryCache::addCacheEntryLocked(int32_t slot, nn::SharedMemory memory) {
Michael Butler76e491f2020-12-19 01:55:32 -0800132 auto hold = kBurstExecutor->cacheMemory(memory);
133 mCache.emplace(slot, std::make_pair(std::move(memory), std::move(hold)));
134}
135
Michael Butler137ee992021-11-01 16:40:31 -0700136void Burst::MemoryCache::removeCacheEntry(int32_t slot) {
Michael Butler76e491f2020-12-19 01:55:32 -0800137 std::lock_guard guard(mMutex);
138 mCache.erase(slot);
139}
140
Michael Butler137ee992021-11-01 16:40:31 -0700141// Burst methods
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800142
Michael Butler137ee992021-11-01 16:40:31 -0700143nn::GeneralResult<sp<Burst>> Burst::create(
144 const sp<V1_2::IBurstCallback>& callback,
145 const MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel,
146 const MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel, nn::SharedBurst burstExecutor,
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800147 std::chrono::microseconds pollingTimeWindow) {
148 // check inputs
Michael Butler76e491f2020-12-19 01:55:32 -0800149 if (callback == nullptr || burstExecutor == nullptr) {
Michael Butler137ee992021-11-01 16:40:31 -0700150 return NN_ERROR() << "Burst::create passed a nullptr";
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800151 }
152
153 // create FMQ objects
Michael Butler76e491f2020-12-19 01:55:32 -0800154 auto requestChannelReceiver =
Michael Butler137ee992021-11-01 16:40:31 -0700155 NN_TRY(V1_2::utils::RequestChannelReceiver::create(requestChannel, pollingTimeWindow));
156 auto resultChannelSender = NN_TRY(V1_2::utils::ResultChannelSender::create(resultChannel));
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800157
158 // check FMQ objects
Michael Butler76e491f2020-12-19 01:55:32 -0800159 CHECK(requestChannelReceiver != nullptr);
160 CHECK(resultChannelSender != nullptr);
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800161
162 // make and return context
Michael Butler137ee992021-11-01 16:40:31 -0700163 return sp<Burst>::make(PrivateConstructorTag{}, callback, std::move(requestChannelReceiver),
164 std::move(resultChannelSender), std::move(burstExecutor));
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800165}
166
Michael Butler137ee992021-11-01 16:40:31 -0700167Burst::Burst(PrivateConstructorTag /*tag*/, const sp<V1_2::IBurstCallback>& callback,
168 std::unique_ptr<V1_2::utils::RequestChannelReceiver> requestChannel,
169 std::unique_ptr<V1_2::utils::ResultChannelSender> resultChannel,
170 nn::SharedBurst burstExecutor)
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800171 : mCallback(callback),
172 mRequestChannelReceiver(std::move(requestChannel)),
173 mResultChannelSender(std::move(resultChannel)),
Michael Butler76e491f2020-12-19 01:55:32 -0800174 mBurstExecutor(std::move(burstExecutor)),
175 mMemoryCache(mBurstExecutor, mCallback) {
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800176 // TODO: highly document the threading behavior of this class
177 mWorker = std::thread([this] { task(); });
178}
179
Michael Butler137ee992021-11-01 16:40:31 -0700180Burst::~Burst() {
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800181 // set teardown flag
182 mTeardown = true;
183 mRequestChannelReceiver->invalidate();
184
185 // wait for task thread to end
186 mWorker.join();
187}
188
Michael Butler137ee992021-11-01 16:40:31 -0700189Return<void> Burst::freeMemory(int32_t slot) {
Michael Butler76e491f2020-12-19 01:55:32 -0800190 mMemoryCache.removeCacheEntry(slot);
191 return Void();
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800192}
193
Michael Butler137ee992021-11-01 16:40:31 -0700194void Burst::task() {
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800195 // loop until the burst object is being destroyed
196 while (!mTeardown) {
197 // receive request
198 auto arguments = mRequestChannelReceiver->getBlocking();
199
Michael Butler76e491f2020-12-19 01:55:32 -0800200 // if the request packet was not properly received, return a generic error and skip the
201 // execution
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800202 //
Michael Butler76e491f2020-12-19 01:55:32 -0800203 // if the burst is being torn down, skip the execution so the "task" function can end
204 if (!arguments.has_value()) {
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800205 if (!mTeardown) {
Michael Butler137ee992021-11-01 16:40:31 -0700206 mResultChannelSender->send(V1_0::ErrorStatus::GENERAL_FAILURE, {}, kTiming);
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800207 }
208 continue;
209 }
210
Michael Butler137ee992021-11-01 16:40:31 -0700211 // unpack the arguments; types are Request, std::vector<int32_t>, and V1_2::MeasureTiming,
Michael Butler76e491f2020-12-19 01:55:32 -0800212 // respectively
213 const auto [requestWithoutPools, slotsOfPools, measure] = std::move(arguments).value();
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800214
Michael Butler76e491f2020-12-19 01:55:32 -0800215 auto result = execute(requestWithoutPools, slotsOfPools, measure);
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800216
217 // return result
Michael Butler76e491f2020-12-19 01:55:32 -0800218 if (result.has_value()) {
219 const auto& [outputShapes, timing] = result.value();
220 mResultChannelSender->send(V1_0::ErrorStatus::NONE, outputShapes, timing);
221 } else {
222 const auto& [message, code, outputShapes] = result.error();
223 LOG(ERROR) << "IBurst::execute failed with " << code << ": " << message;
Michael Butler137ee992021-11-01 16:40:31 -0700224 mResultChannelSender->send(V1_2::utils::convert(code).value(),
225 V1_2::utils::convert(outputShapes).value(), kTiming);
Michael Butler76e491f2020-12-19 01:55:32 -0800226 }
Michael Butlerf6b2d1a2020-12-19 14:44:35 -0800227 }
228}
229
Michael Butler137ee992021-11-01 16:40:31 -0700230nn::ExecutionResult<std::pair<hidl_vec<V1_2::OutputShape>, V1_2::Timing>> Burst::execute(
Michael Butler76e491f2020-12-19 01:55:32 -0800231 const V1_0::Request& requestWithoutPools, const std::vector<int32_t>& slotsOfPools,
Michael Butler137ee992021-11-01 16:40:31 -0700232 V1_2::MeasureTiming measure) {
Michael Butler76e491f2020-12-19 01:55:32 -0800233 NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION,
Michael Butler137ee992021-11-01 16:40:31 -0700234 "Burst getting memory, executing, and returning results");
Michael Butler76e491f2020-12-19 01:55:32 -0800235
236 // ensure executor with cache has required memory
Michael Butlerff9a5a52021-10-15 16:23:20 -0700237 const auto cacheEntries = NN_TRY(mMemoryCache.getCacheEntries(slotsOfPools));
Michael Butler76e491f2020-12-19 01:55:32 -0800238
239 // convert request, populating its pools
240 // This code performs an unvalidated convert because the request object without its pools is
241 // invalid because it is incomplete. Instead, the validation is performed after the memory pools
242 // have been added to the request.
Michael Butlerff9a5a52021-10-15 16:23:20 -0700243 auto canonicalRequest = NN_TRY(nn::unvalidatedConvert(requestWithoutPools));
Michael Butler76e491f2020-12-19 01:55:32 -0800244 CHECK(canonicalRequest.pools.empty());
245 std::transform(cacheEntries.begin(), cacheEntries.end(),
246 std::back_inserter(canonicalRequest.pools),
247 [](const auto& cacheEntry) { return cacheEntry.first; });
Michael Butlerff9a5a52021-10-15 16:23:20 -0700248 NN_TRY(validate(canonicalRequest));
Michael Butler76e491f2020-12-19 01:55:32 -0800249
Michael Butlerff9a5a52021-10-15 16:23:20 -0700250 nn::MeasureTiming canonicalMeasure = NN_TRY(nn::convert(measure));
Michael Butler76e491f2020-12-19 01:55:32 -0800251
252 const auto [outputShapes, timing] =
Michael Butler8414a6e2021-03-10 18:41:05 -0800253 NN_TRY(mBurstExecutor->execute(canonicalRequest, canonicalMeasure, {}, {}));
Michael Butler76e491f2020-12-19 01:55:32 -0800254
Michael Butler137ee992021-11-01 16:40:31 -0700255 return std::make_pair(NN_TRY(V1_2::utils::convert(outputShapes)),
256 NN_TRY(V1_2::utils::convert(timing)));
Michael Butler76e491f2020-12-19 01:55:32 -0800257}
258
Michael Butler137ee992021-11-01 16:40:31 -0700259} // namespace android::hardware::neuralnetworks::adapter