telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 1 | // |
John Mcloughlin | 7c4fc93 | 2023-04-07 16:21:49 +0100 | [diff] [blame] | 2 | // Copyright © 2017, 2023 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 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 11 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) // Using ::android::hardware::neuralnetworks::V1_2 |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 12 | #include "ArmnnPreparedModel_1_2.hpp" |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 13 | #endif |
| 14 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 15 | #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 Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 21 | #include "ModelToINetworkConverter.hpp" |
| 22 | #include "SystemPropertiesUtils.hpp" |
Colm Donelan | 08d9a1c | 2020-09-09 17:56:55 +0100 | [diff] [blame] | 23 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 24 | #include <ValidateHal.h> |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 25 | #include <log/log.h> |
Ryan OShea | c7756cf | 2022-03-08 01:45:36 +0000 | [diff] [blame] | 26 | #include <chrono> |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 27 | |
| 28 | using namespace std; |
| 29 | using namespace android; |
| 30 | using namespace android::nn; |
| 31 | using namespace android::hardware; |
| 32 | |
| 33 | namespace |
| 34 | { |
| 35 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 36 | void NotifyCallbackAndCheck(const sp<V1_0::IPreparedModelCallback>& callback, |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 37 | V1_0::ErrorStatus errorStatus, |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 38 | const sp<V1_0::IPreparedModel>& preparedModelPtr) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 39 | { |
| 40 | Return<void> returned = callback->notify(errorStatus, preparedModelPtr); |
| 41 | // This check is required, if the callback fails and it isn't checked it will bring down the service |
| 42 | if (!returned.isOk()) |
| 43 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 44 | ALOGE("ArmnnDriverImpl::prepareModel: hidl callback failed to return properly: %s ", |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 45 | returned.description().c_str()); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 49 | Return<V1_0::ErrorStatus> FailPrepareModel(V1_0::ErrorStatus error, |
| 50 | const string& message, |
| 51 | const sp<V1_0::IPreparedModelCallback>& callback) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 52 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 53 | ALOGW("ArmnnDriverImpl::prepareModel: %s", message.c_str()); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 54 | NotifyCallbackAndCheck(callback, error, nullptr); |
| 55 | return error; |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | namespace armnn_driver |
| 61 | { |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 62 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 63 | template<typename HalPolicy> |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 64 | Return<V1_0::ErrorStatus> ArmnnDriverImpl<HalPolicy>::prepareModel( |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 65 | const armnn::IRuntimePtr& runtime, |
| 66 | const armnn::IGpuAccTunedParametersPtr& clTunedParameters, |
| 67 | const DriverOptions& options, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 68 | const HalModel& model, |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 69 | const sp<V1_0::IPreparedModelCallback>& cb, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 70 | bool float32ToFloat16) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 71 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 72 | ALOGV("ArmnnDriverImpl::prepareModel()"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 73 | |
Ryan OShea | c7756cf | 2022-03-08 01:45:36 +0000 | [diff] [blame] | 74 | std::chrono::time_point<std::chrono::system_clock> prepareModelTimepoint = std::chrono::system_clock::now(); |
| 75 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 76 | if (cb.get() == nullptr) |
| 77 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 78 | ALOGW("ArmnnDriverImpl::prepareModel: Invalid callback passed to prepareModel"); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 79 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (!runtime) |
| 83 | { |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 84 | return FailPrepareModel(V1_0::ErrorStatus::DEVICE_UNAVAILABLE, "Device unavailable", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if (!android::nn::validateModel(model)) |
| 88 | { |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 89 | return FailPrepareModel(V1_0::ErrorStatus::INVALID_ARGUMENT, "Invalid model passed as input", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Deliberately ignore any unsupported operations requested by the options - |
| 93 | // at this point we're being asked to prepare a model that we've already declared support for |
| 94 | // and the operation indices may be different to those in getSupportedOperations anyway. |
| 95 | set<unsigned int> unsupportedOperations; |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 96 | ModelToINetworkConverter<HalPolicy> modelConverter(options.GetBackends(), |
| 97 | model, |
| 98 | unsupportedOperations); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 99 | |
| 100 | if (modelConverter.GetConversionResult() != ConversionResult::Success) |
| 101 | { |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 102 | FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "ModelToINetworkConverter failed", cb); |
| 103 | return V1_0::ErrorStatus::NONE; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Sadik Armagan | b302143 | 2021-01-13 15:56:51 +0000 | [diff] [blame] | 106 | // Serialize the network graph to a .armnn file if an output directory |
| 107 | // has been specified in the drivers' arguments. |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 108 | std::vector<uint8_t> dataCacheData; |
Sadik Armagan | b302143 | 2021-01-13 15:56:51 +0000 | [diff] [blame] | 109 | auto serializedNetworkFileName = |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 110 | SerializeNetwork(*modelConverter.GetINetwork(), |
| 111 | options.GetRequestInputsAndOutputsDumpDir(), |
| 112 | dataCacheData, |
| 113 | false); |
Sadik Armagan | b302143 | 2021-01-13 15:56:51 +0000 | [diff] [blame] | 114 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 115 | // Optimize the network |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 116 | armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr); |
John Mcloughlin | 7c4fc93 | 2023-04-07 16:21:49 +0100 | [diff] [blame] | 117 | armnn::OptimizerOptionsOpaque OptOptions; |
| 118 | OptOptions.SetReduceFp32ToFp16(float32ToFloat16); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 119 | |
Mike Kelly | 7ed56dd | 2020-09-30 20:22:56 +0100 | [diff] [blame] | 120 | armnn::BackendOptions gpuAcc("GpuAcc", |
| 121 | { |
Sadik Armagan | f36e10b | 2021-01-11 16:34:01 +0000 | [diff] [blame] | 122 | { "FastMathEnabled", options.IsFastMathEnabled() }, |
| 123 | { "SaveCachedNetwork", options.SaveCachedNetwork() }, |
Finn Williams | f5ca16c | 2021-02-12 14:26:23 +0000 | [diff] [blame] | 124 | { "CachedNetworkFilePath", options.GetCachedNetworkFilePath() }, |
| 125 | { "MLGOTuningFilePath", options.GetClMLGOTunedParametersFile() } |
| 126 | |
Mike Kelly | 7ed56dd | 2020-09-30 20:22:56 +0100 | [diff] [blame] | 127 | }); |
Finn Williams | f5ca16c | 2021-02-12 14:26:23 +0000 | [diff] [blame] | 128 | |
Mike Kelly | 7ed56dd | 2020-09-30 20:22:56 +0100 | [diff] [blame] | 129 | armnn::BackendOptions cpuAcc("CpuAcc", |
| 130 | { |
Matthew Sloyan | cd639c9 | 2021-02-11 16:57:38 +0000 | [diff] [blame] | 131 | { "FastMathEnabled", options.IsFastMathEnabled() }, |
| 132 | { "NumberOfThreads", options.GetNumberOfThreads() } |
Mike Kelly | 7ed56dd | 2020-09-30 20:22:56 +0100 | [diff] [blame] | 133 | }); |
John Mcloughlin | 7c4fc93 | 2023-04-07 16:21:49 +0100 | [diff] [blame] | 134 | OptOptions.AddModelOption(gpuAcc); |
| 135 | OptOptions.AddModelOption(cpuAcc); |
Mike Kelly | 7ed56dd | 2020-09-30 20:22:56 +0100 | [diff] [blame] | 136 | |
jimfly01 | 07dedda | 2018-10-09 12:29:41 +0100 | [diff] [blame] | 137 | std::vector<std::string> errMessages; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 138 | try |
| 139 | { |
| 140 | optNet = armnn::Optimize(*modelConverter.GetINetwork(), |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 141 | options.GetBackends(), |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 142 | runtime->GetDeviceSpec(), |
jimfly01 | 07dedda | 2018-10-09 12:29:41 +0100 | [diff] [blame] | 143 | OptOptions, |
| 144 | errMessages); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 145 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 146 | catch (std::exception &e) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 147 | { |
| 148 | stringstream message; |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 149 | message << "Exception (" << e.what() << ") caught from optimize."; |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 150 | FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb); |
| 151 | return V1_0::ErrorStatus::NONE; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // Check that the optimized network is valid. |
| 155 | if (!optNet) |
| 156 | { |
jimfly01 | 07dedda | 2018-10-09 12:29:41 +0100 | [diff] [blame] | 157 | stringstream message; |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame] | 158 | message << "Invalid optimized network"; |
| 159 | for (const string& msg : errMessages) |
| 160 | { |
jimfly01 | 07dedda | 2018-10-09 12:29:41 +0100 | [diff] [blame] | 161 | message << "\n" << msg; |
| 162 | } |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 163 | FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb); |
| 164 | return V1_0::ErrorStatus::NONE; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Export the optimized network graph to a dot file if an output dump directory |
| 168 | // has been specified in the drivers' arguments. |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 169 | std::string dotGraphFileName = ExportNetworkGraphToDotFile(*optNet, options.GetRequestInputsAndOutputsDumpDir()); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 170 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 171 | // Load it into the runtime. |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 172 | armnn::NetworkId netId = 0; |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 173 | std::string msg; |
| 174 | armnn::INetworkProperties networkProperties(options.isAsyncModelExecutionEnabled(), |
| 175 | armnn::MemorySource::Undefined, |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 176 | armnn::MemorySource::Undefined); |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 177 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 178 | try |
| 179 | { |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 180 | if (runtime->LoadNetwork(netId, move(optNet), msg, networkProperties) != armnn::Status::Success) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 181 | { |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 182 | return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be loaded", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 183 | } |
| 184 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 185 | catch (std::exception& e) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 186 | { |
| 187 | stringstream message; |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 188 | message << "Exception (" << e.what()<< ") caught from LoadNetwork."; |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 189 | FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb); |
| 190 | return V1_0::ErrorStatus::NONE; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Sadik Armagan | b302143 | 2021-01-13 15:56:51 +0000 | [diff] [blame] | 193 | // Now that we have a networkId for the graph rename the exported files to use it |
| 194 | // so that we can associate the graph file and the input/output tensor exported files |
| 195 | RenameExportedFiles(serializedNetworkFileName, |
| 196 | dotGraphFileName, |
| 197 | options.GetRequestInputsAndOutputsDumpDir(), |
| 198 | netId); |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 199 | |
Kevin May | d5e9465 | 2019-11-07 14:02:14 +0000 | [diff] [blame] | 200 | sp<ArmnnPreparedModel<HalPolicy>> preparedModel( |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 201 | new ArmnnPreparedModel<HalPolicy>( |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 202 | netId, |
| 203 | runtime.get(), |
| 204 | model, |
| 205 | options.GetRequestInputsAndOutputsDumpDir(), |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 206 | options.IsGpuProfilingEnabled(), |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 207 | options.isAsyncModelExecutionEnabled(), |
Narumol Prangnawarat | d1a947f | 2022-02-07 13:12:24 +0000 | [diff] [blame] | 208 | options.getNoOfArmnnThreads(), |
| 209 | options.isImportEnabled(), |
| 210 | options.isExportEnabled())); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 211 | |
Narumol Prangnawarat | d1a947f | 2022-02-07 13:12:24 +0000 | [diff] [blame] | 212 | if (std::find(options.GetBackends().begin(), |
| 213 | options.GetBackends().end(), |
| 214 | armnn::Compute::GpuAcc) != options.GetBackends().end()) |
Matthew Bentham | 16196e2 | 2019-04-01 17:17:58 +0100 | [diff] [blame] | 215 | { |
Narumol Prangnawarat | d1a947f | 2022-02-07 13:12:24 +0000 | [diff] [blame] | 216 | // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if |
| 217 | // this is enabled) before the first 'real' inference which removes the overhead of the first inference. |
| 218 | if (!preparedModel->ExecuteWithDummyInputs()) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 219 | { |
Narumol Prangnawarat | d1a947f | 2022-02-07 13:12:24 +0000 | [diff] [blame] | 220 | return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be executed", cb); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 221 | } |
Narumol Prangnawarat | d1a947f | 2022-02-07 13:12:24 +0000 | [diff] [blame] | 222 | |
| 223 | if (clTunedParameters && |
| 224 | options.GetClTunedParametersMode() == armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 225 | { |
Narumol Prangnawarat | d1a947f | 2022-02-07 13:12:24 +0000 | [diff] [blame] | 226 | // Now that we've done one inference the CL kernel parameters will have been tuned, so save the updated file |
| 227 | try |
| 228 | { |
| 229 | clTunedParameters->Save(options.GetClTunedParametersFile().c_str()); |
| 230 | } |
| 231 | catch (std::exception& error) |
| 232 | { |
| 233 | ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s", |
| 234 | options.GetClTunedParametersFile().c_str(), error.what()); |
| 235 | } |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 236 | } |
| 237 | } |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 238 | NotifyCallbackAndCheck(cb, V1_0::ErrorStatus::NONE, preparedModel); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 239 | |
Ryan OShea | c7756cf | 2022-03-08 01:45:36 +0000 | [diff] [blame] | 240 | ALOGV("ArmnnDriverImpl::prepareModel cache timing = %lld µs", std::chrono::duration_cast<std::chrono::microseconds> |
| 241 | (std::chrono::system_clock::now() - prepareModelTimepoint).count()); |
| 242 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 243 | return V1_0::ErrorStatus::NONE; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 244 | } |
| 245 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 246 | template<typename HalPolicy> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 247 | Return<void> ArmnnDriverImpl<HalPolicy>::getSupportedOperations(const armnn::IRuntimePtr& runtime, |
| 248 | const DriverOptions& options, |
| 249 | const HalModel& model, |
| 250 | HalGetSupportedOperations_cb cb) |
| 251 | { |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 252 | std::stringstream ss; |
| 253 | ss << "ArmnnDriverImpl::getSupportedOperations()"; |
| 254 | std::string fileName; |
| 255 | std::string timestamp; |
| 256 | if (!options.GetRequestInputsAndOutputsDumpDir().empty()) |
| 257 | { |
Colm Donelan | 08d9a1c | 2020-09-09 17:56:55 +0100 | [diff] [blame] | 258 | ss << " : " |
| 259 | << options.GetRequestInputsAndOutputsDumpDir() |
| 260 | << "/" |
| 261 | << GetFileTimestamp() |
| 262 | << "_getSupportedOperations.txt"; |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 263 | } |
| 264 | ALOGV(ss.str().c_str()); |
| 265 | |
| 266 | if (!options.GetRequestInputsAndOutputsDumpDir().empty()) |
| 267 | { |
| 268 | //dump the marker file |
| 269 | std::ofstream fileStream; |
| 270 | fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc); |
| 271 | if (fileStream.good()) |
| 272 | { |
| 273 | fileStream << timestamp << std::endl; |
| 274 | } |
| 275 | fileStream.close(); |
| 276 | } |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 277 | |
| 278 | vector<bool> result; |
| 279 | |
| 280 | if (!runtime) |
| 281 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 282 | cb(HalErrorStatus::DEVICE_UNAVAILABLE, result); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 283 | return Void(); |
| 284 | } |
| 285 | |
| 286 | // Run general model validation, if this doesn't pass we shouldn't analyse the model anyway. |
| 287 | if (!android::nn::validateModel(model)) |
| 288 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 289 | cb(HalErrorStatus::INVALID_ARGUMENT, result); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 290 | return Void(); |
| 291 | } |
| 292 | |
| 293 | // Attempt to convert the model to an ArmNN input network (INetwork). |
| 294 | ModelToINetworkConverter<HalPolicy> modelConverter(options.GetBackends(), |
| 295 | model, |
| 296 | options.GetForcedUnsupportedOperations()); |
| 297 | |
| 298 | if (modelConverter.GetConversionResult() != ConversionResult::Success |
| 299 | && modelConverter.GetConversionResult() != ConversionResult::UnsupportedFeature) |
| 300 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 301 | cb(HalErrorStatus::GENERAL_FAILURE, result); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 302 | return Void(); |
| 303 | } |
| 304 | |
| 305 | // Check each operation if it was converted successfully and copy the flags |
| 306 | // into the result (vector<bool>) that we need to return to Android. |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 307 | result.reserve(getMainModel(model).operations.size()); |
| 308 | for (uint32_t operationIdx = 0; |
| 309 | operationIdx < getMainModel(model).operations.size(); |
| 310 | ++operationIdx) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 311 | { |
| 312 | bool operationSupported = modelConverter.IsOperationSupported(operationIdx); |
| 313 | result.push_back(operationSupported); |
| 314 | } |
| 315 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 316 | cb(HalErrorStatus::NONE, result); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 317 | return Void(); |
| 318 | } |
| 319 | |
| 320 | template<typename HalPolicy> |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 321 | Return<V1_0::DeviceStatus> ArmnnDriverImpl<HalPolicy>::getStatus() |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 322 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 323 | ALOGV("ArmnnDriver::getStatus()"); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 324 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 325 | return V1_0::DeviceStatus::AVAILABLE; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 326 | } |
| 327 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 328 | /// |
| 329 | /// Class template specializations |
| 330 | /// |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 331 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 332 | template class ArmnnDriverImpl<hal_1_0::HalPolicy>; |
| 333 | |
Matteo Martincigh | 8b287c2 | 2018-09-07 09:25:10 +0100 | [diff] [blame] | 334 | #ifdef ARMNN_ANDROID_NN_V1_1 |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 335 | template class ArmnnDriverImpl<hal_1_1::HalPolicy>; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 336 | #endif |
| 337 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 338 | #ifdef ARMNN_ANDROID_NN_V1_2 |
| 339 | template class ArmnnDriverImpl<hal_1_1::HalPolicy>; |
| 340 | template class ArmnnDriverImpl<hal_1_2::HalPolicy>; |
| 341 | #endif |
| 342 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 343 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 344 | template class ArmnnDriverImpl<hal_1_1::HalPolicy>; |
| 345 | template class ArmnnDriverImpl<hal_1_2::HalPolicy>; |
| 346 | template class ArmnnDriverImpl<hal_1_3::HalPolicy>; |
| 347 | #endif |
| 348 | |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame] | 349 | } // namespace armnn_driver |