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 | { |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame^] | 123 | return FailPrepareModel(ErrorStatus::DEVICE_UNAVAILABLE, "Device unavailable", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (!android::nn::validateModel(model)) |
| 127 | { |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame^] | 128 | return FailPrepareModel(ErrorStatus::INVALID_ARGUMENT, "Invalid model passed as input", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Deliberately ignore any unsupported operations requested by the options - |
| 132 | // at this point we're being asked to prepare a model that we've already declared support for |
| 133 | // and the operation indices may be different to those in getSupportedOperations anyway. |
| 134 | set<unsigned int> unsupportedOperations; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 135 | ModelToINetworkConverter<HalPolicy> modelConverter(options.GetComputeDevice(), |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 136 | model, |
| 137 | unsupportedOperations); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 138 | |
| 139 | if (modelConverter.GetConversionResult() != ConversionResult::Success) |
| 140 | { |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame^] | 141 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, "ModelToINetworkConverter failed", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 142 | return ErrorStatus::NONE; |
| 143 | } |
| 144 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 145 | // Optimize the network |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 146 | armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr); |
| 147 | armnn::OptimizerOptions OptOptions; |
| 148 | OptOptions.m_ReduceFp32ToFp16 = float32ToFloat16; |
| 149 | |
jimfly01 | 07dedda | 2018-10-09 12:29:41 +0100 | [diff] [blame] | 150 | std::vector<std::string> errMessages; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 151 | try |
| 152 | { |
| 153 | optNet = armnn::Optimize(*modelConverter.GetINetwork(), |
| 154 | {options.GetComputeDevice()}, |
| 155 | runtime->GetDeviceSpec(), |
jimfly01 | 07dedda | 2018-10-09 12:29:41 +0100 | [diff] [blame] | 156 | OptOptions, |
| 157 | errMessages); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 158 | } |
| 159 | catch (armnn::Exception &e) |
| 160 | { |
| 161 | stringstream message; |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame^] | 162 | message << "armnn::Exception (" << e.what() << ") caught from optimize."; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 163 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, message.str(), cb); |
| 164 | return ErrorStatus::NONE; |
| 165 | } |
| 166 | |
| 167 | // Check that the optimized network is valid. |
| 168 | if (!optNet) |
| 169 | { |
jimfly01 | 07dedda | 2018-10-09 12:29:41 +0100 | [diff] [blame] | 170 | stringstream message; |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame^] | 171 | message << "Invalid optimized network"; |
| 172 | for (const string& msg : errMessages) |
| 173 | { |
jimfly01 | 07dedda | 2018-10-09 12:29:41 +0100 | [diff] [blame] | 174 | message << "\n" << msg; |
| 175 | } |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame^] | 176 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, message.str(), cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 177 | return ErrorStatus::NONE; |
| 178 | } |
| 179 | |
| 180 | // Export the optimized network graph to a dot file if an output dump directory |
| 181 | // has been specified in the drivers' arguments. |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 182 | ExportNetworkGraphToDotFile<HalModel>(*optNet, options.GetRequestInputsAndOutputsDumpDir(), model); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 183 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 184 | // Load it into the runtime. |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 185 | armnn::NetworkId netId = 0; |
| 186 | try |
| 187 | { |
| 188 | if (runtime->LoadNetwork(netId, move(optNet)) != armnn::Status::Success) |
| 189 | { |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame^] | 190 | return FailPrepareModel(ErrorStatus::GENERAL_FAILURE, "Network could not be loaded", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | catch (armnn::Exception& e) |
| 194 | { |
| 195 | stringstream message; |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame^] | 196 | message << "armnn::Exception (" << e.what()<< ") caught from LoadNetwork."; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 197 | FailPrepareModel(ErrorStatus::GENERAL_FAILURE, message.str(), cb); |
| 198 | return ErrorStatus::NONE; |
| 199 | } |
| 200 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 201 | unique_ptr<ArmnnPreparedModel<HalPolicy>> preparedModel( |
| 202 | new ArmnnPreparedModel<HalPolicy>( |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 203 | netId, |
| 204 | runtime.get(), |
| 205 | model, |
| 206 | options.GetRequestInputsAndOutputsDumpDir(), |
| 207 | options.IsGpuProfilingEnabled())); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 208 | |
| 209 | // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if |
| 210 | // this is enabled) before the first 'real' inference which removes the overhead of the first inference. |
| 211 | preparedModel->ExecuteWithDummyInputs(); |
| 212 | |
| 213 | if (clTunedParameters && |
| 214 | options.GetClTunedParametersMode() == armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters) |
| 215 | { |
| 216 | // Now that we've done one inference the CL kernel parameters will have been tuned, so save the updated file. |
| 217 | try |
| 218 | { |
| 219 | clTunedParameters->Save(options.GetClTunedParametersFile().c_str()); |
| 220 | } |
| 221 | catch (const armnn::Exception& error) |
| 222 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 223 | ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s", |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame^] | 224 | options.GetClTunedParametersFile().c_str(), error.what()); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | NotifyCallbackAndCheck(cb, ErrorStatus::NONE, preparedModel.release()); |
| 229 | |
| 230 | return ErrorStatus::NONE; |
| 231 | } |
| 232 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 233 | template<typename HalPolicy> |
| 234 | Return<DeviceStatus> ArmnnDriverImpl<HalPolicy>::getStatus() |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 235 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 236 | ALOGV("ArmnnDriver::getStatus()"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 237 | |
| 238 | return DeviceStatus::AVAILABLE; |
| 239 | } |
| 240 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 241 | /// |
| 242 | /// Class template specializations |
| 243 | /// |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 244 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 245 | template class ArmnnDriverImpl<hal_1_0::HalPolicy>; |
| 246 | |
Matteo Martincigh | 8b287c2 | 2018-09-07 09:25:10 +0100 | [diff] [blame] | 247 | #ifdef ARMNN_ANDROID_NN_V1_1 |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 248 | template class ArmnnDriverImpl<hal_1_1::HalPolicy>; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 249 | #endif |
| 250 | |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame^] | 251 | } // namespace armnn_driver |