blob: 4f5a290a0d45454b1a123bd03317308d9aeb9c0d [file] [log] [blame]
Jan Eilers45274902020-10-15 18:34:43 +01001//
John Mcloughlinc5ee0d72023-03-24 12:07:25 +00002// Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
Jan Eilers45274902020-10-15 18:34:43 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "ExecuteNetworkParams.hpp"
7
8#include "NetworkExecutionUtils/NetworkExecutionUtils.hpp"
Jan Eilers45274902020-10-15 18:34:43 +01009#include <armnn/Logging.hpp>
10
11#include <fmt/format.h>
Teresa Charlin83b42912022-07-07 14:24:59 +010012#include <armnnUtils/Filesystem.hpp>
Jan Eilers45274902020-10-15 18:34:43 +010013
14void CheckClTuningParameter(const int& tuningLevel,
15 const std::string& tuningPath,
16 const std::vector<armnn::BackendId> computeDevices)
17{
18 if (!tuningPath.empty())
19 {
Keith Daviscb8e3502020-11-12 10:27:19 +000020 if (tuningLevel == 0)
Jan Eilers45274902020-10-15 18:34:43 +010021 {
22 ARMNN_LOG(info) << "Using cl tuning file: " << tuningPath << "\n";
Keith Daviscb8e3502020-11-12 10:27:19 +000023 if (!ValidatePath(tuningPath, true))
Jan Eilers45274902020-10-15 18:34:43 +010024 {
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 Daviscb8e3502020-11-12 10:27:19 +000035 throw armnn::InvalidArgumentException(fmt::format("The tuning level {} is not valid.",
36 tuningLevel));
Jan Eilers45274902020-10-15 18:34:43 +010037 }
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 Eilers45274902020-10-15 18:34:43 +010047}
48
49void ExecuteNetworkParams::ValidateParams()
50{
Jan Eilersf17fcd52021-07-26 22:20:00 +010051 if (m_DynamicBackendsPath == "")
Jan Eilers45274902020-10-15 18:34:43 +010052 {
Jan Eilersf17fcd52021-07-26 22:20:00 +010053 // 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 Murtaghbf18a262020-10-27 15:20:40 +000056 {
Jan Eilersf17fcd52021-07-26 22:20:00 +010057 ARMNN_LOG(fatal) << "The list of preferred devices contains invalid backend IDs: "
58 << invalidBackends;
Francis Murtaghbf18a262020-10-27 15:20:40 +000059 }
Jan Eilers45274902020-10-15 18:34:43 +010060 }
Jan Eilersf17fcd52021-07-26 22:20:00 +010061 CheckClTuningParameter(m_TuningLevel, m_TuningPath, m_ComputeDevices);
62
Ryan OShea31441592022-11-07 16:20:48 +000063 if (m_EnableBf16TurboMode && !m_EnableFastMath)
Keith Daviscb8e3502020-11-12 10:27:19 +000064 {
Ryan OShea31441592022-11-07 16:20:48 +000065 throw armnn::InvalidArgumentException("To use BF16 please use --enable-fast-math. ");
Jan Eilersf17fcd52021-07-26 22:20:00 +010066 }
67
Jan Eilersf17fcd52021-07-26 22:20:00 +010068 // 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 Daviscb8e3502020-11-12 10:27:19 +000079 {
Jan Eilersf17fcd52021-07-26 22:20:00 +010080 throw armnn::InvalidArgumentException("One or more input data file paths are not valid.");
Keith Daviscb8e3502020-11-12 10:27:19 +000081 }
Jan Eilersf17fcd52021-07-26 22:20:00 +010082
83 if (m_InputTensorDataFilePaths.size() < m_InputNames.size())
Keith Daviscb8e3502020-11-12 10:27:19 +000084 {
Jan Eilersf17fcd52021-07-26 22:20:00 +010085 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 Eilersf17fcd52021-07-26 22:20:00 +010092 }
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 Eilers45274902020-10-15 18:34:43 +010099
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 Kelly80512b02022-05-16 23:10:42 +0100105
106 if (m_AllowExpandedDims && m_InferOutputShape)
107 {
108 throw armnn::InvalidArgumentException("infer-output-shape and allow-expanded-dims cannot be used together.");
109 }
Colm Donelan3cff15a2021-10-12 15:06:19 +0100110}
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 */
118armnnDelegate::DelegateOptions ExecuteNetworkParams::ToDelegateOptions() const
119{
120 armnnDelegate::DelegateOptions delegateOptions(m_ComputeDevices);
121 delegateOptions.SetDynamicBackendsPath(m_DynamicBackendsPath);
122 delegateOptions.SetGpuProfilingState(m_EnableProfiling);
Declan-ARM6d2d4ea2023-06-27 10:48:38 +0100123 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 Donelanda7f2f92022-12-20 16:21:35 +0000133
134 // GPU Backend options first.
Colm Donelan3cff15a2021-10-12 15:06:19 +0100135 {
Colm Donelanda7f2f92022-12-20 16:21:35 +0000136 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 Donelan3cff15a2021-10-12 15:06:19 +0100146 }
147
Colm Donelanda7f2f92022-12-20 16:21:35 +0000148 // Optimizer options next.
John Mcloughlinc5ee0d72023-03-24 12:07:25 +0000149 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 Donelanda7f2f92022-12-20 16:21:35 +0000155 if (m_InferOutputShape)
156 {
John Mcloughlinc5ee0d72023-03-24 12:07:25 +0000157 optimizerOptions.SetShapeInferenceMethod(armnn::ShapeInferenceMethod::InferAndValidate);
Colm Donelanda7f2f92022-12-20 16:21:35 +0000158 armnn::BackendOptions networkOption("ShapeInferenceMethod",
159 {
160 {"InferAndValidate", true}
161 });
John Mcloughlinc5ee0d72023-03-24 12:07:25 +0000162 optimizerOptions.AddModelOption(networkOption);
Colm Donelanda7f2f92022-12-20 16:21:35 +0000163 }
164
Tracy Narine1685bcc2023-12-12 13:57:16 +0000165 if (m_EnableLayerDetails)
166 {
167 fs::path filename = m_ModelPath;
168 filename.replace_extension("dot");
169 delegateOptions.SetSerializeToDot(filename);
170 }
171
Colm Donelanda7f2f92022-12-20 16:21:35 +0000172 {
173 armnn::BackendOptions option("GpuAcc", {{"FastMathEnabled", m_EnableFastMath}});
John Mcloughlinc5ee0d72023-03-24 12:07:25 +0000174 optimizerOptions.AddModelOption(option);
Colm Donelanda7f2f92022-12-20 16:21:35 +0000175 }
176 {
177 armnn::BackendOptions option("GpuAcc", {{"CachedNetworkFilePath", m_CachedNetworkFilePath}});
John Mcloughlinc5ee0d72023-03-24 12:07:25 +0000178 optimizerOptions.AddModelOption(option);
Colm Donelanda7f2f92022-12-20 16:21:35 +0000179 }
180 {
181 armnn::BackendOptions option("GpuAcc", {{"MLGOTuningFilePath", m_MLGOTuningFilePath}});
John Mcloughlinc5ee0d72023-03-24 12:07:25 +0000182 optimizerOptions.AddModelOption(option);
Colm Donelanda7f2f92022-12-20 16:21:35 +0000183 }
Colm Donelan3cff15a2021-10-12 15:06:19 +0100184
185 armnn::BackendOptions cpuAcc("CpuAcc",
186 {
187 { "FastMathEnabled", m_EnableFastMath },
188 { "NumberOfThreads", m_NumberOfThreads }
189 });
John Mcloughlinc5ee0d72023-03-24 12:07:25 +0000190 optimizerOptions.AddModelOption(cpuAcc);
Mike Kelly80512b02022-05-16 23:10:42 +0100191 if (m_AllowExpandedDims)
192 {
193 armnn::BackendOptions networkOption("AllowExpandedDims",
194 {
195 {"AllowExpandedDims", true}
196 });
John Mcloughlinc5ee0d72023-03-24 12:07:25 +0000197 optimizerOptions.AddModelOption(networkOption);
Mike Kelly80512b02022-05-16 23:10:42 +0100198 }
Colm Donelanda7f2f92022-12-20 16:21:35 +0000199 delegateOptions.SetOptimizerOptions(optimizerOptions);
Colm Donelan3cff15a2021-10-12 15:06:19 +0100200 return delegateOptions;
201}
Teresa Charlin83b42912022-07-07 14:24:59 +0100202
Colm Donelan3cff15a2021-10-12 15:06:19 +0100203#endif