Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #define LOG_TAG "ArmnnDriver" |
| 7 | |
| 8 | #include "ArmnnPreparedModel_1_2.hpp" |
| 9 | #include "Utils.hpp" |
| 10 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 11 | #include <log/log.h> |
| 12 | #include <OperationsUtils.h> |
| 13 | #include <ExecutionBurstServer.h> |
| 14 | #include <ValidateHal.h> |
| 15 | |
| 16 | #include <cassert> |
| 17 | #include <cinttypes> |
| 18 | |
| 19 | using namespace android; |
| 20 | using namespace android::hardware; |
| 21 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 22 | namespace { |
| 23 | |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 24 | static const Timing g_NoTiming = {.timeOnDevice = UINT64_MAX, .timeInDriver = UINT64_MAX}; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 25 | using namespace armnn_driver; |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 26 | using TimePoint = std::chrono::steady_clock::time_point; |
| 27 | |
| 28 | TimePoint Now() |
| 29 | { |
| 30 | return std::chrono::steady_clock::now(); |
| 31 | } |
| 32 | |
| 33 | unsigned long MicrosecondsDuration(TimePoint endPoint, TimePoint startPoint) |
| 34 | { |
| 35 | return static_cast<unsigned long>(std::chrono::duration_cast<std::chrono::microseconds>( |
| 36 | endPoint - startPoint).count()); |
| 37 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 38 | |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 39 | void NotifyCallbackAndCheck(const ::android::sp<V1_0::IExecutionCallback>& callback, |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 40 | V1_0::ErrorStatus errorStatus, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 41 | std::vector<OutputShape>, |
| 42 | const Timing, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 43 | std::string callingFunction) |
| 44 | { |
| 45 | Return<void> returned = callback->notify(errorStatus); |
| 46 | // This check is required, if the callback fails and it isn't checked it will bring down the service |
| 47 | if (!returned.isOk()) |
| 48 | { |
| 49 | ALOGE("ArmnnDriver::%s: hidl callback failed to return properly: %s", |
| 50 | callingFunction.c_str(), returned.description().c_str()); |
| 51 | } |
| 52 | } |
| 53 | |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 54 | void NotifyCallbackAndCheck(const ::android::sp<V1_2::IExecutionCallback>& callback, |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 55 | V1_0::ErrorStatus errorStatus, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 56 | std::vector<OutputShape> outputShapes, |
| 57 | const Timing timing, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 58 | std::string callingFunction) |
| 59 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 60 | Return<void> returned = callback->notify_1_2(errorStatus, outputShapes, timing); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 61 | // This check is required, if the callback fails and it isn't checked it will bring down the service |
| 62 | if (!returned.isOk()) |
| 63 | { |
| 64 | ALOGE("ArmnnDriver::%s: hidl callback failed to return properly: %s", |
| 65 | callingFunction.c_str(), returned.description().c_str()); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | bool ValidateRequestArgument(const RequestArgument& requestArg, const armnn::TensorInfo& tensorInfo) |
| 70 | { |
| 71 | if (requestArg.dimensions.size() != 0) |
| 72 | { |
| 73 | if (requestArg.dimensions.size() != tensorInfo.GetNumDimensions()) |
| 74 | { |
| 75 | ALOGE("Mismatched dimensions (request argument: %zu, expected: %u)", |
| 76 | requestArg.dimensions.size(), tensorInfo.GetNumDimensions()); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | for (unsigned int d = 0; d < tensorInfo.GetNumDimensions(); ++d) |
| 81 | { |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 82 | if (requestArg.dimensions[d] != 0 && requestArg.dimensions[d] != tensorInfo.GetShape()[d]) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 83 | { |
| 84 | ALOGE("Mismatched size for dimension %d (request argument: %u, expected %u)", |
| 85 | d, requestArg.dimensions[d], tensorInfo.GetShape()[d]); |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | armnn::Tensor GetTensorForRequestArgument(const RequestArgument& requestArg, |
| 95 | const armnn::TensorInfo& tensorInfo, |
| 96 | const std::vector<::android::nn::RunTimePoolInfo>& requestPools) |
| 97 | { |
| 98 | if (!ValidateRequestArgument(requestArg, tensorInfo)) |
| 99 | { |
| 100 | return armnn::Tensor(); |
| 101 | } |
| 102 | |
| 103 | return armnn::Tensor(tensorInfo, GetMemoryFromPool(requestArg.location, requestPools)); |
| 104 | } |
| 105 | |
| 106 | inline std::string BuildTensorName(const char* tensorNamePrefix, std::size_t index) |
| 107 | { |
| 108 | return tensorNamePrefix + std::to_string(index); |
| 109 | } |
| 110 | |
| 111 | } // anonymous namespace |
| 112 | |
| 113 | using namespace android::hardware; |
| 114 | |
| 115 | namespace armnn_driver |
| 116 | { |
| 117 | |
| 118 | template<typename HalVersion> |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 119 | RequestThread<ArmnnPreparedModel_1_2, HalVersion, CallbackContext_1_2> |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 120 | ArmnnPreparedModel_1_2<HalVersion>::m_RequestThread; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 121 | |
| 122 | template<typename HalVersion> |
| 123 | template<typename TensorBindingCollection> |
| 124 | void ArmnnPreparedModel_1_2<HalVersion>::DumpTensorsIfRequired(char const* tensorNamePrefix, |
| 125 | const TensorBindingCollection& tensorBindings) |
| 126 | { |
| 127 | if (!m_RequestInputsAndOutputsDumpDir.empty()) |
| 128 | { |
Colm Donelan | 08d9a1c | 2020-09-09 17:56:55 +0100 | [diff] [blame^] | 129 | 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] | 130 | for (std::size_t i = 0u; i < tensorBindings.size(); ++i) |
| 131 | { |
| 132 | DumpTensor(m_RequestInputsAndOutputsDumpDir, |
| 133 | requestName, |
| 134 | BuildTensorName(tensorNamePrefix, i), |
| 135 | tensorBindings[i].second); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | template<typename HalVersion> |
| 141 | ArmnnPreparedModel_1_2<HalVersion>::ArmnnPreparedModel_1_2(armnn::NetworkId networkId, |
| 142 | armnn::IRuntime* runtime, |
| 143 | const V1_2::Model& model, |
| 144 | const std::string& requestInputsAndOutputsDumpDir, |
| 145 | const bool gpuProfilingEnabled) |
| 146 | : m_NetworkId(networkId) |
| 147 | , m_Runtime(runtime) |
| 148 | , m_Model(model) |
| 149 | , m_RequestCount(0) |
| 150 | , m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir) |
| 151 | , m_GpuProfilingEnabled(gpuProfilingEnabled) |
| 152 | { |
| 153 | // Enable profiling if required. |
| 154 | m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled); |
| 155 | } |
| 156 | |
| 157 | template<typename HalVersion> |
| 158 | ArmnnPreparedModel_1_2<HalVersion>::~ArmnnPreparedModel_1_2() |
| 159 | { |
| 160 | // Get a hold of the profiler used by this model. |
| 161 | std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId); |
| 162 | |
| 163 | // Unload the network associated with this model. |
| 164 | m_Runtime->UnloadNetwork(m_NetworkId); |
| 165 | |
| 166 | // Dump the profiling info to a file if required. |
| 167 | DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId, profiler.get()); |
| 168 | } |
| 169 | |
| 170 | template<typename HalVersion> |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 171 | 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] | 172 | const ::android::sp<V1_0::IExecutionCallback>& callback) |
| 173 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 174 | if (callback.get() == nullptr) |
| 175 | { |
| 176 | ALOGE("ArmnnPreparedModel_1_2::execute invalid callback passed"); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 177 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 180 | auto cb = [callback](V1_0::ErrorStatus errorStatus, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 181 | std::vector<OutputShape> outputShapes, |
| 182 | const Timing& timing, |
| 183 | std::string callingFunction) |
| 184 | { |
| 185 | NotifyCallbackAndCheck(callback, errorStatus, outputShapes, timing, callingFunction); |
| 186 | }; |
| 187 | |
| 188 | return Execute(request, MeasureTiming::NO, cb); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | template<typename HalVersion> |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 192 | Return <V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::execute_1_2( |
| 193 | const V1_0::Request& request, |
| 194 | MeasureTiming measureTiming, |
| 195 | const sp<V1_2::IExecutionCallback>& callback) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 196 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 197 | if (callback.get() == nullptr) |
| 198 | { |
| 199 | ALOGE("ArmnnPreparedModel_1_2::execute_1_2 invalid callback passed"); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 200 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 203 | auto cb = [callback](V1_0::ErrorStatus errorStatus, |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 204 | std::vector<OutputShape> outputShapes, |
| 205 | const Timing& timing, |
| 206 | std::string callingFunction) |
| 207 | { |
| 208 | NotifyCallbackAndCheck(callback, errorStatus, outputShapes, timing, callingFunction); |
| 209 | }; |
| 210 | |
| 211 | return Execute(request, measureTiming, cb); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 214 | template<typename HalVersion> |
| 215 | Return<V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::PrepareMemoryForInputs( |
| 216 | armnn::InputTensors& inputs, |
| 217 | const V1_0::Request& request, |
| 218 | const std::vector<android::nn::RunTimePoolInfo>& memPools) |
| 219 | { |
| 220 | inputs.reserve(request.inputs.size()); |
| 221 | for (unsigned int i = 0; i < request.inputs.size(); i++) |
| 222 | { |
| 223 | const auto& inputArg = request.inputs[i]; |
| 224 | |
| 225 | const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i); |
| 226 | const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, memPools); |
| 227 | |
| 228 | if (inputTensor.GetMemoryArea() == nullptr) |
| 229 | { |
| 230 | ALOGE("Cannot execute request. Error converting request input %u to tensor", i); |
| 231 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 232 | } |
| 233 | |
| 234 | inputs.emplace_back(i, inputTensor); |
| 235 | } |
| 236 | |
| 237 | return V1_0::ErrorStatus::NONE; |
| 238 | } |
| 239 | |
| 240 | template<typename HalVersion> |
| 241 | Return<V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::PrepareMemoryForOutputs( |
| 242 | armnn::OutputTensors& outputs, |
| 243 | std::vector<OutputShape> &outputShapes, |
| 244 | const V1_0::Request& request, |
| 245 | const std::vector<android::nn::RunTimePoolInfo>& memPools) |
| 246 | { |
| 247 | outputs.reserve(request.outputs.size()); |
| 248 | for (unsigned int i = 0; i < request.outputs.size(); i++) |
| 249 | { |
| 250 | const auto& outputArg = request.outputs[i]; |
| 251 | |
| 252 | const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i); |
| 253 | const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, memPools); |
| 254 | if (outputTensor.GetMemoryArea() == nullptr) |
| 255 | { |
| 256 | ALOGE("Cannot execute request. Error converting request output %u to tensor", i); |
| 257 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 258 | } |
| 259 | |
| 260 | const size_t outputSize = outputTensorInfo.GetNumBytes(); |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 261 | |
| 262 | if (outputArg.location.length < outputSize) |
| 263 | { |
| 264 | ALOGW("ArmnnPreparedModel_1_2::Execute failed: outputArg.location.length < outputSize"); |
| 265 | return V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE; |
| 266 | } |
| 267 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 268 | const size_t bufferSize = memPools.at(outputArg.location.poolIndex).getHidlMemory().size(); |
| 269 | if (bufferSize < outputSize) |
| 270 | { |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 271 | ALOGW("ArmnnPreparedModel_1_2::Execute failed: bufferSize < outputSize"); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 272 | return V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE; |
| 273 | } |
| 274 | |
| 275 | outputs.emplace_back(i, outputTensor); |
| 276 | outputShapes[i] = ComputeShape(outputTensorInfo); |
| 277 | } |
| 278 | |
| 279 | return V1_0::ErrorStatus::NONE; |
| 280 | } |
| 281 | |
| 282 | template<typename HalVersion> |
| 283 | Return<V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::PrepareMemoryForIO( |
| 284 | armnn::InputTensors& inputs, |
| 285 | armnn::OutputTensors& outputs, |
| 286 | std::vector<android::nn::RunTimePoolInfo>& memPools, |
| 287 | const V1_0::Request& request, |
| 288 | CallbackAsync_1_2 callback) |
| 289 | { |
| 290 | if (!setRunTimePoolInfosFromHidlMemories(&memPools, request.pools)) |
| 291 | { |
| 292 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 293 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 294 | } |
| 295 | |
| 296 | // add the inputs and outputs with their data |
| 297 | try |
| 298 | { |
| 299 | if (PrepareMemoryForInputs(inputs, request, memPools) != V1_0::ErrorStatus::NONE) |
| 300 | { |
| 301 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 302 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 303 | } |
| 304 | |
| 305 | std::vector<OutputShape> outputShapes(request.outputs.size()); |
| 306 | |
| 307 | auto errorStatus = PrepareMemoryForOutputs(outputs, outputShapes, request, memPools); |
| 308 | if (errorStatus != V1_0::ErrorStatus::NONE) |
| 309 | { |
| 310 | callback(errorStatus, |
| 311 | outputShapes, |
| 312 | g_NoTiming, |
| 313 | "ArmnnPreparedModel_1_2::Execute"); |
| 314 | return errorStatus; |
| 315 | } |
| 316 | } |
| 317 | catch (armnn::Exception& e) |
| 318 | { |
| 319 | ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what()); |
| 320 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 321 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 322 | } |
| 323 | catch (std::exception& e) |
| 324 | { |
| 325 | ALOGE("std::exception caught while preparing for EnqueueWorkload: %s", e.what()); |
| 326 | callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 327 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 328 | } |
| 329 | |
| 330 | return V1_0::ErrorStatus::NONE; |
| 331 | } |
| 332 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 333 | template<typename HalVersion> |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 334 | Return<void> ArmnnPreparedModel_1_2<HalVersion>::executeSynchronously(const V1_0::Request& request, |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 335 | MeasureTiming measureTiming, |
| 336 | executeSynchronously_cb cb) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 337 | { |
| 338 | ALOGV("ArmnnPreparedModel_1_2::executeSynchronously(): %s", GetModelSummary(m_Model).c_str()); |
| 339 | m_RequestCount++; |
| 340 | |
| 341 | if (cb == nullptr) |
| 342 | { |
| 343 | ALOGE("ArmnnPreparedModel_1_2::executeSynchronously invalid callback passed"); |
| 344 | return Void(); |
| 345 | } |
| 346 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 347 | TimePoint driverStart; |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 348 | |
| 349 | if (measureTiming == MeasureTiming::YES) |
| 350 | { |
| 351 | driverStart = Now(); |
| 352 | } |
| 353 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 354 | if (!android::nn::validateRequest(request, m_Model)) |
| 355 | { |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 356 | ALOGE("ArmnnPreparedModel_1_2::executeSynchronously invalid request model"); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 357 | cb(V1_0::ErrorStatus::INVALID_ARGUMENT, {}, g_NoTiming); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 358 | return Void(); |
| 359 | } |
| 360 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 361 | auto cbWrapper = [cb](V1_0::ErrorStatus errorStatus, |
| 362 | std::vector<OutputShape> outputShapes, |
| 363 | const Timing& timing, |
| 364 | std::string) |
| 365 | { |
| 366 | cb(errorStatus, outputShapes, timing); |
| 367 | }; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 368 | |
| 369 | // map the memory pool into shared pointers |
| 370 | // 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] | 371 | auto memPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>(); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 372 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 373 | // allocate the tensors on the heap, as they are passed to the request thread |
| 374 | auto inputs = std::make_shared<armnn::InputTensors>(); |
| 375 | auto outputs = std::make_shared<armnn::OutputTensors>(); |
| 376 | |
| 377 | auto prepareStatus = PrepareMemoryForIO(*inputs, *outputs, *memPools, request, cbWrapper); |
| 378 | if (prepareStatus != V1_0::ErrorStatus::NONE) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 379 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 380 | return Void(); |
| 381 | } |
| 382 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 383 | ALOGV("ArmnnPreparedModel_1_2::executeSynchronously() before Execution"); |
| 384 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 385 | CallbackContext_1_2 cbCtx; |
| 386 | cbCtx.callback = cbWrapper; |
| 387 | cbCtx.ctx.measureTimings = measureTiming; |
| 388 | cbCtx.ctx.driverStart = driverStart; |
| 389 | ExecuteGraph(memPools, *inputs, *outputs, cbCtx); |
| 390 | |
| 391 | return Void(); |
| 392 | } |
| 393 | |
| 394 | template<typename HalVersion> |
| 395 | template<typename CallbackContext> |
| 396 | bool ArmnnPreparedModel_1_2<HalVersion>::ExecuteGraph( |
| 397 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 398 | armnn::InputTensors& inputTensors, |
| 399 | armnn::OutputTensors& outputTensors, |
| 400 | CallbackContext cb) |
| 401 | { |
| 402 | ALOGV("ArmnnPreparedModel_1_2::ExecuteGraph(...)"); |
| 403 | |
| 404 | TimePoint driverEnd, deviceStart, deviceEnd; |
| 405 | |
| 406 | DumpTensorsIfRequired("Input", inputTensors); |
| 407 | |
| 408 | std::vector<OutputShape> outputShapes(outputTensors.size()); |
| 409 | for (unsigned int i = 0; i < outputTensors.size(); i++) |
| 410 | { |
| 411 | std::pair<int, armnn::Tensor> outputTensorPair = outputTensors[i]; |
| 412 | const armnn::Tensor outputTensor = outputTensorPair.second; |
| 413 | const armnn::TensorInfo outputTensorInfo = outputTensor.GetInfo(); |
| 414 | |
| 415 | outputShapes[i] = ComputeShape(outputTensorInfo); |
| 416 | } |
| 417 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 418 | // run it |
| 419 | try |
| 420 | { |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 421 | if (cb.ctx.measureTimings == MeasureTiming::YES) |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 422 | { |
| 423 | deviceStart = Now(); |
| 424 | } |
| 425 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 426 | armnn::Status status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 427 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 428 | if (cb.ctx.measureTimings == MeasureTiming::YES) |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 429 | { |
| 430 | deviceEnd = Now(); |
| 431 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 432 | if (status != armnn::Status::Success) |
| 433 | { |
| 434 | ALOGW("EnqueueWorkload failed"); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 435 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, |
| 436 | "ArmnnPreparedModel_1_2::ExecuteGraph"); |
| 437 | return false; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 438 | } |
| 439 | } |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 440 | catch (armnn::Exception& e) |
| 441 | { |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 442 | ALOGW("armnn:Exception caught from EnqueueWorkload: %s", e.what()); |
| 443 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::ExecuteGraph"); |
| 444 | return false; |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 445 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 446 | catch (std::exception& e) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 447 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 448 | ALOGE("std::exception caught from EnqueueWorkload: %s", e.what()); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 449 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, {}, g_NoTiming, "ArmnnPreparedModel_1_2::ExecuteGraph"); |
| 450 | return false; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 453 | CommitPools(*pMemPools); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 454 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 455 | DumpTensorsIfRequired("Output", outputTensors); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 456 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 457 | if (cb.ctx.measureTimings == MeasureTiming::YES) |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 458 | { |
| 459 | driverEnd = Now(); |
| 460 | Timing timing; |
| 461 | timing.timeOnDevice = MicrosecondsDuration(deviceEnd, deviceStart); |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 462 | timing.timeInDriver = MicrosecondsDuration(driverEnd, cb.ctx.driverStart); |
| 463 | ALOGV("ArmnnPreparedModel_1_2::execute timing - Device = %lu Driver = %lu", timing.timeOnDevice, |
| 464 | timing.timeInDriver); |
| 465 | cb.callback(V1_0::ErrorStatus::NONE, outputShapes, timing, "ArmnnPreparedModel_1_2::ExecuteGraph"); |
| 466 | } else { |
| 467 | 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] | 468 | } |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 469 | |
| 470 | return true; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 473 | template<typename HalVersion> |
| 474 | bool ArmnnPreparedModel_1_2<HalVersion>::ExecuteWithDummyInputs() |
| 475 | { |
| 476 | std::vector<std::vector<char>> storage; |
| 477 | armnn::InputTensors inputTensors; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 478 | for (unsigned int i = 0; i < getMainModel(m_Model).inputIndexes.size(); i++) |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 479 | { |
| 480 | const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i); |
| 481 | storage.emplace_back(inputTensorInfo.GetNumBytes()); |
| 482 | const armnn::ConstTensor inputTensor(inputTensorInfo, storage.back().data()); |
| 483 | |
| 484 | inputTensors.emplace_back(i, inputTensor); |
| 485 | } |
| 486 | |
| 487 | armnn::OutputTensors outputTensors; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 488 | for (unsigned int i = 0; i < getMainModel(m_Model).outputIndexes.size(); i++) |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 489 | { |
| 490 | const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i); |
| 491 | storage.emplace_back(outputTensorInfo.GetNumBytes()); |
| 492 | const armnn::Tensor outputTensor(outputTensorInfo, storage.back().data()); |
| 493 | |
| 494 | outputTensors.emplace_back(i, outputTensor); |
| 495 | } |
| 496 | |
| 497 | auto nullCallback = [](V1_0::ErrorStatus, std::vector<OutputShape>, const Timing&, std::string) {}; |
| 498 | CallbackContext_1_2 callbackContext; |
| 499 | callbackContext.callback = nullCallback; |
| 500 | callbackContext.ctx.measureTimings = MeasureTiming::NO; |
| 501 | auto memPools = std::make_shared<std::vector<::android::nn::RunTimePoolInfo>>(); |
| 502 | return ExecuteGraph(memPools, |
| 503 | inputTensors, |
| 504 | outputTensors, |
| 505 | callbackContext); |
| 506 | } |
| 507 | |
| 508 | template<typename HalVersion> |
| 509 | Return <V1_0::ErrorStatus> ArmnnPreparedModel_1_2<HalVersion>::Execute(const V1_0::Request& request, |
| 510 | MeasureTiming measureTiming, |
| 511 | CallbackAsync_1_2 callback) |
| 512 | { |
| 513 | ExecutionContext_1_2 ctx; |
| 514 | if (measureTiming == MeasureTiming::YES) |
| 515 | { |
| 516 | ctx.measureTimings = measureTiming; |
| 517 | ctx.driverStart = Now(); |
| 518 | } |
| 519 | |
| 520 | ALOGV("ArmnnPreparedModel_1_2::execute(): %s", GetModelSummary(m_Model).c_str()); |
| 521 | m_RequestCount++; |
| 522 | |
| 523 | if (!android::nn::validateRequest(request, m_Model)) |
| 524 | { |
| 525 | callback(V1_0::ErrorStatus::INVALID_ARGUMENT, {}, g_NoTiming, "ArmnnPreparedModel_1_2::execute"); |
| 526 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
| 527 | } |
| 528 | |
| 529 | if (!m_RequestInputsAndOutputsDumpDir.empty()) |
| 530 | { |
| 531 | ALOGD("Dumping inputs and outputs for request %" PRIuPTR, reinterpret_cast<std::uintptr_t>(&callback)); |
| 532 | } |
| 533 | |
| 534 | // map the memory pool into shared pointers |
| 535 | // use a shared memory pools vector on the heap, as it is passed to the request thread |
| 536 | auto memPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>(); |
| 537 | |
| 538 | // allocate the tensors on the heap, as they are passed to the request thread |
| 539 | auto inputTensors = std::make_shared<armnn::InputTensors>(); |
| 540 | auto outputTensors = std::make_shared<armnn::OutputTensors>(); |
| 541 | |
| 542 | auto prepareStatus = PrepareMemoryForIO(*inputTensors, *outputTensors, *memPools, request, callback); |
| 543 | switch(prepareStatus) |
| 544 | { |
| 545 | case V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 546 | return V1_0::ErrorStatus::NONE; |
| 547 | case V1_0::ErrorStatus::GENERAL_FAILURE: |
| 548 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
| 549 | default: |
| 550 | {} |
| 551 | } |
| 552 | |
| 553 | ALOGV("ArmnnPreparedModel_1_2::execute(...) before PostMsg"); |
| 554 | |
| 555 | // post the request for asynchronous execution |
| 556 | CallbackContext_1_2 cb; |
| 557 | cb.callback = callback; |
| 558 | cb.ctx = ctx; |
| 559 | m_RequestThread.PostMsg(this, memPools, inputTensors, outputTensors, cb); |
| 560 | ALOGV("ArmnnPreparedModel_1_2::execute(...) after PostMsg"); |
| 561 | return V1_0::ErrorStatus::NONE; |
| 562 | } |
| 563 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 564 | template<typename HalVersion> |
| 565 | Return<void> ArmnnPreparedModel_1_2<HalVersion>::configureExecutionBurst( |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 566 | const sp<V1_2::IBurstCallback>& callback, |
| 567 | const MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel, |
| 568 | const MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel, |
| 569 | V1_2::IPreparedModel::configureExecutionBurst_cb cb) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 570 | { |
| 571 | ALOGV("ArmnnPreparedModel_1_2::configureExecutionBurst"); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 572 | const sp<V1_2::IBurstContext> burst = ExecutionBurstServer::create(callback, |
| 573 | requestChannel, |
| 574 | resultChannel, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 575 | this); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 576 | |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 577 | if (burst == nullptr) |
| 578 | { |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 579 | cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}); |
Mike Kelly | 4438151 | 2019-07-08 17:37:35 +0100 | [diff] [blame] | 580 | } |
| 581 | else |
| 582 | { |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 583 | cb(V1_0::ErrorStatus::NONE, burst); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 584 | } |
| 585 | return Void(); |
| 586 | } |
| 587 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 588 | #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] | 589 | template class ArmnnPreparedModel_1_2<hal_1_2::HalPolicy>; |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 590 | template bool ArmnnPreparedModel_1_2<hal_1_2::HalPolicy>::ExecuteGraph<CallbackContext_1_2>( |
| 591 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 592 | armnn::InputTensors& pInputTensors, |
| 593 | armnn::OutputTensors& pOutputTensors, |
| 594 | CallbackContext_1_2 cb); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 595 | #endif |
| 596 | |
| 597 | } // namespace armnn_driver |