blob: 9c6d51fd056403f1ee7797aca8b1bdcd11c27678 [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"
23#include <ValidateHal.h>
telsoa01ce3e84a2018-08-31 09:31:35 +010024#include <log/log.h>
25
26using namespace std;
27using namespace android;
28using namespace android::nn;
29using namespace android::hardware;
30
31namespace
32{
33
Matthew Bentham912b3622019-05-03 15:49:14 +010034void NotifyCallbackAndCheck(const sp<V1_0::IPreparedModelCallback>& callback,
Kevin Mayec1e5b82020-02-26 17:00:39 +000035 V1_0::ErrorStatus errorStatus,
Matthew Bentham912b3622019-05-03 15:49:14 +010036 const sp<V1_0::IPreparedModel>& preparedModelPtr)
telsoa01ce3e84a2018-08-31 09:31:35 +010037{
38 Return<void> returned = callback->notify(errorStatus, preparedModelPtr);
39 // This check is required, if the callback fails and it isn't checked it will bring down the service
40 if (!returned.isOk())
41 {
Matteo Martincighe48bdff2018-09-03 13:50:50 +010042 ALOGE("ArmnnDriverImpl::prepareModel: hidl callback failed to return properly: %s ",
Mike Kellyb5fdf382019-06-11 16:35:25 +010043 returned.description().c_str());
telsoa01ce3e84a2018-08-31 09:31:35 +010044 }
45}
46
Kevin Mayec1e5b82020-02-26 17:00:39 +000047Return<V1_0::ErrorStatus> FailPrepareModel(V1_0::ErrorStatus error,
48 const string& message,
49 const sp<V1_0::IPreparedModelCallback>& callback)
telsoa01ce3e84a2018-08-31 09:31:35 +010050{
Matteo Martincighe48bdff2018-09-03 13:50:50 +010051 ALOGW("ArmnnDriverImpl::prepareModel: %s", message.c_str());
telsoa01ce3e84a2018-08-31 09:31:35 +010052 NotifyCallbackAndCheck(callback, error, nullptr);
53 return error;
54}
55
Mike Kellyb5fdf382019-06-11 16:35:25 +010056
telsoa01ce3e84a2018-08-31 09:31:35 +010057} // 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
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100103 // Optimize the network
telsoa01ce3e84a2018-08-31 09:31:35 +0100104 armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr);
105 armnn::OptimizerOptions OptOptions;
106 OptOptions.m_ReduceFp32ToFp16 = float32ToFloat16;
107
jimfly0107dedda2018-10-09 12:29:41 +0100108 std::vector<std::string> errMessages;
telsoa01ce3e84a2018-08-31 09:31:35 +0100109 try
110 {
111 optNet = armnn::Optimize(*modelConverter.GetINetwork(),
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100112 options.GetBackends(),
telsoa01ce3e84a2018-08-31 09:31:35 +0100113 runtime->GetDeviceSpec(),
jimfly0107dedda2018-10-09 12:29:41 +0100114 OptOptions,
115 errMessages);
telsoa01ce3e84a2018-08-31 09:31:35 +0100116 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000117 catch (std::exception &e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100118 {
119 stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000120 message << "Exception (" << e.what() << ") caught from optimize.";
Kevin Mayec1e5b82020-02-26 17:00:39 +0000121 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
122 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100123 }
124
125 // Check that the optimized network is valid.
126 if (!optNet)
127 {
jimfly0107dedda2018-10-09 12:29:41 +0100128 stringstream message;
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100129 message << "Invalid optimized network";
130 for (const string& msg : errMessages)
131 {
jimfly0107dedda2018-10-09 12:29:41 +0100132 message << "\n" << msg;
133 }
Kevin Mayec1e5b82020-02-26 17:00:39 +0000134 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
135 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100136 }
137
138 // Export the optimized network graph to a dot file if an output dump directory
139 // has been specified in the drivers' arguments.
Jim Flynn829ad302019-12-13 14:43:24 +0000140 std::string dotGraphFileName = ExportNetworkGraphToDotFile(*optNet, options.GetRequestInputsAndOutputsDumpDir());
telsoa01ce3e84a2018-08-31 09:31:35 +0100141
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100142 // Load it into the runtime.
telsoa01ce3e84a2018-08-31 09:31:35 +0100143 armnn::NetworkId netId = 0;
144 try
145 {
146 if (runtime->LoadNetwork(netId, move(optNet)) != armnn::Status::Success)
147 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000148 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be loaded", cb);
telsoa01ce3e84a2018-08-31 09:31:35 +0100149 }
150 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000151 catch (std::exception& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100152 {
153 stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000154 message << "Exception (" << e.what()<< ") caught from LoadNetwork.";
Kevin Mayec1e5b82020-02-26 17:00:39 +0000155 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
156 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100157 }
158
Jim Flynn829ad302019-12-13 14:43:24 +0000159 // Now that we have a networkId for the graph rename the dump file to use it
160 // so that we can associate the graph file and the input/output tensor dump files
161 RenameGraphDotFile(dotGraphFileName,
162 options.GetRequestInputsAndOutputsDumpDir(),
163 netId);
164
Kevin Mayd5e94652019-11-07 14:02:14 +0000165 sp<ArmnnPreparedModel<HalPolicy>> preparedModel(
Mike Kellyb5fdf382019-06-11 16:35:25 +0100166 new ArmnnPreparedModel<HalPolicy>(
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100167 netId,
168 runtime.get(),
169 model,
170 options.GetRequestInputsAndOutputsDumpDir(),
171 options.IsGpuProfilingEnabled()));
telsoa01ce3e84a2018-08-31 09:31:35 +0100172
173 // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if
174 // this is enabled) before the first 'real' inference which removes the overhead of the first inference.
Matthew Bentham16196e22019-04-01 17:17:58 +0100175 if (!preparedModel->ExecuteWithDummyInputs())
176 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000177 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be executed", cb);
Matthew Bentham16196e22019-04-01 17:17:58 +0100178 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100179
180 if (clTunedParameters &&
181 options.GetClTunedParametersMode() == armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters)
182 {
183 // Now that we've done one inference the CL kernel parameters will have been tuned, so save the updated file.
184 try
185 {
186 clTunedParameters->Save(options.GetClTunedParametersFile().c_str());
187 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000188 catch (std::exception& error)
telsoa01ce3e84a2018-08-31 09:31:35 +0100189 {
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100190 ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s",
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100191 options.GetClTunedParametersFile().c_str(), error.what());
telsoa01ce3e84a2018-08-31 09:31:35 +0100192 }
193 }
194
Kevin Mayec1e5b82020-02-26 17:00:39 +0000195 NotifyCallbackAndCheck(cb, V1_0::ErrorStatus::NONE, preparedModel);
telsoa01ce3e84a2018-08-31 09:31:35 +0100196
Kevin Mayec1e5b82020-02-26 17:00:39 +0000197 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100198}
199
arovir01b0717b52018-09-05 17:03:25 +0100200template<typename HalPolicy>
Mike Kellyb5fdf382019-06-11 16:35:25 +0100201Return<void> ArmnnDriverImpl<HalPolicy>::getSupportedOperations(const armnn::IRuntimePtr& runtime,
202 const DriverOptions& options,
203 const HalModel& model,
204 HalGetSupportedOperations_cb cb)
205{
Jim Flynn829ad302019-12-13 14:43:24 +0000206 std::stringstream ss;
207 ss << "ArmnnDriverImpl::getSupportedOperations()";
208 std::string fileName;
209 std::string timestamp;
210 if (!options.GetRequestInputsAndOutputsDumpDir().empty())
211 {
212 timestamp = GetFileTimestamp();
213 fileName = boost::str(boost::format("%1%/%2%_getSupportedOperations.txt")
214 % options.GetRequestInputsAndOutputsDumpDir()
215 % timestamp);
216 ss << " : " << fileName;
217 }
218 ALOGV(ss.str().c_str());
219
220 if (!options.GetRequestInputsAndOutputsDumpDir().empty())
221 {
222 //dump the marker file
223 std::ofstream fileStream;
224 fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc);
225 if (fileStream.good())
226 {
227 fileStream << timestamp << std::endl;
228 }
229 fileStream.close();
230 }
Mike Kellyb5fdf382019-06-11 16:35:25 +0100231
232 vector<bool> result;
233
234 if (!runtime)
235 {
Kevin May42477c12020-03-26 13:34:14 +0000236 cb(HalErrorStatus::DEVICE_UNAVAILABLE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100237 return Void();
238 }
239
240 // Run general model validation, if this doesn't pass we shouldn't analyse the model anyway.
241 if (!android::nn::validateModel(model))
242 {
Kevin May42477c12020-03-26 13:34:14 +0000243 cb(HalErrorStatus::INVALID_ARGUMENT, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100244 return Void();
245 }
246
247 // Attempt to convert the model to an ArmNN input network (INetwork).
248 ModelToINetworkConverter<HalPolicy> modelConverter(options.GetBackends(),
249 model,
250 options.GetForcedUnsupportedOperations());
251
252 if (modelConverter.GetConversionResult() != ConversionResult::Success
253 && modelConverter.GetConversionResult() != ConversionResult::UnsupportedFeature)
254 {
Kevin May42477c12020-03-26 13:34:14 +0000255 cb(HalErrorStatus::GENERAL_FAILURE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100256 return Void();
257 }
258
259 // Check each operation if it was converted successfully and copy the flags
260 // into the result (vector<bool>) that we need to return to Android.
Kevin May42477c12020-03-26 13:34:14 +0000261 result.reserve(getMainModel(model).operations.size());
262 for (uint32_t operationIdx = 0;
263 operationIdx < getMainModel(model).operations.size();
264 ++operationIdx)
Mike Kellyb5fdf382019-06-11 16:35:25 +0100265 {
266 bool operationSupported = modelConverter.IsOperationSupported(operationIdx);
267 result.push_back(operationSupported);
268 }
269
Kevin May42477c12020-03-26 13:34:14 +0000270 cb(HalErrorStatus::NONE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100271 return Void();
272}
273
274template<typename HalPolicy>
arovir01b0717b52018-09-05 17:03:25 +0100275Return<DeviceStatus> ArmnnDriverImpl<HalPolicy>::getStatus()
telsoa01ce3e84a2018-08-31 09:31:35 +0100276{
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100277 ALOGV("ArmnnDriver::getStatus()");
telsoa01ce3e84a2018-08-31 09:31:35 +0100278
279 return DeviceStatus::AVAILABLE;
280}
281
arovir01b0717b52018-09-05 17:03:25 +0100282///
283/// Class template specializations
284///
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100285
arovir01b0717b52018-09-05 17:03:25 +0100286template class ArmnnDriverImpl<hal_1_0::HalPolicy>;
287
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100288#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100289template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100290#endif
291
Mike Kellyb5fdf382019-06-11 16:35:25 +0100292#ifdef ARMNN_ANDROID_NN_V1_2
293template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
294template class ArmnnDriverImpl<hal_1_2::HalPolicy>;
295#endif
296
Kevin May42477c12020-03-26 13:34:14 +0000297#ifdef ARMNN_ANDROID_NN_V1_3
298template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
299template class ArmnnDriverImpl<hal_1_2::HalPolicy>;
300template class ArmnnDriverImpl<hal_1_3::HalPolicy>;
301#endif
302
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100303} // namespace armnn_driver