forked from dotnet/machinelearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigRunner.cs
More file actions
274 lines (232 loc) · 11.4 KB
/
Copy pathConfigRunner.cs
File metadata and controls
274 lines (232 loc) · 11.4 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.ML;
using Microsoft.ML.CommandLine;
using Microsoft.ML.Internal.Utilities;
using Microsoft.ML.Sweeper;
using ResultProcessorInternal = Microsoft.ML.Internal.Internallearn.ResultProcessor;
[assembly: LoadableClass(typeof(LocalExeConfigRunner), typeof(LocalExeConfigRunner.Arguments), typeof(SignatureConfigRunner),
"Local Sweep Config Runner", "Local")]
namespace Microsoft.ML.Sweeper
{
public delegate void SignatureConfigRunner();
public interface IConfigRunner
{
IEnumerable<IRunResult> RunConfigs(ParameterSet[] sweeps, int min);
void Finish();
string GetOutputFolderPath(string folderName);
}
public abstract class ExeConfigRunnerBase : IConfigRunner
{
public abstract class ArgumentsBase
{
[Argument(ArgumentType.AtMostOnce, HelpText = "Command pattern for the sweeps", ShortName = "pattern")]
public string ArgsPattern;
[Argument(ArgumentType.AtMostOnce, HelpText = "output folder for the outputs of the sweeps", ShortName = "outfolder")]
public string OutputFolderName;
[Argument(ArgumentType.AtMostOnce, HelpText = "prefix to add to the output file names", ShortName = "pre")]
public string Prefix;
[Argument(ArgumentType.AtMostOnce, HelpText = "The executable name, including the path (the default is MAML.exe)")]
public string Exe;
[Argument(ArgumentType.Multiple, HelpText = "Specify how to extract the metrics from the result file.", ShortName = "ev", SignatureType = typeof(SignatureSweepResultEvaluator))]
public IComponentFactory<ISweepResultEvaluator<string>> ResultProcessor = ComponentFactoryUtils.CreateFromFunction(
env => new InternalSweepResultEvaluator(env, new InternalSweepResultEvaluator.Arguments()));
[Argument(ArgumentType.AtMostOnce, Hide = true)]
public bool CalledFromUnitTestSuite;
}
protected string Exe;
protected readonly string ArgsPattern;
protected readonly string OutputFolder;
protected readonly string Prefix;
protected readonly ISweepResultEvaluator<string> ResultProcessor;
protected readonly List<int> RunNums;
protected readonly IHost Host;
private readonly bool _calledFromUnitTestSuite;
protected ExeConfigRunnerBase(ArgumentsBase args, IHostEnvironment env, string registrationName)
{
Contracts.AssertValue(env);
Host = env.Register(registrationName);
Host.CheckUserArg(!string.IsNullOrEmpty(args.ArgsPattern), nameof(args.ArgsPattern), "The command pattern is missing");
Host.CheckUserArg(!string.IsNullOrEmpty(args.OutputFolderName), nameof(args.OutputFolderName), "Please specify an output folder");
ArgsPattern = args.ArgsPattern;
OutputFolder = GetOutputFolderPath(args.OutputFolderName);
Prefix = string.IsNullOrEmpty(args.Prefix) ? "" : args.Prefix;
ResultProcessor = args.ResultProcessor.CreateComponent(Host);
_calledFromUnitTestSuite = args.CalledFromUnitTestSuite;
RunNums = new List<int>();
}
protected virtual void ProcessFullExePath(string exe)
{
Exe = GetFullExePath(exe);
if (!File.Exists(Exe) && !File.Exists(Exe + ".exe"))
throw Host.ExceptUserArg(nameof(ArgumentsBase.Exe), "Executable {0} not found", Exe);
}
protected virtual string GetFullExePath(string exe)
{
if (!string.IsNullOrWhiteSpace(exe))
return exe;
#if CORECLR
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
return Path.Combine(SweepCommand.LocalExePath, "../Win/maml.exe");
//REVIEW: Need mac support
return Path.Combine(SweepCommand.LocalExePath, "../Linux/maml");
#else
return Path.Combine(SweepCommand.LocalExePath, "maml.exe");
#endif
}
public virtual void Finish()
{
if (Exe == null || Exe.EndsWith("maml", StringComparison.OrdinalIgnoreCase) ||
Exe.EndsWith("maml.exe", StringComparison.OrdinalIgnoreCase))
{
string currentDirectory = Path.GetDirectoryName(typeof(ExeConfigRunnerBase).Module.FullyQualifiedName);
using (var ch = Host.Start("Finish"))
#pragma warning disable CS0618 // As this deals with invoking command lines, this may be OK, though this code has some other problems.
using (AssemblyLoadingUtils.CreateAssemblyRegistrar(Host, currentDirectory))
#pragma warning restore CS0618
{
var runs = RunNums.ToArray();
var args = Utils.BuildArray(RunNums.Count + 2,
i =>
{
if (i == RunNums.Count)
return string.Format(@"o={{{0}\{1}.summary.txt}}", OutputFolder, Prefix);
if (i == RunNums.Count + 1)
return string.Format("calledFromUnitTestSuite{0}", _calledFromUnitTestSuite ? "+" : "-");
return string.Format("{{{0}}}", GetFilePath(runs[i], "out"));
});
ResultProcessorInternal.ResultProcessor.Main(args);
ch.Info(@"The summary of the run results has been saved to the file {0}\{1}.summary.txt", OutputFolder, Prefix);
}
}
}
public virtual string GetOutputFolderPath(string folderName)
{
var folderPath = Path.GetFullPath(folderName);
try
{
if (!Directory.Exists(folderName))
Directory.CreateDirectory(folderName);
return folderPath;
}
catch (Exception e)
{
throw Host.Except(e, e.Message);
}
}
// REVIEW: in case we want to use sweep command on linux we need to reconsider our syntax.
// $something get treated in bash as variable something and if you have command line which looks like:
// lr=$LR$
// you get lr=$ only as argument because $LR is variable and empty.
protected string GetCommandLine(ParameterSet sweep)
{
var arguments = ArgsPattern;
foreach (var parameterValue in sweep)
arguments = arguments.Replace("$" + parameterValue.Name + "$", parameterValue.ValueText);
return arguments;
}
public IEnumerable<IRunResult> RunConfigs(ParameterSet[] sweeps, int min)
{
RunNums.AddRange(Enumerable.Range(min, sweeps.Length));
using (var ch = Host.Start("Evaluate"))
{
for (int i = 0; i < sweeps.Length; i++)
ch.Info("Parameter set: {0}", string.Join(", ", sweeps[i].Select(p => string.Format("{0}:{1}", p.Name, p.ValueText))));
return RunConfigsCore(sweeps, ch, min);
}
}
protected string GetFilePath(int i, string kind)
{
return string.Format(@"{0}\{1}{2}.{3}.txt", OutputFolder, Prefix, i, kind);
}
protected abstract IEnumerable<IRunResult> RunConfigsCore(ParameterSet[] sweeps, IChannel ch, int min);
}
public sealed class LocalExeConfigRunner : ExeConfigRunnerBase
{
public sealed class Arguments : ArgumentsBase
{
[Argument(ArgumentType.AtMostOnce, HelpText = "The number of threads to use for the sweep (default auto determined by the number of cores)", ShortName = "t")]
public int? NumThreads;
}
private readonly ParallelOptions _parallelOptions;
public LocalExeConfigRunner(IHostEnvironment env, Arguments args)
: base(args, env, "LocalExeSweepEvaluator")
{
Contracts.CheckParam(args.NumThreads == null || args.NumThreads.Value > 0, nameof(args.NumThreads), "Cannot be 0 or negative");
_parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = args.NumThreads ?? -1 };
Contracts.AssertNonEmpty(args.OutputFolderName);
ProcessFullExePath(args.Exe);
}
protected override IEnumerable<IRunResult> RunConfigsCore(ParameterSet[] sweeps, IChannel ch, int min)
{
Parallel.For(0, sweeps.Length, _parallelOptions, j =>
{
var outFile = GetFilePath(min + j, "out");
var errorFile = GetFilePath(min + j, "err");
var arguments = GetCommandLine(sweeps[j]);
RunProcess(Exe, new string[] { arguments }, Environment.CurrentDirectory,
new StreamWriter(outFile),
new StreamWriter(errorFile));
if (File.Exists(errorFile) && new FileInfo(errorFile).Length == 0)
{
File.Delete(errorFile);
}
});
return sweeps.Select((sweep, j) =>
ResultProcessor.GetRunResult(sweep, string.Format(@"{0}\{1}.out.txt", OutputFolder, min + j)));
}
/// <summary>
/// Run specified EXE with given arguments
/// </summary>
private void RunProcess(string exeFilename, string[] args, string workingDir,
TextWriter standardOutputWriter = null, TextWriter standardErrorWriter = null)
{
var p = new System.Diagnostics.Process
{
StartInfo =
{
UseShellExecute = false,
CreateNoWindow = true,
FileName = exeFilename,
Arguments = (args == null ? "" : string.Join(" ", args)),
}
};
if (workingDir != null)
p.StartInfo.WorkingDirectory = workingDir;
if (standardOutputWriter != null)
{
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += (s, a) => { if (a.Data != null) standardOutputWriter.WriteLine(a.Data); };
}
if (standardErrorWriter != null)
{
p.StartInfo.RedirectStandardError = true;
p.ErrorDataReceived += (s, a) => { if (a.Data != null) standardErrorWriter.WriteLine(a.Data); };
}
p.Start();
//p.EnableRaisingEvents = true; // REVIEW: Why would you claim you wanted to
// use the async exit handler, only to just use WaitForExit downstream?
if (standardOutputWriter != null)
p.BeginOutputReadLine();
if (standardErrorWriter != null)
p.BeginErrorReadLine();
p.WaitForExit();
if (standardOutputWriter != null)
{
standardOutputWriter.Flush();
standardOutputWriter.Close();
}
if (standardErrorWriter != null)
{
standardErrorWriter.Flush();
standardErrorWriter.Close();
}
}
}
}