telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | 93e4898 | 2018-09-05 13:05:09 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #define LOG_TAG "ArmnnDriver" |
| 7 | |
| 8 | #include "ArmnnPreparedModel.hpp" |
| 9 | #include "Utils.hpp" |
| 10 | |
| 11 | #include <boost/format.hpp> |
| 12 | #include <log/log.h> |
| 13 | #include <OperationsUtils.h> |
| 14 | |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 15 | #if defined(ARMNN_ANDROID_P) |
| 16 | // The headers of the ML framework have changed between Android O and Android P. |
| 17 | // The validation functions have been moved into their own header, ValidateHal.h. |
| 18 | #include <ValidateHal.h> |
| 19 | #endif |
| 20 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 21 | #include <cassert> |
| 22 | #include <cinttypes> |
| 23 | |
| 24 | using namespace android; |
| 25 | |
| 26 | namespace |
| 27 | { |
| 28 | using namespace armnn_driver; |
| 29 | |
| 30 | void NotifyCallbackAndCheck(const ::android::sp<IExecutionCallback>& callback, ErrorStatus errorStatus, |
| 31 | std::string callingFunction) |
| 32 | { |
| 33 | Return<void> returned = callback->notify(errorStatus); |
| 34 | // This check is required, if the callback fails and it isn't checked it will bring down the service |
| 35 | if (!returned.isOk()) |
| 36 | { |
| 37 | ALOGE("ArmnnDriver::%s: hidl callback failed to return properly: %s", |
| 38 | callingFunction.c_str(), returned.description().c_str()); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | bool ValidateRequestArgument(const RequestArgument& requestArg, const armnn::TensorInfo& tensorInfo) |
| 43 | { |
| 44 | if (requestArg.dimensions.size() != 0) |
| 45 | { |
| 46 | if (requestArg.dimensions.size() != tensorInfo.GetNumDimensions()) |
| 47 | { |
| 48 | ALOGE("Mismatched dimensions (request argument: %zu, expected: %u)", |
| 49 | requestArg.dimensions.size(), tensorInfo.GetNumDimensions()); |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | for (unsigned int d = 0; d < tensorInfo.GetNumDimensions(); ++d) |
| 54 | { |
| 55 | if (requestArg.dimensions[d] != tensorInfo.GetShape()[d]) |
| 56 | { |
| 57 | ALOGE("Mismatched size for dimension %d (request argument: %u, expected %u)", |
| 58 | d, requestArg.dimensions[d], tensorInfo.GetShape()[d]); |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | armnn::Tensor GetTensorForRequestArgument(const RequestArgument& requestArg, |
| 68 | const armnn::TensorInfo& tensorInfo, |
| 69 | const std::vector<::android::nn::RunTimePoolInfo>& requestPools) |
| 70 | { |
| 71 | if (!ValidateRequestArgument(requestArg, tensorInfo)) |
| 72 | { |
| 73 | return armnn::Tensor(); |
| 74 | } |
| 75 | |
| 76 | return armnn::Tensor(tensorInfo, GetMemoryFromPool(requestArg.location, requestPools)); |
| 77 | } |
| 78 | |
| 79 | inline std::string BuildTensorName(const char* tensorNamePrefix, std::size_t index) |
| 80 | { |
| 81 | return tensorNamePrefix + std::to_string(index); |
| 82 | } |
| 83 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 84 | } // anonymous namespace |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 85 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 86 | using namespace android::hardware; |
| 87 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 88 | namespace armnn_driver |
| 89 | { |
| 90 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 91 | template<typename HalVersion> |
| 92 | RequestThread<HalVersion> ArmnnPreparedModel<HalVersion>::m_RequestThread; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 93 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 94 | template<typename HalVersion> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 95 | template <typename TensorBindingCollection> |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 96 | void ArmnnPreparedModel<HalVersion>::DumpTensorsIfRequired(char const* tensorNamePrefix, |
| 97 | const TensorBindingCollection& tensorBindings) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 98 | { |
| 99 | if (!m_RequestInputsAndOutputsDumpDir.empty()) |
| 100 | { |
| 101 | const std::string requestName = boost::str(boost::format("%1%_%2%.dump") % m_NetworkId % m_RequestCount); |
| 102 | for (std::size_t i = 0u; i < tensorBindings.size(); ++i) |
| 103 | { |
| 104 | DumpTensor(m_RequestInputsAndOutputsDumpDir, |
| 105 | requestName, |
| 106 | BuildTensorName(tensorNamePrefix, i), |
| 107 | tensorBindings[i].second); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 112 | template<typename HalVersion> |
| 113 | ArmnnPreparedModel<HalVersion>::ArmnnPreparedModel(armnn::NetworkId networkId, |
| 114 | armnn::IRuntime* runtime, |
| 115 | const HalModel& model, |
| 116 | const std::string& requestInputsAndOutputsDumpDir, |
| 117 | const bool gpuProfilingEnabled) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 118 | : m_NetworkId(networkId) |
| 119 | , m_Runtime(runtime) |
| 120 | , m_Model(model) |
| 121 | , m_RequestCount(0) |
| 122 | , m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir) |
| 123 | , m_GpuProfilingEnabled(gpuProfilingEnabled) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 124 | { |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 125 | // Enable profiling if required. |
| 126 | m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 129 | template<typename HalVersion> |
| 130 | ArmnnPreparedModel<HalVersion>::~ArmnnPreparedModel() |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 131 | { |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 132 | // Get a hold of the profiler used by this model. |
| 133 | std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId); |
| 134 | |
| 135 | // Unload the network associated with this model. |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 136 | m_Runtime->UnloadNetwork(m_NetworkId); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 137 | |
| 138 | // Dump the profiling info to a file if required. |
| 139 | DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId, profiler.get()); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 142 | template<typename HalVersion> |
| 143 | Return<ErrorStatus> ArmnnPreparedModel<HalVersion>::execute(const Request& request, |
| 144 | const ::android::sp<IExecutionCallback>& callback) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 145 | { |
| 146 | ALOGV("ArmnnPreparedModel::execute(): %s", GetModelSummary(m_Model).c_str()); |
| 147 | m_RequestCount++; |
| 148 | |
| 149 | if (callback.get() == nullptr) { |
| 150 | ALOGE("ArmnnPreparedModel::execute invalid callback passed"); |
| 151 | return ErrorStatus::INVALID_ARGUMENT; |
| 152 | } |
| 153 | |
| 154 | if (!android::nn::validateRequest(request, m_Model)) |
| 155 | { |
| 156 | NotifyCallbackAndCheck(callback, ErrorStatus::INVALID_ARGUMENT, "ArmnnPreparedModel::execute"); |
| 157 | return ErrorStatus::INVALID_ARGUMENT; |
| 158 | } |
| 159 | |
| 160 | if (!m_RequestInputsAndOutputsDumpDir.empty()) |
| 161 | { |
| 162 | ALOGD("Dumping inputs and outputs for request %" PRIuPTR, reinterpret_cast<std::uintptr_t>(callback.get())); |
| 163 | } |
| 164 | |
| 165 | // allocate the tensors on the heap, as they are passed to the request thread |
| 166 | auto pInputTensors = std::make_shared<armnn::InputTensors>(); |
| 167 | auto pOutputTensors = std::make_shared<armnn::OutputTensors>(); |
| 168 | |
| 169 | // map the memory pool into shared pointers |
| 170 | // use a shared memory pools vector on the heap, as it is passed to the request thread |
| 171 | auto pMemPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>(); |
| 172 | if (!setRunTimePoolInfosFromHidlMemories(pMemPools.get(), request.pools)) |
| 173 | { |
| 174 | NotifyCallbackAndCheck(callback, ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute"); |
| 175 | return ErrorStatus::GENERAL_FAILURE; |
| 176 | } |
| 177 | |
| 178 | // add the inputs and outputs with their data |
| 179 | try |
| 180 | { |
| 181 | pInputTensors->reserve(request.inputs.size()); |
| 182 | for (unsigned int i = 0; i < request.inputs.size(); i++) |
| 183 | { |
| 184 | const auto& inputArg = request.inputs[i]; |
| 185 | |
| 186 | const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i); |
| 187 | const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, *pMemPools); |
| 188 | if (inputTensor.GetMemoryArea() == nullptr) |
| 189 | { |
| 190 | ALOGE("Cannot execute request. Error converting request input %u to tensor", i); |
| 191 | return ErrorStatus::GENERAL_FAILURE; |
| 192 | } |
| 193 | |
| 194 | pInputTensors->emplace_back(i, inputTensor); |
| 195 | } |
| 196 | |
| 197 | pOutputTensors->reserve(request.outputs.size()); |
| 198 | for (unsigned int i = 0; i < request.outputs.size(); i++) |
| 199 | { |
| 200 | const auto& outputArg = request.outputs[i]; |
| 201 | |
| 202 | const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i); |
| 203 | const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, *pMemPools); |
| 204 | if (outputTensor.GetMemoryArea() == nullptr) |
| 205 | { |
| 206 | ALOGE("Cannot execute request. Error converting request output %u to tensor", i); |
| 207 | return ErrorStatus::GENERAL_FAILURE; |
| 208 | } |
| 209 | |
| 210 | pOutputTensors->emplace_back(i, outputTensor); |
| 211 | } |
| 212 | } |
| 213 | catch (armnn::Exception& e) |
| 214 | { |
| 215 | ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what()); |
| 216 | NotifyCallbackAndCheck(callback, ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute"); |
| 217 | return ErrorStatus::GENERAL_FAILURE; |
| 218 | } |
| 219 | |
| 220 | ALOGV("ArmnnPreparedModel::execute(...) before PostMsg"); |
| 221 | // post the request for asynchronous execution |
| 222 | m_RequestThread.PostMsg(this, pMemPools, pInputTensors, pOutputTensors, callback); |
| 223 | ALOGV("ArmnnPreparedModel::execute(...) after PostMsg"); |
| 224 | |
| 225 | return ErrorStatus::NONE; // successfully queued |
| 226 | } |
| 227 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 228 | template<typename HalVersion> |
| 229 | void ArmnnPreparedModel<HalVersion>::ExecuteGraph( |
| 230 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 231 | std::shared_ptr<armnn::InputTensors>& pInputTensors, |
| 232 | std::shared_ptr<armnn::OutputTensors>& pOutputTensors, |
| 233 | const ::android::sp<IExecutionCallback>& callback) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 234 | { |
| 235 | ALOGV("ArmnnPreparedModel::ExecuteGraph(...)"); |
| 236 | |
| 237 | DumpTensorsIfRequired("Input", *pInputTensors); |
| 238 | |
| 239 | // run it |
| 240 | try |
| 241 | { |
| 242 | m_Runtime->EnqueueWorkload(m_NetworkId, *pInputTensors, *pOutputTensors); |
| 243 | } |
| 244 | catch (armnn::Exception& e) |
| 245 | { |
| 246 | ALOGW("armnn::Exception caught from EnqueueWorkload: %s", e.what()); |
| 247 | NotifyCallbackAndCheck(callback, ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph"); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | DumpTensorsIfRequired("Output", *pOutputTensors); |
| 252 | |
| 253 | // Commit output buffers. |
| 254 | // Note that we update *all* pools, even if they aren't actually used as outputs - |
| 255 | // this is simpler and is what the CpuExecutor does. |
| 256 | for (android::nn::RunTimePoolInfo& pool : *pMemPools) |
| 257 | { |
| 258 | pool.update(); |
| 259 | } |
| 260 | |
| 261 | NotifyCallbackAndCheck(callback, ErrorStatus::NONE, "ExecuteGraph"); |
| 262 | } |
| 263 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 264 | template<typename HalVersion> |
| 265 | void ArmnnPreparedModel<HalVersion>::ExecuteWithDummyInputs() |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 266 | { |
| 267 | std::vector<std::vector<char>> storage; |
| 268 | armnn::InputTensors inputTensors; |
| 269 | for (unsigned int i = 0; i < m_Model.inputIndexes.size(); i++) |
| 270 | { |
| 271 | const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i); |
| 272 | storage.emplace_back(inputTensorInfo.GetNumBytes()); |
| 273 | const armnn::ConstTensor inputTensor(inputTensorInfo, storage.back().data()); |
| 274 | |
| 275 | inputTensors.emplace_back(i, inputTensor); |
| 276 | } |
| 277 | |
| 278 | armnn::OutputTensors outputTensors; |
| 279 | for (unsigned int i = 0; i < m_Model.outputIndexes.size(); i++) |
| 280 | { |
| 281 | const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i); |
| 282 | storage.emplace_back(outputTensorInfo.GetNumBytes()); |
| 283 | const armnn::Tensor outputTensor(outputTensorInfo, storage.back().data()); |
| 284 | |
| 285 | outputTensors.emplace_back(i, outputTensor); |
| 286 | } |
| 287 | |
| 288 | try |
| 289 | { |
| 290 | m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors); |
| 291 | } |
| 292 | catch (armnn::Exception& e) |
| 293 | { |
| 294 | ALOGW("ExecuteWithDummyInputs: armnn::Exception caught from EnqueueWorkload: %s", e.what()); |
| 295 | } |
| 296 | } |
| 297 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 298 | /// |
| 299 | /// Class template specializations |
| 300 | /// |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 301 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 302 | template class ArmnnPreparedModel<hal_1_0::HalPolicy>; |
| 303 | |
Matteo Martincigh | 8b287c2 | 2018-09-07 09:25:10 +0100 | [diff] [blame] | 304 | #ifdef ARMNN_ANDROID_NN_V1_1 |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 305 | template class ArmnnPreparedModel<hal_1_1::HalPolicy>; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 306 | #endif |
| 307 | |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 308 | } // namespace armnn_driver |