ONE - On-device Neural Engine
Loading...
Searching...
No Matches
training_configure_tool Namespace Reference

Data Structures

struct  TrainConfigFileData
 
struct  TrainData
 
struct  TrainResult
 

Typedefs

using DataBuffer = std::vector< char >
 

Enumerations

enum  OpTrainableRank {
  ALL = 0 , ONLY_BIAS = 1 , UP_1_2_PART = 2 , LOWER_1_2_PART = 3 ,
  MAX_VALUE = 4
}
 

Functions

onert_micro::OMStatus findBestTrainableOpIndexes (onert_micro::OMConfig &config, TrainData &train_data, std::unordered_set< uint16_t > &best_trainable_op_indexes)
 
bool cmpTrainResults (const training_configure_tool::TrainResult &left, const training_configure_tool::TrainResult &right, const float acceptable_diff)
 
std::unordered_set< uint16_t > findAllTrainableOps (const char *circle_model_path)
 
std::vector< std::unordered_set< uint16_t > > generateAllPossibleOpIndexesSets (const std::unordered_set< uint16_t > &initial_train_op_indexes)
 
std::vector< std::unordered_set< uint16_t > > selectOpIndexesSetsAccordingToMemoryRestriction (const std::vector< std::unordered_set< uint16_t > > &op_indexes_sets, onert_micro::OMConfig config, training_configure_tool::TrainData train_data)
 
std::vector< std::unordered_map< uint16_t, OpTrainableRank > > findAllTensorsRanksCombinations (const std::unordered_set< uint16_t > &selected_op_indexes, onert_micro::OMConfig config, training_configure_tool::TrainData train_data)
 
onert_micro::OMStatus findBestSparseBackpropagationTensorsRanks (onert_micro::OMConfig &config, TrainData &train_data, const std::unordered_set< uint16_t > &selected_op_indexes, std::unordered_map< uint16_t, OpTrainableRank > &best_train_ranks)
 
void readDataFromFile (const std::string &filename, char *data, size_t data_size, size_t start_position=0)
 
void writeDataToFile (const std::string &filename, const char *data, size_t data_size)
 
DataBuffer readFile (const char *path)
 
onert_micro::OMStatus createResultFile (const TrainConfigFileData &train_data, const char *save_path)
 
onert_micro::OMStatus createResultData (const TrainConfigFileData &train_data, std::vector< char > &result_buffer)
 
onert_micro::OMStatus runTrainProcessWithCurConfig (onert_micro::OMConfig &config, const training_configure_tool::TrainData &train_data, TrainResult &train_result)
 

Typedef Documentation

◆ DataBuffer

using training_configure_tool::DataBuffer = typedef std::vector<char>

Definition at line 30 of file TrainingConfigureFileHandler.h.

Enumeration Type Documentation

◆ OpTrainableRank

Enumerator
ALL 
ONLY_BIAS 
UP_1_2_PART 
LOWER_1_2_PART 
MAX_VALUE 

Definition at line 34 of file TrainConfigData.h.

35{
36 ALL = 0, // 0 - Train all weights in the operation
37 ONLY_BIAS = 1, // 1 - Train bias only in the operation
38 UP_1_2_PART = 2, // 2 - Train the upper 1/2 part of the operation
39 LOWER_1_2_PART = 3, // 3 - Train the lower 1/2 part of the operation
40 MAX_VALUE = 4,
41 // TODO add more
42};

Function Documentation

◆ cmpTrainResults()

bool training_configure_tool::cmpTrainResults ( const training_configure_tool::TrainResult left,
const training_configure_tool::TrainResult right,
const float  acceptable_diff 
)

Definition at line 155 of file SparseBackpropagationHelper.cpp.

158{
159 // Metrics should be the same
160 assert(left.best_metrics_results.first == right.best_metrics_results.first);
161 OMMetrics metric = left.best_metrics_results.first;
162 float left_metric_res = left.best_metrics_results.second;
163 float right_metric_res = right.best_metrics_results.second;
164
165 bool is_in_acceptable_diff = std::abs(left_metric_res - right_metric_res) <= acceptable_diff;
166 if (is_in_acceptable_diff)
167 {
169 }
170
171 switch (metric)
172 {
174 {
175 return left.best_metrics_results.second > right.best_metrics_results.second;
176 }
177 break;
181 {
182
183 return left.best_metrics_results.second < right.best_metrics_results.second;
184 }
185 break;
186 default:
187 assert(false && "Unsupported type");
188 break;
189 }
190 return true;
191}
@ CROSS_ENTROPY_METRICS
Definition OMConfig.h:42
std::pair< onert_micro::OMMetrics, float > best_metrics_results

References onert_micro::ACCURACY, training_configure_tool::TrainResult::best_metrics_results, onert_micro::CROSS_ENTROPY_METRICS, onert_micro::MAE_METRICS, onert_micro::MSE_METRICS, and training_configure_tool::TrainResult::peak_memory_footprint.

Referenced by findBestSparseBackpropagationTensorsRanks(), and findBestTrainableOpIndexes().

◆ createResultData()

onert_micro::OMStatus training_configure_tool::createResultData ( const TrainConfigFileData train_data,
std::vector< char > &  result_buffer 
)

Definition at line 155 of file TrainingConfigureFileHandler.cpp.

157{
158 writeTrainConfigFileDataIntoBuffer(train_data, result_buffer);
159
160 return onert_micro::Ok;
161}

References onert_micro::Ok.

Referenced by findBestSparseBackpropagationTensorsRanks(), findBestTrainableOpIndexes(), and selectOpIndexesSetsAccordingToMemoryRestriction().

◆ createResultFile()

onert_micro::OMStatus training_configure_tool::createResultFile ( const TrainConfigFileData train_data,
const char *  save_path 
)

Definition at line 132 of file TrainingConfigureFileHandler.cpp.

134{
135 std::vector<char> buffer;
136
137 writeTrainConfigFileDataIntoBuffer(train_data, buffer);
138
139 // Open or create file
140 // Note: if the file existed, it will be overwritten
141 std::ofstream out_file(save_path, std::ios::binary | std::ios::trunc);
142 if (not out_file.is_open())
144
145 // Write data
146 out_file.write(buffer.data(), static_cast<long>(buffer.size()));
147
148 // Close file
149 out_file.close();
150
151 return onert_micro::Ok;
152}

References onert_micro::Ok, and onert_micro::UnknownError.

Referenced by entry().

◆ findAllTensorsRanksCombinations()

std::vector< std::unordered_map< uint16_t, training_configure_tool::OpTrainableRank > > training_configure_tool::findAllTensorsRanksCombinations ( const std::unordered_set< uint16_t > &  selected_op_indexes,
onert_micro::OMConfig  config,
training_configure_tool::TrainData  train_data 
)

Definition at line 327 of file SparseBackpropagationHelper.cpp.

330{
331 // 1 - Find pairs: selected ops indexes - divided dim max rank value
332 // 2 - Find for every tensor index every possible rank according to its opcode and size
333 // 3 - Get result
334 std::vector<std::unordered_map<uint16_t, training_configure_tool::OpTrainableRank>> result;
335
338
339 // Init reader
340 OMStatus status = Ok;
342 assert(model_ptr.data() != nullptr);
343 status = reader.parse(model_ptr.data());
344 assert(status == Ok);
345 // return empty set
346 if (status != Ok)
347 return result;
348 // TODO: support multi subgraph models
349 status = reader.select_subgraph(0);
350 // return empty set
351 if (status != Ok)
352 return result;
353
354 // 1 - Find pairs: selected ops indexes - divided dim max rank value
355 std::unordered_map<uint16_t, uint32_t> operator_index_to_tensor_index =
356 findTrainableTensorsMaxDivideRankAccordingToOperatorIndex(selected_op_indexes, reader);
357 assert(operator_index_to_tensor_index.size() == selected_op_indexes.size());
358 // 2 - Find for every tensor index every possible rank according to its opcode and size
359 std::unordered_map<uint16_t, std::unordered_set<uint16_t>> op_index_to_all_possible_ranks;
360 for (auto &p : operator_index_to_tensor_index)
361 {
362 const auto op_index = p.first;
363 const auto max_value = p.second;
364
365 uint16_t cur_value = 2;
366 op_index_to_all_possible_ranks[op_index] = {ALL, ONLY_BIAS};
367 while (cur_value < uint32_t(OpTrainableRank::MAX_VALUE) and cur_value <= max_value)
368 {
369 auto new_value = cur_value * 2;
370 while (cur_value < uint16_t(OpTrainableRank::MAX_VALUE) and cur_value < new_value)
371 {
372 op_index_to_all_possible_ranks[op_index].insert(cur_value);
373 cur_value++;
374 }
375 }
376 }
377 // Get all op indices
378 std::vector<uint16_t> indices(selected_op_indexes.begin(), selected_op_indexes.end());
379 std::vector<std::vector<uint16_t>> rank_combinations;
380 std::vector<uint16_t> cur_v;
381 generateRankCombinations(op_index_to_all_possible_ranks, indices, 0, cur_v, rank_combinations);
382
383 for (const auto &ranks : rank_combinations)
384 {
385 std::unordered_map<uint16_t, OpTrainableRank> combination;
386 for (size_t i = 0; i < indices.size(); ++i)
387 {
388 combination[indices[i]] = OpTrainableRank(ranks[i]);
389 }
390 result.push_back(std::move(combination));
391 }
392
393 return result;
394}
Loads Circle file and provides helpers to access attributes.
OMStatus parse(const char *model_ptr)
OMStatus select_subgraph(uint32_t subgraph)
result
Definition infer.py:103
DataBuffer readFile(const char *path)

References training_configure_tool::TrainData::circle_model_path, onert_micro::Ok, onert_micro::core::reader::OMCircleReader::parse(), readFile(), and onert_micro::core::reader::OMCircleReader::select_subgraph().

Referenced by findBestSparseBackpropagationTensorsRanks().

◆ findAllTrainableOps()

std::unordered_set< uint16_t > training_configure_tool::findAllTrainableOps ( const char *  circle_model_path)

Definition at line 276 of file SparseBackpropagationHelper.cpp.

277{
278 std::unordered_set<uint16_t> result;
279
281 training_configure_tool::readFile(circle_model_path);
282
283 // Init reader
284 OMStatus status = Ok;
286 assert(model_ptr.data() != nullptr);
287 status = reader.parse(model_ptr.data());
288 assert(status == Ok);
289 // return empty set
290 if (status != Ok)
291 return result;
292 // TODO: support multi subgraph models
293 status = reader.select_subgraph(0);
294 // return empty set
295 if (status != Ok)
296 return result;
297
298 // Read ops
299 auto operators = reader.operators();
300 assert(operators != nullptr);
301
302 auto op_size = operators->size();
303
304 // Obtain operation codes
305 auto op_codes = reader.opcodes();
306
307 // Run through all ops
308 for (uint32_t i = 0; i < op_size; ++i)
309 {
310 auto cur_op = operators->operator[](i);
311
312 // Get opcode index
313 uint32_t cur_opcode_index = cur_op->opcode_index();
314 assert(cur_opcode_index < op_codes->size());
315
316 const auto opcode = op_codes->operator[](cur_opcode_index);
317
318 // If op is trainable - insert it
319 if (isTrainableWeights(opcode))
320 result.insert(static_cast<uint16_t>(i));
321 }
322
323 return result;
324}
const CircleOperatorCodes * opcodes() const
const CircleOperators * operators() const
int32_t size[5]
Definition Slice.cpp:35

References onert_micro::Ok, onert_micro::core::reader::OMCircleReader::opcodes(), onert_micro::core::reader::OMCircleReader::operators(), onert_micro::core::reader::OMCircleReader::parse(), readFile(), onert_micro::core::reader::OMCircleReader::select_subgraph(), and size.

Referenced by findBestTrainableOpIndexes().

◆ findBestSparseBackpropagationTensorsRanks()

OMStatus training_configure_tool::findBestSparseBackpropagationTensorsRanks ( onert_micro::OMConfig config,
TrainData train_data,
const std::unordered_set< uint16_t > &  selected_op_indexes,
std::unordered_map< uint16_t, OpTrainableRank > &  best_train_ranks 
)

Definition at line 35 of file TensorRankSparseBackpropagationHandler.cpp.

39{
40 // Clear to find best values
41 best_train_ranks.clear();
42
43 // 1 - Find All combinations with ranks for current selected op indexes
44 // 2 - Run all of them to find best variant
45
46 // 1 - Find All combinations with ranks for current selected op indexes
47 std::vector<std::unordered_map<uint16_t, OpTrainableRank>> all_combinations =
48 findAllTensorsRanksCombinations(selected_op_indexes, config, train_data);
49
50#if PRINT
51 printf("All combinations: op_index : rank_value; { \n");
52 for (const auto &combination : all_combinations)
53 {
54 for (auto &p : combination)
55 {
56 printf("(%d : %d); ", p.first, p.second);
57 }
58 printf("\n");
59 }
60 printf("}\n");
61
62#endif // PRINT
63
64 // 2 - Run all of them to find best variant
65 TrainResult best_train_result(train_data);
66 for (const auto &combination : all_combinations)
67 {
68#if PRINT
69 printf("Current checked combination: op_index : rank_value; { ");
70 for (auto &p : combination)
71 {
72 printf("(%d : %d); ", p.first, p.second);
73 }
74 printf("}\n");
75#endif
76
77 std::vector<char> tmp_buffer;
78 // Create data with current buffer information
79 createResultData({combination}, tmp_buffer);
80 config.training_context.training_config_info_data = tmp_buffer.data();
81
82 TrainResult train_result(train_data);
83 // Run train with this information
84 runTrainProcessWithCurConfig(config, train_data, train_result);
85
86#if PRINT
87 printf("Find the following result:\n");
88 if (train_result.best_metrics_results.first == CROSS_ENTROPY_METRICS)
89 {
90 printf("CROSS_ENTROPY_METRIC = %f\n", train_result.best_metrics_results.second);
91 printf("PEAK_MEMORY_RESULT = %zu\n", train_result.peak_memory_footprint);
92 }
93#endif
94
95 // Compare with best result to find best
96 bool cmp_result = cmpTrainResults(train_result, best_train_result, train_data.acceptable_diff);
97 if (cmp_result)
98 {
99 // Cur rest is better
100#if PRINT
101 printf("BETTER RESULT\n");
102#endif
103 best_train_result = train_result;
104 best_train_ranks = combination;
105 }
106 }
107
108#if PRINT
109 printf("FINISH\n");
110
111 printf("Best rank combination: op_index : rank_value; { ");
112 for (auto &p : best_train_ranks)
113 {
114 printf("(%d : %d); ", p.first, p.second);
115 }
116 printf("}\n");
117
118 printf("Find the following result:\n");
119 if (best_train_result.best_metrics_results.first == CROSS_ENTROPY_METRICS)
120 {
121 printf("CROSS_ENTROPY_METRIC = %f\n", best_train_result.best_metrics_results.second);
122 printf("PEAK_MEMORY_RESULT = %zu\n", best_train_result.peak_memory_footprint);
123 }
124#endif
125
126 return Ok;
127}
onert_micro::OMStatus createResultData(const TrainConfigFileData &train_data, std::vector< char > &result_buffer)
onert_micro::OMStatus runTrainProcessWithCurConfig(onert_micro::OMConfig &config, const training_configure_tool::TrainData &train_data, TrainResult &train_result)
bool cmpTrainResults(const training_configure_tool::TrainResult &left, const training_configure_tool::TrainResult &right, const float acceptable_diff)
std::vector< std::unordered_map< uint16_t, OpTrainableRank > > findAllTensorsRanksCombinations(const std::unordered_set< uint16_t > &selected_op_indexes, onert_micro::OMConfig config, training_configure_tool::TrainData train_data)

References training_configure_tool::TrainData::acceptable_diff, training_configure_tool::TrainResult::best_metrics_results, cmpTrainResults(), createResultData(), onert_micro::CROSS_ENTROPY_METRICS, findAllTensorsRanksCombinations(), onert_micro::Ok, training_configure_tool::TrainResult::peak_memory_footprint, and runTrainProcessWithCurConfig().

Referenced by entry().

◆ findBestTrainableOpIndexes()

OMStatus training_configure_tool::findBestTrainableOpIndexes ( onert_micro::OMConfig config,
training_configure_tool::TrainData train_data,
std::unordered_set< uint16_t > &  best_trainable_op_indexes 
)

Definition at line 30 of file SparseBackpropagationHandler.cpp.

33{
34 // Clear to find best values
35 best_trainable_op_indexes.clear();
36 // 1 - Find all trainable ops indexes in the model - initial_train_op_indexes
37 // 2 - Generate all possible sets from initial_train_op_indexes
38 // 3 - If above memory restriction is defined - then remove operations indexes sets with peak
39 // memory footprint greater then given restriction 4 - Try all found sets to find best metrics
40 // results
41
42 // 1 - Find all trainable ops indexes in the model - initial_train_op_indexes
43 std::unordered_set<uint16_t> initial_train_op_indexes =
45 assert(!initial_train_op_indexes.empty());
46 if (initial_train_op_indexes.empty())
47 return UnknownError;
48#if PRINT
49 printf("Found next trainable indexes in the model: ");
50 for (auto i : initial_train_op_indexes)
51 {
52 printf("%d ", i);
53 }
54 printf("\n");
55#endif
56
57 // 2 - Generate all possible sets from initial_train_op_indexes
58 std::vector<std::unordered_set<uint16_t>> all_possible_train_op_indexes_sets =
60 assert(all_possible_train_op_indexes_sets.empty() == false);
61 if (all_possible_train_op_indexes_sets.empty() == true)
62 return UnknownError;
63#if PRINT
64 printf("Found %zu unique trainable ops indexes in the model:\n",
65 all_possible_train_op_indexes_sets.size());
66 for (const auto &s : all_possible_train_op_indexes_sets)
67 {
68 printf("Op indexes set = { ");
69 for (auto i : s)
70 {
71 printf("%d ", i);
72 }
73 printf("}\n");
74 }
75#endif
76 // Clear initial due to is not needed
77 initial_train_op_indexes.clear();
78
79 // 3 - If above memory restriction is defined, then save only sets with peak memory less then
80 // restriction
81 std::vector<std::unordered_set<uint16_t>> selected_op_indexes_sets =
83 all_possible_train_op_indexes_sets, config, train_data);
84#if PRINT
85 printf("Found %zu selected op indexes sets:\n", selected_op_indexes_sets.size());
86 for (const auto &s : selected_op_indexes_sets)
87 {
88 printf("Op indexes set = { ");
89 for (auto i : s)
90 {
91 printf("%d ", i);
92 }
93 printf("}\n");
94 }
95#endif
96 // Clear not needed object
97 all_possible_train_op_indexes_sets.clear();
98
99 // 4 - Try all found sets to find best metrics results
100 // To save best values
101 TrainResult best_train_result(train_data);
102 for (const auto &index_set : selected_op_indexes_sets)
103 {
104#if PRINT
105 printf("Current checked op indexes set = { ");
106 for (auto i : index_set)
107 {
108 printf("%d ", i);
109 }
110 printf("}\n");
111#endif
112
113 // Construct mapping with current indexes - use default train ALL parts
114 std::unordered_map<uint16_t, OpTrainableRank> train_op_ranks;
115 for (auto index : index_set)
116 {
117 train_op_ranks[index] = ALL;
118 }
119
120 std::vector<char> tmp_buffer;
121 // Create data with current buffer information
122 createResultData({train_op_ranks}, tmp_buffer);
123 config.training_context.training_config_info_data = tmp_buffer.data();
124
125 TrainResult train_result(train_data);
126 // Run train with this information
127 runTrainProcessWithCurConfig(config, train_data, train_result);
128
129#if PRINT
130 printf("Find the following result:\n");
131 if (train_result.best_metrics_results.first == CROSS_ENTROPY_METRICS)
132 {
133 printf("CROSS_ENTROPY_METRIC = %f\n", train_result.best_metrics_results.second);
134 printf("PEAK_MEMORY_RESULT = %zu\n", train_result.peak_memory_footprint);
135 }
136#endif
137
138 // Compare with best result to find best
139 bool cmp_result = cmpTrainResults(train_result, best_train_result, train_data.acceptable_diff);
140 if (cmp_result)
141 {
142 // Cur rest is better
143#if PRINT
144 printf("BETTER RESULT\n");
145#endif
146 best_train_result = train_result;
147 best_trainable_op_indexes = index_set;
148 }
149 }
150
151#if PRINT
152 printf("FINISH\n");
153
154 printf("Best op indexes set = { ");
155 for (auto i : best_trainable_op_indexes)
156 {
157 printf("%d ", i);
158 }
159 printf("}\n");
160
161 printf("Find the following result:\n");
162 if (best_train_result.best_metrics_results.first == CROSS_ENTROPY_METRICS)
163 {
164 printf("CROSS_ENTROPY_METRIC = %f\n", best_train_result.best_metrics_results.second);
165 printf("PEAK_MEMORY_RESULT = %zu\n", best_train_result.peak_memory_footprint);
166 }
167#endif
168
169 return Ok;
170}
loco::GraphInputIndex index(const TFPlaceholder *node)
Definition TFNode.cpp:54
std::vector< std::unordered_set< uint16_t > > generateAllPossibleOpIndexesSets(const std::unordered_set< uint16_t > &initial_train_op_indexes)
std::vector< std::unordered_set< uint16_t > > selectOpIndexesSetsAccordingToMemoryRestriction(const std::vector< std::unordered_set< uint16_t > > &op_indexes_sets, onert_micro::OMConfig config, training_configure_tool::TrainData train_data)
std::unordered_set< uint16_t > findAllTrainableOps(const char *circle_model_path)

References training_configure_tool::TrainData::acceptable_diff, training_configure_tool::TrainResult::best_metrics_results, training_configure_tool::TrainData::circle_model_path, cmpTrainResults(), createResultData(), onert_micro::CROSS_ENTROPY_METRICS, findAllTrainableOps(), generateAllPossibleOpIndexesSets(), onert_micro::Ok, training_configure_tool::TrainResult::peak_memory_footprint, runTrainProcessWithCurConfig(), selectOpIndexesSetsAccordingToMemoryRestriction(), and onert_micro::UnknownError.

Referenced by entry().

◆ generateAllPossibleOpIndexesSets()

std::vector< std::unordered_set< uint16_t > > training_configure_tool::generateAllPossibleOpIndexesSets ( const std::unordered_set< uint16_t > &  initial_train_op_indexes)

Definition at line 261 of file SparseBackpropagationHelper.cpp.

263{
264 std::vector<std::unordered_set<uint16_t>> result;
265 std::unordered_set<uint16_t> cur_set;
266
267 auto begin_it = initial_train_op_indexes.begin();
268 auto end_it = initial_train_op_indexes.end();
269 recursiveGenerateAllPossibleOpIndexesSetsHelper(result, cur_set, begin_it, end_it);
270
271 return result;
272}

Referenced by findBestTrainableOpIndexes().

◆ readDataFromFile()

void training_configure_tool::readDataFromFile ( const std::string &  filename,
char *  data,
size_t  data_size,
size_t  start_position = 0 
)

Definition at line 76 of file TrainingConfigureFileHandler.cpp.

78{
79 std::streampos start = start_position;
80
81 std::ifstream fs(filename, std::ifstream::binary);
82 if (fs.fail())
83 throw std::runtime_error("Cannot open file \"" + filename + "\".\n");
84
85 fs.seekg(start);
86
87 if (fs.read(data, data_size).fail())
88 throw std::runtime_error("Failed to read data from file \"" + filename + "\".\n");
89 fs.close();
90}

Referenced by runTrainProcessWithCurConfig().

◆ readFile()

training_configure_tool::DataBuffer training_configure_tool::readFile ( const char *  path)

Definition at line 104 of file TrainingConfigureFileHandler.cpp.

105{
106 std::ifstream file(path, std::ios::binary | std::ios::in);
107 if (!file.good())
108 {
109 std::string errmsg = "Failed to open file";
110 throw std::runtime_error(errmsg.c_str());
111 }
112
113 file.seekg(0, std::ios::end);
114 auto fileSize = file.tellg();
115 file.seekg(0, std::ios::beg);
116
117 // reserve capacity
118 DataBuffer model_data(fileSize);
119
120 // read the data
121 file.read(model_data.data(), fileSize);
122 if (file.fail())
123 {
124 std::string errmsg = "Failed to read file";
125 throw std::runtime_error(errmsg.c_str());
126 }
127
128 return model_data;
129}
model_data
Definition infer.py:82
std::vector< char > DataBuffer

Referenced by findAllTensorsRanksCombinations(), findAllTrainableOps(), and runTrainProcessWithCurConfig().

◆ runTrainProcessWithCurConfig()

OMStatus training_configure_tool::runTrainProcessWithCurConfig ( onert_micro::OMConfig config,
const training_configure_tool::TrainData train_data,
TrainResult train_result 
)

Definition at line 40 of file TrainingDriverHandler.cpp.

42{
43 // Clear previous results
44 train_result.peak_memory_footprint = 0;
45
49 // If defined wof file
50 if (train_data.wof_file_path != nullptr)
52
53 // Save model size and model ptr in config
54 config.model_size = circle_model.size();
55 config.model_ptr = circle_model.data();
56
57 // If defined wof file
58 if (train_data.wof_file_path != nullptr)
59 config.wof_ptr = nullptr;
60
61 config.train_mode = true;
62
63 // Create training interpreter and import models
64 onert_micro::OMTrainingInterpreter train_interpreter;
65 train_interpreter.importTrainModel(config.model_ptr, config);
66
67 const auto batch_size = config.training_context.batch_size;
68 // TODO: support more inputs
69 const auto input_size = train_interpreter.getInputSizeAt(0);
70 const auto output_size = train_interpreter.getOutputSizeAt(0);
71
72 // Temporary buffer to read input data from file using BATCH_SIZE
73 float training_input[batch_size * input_size];
74 float training_target[batch_size * output_size];
75 // Note: here test size used with BATCH_SIZE = 1
76 float test_input[input_size];
77 float test_target[output_size];
78
79 // Best results
80 float max_accuracy = std::numeric_limits<float>::min();
81 float min_mse = std::numeric_limits<float>::max();
82 float min_mae = std::numeric_limits<float>::max();
83 float min_entropy = std::numeric_limits<float>::max();
84
85 const auto training_epochs = config.training_context.epochs;
86 for (uint32_t e = 0; e < training_epochs; ++e)
87 {
88#if PRINT
89 printf("Epoch: %d/%d\n", e + 1, training_epochs);
90#endif
91 std::vector<float> accuracy_v;
92 std::vector<float> cross_entropy_v;
93 std::vector<float> mse_v;
94 std::vector<float> mae_v;
95
96 // Run train for current epoch
97 config.training_context.num_epoch = e + 1;
98 uint32_t num_steps = train_data.num_train_data_samples / batch_size;
99 for (int i = 0; i < num_steps; ++i)
100 {
101 uint32_t cur_batch_size =
102 std::min(batch_size, train_data.num_train_data_samples - batch_size * i - 1);
103 cur_batch_size = std::max(1u, cur_batch_size);
104
105 config.training_context.batch_size = cur_batch_size;
106
107 // Read current input and target data
109 reinterpret_cast<char *>(training_input),
110 sizeof(float) * input_size * cur_batch_size,
111 i * sizeof(MODEL_TYPE) * input_size * batch_size);
112
114 reinterpret_cast<char *>(training_target),
115 sizeof(float) * output_size * cur_batch_size,
116 i * sizeof(MODEL_TYPE) * output_size * batch_size);
117
118 // Set input and target
119 train_interpreter.setInput(reinterpret_cast<uint8_t *>(training_input), 0);
120 train_interpreter.setTarget(reinterpret_cast<uint8_t *>(training_target), 0);
121
122 // Train with current batch size
123 train_interpreter.trainSingleStep(config);
124 }
125
126 train_interpreter.reset();
127
128 // Reset num step value
129 config.training_context.num_step = 0;
130 num_steps = train_data.num_test_data_samples;
131
132 accuracy_v.clear();
133 cross_entropy_v.clear();
134 mae_v.clear();
135 mse_v.clear();
136
137 if (train_data.metrics_to_check_best_config == NONE)
138 continue;
139
140 for (int i = 0; i < num_steps; ++i)
141 {
142 uint32_t cur_batch_size = 1;
144 train_data.input_input_test_data_path, reinterpret_cast<char *>(test_input),
145 sizeof(float) * input_size * cur_batch_size, i * sizeof(MODEL_TYPE) * input_size);
146
148 train_data.input_target_test_data_path, reinterpret_cast<char *>(test_target),
149 sizeof(float) * output_size * cur_batch_size, i * sizeof(MODEL_TYPE) * output_size);
150
151 train_interpreter.setInput(reinterpret_cast<uint8_t *>(test_input), 0);
152 train_interpreter.setTarget(reinterpret_cast<uint8_t *>(test_target), 0);
153
154 switch (train_data.metrics_to_check_best_config)
155 {
157 {
158 float cross_entropy_metric = 0.f;
159 train_interpreter.evaluateMetric(config, onert_micro::CROSS_ENTROPY_METRICS,
160 reinterpret_cast<void *>(&cross_entropy_metric),
161 cur_batch_size);
162 cross_entropy_v.push_back(cross_entropy_metric);
163 }
164 break;
166 {
167 float accuracy = 0.f;
168 train_interpreter.evaluateMetric(config, onert_micro::ACCURACY,
169 reinterpret_cast<void *>(&accuracy), cur_batch_size);
170 accuracy_v.push_back(accuracy);
171 }
172 break;
174 {
175 float mse = 0.f;
176 train_interpreter.evaluateMetric(config, onert_micro::MSE_METRICS,
177 reinterpret_cast<void *>(&mse), cur_batch_size);
178 mse_v.push_back(mse);
179 }
180 break;
182 {
183 float mae = 0.f;
184 train_interpreter.evaluateMetric(config, onert_micro::MAE_METRICS,
185 reinterpret_cast<void *>(&mae), cur_batch_size);
186 mae_v.push_back(mae);
187 }
188 break;
189 default:
190 {
191 assert(false && "Not supported");
192 return UnsupportedType;
193 }
194 }
195 }
196
197 // Calculate and use average values
198 switch (train_data.metrics_to_check_best_config)
199 {
201 {
202 auto average_value = findAverage(cross_entropy_v);
203 if (average_value < min_entropy)
204 min_entropy = average_value;
205 }
206 break;
208 {
209 auto average_value = findAverage(accuracy_v);
210 if (average_value > max_accuracy)
211 max_accuracy = average_value;
212 }
213 break;
215 {
216 auto average_value = findAverage(mse_v);
217 if (average_value < min_mse)
218 min_mse = average_value;
219 }
220 break;
222 {
223 auto average_value = findAverage(mae_v);
224 if (average_value < min_mae)
225 min_mae = average_value;
226 }
227 break;
228 default:
229 {
230 assert(false && "Not supported");
231 return UnsupportedType;
232 }
233 }
234 }
235 train_result.peak_memory_footprint = train_interpreter.getPeakFootprintMemory();
236 switch (train_data.metrics_to_check_best_config)
237 {
239 {
240 train_result.best_metrics_results = {train_data.metrics_to_check_best_config, min_entropy};
241 }
242 break;
244 {
245 train_result.best_metrics_results = {train_data.metrics_to_check_best_config, max_accuracy};
246 }
247 break;
249 {
250 train_result.best_metrics_results = {train_data.metrics_to_check_best_config, min_mse};
251 }
252 break;
254 {
255 train_result.best_metrics_results = {train_data.metrics_to_check_best_config, min_mae};
256 }
257 break;
259 {
260 break;
261 }
262 default:
263 {
264 assert(false && "Not supported");
265 return UnsupportedType;
266 }
267 }
268 return Ok;
269}
#define MODEL_TYPE
uint32_t getOutputSizeAt(uint32_t position)
void setTarget(uint8_t *data, uint32_t target_index)
OMStatus importTrainModel(char *model_ptr, const OMConfig &config)
void setInput(uint8_t *data, uint32_t input_index)
uint32_t getInputSizeAt(uint32_t position)
OMStatus evaluateMetric(const OMConfig &config, OMMetrics metric, void *metric_val, uint32_t test_size)
OMStatus trainSingleStep(OMConfig &config)
@ UnsupportedType
Definition OMStatus.h:26
void readDataFromFile(const std::string &filename, char *data, size_t data_size, size_t start_position=0)
onert_micro::OMMetrics metrics_to_check_best_config

References onert_micro::ACCURACY, training_configure_tool::TrainResult::best_metrics_results, training_configure_tool::TrainData::circle_model_path, onert_micro::CROSS_ENTROPY_METRICS, onert_micro::OMTrainingInterpreter::evaluateMetric(), onert_micro::OMTrainingInterpreter::getInputSizeAt(), onert_micro::OMTrainingInterpreter::getOutputSizeAt(), onert_micro::OMTrainingInterpreter::importTrainModel(), training_configure_tool::TrainData::input_input_test_data_path, training_configure_tool::TrainData::input_input_train_data_path, training_configure_tool::TrainData::input_target_test_data_path, training_configure_tool::TrainData::input_target_train_data_path, onert_micro::MAE_METRICS, training_configure_tool::TrainData::metrics_to_check_best_config, MODEL_TYPE, onert_micro::MSE_METRICS, onert_micro::NONE, training_configure_tool::TrainData::num_test_data_samples, training_configure_tool::TrainData::num_train_data_samples, onert_micro::Ok, training_configure_tool::TrainResult::peak_memory_footprint, readDataFromFile(), readFile(), onert_micro::OMTrainingInterpreter::reset(), onert_micro::OMTrainingInterpreter::setInput(), onert_micro::OMTrainingInterpreter::setTarget(), onert_micro::OMTrainingInterpreter::trainSingleStep(), onert_micro::UnsupportedType, and training_configure_tool::TrainData::wof_file_path.

Referenced by findBestSparseBackpropagationTensorsRanks(), findBestTrainableOpIndexes(), and selectOpIndexesSetsAccordingToMemoryRestriction().

◆ selectOpIndexesSetsAccordingToMemoryRestriction()

std::vector< std::unordered_set< uint16_t > > training_configure_tool::selectOpIndexesSetsAccordingToMemoryRestriction ( const std::vector< std::unordered_set< uint16_t > > &  op_indexes_sets,
onert_micro::OMConfig  config,
training_configure_tool::TrainData  train_data 
)

Definition at line 198 of file SparseBackpropagationHelper.cpp.

201{
202 // It 0 - then is not set
203 if (train_data.memory_above_restriction == 0)
204 {
205 return op_indexes_sets;
206 }
207
208 std::vector<std::unordered_set<uint16_t>> result;
209
210 // To obtain real estimation we need minimum batch_size = 2 and num_train_data_samples = 4
211 // Change config train and test sample values
212 train_data.num_test_data_samples = 0;
213 train_data.num_train_data_samples = std::min(4, train_data.num_train_data_samples);
214 // To disable tests
216 // Set number of the epochs and batch size to one
217 config.training_context.epochs = 1;
218 config.training_context.batch_size = std::min(2u, config.training_context.batch_size);
219
220 for (const auto &op_indexes_set : op_indexes_sets)
221 {
222#if PRINT
223 printf("Start checking: { ");
224 for (auto i : op_indexes_set)
225 {
226 printf("%d ", i);
227 }
228 printf("}\n");
229#endif
230 // Construct mapping with current indexes - use default train ALL parts
231 std::unordered_map<uint16_t, OpTrainableRank> train_op_ranks;
232 for (auto index : op_indexes_set)
233 {
234 train_op_ranks[index] = ALL;
235 }
236
237 std::vector<char> tmp_buffer;
238 // Create data with current buffer information
239 createResultData({train_op_ranks}, tmp_buffer);
240 config.training_context.training_config_info_data = tmp_buffer.data();
241
242 TrainResult train_result;
243 // Run train with this information
244 runTrainProcessWithCurConfig(config, train_data, train_result);
245#if PRINT
246 printf("CURRENT MEMORY PEAK = %zu\n", train_result.peak_memory_footprint);
247#endif
248 if (train_result.peak_memory_footprint < train_data.memory_above_restriction)
249 {
250#if PRINT
251 printf("Added to the result\n");
252#endif
253 result.push_back(op_indexes_set);
254 }
255 }
256
257 return result;
258}

References createResultData(), training_configure_tool::TrainData::memory_above_restriction, training_configure_tool::TrainData::metrics_to_check_best_config, onert_micro::NONE, training_configure_tool::TrainData::num_test_data_samples, training_configure_tool::TrainData::num_train_data_samples, training_configure_tool::TrainResult::peak_memory_footprint, and runTrainProcessWithCurConfig().

Referenced by findBestTrainableOpIndexes().

◆ writeDataToFile()

void training_configure_tool::writeDataToFile ( const std::string &  filename,
const char *  data,
size_t  data_size 
)

Definition at line 92 of file TrainingConfigureFileHandler.cpp.

94{
95 std::ofstream fs(filename, std::ofstream::binary);
96 if (fs.fail())
97 throw std::runtime_error("Cannot open file \"" + filename + "\".\n");
98 if (fs.write(data, data_size).fail())
99 {
100 throw std::runtime_error("Failed to write data to file \"" + filename + "\".\n");
101 }
102}