blob: 3e4aab3c5b72d98a881656e277489aa027c51d92 [file] [log] [blame]
telsoa01ce3e84a2018-08-31 09:31:35 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
telsoa01ce3e84a2018-08-31 09:31:35 +01004//
5
Matteo Martincighe48bdff2018-09-03 13:50:50 +01006#define LOG_TAG "ArmnnDriver"
7
telsoa01ce3e84a2018-08-31 09:31:35 +01008#include "ArmnnDriverImpl.hpp"
telsoa01ce3e84a2018-08-31 09:31:35 +01009#include "ArmnnPreparedModel.hpp"
telsoa01ce3e84a2018-08-31 09:31:35 +010010
Kevin May42477c12020-03-26 13:34:14 +000011#if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) // Using ::android::hardware::neuralnetworks::V1_2
Mike Kellyb5fdf382019-06-11 16:35:25 +010012#include "ArmnnPreparedModel_1_2.hpp"
telsoa01ce3e84a2018-08-31 09:31:35 +010013#endif
14
Kevin May42477c12020-03-26 13:34:14 +000015#ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_2
16#include "ArmnnPreparedModel_1_3.hpp"
17#endif
18
19#include "Utils.hpp"
20
Mike Kellyb5fdf382019-06-11 16:35:25 +010021#include "ModelToINetworkConverter.hpp"
22#include "SystemPropertiesUtils.hpp"
Colm Donelan08d9a1c2020-09-09 17:56:55 +010023
Mike Kellyb5fdf382019-06-11 16:35:25 +010024#include <ValidateHal.h>
telsoa01ce3e84a2018-08-31 09:31:35 +010025#include <log/log.h>
26
27using namespace std;
28using namespace android;
29using namespace android::nn;
30using namespace android::hardware;
31
32namespace
33{
34
Matthew Bentham912b3622019-05-03 15:49:14 +010035void NotifyCallbackAndCheck(const sp<V1_0::IPreparedModelCallback>& callback,
Kevin Mayec1e5b82020-02-26 17:00:39 +000036 V1_0::ErrorStatus errorStatus,
Matthew Bentham912b3622019-05-03 15:49:14 +010037 const sp<V1_0::IPreparedModel>& preparedModelPtr)
telsoa01ce3e84a2018-08-31 09:31:35 +010038{
39 Return<void> returned = callback->notify(errorStatus, preparedModelPtr);
40 // This check is required, if the callback fails and it isn't checked it will bring down the service
41 if (!returned.isOk())
42 {
Matteo Martincighe48bdff2018-09-03 13:50:50 +010043 ALOGE("ArmnnDriverImpl::prepareModel: hidl callback failed to return properly: %s ",
Mike Kellyb5fdf382019-06-11 16:35:25 +010044 returned.description().c_str());
telsoa01ce3e84a2018-08-31 09:31:35 +010045 }
46}
47
Kevin Mayec1e5b82020-02-26 17:00:39 +000048Return<V1_0::ErrorStatus> FailPrepareModel(V1_0::ErrorStatus error,
49 const string& message,
50 const sp<V1_0::IPreparedModelCallback>& callback)
telsoa01ce3e84a2018-08-31 09:31:35 +010051{
Matteo Martincighe48bdff2018-09-03 13:50:50 +010052 ALOGW("ArmnnDriverImpl::prepareModel: %s", message.c_str());
telsoa01ce3e84a2018-08-31 09:31:35 +010053 NotifyCallbackAndCheck(callback, error, nullptr);
54 return error;
55}
56
57} // namespace
58
59namespace armnn_driver
60{
telsoa01ce3e84a2018-08-31 09:31:35 +010061
arovir01b0717b52018-09-05 17:03:25 +010062template<typename HalPolicy>
Kevin Mayec1e5b82020-02-26 17:00:39 +000063Return<V1_0::ErrorStatus> ArmnnDriverImpl<HalPolicy>::prepareModel(
telsoa01ce3e84a2018-08-31 09:31:35 +010064 const armnn::IRuntimePtr& runtime,
65 const armnn::IGpuAccTunedParametersPtr& clTunedParameters,
66 const DriverOptions& options,
Matteo Martincighe48bdff2018-09-03 13:50:50 +010067 const HalModel& model,
Matthew Bentham912b3622019-05-03 15:49:14 +010068 const sp<V1_0::IPreparedModelCallback>& cb,
Matteo Martincighe48bdff2018-09-03 13:50:50 +010069 bool float32ToFloat16)
telsoa01ce3e84a2018-08-31 09:31:35 +010070{
Matteo Martincighe48bdff2018-09-03 13:50:50 +010071 ALOGV("ArmnnDriverImpl::prepareModel()");
telsoa01ce3e84a2018-08-31 09:31:35 +010072
73 if (cb.get() == nullptr)
74 {
Matteo Martincighe48bdff2018-09-03 13:50:50 +010075 ALOGW("ArmnnDriverImpl::prepareModel: Invalid callback passed to prepareModel");
Kevin Mayec1e5b82020-02-26 17:00:39 +000076 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa01ce3e84a2018-08-31 09:31:35 +010077 }
78
79 if (!runtime)
80 {
Kevin Mayec1e5b82020-02-26 17:00:39 +000081 return FailPrepareModel(V1_0::ErrorStatus::DEVICE_UNAVAILABLE, "Device unavailable", cb);
telsoa01ce3e84a2018-08-31 09:31:35 +010082 }
83
84 if (!android::nn::validateModel(model))
85 {
Kevin Mayec1e5b82020-02-26 17:00:39 +000086 return FailPrepareModel(V1_0::ErrorStatus::INVALID_ARGUMENT, "Invalid model passed as input", cb);
telsoa01ce3e84a2018-08-31 09:31:35 +010087 }
88
89 // Deliberately ignore any unsupported operations requested by the options -
90 // at this point we're being asked to prepare a model that we've already declared support for
91 // and the operation indices may be different to those in getSupportedOperations anyway.
92 set<unsigned int> unsupportedOperations;
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010093 ModelToINetworkConverter<HalPolicy> modelConverter(options.GetBackends(),
94 model,
95 unsupportedOperations);
telsoa01ce3e84a2018-08-31 09:31:35 +010096
97 if (modelConverter.GetConversionResult() != ConversionResult::Success)
98 {
Kevin Mayec1e5b82020-02-26 17:00:39 +000099 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "ModelToINetworkConverter failed", cb);
100 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100101 }
102
Sadik Armaganb3021432021-01-13 15:56:51 +0000103 // Serialize the network graph to a .armnn file if an output directory
104 // has been specified in the drivers' arguments.
105 auto serializedNetworkFileName =
106 SerializeNetwork(*modelConverter.GetINetwork(), options.GetRequestInputsAndOutputsDumpDir());
107
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100108 // Optimize the network
telsoa01ce3e84a2018-08-31 09:31:35 +0100109 armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr);
110 armnn::OptimizerOptions OptOptions;
111 OptOptions.m_ReduceFp32ToFp16 = float32ToFloat16;
112
Mike Kelly7ed56dd2020-09-30 20:22:56 +0100113 armnn::BackendOptions gpuAcc("GpuAcc",
114 {
Sadik Armaganf36e10b2021-01-11 16:34:01 +0000115 { "FastMathEnabled", options.IsFastMathEnabled() },
116 { "SaveCachedNetwork", options.SaveCachedNetwork() },
Finn Williamsf5ca16c2021-02-12 14:26:23 +0000117 { "CachedNetworkFilePath", options.GetCachedNetworkFilePath() },
118 { "MLGOTuningFilePath", options.GetClMLGOTunedParametersFile() }
119
Mike Kelly7ed56dd2020-09-30 20:22:56 +0100120 });
Finn Williamsf5ca16c2021-02-12 14:26:23 +0000121
Mike Kelly7ed56dd2020-09-30 20:22:56 +0100122 armnn::BackendOptions cpuAcc("CpuAcc",
123 {
Matthew Sloyancd639c92021-02-11 16:57:38 +0000124 { "FastMathEnabled", options.IsFastMathEnabled() },
125 { "NumberOfThreads", options.GetNumberOfThreads() }
Mike Kelly7ed56dd2020-09-30 20:22:56 +0100126 });
127 OptOptions.m_ModelOptions.push_back(gpuAcc);
128 OptOptions.m_ModelOptions.push_back(cpuAcc);
129
jimfly0107dedda2018-10-09 12:29:41 +0100130 std::vector<std::string> errMessages;
telsoa01ce3e84a2018-08-31 09:31:35 +0100131 try
132 {
133 optNet = armnn::Optimize(*modelConverter.GetINetwork(),
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100134 options.GetBackends(),
telsoa01ce3e84a2018-08-31 09:31:35 +0100135 runtime->GetDeviceSpec(),
jimfly0107dedda2018-10-09 12:29:41 +0100136 OptOptions,
137 errMessages);
telsoa01ce3e84a2018-08-31 09:31:35 +0100138 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000139 catch (std::exception &e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100140 {
141 stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000142 message << "Exception (" << e.what() << ") caught from optimize.";
Kevin Mayec1e5b82020-02-26 17:00:39 +0000143 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
144 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100145 }
146
147 // Check that the optimized network is valid.
148 if (!optNet)
149 {
jimfly0107dedda2018-10-09 12:29:41 +0100150 stringstream message;
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100151 message << "Invalid optimized network";
152 for (const string& msg : errMessages)
153 {
jimfly0107dedda2018-10-09 12:29:41 +0100154 message << "\n" << msg;
155 }
Kevin Mayec1e5b82020-02-26 17:00:39 +0000156 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
157 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100158 }
159
160 // Export the optimized network graph to a dot file if an output dump directory
161 // has been specified in the drivers' arguments.
Jim Flynn829ad302019-12-13 14:43:24 +0000162 std::string dotGraphFileName = ExportNetworkGraphToDotFile(*optNet, options.GetRequestInputsAndOutputsDumpDir());
telsoa01ce3e84a2018-08-31 09:31:35 +0100163
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100164 // Load it into the runtime.
telsoa01ce3e84a2018-08-31 09:31:35 +0100165 armnn::NetworkId netId = 0;
166 try
167 {
168 if (runtime->LoadNetwork(netId, move(optNet)) != armnn::Status::Success)
169 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000170 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be loaded", cb);
telsoa01ce3e84a2018-08-31 09:31:35 +0100171 }
172 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000173 catch (std::exception& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100174 {
175 stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000176 message << "Exception (" << e.what()<< ") caught from LoadNetwork.";
Kevin Mayec1e5b82020-02-26 17:00:39 +0000177 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
178 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100179 }
180
Sadik Armaganb3021432021-01-13 15:56:51 +0000181 // Now that we have a networkId for the graph rename the exported files to use it
182 // so that we can associate the graph file and the input/output tensor exported files
183 RenameExportedFiles(serializedNetworkFileName,
184 dotGraphFileName,
185 options.GetRequestInputsAndOutputsDumpDir(),
186 netId);
Jim Flynn829ad302019-12-13 14:43:24 +0000187
Kevin Mayd5e94652019-11-07 14:02:14 +0000188 sp<ArmnnPreparedModel<HalPolicy>> preparedModel(
Mike Kellyb5fdf382019-06-11 16:35:25 +0100189 new ArmnnPreparedModel<HalPolicy>(
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100190 netId,
191 runtime.get(),
192 model,
193 options.GetRequestInputsAndOutputsDumpDir(),
194 options.IsGpuProfilingEnabled()));
telsoa01ce3e84a2018-08-31 09:31:35 +0100195
196 // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if
197 // this is enabled) before the first 'real' inference which removes the overhead of the first inference.
Matthew Bentham16196e22019-04-01 17:17:58 +0100198 if (!preparedModel->ExecuteWithDummyInputs())
199 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000200 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be executed", cb);
Matthew Bentham16196e22019-04-01 17:17:58 +0100201 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100202
203 if (clTunedParameters &&
204 options.GetClTunedParametersMode() == armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters)
205 {
206 // Now that we've done one inference the CL kernel parameters will have been tuned, so save the updated file.
207 try
208 {
209 clTunedParameters->Save(options.GetClTunedParametersFile().c_str());
210 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000211 catch (std::exception& error)
telsoa01ce3e84a2018-08-31 09:31:35 +0100212 {
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100213 ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s",
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100214 options.GetClTunedParametersFile().c_str(), error.what());
telsoa01ce3e84a2018-08-31 09:31:35 +0100215 }
216 }
217
Kevin Mayec1e5b82020-02-26 17:00:39 +0000218 NotifyCallbackAndCheck(cb, V1_0::ErrorStatus::NONE, preparedModel);
telsoa01ce3e84a2018-08-31 09:31:35 +0100219
Kevin Mayec1e5b82020-02-26 17:00:39 +0000220 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100221}
222
arovir01b0717b52018-09-05 17:03:25 +0100223template<typename HalPolicy>
Mike Kellyb5fdf382019-06-11 16:35:25 +0100224Return<void> ArmnnDriverImpl<HalPolicy>::getSupportedOperations(const armnn::IRuntimePtr& runtime,
225 const DriverOptions& options,
226 const HalModel& model,
227 HalGetSupportedOperations_cb cb)
228{
Jim Flynn829ad302019-12-13 14:43:24 +0000229 std::stringstream ss;
230 ss << "ArmnnDriverImpl::getSupportedOperations()";
231 std::string fileName;
232 std::string timestamp;
233 if (!options.GetRequestInputsAndOutputsDumpDir().empty())
234 {
Colm Donelan08d9a1c2020-09-09 17:56:55 +0100235 ss << " : "
236 << options.GetRequestInputsAndOutputsDumpDir()
237 << "/"
238 << GetFileTimestamp()
239 << "_getSupportedOperations.txt";
Jim Flynn829ad302019-12-13 14:43:24 +0000240 }
241 ALOGV(ss.str().c_str());
242
243 if (!options.GetRequestInputsAndOutputsDumpDir().empty())
244 {
245 //dump the marker file
246 std::ofstream fileStream;
247 fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc);
248 if (fileStream.good())
249 {
250 fileStream << timestamp << std::endl;
251 }
252 fileStream.close();
253 }
Mike Kellyb5fdf382019-06-11 16:35:25 +0100254
255 vector<bool> result;
256
257 if (!runtime)
258 {
Kevin May42477c12020-03-26 13:34:14 +0000259 cb(HalErrorStatus::DEVICE_UNAVAILABLE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100260 return Void();
261 }
262
263 // Run general model validation, if this doesn't pass we shouldn't analyse the model anyway.
264 if (!android::nn::validateModel(model))
265 {
Kevin May42477c12020-03-26 13:34:14 +0000266 cb(HalErrorStatus::INVALID_ARGUMENT, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100267 return Void();
268 }
269
270 // Attempt to convert the model to an ArmNN input network (INetwork).
271 ModelToINetworkConverter<HalPolicy> modelConverter(options.GetBackends(),
272 model,
273 options.GetForcedUnsupportedOperations());
274
275 if (modelConverter.GetConversionResult() != ConversionResult::Success
276 && modelConverter.GetConversionResult() != ConversionResult::UnsupportedFeature)
277 {
Kevin May42477c12020-03-26 13:34:14 +0000278 cb(HalErrorStatus::GENERAL_FAILURE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100279 return Void();
280 }
281
282 // Check each operation if it was converted successfully and copy the flags
283 // into the result (vector<bool>) that we need to return to Android.
Kevin May42477c12020-03-26 13:34:14 +0000284 result.reserve(getMainModel(model).operations.size());
285 for (uint32_t operationIdx = 0;
286 operationIdx < getMainModel(model).operations.size();
287 ++operationIdx)
Mike Kellyb5fdf382019-06-11 16:35:25 +0100288 {
289 bool operationSupported = modelConverter.IsOperationSupported(operationIdx);
290 result.push_back(operationSupported);
291 }
292
Kevin May42477c12020-03-26 13:34:14 +0000293 cb(HalErrorStatus::NONE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100294 return Void();
295}
296
297template<typename HalPolicy>
Sadik Armagan188675f2021-02-12 17:16:42 +0000298Return<V1_0::DeviceStatus> ArmnnDriverImpl<HalPolicy>::getStatus()
telsoa01ce3e84a2018-08-31 09:31:35 +0100299{
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100300 ALOGV("ArmnnDriver::getStatus()");
telsoa01ce3e84a2018-08-31 09:31:35 +0100301
Sadik Armagan188675f2021-02-12 17:16:42 +0000302 return V1_0::DeviceStatus::AVAILABLE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100303}
304
arovir01b0717b52018-09-05 17:03:25 +0100305///
306/// Class template specializations
307///
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100308
arovir01b0717b52018-09-05 17:03:25 +0100309template class ArmnnDriverImpl<hal_1_0::HalPolicy>;
310
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100311#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100312template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100313#endif
314
Mike Kellyb5fdf382019-06-11 16:35:25 +0100315#ifdef ARMNN_ANDROID_NN_V1_2
316template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
317template class ArmnnDriverImpl<hal_1_2::HalPolicy>;
318#endif
319
Kevin May42477c12020-03-26 13:34:14 +0000320#ifdef ARMNN_ANDROID_NN_V1_3
321template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
322template class ArmnnDriverImpl<hal_1_2::HalPolicy>;
323template class ArmnnDriverImpl<hal_1_3::HalPolicy>;
324#endif
325
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100326} // namespace armnn_driver