telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [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 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 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" |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 9 | #include "ArmnnPreparedModel.hpp" |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 10 | #include "ModelToINetworkConverter.hpp" |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 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 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 29 | void NotifyCallbackAndCheck(const sp<IPreparedModelCallback>& callback, |
| 30 | ErrorStatus errorStatus, |
| 31 | const sp<IPreparedModel>& preparedModelPtr) |
| 32 | { |
| 33 | Return<void> returned = callback->notify(errorStatus, preparedModelPtr); |
| 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 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 37 | ALOGE("ArmnnDriverImpl::prepareModel: hidl callback failed to return properly: %s ", |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 38 | returned.description().c_str()); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | Return<ErrorStatus> FailPrepareModel(ErrorStatus error, |
| 43 | const string& message, |
| 44 | const sp<IPreparedModelCallback>& callback) |
| 45 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 46 | ALOGW("ArmnnDriverImpl::prepareModel: %s", message.c_str()); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 47 | NotifyCallbackAndCheck(callback, error, nullptr); |
| 48 | return error; |
| 49 | } |
| 50 | |
| 51 | } // namespace |
| 52 | |
| 53 | namespace armnn_driver |
| 54 | { |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 55 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 56 | template<typename HalPolicy> |
| 57 | Return<void> ArmnnDriverImpl<HalPolicy>::getSupportedOperations(const armnn::IRuntimePtr& runtime, |
| 58 | const DriverOptions& options, |
| 59 | const HalModel& model, |
| 60 | HalGetSupportedOperations_cb cb) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 61 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 62 | ALOGV("ArmnnDriverImpl::getSupportedOperations()"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 63 | |
| 64 | vector<bool> result; |
| 65 | |
| 66 | if (!runtime) |
| 67 | { |
| 68 | cb(ErrorStatus::DEVICE_UNAVAILABLE, result); |
| 69 | return Void(); |
| 70 | } |
| 71 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 72 | // 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] | 73 | if (!android::nn::validateModel(model)) |
| 74 | { |
| 75 | cb(ErrorStatus::INVALID_ARGUMENT, result); |
| 76 | return Void(); |
| 77 | } |
| 78 | |
| 79 | // Attempt to convert the model to an ArmNN input network (INetwork). |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 80 | ModelToINetworkConverter<HalPolicy> modelConverter(options.GetComputeDevice(), |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 81 | model, |
| 82 | options.GetForcedUnsupportedOperations()); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 83 | |
| 84 | if (modelConverter.GetConversionResult() != ConversionResult::Success |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 85 | && modelConverter.GetConversionResult() != ConversionResult::UnsupportedFeature) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 86 | { |
| 87 | cb(ErrorStatus::GENERAL_FAILURE, result); |
| 88 | return Void(); |
| 89 | } |
| 90 | |
| 91 | // Check each operation if it was converted successfully and copy the flags |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 92 | // into the result (vector<bool>) that we need to return to Android. |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 93 | result.reserve(model.operations.size()); |
| 94 | for (uint32_t operationIdx = 0; operationIdx < model.operations.size(); operationIdx++) |
| 95 | { |
| 96 | bool operationSupported = modelConverter.IsOperationSupported(operationIdx); |
| 97 | result.push_back(operationSupported); |
| 98 | } |
| 99 | |
| 100 | cb(ErrorStatus::NONE, result); |
| 101 | return Void(); |
| 102 | } |
| 103 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 104 | template<typename HalPolicy> |
| 105 | Return<ErrorStatus> ArmnnDriverImpl<HalPolicy>::prepareModel( |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 106 | const armnn::IRuntimePtr& runtime, |
| 107 | const armnn::IGpuAccTunedParametersPtr& clTunedParameters, |
| 108 | const DriverOptions& options, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 109 | const HalModel& model, |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 110 | const sp<IPreparedModelCallback>& cb, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 111 | bool float32ToFloat16) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 112 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 113 | ALOGV("ArmnnDriverImpl::prepareModel()"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 114 | |
| 115 | if (cb.get() == nullptr) |
| 116 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 117 | ALOGW("ArmnnDriverImpl::prepareModel: Invalid callback passed to prepareModel"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 118 | return ErrorStatus::INVALID_ARGUMENT; |
| 119 | } |
| 120 | |
| 121 | if (!runtime) |
| 122 | { |
| 123 | return FailPrepareModel(ErrorStatus::DEVICE_UNAVAILABLE, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 124 | "ArmnnDriverImpl::prepareModel: Device unavailable", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | if (!android::nn::validateModel(model)) |
| 128 | { |
| 129 | return FailPrepareModel(ErrorStatus::INVALID_ARGUMENT, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 130 | "ArmnnDriverImpl::prepareModel: Invalid model passed as input", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Deliberately ignore any unsupported operations requested by the options - |
| 134 | // at this point we're being asked to prepare a model that we've already declared support for |
| 135 | // and the operation indices may be different to those in getSupportedOperations anyway. |
| 136 | set<unsigned int> unsupportedOperations; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 137 | ModelToINetworkConverter<HalPolicy> modelConverter(options.GetComputeDevice(), |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 138 | model, |
| 139 | unsupportedOperations); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 140 | |
| 141 | if (modelConverter.GetConversionResult() != ConversionResult::Success) |
| 142 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 143 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, |
| 144 | "ArmnnDriverImpl::prepareModel: ModelToINetworkConverter failed", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 145 | return ErrorStatus::NONE; |
| 146 | } |
| 147 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 148 | // Optimize the network |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 149 | armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr); |
| 150 | armnn::OptimizerOptions OptOptions; |
| 151 | OptOptions.m_ReduceFp32ToFp16 = float32ToFloat16; |
| 152 | |
| 153 | try |
| 154 | { |
| 155 | optNet = armnn::Optimize(*modelConverter.GetINetwork(), |
| 156 | {options.GetComputeDevice()}, |
| 157 | runtime->GetDeviceSpec(), |
| 158 | OptOptions); |
| 159 | } |
| 160 | catch (armnn::Exception &e) |
| 161 | { |
| 162 | stringstream message; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 163 | message << "ArmnnDriverImpl::prepareModel: armnn::Exception (" << e.what() << ") caught from optimize."; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 164 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, message.str(), cb); |
| 165 | return ErrorStatus::NONE; |
| 166 | } |
| 167 | |
| 168 | // Check that the optimized network is valid. |
| 169 | if (!optNet) |
| 170 | { |
| 171 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 172 | "ArmnnDriverImpl::prepareModel: Invalid optimized network", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 173 | return ErrorStatus::NONE; |
| 174 | } |
| 175 | |
| 176 | // Export the optimized network graph to a dot file if an output dump directory |
| 177 | // has been specified in the drivers' arguments. |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 178 | ExportNetworkGraphToDotFile<HalModel>(*optNet, options.GetRequestInputsAndOutputsDumpDir(), model); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 179 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 180 | // Load it into the runtime. |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 181 | armnn::NetworkId netId = 0; |
| 182 | try |
| 183 | { |
| 184 | if (runtime->LoadNetwork(netId, move(optNet)) != armnn::Status::Success) |
| 185 | { |
| 186 | return FailPrepareModel(ErrorStatus::GENERAL_FAILURE, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 187 | "ArmnnDriverImpl::prepareModel: Network could not be loaded", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | catch (armnn::Exception& e) |
| 191 | { |
| 192 | stringstream message; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 193 | message << "ArmnnDriverImpl::prepareModel: armnn::Exception (" << e.what()<< ") caught from LoadNetwork."; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 194 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, message.str(), cb); |
| 195 | return ErrorStatus::NONE; |
| 196 | } |
| 197 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 198 | unique_ptr<ArmnnPreparedModel<HalPolicy>> preparedModel( |
| 199 | new ArmnnPreparedModel<HalPolicy>( |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 200 | netId, |
| 201 | runtime.get(), |
| 202 | model, |
| 203 | options.GetRequestInputsAndOutputsDumpDir(), |
| 204 | options.IsGpuProfilingEnabled())); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 205 | |
| 206 | // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if |
| 207 | // this is enabled) before the first 'real' inference which removes the overhead of the first inference. |
| 208 | preparedModel->ExecuteWithDummyInputs(); |
| 209 | |
| 210 | if (clTunedParameters && |
| 211 | options.GetClTunedParametersMode() == armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters) |
| 212 | { |
| 213 | // Now that we've done one inference the CL kernel parameters will have been tuned, so save the updated file. |
| 214 | try |
| 215 | { |
| 216 | clTunedParameters->Save(options.GetClTunedParametersFile().c_str()); |
| 217 | } |
| 218 | catch (const armnn::Exception& error) |
| 219 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 220 | ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s", |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 221 | options.GetClTunedParametersFile().c_str(), error.what()); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | NotifyCallbackAndCheck(cb, ErrorStatus::NONE, preparedModel.release()); |
| 226 | |
| 227 | return ErrorStatus::NONE; |
| 228 | } |
| 229 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 230 | template<typename HalPolicy> |
| 231 | Return<DeviceStatus> ArmnnDriverImpl<HalPolicy>::getStatus() |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 232 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 233 | ALOGV("ArmnnDriver::getStatus()"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 234 | |
| 235 | return DeviceStatus::AVAILABLE; |
| 236 | } |
| 237 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 238 | /// |
| 239 | /// Class template specializations |
| 240 | /// |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 241 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 242 | template class ArmnnDriverImpl<hal_1_0::HalPolicy>; |
| 243 | |
Matteo Martincigh | 8b287c2 | 2018-09-07 09:25:10 +0100 | [diff] [blame^] | 244 | #ifdef ARMNN_ANDROID_NN_V1_1 |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 245 | template class ArmnnDriverImpl<hal_1_1::HalPolicy>; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 246 | #endif |
| 247 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 248 | } // namespace armnn_driver |