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