telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // See LICENSE file in the project root for full license information. |
| 4 | // |
| 5 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 6 | #define LOG_TAG "ArmnnDriver" |
| 7 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 8 | #include "ArmnnDriverImpl.hpp" |
| 9 | #include "ModelToINetworkConverter.hpp" |
| 10 | #include "ArmnnPreparedModel.hpp" |
| 11 | #include "SystemPropertiesUtils.hpp" |
| 12 | |
| 13 | #if defined(ARMNN_ANDROID_P) |
| 14 | // The headers of the ML framework have changed between Android O and Android P. |
| 15 | // The validation functions have been moved into their own header, ValidateHal.h. |
| 16 | #include <ValidateHal.h> |
| 17 | #endif |
| 18 | |
| 19 | #include <log/log.h> |
| 20 | |
| 21 | using namespace std; |
| 22 | using namespace android; |
| 23 | using namespace android::nn; |
| 24 | using namespace android::hardware; |
| 25 | |
| 26 | namespace |
| 27 | { |
| 28 | |
| 29 | const char *g_Float32PerformanceExecTimeName = "ArmNN.float32Performance.execTime"; |
| 30 | const char *g_Float32PerformancePowerUsageName = "ArmNN.float32Performance.powerUsage"; |
| 31 | const char *g_Quantized8PerformanceExecTimeName = "ArmNN.quantized8Performance.execTime"; |
| 32 | const char *g_Quantized8PerformancePowerUsageName = "ArmNN.quantized8Performance.powerUsage"; |
| 33 | |
| 34 | void NotifyCallbackAndCheck(const sp<IPreparedModelCallback>& callback, |
| 35 | ErrorStatus errorStatus, |
| 36 | const sp<IPreparedModel>& preparedModelPtr) |
| 37 | { |
| 38 | Return<void> returned = callback->notify(errorStatus, preparedModelPtr); |
| 39 | // This check is required, if the callback fails and it isn't checked it will bring down the service |
| 40 | if (!returned.isOk()) |
| 41 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 42 | ALOGE("ArmnnDriverImpl::prepareModel: hidl callback failed to return properly: %s ", |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 43 | returned.description().c_str()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | Return<ErrorStatus> FailPrepareModel(ErrorStatus error, |
| 48 | const string& message, |
| 49 | const sp<IPreparedModelCallback>& callback) |
| 50 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 51 | ALOGW("ArmnnDriverImpl::prepareModel: %s", message.c_str()); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 52 | NotifyCallbackAndCheck(callback, error, nullptr); |
| 53 | return error; |
| 54 | } |
| 55 | |
| 56 | } // namespace |
| 57 | |
| 58 | namespace armnn_driver |
| 59 | { |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 60 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 61 | template <typename HalVersion> |
| 62 | Return<void> ArmnnDriverImpl<HalVersion>::getCapabilities( |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 63 | const armnn::IRuntimePtr& runtime, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 64 | HalGetCapabilities_cb cb) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 65 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 66 | ALOGV("ArmnnDriverImpl::getCapabilities()"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 67 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 68 | HalCapabilities capabilities; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 69 | if (runtime) |
| 70 | { |
| 71 | capabilities.float32Performance.execTime = |
| 72 | ParseSystemProperty(g_Float32PerformanceExecTimeName, .1f); |
| 73 | |
| 74 | capabilities.float32Performance.powerUsage = |
| 75 | ParseSystemProperty(g_Float32PerformancePowerUsageName, .1f); |
| 76 | |
| 77 | capabilities.quantized8Performance.execTime = |
| 78 | ParseSystemProperty(g_Quantized8PerformanceExecTimeName, .1f); |
| 79 | |
| 80 | capabilities.quantized8Performance.powerUsage = |
| 81 | ParseSystemProperty(g_Quantized8PerformancePowerUsageName, .1f); |
| 82 | |
| 83 | cb(ErrorStatus::NONE, capabilities); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | capabilities.float32Performance.execTime = 0; |
| 88 | capabilities.float32Performance.powerUsage = 0; |
| 89 | capabilities.quantized8Performance.execTime = 0; |
| 90 | capabilities.quantized8Performance.powerUsage = 0; |
| 91 | |
| 92 | cb(ErrorStatus::DEVICE_UNAVAILABLE, capabilities); |
| 93 | } |
| 94 | |
| 95 | return Void(); |
| 96 | } |
| 97 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 98 | template <typename HalVersion> |
| 99 | Return<void> ArmnnDriverImpl<HalVersion>::getSupportedOperations( |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 100 | const armnn::IRuntimePtr& runtime, |
| 101 | const DriverOptions& options, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 102 | const HalModel& model, |
| 103 | HalGetSupportedOperations_cb cb) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 104 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 105 | ALOGV("ArmnnDriverImpl::getSupportedOperations()"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 106 | |
| 107 | vector<bool> result; |
| 108 | |
| 109 | if (!runtime) |
| 110 | { |
| 111 | cb(ErrorStatus::DEVICE_UNAVAILABLE, result); |
| 112 | return Void(); |
| 113 | } |
| 114 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 115 | // Run general model validation, if this doesn't pass we shouldn't analyse the model anyway. |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 116 | if (!android::nn::validateModel(model)) |
| 117 | { |
| 118 | cb(ErrorStatus::INVALID_ARGUMENT, result); |
| 119 | return Void(); |
| 120 | } |
| 121 | |
| 122 | // Attempt to convert the model to an ArmNN input network (INetwork). |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 123 | ModelToINetworkConverter<HalVersion> modelConverter(options.GetComputeDevice(), |
| 124 | model, |
| 125 | options.GetForcedUnsupportedOperations()); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 126 | |
| 127 | if (modelConverter.GetConversionResult() != ConversionResult::Success |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 128 | && modelConverter.GetConversionResult() != ConversionResult::UnsupportedFeature) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 129 | { |
| 130 | cb(ErrorStatus::GENERAL_FAILURE, result); |
| 131 | return Void(); |
| 132 | } |
| 133 | |
| 134 | // Check each operation if it was converted successfully and copy the flags |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 135 | // into the result (vector<bool>) that we need to return to Android. |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 136 | result.reserve(model.operations.size()); |
| 137 | for (uint32_t operationIdx = 0; operationIdx < model.operations.size(); operationIdx++) |
| 138 | { |
| 139 | bool operationSupported = modelConverter.IsOperationSupported(operationIdx); |
| 140 | result.push_back(operationSupported); |
| 141 | } |
| 142 | |
| 143 | cb(ErrorStatus::NONE, result); |
| 144 | return Void(); |
| 145 | } |
| 146 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 147 | template <typename HalVersion> |
| 148 | Return<ErrorStatus> ArmnnDriverImpl<HalVersion>::prepareModel( |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 149 | const armnn::IRuntimePtr& runtime, |
| 150 | const armnn::IGpuAccTunedParametersPtr& clTunedParameters, |
| 151 | const DriverOptions& options, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 152 | const HalModel& model, |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 153 | const sp<IPreparedModelCallback>& cb, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 154 | bool float32ToFloat16) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 155 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 156 | ALOGV("ArmnnDriverImpl::prepareModel()"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 157 | |
| 158 | if (cb.get() == nullptr) |
| 159 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 160 | ALOGW("ArmnnDriverImpl::prepareModel: Invalid callback passed to prepareModel"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 161 | return ErrorStatus::INVALID_ARGUMENT; |
| 162 | } |
| 163 | |
| 164 | if (!runtime) |
| 165 | { |
| 166 | return FailPrepareModel(ErrorStatus::DEVICE_UNAVAILABLE, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 167 | "ArmnnDriverImpl::prepareModel: Device unavailable", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (!android::nn::validateModel(model)) |
| 171 | { |
| 172 | return FailPrepareModel(ErrorStatus::INVALID_ARGUMENT, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 173 | "ArmnnDriverImpl::prepareModel: Invalid model passed as input", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Deliberately ignore any unsupported operations requested by the options - |
| 177 | // at this point we're being asked to prepare a model that we've already declared support for |
| 178 | // and the operation indices may be different to those in getSupportedOperations anyway. |
| 179 | set<unsigned int> unsupportedOperations; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 180 | ModelToINetworkConverter<HalVersion> modelConverter(options.GetComputeDevice(), |
| 181 | model, |
| 182 | unsupportedOperations); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 183 | |
| 184 | if (modelConverter.GetConversionResult() != ConversionResult::Success) |
| 185 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 186 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, |
| 187 | "ArmnnDriverImpl::prepareModel: ModelToINetworkConverter failed", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 188 | return ErrorStatus::NONE; |
| 189 | } |
| 190 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 191 | // Optimize the network |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 192 | armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr); |
| 193 | armnn::OptimizerOptions OptOptions; |
| 194 | OptOptions.m_ReduceFp32ToFp16 = float32ToFloat16; |
| 195 | |
| 196 | try |
| 197 | { |
| 198 | optNet = armnn::Optimize(*modelConverter.GetINetwork(), |
| 199 | {options.GetComputeDevice()}, |
| 200 | runtime->GetDeviceSpec(), |
| 201 | OptOptions); |
| 202 | } |
| 203 | catch (armnn::Exception &e) |
| 204 | { |
| 205 | stringstream message; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 206 | message << "ArmnnDriverImpl::prepareModel: armnn::Exception (" << e.what() << ") caught from optimize."; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 207 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, message.str(), cb); |
| 208 | return ErrorStatus::NONE; |
| 209 | } |
| 210 | |
| 211 | // Check that the optimized network is valid. |
| 212 | if (!optNet) |
| 213 | { |
| 214 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 215 | "ArmnnDriverImpl::prepareModel: Invalid optimized network", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 216 | return ErrorStatus::NONE; |
| 217 | } |
| 218 | |
| 219 | // Export the optimized network graph to a dot file if an output dump directory |
| 220 | // has been specified in the drivers' arguments. |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 221 | ExportNetworkGraphToDotFile<HalModel>(*optNet, options.GetRequestInputsAndOutputsDumpDir(), model); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 222 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 223 | // Load it into the runtime. |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 224 | armnn::NetworkId netId = 0; |
| 225 | try |
| 226 | { |
| 227 | if (runtime->LoadNetwork(netId, move(optNet)) != armnn::Status::Success) |
| 228 | { |
| 229 | return FailPrepareModel(ErrorStatus::GENERAL_FAILURE, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 230 | "ArmnnDriverImpl::prepareModel: Network could not be loaded", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | catch (armnn::Exception& e) |
| 234 | { |
| 235 | stringstream message; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 236 | message << "ArmnnDriverImpl::prepareModel: armnn::Exception (" << e.what()<< ") caught from LoadNetwork."; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 237 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, message.str(), cb); |
| 238 | return ErrorStatus::NONE; |
| 239 | } |
| 240 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 241 | unique_ptr<ArmnnPreparedModel<HalVersion>> preparedModel( |
| 242 | new ArmnnPreparedModel<HalVersion>( |
| 243 | netId, |
| 244 | runtime.get(), |
| 245 | model, |
| 246 | options.GetRequestInputsAndOutputsDumpDir(), |
| 247 | options.IsGpuProfilingEnabled())); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 248 | |
| 249 | // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if |
| 250 | // this is enabled) before the first 'real' inference which removes the overhead of the first inference. |
| 251 | preparedModel->ExecuteWithDummyInputs(); |
| 252 | |
| 253 | if (clTunedParameters && |
| 254 | options.GetClTunedParametersMode() == armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters) |
| 255 | { |
| 256 | // Now that we've done one inference the CL kernel parameters will have been tuned, so save the updated file. |
| 257 | try |
| 258 | { |
| 259 | clTunedParameters->Save(options.GetClTunedParametersFile().c_str()); |
| 260 | } |
| 261 | catch (const armnn::Exception& error) |
| 262 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 263 | ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s", |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 264 | options.GetClTunedParametersFile().c_str(), error.what()); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | NotifyCallbackAndCheck(cb, ErrorStatus::NONE, preparedModel.release()); |
| 269 | |
| 270 | return ErrorStatus::NONE; |
| 271 | } |
| 272 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 273 | template <typename HalVersion> |
| 274 | Return<DeviceStatus> ArmnnDriverImpl<HalVersion>::getStatus() |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 275 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 276 | ALOGV("ArmnnDriver::getStatus()"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 277 | |
| 278 | return DeviceStatus::AVAILABLE; |
| 279 | } |
| 280 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame^] | 281 | // Class template specializations |
| 282 | template class ArmnnDriverImpl<HalVersion_1_0>; |
| 283 | |
| 284 | #ifdef ARMNN_ANDROID_NN_V1_1 |
| 285 | template class ArmnnDriverImpl<HalVersion_1_1>; |
| 286 | #endif |
| 287 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 288 | } // namespace armnn_driver |