xref: /aosp_15_r20/external/ComputeLibrary/utils/CommonGraphOptions.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "CommonGraphOptions.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/Utils.h"
27*c217d954SCole Faust #include "arm_compute/graph/TypeLoader.h"
28*c217d954SCole Faust #include "arm_compute/graph/TypePrinter.h"
29*c217d954SCole Faust 
30*c217d954SCole Faust #include "support/StringSupport.h"
31*c217d954SCole Faust 
32*c217d954SCole Faust #include <map>
33*c217d954SCole Faust 
34*c217d954SCole Faust using namespace arm_compute::graph;
35*c217d954SCole Faust 
36*c217d954SCole Faust namespace
37*c217d954SCole Faust {
parse_validation_range(const std::string & validation_range)38*c217d954SCole Faust std::pair<unsigned int, unsigned int> parse_validation_range(const std::string &validation_range)
39*c217d954SCole Faust {
40*c217d954SCole Faust     std::pair<unsigned int /* start */, unsigned int /* end */> range = { 0, std::numeric_limits<unsigned int>::max() };
41*c217d954SCole Faust     if(!validation_range.empty())
42*c217d954SCole Faust     {
43*c217d954SCole Faust         std::string       str;
44*c217d954SCole Faust         std::stringstream stream(validation_range);
45*c217d954SCole Faust 
46*c217d954SCole Faust         // Get first value
47*c217d954SCole Faust         std::getline(stream, str, ',');
48*c217d954SCole Faust         if(stream.fail())
49*c217d954SCole Faust         {
50*c217d954SCole Faust             return range;
51*c217d954SCole Faust         }
52*c217d954SCole Faust         else
53*c217d954SCole Faust         {
54*c217d954SCole Faust             range.first = arm_compute::support::cpp11::stoi(str);
55*c217d954SCole Faust         }
56*c217d954SCole Faust 
57*c217d954SCole Faust         // Get second value
58*c217d954SCole Faust         std::getline(stream, str);
59*c217d954SCole Faust         if(stream.fail())
60*c217d954SCole Faust         {
61*c217d954SCole Faust             range.second = range.first;
62*c217d954SCole Faust             return range;
63*c217d954SCole Faust         }
64*c217d954SCole Faust         else
65*c217d954SCole Faust         {
66*c217d954SCole Faust             range.second = arm_compute::support::cpp11::stoi(str);
67*c217d954SCole Faust         }
68*c217d954SCole Faust     }
69*c217d954SCole Faust     return range;
70*c217d954SCole Faust }
71*c217d954SCole Faust } // namespace
72*c217d954SCole Faust 
73*c217d954SCole Faust namespace arm_compute
74*c217d954SCole Faust {
75*c217d954SCole Faust namespace utils
76*c217d954SCole Faust {
operator <<(::std::ostream & os,const CommonGraphParams & common_params)77*c217d954SCole Faust ::std::ostream &operator<<(::std::ostream &os, const CommonGraphParams &common_params)
78*c217d954SCole Faust {
79*c217d954SCole Faust     std::string false_str = std::string("false");
80*c217d954SCole Faust     std::string true_str  = std::string("true");
81*c217d954SCole Faust 
82*c217d954SCole Faust     os << "Threads : " << common_params.threads << std::endl;
83*c217d954SCole Faust     os << "Target : " << common_params.target << std::endl;
84*c217d954SCole Faust     os << "Data type : " << common_params.data_type << std::endl;
85*c217d954SCole Faust     os << "Data layout : " << common_params.data_layout << std::endl;
86*c217d954SCole Faust     os << "Tuner enabled? : " << (common_params.enable_tuner ? true_str : false_str) << std::endl;
87*c217d954SCole Faust     os << "Cache enabled? : " << (common_params.enable_cl_cache ? true_str : false_str) << std::endl;
88*c217d954SCole Faust     os << "Tuner mode : " << common_params.tuner_mode << std::endl;
89*c217d954SCole Faust     os << "Tuner file : " << common_params.tuner_file << std::endl;
90*c217d954SCole Faust     os << "MLGO file : " << common_params.mlgo_file << std::endl;
91*c217d954SCole Faust     os << "Fast math enabled? : " << (common_params.fast_math_hint == FastMathHint::Enabled ? true_str : false_str) << std::endl;
92*c217d954SCole Faust     if(!common_params.data_path.empty())
93*c217d954SCole Faust     {
94*c217d954SCole Faust         os << "Data path : " << common_params.data_path << std::endl;
95*c217d954SCole Faust     }
96*c217d954SCole Faust     if(!common_params.image.empty())
97*c217d954SCole Faust     {
98*c217d954SCole Faust         os << "Image file : " << common_params.image << std::endl;
99*c217d954SCole Faust     }
100*c217d954SCole Faust     if(!common_params.labels.empty())
101*c217d954SCole Faust     {
102*c217d954SCole Faust         os << "Labels file : " << common_params.labels << std::endl;
103*c217d954SCole Faust     }
104*c217d954SCole Faust     if(!common_params.validation_file.empty())
105*c217d954SCole Faust     {
106*c217d954SCole Faust         os << "Validation range : " << common_params.validation_range_start << "-" << common_params.validation_range_end << std::endl;
107*c217d954SCole Faust         os << "Validation file : " << common_params.validation_file << std::endl;
108*c217d954SCole Faust         if(!common_params.validation_path.empty())
109*c217d954SCole Faust         {
110*c217d954SCole Faust             os << "Validation path : " << common_params.validation_path << std::endl;
111*c217d954SCole Faust         }
112*c217d954SCole Faust     }
113*c217d954SCole Faust 
114*c217d954SCole Faust     return os;
115*c217d954SCole Faust }
116*c217d954SCole Faust 
CommonGraphOptions(CommandLineParser & parser)117*c217d954SCole Faust CommonGraphOptions::CommonGraphOptions(CommandLineParser &parser)
118*c217d954SCole Faust     : help(parser.add_option<ToggleOption>("help")),
119*c217d954SCole Faust       threads(parser.add_option<SimpleOption<int>>("threads", 1)),
120*c217d954SCole Faust       batches(parser.add_option<SimpleOption<int>>("batches", 1)),
121*c217d954SCole Faust       target(),
122*c217d954SCole Faust       data_type(),
123*c217d954SCole Faust       data_layout(),
124*c217d954SCole Faust       enable_tuner(parser.add_option<ToggleOption>("enable-tuner")),
125*c217d954SCole Faust       enable_cl_cache(parser.add_option<ToggleOption>("enable-cl-cache")),
126*c217d954SCole Faust       tuner_mode(),
127*c217d954SCole Faust       fast_math_hint(parser.add_option<ToggleOption>("fast-math")),
128*c217d954SCole Faust       data_path(parser.add_option<SimpleOption<std::string>>("data")),
129*c217d954SCole Faust       image(parser.add_option<SimpleOption<std::string>>("image")),
130*c217d954SCole Faust       labels(parser.add_option<SimpleOption<std::string>>("labels")),
131*c217d954SCole Faust       validation_file(parser.add_option<SimpleOption<std::string>>("validation-file")),
132*c217d954SCole Faust       validation_path(parser.add_option<SimpleOption<std::string>>("validation-path")),
133*c217d954SCole Faust       validation_range(parser.add_option<SimpleOption<std::string>>("validation-range")),
134*c217d954SCole Faust       tuner_file(parser.add_option<SimpleOption<std::string>>("tuner-file")),
135*c217d954SCole Faust       mlgo_file(parser.add_option<SimpleOption<std::string>>("mlgo-file"))
136*c217d954SCole Faust {
137*c217d954SCole Faust     std::set<arm_compute::graph::Target> supported_targets
138*c217d954SCole Faust     {
139*c217d954SCole Faust         Target::NEON,
140*c217d954SCole Faust         Target::CL,
141*c217d954SCole Faust         Target::CLVK,
142*c217d954SCole Faust     };
143*c217d954SCole Faust 
144*c217d954SCole Faust     std::set<arm_compute::DataType> supported_data_types
145*c217d954SCole Faust     {
146*c217d954SCole Faust         DataType::F16,
147*c217d954SCole Faust         DataType::F32,
148*c217d954SCole Faust         DataType::QASYMM8,
149*c217d954SCole Faust         DataType::QASYMM8_SIGNED,
150*c217d954SCole Faust     };
151*c217d954SCole Faust 
152*c217d954SCole Faust     std::set<DataLayout> supported_data_layouts
153*c217d954SCole Faust     {
154*c217d954SCole Faust         DataLayout::NHWC,
155*c217d954SCole Faust         DataLayout::NCHW,
156*c217d954SCole Faust     };
157*c217d954SCole Faust 
158*c217d954SCole Faust     const std::set<CLTunerMode> supported_tuner_modes
159*c217d954SCole Faust     {
160*c217d954SCole Faust         CLTunerMode::EXHAUSTIVE,
161*c217d954SCole Faust         CLTunerMode::NORMAL,
162*c217d954SCole Faust         CLTunerMode::RAPID
163*c217d954SCole Faust     };
164*c217d954SCole Faust 
165*c217d954SCole Faust     target      = parser.add_option<EnumOption<Target>>("target", supported_targets, Target::NEON);
166*c217d954SCole Faust     data_type   = parser.add_option<EnumOption<DataType>>("type", supported_data_types, DataType::F32);
167*c217d954SCole Faust     data_layout = parser.add_option<EnumOption<DataLayout>>("layout", supported_data_layouts);
168*c217d954SCole Faust     tuner_mode  = parser.add_option<EnumOption<CLTunerMode>>("tuner-mode", supported_tuner_modes, CLTunerMode::NORMAL);
169*c217d954SCole Faust 
170*c217d954SCole Faust     help->set_help("Show this help message");
171*c217d954SCole Faust     threads->set_help("Number of threads to use");
172*c217d954SCole Faust     batches->set_help("Number of batches to use for the inputs");
173*c217d954SCole Faust     target->set_help("Target to execute on");
174*c217d954SCole Faust     data_type->set_help("Data type to use");
175*c217d954SCole Faust     data_layout->set_help("Data layout to use");
176*c217d954SCole Faust     enable_tuner->set_help("Enable OpenCL dynamic tuner");
177*c217d954SCole Faust     enable_cl_cache->set_help("Enable OpenCL program caches");
178*c217d954SCole Faust     tuner_mode->set_help(
179*c217d954SCole Faust         "Configures the time taken by the tuner to tune. "
180*c217d954SCole Faust         "Exhaustive: slowest but produces the most performant LWS configuration. "
181*c217d954SCole Faust         "Normal: slow but produces the LWS configurations on par with Exhaustive most of the time. "
182*c217d954SCole Faust         "Rapid: fast but produces less performant LWS configurations");
183*c217d954SCole Faust     fast_math_hint->set_help("Enable fast math");
184*c217d954SCole Faust     data_path->set_help("Path where graph parameters reside");
185*c217d954SCole Faust     image->set_help("Input image for the graph");
186*c217d954SCole Faust     labels->set_help("File containing the output labels");
187*c217d954SCole Faust     validation_file->set_help("File used to validate the graph");
188*c217d954SCole Faust     validation_path->set_help("Path to the validation data");
189*c217d954SCole Faust     validation_range->set_help("Range of the images to validate for (Format : start,end)");
190*c217d954SCole Faust     tuner_file->set_help("File to load/save CLTuner values");
191*c217d954SCole Faust     mlgo_file->set_help("File to load MLGO heuristics");
192*c217d954SCole Faust }
193*c217d954SCole Faust 
consume_common_graph_parameters(CommonGraphOptions & options)194*c217d954SCole Faust CommonGraphParams consume_common_graph_parameters(CommonGraphOptions &options)
195*c217d954SCole Faust {
196*c217d954SCole Faust     FastMathHint fast_math_hint_value = options.fast_math_hint->value() ? FastMathHint::Enabled : FastMathHint::Disabled;
197*c217d954SCole Faust     auto         validation_range     = parse_validation_range(options.validation_range->value());
198*c217d954SCole Faust 
199*c217d954SCole Faust     CommonGraphParams common_params;
200*c217d954SCole Faust     common_params.help      = options.help->is_set() ? options.help->value() : false;
201*c217d954SCole Faust     common_params.threads   = options.threads->value();
202*c217d954SCole Faust     common_params.batches   = options.batches->value();
203*c217d954SCole Faust     common_params.target    = options.target->value();
204*c217d954SCole Faust     common_params.data_type = options.data_type->value();
205*c217d954SCole Faust     if(options.data_layout->is_set())
206*c217d954SCole Faust     {
207*c217d954SCole Faust         common_params.data_layout = options.data_layout->value();
208*c217d954SCole Faust     }
209*c217d954SCole Faust     common_params.enable_tuner           = options.enable_tuner->is_set() ? options.enable_tuner->value() : false;
210*c217d954SCole Faust     common_params.enable_cl_cache        = common_params.target == arm_compute::graph::Target::NEON ? false : (options.enable_cl_cache->is_set() ? options.enable_cl_cache->value() : true);
211*c217d954SCole Faust     common_params.tuner_mode             = options.tuner_mode->value();
212*c217d954SCole Faust     common_params.fast_math_hint         = options.fast_math_hint->is_set() ? fast_math_hint_value : FastMathHint::Disabled;
213*c217d954SCole Faust     common_params.data_path              = options.data_path->value();
214*c217d954SCole Faust     common_params.image                  = options.image->value();
215*c217d954SCole Faust     common_params.labels                 = options.labels->value();
216*c217d954SCole Faust     common_params.validation_file        = options.validation_file->value();
217*c217d954SCole Faust     common_params.validation_path        = options.validation_path->value();
218*c217d954SCole Faust     common_params.validation_range_start = validation_range.first;
219*c217d954SCole Faust     common_params.validation_range_end   = validation_range.second;
220*c217d954SCole Faust     common_params.tuner_file             = options.tuner_file->value();
221*c217d954SCole Faust     common_params.mlgo_file              = options.mlgo_file->value();
222*c217d954SCole Faust 
223*c217d954SCole Faust     return common_params;
224*c217d954SCole Faust }
225*c217d954SCole Faust } // namespace utils
226*c217d954SCole Faust } // namespace arm_compute
227