xref: /aosp_15_r20/external/ComputeLibrary/utils/CommonGraphOptions.h (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 #ifndef ARM_COMPUTE_EXAMPLES_UTILS_COMMON_GRAPH_OPTIONS
25*c217d954SCole Faust #define ARM_COMPUTE_EXAMPLES_UTILS_COMMON_GRAPH_OPTIONS
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "utils/command_line/CommandLineOptions.h"
28*c217d954SCole Faust #include "utils/command_line/CommandLineParser.h"
29*c217d954SCole Faust 
30*c217d954SCole Faust #include "arm_compute/graph/TypeLoader.h"
31*c217d954SCole Faust #include "arm_compute/graph/TypePrinter.h"
32*c217d954SCole Faust #include "arm_compute/runtime/CL/CLTunerTypes.h"
33*c217d954SCole Faust 
34*c217d954SCole Faust namespace arm_compute
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace utils
37*c217d954SCole Faust {
38*c217d954SCole Faust /* ![Common graph examples parameters] */
39*c217d954SCole Faust /* Common graph parameters
40*c217d954SCole Faust  *
41*c217d954SCole Faust  * --help             : Print the example's help message.
42*c217d954SCole Faust  * --threads          : The number of threads to be used by the example during execution.
43*c217d954SCole Faust  * --target           : Execution target to be used by the examples. Supported target options: Neon, CL, CLVK.
44*c217d954SCole Faust  * --type             : Data type to be used by the examples. Supported data type options: QASYMM8, F16, F32.
45*c217d954SCole Faust  * --layout           : Data layout to be used by the examples. Supported data layout options : NCHW, NHWC.
46*c217d954SCole Faust  * --enable-tuner     : Toggle option to enable the OpenCL dynamic tuner.
47*c217d954SCole Faust  * --enable-cl-cache  : Toggle option to load the prebuilt opencl kernels from a cache file.
48*c217d954SCole Faust  * --fast-math        : Toggle option to enable the fast math option.
49*c217d954SCole Faust  * --data             : Path that contains the trainable parameter files of graph layers.
50*c217d954SCole Faust  * --image            : Image to load and operate on. Image types supported: PPM, JPEG, NPY.
51*c217d954SCole Faust  * --labels           : File that contains the labels that classify upon.
52*c217d954SCole Faust  * --validation-file  : File that contains a list of image names with their corresponding label id (e.g. image0.jpg 5).
53*c217d954SCole Faust  *                      This is used to run the graph over a number of images and report top-1 and top-5 metrics.
54*c217d954SCole Faust  * --validation-path  : The path where the validation images specified in the validation file reside.
55*c217d954SCole Faust  * --validation-range : The range of the images to validate from the validation file (e.g 0,9).
56*c217d954SCole Faust  *                      If not specified all the images will be validated.
57*c217d954SCole Faust  * --tuner-file       : The file to store the OpenCL dynamic tuner tuned parameters.
58*c217d954SCole Faust  * --tuner-mode       : Select tuner mode. Supported modes: Exhaustive,Normal,Rapid
59*c217d954SCole Faust  *                      * Exhaustive: slowest but produces the most performant LWS configuration.
60*c217d954SCole Faust  *                      * Normal: slow but produces the LWS configurations on par with Exhaustive most of the time.
61*c217d954SCole Faust  *                      * Rapid: fast but produces less performant LWS configurations
62*c217d954SCole Faust  *
63*c217d954SCole Faust  * Note that data, image and labels options should be provided to perform an inference run on an image.
64*c217d954SCole Faust  * Note that validation-file and validation-path should be provided to perform a graph accuracy estimation.
65*c217d954SCole Faust  *
66*c217d954SCole Faust  * Example execution commands:
67*c217d954SCole Faust  *
68*c217d954SCole Faust  * Execute a single inference given an image and a file containing the correspondence between label ids and human readable labels:
69*c217d954SCole Faust  * ./graph_vgg16 --data=data/ --target=cl --layout=nhwc --image=kart.jpeg --labels=imagenet1000_clsid_to_human.txt
70*c217d954SCole Faust  *
71*c217d954SCole Faust  * Perform a graph validation on a list of images:
72*c217d954SCole Faust  * ./graph_vgg16 --data=data/ --target=neon --threads=4 --layout=nchw --validation-file=val.txt --validation-path=ilsvrc_test_images/
73*c217d954SCole Faust  *
74*c217d954SCole Faust  * File formats:
75*c217d954SCole Faust  *
76*c217d954SCole Faust  * Validation file should be a plain file containing the names of the images followed by the correct label id.
77*c217d954SCole Faust  * For example:
78*c217d954SCole Faust  *
79*c217d954SCole Faust  * image0.jpeg 882
80*c217d954SCole Faust  * image1.jpeg 34
81*c217d954SCole Faust  * image2.jpeg 354
82*c217d954SCole Faust  *
83*c217d954SCole Faust  * Labels file should be a plain file where each line is the respective human readable label (counting starts from 0).
84*c217d954SCole Faust  * For example:
85*c217d954SCole Faust  *
86*c217d954SCole Faust  * 0: label0_name            label0_name
87*c217d954SCole Faust  * 1: label1_name     or     label1_name
88*c217d954SCole Faust  * 2: label2_name            label2_name
89*c217d954SCole Faust  */
90*c217d954SCole Faust /* ![Common graph examples parameters] */
91*c217d954SCole Faust 
92*c217d954SCole Faust /** Structure holding all the common graph parameters */
93*c217d954SCole Faust struct CommonGraphParams
94*c217d954SCole Faust {
95*c217d954SCole Faust     bool                             help{ false };
96*c217d954SCole Faust     int                              threads{ 0 };
97*c217d954SCole Faust     int                              batches{ 1 };
98*c217d954SCole Faust     arm_compute::graph::Target       target{ arm_compute::graph::Target::NEON };
99*c217d954SCole Faust     arm_compute::DataType            data_type{ DataType::F32 };
100*c217d954SCole Faust     arm_compute::DataLayout          data_layout{ DataLayout::NHWC };
101*c217d954SCole Faust     bool                             enable_tuner{ false };
102*c217d954SCole Faust     bool                             enable_cl_cache{ false };
103*c217d954SCole Faust     arm_compute::CLTunerMode         tuner_mode{ CLTunerMode::NORMAL };
104*c217d954SCole Faust     arm_compute::graph::FastMathHint fast_math_hint{ arm_compute::graph::FastMathHint::Disabled };
105*c217d954SCole Faust     std::string                      data_path{};
106*c217d954SCole Faust     std::string                      image{};
107*c217d954SCole Faust     std::string                      labels{};
108*c217d954SCole Faust     std::string                      validation_file{};
109*c217d954SCole Faust     std::string                      validation_path{};
110*c217d954SCole Faust     std::string                      tuner_file{};
111*c217d954SCole Faust     std::string                      mlgo_file{};
112*c217d954SCole Faust     unsigned int                     validation_range_start{ 0 };
113*c217d954SCole Faust     unsigned int                     validation_range_end{ std::numeric_limits<unsigned int>::max() };
114*c217d954SCole Faust };
115*c217d954SCole Faust 
116*c217d954SCole Faust /** Formatted output of the CommonGraphParams type
117*c217d954SCole Faust  *
118*c217d954SCole Faust  * @param[out] os            Output stream.
119*c217d954SCole Faust  * @param[in]  common_params Common parameters to output
120*c217d954SCole Faust  *
121*c217d954SCole Faust  * @return Modified output stream.
122*c217d954SCole Faust  */
123*c217d954SCole Faust ::std::ostream &operator<<(::std::ostream &os, const CommonGraphParams &common_params);
124*c217d954SCole Faust 
125*c217d954SCole Faust /** Common command line options used to configure the graph examples
126*c217d954SCole Faust  *
127*c217d954SCole Faust  * The options in this object get populated when "parse()" is called on the parser used to construct it.
128*c217d954SCole Faust  * The expected workflow is:
129*c217d954SCole Faust  *
130*c217d954SCole Faust  * CommandLineParser parser;
131*c217d954SCole Faust  * CommonOptions options( parser );
132*c217d954SCole Faust  * parser.parse(argc, argv);
133*c217d954SCole Faust  */
134*c217d954SCole Faust class CommonGraphOptions
135*c217d954SCole Faust {
136*c217d954SCole Faust public:
137*c217d954SCole Faust     /** Constructor
138*c217d954SCole Faust      *
139*c217d954SCole Faust      * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
140*c217d954SCole Faust      */
141*c217d954SCole Faust     CommonGraphOptions(CommandLineParser &parser);
142*c217d954SCole Faust     /** Prevent instances of this class from being copied (As this class contains pointers) */
143*c217d954SCole Faust     CommonGraphOptions(const CommonGraphOptions &) = delete;
144*c217d954SCole Faust     /** Prevent instances of this class from being copied (As this class contains pointers) */
145*c217d954SCole Faust     CommonGraphOptions &operator=(const CommonGraphOptions &) = delete;
146*c217d954SCole Faust     /** Allow instances of this class to be moved */
147*c217d954SCole Faust     CommonGraphOptions(CommonGraphOptions &&) = default;
148*c217d954SCole Faust     /** Allow instances of this class to be moved */
149*c217d954SCole Faust     CommonGraphOptions &operator=(CommonGraphOptions &&) = default;
150*c217d954SCole Faust     /** Default destructor */
151*c217d954SCole Faust     ~CommonGraphOptions() = default;
152*c217d954SCole Faust 
153*c217d954SCole Faust     ToggleOption                           *help;             /**< Show help option */
154*c217d954SCole Faust     SimpleOption<int>                      *threads;          /**< Number of threads option */
155*c217d954SCole Faust     SimpleOption<int>                      *batches;          /**< Number of batches */
156*c217d954SCole Faust     EnumOption<arm_compute::graph::Target> *target;           /**< Graph execution target */
157*c217d954SCole Faust     EnumOption<arm_compute::DataType>      *data_type;        /**< Graph data type */
158*c217d954SCole Faust     EnumOption<arm_compute::DataLayout>    *data_layout;      /**< Graph data layout */
159*c217d954SCole Faust     ToggleOption                           *enable_tuner;     /**< Enable tuner */
160*c217d954SCole Faust     ToggleOption                           *enable_cl_cache;  /**< Enable opencl kernels cache */
161*c217d954SCole Faust     SimpleOption<arm_compute::CLTunerMode> *tuner_mode;       /**< Tuner mode */
162*c217d954SCole Faust     ToggleOption                           *fast_math_hint;   /**< Fast math hint */
163*c217d954SCole Faust     SimpleOption<std::string>              *data_path;        /**< Trainable parameters path */
164*c217d954SCole Faust     SimpleOption<std::string>              *image;            /**< Image */
165*c217d954SCole Faust     SimpleOption<std::string>              *labels;           /**< Labels */
166*c217d954SCole Faust     SimpleOption<std::string>              *validation_file;  /**< Validation file */
167*c217d954SCole Faust     SimpleOption<std::string>              *validation_path;  /**< Validation data path */
168*c217d954SCole Faust     SimpleOption<std::string>              *validation_range; /**< Validation range */
169*c217d954SCole Faust     SimpleOption<std::string>              *tuner_file;       /**< File to load/store the tuner's values from */
170*c217d954SCole Faust     SimpleOption<std::string>              *mlgo_file;        /**< File to load the MLGO heuristics from */
171*c217d954SCole Faust };
172*c217d954SCole Faust 
173*c217d954SCole Faust /** Consumes the common graph options and creates a structure containing any information
174*c217d954SCole Faust  *
175*c217d954SCole Faust  * @param[in] options Options to consume
176*c217d954SCole Faust  *
177*c217d954SCole Faust  * @return Structure containing the common graph parameters
178*c217d954SCole Faust  */
179*c217d954SCole Faust CommonGraphParams consume_common_graph_parameters(CommonGraphOptions &options);
180*c217d954SCole Faust } // namespace utils
181*c217d954SCole Faust } // namespace arm_compute
182*c217d954SCole Faust #endif /* ARM_COMPUTE_EXAMPLES_UTILS_COMMON_GRAPH_OPTIONS */
183