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