d8ed95e9ac74c2c669fc72bfbab70890be25cd1c
[platform/core/ml/nnfw.git] / compiler / record-minmax / driver / Driver.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "RecordMinMax.h"
18
19 #include <arser/arser.h>
20 #include <vconone/vconone.h>
21
22 #include <luci/UserSettings.h>
23
24 // TODO declare own log signature of record-minmax
25 #include <luci/Log.h>
26
27 void print_version(void)
28 {
29   std::cout << "record-minmax version " << vconone::get_string() << std::endl;
30   std::cout << vconone::get_copyright() << std::endl;
31 }
32
33 int entry(const int argc, char **argv)
34 {
35   using namespace record_minmax;
36
37   LOGGER(l);
38
39   arser::Arser arser(
40     "Embedding min/max values of activations to the circle model for post-training quantization");
41
42   arser::Helper::add_version(arser, print_version);
43   arser::Helper::add_verbose(arser);
44
45   arser.add_argument("--input_model").required(true).help("Input model filepath");
46
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 "
50           "model accuracy.");
51
52   arser.add_argument("--output_model").required(true).help("Output model filepath");
53
54   arser.add_argument("--min_percentile")
55     .type(arser::DataType::FLOAT)
56     .help("Record n'th percentile of min");
57
58   arser.add_argument("--num_threads")
59     .type(arser::DataType::INT32)
60     .help("Number of threads (default: 1)");
61
62   arser.add_argument("--max_percentile")
63     .type(arser::DataType::FLOAT)
64     .help("Record n'th percentile of max");
65
66   arser.add_argument("--mode").help("Record mode. percentile (default) or moving_average");
67
68   arser.add_argument("--input_data_format")
69     .help("Input data format. h5/hdf5 (default) or list/filelist");
70
71   arser.add_argument("--generate_profile_data")
72     .nargs(0)
73     .default_value(false)
74     .help("This will turn on profiling data generation.");
75
76   try
77   {
78     arser.parse(argc, argv);
79   }
80   catch (const std::runtime_error &err)
81   {
82     std::cout << err.what() << std::endl;
83     std::cout << arser;
84     return 255;
85   }
86
87   if (arser.get<bool>("--verbose"))
88   {
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);
92   }
93
94   auto settings = luci::UserSettings::settings();
95
96   auto input_model_path = arser.get<std::string>("--input_model");
97   auto output_model_path = arser.get<std::string>("--output_model");
98
99   // Default values
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;
105
106   if (arser["--min_percentile"])
107     min_percentile = arser.get<float>("--min_percentile");
108
109   if (arser["--num_threads"])
110     num_threads = arser.get<int>("--num_threads");
111
112   if (num_threads < 1)
113     throw std::runtime_error("The number of threads must be greater than zero");
114
115   if (arser["--max_percentile"])
116     max_percentile = arser.get<float>("--max_percentile");
117
118   if (arser["--mode"])
119     mode = arser.get<std::string>("--mode");
120
121   if (mode != "percentile" && mode != "moving_average")
122     throw std::runtime_error("Unsupported mode");
123
124   if (arser["--generate_profile_data"])
125     settings->set(luci::UserSettings::Key::ProfilingDataGen, true);
126
127   if (arser["--input_data_format"])
128     input_data_format = arser.get<std::string>("--input_data_format");
129
130   RecordMinMax rmm(num_threads);
131
132   // TODO: support parallel record for profile with random data
133   if (num_threads > 1 and not arser["--input_data"])
134   {
135     throw std::runtime_error("Input data must be given for parallel recording");
136   }
137
138   // Initialize interpreter and observer
139   rmm.initialize(input_model_path);
140
141   if (arser["--input_data"])
142   {
143     auto input_data_path = arser.get<std::string>("--input_data");
144
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"))
147     {
148       throw std::runtime_error("Parallel recording is used only for h5 now");
149     }
150
151     if (input_data_format == "h5" || input_data_format == "hdf5")
152     {
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);
156       else
157       {
158         INFO(l) << "Using parallel recording" << std::endl;
159         rmm.profileDataInParallel(mode, input_data_path, min_percentile, max_percentile);
160       }
161     }
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
165     //
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")
171     {
172       // Profile min/max while executing the list of Raw data
173       rmm.profileRawData(mode, input_data_path, min_percentile, max_percentile);
174     }
175     else if (input_data_format == "directory" || input_data_format == "dir")
176     {
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);
180     }
181     else
182     {
183       throw std::runtime_error(
184         "Unsupported input data format (supported formats: h5/hdf5 (default), list/filelist)");
185     }
186   }
187   else
188   {
189     // Profile min/max while executing random input data
190     rmm.profileDataWithRandomInputs(mode, min_percentile, max_percentile);
191   }
192
193   // Save profiled values to the model
194   rmm.saveModel(output_model_path);
195
196   return EXIT_SUCCESS;
197 }