Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Jan Eilers | 43a430d | 2020-02-28 15:40:44 +0000 | [diff] [blame] | 5 | // Note: the ArmnnBurstExecutorWithCache in this file is based on Android code |
| 6 | // under the Apache 2.0 license. See comment below for details. |
| 7 | // |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 8 | |
| 9 | #define LOG_TAG "ArmnnDriver" |
| 10 | |
| 11 | #include "ArmnnPreparedModel_1_2.hpp" |
| 12 | #include "Utils.hpp" |
| 13 | |
| 14 | #include <boost/format.hpp> |
| 15 | #include <log/log.h> |
| 16 | #include <OperationsUtils.h> |
| 17 | #include <ExecutionBurstServer.h> |
| 18 | #include <ValidateHal.h> |
| 19 | |
| 20 | #include <cassert> |
| 21 | #include <cinttypes> |
| 22 | |
| 23 | using namespace android; |
| 24 | using namespace android::hardware; |
| 25 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 26 | namespace { |
| 27 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 28 | static const V1_2::Timing g_NoTiming = {.timeOnDevice = UINT64_MAX, .timeInDriver = UINT64_MAX}; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 29 | using namespace armnn_driver; |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 30 | using TimePoint = std::chrono::steady_clock::time_point; |
| 31 | |
| 32 | TimePoint Now() |
| 33 | { |
| 34 | return std::chrono::steady_clock::now(); |
| 35 | } |
| 36 | |
| 37 | unsigned long MicrosecondsDuration(TimePoint endPoint, TimePoint startPoint) |
| 38 | { |
| 39 | return static_cast<unsigned long>(std::chrono::duration_cast<std::chrono::microseconds>( |
| 40 | endPoint - startPoint).count()); |
| 41 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 42 | |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 43 | void NotifyCallbackAndCheck(const ::android::sp<V1_0::IExecutionCallback>& callback, |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 44 | V1_0::ErrorStatus errorStatus, |
| 45 | std::vector<V1_2::OutputShape>, |
| 46 | const V1_2::Timing, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 47 | std::string callingFunction) |
| 48 | { |
| 49 | Return<void> returned = callback->notify(errorStatus); |
| 50 | // This check is required, if the callback fails and it isn't checked it will bring down the service |
| 51 | if (!returned.isOk()) |
| 52 | { |
| 53 | ALOGE("ArmnnDriver::%s: hidl callback failed to return properly: %s", |
| 54 | callingFunction.c_str(), returned.description().c_str()); |
| 55 | } |
| 56 | } |
| 57 | |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 58 | void NotifyCallbackAndCheck(const ::android::sp<V1_2::IExecutionCallback>& callback, |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 59 | V1_0::ErrorStatus errorStatus, |
| 60 | std::vector<V1_2::OutputShape> outputShapes, |
| 61 | const V1_2::Timing timing, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 62 | std::string callingFunction) |
| 63 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 64 | Return<void> returned = callback->notify_1_2(errorStatus, outputShapes, timing); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 65 | // This check is required, if the callback fails and it isn't checked it will bring down the service |
| 66 | if (!returned.isOk()) |
| 67 | { |
| 68 | ALOGE("ArmnnDriver::%s: hidl callback failed to return properly: %s", |
| 69 | callingFunction.c_str(), returned.description().c_str()); |
| 70 | } |
| 71 | } |
| 72 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 73 | bool ValidateRequestArgument(const V1_0::RequestArgument& requestArg, const armnn::TensorInfo& tensorInfo) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 74 | { |
| 75 | if (requestArg.dimensions.size() != 0) |
| 76 | { |
| 77 | if (requestArg.dimensions.size() != tensorInfo.GetNumDimensions()) |
| 78 | { |
| 79 | ALOGE("Mismatched dimensions (request argument: %zu, expected: %u)", |
| 80 | requestArg.dimensions.size(), tensorInfo.GetNumDimensions()); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | for (unsigned int d = 0; d < tensorInfo.GetNumDimensions(); ++d) |
| 85 | { |
| 86 | if (requestArg.dimensions[d] != tensorInfo.GetShape()[d]) |
| 87 | { |
| 88 | ALOGE("Mismatched size for dimension %d (request argument: %u, expected %u)", |
| 89 | d, requestArg.dimensions[d], tensorInfo.GetShape()[d]); |
| 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 98 | armnn::Tensor GetTensorForRequestArgument(const V1_0::RequestArgument& requestArg, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 99 | const armnn::TensorInfo& tensorInfo, |
| 100 | const std::vector<::android::nn::RunTimePoolInfo>& requestPools) |
| 101 | { |
| 102 | if (!ValidateRequestArgument(requestArg, tensorInfo)) |
| 103 | { |
| 104 | return armnn::Tensor(); |
| 105 | } |
| 106 | |
| 107 | return armnn::Tensor(tensorInfo, GetMemoryFromPool(requestArg.location, requestPools)); |
| 108 | } |
| 109 | |
| 110 | inline std::string BuildTensorName(const char* tensorNamePrefix, std::size_t index) |
| 111 | { |
| 112 | return tensorNamePrefix + std::to_string(index); |
| 113 | } |
| 114 | |
| 115 | } // anonymous namespace |
| 116 | |
| 117 | using namespace android::hardware; |
| 118 | |
| 119 | namespace armnn_driver |
| 120 | { |
| 121 | |
| 122 | template<typename HalVersion> |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 123 | RequestThread<ArmnnPreparedModel_1_2, HalVersion, ArmnnCallback_1_2> |
| 124 | ArmnnPreparedModel_1_2<HalVersion>::m_RequestThread; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 125 | |
| 126 | template<typename HalVersion> |
| 127 | template<typename TensorBindingCollection> |
| 128 | void ArmnnPreparedModel_1_2<HalVersion>::DumpTensorsIfRequired(char const* tensorNamePrefix, |
| 129 | const TensorBindingCollection& tensorBindings) |
| 130 | { |
| 131 | if (!m_RequestInputsAndOutputsDumpDir.empty()) |
| 132 | { |
| 133 | const std::string requestName = boost::str(boost::format("%1%_%2%.dump") % m_NetworkId % m_RequestCount); |
| 134 | for (std::size_t i = 0u; i < tensorBindings.size(); ++i) |
| 135 | { |
| 136 | DumpTensor(m_RequestInputsAndOutputsDumpDir, |
| 137 | requestName, |
| 138 | BuildTensorName(tensorNamePrefix, i), |
| 139 | tensorBindings[i].second); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | template<typename HalVersion> |
| 145 | ArmnnPreparedModel_1_2<HalVersion>::ArmnnPreparedModel_1_2(armnn::NetworkId networkId, |
| 146 | armnn::IRuntime* runtime, |
| 147 | const V1_2::Model& model, |
| 148 | const std::string& requestInputsAndOutputsDumpDir, |
| 149 | const bool gpuProfilingEnabled) |
| 150 | : m_NetworkId(networkId) |
| 151 | , m_Runtime(runtime) |
| 152 | , m_Model(model) |
| 153 | , m_RequestCount(0) |
| 154 | , m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir) |
| 155 | , m_GpuProfilingEnabled(gpuProfilingEnabled) |
| 156 | { |
| 157 | // Enable profiling if required. |
| 158 | m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled); |
| 159 | } |
| 160 | |
| 161 | template<typename HalVersion> |
| 162 | ArmnnPreparedModel_1_2<HalVersion>::~ArmnnPreparedModel_1_2() |
| 163 | { |
| 164 | // Get a hold of the profiler used by this model. |
| 165 | std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId); |
| 166 | |
| 167 | // Unload the network associated with this model. |
| 168 | m_Runtime->UnloadNetwork(m_NetworkId); |
| 169 | |
| 170 | // Dump the profiling info to a file if required. |
| 171 | DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId, profiler.get()); |
| 172 | } |
| 173 | |
| 174 | template<typename HalVersion> |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 175 | Return <V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::execute(const V1_0::Request& request, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 176 | const ::android::sp<V1_0::IExecutionCallback>& callback) |
| 177 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 178 | if (callback.get() == nullptr) |
| 179 | { |
| 180 | ALOGE("ArmnnPreparedModel_1_2::execute invalid callback passed"); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 181 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 184 | auto cb = [callback](V1_0::ErrorStatus errorStatus, |
| 185 | std::vector<V1_2::OutputShape> outputShapes, |
| 186 | const V1_2::Timing& timing, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 187 | std::string callingFunction) |
| 188 | { |
| 189 | NotifyCallbackAndCheck(callback, errorStatus, outputShapes, timing, callingFunction); |
| 190 | }; |
| 191 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 192 | return Execute(request, V1_2::MeasureTiming::NO, cb); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | template<typename HalVersion> |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 196 | Return <V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::execute_1_2(const V1_0::Request& request, |
| 197 | V1_2::MeasureTiming measureTiming, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 198 | const sp<V1_2::IExecutionCallback>& callback) |
| 199 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 200 | if (callback.get() == nullptr) |
| 201 | { |
| 202 | ALOGE("ArmnnPreparedModel_1_2::execute_1_2 invalid callback passed"); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 203 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 206 | auto cb = [callback](V1_0::ErrorStatus errorStatus, |
| 207 | std::vector<V1_2::OutputShape> outputShapes, |
| 208 | const V1_2::Timing& timing, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 209 | std::string callingFunction) |
| 210 | { |
| 211 | NotifyCallbackAndCheck(callback, errorStatus, outputShapes, timing, callingFunction); |
| 212 | }; |
| 213 | |
| 214 | return Execute(request, measureTiming, cb); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | template<typename HalVersion> |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 218 | Return<void> ArmnnPreparedModel_1_2<HalVersion>::executeSynchronously(const V1_0::Request& request, |
| 219 | V1_2::MeasureTiming measureTiming, |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 220 | executeSynchronously_cb cb) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 221 | { |
| 222 | ALOGV("ArmnnPreparedModel_1_2::executeSynchronously(): %s", GetModelSummary(m_Model).c_str()); |
| 223 | m_RequestCount++; |
| 224 | |
| 225 | if (cb == nullptr) |
| 226 | { |
| 227 | ALOGE("ArmnnPreparedModel_1_2::executeSynchronously invalid callback passed"); |
| 228 | return Void(); |
| 229 | } |
| 230 | |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 231 | TimePoint driverStart, driverEnd, deviceStart, deviceEnd; |
| 232 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 233 | if (measureTiming == V1_2::MeasureTiming::YES) |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 234 | { |
| 235 | driverStart = Now(); |
| 236 | } |
| 237 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 238 | if (!android::nn::validateRequest(request, m_Model)) |
| 239 | { |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 240 | ALOGE("ArmnnPreparedModel_1_2::executeSynchronously invalid request model"); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 241 | cb(V1_0::ErrorStatus::INVALID_ARGUMENT, {}, g_NoTiming); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 242 | return Void(); |
| 243 | } |
| 244 | |
| 245 | // allocate the tensors on the heap, as they are passed to the request thread |
| 246 | auto pInputTensors = std::make_shared<armnn::InputTensors>(); |
| 247 | auto pOutputTensors = std::make_shared<armnn::OutputTensors>(); |
| 248 | |
| 249 | // map the memory pool into shared pointers |
| 250 | // use a shared memory pools vector on the heap, as it is passed to the request thread |
| 251 | auto pMemPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>(); |
| 252 | |
| 253 | if (!setRunTimePoolInfosFromHidlMemories(pMemPools.get(), request.pools)) |
| 254 | { |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 255 | cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 256 | return Void(); |
| 257 | } |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 258 | std::vector<V1_2::OutputShape> outputShapes(request.outputs.size()); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 259 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 260 | try |
| 261 | { |
| 262 | pInputTensors->reserve(request.inputs.size()); |
| 263 | for (unsigned int i = 0; i < request.inputs.size(); i++) |
| 264 | { |
| 265 | const auto& inputArg = request.inputs[i]; |
| 266 | |
| 267 | const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i); |
| 268 | const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, *pMemPools); |
| 269 | |
| 270 | if (inputTensor.GetMemoryArea() == nullptr) |
| 271 | { |
| 272 | ALOGE("Cannot execute request. Error converting request input %u to tensor", i); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 273 | cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 274 | return Void(); |
| 275 | } |
| 276 | |
| 277 | pInputTensors->emplace_back(i, inputTensor); |
| 278 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 279 | pOutputTensors->reserve(request.outputs.size()); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 280 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 281 | for (unsigned int i = 0; i < request.outputs.size(); i++) |
| 282 | { |
| 283 | const auto& outputArg = request.outputs[i]; |
| 284 | |
| 285 | const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i); |
| 286 | const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, *pMemPools); |
| 287 | |
| 288 | if (outputTensor.GetMemoryArea() == nullptr) |
| 289 | { |
| 290 | ALOGE("Cannot execute request. Error converting request output %u to tensor", i); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 291 | cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 292 | return Void(); |
| 293 | } |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 294 | const size_t outputSize = outputTensorInfo.GetNumBytes(); |
Kevin DuBois | 2099015 | 2020-11-03 10:27:42 -0800 | [diff] [blame] | 295 | const size_t bufferSize = pMemPools->at(outputArg.location.poolIndex).getSize(); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 296 | |
| 297 | hidl_vec<uint32_t> dimensions; |
| 298 | |
| 299 | armnn::TensorShape tensorShape = outputTensorInfo.GetShape(); |
| 300 | const unsigned int numDims = tensorShape.GetNumDimensions(); |
| 301 | dimensions.resize(numDims); |
| 302 | |
| 303 | for (unsigned int outputIdx = 0u; outputIdx < numDims; ++outputIdx) |
| 304 | { |
| 305 | dimensions[outputIdx] = tensorShape[outputIdx]; |
| 306 | } |
| 307 | outputShapes[i].dimensions = dimensions; |
| 308 | outputShapes[i].isSufficient = bufferSize >= outputSize; |
| 309 | |
| 310 | if (bufferSize < outputSize) |
| 311 | { |
| 312 | ALOGW("ArmnnPreparedModel_1_2::Execute failed"); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 313 | cb(V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, outputShapes, g_NoTiming); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 314 | return Void(); |
| 315 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 316 | |
| 317 | pOutputTensors->emplace_back(i, outputTensor); |
| 318 | } |
| 319 | } |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 320 | catch (armnn::Exception& e) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 321 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 322 | ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what()); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 323 | cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 324 | return Void(); |
| 325 | } |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 326 | catch (std::exception& e) |
| 327 | { |
| 328 | ALOGE("std::exception caught while preparing for EnqueueWorkload: %s", e.what()); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 329 | cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming); |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 330 | return Void(); |
| 331 | } |
| 332 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 333 | ALOGV("ArmnnPreparedModel_1_2::executeSynchronously() before Execution"); |
| 334 | |
| 335 | DumpTensorsIfRequired("Input", *pInputTensors); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 336 | // run it |
| 337 | try |
| 338 | { |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 339 | if (measureTiming == V1_2::MeasureTiming::YES) |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 340 | { |
| 341 | deviceStart = Now(); |
| 342 | } |
| 343 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 344 | armnn::Status status = m_Runtime->EnqueueWorkload(m_NetworkId, *pInputTensors, *pOutputTensors); |
| 345 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 346 | if (measureTiming == V1_2::MeasureTiming::YES) |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 347 | { |
| 348 | deviceEnd = Now(); |
| 349 | } |
| 350 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 351 | if (status != armnn::Status::Success) |
| 352 | { |
| 353 | ALOGW("EnqueueWorkload failed"); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 354 | cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 355 | return Void(); |
| 356 | } |
| 357 | } |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 358 | catch (armnn::Exception& e) |
| 359 | { |
| 360 | ALOGW("armnn::Exception caught from EnqueueWorkload: %s", e.what()); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 361 | cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming); |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 362 | return Void(); |
| 363 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 364 | catch (std::exception& e) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 365 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 366 | ALOGE("std::exception caught from EnqueueWorkload: %s", e.what()); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 367 | cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 368 | return Void(); |
| 369 | } |
| 370 | |
| 371 | DumpTensorsIfRequired("Output", *pOutputTensors); |
| 372 | |
| 373 | // Commit output buffers. |
| 374 | // Note that we update *all* pools, even if they aren't actually used as outputs - |
| 375 | // this is simpler and is what the CpuExecutor does. |
| 376 | for (android::nn::RunTimePoolInfo& pool : *pMemPools) |
| 377 | { |
Kevin DuBois | 03993ba | 2020-08-26 14:07:41 -0700 | [diff] [blame] | 378 | pool.flush(); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 379 | } |
| 380 | ALOGV("ArmnnPreparedModel_1_2::executeSynchronously() after Execution"); |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 381 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 382 | if (measureTiming == V1_2::MeasureTiming::YES) |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 383 | { |
| 384 | driverEnd = Now(); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 385 | V1_2::Timing timing; |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 386 | timing.timeOnDevice = MicrosecondsDuration(deviceEnd, deviceStart); |
| 387 | timing.timeInDriver = MicrosecondsDuration(driverEnd, driverStart); |
Kevin DuBois | 1866182 | 2020-08-26 15:02:00 -0700 | [diff] [blame] | 388 | ALOGV("ArmnnPreparedModel_1_2::executeSynchronously timing Device = %" PRIu64 " Driver = %" PRIu64, |
| 389 | timing.timeOnDevice, timing.timeInDriver); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 390 | cb(V1_0::ErrorStatus::NONE, outputShapes, timing); |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 391 | } |
| 392 | else |
| 393 | { |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 394 | cb(V1_0::ErrorStatus::NONE, outputShapes, g_NoTiming); |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 395 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 396 | return Void(); |
| 397 | } |
| 398 | |
Jan Eilers | 43a430d | 2020-02-28 15:40:44 +0000 | [diff] [blame] | 399 | /// This class is strongly inspired by the default implementation in Android named DefaultBurstExecutorWithCache. |
| 400 | /// The original code is licensed under Apache-2.0 and can be found at the following link: |
| 401 | /// https://android.googlesource.com/platform/frameworks/ |
| 402 | /// ml/+/refs/tags/android-10.0.0_r20/nn/common/ExecutionBurstServer.cpp |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 403 | class ArmnnBurstExecutorWithCache : public ExecutionBurstServer::IBurstExecutorWithCache { |
| 404 | public: |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 405 | ArmnnBurstExecutorWithCache(V1_2::IPreparedModel* preparedModel) |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 406 | : m_PreparedModel(preparedModel) |
| 407 | {} |
| 408 | |
| 409 | bool isCacheEntryPresent(int32_t slot) const override |
| 410 | { |
| 411 | const auto it = m_MemoryCache.find(slot); |
| 412 | return (it != m_MemoryCache.end()) && it->second.valid(); |
| 413 | } |
| 414 | |
| 415 | void addCacheEntry(const hidl_memory& memory, int32_t slot) override |
| 416 | { |
| 417 | m_MemoryCache[slot] = memory; |
| 418 | } |
| 419 | |
| 420 | void removeCacheEntry(int32_t slot) override |
| 421 | { |
| 422 | m_MemoryCache.erase(slot); |
| 423 | } |
| 424 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 425 | std::tuple<V1_0::ErrorStatus, hidl_vec<V1_2::OutputShape>, V1_2::Timing> execute( |
| 426 | const V1_0::Request& request, const std::vector<int32_t>& slots, |
| 427 | V1_2::MeasureTiming measure) override |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 428 | { |
| 429 | ALOGV("ArmnnPreparedModel_1_2::BurstExecutorWithCache::execute"); |
| 430 | hidl_vec<hidl_memory> pools(slots.size()); |
| 431 | |
| 432 | std::transform(slots.begin(), slots.end(), pools.begin(), [this](int32_t slot) |
| 433 | { |
| 434 | return m_MemoryCache[slot]; |
| 435 | }); |
| 436 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 437 | V1_0::Request fullRequest = request; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 438 | fullRequest.pools = std::move(pools); |
| 439 | |
| 440 | // Setup Callback |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 441 | V1_0::ErrorStatus returnedStatus = V1_0::ErrorStatus::GENERAL_FAILURE; |
| 442 | hidl_vec<V1_2::OutputShape> returnedOutputShapes; |
| 443 | V1_2::Timing returnedTiming; |
| 444 | auto cb = [&returnedStatus, &returnedOutputShapes, &returnedTiming](V1_0::ErrorStatus status, |
| 445 | const hidl_vec<V1_2::OutputShape>& outputShapes, |
| 446 | const V1_2::Timing& timing) |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 447 | { |
| 448 | returnedStatus = status; |
| 449 | returnedOutputShapes = outputShapes; |
| 450 | returnedTiming = timing; |
| 451 | }; |
| 452 | |
| 453 | // Execute |
| 454 | ALOGV("ArmnnPreparedModel_1_2::BurstExecutorWithCache executing"); |
| 455 | const Return<void> ret = m_PreparedModel->executeSynchronously(fullRequest, measure, cb); |
| 456 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 457 | if (!ret.isOk() || returnedStatus != V1_0::ErrorStatus::NONE) |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 458 | { |
| 459 | ALOGE("ArmnnPreparedModel_1_2::BurstExecutorWithCache::error executing"); |
| 460 | } |
| 461 | return std::make_tuple(returnedStatus, std::move(returnedOutputShapes), returnedTiming); |
| 462 | } |
| 463 | |
| 464 | private: |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 465 | V1_2::IPreparedModel* const m_PreparedModel; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 466 | std::map<int, hidl_memory> m_MemoryCache; |
| 467 | }; |
| 468 | |
| 469 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 470 | template<typename HalVersion> |
| 471 | Return<void> ArmnnPreparedModel_1_2<HalVersion>::configureExecutionBurst( |
| 472 | const sp<V1_2::IBurstCallback>& callback, |
| 473 | const MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel, |
| 474 | const MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel, |
| 475 | V1_2::IPreparedModel::configureExecutionBurst_cb cb) |
| 476 | { |
| 477 | ALOGV("ArmnnPreparedModel_1_2::configureExecutionBurst"); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 478 | const std::shared_ptr<ArmnnBurstExecutorWithCache> executorWithCache = |
| 479 | std::make_shared<ArmnnBurstExecutorWithCache>(this); |
| 480 | const sp<V1_2::IBurstContext> burst = ExecutionBurstServer::create(callback, |
| 481 | requestChannel, |
| 482 | resultChannel, |
| 483 | executorWithCache); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 484 | |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 485 | if (burst == nullptr) |
| 486 | { |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 487 | cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}); |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 488 | } |
| 489 | else |
| 490 | { |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 491 | cb(V1_0::ErrorStatus::NONE, burst); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 492 | } |
| 493 | return Void(); |
| 494 | } |
| 495 | |
| 496 | template<typename HalVersion> |
| 497 | void ArmnnPreparedModel_1_2<HalVersion>::ExecuteGraph( |
| 498 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 499 | std::shared_ptr<armnn::InputTensors>& pInputTensors, |
| 500 | std::shared_ptr<armnn::OutputTensors>& pOutputTensors, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 501 | ArmnnCallback_1_2 cb) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 502 | { |
| 503 | ALOGV("ArmnnPreparedModel_1_2::ExecuteGraph(...)"); |
| 504 | |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 505 | TimePoint driverEnd, deviceStart, deviceEnd; |
| 506 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 507 | DumpTensorsIfRequired("Input", *pInputTensors); |
| 508 | |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 509 | std::vector<std::pair<int, armnn::Tensor> > outputTensors = *pOutputTensors.get(); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 510 | std::vector<V1_2::OutputShape> outputShapes(outputTensors.size()); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 511 | |
| 512 | for (unsigned int i = 0; i < outputTensors.size(); i++) |
| 513 | { |
| 514 | std::pair<int, armnn::Tensor> outputTensorPair = outputTensors[i]; |
| 515 | const armnn::Tensor outputTensor = outputTensorPair.second; |
| 516 | const armnn::TensorInfo outputTensorInfo = outputTensor.GetInfo(); |
| 517 | |
| 518 | hidl_vec<uint32_t> dimensions; |
| 519 | |
| 520 | armnn::TensorShape tensorShape = outputTensorInfo.GetShape(); |
| 521 | const unsigned int numDims = tensorShape.GetNumDimensions(); |
| 522 | dimensions.resize(numDims); |
| 523 | |
| 524 | for (unsigned int outputIdx = 0u; outputIdx < numDims; ++outputIdx) |
| 525 | { |
| 526 | dimensions[outputIdx] = tensorShape[outputIdx]; |
| 527 | } |
| 528 | outputShapes[i].dimensions = dimensions; |
| 529 | outputShapes[i].isSufficient = true; |
| 530 | } |
| 531 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 532 | // run it |
| 533 | try |
| 534 | { |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 535 | if (cb.measureTiming == V1_2::MeasureTiming::YES) |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 536 | { |
| 537 | deviceStart = Now(); |
| 538 | } |
| 539 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 540 | armnn::Status status = m_Runtime->EnqueueWorkload(m_NetworkId, *pInputTensors, *pOutputTensors); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 541 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 542 | if (cb.measureTiming == V1_2::MeasureTiming::YES) |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 543 | { |
| 544 | deviceEnd = Now(); |
| 545 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 546 | if (status != armnn::Status::Success) |
| 547 | { |
| 548 | ALOGW("EnqueueWorkload failed"); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 549 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 550 | "ArmnnPreparedModel_1_2::ExecuteGraph"); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 551 | return; |
| 552 | } |
| 553 | } |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 554 | catch (armnn::Exception& e) |
| 555 | { |
| 556 | ALOGW("armnn:Exception caught from EnqueueWorkload: %s", e.what()); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 557 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::ExecuteGraph"); |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 558 | return; |
| 559 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 560 | catch (std::exception& e) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 561 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 562 | ALOGE("std::exception caught from EnqueueWorkload: %s", e.what()); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 563 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::ExecuteGraph"); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 564 | return; |
| 565 | } |
| 566 | |
| 567 | DumpTensorsIfRequired("Output", *pOutputTensors); |
| 568 | |
| 569 | // Commit output buffers. |
| 570 | // Note that we update *all* pools, even if they aren't actually used as outputs - |
| 571 | // this is simpler and is what the CpuExecutor does. |
| 572 | for (android::nn::RunTimePoolInfo& pool : *pMemPools) |
| 573 | { |
Kevin DuBois | 03993ba | 2020-08-26 14:07:41 -0700 | [diff] [blame] | 574 | pool.flush(); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 575 | } |
| 576 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 577 | if (cb.measureTiming == V1_2::MeasureTiming::YES) |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 578 | { |
| 579 | driverEnd = Now(); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 580 | V1_2::Timing timing; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 581 | timing.timeOnDevice = MicrosecondsDuration(deviceEnd, deviceStart); |
| 582 | timing.timeInDriver = MicrosecondsDuration(driverEnd, cb.driverStart); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 583 | cb.callback(V1_0::ErrorStatus::NONE, outputShapes, timing, "ExecuteGraph"); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 584 | } else { |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 585 | cb.callback(V1_0::ErrorStatus::NONE, outputShapes, g_NoTiming, "ExecuteGraph"); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 586 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | template<typename HalVersion> |
| 590 | bool ArmnnPreparedModel_1_2<HalVersion>::ExecuteWithDummyInputs() |
| 591 | { |
| 592 | std::vector<std::vector<char>> storage; |
| 593 | armnn::InputTensors inputTensors; |
| 594 | for (unsigned int i = 0; i < m_Model.inputIndexes.size(); i++) |
| 595 | { |
| 596 | const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i); |
| 597 | storage.emplace_back(inputTensorInfo.GetNumBytes()); |
| 598 | const armnn::ConstTensor inputTensor(inputTensorInfo, storage.back().data()); |
| 599 | |
| 600 | inputTensors.emplace_back(i, inputTensor); |
| 601 | } |
| 602 | |
| 603 | armnn::OutputTensors outputTensors; |
| 604 | for (unsigned int i = 0; i < m_Model.outputIndexes.size(); i++) |
| 605 | { |
| 606 | const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i); |
| 607 | storage.emplace_back(outputTensorInfo.GetNumBytes()); |
| 608 | const armnn::Tensor outputTensor(outputTensorInfo, storage.back().data()); |
| 609 | |
| 610 | outputTensors.emplace_back(i, outputTensor); |
| 611 | } |
| 612 | |
| 613 | try |
| 614 | { |
| 615 | armnn::Status status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors); |
| 616 | if (status != armnn::Status::Success) |
| 617 | { |
| 618 | ALOGW("ExecuteWithDummyInputs: EnqueueWorkload failed"); |
| 619 | return false; |
| 620 | } |
| 621 | } |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 622 | catch (armnn::Exception& e) |
| 623 | { |
| 624 | ALOGW("ExecuteWithDummyInputs: armnn::Exception caught from EnqueueWorkload: %s", e.what()); |
| 625 | return false; |
| 626 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 627 | catch (std::exception& e) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 628 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 629 | ALOGE("ExecuteWithDummyInputs: std::exception caught from EnqueueWorkload: %s", e.what()); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 630 | return false; |
| 631 | } |
| 632 | return true; |
| 633 | } |
| 634 | |
| 635 | template<typename HalVersion> |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 636 | Return <V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::Execute(const V1_0::Request& request, |
| 637 | V1_2::MeasureTiming measureTiming, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 638 | armnnExecuteCallback_1_2 callback) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 639 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 640 | TimePoint driverStart; |
| 641 | |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 642 | if (measureTiming == V1_2::MeasureTiming::YES) |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 643 | { |
| 644 | driverStart = Now(); |
| 645 | } |
| 646 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 647 | ALOGV("ArmnnPreparedModel_1_2::execute(): %s", GetModelSummary(m_Model).c_str()); |
| 648 | m_RequestCount++; |
| 649 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 650 | if (!android::nn::validateRequest(request, m_Model)) |
| 651 | { |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 652 | callback(V1_0::ErrorStatus::INVALID_ARGUMENT, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 653 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | if (!m_RequestInputsAndOutputsDumpDir.empty()) |
| 657 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 658 | ALOGD("Dumping inputs and outputs for request %" PRIuPTR, reinterpret_cast<std::uintptr_t>(&callback)); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // allocate the tensors on the heap, as they are passed to the request thread |
| 662 | auto pInputTensors = std::make_shared<armnn::InputTensors>(); |
| 663 | auto pOutputTensors = std::make_shared<armnn::OutputTensors>(); |
| 664 | |
| 665 | // map the memory pool into shared pointers |
| 666 | // use a shared memory pools vector on the heap, as it is passed to the request thread |
| 667 | auto pMemPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>(); |
| 668 | |
| 669 | if (!setRunTimePoolInfosFromHidlMemories(pMemPools.get(), request.pools)) |
| 670 | { |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 671 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 672 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | // add the inputs and outputs with their data |
| 676 | try |
| 677 | { |
| 678 | pInputTensors->reserve(request.inputs.size()); |
| 679 | for (unsigned int i = 0; i < request.inputs.size(); i++) |
| 680 | { |
| 681 | const auto& inputArg = request.inputs[i]; |
| 682 | |
| 683 | const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i); |
| 684 | const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, *pMemPools); |
| 685 | |
| 686 | if (inputTensor.GetMemoryArea() == nullptr) |
| 687 | { |
| 688 | ALOGE("Cannot execute request. Error converting request input %u to tensor", i); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 689 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 690 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | pInputTensors->emplace_back(i, inputTensor); |
| 694 | } |
| 695 | |
| 696 | pOutputTensors->reserve(request.outputs.size()); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 697 | std::vector<V1_2::OutputShape> outputShapes(request.outputs.size()); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 698 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 699 | for (unsigned int i = 0; i < request.outputs.size(); i++) |
| 700 | { |
| 701 | const auto& outputArg = request.outputs[i]; |
| 702 | |
| 703 | const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i); |
| 704 | const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, *pMemPools); |
| 705 | if (outputTensor.GetMemoryArea() == nullptr) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 706 | { |
| 707 | ALOGE("Cannot execute request. Error converting request output %u to tensor", i); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 708 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 709 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 710 | } |
| 711 | |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 712 | const size_t outputSize = outputTensorInfo.GetNumBytes(); |
Kevin DuBois | 2099015 | 2020-11-03 10:27:42 -0800 | [diff] [blame] | 713 | const size_t bufferSize = pMemPools->at(outputArg.location.poolIndex).getSize(); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 714 | pOutputTensors->emplace_back(i, outputTensor); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 715 | |
| 716 | hidl_vec<uint32_t> dimensions; |
| 717 | |
| 718 | armnn::TensorShape tensorShape = outputTensorInfo.GetShape(); |
| 719 | const unsigned int numDims = tensorShape.GetNumDimensions(); |
| 720 | dimensions.resize(numDims); |
| 721 | |
| 722 | for (unsigned int outputIdx = 0u; outputIdx < numDims; ++outputIdx) |
| 723 | { |
| 724 | dimensions[outputIdx] = tensorShape[outputIdx]; |
| 725 | } |
| 726 | outputShapes[i].dimensions = dimensions; |
| 727 | outputShapes[i].isSufficient = bufferSize >= outputSize; |
| 728 | |
| 729 | if (bufferSize < outputSize) |
| 730 | { |
| 731 | ALOGW("ArmnnPreparedModel_1_2::Execute failed"); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 732 | callback(V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 733 | outputShapes, |
| 734 | g_NoTiming, |
| 735 | "ArmnnPreparedModel_1_2::Execute"); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 736 | return V1_0::ErrorStatus::NONE; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 737 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 738 | } |
| 739 | } |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 740 | catch (armnn::Exception& e) |
| 741 | { |
| 742 | ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what()); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 743 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 744 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 745 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 746 | catch (std::exception& e) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 747 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 748 | ALOGE("std::exception caught while preparing for EnqueueWorkload: %s", e.what()); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 749 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 750 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | ALOGV("ArmnnPreparedModel_1_2::execute(...) before PostMsg"); |
| 754 | // post the request for asynchronous execution |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 755 | ArmnnCallback_1_2 armnnCb; |
| 756 | armnnCb.callback = callback; |
| 757 | armnnCb.measureTiming = measureTiming; |
| 758 | armnnCb.driverStart = driverStart; |
| 759 | m_RequestThread.PostMsg(this, pMemPools, pInputTensors, pOutputTensors, armnnCb); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 760 | ALOGV("ArmnnPreparedModel_1_2::execute(...) after PostMsg"); |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame^] | 761 | return V1_0::ErrorStatus::NONE; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 762 | } |
| 763 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 764 | #ifdef ARMNN_ANDROID_NN_V1_2 |
| 765 | template class ArmnnPreparedModel_1_2<hal_1_2::HalPolicy>; |
| 766 | #endif |
| 767 | |
| 768 | } // namespace armnn_driver |