Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 1 | // |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ExecuteNetworkParams.hpp" |
| 7 | |
| 8 | #include "NetworkExecutionUtils/NetworkExecutionUtils.hpp" |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 9 | #include <armnn/Logging.hpp> |
| 10 | |
| 11 | #include <fmt/format.h> |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 12 | #include <armnnUtils/Filesystem.hpp> |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 13 | |
| 14 | void CheckClTuningParameter(const int& tuningLevel, |
| 15 | const std::string& tuningPath, |
| 16 | const std::vector<armnn::BackendId> computeDevices) |
| 17 | { |
| 18 | if (!tuningPath.empty()) |
| 19 | { |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 20 | if (tuningLevel == 0) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 21 | { |
| 22 | ARMNN_LOG(info) << "Using cl tuning file: " << tuningPath << "\n"; |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 23 | if (!ValidatePath(tuningPath, true)) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 24 | { |
| 25 | throw armnn::InvalidArgumentException("The tuning path is not valid"); |
| 26 | } |
| 27 | } |
| 28 | else if ((1 <= tuningLevel) && (tuningLevel <= 3)) |
| 29 | { |
| 30 | ARMNN_LOG(info) << "Starting execution to generate a cl tuning file: " << tuningPath << "\n" |
| 31 | << "Tuning level in use: " << tuningLevel << "\n"; |
| 32 | } |
| 33 | else if ((0 < tuningLevel) || (tuningLevel > 3)) |
| 34 | { |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 35 | throw armnn::InvalidArgumentException(fmt::format("The tuning level {} is not valid.", |
| 36 | tuningLevel)); |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // Ensure that a GpuAcc is enabled. Otherwise no tuning data are used or genereted |
| 40 | // Only warn if it's not enabled |
| 41 | auto it = std::find(computeDevices.begin(), computeDevices.end(), "GpuAcc"); |
| 42 | if (it == computeDevices.end()) |
| 43 | { |
| 44 | ARMNN_LOG(warning) << "To use Cl Tuning the compute device GpuAcc needs to be active."; |
| 45 | } |
| 46 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void ExecuteNetworkParams::ValidateParams() |
| 50 | { |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 51 | if (m_DynamicBackendsPath == "") |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 52 | { |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 53 | // Check compute devices are valid unless they are dynamically loaded at runtime |
| 54 | std::string invalidBackends; |
| 55 | if (!CheckRequestedBackendsAreValid(m_ComputeDevices, armnn::Optional<std::string&>(invalidBackends))) |
Francis Murtagh | bf18a26 | 2020-10-27 15:20:40 +0000 | [diff] [blame] | 56 | { |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 57 | ARMNN_LOG(fatal) << "The list of preferred devices contains invalid backend IDs: " |
| 58 | << invalidBackends; |
Francis Murtagh | bf18a26 | 2020-10-27 15:20:40 +0000 | [diff] [blame] | 59 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 60 | } |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 61 | CheckClTuningParameter(m_TuningLevel, m_TuningPath, m_ComputeDevices); |
| 62 | |
Ryan OShea | 3144159 | 2022-11-07 16:20:48 +0000 | [diff] [blame] | 63 | if (m_EnableBf16TurboMode && !m_EnableFastMath) |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 64 | { |
Ryan OShea | 3144159 | 2022-11-07 16:20:48 +0000 | [diff] [blame] | 65 | throw armnn::InvalidArgumentException("To use BF16 please use --enable-fast-math. "); |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 68 | // Check input tensor shapes |
| 69 | if ((m_InputTensorShapes.size() != 0) && |
| 70 | (m_InputTensorShapes.size() != m_InputNames.size())) |
| 71 | { |
| 72 | throw armnn::InvalidArgumentException("input-name and input-tensor-shape must have " |
| 73 | "the same amount of elements. "); |
| 74 | } |
| 75 | |
| 76 | if (m_InputTensorDataFilePaths.size() != 0) |
| 77 | { |
| 78 | if (!ValidatePaths(m_InputTensorDataFilePaths, true)) |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 79 | { |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 80 | throw armnn::InvalidArgumentException("One or more input data file paths are not valid."); |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 81 | } |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 82 | |
| 83 | if (m_InputTensorDataFilePaths.size() < m_InputNames.size()) |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 84 | { |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 85 | throw armnn::InvalidArgumentException( |
| 86 | fmt::format("According to the number of input names the user provided the network has {} " |
| 87 | "inputs. But only {} input-tensor-data file paths were provided. Each input of the " |
| 88 | "model is expected to be stored in it's own file.", |
| 89 | m_InputNames.size(), |
| 90 | m_InputTensorDataFilePaths.size())); |
| 91 | } |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Check that threshold time is not less than zero |
| 95 | if (m_ThresholdTime < 0) |
| 96 | { |
| 97 | throw armnn::InvalidArgumentException("Threshold time supplied as a command line argument is less than zero."); |
| 98 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 99 | |
| 100 | // Warn if ExecuteNetwork will generate dummy input data |
| 101 | if (m_GenerateTensorData) |
| 102 | { |
| 103 | ARMNN_LOG(warning) << "No input files provided, input tensors will be filled with 0s."; |
| 104 | } |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 105 | |
| 106 | if (m_AllowExpandedDims && m_InferOutputShape) |
| 107 | { |
| 108 | throw armnn::InvalidArgumentException("infer-output-shape and allow-expanded-dims cannot be used together."); |
| 109 | } |
Colm Donelan | 3cff15a | 2021-10-12 15:06:19 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | #if defined(ARMNN_TFLITE_DELEGATE) |
| 113 | /** |
| 114 | * A utility method that populates a DelegateOptions object from this ExecuteNetworkParams. |
| 115 | * |
| 116 | * @return a populated armnnDelegate::DelegateOptions object. |
| 117 | */ |
| 118 | armnnDelegate::DelegateOptions ExecuteNetworkParams::ToDelegateOptions() const |
| 119 | { |
| 120 | armnnDelegate::DelegateOptions delegateOptions(m_ComputeDevices); |
| 121 | delegateOptions.SetDynamicBackendsPath(m_DynamicBackendsPath); |
| 122 | delegateOptions.SetGpuProfilingState(m_EnableProfiling); |
Declan-ARM | 6d2d4ea | 2023-06-27 10:48:38 +0100 | [diff] [blame] | 123 | delegateOptions.SetInternalProfilingParams(m_EnableProfiling, armnn::ProfilingDetailsMethod::Undefined); |
| 124 | |
| 125 | if (m_OutputDetailsOnlyToStdOut) |
| 126 | { |
| 127 | delegateOptions.SetInternalProfilingParams(m_EnableProfiling, armnn::ProfilingDetailsMethod::DetailsOnly); |
| 128 | } |
| 129 | else if (m_OutputDetailsToStdOut) |
| 130 | { |
| 131 | delegateOptions.SetInternalProfilingParams(m_EnableProfiling, armnn::ProfilingDetailsMethod::DetailsWithEvents); |
| 132 | } |
Colm Donelan | da7f2f9 | 2022-12-20 16:21:35 +0000 | [diff] [blame] | 133 | |
| 134 | // GPU Backend options first. |
Colm Donelan | 3cff15a | 2021-10-12 15:06:19 +0100 | [diff] [blame] | 135 | { |
Colm Donelan | da7f2f9 | 2022-12-20 16:21:35 +0000 | [diff] [blame] | 136 | armnn::BackendOptions gpuOption("GpuAcc", {{"TuningLevel", m_TuningLevel}}); |
| 137 | delegateOptions.AddBackendOption(gpuOption); |
| 138 | } |
| 139 | { |
| 140 | armnn::BackendOptions gpuOption("GpuAcc", {{"TuningFile", m_TuningPath.c_str()}}); |
| 141 | delegateOptions.AddBackendOption(gpuOption); |
| 142 | } |
| 143 | { |
| 144 | armnn::BackendOptions gpuOption("GpuAcc", {{"KernelProfilingEnabled", m_EnableProfiling}}); |
| 145 | delegateOptions.AddBackendOption(gpuOption); |
Colm Donelan | 3cff15a | 2021-10-12 15:06:19 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Colm Donelan | da7f2f9 | 2022-12-20 16:21:35 +0000 | [diff] [blame] | 148 | // Optimizer options next. |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 149 | armnn::OptimizerOptionsOpaque optimizerOptions; |
| 150 | optimizerOptions.SetReduceFp32ToFp16(m_EnableFp16TurboMode); |
| 151 | optimizerOptions.SetDebugEnabled(m_PrintIntermediate); |
| 152 | optimizerOptions.SetDebugToFileEnabled(m_PrintIntermediateOutputsToFile); |
| 153 | optimizerOptions.SetProfilingEnabled(m_EnableProfiling); |
| 154 | optimizerOptions.SetShapeInferenceMethod(armnn::ShapeInferenceMethod::ValidateOnly); |
Colm Donelan | da7f2f9 | 2022-12-20 16:21:35 +0000 | [diff] [blame] | 155 | if (m_InferOutputShape) |
| 156 | { |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 157 | optimizerOptions.SetShapeInferenceMethod(armnn::ShapeInferenceMethod::InferAndValidate); |
Colm Donelan | da7f2f9 | 2022-12-20 16:21:35 +0000 | [diff] [blame] | 158 | armnn::BackendOptions networkOption("ShapeInferenceMethod", |
| 159 | { |
| 160 | {"InferAndValidate", true} |
| 161 | }); |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 162 | optimizerOptions.AddModelOption(networkOption); |
Colm Donelan | da7f2f9 | 2022-12-20 16:21:35 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | { |
| 166 | armnn::BackendOptions option("GpuAcc", {{"FastMathEnabled", m_EnableFastMath}}); |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 167 | optimizerOptions.AddModelOption(option); |
Colm Donelan | da7f2f9 | 2022-12-20 16:21:35 +0000 | [diff] [blame] | 168 | } |
| 169 | { |
| 170 | armnn::BackendOptions option("GpuAcc", {{"CachedNetworkFilePath", m_CachedNetworkFilePath}}); |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 171 | optimizerOptions.AddModelOption(option); |
Colm Donelan | da7f2f9 | 2022-12-20 16:21:35 +0000 | [diff] [blame] | 172 | } |
| 173 | { |
| 174 | armnn::BackendOptions option("GpuAcc", {{"MLGOTuningFilePath", m_MLGOTuningFilePath}}); |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 175 | optimizerOptions.AddModelOption(option); |
Colm Donelan | da7f2f9 | 2022-12-20 16:21:35 +0000 | [diff] [blame] | 176 | } |
Colm Donelan | 3cff15a | 2021-10-12 15:06:19 +0100 | [diff] [blame] | 177 | |
| 178 | armnn::BackendOptions cpuAcc("CpuAcc", |
| 179 | { |
| 180 | { "FastMathEnabled", m_EnableFastMath }, |
| 181 | { "NumberOfThreads", m_NumberOfThreads } |
| 182 | }); |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 183 | optimizerOptions.AddModelOption(cpuAcc); |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 184 | if (m_AllowExpandedDims) |
| 185 | { |
| 186 | armnn::BackendOptions networkOption("AllowExpandedDims", |
| 187 | { |
| 188 | {"AllowExpandedDims", true} |
| 189 | }); |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 190 | optimizerOptions.AddModelOption(networkOption); |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 191 | } |
Colm Donelan | da7f2f9 | 2022-12-20 16:21:35 +0000 | [diff] [blame] | 192 | delegateOptions.SetOptimizerOptions(optimizerOptions); |
Colm Donelan | 3cff15a | 2021-10-12 15:06:19 +0100 | [diff] [blame] | 193 | return delegateOptions; |
| 194 | } |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 195 | |
Colm Donelan | 3cff15a | 2021-10-12 15:06:19 +0100 | [diff] [blame] | 196 | #endif |