2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "RecordMinMax.h"
19 #include <arser/arser.h>
20 #include <vconone/vconone.h>
22 #include <luci/UserSettings.h>
24 // TODO declare own log signature of record-minmax
27 void print_version(void)
29 std::cout << "record-minmax version " << vconone::get_string() << std::endl;
30 std::cout << vconone::get_copyright() << std::endl;
33 int entry(const int argc, char **argv)
35 using namespace record_minmax;
40 "Embedding min/max values of activations to the circle model for post-training quantization");
42 arser::Helper::add_version(arser, print_version);
43 arser::Helper::add_verbose(arser);
45 arser.add_argument("--input_model").required(true).help("Input model filepath");
47 arser.add_argument("--input_data")
48 .help("Input data filepath. If not given, record-minmax will run with randomly generated data. "
49 "Note that the random dataset does not represent inference workload, leading to poor "
52 arser.add_argument("--output_model").required(true).help("Output model filepath");
54 arser.add_argument("--min_percentile")
55 .type(arser::DataType::FLOAT)
56 .help("Record n'th percentile of min");
58 arser.add_argument("--num_threads")
59 .type(arser::DataType::INT32)
60 .help("Number of threads (default: 1)");
62 arser.add_argument("--max_percentile")
63 .type(arser::DataType::FLOAT)
64 .help("Record n'th percentile of max");
66 arser.add_argument("--mode").help("Record mode. percentile (default) or moving_average");
68 arser.add_argument("--input_data_format")
69 .help("Input data format. h5/hdf5 (default) or list/filelist");
71 arser.add_argument("--generate_profile_data")
74 .help("This will turn on profiling data generation.");
78 arser.parse(argc, argv);
80 catch (const std::runtime_error &err)
82 std::cout << err.what() << std::endl;
87 if (arser.get<bool>("--verbose"))
89 // The third parameter of setenv means REPLACE.
90 // If REPLACE is zero, it does not overwrite an existing value.
91 setenv("LUCI_LOG", "100", 0);
94 auto settings = luci::UserSettings::settings();
96 auto input_model_path = arser.get<std::string>("--input_model");
97 auto output_model_path = arser.get<std::string>("--output_model");
100 std::string mode("percentile");
101 float min_percentile = 1.0;
102 float max_percentile = 99.0;
103 std::string input_data_format("h5");
104 uint32_t num_threads = 1;
106 if (arser["--min_percentile"])
107 min_percentile = arser.get<float>("--min_percentile");
109 if (arser["--num_threads"])
110 num_threads = arser.get<int>("--num_threads");
113 throw std::runtime_error("The number of threads must be greater than zero");
115 if (arser["--max_percentile"])
116 max_percentile = arser.get<float>("--max_percentile");
119 mode = arser.get<std::string>("--mode");
121 if (mode != "percentile" && mode != "moving_average")
122 throw std::runtime_error("Unsupported mode");
124 if (arser["--generate_profile_data"])
125 settings->set(luci::UserSettings::Key::ProfilingDataGen, true);
127 if (arser["--input_data_format"])
128 input_data_format = arser.get<std::string>("--input_data_format");
130 RecordMinMax rmm(num_threads);
132 // TODO: support parallel record for profile with random data
133 if (num_threads > 1 and not arser["--input_data"])
135 throw std::runtime_error("Input data must be given for parallel recording");
138 // Initialize interpreter and observer
139 rmm.initialize(input_model_path);
141 if (arser["--input_data"])
143 auto input_data_path = arser.get<std::string>("--input_data");
145 // TODO: support parallel record from file and dir input data format
146 if (num_threads > 1 and not(input_data_format == "h5") and not(input_data_format == "hdf5"))
148 throw std::runtime_error("Parallel recording is used only for h5 now");
151 if (input_data_format == "h5" || input_data_format == "hdf5")
153 // Profile min/max while executing the H5 data
154 if (num_threads == 1)
155 rmm.profileData(mode, input_data_path, min_percentile, max_percentile);
158 INFO(l) << "Using parallel recording" << std::endl;
159 rmm.profileDataInParallel(mode, input_data_path, min_percentile, max_percentile);
162 // input_data is a text file having a file path in each line.
163 // Each data file is composed of inputs of a model, concatenated in
164 // the same order with the input index of the model
166 // For example, for a model with n inputs, the contents of each data
167 // file can be visualized as below
168 // [input 1][input 2]...[input n]
169 // |start............end of file|
170 else if (input_data_format == "list" || input_data_format == "filelist")
172 // Profile min/max while executing the list of Raw data
173 rmm.profileRawData(mode, input_data_path, min_percentile, max_percentile);
175 else if (input_data_format == "directory" || input_data_format == "dir")
177 // Profile min/max while executing all files under the given directory
178 // The contents of each file is same as the raw data in the 'list' type
179 rmm.profileRawDataDirectory(mode, input_data_path, min_percentile, max_percentile);
183 throw std::runtime_error(
184 "Unsupported input data format (supported formats: h5/hdf5 (default), list/filelist)");
189 // Profile min/max while executing random input data
190 rmm.profileDataWithRandomInputs(mode, min_percentile, max_percentile);
193 // Save profiled values to the model
194 rmm.saveModel(output_model_path);