-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathProjMgrMlops.cpp
More file actions
284 lines (252 loc) · 10.3 KB
/
ProjMgrMlops.cpp
File metadata and controls
284 lines (252 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright (c) 2026 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "ProjMgrMlops.h"
#include "ProjMgrParser.h"
#include "ProjMgrUtils.h"
using namespace std;
ProjMgrMlops::ProjMgrMlops(ProjMgrWorker* worker) : m_worker(worker) {
// Reserved
}
ProjMgrMlops::~ProjMgrMlops(void) {
// Reserved
}
bool ProjMgrMlops::FindTargetType(const CsolutionItem& csolution, const string& typeName, TargetType& targetType) const {
for (const auto& [name, type] : csolution.targetTypes) {
if (name == typeName) {
targetType = type;
return true;
}
}
return false;
}
bool ProjMgrMlops::GetTargetSetItemRef(const TargetType& targetType, const string& targetTypeName,
const string& targetSetName, bool simulatorDefault, TargetSetItem& targetSet) const {
if (targetSetName.empty()) {
if (simulatorDefault) {
for (const auto& set : targetType.targetSet) {
if (set.debugger.name == "Arm-FVP") {
targetSet = set;
return true;
}
}
return false;
}
if (!targetType.targetSet.empty()) {
targetSet = targetType.targetSet.front();
return true;
}
} else {
for (const auto& set : targetType.targetSet) {
if (set.set == targetSetName) {
targetSet = set;
return true;
}
}
}
return false;
}
string ProjMgrMlops::BuildActive(const string& targetType, const string& targetSet) const {
return targetSet.empty() ? targetType : targetType + "@" + targetSet;
}
string ProjMgrMlops::GetCustomScalar(const CustomItem& custom, const string& key) const {
for (const auto& [customKey, value] : custom.map) {
if (customKey == key) {
return value.scalar;
}
}
return RteUtils::EMPTY_STRING;
}
string ProjMgrMlops::BuildVelaOptions(const MlopsNpuType& npu, const MlopsVelaItem& vela) const {
string options;
if (!npu.type.empty() && !npu.macs.empty()) {
options += "--accelerator-config " + RteUtils::ToLower(npu.type) + "-" + npu.macs;
}
if (!vela.system.empty()) {
options += (options.empty() ? "" : " ") + string("--system-config ") + vela.system;
}
if (!vela.memory.empty()) {
options += (options.empty() ? "" : " ") + string("--memory-mode ") + vela.memory;
}
if (!vela.misc.empty()) {
options += (options.empty() ? "" : " ") + vela.misc;
}
return options;
}
bool ProjMgrMlops::ResolveMlopsPath(string& path, const string& solutionDir,
bool hardwareFound, ContextItem& hardwareContext) const {
if (hardwareFound && !m_worker->ProcessSequenceRelative(hardwareContext, path, solutionDir, false)) {
return false;
}
if (ProjMgrUtils::HasAccessSequence(path)) {
path.clear();
} else if (RteFsUtils::IsRelative(path)) {
const auto& baseDir = hardwareFound ? hardwareContext.directories.cprj : solutionDir;
RteFsUtils::NormalizePath(path, baseDir);
}
return true;
}
void ProjMgrMlops::SetMlopsRunType(MlopsRunType& run, const string& targetType, const string& targetSet,
const vector<ContextItem>& contexts, const string& outBaseDir, const string& solutionName) const {
run.active = BuildActive(targetType, targetSet);
run.cbuildRun = outBaseDir + '/' + solutionName + '+' + targetType + ".cbuild-run.yml";
for (const auto& context : contexts) {
if (context.outputTypes.elf.on) {
MlopsOutputType output;
output.file = context.directories.cprj + '/' + context.directories.outdir + '/' + context.outputTypes.elf.filename;
output.type = RteConstants::OUTPUT_TYPE_ELF;
run.output.push_back(output);
}
}
}
bool ProjMgrMlops::CollectSettings(const CsolutionItem& csolution, MlopsType& mlops) {
mlops = {};
const auto& solutionMlops = csolution.mlops;
// hardware and simulator target types
const string hardwareType = !solutionMlops.hardware.targetType.empty() ? solutionMlops.hardware.targetType :
(!csolution.targetTypes.empty() ? csolution.targetTypes.front().first : RteUtils::EMPTY_STRING);
const string simulatorType = !solutionMlops.simulator.targetType.empty() ? solutionMlops.simulator.targetType :
(!csolution.targetTypes.empty() ? csolution.targetTypes.back().first : RteUtils::EMPTY_STRING);
// get hardware set
TargetType hardwareTargetType;
TargetSetItem hardwareTargetSet;
if (FindTargetType(csolution, hardwareType, hardwareTargetType)) {
GetTargetSetItemRef(hardwareTargetType, hardwareType, solutionMlops.hardware.targetSet, false, hardwareTargetSet);
}
// get simulator set
TargetType simulatorTargetType;
TargetSetItem simulatorTargetSet;
if (FindTargetType(csolution, simulatorType, simulatorTargetType)) {
GetTargetSetItemRef(simulatorTargetType, simulatorType, solutionMlops.simulator.targetSet, true, simulatorTargetSet);
}
// get all context items
map<string, ContextItem>* contexts = nullptr;
m_worker->GetContexts(contexts);
// find hardware and simulator contexts
vector<ContextItem> hardwareContexts, simulatorContexts;
StrSet pnames;
vector<tuple<const TargetSetItem&, const string&, vector<ContextItem>&>> refs = {
{hardwareTargetSet, hardwareType, hardwareContexts}, {simulatorTargetSet, simulatorType, simulatorContexts}};
for (auto& [targetSet, targetType, ref] : refs) {
for (const auto& image : targetSet.images) {
if (!image.context.empty()) {
const string contextName = image.context + "+" + targetType;
if (contexts->find(contextName) != contexts->end()) {
// process context precedences if needed
auto& context = contexts->at(contextName);
if (!context.precedences) {
if (!m_worker->ParseContextLayers(context) || !m_worker->LoadPacks(context) ||
!m_worker->ProcessPrecedences(context, BoardOrDevice::Both) ||
!m_worker->SetTargetAttributes(context, context.targetAttributes)) {
return false;
}
m_worker->CollectNpuInfo(context);
}
ref.push_back(context);
pnames.insert(context.deviceItem.pname);
}
}
}
}
// check if hardware and simulator contexts were found
bool hardwareFound = !hardwareContexts.empty();
bool simulatorFound = !simulatorContexts.empty();
ContextItem defaultContext;
ContextItem& hardwareContext = hardwareFound ? hardwareContexts.front() : defaultContext;
ContextItem& simulatorContext = simulatorFound ? simulatorContexts.front() : defaultContext;
// mlops description
mlops.description = solutionMlops.description;
// get hardware processor type ("Dcore")
if (hardwareContext.targetAttributes.find("Dcore") != hardwareContext.targetAttributes.end()) {
mlops.processor.type = hardwareContext.targetAttributes.at("Dcore");
}
// npu type and macs
mlops.npu.type = solutionMlops.npu.type;
mlops.npu.macs = solutionMlops.npu.macs;
// filter npu info items
vector<NpuInfoItem> npuInfoItems;
for (NpuInfoItem npu : hardwareContext.npuInfoItems) {
if (pnames.find(npu.pname) != pnames.end()) {
npu.macs = RteUtils::StripSuffix(npu.macs, "MACs");
npuInfoItems.push_back(npu);
}
}
if (!npuInfoItems.empty()) {
if (mlops.npu.type.empty() && mlops.npu.macs.empty()) {
// default npu type and macs
mlops.npu.type = npuInfoItems.front().type;
mlops.npu.macs = npuInfoItems.front().macs;
} else if (!mlops.npu.type.empty() && mlops.npu.macs.empty()) {
// explicit type, find matching macs
for (const auto& npu : npuInfoItems) {
if (npu.type == mlops.npu.type) {
mlops.npu.macs = npu.macs;
break;
}
}
} else if (mlops.npu.type.empty() && !mlops.npu.macs.empty()) {
// explicit macs, find matching type
for (const auto& npu : npuInfoItems) {
if (RteUtils::StringToULL(npu.macs) == RteUtils::StringToULL(mlops.npu.macs)) {
mlops.npu.type = npu.type;
break;
}
}
}
}
// vela options
mlops.vela.options = BuildVelaOptions(mlops.npu, solutionMlops.vela);
// vela ini
if (solutionMlops.vela.ini.empty()) {
// default vela ini from DFP for matching NPU
for (const auto& npu : npuInfoItems) {
if (npu.type == mlops.npu.type &&
RteUtils::StringToULL(npu.macs) == RteUtils::StringToULL(mlops.npu.macs)) {
mlops.vela.ini = fs::path(npu.velaAbsolutePath).filename().generic_string();
RteFsUtils::NormalizePath(mlops.vela.ini, csolution.directory + "/.cmsis/");
// copy file to .cmsis if it does not exist yet
if (!RteFsUtils::Exists(mlops.vela.ini)) {
RteFsUtils::CopyCheckFile(npu.velaAbsolutePath, mlops.vela.ini, false);
}
}
}
} else {
// explicit vela ini
mlops.vela.ini = solutionMlops.vela.ini;
if (!ResolveMlopsPath(mlops.vela.ini, csolution.directory, hardwareFound, hardwareContext)) {
return false;
}
}
// model name and clayer
if (!solutionMlops.model.clayer.empty()) {
mlops.model.name = solutionMlops.model.name.empty() ? "Algorithm" : solutionMlops.model.name;
mlops.model.clayer = solutionMlops.model.clayer;
if (!ResolveMlopsPath(mlops.model.clayer, csolution.directory, hardwareFound, hardwareContext)) {
return false;
}
}
if (hardwareFound) {
// set hardware run types
const string outBaseDir = hardwareContext.directories.cprj + "/" + hardwareContext.directories.outBaseDir;
SetMlopsRunType(mlops.hardware, hardwareType, hardwareTargetSet.set, hardwareContexts, outBaseDir, csolution.name);
}
if (simulatorFound) {
// set simulator run types
const string outBaseDir = simulatorContext.directories.cprj + "/" + simulatorContext.directories.outBaseDir;
SetMlopsRunType(mlops.simulator, simulatorType, simulatorTargetSet.set, simulatorContexts, outBaseDir, csolution.name);
// get debugger model and config-file
mlops.simulator.model = GetCustomScalar(simulatorTargetSet.debugger.custom, "model");
mlops.simulator.configFile = GetCustomScalar(simulatorTargetSet.debugger.custom, "config-file");
if (!mlops.simulator.configFile.empty()) {
if (!m_worker->ProcessSequenceRelative(simulatorContext, mlops.simulator.configFile, csolution.directory, false)) {
return false;
}
if (RteFsUtils::IsRelative(mlops.simulator.configFile)) {
RteFsUtils::NormalizePath(mlops.simulator.configFile, simulatorContext.directories.cprj);
}
}
}
return true;
}