blob: 78ef12fe66e10ee2e81c14aea30820fa356125a1 [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;
Finn Williamsd8fb5402021-05-19 20:52:00 +0100166 std::string msg;
167 armnn::INetworkProperties networkProperties(options.isAsyncModelExecutionEnabled(),
168 armnn::MemorySource::Undefined,
Finn Williamsca3a3e02021-06-11 15:04:02 +0100169 armnn::MemorySource::Undefined);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100170
telsoa01ce3e84a2018-08-31 09:31:35 +0100171 try
172 {
Finn Williamsd8fb5402021-05-19 20:52:00 +0100173 if (runtime->LoadNetwork(netId, move(optNet), msg, networkProperties) != armnn::Status::Success)
telsoa01ce3e84a2018-08-31 09:31:35 +0100174 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000175 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be loaded", cb);
telsoa01ce3e84a2018-08-31 09:31:35 +0100176 }
177 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000178 catch (std::exception& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100179 {
180 stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000181 message << "Exception (" << e.what()<< ") caught from LoadNetwork.";
Kevin Mayec1e5b82020-02-26 17:00:39 +0000182 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
183 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100184 }
185
Sadik Armaganb3021432021-01-13 15:56:51 +0000186 // Now that we have a networkId for the graph rename the exported files to use it
187 // so that we can associate the graph file and the input/output tensor exported files
188 RenameExportedFiles(serializedNetworkFileName,
189 dotGraphFileName,
190 options.GetRequestInputsAndOutputsDumpDir(),
191 netId);
Jim Flynn829ad302019-12-13 14:43:24 +0000192
Kevin Mayd5e94652019-11-07 14:02:14 +0000193 sp<ArmnnPreparedModel<HalPolicy>> preparedModel(
Mike Kellyb5fdf382019-06-11 16:35:25 +0100194 new ArmnnPreparedModel<HalPolicy>(
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100195 netId,
196 runtime.get(),
197 model,
198 options.GetRequestInputsAndOutputsDumpDir(),
Finn Williamsd8fb5402021-05-19 20:52:00 +0100199 options.IsGpuProfilingEnabled(),
Finn Williamsca3a3e02021-06-11 15:04:02 +0100200 options.isAsyncModelExecutionEnabled(),
201 options.getNoOfArmnnThreads()));
telsoa01ce3e84a2018-08-31 09:31:35 +0100202
203 // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if
204 // this is enabled) before the first 'real' inference which removes the overhead of the first inference.
Matthew Bentham16196e22019-04-01 17:17:58 +0100205 if (!preparedModel->ExecuteWithDummyInputs())
206 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000207 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be executed", cb);
Matthew Bentham16196e22019-04-01 17:17:58 +0100208 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100209
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 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000218 catch (std::exception& error)
telsoa01ce3e84a2018-08-31 09:31:35 +0100219 {
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100220 ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s",
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100221 options.GetClTunedParametersFile().c_str(), error.what());
telsoa01ce3e84a2018-08-31 09:31:35 +0100222 }
223 }
224
Kevin Mayec1e5b82020-02-26 17:00:39 +0000225 NotifyCallbackAndCheck(cb, V1_0::ErrorStatus::NONE, preparedModel);
telsoa01ce3e84a2018-08-31 09:31:35 +0100226
Kevin Mayec1e5b82020-02-26 17:00:39 +0000227 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100228}
229
arovir01b0717b52018-09-05 17:03:25 +0100230template<typename HalPolicy>
Mike Kellyb5fdf382019-06-11 16:35:25 +0100231Return<void> ArmnnDriverImpl<HalPolicy>::getSupportedOperations(const armnn::IRuntimePtr& runtime,
232 const DriverOptions& options,
233 const HalModel& model,
234 HalGetSupportedOperations_cb cb)
235{
Jim Flynn829ad302019-12-13 14:43:24 +0000236 std::stringstream ss;
237 ss << "ArmnnDriverImpl::getSupportedOperations()";
238 std::string fileName;
239 std::string timestamp;
240 if (!options.GetRequestInputsAndOutputsDumpDir().empty())
241 {
Colm Donelan08d9a1c2020-09-09 17:56:55 +0100242 ss << " : "
243 << options.GetRequestInputsAndOutputsDumpDir()
244 << "/"
245 << GetFileTimestamp()
246 << "_getSupportedOperations.txt";
Jim Flynn829ad302019-12-13 14:43:24 +0000247 }
248 ALOGV(ss.str().c_str());
249
250 if (!options.GetRequestInputsAndOutputsDumpDir().empty())
251 {
252 //dump the marker file
253 std::ofstream fileStream;
254 fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc);
255 if (fileStream.good())
256 {
257 fileStream << timestamp << std::endl;
258 }
259 fileStream.close();
260 }
Mike Kellyb5fdf382019-06-11 16:35:25 +0100261
262 vector<bool> result;
263
264 if (!runtime)
265 {
Kevin May42477c12020-03-26 13:34:14 +0000266 cb(HalErrorStatus::DEVICE_UNAVAILABLE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100267 return Void();
268 }
269
270 // Run general model validation, if this doesn't pass we shouldn't analyse the model anyway.
271 if (!android::nn::validateModel(model))
272 {
Kevin May42477c12020-03-26 13:34:14 +0000273 cb(HalErrorStatus::INVALID_ARGUMENT, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100274 return Void();
275 }
276
277 // Attempt to convert the model to an ArmNN input network (INetwork).
278 ModelToINetworkConverter<HalPolicy> modelConverter(options.GetBackends(),
279 model,
280 options.GetForcedUnsupportedOperations());
281
282 if (modelConverter.GetConversionResult() != ConversionResult::Success
283 && modelConverter.GetConversionResult() != ConversionResult::UnsupportedFeature)
284 {
Kevin May42477c12020-03-26 13:34:14 +0000285 cb(HalErrorStatus::GENERAL_FAILURE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100286 return Void();
287 }
288
289 // Check each operation if it was converted successfully and copy the flags
290 // into the result (vector<bool>) that we need to return to Android.
Kevin May42477c12020-03-26 13:34:14 +0000291 result.reserve(getMainModel(model).operations.size());
292 for (uint32_t operationIdx = 0;
293 operationIdx < getMainModel(model).operations.size();
294 ++operationIdx)
Mike Kellyb5fdf382019-06-11 16:35:25 +0100295 {
296 bool operationSupported = modelConverter.IsOperationSupported(operationIdx);
297 result.push_back(operationSupported);
298 }
299
Kevin May42477c12020-03-26 13:34:14 +0000300 cb(HalErrorStatus::NONE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100301 return Void();
302}
303
304template<typename HalPolicy>
Sadik Armagan188675f2021-02-12 17:16:42 +0000305Return<V1_0::DeviceStatus> ArmnnDriverImpl<HalPolicy>::getStatus()
telsoa01ce3e84a2018-08-31 09:31:35 +0100306{
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100307 ALOGV("ArmnnDriver::getStatus()");
telsoa01ce3e84a2018-08-31 09:31:35 +0100308
Sadik Armagan188675f2021-02-12 17:16:42 +0000309 return V1_0::DeviceStatus::AVAILABLE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100310}
311
arovir01b0717b52018-09-05 17:03:25 +0100312///
313/// Class template specializations
314///
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100315
arovir01b0717b52018-09-05 17:03:25 +0100316template class ArmnnDriverImpl<hal_1_0::HalPolicy>;
317
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100318#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100319template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100320#endif
321
Mike Kellyb5fdf382019-06-11 16:35:25 +0100322#ifdef ARMNN_ANDROID_NN_V1_2
323template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
324template class ArmnnDriverImpl<hal_1_2::HalPolicy>;
325#endif
326
Kevin May42477c12020-03-26 13:34:14 +0000327#ifdef ARMNN_ANDROID_NN_V1_3
328template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
329template class ArmnnDriverImpl<hal_1_2::HalPolicy>;
330template class ArmnnDriverImpl<hal_1_3::HalPolicy>;
331#endif
332
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100333} // namespace armnn_driver