Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | fbd2817 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 2 | // Copyright © 2022 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 | fbd2817 | 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 | |
| 63 | if (m_EnableBf16TurboMode && m_EnableFp16TurboMode) |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 64 | { |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 65 | throw armnn::InvalidArgumentException("BFloat16 and Float16 turbo mode cannot be " |
| 66 | "enabled at the same time."); |
| 67 | } |
| 68 | |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 69 | // Check input tensor shapes |
| 70 | if ((m_InputTensorShapes.size() != 0) && |
| 71 | (m_InputTensorShapes.size() != m_InputNames.size())) |
| 72 | { |
| 73 | throw armnn::InvalidArgumentException("input-name and input-tensor-shape must have " |
| 74 | "the same amount of elements. "); |
| 75 | } |
| 76 | |
| 77 | if (m_InputTensorDataFilePaths.size() != 0) |
| 78 | { |
| 79 | if (!ValidatePaths(m_InputTensorDataFilePaths, true)) |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 80 | { |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 81 | 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] | 82 | } |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 83 | |
| 84 | if (m_InputTensorDataFilePaths.size() < m_InputNames.size()) |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 85 | { |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 86 | throw armnn::InvalidArgumentException( |
| 87 | fmt::format("According to the number of input names the user provided the network has {} " |
| 88 | "inputs. But only {} input-tensor-data file paths were provided. Each input of the " |
| 89 | "model is expected to be stored in it's own file.", |
| 90 | m_InputNames.size(), |
| 91 | m_InputTensorDataFilePaths.size())); |
| 92 | } |
Jan Eilers | f17fcd5 | 2021-07-26 22:20:00 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Check that threshold time is not less than zero |
| 96 | if (m_ThresholdTime < 0) |
| 97 | { |
| 98 | throw armnn::InvalidArgumentException("Threshold time supplied as a command line argument is less than zero."); |
| 99 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 100 | |
| 101 | // Warn if ExecuteNetwork will generate dummy input data |
| 102 | if (m_GenerateTensorData) |
| 103 | { |
| 104 | ARMNN_LOG(warning) << "No input files provided, input tensors will be filled with 0s."; |
| 105 | } |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 106 | |
| 107 | if (m_AllowExpandedDims && m_InferOutputShape) |
| 108 | { |
| 109 | throw armnn::InvalidArgumentException("infer-output-shape and allow-expanded-dims cannot be used together."); |
| 110 | } |
Colm Donelan | 3cff15a | 2021-10-12 15:06:19 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | #if defined(ARMNN_TFLITE_DELEGATE) |
| 114 | /** |
| 115 | * A utility method that populates a DelegateOptions object from this ExecuteNetworkParams. |
| 116 | * |
| 117 | * @return a populated armnnDelegate::DelegateOptions object. |
| 118 | */ |
| 119 | armnnDelegate::DelegateOptions ExecuteNetworkParams::ToDelegateOptions() const |
| 120 | { |
| 121 | armnnDelegate::DelegateOptions delegateOptions(m_ComputeDevices); |
| 122 | delegateOptions.SetDynamicBackendsPath(m_DynamicBackendsPath); |
| 123 | delegateOptions.SetGpuProfilingState(m_EnableProfiling); |
| 124 | |
| 125 | armnn::OptimizerOptions options; |
| 126 | options.m_ReduceFp32ToFp16 = m_EnableFp16TurboMode; |
| 127 | options.m_ReduceFp32ToBf16 = m_EnableBf16TurboMode; |
| 128 | options.m_Debug = m_PrintIntermediate; |
Colm Donelan | 4514228 | 2021-10-21 23:39:52 +0100 | [diff] [blame] | 129 | options.m_ProfilingEnabled = m_EnableProfiling; |
| 130 | delegateOptions.SetInternalProfilingParams(m_EnableProfiling, armnn::ProfilingDetailsMethod::DetailsWithEvents); |
Colm Donelan | 3cff15a | 2021-10-12 15:06:19 +0100 | [diff] [blame] | 131 | options.m_shapeInferenceMethod = armnn::ShapeInferenceMethod::ValidateOnly; |
| 132 | if (m_InferOutputShape) |
| 133 | { |
| 134 | options.m_shapeInferenceMethod = armnn::ShapeInferenceMethod::InferAndValidate; |
| 135 | } |
| 136 | |
| 137 | armnn::BackendOptions gpuAcc("GpuAcc", |
| 138 | { |
| 139 | { "FastMathEnabled", m_EnableFastMath }, |
| 140 | { "SaveCachedNetwork", m_SaveCachedNetwork }, |
| 141 | { "CachedNetworkFilePath", m_CachedNetworkFilePath }, |
| 142 | { "TuningLevel", m_TuningLevel}, |
| 143 | { "TuningFile", m_TuningPath.c_str()}, |
| 144 | { "KernelProfilingEnabled", m_EnableProfiling}, |
| 145 | { "MLGOTuningFilePath", m_MLGOTuningFilePath} |
| 146 | }); |
| 147 | |
| 148 | armnn::BackendOptions cpuAcc("CpuAcc", |
| 149 | { |
| 150 | { "FastMathEnabled", m_EnableFastMath }, |
| 151 | { "NumberOfThreads", m_NumberOfThreads } |
| 152 | }); |
| 153 | options.m_ModelOptions.push_back(gpuAcc); |
| 154 | options.m_ModelOptions.push_back(cpuAcc); |
| 155 | |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 156 | if (m_InferOutputShape) |
| 157 | { |
| 158 | armnn::BackendOptions networkOption("ShapeInferenceMethod", |
| 159 | { |
| 160 | {"InferAndValidate", true} |
| 161 | }); |
| 162 | options.m_ModelOptions.push_back(networkOption); |
| 163 | } |
| 164 | if (m_AllowExpandedDims) |
| 165 | { |
| 166 | armnn::BackendOptions networkOption("AllowExpandedDims", |
| 167 | { |
| 168 | {"AllowExpandedDims", true} |
| 169 | }); |
| 170 | options.m_ModelOptions.push_back(networkOption); |
| 171 | } |
Colm Donelan | 3cff15a | 2021-10-12 15:06:19 +0100 | [diff] [blame] | 172 | delegateOptions.SetOptimizerOptions(options); |
| 173 | |
Colm Donelan | 3cff15a | 2021-10-12 15:06:19 +0100 | [diff] [blame] | 174 | return delegateOptions; |
| 175 | } |
Teresa Charlin | fbd2817 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 176 | |
Colm Donelan | 3cff15a | 2021-10-12 15:06:19 +0100 | [diff] [blame] | 177 | #endif |