-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathCommandLineArgs.cpp
More file actions
451 lines (426 loc) · 17 KB
/
CommandLineArgs.cpp
File metadata and controls
451 lines (426 loc) · 17 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include <Windows.h>
#include <string>
#include <iostream>
#include "CommandLineArgs.h"
#include <ctime>
#include <iomanip>
#include <filesystem>
#include "Filehelper.h"
using namespace Windows::AI::MachineLearning;
void CommandLineArgs::PrintUsage()
{
std::cout << "WinML Runner" << std::endl;
std::cout << " ---------------------------------------------------------------" << std::endl;
std::cout << "WinmlRunner.exe <-model | -folder> <fully qualified path> [options]" << std::endl;
std::cout << std::endl;
std::cout << "options: " << std::endl;
std::cout << " -version: prints the version information for this build of WinMLRunner.exe" << std::endl;
std::cout << " -CPU : run model on default CPU" << std::endl;
std::cout << " -GPU : run model on default GPU" << std::endl;
std::cout << " -GPUHighPerformance : run model on GPU with highest performance" << std::endl;
std::cout << " -GPUMinPower : run model on GPU with the least power" << std::endl;
std::cout << " -GPUAdapterName <adapter name substring>: run model on GPU specified by its name." << std::endl;
std::cout << " -CreateDeviceOnClient : create the D3D device on the client and pass it to WinML to create session" << std::endl;
std::cout << " -CreateDeviceInWinML : create the device inside WinML" << std::endl;
std::cout << " -CPUBoundInput : bind the input to the CPU" << std::endl;
std::cout << " -GPUBoundInput : bind the input to the GPU" << std::endl;
std::cout << " -RGB : load the input as an RGB image" << std::endl;
std::cout << " -BGR : load the input as a BGR image" << std::endl;
std::cout << " -Tensor : load the input as a tensor" << std::endl;
std::cout << " -Perf [all]: capture performance measurements such as timing and memory usage. Specifying \"all\" "
"will output all measurements"
<< std::endl;
std::cout << " -Iterations : # times perf measurements will be run/averaged. (maximum: 1024 times)" << std::endl;
std::cout << " -Input <path to input file>: binds image or CSV to model" << std::endl;
std::cout << " -InputImageFolder <path to directory of images> : specify folder of images to bind to model" << std::endl;
std::cout << " -TopK <number> : print top <number> values in the result. Default to 1" << std::endl;
std::cout << " -BaseOutputPath [<fully qualified path>] : base output directory path for results, default to cwd"
<< std::endl;
std::cout << " -PerfOutput [<path>] : fully qualified or relative path including csv filename for perf results"
<< std::endl;
std::cout << " -SavePerIterationPerf : save per iteration performance results to csv file" << std::endl;
std::cout << " -PerIterationPath <directory_path> : Relative or fully qualified path for per iteration and save "
"tensor output results. If not specified a default(timestamped) folder will be created."
<< std::endl;
std::cout << " -SaveTensorData <saveMode>: saveMode: save first iteration or all iteration output "
"tensor results to csv file [First, All]"
<< std::endl;
std::cout << " -DebugEvaluate: Print evaluation debug output to debug console if debugger is present."
<< std::endl;
std::cout << " -Terse: Terse Mode (suppresses repetitive console output)" << std::endl;
std::cout << " -AutoScale <interpolationMode>: Enable image autoscaling and set the interpolation mode [Nearest, "
"Linear, Cubic, Fant]"
<< std::endl;
std::cout << std::endl;
std::cout << "Concurrency Options:" << std::endl;
std::cout << " -ConcurrentLoad: load models concurrently" << std::endl;
std::cout << " -NumThreads <number>: number of threads to load a model. By default this will be the number of "
"model files to be executed"
<< std::endl;
std::cout << " -ThreadInterval <milliseconds>: interval time between two thread creations in milliseconds"
<< std::endl;
}
void CheckAPICall(int return_value)
{
if (return_value == 0)
{
auto code = GetLastError();
std::wstring msg = L"failed to get the version of this file with error code: ";
msg += std::to_wstring(code);
throw hresult_invalid_argument(msg.c_str());
}
}
CommandLineArgs::CommandLineArgs(const std::vector<std::wstring>& args)
{
std::wstring sPerfOutputPath;
std::wstring sBaseOutputPath;
std::wstring sPerIterationDataPath;
for (UINT i = 0; i < args.size(); i++)
{
if ((_wcsicmp(args[i].c_str(), L"-CPU") == 0))
{
m_useCPU = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-GPU") == 0))
{
m_useGPU = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-GPUHighPerformance") == 0))
{
m_useGPUHighPerformance = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-GPUMinPower") == 0))
{
m_useGPUMinPower = true;
}
else if (_wcsicmp(args[i].c_str(), L"-GPUAdapterName") == 0)
{
CheckNextArgument(args, i);
#ifdef DXCORE_SUPPORTED_BUILD
HMODULE library = nullptr;
library = LoadLibrary(L"dxcore.dll");
if (!library)
{
throw hresult_invalid_argument(
L"ERROR: DXCORE isn't supported on this machine. "
L"GpuAdapterName flag should only be used with DXCore supported machines.");
}
#endif
m_adapterName = args[++i];
m_useGPU = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-CreateDeviceOnClient") == 0))
{
m_createDeviceOnClient = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-CreateDeviceInWinML") == 0))
{
m_createDeviceInWinML = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-Iterations") == 0) && (i + 1 < args.size()))
{
m_numIterations = static_cast<UINT>(_wtoi(args[++i].c_str()));
}
else if ((_wcsicmp(args[i].c_str(), L"-Model") == 0))
{
CheckNextArgument(args, i);
m_modelPath = args[++i];
}
else if ((_wcsicmp(args[i].c_str(), L"-Folder") == 0))
{
CheckNextArgument(args, i);
m_modelFolderPath = args[++i];
}
else if ((_wcsicmp(args[i].c_str(), L"-Input") == 0))
{
CheckNextArgument(args, i);
m_inputData = FileHelper::GetAbsolutePath(args[++i]);
}
else if ((_wcsicmp(args[i].c_str(), L"-InputImageFolder") == 0))
{
CheckNextArgument(args, i);
m_inputImageFolderPath = FileHelper::GetAbsolutePath(args[++i]);
}
else if ((_wcsicmp(args[i].c_str(), L"-PerfOutput") == 0))
{
if (i + 1 < args.size() && args[i + 1][0] != L'-')
{
sPerfOutputPath = args[++i];
}
m_perfOutput = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-RGB") == 0))
{
m_useRGB = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-BGR") == 0))
{
m_useBGR = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-Tensor") == 0))
{
m_useTensor = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-CPUBoundInput") == 0))
{
m_useCPUBoundInput = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-GPUBoundInput") == 0))
{
m_useGPUBoundInput = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-Perf") == 0))
{
if (i + 1 < args.size() && args[i + 1][0] != L'-' && (_wcsicmp(args[i + 1].c_str(), L"all") == 0))
{
m_perfConsoleOutputAll = true;
i++;
}
m_perfCapture = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-DebugEvaluate") == 0))
{
if (!IsDebuggerPresent())
{
throw hresult_invalid_argument(
L"-DebugEvaluate flag should only be used when WinMLRunner is under a user-mode debugger!");
}
ToggleEvaluationDebugOutput(true);
}
else if ((_wcsicmp(args[i].c_str(), L"-SavePerIterationPerf") == 0))
{
m_perIterCapture = true;
}
else if (_wcsicmp(args[i].c_str(), L"-BaseOutputPath") == 0)
{
CheckNextArgument(args, i);
sBaseOutputPath = args[++i].c_str();
}
else if (_wcsicmp(args[i].c_str(), L"-PerIterationPath") == 0)
{
CheckNextArgument(args, i);
sPerIterationDataPath = args[++i].c_str();
}
else if ((_wcsicmp(args[i].c_str(), L"-Terse") == 0))
{
m_terseOutput = true;
}
else if ((_wcsicmp(args[i].c_str(), L"-AutoScale") == 0))
{
CheckNextArgument(args, i);
m_autoScale = true;
if (_wcsicmp(args[++i].c_str(), L"Nearest") == 0)
{
m_autoScaleInterpMode = BitmapInterpolationMode::NearestNeighbor;
}
else if (_wcsicmp(args[i].c_str(), L"Linear") == 0)
{
m_autoScaleInterpMode = BitmapInterpolationMode::Linear;
}
else if (_wcsicmp(args[i].c_str(), L"Cubic") == 0)
{
m_autoScaleInterpMode = BitmapInterpolationMode::Cubic;
}
else if (_wcsicmp(args[i].c_str(), L"Fant") == 0)
{
m_autoScaleInterpMode = BitmapInterpolationMode::Fant;
}
else
{
PrintUsage();
throw hresult_invalid_argument(L"Unknown AutoScale Interpolation Mode!");
}
}
else if (_wcsicmp(args[i].c_str(), L"-SaveTensorData") == 0)
{
CheckNextArgument(args, i);
m_saveTensor = true;
if (_wcsicmp(args[++i].c_str(), L"First") == 0)
{
m_saveTensorMode = L"First";
}
else if (_wcsicmp(args[i].c_str(), L"All") == 0)
{
m_saveTensorMode = L"All";
}
else
{
PrintUsage();
throw hresult_invalid_argument(L"Unknown SaveTensorData Mode[" + m_saveTensorMode + L"]!");
}
}
else if (_wcsicmp(args[i].c_str(), L"-version") == 0)
{
TCHAR szExeFileName[MAX_PATH];
auto ret = GetModuleFileName(NULL, szExeFileName, MAX_PATH);
CheckAPICall(ret);
uint32_t versionInfoSize = GetFileVersionInfoSize(szExeFileName, 0);
wchar_t* pVersionData = new wchar_t[versionInfoSize / sizeof(wchar_t)];
CheckAPICall(GetFileVersionInfo(szExeFileName, 0, versionInfoSize, pVersionData));
wchar_t* pOriginalFilename;
uint32_t originalFilenameSize;
CheckAPICall(VerQueryValue(pVersionData, L"\\StringFileInfo\\040904b0\\OriginalFilename",
(void**)&pOriginalFilename, &originalFilenameSize));
wchar_t* pProductVersion;
uint32_t productVersionSize;
CheckAPICall(VerQueryValue(pVersionData, L"\\StringFileInfo\\040904b0\\ProductVersion",
(void**)&pProductVersion, &productVersionSize));
wchar_t* pFileVersion;
uint32_t fileVersionSize;
CheckAPICall(VerQueryValue(pVersionData, L"\\StringFileInfo\\040904b0\\FileVersion", (void**)&pFileVersion,
&fileVersionSize));
std::wcout << pOriginalFilename << std::endl;
std::wcout << L"Version: " << pFileVersion << "." << pProductVersion << std::endl;
delete[] pVersionData;
}
else if ((_wcsicmp(args[i].c_str(), L"/?") == 0))
{
PrintUsage();
return;
}
// concurrency options
else if ((_wcsicmp(args[i].c_str(), L"-ConcurrentLoad") == 0))
{
ToggleConcurrentLoad(true);
}
else if ((_wcsicmp(args[i].c_str(), L"-NumThreads") == 0))
{
CheckNextArgument(args, i);
unsigned num_threads = std::stoi(args[++i].c_str());
SetNumThreads(num_threads);
}
else if ((_wcsicmp(args[i].c_str(), L"-ThreadInterval") == 0))
{
CheckNextArgument(args, i);
unsigned thread_interval = std::stoi(args[++i].c_str());
SetThreadInterval(thread_interval);
}
else if ((_wcsicmp(args[i].c_str(), L"-TopK") == 0))
{
CheckNextArgument(args, i);
SetTopK(std::stoi(args[++i].c_str()));
}
else
{
std::wstring msg = L"Unknown option ";
msg += args[i].c_str();
throw hresult_invalid_argument(msg.c_str());
}
}
if (m_modelPath.empty() && m_modelFolderPath.empty())
{
std::cout << std::endl;
PrintUsage();
return;
}
if (!m_inputData.empty())
{
std::transform(m_inputData.begin(), m_inputData.end(), m_inputData.begin(), ::towlower);
if (m_inputData.find(L".png") != std::string::npos || m_inputData.find(L".jpg") != std::string::npos ||
m_inputData.find(L".jpeg") != std::string::npos)
{
m_imagePaths.push_back(m_inputData);
}
else if (m_inputData.find(L".csv") != std::string::npos)
{
m_csvData = m_inputData;
}
else
{
std::wstring msg = L"unknown input type ";
msg += m_inputData;
throw hresult_invalid_argument(msg.c_str());
}
}
if (!m_inputImageFolderPath.empty())
{
PopulateInputImagePaths();
}
SetupOutputDirectories(sBaseOutputPath, sPerfOutputPath, sPerIterationDataPath);
CheckForInvalidArguments();
}
void CommandLineArgs::PopulateInputImagePaths()
{
for (auto& it : std::filesystem::directory_iterator(m_inputImageFolderPath))
{
std::string path = it.path().string();
if (it.path().string().find(".png") != std::string::npos ||
it.path().string().find(".jpg") != std::string::npos ||
it.path().string().find(".jpeg") != std::string::npos)
{
std::wstring fileName;
fileName.assign(path.begin(), path.end());
m_imagePaths.push_back(fileName);
}
}
}
void CommandLineArgs::SetupOutputDirectories(const std::wstring& sBaseOutputPath,
const std::wstring& sPerfOutputPath,
const std::wstring& sPerIterationDataPath)
{
std::filesystem::path PerfOutputPath(sPerfOutputPath);
std::filesystem::path BaseOutputPath(sBaseOutputPath);
std::filesystem::path PerIterationDataPath(sPerIterationDataPath);
if (PerfOutputPath.is_absolute())
{
m_perfOutputPath = PerfOutputPath.c_str();
if (BaseOutputPath.empty())
{
BaseOutputPath = PerfOutputPath.remove_filename();
}
}
if (PerIterationDataPath.is_absolute())
{
m_perIterationDataPath = PerIterationDataPath.c_str();
if (BaseOutputPath.empty())
{
BaseOutputPath = PerIterationDataPath;
}
}
if (m_perfOutputPath.empty() || m_perIterationDataPath.empty())
{
auto time = std::time(nullptr);
struct tm localTime;
localtime_s(&localTime, &time);
std::wostringstream oss;
oss << std::put_time(&localTime, L"%Y-%m-%d_%H.%M.%S");
if (BaseOutputPath.empty())
{
BaseOutputPath = std::filesystem::current_path();
}
if (m_perfOutputPath.empty())
{
if (sPerfOutputPath.empty())
PerfOutputPath = L"WinMLRunner[" + oss.str() + L"].csv";
PerfOutputPath = BaseOutputPath / PerfOutputPath;
m_perfOutputPath = PerfOutputPath.c_str();
}
if (m_perIterationDataPath.empty())
{
if (sPerIterationDataPath.empty())
PerIterationDataPath = L"PerIterationRun[" + oss.str() + L"]";
PerIterationDataPath = BaseOutputPath / PerIterationDataPath;
m_perIterationDataPath = PerIterationDataPath.c_str();
}
}
}
void CommandLineArgs::CheckNextArgument(const std::vector<std::wstring>& args, UINT i)
{
if (i + 1 >= args.size() || args[i + 1][0] == L'-')
{
std::wstring msg = L"Invalid parameter for ";
msg += args[i].c_str();
throw hresult_invalid_argument(msg.c_str());
}
}
void CommandLineArgs::CheckForInvalidArguments()
{
if (IsGarbageInput() && IsSaveTensor())
{
throw hresult_invalid_argument(L"Cannot save tensor output if no input data is provided!");
}
if (m_imagePaths.size() > 1 && IsSaveTensor())
{
throw hresult_not_implemented(L"Saving tensor output for multiple images isn't implemented.");
}
}