Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Butler | 89e99ba | 2019-01-24 02:36:37 -0800 | [diff] [blame] | 17 | #define LOG_TAG "ExecutionBurstServer" |
| 18 | |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 19 | #include "ExecutionBurstServer.h" |
| 20 | |
| 21 | #include <android-base/logging.h> |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 22 | #include <limits> |
Michael Butler | 3db6fe5 | 2019-01-29 11:20:30 -0800 | [diff] [blame] | 23 | #include "Tracing.h" |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 24 | |
Michael Butler | 3db6fe5 | 2019-01-29 11:20:30 -0800 | [diff] [blame] | 25 | namespace android::nn { |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 26 | namespace { |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 27 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 28 | constexpr Timing kNoTiming = {std::numeric_limits<uint64_t>::max(), |
| 29 | std::numeric_limits<uint64_t>::max()}; |
| 30 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 31 | // DefaultBurstExecutorWithCache adapts an IPreparedModel so that it can be |
| 32 | // used as an IBurstExecutorWithCache. Specifically, the cache simply stores the |
| 33 | // hidl_memory object, and the execution forwards calls to the provided |
| 34 | // IPreparedModel's "executeSynchronously" method. With this class, hidl_memory |
| 35 | // must be mapped and unmapped for each execution. |
| 36 | class DefaultBurstExecutorWithCache : public ExecutionBurstServer::IBurstExecutorWithCache { |
| 37 | public: |
| 38 | DefaultBurstExecutorWithCache(IPreparedModel* preparedModel) : mpPreparedModel(preparedModel) {} |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 39 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 40 | bool isCacheEntryPresent(int32_t slot) const override { |
Michael Butler | 47c988f6 | 2019-03-14 17:34:48 -0700 | [diff] [blame] | 41 | return slot < mMemoryCache.size() && mMemoryCache[slot].valid(); |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 42 | } |
Michael Butler | 47c988f6 | 2019-03-14 17:34:48 -0700 | [diff] [blame] | 43 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 44 | void addCacheEntry(const hidl_memory& memory, int32_t slot) override { |
| 45 | if (slot >= mMemoryCache.size()) { |
| 46 | mMemoryCache.resize(slot + 1); |
| 47 | } |
| 48 | mMemoryCache[slot] = memory; |
| 49 | } |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 50 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 51 | void removeCacheEntry(int32_t slot) override { |
| 52 | if (slot < mMemoryCache.size()) { |
| 53 | mMemoryCache[slot] = {}; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | std::tuple<ErrorStatus, hidl_vec<OutputShape>, Timing> execute( |
| 58 | const Request& request, const std::vector<int32_t>& slots, |
| 59 | MeasureTiming measure) override { |
| 60 | // convert slots to pools |
| 61 | hidl_vec<hidl_memory> pools(slots.size()); |
| 62 | std::transform(slots.begin(), slots.end(), pools.begin(), [this](int32_t slot) { |
| 63 | return slot < mMemoryCache.size() ? mMemoryCache[slot] : hidl_memory{}; |
| 64 | }); |
| 65 | |
| 66 | // create full request |
| 67 | Request fullRequest = request; |
| 68 | fullRequest.pools = std::move(pools); |
| 69 | |
| 70 | // setup execution |
| 71 | ErrorStatus returnedStatus = ErrorStatus::GENERAL_FAILURE; |
| 72 | hidl_vec<OutputShape> returnedOutputShapes; |
| 73 | Timing returnedTiming; |
| 74 | auto cb = [&returnedStatus, &returnedOutputShapes, &returnedTiming]( |
| 75 | ErrorStatus status, const hidl_vec<OutputShape>& outputShapes, |
| 76 | const Timing& timing) { |
| 77 | returnedStatus = status; |
| 78 | returnedOutputShapes = outputShapes; |
| 79 | returnedTiming = timing; |
Michael Butler | 47c988f6 | 2019-03-14 17:34:48 -0700 | [diff] [blame] | 80 | }; |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 81 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 82 | // execute |
| 83 | const Return<void> ret = mpPreparedModel->executeSynchronously(fullRequest, measure, cb); |
| 84 | if (!ret.isOk() || returnedStatus != ErrorStatus::NONE) { |
| 85 | LOG(ERROR) << "IPreparedModelAdapter::execute -- Error executing"; |
| 86 | return {ErrorStatus::GENERAL_FAILURE, {}, {}}; |
Michael Butler | 89e99ba | 2019-01-24 02:36:37 -0800 | [diff] [blame] | 87 | } |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 88 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 89 | return std::make_tuple(returnedStatus, std::move(returnedOutputShapes), returnedTiming); |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 92 | private: |
| 93 | IPreparedModel* const mpPreparedModel; |
| 94 | std::vector<hidl_memory> mMemoryCache; |
| 95 | }; |
Michael Butler | 47c988f6 | 2019-03-14 17:34:48 -0700 | [diff] [blame] | 96 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 97 | } // anonymous namespace |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 98 | |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 99 | // serialize result |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 100 | std::vector<FmqResultDatum> serialize(ErrorStatus errorStatus, |
| 101 | const std::vector<OutputShape>& outputShapes, Timing timing) { |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 102 | // count how many elements need to be sent for a request |
| 103 | size_t count = 2 + outputShapes.size(); |
| 104 | for (const auto& outputShape : outputShapes) { |
| 105 | count += outputShape.dimensions.size(); |
| 106 | } |
| 107 | |
| 108 | // create buffer to temporarily store elements |
| 109 | std::vector<FmqResultDatum> data; |
| 110 | data.reserve(count); |
| 111 | |
| 112 | // package packetInfo |
| 113 | { |
| 114 | FmqResultDatum datum; |
| 115 | datum.packetInformation({/*.packetSize=*/static_cast<uint32_t>(count), |
| 116 | /*.errorStatus=*/errorStatus, |
| 117 | /*.numberOfOperands=*/static_cast<uint32_t>(outputShapes.size())}); |
| 118 | data.push_back(datum); |
| 119 | } |
| 120 | |
| 121 | // package output shape data |
| 122 | for (const auto& operand : outputShapes) { |
| 123 | // package operand information |
Steven Moreland | 393ac6d | 2019-04-25 15:33:25 -0700 | [diff] [blame^] | 124 | FmqResultDatum::OperandInformation info{}; |
| 125 | info.isSufficient = operand.isSufficient; |
| 126 | info.numberOfDimensions = static_cast<uint32_t>(operand.dimensions.size()); |
| 127 | |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 128 | FmqResultDatum datum; |
Steven Moreland | 393ac6d | 2019-04-25 15:33:25 -0700 | [diff] [blame^] | 129 | datum.operandInformation(info); |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 130 | data.push_back(datum); |
| 131 | |
| 132 | // package operand dimensions |
| 133 | for (uint32_t dimension : operand.dimensions) { |
| 134 | FmqResultDatum datum; |
| 135 | datum.operandDimensionValue(dimension); |
| 136 | data.push_back(datum); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // package executionTiming |
| 141 | { |
| 142 | FmqResultDatum datum; |
| 143 | datum.executionTiming(timing); |
| 144 | data.push_back(datum); |
| 145 | } |
| 146 | |
| 147 | // return result |
| 148 | return data; |
| 149 | } |
| 150 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 151 | // deserialize request |
| 152 | std::optional<std::tuple<Request, std::vector<int32_t>, MeasureTiming>> deserialize( |
| 153 | const std::vector<FmqRequestDatum>& data) { |
| 154 | using discriminator = FmqRequestDatum::hidl_discriminator; |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 155 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 156 | size_t index = 0; |
| 157 | |
| 158 | // validate packet information |
| 159 | if (data[index].getDiscriminator() != discriminator::packetInformation) { |
| 160 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 161 | return std::nullopt; |
| 162 | } |
| 163 | |
| 164 | // unpackage packet information |
| 165 | const FmqRequestDatum::PacketInformation& packetInfo = data[index].packetInformation(); |
| 166 | index++; |
| 167 | const uint32_t packetSize = packetInfo.packetSize; |
| 168 | const uint32_t numberOfInputOperands = packetInfo.numberOfInputOperands; |
| 169 | const uint32_t numberOfOutputOperands = packetInfo.numberOfOutputOperands; |
| 170 | const uint32_t numberOfPools = packetInfo.numberOfPools; |
| 171 | |
| 172 | // unpackage input operands |
| 173 | std::vector<RequestArgument> inputs; |
| 174 | inputs.reserve(numberOfInputOperands); |
| 175 | for (size_t operand = 0; operand < numberOfInputOperands; ++operand) { |
| 176 | // validate input operand information |
| 177 | if (data[index].getDiscriminator() != discriminator::inputOperandInformation) { |
| 178 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 179 | return std::nullopt; |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 182 | // unpackage operand information |
| 183 | const FmqRequestDatum::OperandInformation& operandInfo = |
| 184 | data[index].inputOperandInformation(); |
| 185 | index++; |
| 186 | const bool hasNoValue = operandInfo.hasNoValue; |
| 187 | const DataLocation location = operandInfo.location; |
| 188 | const uint32_t numberOfDimensions = operandInfo.numberOfDimensions; |
Michael Butler | 3db6fe5 | 2019-01-29 11:20:30 -0800 | [diff] [blame] | 189 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 190 | // unpackage operand dimensions |
| 191 | std::vector<uint32_t> dimensions; |
| 192 | dimensions.reserve(numberOfDimensions); |
| 193 | for (size_t i = 0; i < numberOfDimensions; ++i) { |
| 194 | // validate dimension |
| 195 | if (data[index].getDiscriminator() != discriminator::inputOperandDimensionValue) { |
| 196 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 197 | return std::nullopt; |
| 198 | } |
| 199 | |
| 200 | // unpackage dimension |
| 201 | const uint32_t dimension = data[index].inputOperandDimensionValue(); |
| 202 | index++; |
| 203 | |
| 204 | // store result |
| 205 | dimensions.push_back(dimension); |
| 206 | } |
| 207 | |
| 208 | // store result |
| 209 | inputs.push_back( |
| 210 | {/*.hasNoValue=*/hasNoValue, /*.location=*/location, /*.dimensions=*/dimensions}); |
| 211 | } |
| 212 | |
| 213 | // unpackage output operands |
| 214 | std::vector<RequestArgument> outputs; |
| 215 | outputs.reserve(numberOfOutputOperands); |
| 216 | for (size_t operand = 0; operand < numberOfOutputOperands; ++operand) { |
| 217 | // validate output operand information |
| 218 | if (data[index].getDiscriminator() != discriminator::outputOperandInformation) { |
| 219 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 220 | return std::nullopt; |
| 221 | } |
| 222 | |
| 223 | // unpackage operand information |
| 224 | const FmqRequestDatum::OperandInformation& operandInfo = |
| 225 | data[index].outputOperandInformation(); |
| 226 | index++; |
| 227 | const bool hasNoValue = operandInfo.hasNoValue; |
| 228 | const DataLocation location = operandInfo.location; |
| 229 | const uint32_t numberOfDimensions = operandInfo.numberOfDimensions; |
| 230 | |
| 231 | // unpackage operand dimensions |
| 232 | std::vector<uint32_t> dimensions; |
| 233 | dimensions.reserve(numberOfDimensions); |
| 234 | for (size_t i = 0; i < numberOfDimensions; ++i) { |
| 235 | // validate dimension |
| 236 | if (data[index].getDiscriminator() != discriminator::outputOperandDimensionValue) { |
| 237 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 238 | return std::nullopt; |
| 239 | } |
| 240 | |
| 241 | // unpackage dimension |
| 242 | const uint32_t dimension = data[index].outputOperandDimensionValue(); |
| 243 | index++; |
| 244 | |
| 245 | // store result |
| 246 | dimensions.push_back(dimension); |
| 247 | } |
| 248 | |
| 249 | // store result |
| 250 | outputs.push_back( |
| 251 | {/*.hasNoValue=*/hasNoValue, /*.location=*/location, /*.dimensions=*/dimensions}); |
| 252 | } |
| 253 | |
| 254 | // unpackage pools |
| 255 | std::vector<int32_t> slots; |
| 256 | slots.reserve(numberOfPools); |
| 257 | for (size_t pool = 0; pool < numberOfPools; ++pool) { |
| 258 | // validate input operand information |
| 259 | if (data[index].getDiscriminator() != discriminator::poolIdentifier) { |
| 260 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 261 | return std::nullopt; |
| 262 | } |
| 263 | |
| 264 | // unpackage operand information |
| 265 | const int32_t poolId = data[index].poolIdentifier(); |
| 266 | index++; |
| 267 | |
| 268 | // store result |
| 269 | slots.push_back(poolId); |
| 270 | } |
| 271 | |
| 272 | // validate measureTiming |
| 273 | if (data[index].getDiscriminator() != discriminator::measureTiming) { |
| 274 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 275 | return std::nullopt; |
| 276 | } |
| 277 | |
| 278 | // unpackage measureTiming |
| 279 | const MeasureTiming measure = data[index].measureTiming(); |
| 280 | index++; |
| 281 | |
| 282 | // validate packet information |
| 283 | if (index != packetSize) { |
| 284 | LOG(ERROR) << "FMQ Result packet ill-formed"; |
| 285 | return std::nullopt; |
| 286 | } |
| 287 | |
| 288 | // return request |
| 289 | Request request = {/*.inputs=*/inputs, /*.outputs=*/outputs, /*.pools=*/{}}; |
| 290 | return std::make_tuple(std::move(request), std::move(slots), measure); |
| 291 | } |
| 292 | |
| 293 | // RequestChannelReceiver methods |
| 294 | |
| 295 | std::unique_ptr<RequestChannelReceiver> RequestChannelReceiver::create( |
| 296 | const FmqRequestDescriptor& requestChannel) { |
| 297 | std::unique_ptr<FmqRequestChannel> fmqRequestChannel = |
| 298 | std::make_unique<FmqRequestChannel>(requestChannel); |
| 299 | if (!fmqRequestChannel->isValid()) { |
| 300 | LOG(ERROR) << "Unable to create RequestChannelReceiver"; |
| 301 | return nullptr; |
| 302 | } |
| 303 | const bool blocking = fmqRequestChannel->getEventFlagWord() != nullptr; |
| 304 | return std::make_unique<RequestChannelReceiver>(std::move(fmqRequestChannel), blocking); |
| 305 | } |
| 306 | |
| 307 | RequestChannelReceiver::RequestChannelReceiver(std::unique_ptr<FmqRequestChannel> fmqRequestChannel, |
| 308 | bool blocking) |
| 309 | : mFmqRequestChannel(std::move(fmqRequestChannel)), mBlocking(blocking) {} |
| 310 | |
| 311 | std::optional<std::tuple<Request, std::vector<int32_t>, MeasureTiming>> |
| 312 | RequestChannelReceiver::getBlocking() { |
| 313 | const auto packet = getPacketBlocking(); |
| 314 | if (!packet) { |
| 315 | return std::nullopt; |
| 316 | } |
| 317 | |
| 318 | return deserialize(*packet); |
| 319 | } |
| 320 | |
| 321 | void RequestChannelReceiver::invalidate() { |
| 322 | mTeardown = true; |
| 323 | |
| 324 | // force unblock |
| 325 | // ExecutionBurstServer is by default waiting on a request packet. If the |
| 326 | // client process destroys its burst object, the server will still be |
| 327 | // waiting on the futex (assuming mBlocking is true). This force unblock |
| 328 | // wakes up any thread waiting on the futex. |
| 329 | if (mBlocking) { |
| 330 | // TODO: look for a different/better way to signal/notify the futex to |
| 331 | // wake up any thread waiting on it |
| 332 | FmqRequestDatum datum; |
| 333 | datum.packetInformation({/*.packetSize=*/0, /*.numberOfInputOperands=*/0, |
| 334 | /*.numberOfOutputOperands=*/0, /*.numberOfPools=*/0}); |
| 335 | mFmqRequestChannel->writeBlocking(&datum, 1); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | std::optional<std::vector<FmqRequestDatum>> RequestChannelReceiver::getPacketBlocking() { |
| 340 | using discriminator = FmqRequestDatum::hidl_discriminator; |
| 341 | |
| 342 | if (mTeardown) { |
| 343 | return std::nullopt; |
| 344 | } |
| 345 | |
| 346 | // wait for request packet and read first element of request packet |
| 347 | // TODO: have a more elegant way to wait for data, and read it all at once. |
| 348 | // For example, EventFlag can be used to directly wait on the futex, and all |
| 349 | // the data can be read at once with a non-blocking call to |
| 350 | // MessageQueue::read. For further optimization, MessageQueue::beginRead and |
| 351 | // MessageQueue::commitRead can be used to avoid an extra copy of the |
| 352 | // metadata. |
| 353 | FmqRequestDatum datum; |
| 354 | bool success = false; |
| 355 | if (mBlocking) { |
| 356 | success = mFmqRequestChannel->readBlocking(&datum, 1); |
| 357 | } else { |
| 358 | while ((success = !mTeardown.load(std::memory_order_relaxed)) && |
| 359 | !mFmqRequestChannel->read(&datum, 1)) { |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // terminate loop |
| 364 | if (mTeardown) { |
| 365 | return std::nullopt; |
| 366 | } |
| 367 | |
| 368 | // validate packet information |
| 369 | if (!success || datum.getDiscriminator() != discriminator::packetInformation) { |
| 370 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 371 | return std::make_optional<std::vector<FmqRequestDatum>>(); |
| 372 | } |
| 373 | |
| 374 | NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, "ExecutionBurstServer getting packet"); |
| 375 | |
| 376 | // unpack packet information |
| 377 | const auto& packetInfo = datum.packetInformation(); |
| 378 | const size_t count = packetInfo.packetSize; |
| 379 | |
| 380 | // retrieve remaining elements |
| 381 | // NOTE: all of the data is already available at this point, so there's no |
| 382 | // need to do a blocking wait to wait for more data. This is known because |
| 383 | // in FMQ, all writes are published (made available) atomically. Currently, |
| 384 | // the producer always publishes the entire packet in one function call, so |
| 385 | // if the first element of the packet is available, the remaining elements |
| 386 | // are also available. |
| 387 | std::vector<FmqRequestDatum> packet(count); |
| 388 | packet.front() = datum; |
| 389 | success = mFmqRequestChannel->read(packet.data() + 1, packet.size() - 1); |
| 390 | |
| 391 | if (!success) { |
| 392 | return std::make_optional<std::vector<FmqRequestDatum>>(); |
| 393 | } |
| 394 | |
| 395 | return packet; |
| 396 | } |
| 397 | |
| 398 | // ResultChannelSender methods |
| 399 | |
| 400 | std::unique_ptr<ResultChannelSender> ResultChannelSender::create( |
| 401 | const FmqResultDescriptor& resultChannel) { |
| 402 | std::unique_ptr<FmqResultChannel> fmqResultChannel = |
| 403 | std::make_unique<FmqResultChannel>(resultChannel); |
| 404 | if (!fmqResultChannel->isValid()) { |
| 405 | LOG(ERROR) << "Unable to create RequestChannelSender"; |
| 406 | return nullptr; |
| 407 | } |
| 408 | const bool blocking = fmqResultChannel->getEventFlagWord() != nullptr; |
| 409 | return std::make_unique<ResultChannelSender>(std::move(fmqResultChannel), blocking); |
| 410 | } |
| 411 | |
| 412 | ResultChannelSender::ResultChannelSender(std::unique_ptr<FmqResultChannel> fmqResultChannel, |
| 413 | bool blocking) |
| 414 | : mFmqResultChannel(std::move(fmqResultChannel)), mBlocking(blocking) {} |
| 415 | |
| 416 | bool ResultChannelSender::send(ErrorStatus errorStatus, |
| 417 | const std::vector<OutputShape>& outputShapes, Timing timing) { |
| 418 | const std::vector<FmqResultDatum> serialized = serialize(errorStatus, outputShapes, timing); |
| 419 | return sendPacket(serialized); |
| 420 | } |
| 421 | |
| 422 | bool ResultChannelSender::sendPacket(const std::vector<FmqResultDatum>& packet) { |
| 423 | if (mBlocking) { |
| 424 | return mFmqResultChannel->writeBlocking(packet.data(), packet.size()); |
| 425 | } else { |
| 426 | return mFmqResultChannel->write(packet.data(), packet.size()); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // ExecutionBurstServer methods |
| 431 | |
| 432 | sp<ExecutionBurstServer> ExecutionBurstServer::create( |
| 433 | const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel, |
| 434 | const MQDescriptorSync<FmqResultDatum>& resultChannel, |
| 435 | std::shared_ptr<IBurstExecutorWithCache> executorWithCache) { |
| 436 | // check inputs |
| 437 | if (callback == nullptr || executorWithCache == nullptr) { |
| 438 | LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr"; |
| 439 | return nullptr; |
| 440 | } |
| 441 | |
| 442 | // create FMQ objects |
| 443 | std::unique_ptr<RequestChannelReceiver> requestChannelReceiver = |
| 444 | RequestChannelReceiver::create(requestChannel); |
| 445 | std::unique_ptr<ResultChannelSender> resultChannelSender = |
| 446 | ResultChannelSender::create(resultChannel); |
| 447 | |
| 448 | // check FMQ objects |
| 449 | if (!requestChannelReceiver || !resultChannelSender) { |
| 450 | LOG(ERROR) << "ExecutionBurstServer::create failed to create FastMessageQueue"; |
| 451 | return nullptr; |
| 452 | } |
| 453 | |
| 454 | // make and return context |
| 455 | return new ExecutionBurstServer(callback, std::move(requestChannelReceiver), |
| 456 | std::move(resultChannelSender), std::move(executorWithCache)); |
| 457 | } |
| 458 | |
| 459 | sp<ExecutionBurstServer> ExecutionBurstServer::create( |
| 460 | const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel, |
| 461 | const MQDescriptorSync<FmqResultDatum>& resultChannel, IPreparedModel* preparedModel) { |
| 462 | // check relevant input |
| 463 | if (preparedModel == nullptr) { |
| 464 | LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr"; |
| 465 | return nullptr; |
| 466 | } |
| 467 | |
| 468 | // adapt IPreparedModel to have caching |
| 469 | const std::shared_ptr<DefaultBurstExecutorWithCache> preparedModelAdapter = |
| 470 | std::make_shared<DefaultBurstExecutorWithCache>(preparedModel); |
| 471 | |
| 472 | // make and return context |
| 473 | return ExecutionBurstServer::create(callback, requestChannel, resultChannel, |
| 474 | preparedModelAdapter); |
| 475 | } |
| 476 | |
| 477 | ExecutionBurstServer::ExecutionBurstServer( |
| 478 | const sp<IBurstCallback>& callback, std::unique_ptr<RequestChannelReceiver> requestChannel, |
| 479 | std::unique_ptr<ResultChannelSender> resultChannel, |
| 480 | std::shared_ptr<IBurstExecutorWithCache> executorWithCache) |
| 481 | : mCallback(callback), |
| 482 | mRequestChannelReceiver(std::move(requestChannel)), |
| 483 | mResultChannelSender(std::move(resultChannel)), |
| 484 | mExecutorWithCache(std::move(executorWithCache)) { |
| 485 | // TODO: highly document the threading behavior of this class |
| 486 | mWorker = std::thread([this] { task(); }); |
| 487 | } |
| 488 | |
| 489 | ExecutionBurstServer::~ExecutionBurstServer() { |
| 490 | // set teardown flag |
| 491 | mTeardown = true; |
| 492 | mRequestChannelReceiver->invalidate(); |
| 493 | |
| 494 | // wait for task thread to end |
| 495 | mWorker.join(); |
| 496 | } |
| 497 | |
| 498 | Return<void> ExecutionBurstServer::freeMemory(int32_t slot) { |
| 499 | mExecutorWithCache->removeCacheEntry(slot); |
| 500 | return Void(); |
| 501 | } |
| 502 | |
| 503 | void ExecutionBurstServer::ensureCacheEntriesArePresentLocked(const std::vector<int32_t>& slots) { |
| 504 | const auto slotIsKnown = [this](int32_t slot) { |
| 505 | return mExecutorWithCache->isCacheEntryPresent(slot); |
| 506 | }; |
| 507 | |
| 508 | // find unique unknown slots |
| 509 | std::vector<int32_t> unknownSlots = slots; |
| 510 | auto unknownSlotsEnd = unknownSlots.end(); |
| 511 | std::sort(unknownSlots.begin(), unknownSlotsEnd); |
| 512 | unknownSlotsEnd = std::unique(unknownSlots.begin(), unknownSlotsEnd); |
| 513 | unknownSlotsEnd = std::remove_if(unknownSlots.begin(), unknownSlotsEnd, slotIsKnown); |
| 514 | unknownSlots.erase(unknownSlotsEnd, unknownSlots.end()); |
| 515 | |
| 516 | // quick-exit if all slots are known |
| 517 | if (unknownSlots.empty()) { |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | ErrorStatus errorStatus = ErrorStatus::GENERAL_FAILURE; |
| 522 | std::vector<hidl_memory> returnedMemories; |
| 523 | auto cb = [&errorStatus, &returnedMemories](ErrorStatus status, |
| 524 | const hidl_vec<hidl_memory>& memories) { |
| 525 | errorStatus = status; |
| 526 | returnedMemories = memories; |
| 527 | }; |
| 528 | |
| 529 | const Return<void> ret = mCallback->getMemories(unknownSlots, cb); |
| 530 | |
| 531 | if (!ret.isOk() || errorStatus != ErrorStatus::NONE || |
| 532 | returnedMemories.size() != unknownSlots.size()) { |
| 533 | LOG(ERROR) << "Error retrieving memories"; |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | // add memories to unknown slots |
| 538 | for (size_t i = 0; i < unknownSlots.size(); ++i) { |
| 539 | mExecutorWithCache->addCacheEntry(returnedMemories[i], unknownSlots[i]); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | void ExecutionBurstServer::task() { |
| 544 | // loop until the burst object is being destroyed |
| 545 | while (!mTeardown) { |
| 546 | // receive request |
| 547 | auto arguments = mRequestChannelReceiver->getBlocking(); |
| 548 | |
| 549 | // if the request packet was not properly received, return a generic |
| 550 | // error and skip the execution |
| 551 | // |
| 552 | // if the burst is being torn down, skip the execution exection so the |
| 553 | // "task" function can end |
| 554 | if (!arguments) { |
| 555 | if (!mTeardown) { |
| 556 | mResultChannelSender->send(ErrorStatus::GENERAL_FAILURE, {}, kNoTiming); |
| 557 | } |
| 558 | continue; |
| 559 | } |
| 560 | |
| 561 | // otherwise begin tracing execution |
| 562 | NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, |
| 563 | "ExecutionBurstServer getting memory, executing, and returning results"); |
| 564 | |
| 565 | // unpack the arguments; types are Request, std::vector<int32_t>, and |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 566 | // MeasureTiming, respectively |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 567 | const auto [requestWithoutPools, slotsOfPools, measure] = std::move(*arguments); |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 568 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 569 | // ensure executor with cache has required memory |
| 570 | std::lock_guard<std::mutex> hold(mMutex); |
| 571 | ensureCacheEntriesArePresentLocked(slotsOfPools); |
| 572 | |
| 573 | // perform computation; types are ErrorStatus, hidl_vec<OutputShape>, |
| 574 | // and Timing, respectively |
| 575 | const auto [errorStatus, outputShapes, returnedTiming] = |
| 576 | mExecutorWithCache->execute(requestWithoutPools, slotsOfPools, measure); |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 577 | |
| 578 | // return result |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 579 | mResultChannelSender->send(errorStatus, outputShapes, returnedTiming); |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Michael Butler | 3db6fe5 | 2019-01-29 11:20:30 -0800 | [diff] [blame] | 583 | } // namespace android::nn |