Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1 | // |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #define LOG_TAG "ArmnnDriver" |
| 7 | |
| 8 | #include "ArmnnPreparedModel_1_2.hpp" |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 9 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 10 | #include "Utils.hpp" |
| 11 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 12 | #include <log/log.h> |
| 13 | #include <OperationsUtils.h> |
| 14 | #include <ExecutionBurstServer.h> |
| 15 | #include <ValidateHal.h> |
| 16 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 17 | #include <cinttypes> |
| 18 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 19 | #ifdef ARMNN_ANDROID_S |
| 20 | #include <LegacyUtils.h> |
| 21 | #endif |
| 22 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 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 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [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 May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 44 | V1_0::ErrorStatus errorStatus, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 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 May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 59 | V1_0::ErrorStatus errorStatus, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 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 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [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 | { |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 86 | if (requestArg.dimensions[d] != 0 && requestArg.dimensions[d] != tensorInfo.GetShape()[d]) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 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 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [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> |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 123 | RequestThread<ArmnnPreparedModel_1_2, HalVersion, CallbackContext_1_2> |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 124 | ArmnnPreparedModel_1_2<HalVersion>::m_RequestThread; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 125 | |
| 126 | template<typename HalVersion> |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 127 | std::unique_ptr<armnn::Threadpool> ArmnnPreparedModel_1_2<HalVersion>::m_Threadpool(nullptr); |
| 128 | |
| 129 | template<typename HalVersion> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 130 | template<typename TensorBindingCollection> |
| 131 | void ArmnnPreparedModel_1_2<HalVersion>::DumpTensorsIfRequired(char const* tensorNamePrefix, |
| 132 | const TensorBindingCollection& tensorBindings) |
| 133 | { |
| 134 | if (!m_RequestInputsAndOutputsDumpDir.empty()) |
| 135 | { |
Colm Donelan | 08d9a1c | 2020-09-09 17:56:55 +0100 | [diff] [blame] | 136 | const std::string requestName = std::to_string(m_NetworkId) + "_" + std::to_string(m_RequestCount) + ".dump"; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 137 | for (std::size_t i = 0u; i < tensorBindings.size(); ++i) |
| 138 | { |
| 139 | DumpTensor(m_RequestInputsAndOutputsDumpDir, |
| 140 | requestName, |
| 141 | BuildTensorName(tensorNamePrefix, i), |
| 142 | tensorBindings[i].second); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | template<typename HalVersion> |
| 148 | ArmnnPreparedModel_1_2<HalVersion>::ArmnnPreparedModel_1_2(armnn::NetworkId networkId, |
| 149 | armnn::IRuntime* runtime, |
| 150 | const V1_2::Model& model, |
| 151 | const std::string& requestInputsAndOutputsDumpDir, |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 152 | const bool gpuProfilingEnabled, |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 153 | const bool asyncModelExecutionEnabled, |
| 154 | const unsigned int numberOfThreads) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 155 | : m_NetworkId(networkId) |
| 156 | , m_Runtime(runtime) |
| 157 | , m_Model(model) |
| 158 | , m_RequestCount(0) |
| 159 | , m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir) |
| 160 | , m_GpuProfilingEnabled(gpuProfilingEnabled) |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 161 | , m_AsyncModelExecutionEnabled(asyncModelExecutionEnabled) |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 162 | , m_PreparedFromCache(false) |
| 163 | { |
| 164 | // Enable profiling if required. |
| 165 | m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled); |
| 166 | |
| 167 | if (m_AsyncModelExecutionEnabled) |
| 168 | { |
| 169 | std::vector<std::shared_ptr<armnn::IWorkingMemHandle>> memHandles; |
| 170 | for (unsigned int i=0; i < numberOfThreads; ++i) |
| 171 | { |
| 172 | memHandles.emplace_back(m_Runtime->CreateWorkingMemHandle(networkId)); |
| 173 | } |
| 174 | |
| 175 | if (!m_Threadpool) |
| 176 | { |
| 177 | m_Threadpool = std::make_unique<armnn::Threadpool>(numberOfThreads, runtime, memHandles); |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | m_Threadpool->LoadMemHandles(memHandles); |
| 182 | } |
| 183 | |
| 184 | m_WorkingMemHandle = memHandles.back(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | template<typename HalVersion> |
| 189 | ArmnnPreparedModel_1_2<HalVersion>::ArmnnPreparedModel_1_2(armnn::NetworkId networkId, |
| 190 | armnn::IRuntime* runtime, |
| 191 | const std::string& requestInputsAndOutputsDumpDir, |
| 192 | const bool gpuProfilingEnabled, |
| 193 | const bool asyncModelExecutionEnabled, |
| 194 | const unsigned int numberOfThreads, |
| 195 | const bool preparedFromCache) |
| 196 | : m_NetworkId(networkId) |
| 197 | , m_Runtime(runtime) |
| 198 | , m_RequestCount(0) |
| 199 | , m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir) |
| 200 | , m_GpuProfilingEnabled(gpuProfilingEnabled) |
| 201 | , m_AsyncModelExecutionEnabled(asyncModelExecutionEnabled) |
| 202 | , m_PreparedFromCache(preparedFromCache) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 203 | { |
| 204 | // Enable profiling if required. |
| 205 | m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled); |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 206 | |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 207 | if (m_AsyncModelExecutionEnabled) |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 208 | { |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 209 | std::vector<std::shared_ptr<armnn::IWorkingMemHandle>> memHandles; |
Finn Williams | d27c13b | 2021-06-25 10:06:09 +0100 | [diff] [blame] | 210 | for (unsigned int i=0; i < numberOfThreads; ++i) |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 211 | { |
| 212 | memHandles.emplace_back(m_Runtime->CreateWorkingMemHandle(networkId)); |
| 213 | } |
| 214 | |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 215 | if (!m_Threadpool) |
| 216 | { |
| 217 | m_Threadpool = std::make_unique<armnn::Threadpool>(numberOfThreads, runtime, memHandles); |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | m_Threadpool->LoadMemHandles(memHandles); |
| 222 | } |
| 223 | |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 224 | m_WorkingMemHandle = memHandles.back(); |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 225 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | template<typename HalVersion> |
| 229 | ArmnnPreparedModel_1_2<HalVersion>::~ArmnnPreparedModel_1_2() |
| 230 | { |
| 231 | // Get a hold of the profiler used by this model. |
| 232 | std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId); |
| 233 | |
| 234 | // Unload the network associated with this model. |
| 235 | m_Runtime->UnloadNetwork(m_NetworkId); |
| 236 | |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 237 | // Unload the network memhandles from the threadpool |
| 238 | if (m_AsyncModelExecutionEnabled) |
| 239 | { |
| 240 | m_Threadpool->UnloadMemHandles(m_NetworkId); |
| 241 | } |
| 242 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 243 | // Dump the profiling info to a file if required. |
| 244 | DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId, profiler.get()); |
| 245 | } |
| 246 | |
| 247 | template<typename HalVersion> |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 248 | 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] | 249 | const ::android::sp<V1_0::IExecutionCallback>& callback) |
| 250 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 251 | if (callback.get() == nullptr) |
| 252 | { |
| 253 | ALOGE("ArmnnPreparedModel_1_2::execute invalid callback passed"); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 254 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 257 | auto cb = [callback](V1_0::ErrorStatus errorStatus, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 258 | std::vector<V1_2::OutputShape> outputShapes, |
| 259 | const V1_2::Timing& timing, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 260 | std::string callingFunction) |
| 261 | { |
| 262 | NotifyCallbackAndCheck(callback, errorStatus, outputShapes, timing, callingFunction); |
| 263 | }; |
| 264 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 265 | return Execute(request, V1_2::MeasureTiming::NO, cb); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | template<typename HalVersion> |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 269 | Return <V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::execute_1_2( |
| 270 | const V1_0::Request& request, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 271 | V1_2::MeasureTiming measureTiming, |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 272 | const sp<V1_2::IExecutionCallback>& callback) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 273 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 274 | if (callback.get() == nullptr) |
| 275 | { |
| 276 | ALOGE("ArmnnPreparedModel_1_2::execute_1_2 invalid callback passed"); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 277 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 280 | auto cb = [callback](V1_0::ErrorStatus errorStatus, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 281 | std::vector<V1_2::OutputShape> outputShapes, |
| 282 | const V1_2::Timing& timing, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 283 | std::string callingFunction) |
| 284 | { |
| 285 | NotifyCallbackAndCheck(callback, errorStatus, outputShapes, timing, callingFunction); |
| 286 | }; |
| 287 | |
| 288 | return Execute(request, measureTiming, cb); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 291 | template<typename HalVersion> |
| 292 | Return<V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::PrepareMemoryForInputs( |
| 293 | armnn::InputTensors& inputs, |
| 294 | const V1_0::Request& request, |
| 295 | const std::vector<android::nn::RunTimePoolInfo>& memPools) |
| 296 | { |
| 297 | inputs.reserve(request.inputs.size()); |
| 298 | for (unsigned int i = 0; i < request.inputs.size(); i++) |
| 299 | { |
| 300 | const auto& inputArg = request.inputs[i]; |
| 301 | |
Cathal Corbett | e27d4e8 | 2021-10-28 12:28:35 +0100 | [diff] [blame] | 302 | armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i); |
| 303 | // inputs (of type InputTensors) is composed of a vector of ConstTensors. |
| 304 | // Therefore, set all TensorInfo isConstant parameters of input Tensors to true. |
| 305 | inputTensorInfo.SetConstant(); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 306 | const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, memPools); |
| 307 | |
| 308 | if (inputTensor.GetMemoryArea() == nullptr) |
| 309 | { |
| 310 | ALOGE("Cannot execute request. Error converting request input %u to tensor", i); |
| 311 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 312 | } |
| 313 | |
| 314 | inputs.emplace_back(i, inputTensor); |
| 315 | } |
| 316 | |
| 317 | return V1_0::ErrorStatus::NONE; |
| 318 | } |
| 319 | |
| 320 | template<typename HalVersion> |
| 321 | Return<V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::PrepareMemoryForOutputs( |
| 322 | armnn::OutputTensors& outputs, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 323 | std::vector<V1_2::OutputShape> &outputShapes, |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 324 | const V1_0::Request& request, |
| 325 | const std::vector<android::nn::RunTimePoolInfo>& memPools) |
| 326 | { |
| 327 | outputs.reserve(request.outputs.size()); |
| 328 | for (unsigned int i = 0; i < request.outputs.size(); i++) |
| 329 | { |
| 330 | const auto& outputArg = request.outputs[i]; |
| 331 | |
| 332 | const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i); |
| 333 | const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, memPools); |
| 334 | if (outputTensor.GetMemoryArea() == nullptr) |
| 335 | { |
| 336 | ALOGE("Cannot execute request. Error converting request output %u to tensor", i); |
| 337 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 338 | } |
| 339 | |
| 340 | const size_t outputSize = outputTensorInfo.GetNumBytes(); |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 341 | |
| 342 | if (outputArg.location.length < outputSize) |
| 343 | { |
| 344 | ALOGW("ArmnnPreparedModel_1_2::Execute failed: outputArg.location.length < outputSize"); |
| 345 | return V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE; |
| 346 | } |
| 347 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 348 | #if !defined(ARMNN_ANDROID_S) |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 349 | const size_t bufferSize = memPools.at(outputArg.location.poolIndex).getHidlMemory().size(); |
| 350 | if (bufferSize < outputSize) |
| 351 | { |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 352 | ALOGW("ArmnnPreparedModel_1_2::Execute failed: bufferSize < outputSize"); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 353 | return V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE; |
| 354 | } |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 355 | #else |
Kevin May | dc873f6 | 2021-06-14 11:21:11 +0100 | [diff] [blame] | 356 | const size_t bufferSize = memPools.at(outputArg.location.poolIndex).getSize(); |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 357 | if (bufferSize < outputSize) |
| 358 | { |
| 359 | ALOGW("ArmnnPreparedModel_1_2::Execute failed bufferSize (%s) < outputSize (%s)", |
| 360 | std::to_string(bufferSize).c_str(), std::to_string(outputSize).c_str()); |
| 361 | outputShapes[i].isSufficient = false; |
| 362 | return V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE; |
| 363 | } |
| 364 | #endif |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 365 | outputs.emplace_back(i, outputTensor); |
| 366 | outputShapes[i] = ComputeShape(outputTensorInfo); |
| 367 | } |
| 368 | |
| 369 | return V1_0::ErrorStatus::NONE; |
| 370 | } |
| 371 | |
| 372 | template<typename HalVersion> |
| 373 | Return<V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::PrepareMemoryForIO( |
| 374 | armnn::InputTensors& inputs, |
| 375 | armnn::OutputTensors& outputs, |
| 376 | std::vector<android::nn::RunTimePoolInfo>& memPools, |
| 377 | const V1_0::Request& request, |
| 378 | CallbackAsync_1_2 callback) |
| 379 | { |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 380 | #if !defined(ARMNN_ANDROID_S) |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 381 | if (!setRunTimePoolInfosFromHidlMemories(&memPools, request.pools)) |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 382 | #else |
| 383 | if (!setRunTimePoolInfosFromCanonicalMemories(&memPools, uncheckedConvert(request.pools))) |
| 384 | #endif |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 385 | { |
| 386 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 387 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 388 | } |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 389 | // add the inputs and outputs with their data |
| 390 | try |
| 391 | { |
| 392 | if (PrepareMemoryForInputs(inputs, request, memPools) != V1_0::ErrorStatus::NONE) |
| 393 | { |
| 394 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 395 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 396 | } |
| 397 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 398 | std::vector<V1_2::OutputShape> outputShapes(request.outputs.size()); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 399 | |
| 400 | auto errorStatus = PrepareMemoryForOutputs(outputs, outputShapes, request, memPools); |
| 401 | if (errorStatus != V1_0::ErrorStatus::NONE) |
| 402 | { |
| 403 | callback(errorStatus, |
| 404 | outputShapes, |
| 405 | g_NoTiming, |
| 406 | "ArmnnPreparedModel_1_2::Execute"); |
| 407 | return errorStatus; |
| 408 | } |
| 409 | } |
| 410 | catch (armnn::Exception& e) |
| 411 | { |
| 412 | ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what()); |
| 413 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 414 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 415 | } |
| 416 | catch (std::exception& e) |
| 417 | { |
| 418 | ALOGE("std::exception caught while preparing for EnqueueWorkload: %s", e.what()); |
| 419 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 420 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 421 | } |
| 422 | |
| 423 | return V1_0::ErrorStatus::NONE; |
| 424 | } |
| 425 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 426 | template<typename HalVersion> |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 427 | Return<void> ArmnnPreparedModel_1_2<HalVersion>::executeSynchronously(const V1_0::Request& request, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 428 | V1_2::MeasureTiming measureTiming, |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 429 | executeSynchronously_cb cb) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 430 | { |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 431 | if (!m_PreparedFromCache) |
| 432 | { |
| 433 | ALOGV("ArmnnPreparedModel_1_2::executeSynchronously(): %s", GetModelSummary(m_Model).c_str()); |
| 434 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 435 | m_RequestCount++; |
| 436 | |
| 437 | if (cb == nullptr) |
| 438 | { |
| 439 | ALOGE("ArmnnPreparedModel_1_2::executeSynchronously invalid callback passed"); |
| 440 | return Void(); |
| 441 | } |
| 442 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 443 | TimePoint driverStart; |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 444 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 445 | if (measureTiming == V1_2::MeasureTiming::YES) |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 446 | { |
| 447 | driverStart = Now(); |
| 448 | } |
| 449 | |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 450 | if (!m_PreparedFromCache && !android::nn::validateRequest(request, m_Model)) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 451 | { |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 452 | ALOGE("ArmnnPreparedModel_1_2::executeSynchronously invalid request model"); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 453 | cb(V1_0::ErrorStatus::INVALID_ARGUMENT, {}, g_NoTiming); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 454 | return Void(); |
| 455 | } |
| 456 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 457 | auto cbWrapper = [cb](V1_0::ErrorStatus errorStatus, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 458 | std::vector<V1_2::OutputShape> outputShapes, |
| 459 | const V1_2::Timing& timing, |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 460 | std::string) |
| 461 | { |
| 462 | cb(errorStatus, outputShapes, timing); |
| 463 | }; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 464 | |
| 465 | // map the memory pool into shared pointers |
| 466 | // use a shared memory pools vector on the heap, as it is passed to the request thread |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 467 | auto memPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>(); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 468 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 469 | // allocate the tensors on the heap, as they are passed to the request thread |
| 470 | auto inputs = std::make_shared<armnn::InputTensors>(); |
| 471 | auto outputs = std::make_shared<armnn::OutputTensors>(); |
| 472 | |
| 473 | auto prepareStatus = PrepareMemoryForIO(*inputs, *outputs, *memPools, request, cbWrapper); |
| 474 | if (prepareStatus != V1_0::ErrorStatus::NONE) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 475 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 476 | return Void(); |
| 477 | } |
| 478 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 479 | ALOGV("ArmnnPreparedModel_1_2::executeSynchronously() before Execution"); |
| 480 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 481 | CallbackContext_1_2 cbCtx; |
| 482 | cbCtx.callback = cbWrapper; |
| 483 | cbCtx.ctx.measureTimings = measureTiming; |
| 484 | cbCtx.ctx.driverStart = driverStart; |
| 485 | ExecuteGraph(memPools, *inputs, *outputs, cbCtx); |
| 486 | |
| 487 | return Void(); |
| 488 | } |
| 489 | |
| 490 | template<typename HalVersion> |
| 491 | template<typename CallbackContext> |
| 492 | bool ArmnnPreparedModel_1_2<HalVersion>::ExecuteGraph( |
| 493 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 494 | armnn::InputTensors& inputTensors, |
| 495 | armnn::OutputTensors& outputTensors, |
| 496 | CallbackContext cb) |
| 497 | { |
| 498 | ALOGV("ArmnnPreparedModel_1_2::ExecuteGraph(...)"); |
| 499 | |
| 500 | TimePoint driverEnd, deviceStart, deviceEnd; |
| 501 | |
| 502 | DumpTensorsIfRequired("Input", inputTensors); |
| 503 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 504 | std::vector<V1_2::OutputShape> outputShapes(outputTensors.size()); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 505 | for (unsigned int i = 0; i < outputTensors.size(); i++) |
| 506 | { |
| 507 | std::pair<int, armnn::Tensor> outputTensorPair = outputTensors[i]; |
| 508 | const armnn::Tensor outputTensor = outputTensorPair.second; |
| 509 | const armnn::TensorInfo outputTensorInfo = outputTensor.GetInfo(); |
| 510 | |
| 511 | outputShapes[i] = ComputeShape(outputTensorInfo); |
| 512 | } |
| 513 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 514 | // run it |
| 515 | try |
| 516 | { |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 517 | if (cb.ctx.measureTimings == V1_2::MeasureTiming::YES) |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 518 | { |
| 519 | deviceStart = Now(); |
| 520 | } |
| 521 | |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 522 | armnn::Status status; |
| 523 | if (m_AsyncModelExecutionEnabled) |
| 524 | { |
| 525 | ALOGW("ArmnnPreparedModel_1_2::ExecuteGraph m_AsyncModelExecutionEnabled true"); |
| 526 | status = m_Runtime->Execute(*m_WorkingMemHandle, inputTensors, outputTensors); |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | ALOGW("ArmnnPreparedModel_1_2::ExecuteGraph m_AsyncModelExecutionEnabled false"); |
| 531 | status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors); |
| 532 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 533 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 534 | if (cb.ctx.measureTimings == V1_2::MeasureTiming::YES) |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 535 | { |
| 536 | deviceEnd = Now(); |
| 537 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 538 | if (status != armnn::Status::Success) |
| 539 | { |
| 540 | ALOGW("EnqueueWorkload failed"); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 541 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, |
| 542 | "ArmnnPreparedModel_1_2::ExecuteGraph"); |
| 543 | return false; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 544 | } |
| 545 | } |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 546 | catch (armnn::Exception& e) |
| 547 | { |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 548 | ALOGW("armnn:Exception caught from EnqueueWorkload: %s", e.what()); |
| 549 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::ExecuteGraph"); |
| 550 | return false; |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 551 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 552 | catch (std::exception& e) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 553 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 554 | ALOGE("std::exception caught from EnqueueWorkload: %s", e.what()); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 555 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::ExecuteGraph"); |
| 556 | return false; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 557 | } |
| 558 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 559 | CommitPools(*pMemPools); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 560 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 561 | DumpTensorsIfRequired("Output", outputTensors); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 562 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 563 | if (cb.ctx.measureTimings == V1_2::MeasureTiming::YES) |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 564 | { |
| 565 | driverEnd = Now(); |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 566 | V1_2::Timing timing; |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 567 | timing.timeOnDevice = MicrosecondsDuration(deviceEnd, deviceStart); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 568 | timing.timeInDriver = MicrosecondsDuration(driverEnd, cb.ctx.driverStart); |
| 569 | ALOGV("ArmnnPreparedModel_1_2::execute timing - Device = %lu Driver = %lu", timing.timeOnDevice, |
| 570 | timing.timeInDriver); |
| 571 | cb.callback(V1_0::ErrorStatus::NONE, outputShapes, timing, "ArmnnPreparedModel_1_2::ExecuteGraph"); |
| 572 | } else { |
| 573 | cb.callback(V1_0::ErrorStatus::NONE, outputShapes, g_NoTiming, "ArmnnPreparedModel_1_2::ExecuteGraph"); |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 574 | } |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 575 | |
| 576 | return true; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 577 | } |
| 578 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 579 | template<typename HalVersion> |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 580 | bool ArmnnPreparedModel_1_2<HalVersion>::ExecuteWithDummyInputs(unsigned int numInputs, unsigned int numOutputs) |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 581 | { |
| 582 | std::vector<std::vector<char>> storage; |
| 583 | armnn::InputTensors inputTensors; |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 584 | for (unsigned int i = 0; i < numInputs; i++) |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 585 | { |
Cathal Corbett | e27d4e8 | 2021-10-28 12:28:35 +0100 | [diff] [blame] | 586 | armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i); |
| 587 | // pInputTensors (of type InputTensors) is composed of a vector of ConstTensors. |
| 588 | // Therefore, set all TensorInfo isConstant parameters of input Tensors to true. |
| 589 | inputTensorInfo.SetConstant(); |
| 590 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 591 | storage.emplace_back(inputTensorInfo.GetNumBytes()); |
| 592 | const armnn::ConstTensor inputTensor(inputTensorInfo, storage.back().data()); |
| 593 | |
| 594 | inputTensors.emplace_back(i, inputTensor); |
| 595 | } |
| 596 | |
| 597 | armnn::OutputTensors outputTensors; |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 598 | for (unsigned int i = 0; i < numOutputs; i++) |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 599 | { |
| 600 | const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i); |
| 601 | storage.emplace_back(outputTensorInfo.GetNumBytes()); |
| 602 | const armnn::Tensor outputTensor(outputTensorInfo, storage.back().data()); |
| 603 | |
| 604 | outputTensors.emplace_back(i, outputTensor); |
| 605 | } |
| 606 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 607 | auto nullCallback = [](V1_0::ErrorStatus, std::vector<V1_2::OutputShape>, const V1_2::Timing&, std::string) {}; |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 608 | CallbackContext_1_2 callbackContext; |
| 609 | callbackContext.callback = nullCallback; |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 610 | callbackContext.ctx.measureTimings = V1_2::MeasureTiming::NO; |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 611 | auto memPools = std::make_shared<std::vector<::android::nn::RunTimePoolInfo>>(); |
| 612 | return ExecuteGraph(memPools, |
| 613 | inputTensors, |
| 614 | outputTensors, |
| 615 | callbackContext); |
| 616 | } |
| 617 | |
| 618 | template<typename HalVersion> |
| 619 | Return <V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::Execute(const V1_0::Request& request, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 620 | V1_2::MeasureTiming measureTiming, |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 621 | CallbackAsync_1_2 callback) |
| 622 | { |
| 623 | ExecutionContext_1_2 ctx; |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 624 | if (measureTiming == V1_2::MeasureTiming::YES) |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 625 | { |
| 626 | ctx.measureTimings = measureTiming; |
| 627 | ctx.driverStart = Now(); |
| 628 | } |
| 629 | |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 630 | if (!m_PreparedFromCache) |
| 631 | { |
| 632 | ALOGV("ArmnnPreparedModel_1_2::execute(): %s", GetModelSummary(m_Model).c_str()); |
| 633 | } |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 634 | m_RequestCount++; |
| 635 | |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 636 | if (!m_PreparedFromCache && !android::nn::validateRequest(request, m_Model)) |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 637 | { |
| 638 | callback(V1_0::ErrorStatus::INVALID_ARGUMENT, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 639 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
| 640 | } |
| 641 | |
| 642 | if (!m_RequestInputsAndOutputsDumpDir.empty()) |
| 643 | { |
| 644 | ALOGD("Dumping inputs and outputs for request %" PRIuPTR, reinterpret_cast<std::uintptr_t>(&callback)); |
| 645 | } |
| 646 | |
| 647 | // map the memory pool into shared pointers |
| 648 | // use a shared memory pools vector on the heap, as it is passed to the request thread |
| 649 | auto memPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>(); |
| 650 | |
| 651 | // allocate the tensors on the heap, as they are passed to the request thread |
| 652 | auto inputTensors = std::make_shared<armnn::InputTensors>(); |
| 653 | auto outputTensors = std::make_shared<armnn::OutputTensors>(); |
| 654 | |
| 655 | auto prepareStatus = PrepareMemoryForIO(*inputTensors, *outputTensors, *memPools, request, callback); |
| 656 | switch(prepareStatus) |
| 657 | { |
| 658 | case V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 659 | return V1_0::ErrorStatus::NONE; |
| 660 | case V1_0::ErrorStatus::GENERAL_FAILURE: |
| 661 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 662 | default: |
| 663 | {} |
| 664 | } |
| 665 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 666 | |
| 667 | // post the request for asynchronous execution |
| 668 | CallbackContext_1_2 cb; |
| 669 | cb.callback = callback; |
| 670 | cb.ctx = ctx; |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 671 | |
| 672 | if (m_AsyncModelExecutionEnabled) |
| 673 | { |
| 674 | ALOGV("ArmnnPreparedModel_1_2::execute(...) before ScheduleGraphForExecution"); |
| 675 | ScheduleGraphForExecution(memPools, inputTensors, outputTensors, cb); |
| 676 | ALOGV("ArmnnPreparedModel_1_2::execute(...) after ScheduleGraphForExecution"); |
| 677 | return V1_0::ErrorStatus::NONE; |
| 678 | } |
| 679 | |
| 680 | ALOGV("ArmnnPreparedModel_1_2::execute(...) before PostMsg"); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 681 | m_RequestThread.PostMsg(this, memPools, inputTensors, outputTensors, cb); |
| 682 | ALOGV("ArmnnPreparedModel_1_2::execute(...) after PostMsg"); |
| 683 | return V1_0::ErrorStatus::NONE; |
| 684 | } |
| 685 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 686 | template<typename HalVersion> |
| 687 | Return<void> ArmnnPreparedModel_1_2<HalVersion>::configureExecutionBurst( |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 688 | const sp<V1_2::IBurstCallback>& callback, |
| 689 | const MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel, |
| 690 | const MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel, |
| 691 | V1_2::IPreparedModel::configureExecutionBurst_cb cb) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 692 | { |
| 693 | ALOGV("ArmnnPreparedModel_1_2::configureExecutionBurst"); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 694 | const sp<V1_2::IBurstContext> burst = ExecutionBurstServer::create(callback, |
| 695 | requestChannel, |
| 696 | resultChannel, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 697 | this); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 698 | |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 699 | if (burst == nullptr) |
| 700 | { |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 701 | cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}); |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 702 | } |
| 703 | else |
| 704 | { |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 705 | cb(V1_0::ErrorStatus::NONE, burst); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 706 | } |
| 707 | return Void(); |
| 708 | } |
| 709 | |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 710 | /// Schedule the graph prepared from the request for execution |
| 711 | template<typename HalVersion> |
| 712 | template<typename CallbackContext> |
| 713 | void ArmnnPreparedModel_1_2<HalVersion>::ScheduleGraphForExecution( |
| 714 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 715 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 716 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 717 | CallbackContext callbackContext) |
| 718 | { |
| 719 | ALOGV("ArmnnPreparedModel_1_2::ScheduleGraphForExecution(...)"); |
| 720 | |
| 721 | DumpTensorsIfRequired("Input", *inputTensors); |
| 722 | |
| 723 | unsigned int outputTensorSize = outputTensors.get()->size(); |
| 724 | std::vector<V1_2::OutputShape> outputShapes(outputTensorSize); |
| 725 | for (unsigned int i = 0; i < outputTensorSize; i++) |
| 726 | { |
| 727 | std::pair<int, armnn::Tensor> outputTensorPair = outputTensors.get()->at(i); |
| 728 | const armnn::Tensor outputTensor = outputTensorPair.second; |
| 729 | const armnn::TensorInfo outputTensorInfo = outputTensor.GetInfo(); |
| 730 | |
| 731 | outputShapes[i] = ComputeShape(outputTensorInfo); |
| 732 | } |
| 733 | |
| 734 | auto tpCb = std::make_shared< |
| 735 | ArmnnThreadPoolCallback_1_2<CallbackContext_1_2>>(this, |
| 736 | pMemPools, |
| 737 | outputShapes, |
| 738 | inputTensors, |
| 739 | outputTensors, |
| 740 | callbackContext); |
| 741 | |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 742 | m_Threadpool->Schedule(m_NetworkId, |
| 743 | *tpCb->m_InputTensors, |
| 744 | *tpCb->m_OutputTensors, |
| 745 | armnn::QosExecPriority::Medium, |
| 746 | tpCb); |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 747 | ALOGV("ArmnnPreparedModel_1_2::ScheduleGraphForExecution end"); |
| 748 | } |
| 749 | |
| 750 | template<typename HalVersion> |
| 751 | template <typename CallbackContext> |
| 752 | void ArmnnPreparedModel_1_2<HalVersion>::ArmnnThreadPoolCallback_1_2<CallbackContext>::Notify( |
| 753 | armnn::Status status, armnn::InferenceTimingPair timeTaken) |
| 754 | { |
| 755 | ALOGV("ArmnnPreparedModel_1_2::ArmnnThreadPoolCallback_1_2 Notify"); |
| 756 | |
| 757 | TimePoint driverEnd; |
| 758 | |
| 759 | CommitPools(*m_MemPools); |
| 760 | |
| 761 | m_Model->DumpTensorsIfRequired("Output", *m_OutputTensors); |
| 762 | |
| 763 | if (status != armnn::Status::Success) |
| 764 | { |
| 765 | ALOGW("ArmnnThreadPoolCallback::Notify EnqueueWorkload failed"); |
| 766 | m_CallbackContext.callback( |
| 767 | V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel::ExecuteGraph"); |
| 768 | return; |
| 769 | } |
| 770 | |
| 771 | if (m_CallbackContext.ctx.measureTimings == V1_2::MeasureTiming::YES) |
| 772 | { |
| 773 | driverEnd = std::chrono::steady_clock::now(); |
| 774 | V1_2::Timing timing; |
| 775 | timing.timeOnDevice = MicrosecondsDuration(timeTaken.second, timeTaken.first); |
| 776 | timing.timeInDriver = MicrosecondsDuration(driverEnd, m_CallbackContext.ctx.driverStart); |
| 777 | ALOGV("ArmnnPreparedModel_1_2::execute timing - Device = %lu Driver = %lu", timing.timeOnDevice, |
| 778 | timing.timeInDriver); |
| 779 | m_CallbackContext.callback( |
| 780 | V1_0::ErrorStatus::NONE, m_OutputShapes, timing, "ArmnnPreparedModel_1_2::ExecuteGraph"); |
| 781 | } else { |
| 782 | m_CallbackContext.callback( |
| 783 | V1_0::ErrorStatus::NONE, m_OutputShapes, g_NoTiming, "ArmnnPreparedModel_1_2::ExecuteGraph"); |
| 784 | } |
| 785 | return; |
| 786 | } |
| 787 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 788 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 789 | template class ArmnnPreparedModel_1_2<hal_1_2::HalPolicy>; |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 790 | template bool ArmnnPreparedModel_1_2<hal_1_2::HalPolicy>::ExecuteGraph<CallbackContext_1_2>( |
| 791 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 792 | armnn::InputTensors& pInputTensors, |
| 793 | armnn::OutputTensors& pOutputTensors, |
| 794 | CallbackContext_1_2 cb); |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 795 | |
| 796 | template void ArmnnPreparedModel_1_2<hal_1_2::HalPolicy>::ScheduleGraphForExecution<CallbackContext_1_2>( |
| 797 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 798 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 799 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 800 | CallbackContext_1_2 callbackContext); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 801 | #endif |
| 802 | |
| 803 | } // namespace armnn_driver |