blob: a9b41bd37d847906253a147f5364f0095718ad59 [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 DuBois30c34ae2020-08-26 13:53:41 -070036 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 DuBois30c34ae2020-08-26 13:53:41 -070048Return<V1_0::ErrorStatus> FailPrepareModel(V1_0::ErrorStatus error,
Kevin Mayec1e5b82020-02-26 17:00:39 +000049 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 DuBois30c34ae2020-08-26 13:53:41 -070063Return<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 DuBois30c34ae2020-08-26 13:53:41 -070076 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa01ce3e84a2018-08-31 09:31:35 +010077 }
78
79 if (!runtime)
80 {
Kevin DuBois30c34ae2020-08-26 13:53:41 -070081 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 DuBois30c34ae2020-08-26 13:53:41 -070086 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 DuBois30c34ae2020-08-26 13:53:41 -070099 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
Mike Kelly7ed56dd2020-09-30 20:22:56 +0100108 armnn::BackendOptions gpuAcc("GpuAcc",
109 {
110 { "FastMathEnabled", options.IsFastMathEnabled() }
111 });
112 armnn::BackendOptions cpuAcc("CpuAcc",
113 {
114 { "FastMathEnabled", options.IsFastMathEnabled() }
115 });
116 OptOptions.m_ModelOptions.push_back(gpuAcc);
117 OptOptions.m_ModelOptions.push_back(cpuAcc);
118
jimfly0107dedda2018-10-09 12:29:41 +0100119 std::vector<std::string> errMessages;
telsoa01ce3e84a2018-08-31 09:31:35 +0100120 try
121 {
122 optNet = armnn::Optimize(*modelConverter.GetINetwork(),
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100123 options.GetBackends(),
telsoa01ce3e84a2018-08-31 09:31:35 +0100124 runtime->GetDeviceSpec(),
jimfly0107dedda2018-10-09 12:29:41 +0100125 OptOptions,
126 errMessages);
telsoa01ce3e84a2018-08-31 09:31:35 +0100127 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000128 catch (std::exception &e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100129 {
130 stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000131 message << "Exception (" << e.what() << ") caught from optimize.";
Kevin DuBois30c34ae2020-08-26 13:53:41 -0700132 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
133 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100134 }
135
136 // Check that the optimized network is valid.
137 if (!optNet)
138 {
jimfly0107dedda2018-10-09 12:29:41 +0100139 stringstream message;
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100140 message << "Invalid optimized network";
141 for (const string& msg : errMessages)
142 {
jimfly0107dedda2018-10-09 12:29:41 +0100143 message << "\n" << msg;
144 }
Kevin DuBois30c34ae2020-08-26 13:53:41 -0700145 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
146 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100147 }
148
149 // Export the optimized network graph to a dot file if an output dump directory
150 // has been specified in the drivers' arguments.
Jim Flynn829ad302019-12-13 14:43:24 +0000151 std::string dotGraphFileName = ExportNetworkGraphToDotFile(*optNet, options.GetRequestInputsAndOutputsDumpDir());
telsoa01ce3e84a2018-08-31 09:31:35 +0100152
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100153 // Load it into the runtime.
telsoa01ce3e84a2018-08-31 09:31:35 +0100154 armnn::NetworkId netId = 0;
155 try
156 {
157 if (runtime->LoadNetwork(netId, move(optNet)) != armnn::Status::Success)
158 {
Kevin DuBois30c34ae2020-08-26 13:53:41 -0700159 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be loaded", cb);
telsoa01ce3e84a2018-08-31 09:31:35 +0100160 }
161 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000162 catch (std::exception& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100163 {
164 stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000165 message << "Exception (" << e.what()<< ") caught from LoadNetwork.";
Kevin DuBois30c34ae2020-08-26 13:53:41 -0700166 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
167 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100168 }
169
Jim Flynn829ad302019-12-13 14:43:24 +0000170 // Now that we have a networkId for the graph rename the dump file to use it
171 // so that we can associate the graph file and the input/output tensor dump files
172 RenameGraphDotFile(dotGraphFileName,
173 options.GetRequestInputsAndOutputsDumpDir(),
174 netId);
175
Kevin Mayd5e94652019-11-07 14:02:14 +0000176 sp<ArmnnPreparedModel<HalPolicy>> preparedModel(
Mike Kellyb5fdf382019-06-11 16:35:25 +0100177 new ArmnnPreparedModel<HalPolicy>(
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100178 netId,
179 runtime.get(),
180 model,
181 options.GetRequestInputsAndOutputsDumpDir(),
182 options.IsGpuProfilingEnabled()));
telsoa01ce3e84a2018-08-31 09:31:35 +0100183
184 // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if
185 // this is enabled) before the first 'real' inference which removes the overhead of the first inference.
Matthew Bentham16196e22019-04-01 17:17:58 +0100186 if (!preparedModel->ExecuteWithDummyInputs())
187 {
Kevin DuBois30c34ae2020-08-26 13:53:41 -0700188 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be executed", cb);
Matthew Bentham16196e22019-04-01 17:17:58 +0100189 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100190
191 if (clTunedParameters &&
192 options.GetClTunedParametersMode() == armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters)
193 {
194 // Now that we've done one inference the CL kernel parameters will have been tuned, so save the updated file.
195 try
196 {
197 clTunedParameters->Save(options.GetClTunedParametersFile().c_str());
198 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000199 catch (std::exception& error)
telsoa01ce3e84a2018-08-31 09:31:35 +0100200 {
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100201 ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s",
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100202 options.GetClTunedParametersFile().c_str(), error.what());
telsoa01ce3e84a2018-08-31 09:31:35 +0100203 }
204 }
205
Kevin DuBois30c34ae2020-08-26 13:53:41 -0700206 NotifyCallbackAndCheck(cb, V1_0::ErrorStatus::NONE, preparedModel);
telsoa01ce3e84a2018-08-31 09:31:35 +0100207
Kevin DuBois30c34ae2020-08-26 13:53:41 -0700208 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100209}
210
arovir01b0717b52018-09-05 17:03:25 +0100211template<typename HalPolicy>
Mike Kellyb5fdf382019-06-11 16:35:25 +0100212Return<void> ArmnnDriverImpl<HalPolicy>::getSupportedOperations(const armnn::IRuntimePtr& runtime,
213 const DriverOptions& options,
214 const HalModel& model,
215 HalGetSupportedOperations_cb cb)
216{
Jim Flynn829ad302019-12-13 14:43:24 +0000217 std::stringstream ss;
218 ss << "ArmnnDriverImpl::getSupportedOperations()";
219 std::string fileName;
220 std::string timestamp;
221 if (!options.GetRequestInputsAndOutputsDumpDir().empty())
222 {
Colm Donelan08d9a1c2020-09-09 17:56:55 +0100223 ss << " : "
224 << options.GetRequestInputsAndOutputsDumpDir()
225 << "/"
226 << GetFileTimestamp()
227 << "_getSupportedOperations.txt";
Jim Flynn829ad302019-12-13 14:43:24 +0000228 }
229 ALOGV(ss.str().c_str());
230
231 if (!options.GetRequestInputsAndOutputsDumpDir().empty())
232 {
233 //dump the marker file
234 std::ofstream fileStream;
235 fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc);
236 if (fileStream.good())
237 {
238 fileStream << timestamp << std::endl;
239 }
240 fileStream.close();
241 }
Mike Kellyb5fdf382019-06-11 16:35:25 +0100242
243 vector<bool> result;
244
245 if (!runtime)
246 {
Kevin May42477c12020-03-26 13:34:14 +0000247 cb(HalErrorStatus::DEVICE_UNAVAILABLE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100248 return Void();
249 }
250
251 // Run general model validation, if this doesn't pass we shouldn't analyse the model anyway.
252 if (!android::nn::validateModel(model))
253 {
Kevin May42477c12020-03-26 13:34:14 +0000254 cb(HalErrorStatus::INVALID_ARGUMENT, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100255 return Void();
256 }
257
258 // Attempt to convert the model to an ArmNN input network (INetwork).
259 ModelToINetworkConverter<HalPolicy> modelConverter(options.GetBackends(),
260 model,
261 options.GetForcedUnsupportedOperations());
262
263 if (modelConverter.GetConversionResult() != ConversionResult::Success
264 && modelConverter.GetConversionResult() != ConversionResult::UnsupportedFeature)
265 {
Kevin May42477c12020-03-26 13:34:14 +0000266 cb(HalErrorStatus::GENERAL_FAILURE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100267 return Void();
268 }
269
270 // Check each operation if it was converted successfully and copy the flags
271 // into the result (vector<bool>) that we need to return to Android.
Kevin May42477c12020-03-26 13:34:14 +0000272 result.reserve(getMainModel(model).operations.size());
273 for (uint32_t operationIdx = 0;
274 operationIdx < getMainModel(model).operations.size();
275 ++operationIdx)
Mike Kellyb5fdf382019-06-11 16:35:25 +0100276 {
277 bool operationSupported = modelConverter.IsOperationSupported(operationIdx);
278 result.push_back(operationSupported);
279 }
280
Kevin May42477c12020-03-26 13:34:14 +0000281 cb(HalErrorStatus::NONE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100282 return Void();
283}
284
285template<typename HalPolicy>
Kevin DuBois30c34ae2020-08-26 13:53:41 -0700286Return<V1_0::DeviceStatus> ArmnnDriverImpl<HalPolicy>::getStatus()
telsoa01ce3e84a2018-08-31 09:31:35 +0100287{
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100288 ALOGV("ArmnnDriver::getStatus()");
telsoa01ce3e84a2018-08-31 09:31:35 +0100289
Kevin DuBois30c34ae2020-08-26 13:53:41 -0700290 return V1_0::DeviceStatus::AVAILABLE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100291}
292
arovir01b0717b52018-09-05 17:03:25 +0100293///
294/// Class template specializations
295///
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100296
arovir01b0717b52018-09-05 17:03:25 +0100297template class ArmnnDriverImpl<hal_1_0::HalPolicy>;
298
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100299#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100300template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100301#endif
302
Mike Kellyb5fdf382019-06-11 16:35:25 +0100303#ifdef ARMNN_ANDROID_NN_V1_2
304template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
305template class ArmnnDriverImpl<hal_1_2::HalPolicy>;
306#endif
307
Kevin May42477c12020-03-26 13:34:14 +0000308#ifdef ARMNN_ANDROID_NN_V1_3
309template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
310template class ArmnnDriverImpl<hal_1_2::HalPolicy>;
311template class ArmnnDriverImpl<hal_1_3::HalPolicy>;
312#endif
313
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100314} // namespace armnn_driver