Compute Library
 21.08
CommonGraphOptions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "CommonGraphOptions.h"
25 
26 #include "arm_compute/core/Utils.h"
29 
30 #include "support/StringSupport.h"
31 
32 #include <map>
33 
34 using namespace arm_compute::graph;
35 
36 namespace
37 {
38 std::pair<unsigned int, unsigned int> parse_validation_range(const std::string &validation_range)
39 {
40  std::pair<unsigned int /* start */, unsigned int /* end */> range = { 0, std::numeric_limits<unsigned int>::max() };
41  if(!validation_range.empty())
42  {
43  std::string str;
44  std::stringstream stream(validation_range);
45 
46  // Get first value
47  std::getline(stream, str, ',');
48  if(stream.fail())
49  {
50  return range;
51  }
52  else
53  {
54  range.first = arm_compute::support::cpp11::stoi(str);
55  }
56 
57  // Get second value
58  std::getline(stream, str);
59  if(stream.fail())
60  {
61  range.second = range.first;
62  return range;
63  }
64  else
65  {
66  range.second = arm_compute::support::cpp11::stoi(str);
67  }
68  }
69  return range;
70 }
71 } // namespace
72 
73 namespace arm_compute
74 {
75 namespace utils
76 {
77 ::std::ostream &operator<<(::std::ostream &os, const CommonGraphParams &common_params)
78 {
79  std::string false_str = std::string("false");
80  std::string true_str = std::string("true");
81 
82  os << "Threads : " << common_params.threads << std::endl;
83  os << "Target : " << common_params.target << std::endl;
84  os << "Data type : " << common_params.data_type << std::endl;
85  os << "Data layout : " << common_params.data_layout << std::endl;
86  os << "Tuner enabled? : " << (common_params.enable_tuner ? true_str : false_str) << std::endl;
87  os << "Cache enabled? : " << (common_params.enable_cl_cache ? true_str : false_str) << std::endl;
88  os << "Tuner mode : " << common_params.tuner_mode << std::endl;
89  os << "Tuner file : " << common_params.tuner_file << std::endl;
90  os << "MLGO file : " << common_params.mlgo_file << std::endl;
91  os << "Fast math enabled? : " << (common_params.fast_math_hint == FastMathHint::Enabled ? true_str : false_str) << std::endl;
92  if(!common_params.data_path.empty())
93  {
94  os << "Data path : " << common_params.data_path << std::endl;
95  }
96  if(!common_params.image.empty())
97  {
98  os << "Image file : " << common_params.image << std::endl;
99  }
100  if(!common_params.labels.empty())
101  {
102  os << "Labels file : " << common_params.labels << std::endl;
103  }
104  if(!common_params.validation_file.empty())
105  {
106  os << "Validation range : " << common_params.validation_range_start << "-" << common_params.validation_range_end << std::endl;
107  os << "Validation file : " << common_params.validation_file << std::endl;
108  if(!common_params.validation_path.empty())
109  {
110  os << "Validation path : " << common_params.validation_path << std::endl;
111  }
112  }
113 
114  return os;
115 }
116 
117 CommonGraphOptions::CommonGraphOptions(CommandLineParser &parser)
118  : help(parser.add_option<ToggleOption>("help")),
119  threads(parser.add_option<SimpleOption<int>>("threads", 1)),
120  batches(parser.add_option<SimpleOption<int>>("batches", 1)),
121  target(),
122  data_type(),
123  data_layout(),
124  enable_tuner(parser.add_option<ToggleOption>("enable-tuner")),
125  enable_cl_cache(parser.add_option<ToggleOption>("enable-cl-cache")),
126  tuner_mode(),
127  fast_math_hint(parser.add_option<ToggleOption>("fast-math")),
128  data_path(parser.add_option<SimpleOption<std::string>>("data")),
129  image(parser.add_option<SimpleOption<std::string>>("image")),
130  labels(parser.add_option<SimpleOption<std::string>>("labels")),
131  validation_file(parser.add_option<SimpleOption<std::string>>("validation-file")),
132  validation_path(parser.add_option<SimpleOption<std::string>>("validation-path")),
133  validation_range(parser.add_option<SimpleOption<std::string>>("validation-range")),
134  tuner_file(parser.add_option<SimpleOption<std::string>>("tuner-file")),
135  mlgo_file(parser.add_option<SimpleOption<std::string>>("mlgo-file"))
136 {
137  std::set<arm_compute::graph::Target> supported_targets
138  {
139  Target::NEON,
140  Target::CL,
141  Target::CLVK,
142  };
143 
144  std::set<arm_compute::DataType> supported_data_types
145  {
150  };
151 
152  std::set<DataLayout> supported_data_layouts
153  {
156  };
157 
158  const std::set<CLTunerMode> supported_tuner_modes
159  {
163  };
164 
165  target = parser.add_option<EnumOption<Target>>("target", supported_targets, Target::NEON);
166  data_type = parser.add_option<EnumOption<DataType>>("type", supported_data_types, DataType::F32);
167  data_layout = parser.add_option<EnumOption<DataLayout>>("layout", supported_data_layouts);
168  tuner_mode = parser.add_option<EnumOption<CLTunerMode>>("tuner-mode", supported_tuner_modes, CLTunerMode::NORMAL);
169 
170  help->set_help("Show this help message");
171  threads->set_help("Number of threads to use");
172  batches->set_help("Number of batches to use for the inputs");
173  target->set_help("Target to execute on");
174  data_type->set_help("Data type to use");
175  data_layout->set_help("Data layout to use");
176  enable_tuner->set_help("Enable OpenCL dynamic tuner");
177  enable_cl_cache->set_help("Enable OpenCL program caches");
178  tuner_mode->set_help(
179  "Configures the time taken by the tuner to tune. "
180  "Exhaustive: slowest but produces the most performant LWS configuration. "
181  "Normal: slow but produces the LWS configurations on par with Exhaustive most of the time. "
182  "Rapid: fast but produces less performant LWS configurations");
183  fast_math_hint->set_help("Enable fast math");
184  data_path->set_help("Path where graph parameters reside");
185  image->set_help("Input image for the graph");
186  labels->set_help("File containing the output labels");
187  validation_file->set_help("File used to validate the graph");
188  validation_path->set_help("Path to the validation data");
189  validation_range->set_help("Range of the images to validate for (Format : start,end)");
190  tuner_file->set_help("File to load/save CLTuner values");
191  mlgo_file->set_help("File to load MLGO heuristics");
192 }
193 
195 {
196  FastMathHint fast_math_hint_value = options.fast_math_hint->value() ? FastMathHint::Enabled : FastMathHint::Disabled;
197  auto validation_range = parse_validation_range(options.validation_range->value());
198 
199  CommonGraphParams common_params;
200  common_params.help = options.help->is_set() ? options.help->value() : false;
201  common_params.threads = options.threads->value();
202  common_params.batches = options.batches->value();
203  common_params.target = options.target->value();
204  common_params.data_type = options.data_type->value();
205  if(options.data_layout->is_set())
206  {
207  common_params.data_layout = options.data_layout->value();
208  }
209  common_params.enable_tuner = options.enable_tuner->is_set() ? options.enable_tuner->value() : false;
210  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  common_params.tuner_mode = options.tuner_mode->value();
212  common_params.fast_math_hint = options.fast_math_hint->is_set() ? fast_math_hint_value : FastMathHint::Disabled;
213  common_params.data_path = options.data_path->value();
214  common_params.image = options.image->value();
215  common_params.labels = options.labels->value();
216  common_params.validation_file = options.validation_file->value();
217  common_params.validation_path = options.validation_path->value();
218  common_params.validation_range_start = validation_range.first;
219  common_params.validation_range_end = validation_range.second;
220  common_params.tuner_file = options.tuner_file->value();
221  common_params.mlgo_file = options.mlgo_file->value();
222 
223  return common_params;
224 }
225 } // namespace utils
226 } // namespace arm_compute
Arm® Neon™ capable target device.
ToggleOption * enable_tuner
Enable tuner.
SimpleOption< arm_compute::CLTunerMode > * tuner_mode
Tuner mode.
ToggleOption * help
Show help option.
SimpleOption< std::string > * mlgo_file
File to load the MLGO heuristics from.
EnumOption< arm_compute::DataType > * data_type
Graph data type.
EnumOption< arm_compute::DataLayout > * data_layout
Graph data layout.
Implementation of an option that can be either true or false.
Definition: ToggleOption.h:36
1 channel, 1 F32 per channel
Searches a subset of LWS configurations while tuning.
const DataLayout data_layout
Definition: Im2Col.cpp:151
Fast math enabled for Convolution layer.
void consume_common_graph_parameters(CommonGraphValidateOptions &options, CommonParams &common_params)
Consumes the consume_common_graph_parameters graph options and creates a structure containing any inf...
Common command line options used to configure the graph examples.
SimpleOption< std::string > * tuner_file
File to load/store the tuner&#39;s values from.
Class to parse command line arguments.
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
EnumOption< arm_compute::graph::Target > * target
Graph execution target.
Searches a minimal subset of LWS configurations while tuning.
const DataType data_type
Definition: Im2Col.cpp:150
T * add_option(const std::string &name, As &&... args)
Function to add a new option to the parser.
inline ::std::ostream & operator<<(::std::ostream &os, const Target &target)
Formatted output of the Target.
Definition: TypePrinter.h:38
SimpleTensor< T > range(SimpleTensor< T > &dst, float start, const size_t num_of_elements, float step)
Definition: Range.cpp:50
ToggleOption * enable_cl_cache
Enable opencl kernels cache.
quantized, asymmetric fixed-point 8-bit number unsigned
SimpleOption< int > * batches
Number of batches.
const T & value() const
Get the option value.
Definition: SimpleOption.h:112
int stoi(const std::string &str, std::size_t *pos=0, NumericBase base=NumericBase::BASE_10)
Convert string values to integer.
Definition: StringSupport.h:55
ToggleOption * fast_math_hint
Fast math hint.
SimpleOption< std::string > * validation_file
Validation file.
Num samples, channels, height, width.
FastMathHint
Enable or disable fast math for Convolution layer.
Definition: Types.h:142
Structure holding all the common graph parameters.
SimpleOption< std::string > * validation_path
Validation data path.
Num samples, height, width, channels.
Implementation of a simple option that accepts a value from a fixed set.
Definition: EnumOption.h:40
SimpleOption< std::string > * validation_range
Validation range.
SimpleOption< std::string > * data_path
Trainable parameters path.
SimpleOption< std::string > * labels
Labels.
bool is_set() const
Has a value been assigned to the option?
Definition: Option.h:135
quantized, asymmetric fixed-point 8-bit number signed
unsigned int batches
const T & value() const
Get the selected value.
SimpleOption< int > * threads
Number of threads option.
Searches all possible LWS configurations while tuning.
arm_compute::graph::FastMathHint fast_math_hint
Implementation of an option that accepts a single value.
Definition: SimpleOption.h:39
void set_help(std::string help)
Set the help message for the option.
Definition: Option.h:125
SimpleOption< std::string > * image
Image.