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