Skip to content

Use Timer and ctx.CancelExecution() to fix AutoML max-time experiment bug #5445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d0f7054
Use ctx.CalncelExecution() to fix AutoML max-time experiment bug
mstfbl Oct 21, 2020
4fa26f8
Added unit test for checking canceled experiment
mstfbl Oct 21, 2020
48a6267
Nit fix
mstfbl Oct 21, 2020
f324030
Different run time on Linux
mstfbl Oct 22, 2020
ee70024
Review
mstfbl Oct 22, 2020
36bf24e
Testing four ouput
mstfbl Oct 22, 2020
d5d23de
Used reflection to test for contexts being canceled
mstfbl Oct 23, 2020
33cf5a6
Reviews
mstfbl Oct 26, 2020
bfc93e9
Merge remote-tracking branch 'upstream/master' into issue5437
mstfbl Oct 26, 2020
c69a19f
Reviews
mstfbl Oct 28, 2020
299b05b
Added main MLContext listener-timer
mstfbl Oct 29, 2020
2e2d441
Merge remote-tracking branch 'upstream/master' into issue5437
mstfbl Oct 29, 2020
ce747fb
Added PRNG on _context, held onto timers for avoiding GC
mstfbl Oct 30, 2020
7635500
Addressed reviews
mstfbl Oct 30, 2020
94a80de
Unit test edits
mstfbl Oct 30, 2020
abe1d7f
Increase run time of experiment to guarantee probabilities
mstfbl Oct 30, 2020
9585a50
Edited unit test to check produced schema of next run model's predict…
mstfbl Oct 30, 2020
1ab662f
Remove scheme check as different CI builds result in varying schemas
mstfbl Oct 30, 2020
bc9e578
Decrease max experiment time unit test time
mstfbl Oct 30, 2020
71ebf23
Merged with master
mstfbl Oct 31, 2020
2d8d06f
Added Timers
mstfbl Nov 2, 2020
490d8c1
Increase second timer time, edit unit test
mstfbl Nov 2, 2020
b0de1d3
Added try catch for OperationCanceledException in Execute()
mstfbl Nov 3, 2020
0918afa
Add AggregateException try catch to slow unit tests for parallel testing
mstfbl Nov 3, 2020
0922aed
Reviews
mstfbl Nov 3, 2020
ef4b34f
Final reviews
mstfbl Nov 3, 2020
b4b49ce
Added LightGBMFact to binary classification test
mstfbl Nov 3, 2020
6502fc8
Removed extra Operation Stopped exception try catch
mstfbl Nov 3, 2020
28e2f2e
Add back OperationCanceledException to Experiment.cs
mstfbl Nov 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added unit test for checking canceled experiment
  • Loading branch information
mstfbl committed Oct 21, 2020
commit 4fa26f82676718172c7f24e15b84a9bcbdb25635
35 changes: 26 additions & 9 deletions src/Microsoft.ML.AutoML/Experiment/Experiment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal class Experiment<TRunDetail, TMetrics> where TRunDetail : RunDetail
private readonly IRunner<TRunDetail> _runner;
private readonly IList<SuggestedPipelineRunDetail> _history;
private readonly IChannel _logger;
private bool _endExperimentWhenAble = false;

public Experiment(MLContext context,
TaskKind task,
Expand Down Expand Up @@ -54,21 +55,36 @@ public Experiment(MLContext context,

private void MaxExperimentTimeExpiredEvent(object sender, EventArgs e)
{
_logger.Warning("Allocated time for Experiment of {0} seconds has elapsed with {1} models run. Ending experiment...",
_experimentSettings.MaxExperimentTimeInSeconds, _history.Count());
_context.CancelExecution();
// If at least one model was run, end experiment immediately.
// Else, wait for first model to run before experiment is concluded.
_endExperimentWhenAble = true;
if (_history.Count > 0)
{
_logger.Warning("Allocated time for Experiment of {0} seconds has elapsed with {1} models run. Ending experiment...",
_experimentSettings.MaxExperimentTimeInSeconds, _history.Count());
_context.CancelExecution();
}
}

public IList<TRunDetail> Execute()
{
var iterationResults = new List<TRunDetail>();
// Create a timer for the max duration of experiment. When given time has
// elapsed, MaxExperimentTimeExpiredEvent is called to interrupt training
// of current model.
Timer timer = new Timer(_experimentSettings.MaxExperimentTimeInSeconds * 1000);
timer.Elapsed += MaxExperimentTimeExpiredEvent;
timer.AutoReset = false;
timer.Enabled = true;
// of current model. Timer is not used if no experiment time is given, or
// is not a positive number.
if (_experimentSettings.MaxExperimentTimeInSeconds > 0)
{
Timer timer = new Timer(_experimentSettings.MaxExperimentTimeInSeconds * 1000);
timer.Elapsed += MaxExperimentTimeExpiredEvent;
timer.AutoReset = false;
timer.Enabled = true;
}
// If given max duration of experiment is 0, only 1 model will be trained.
// _experimentSettings.MaxExperimentTimeInSeconds is of type uint, it is
// either 0 or >0.
else
_endExperimentWhenAble = true;

do
{
Expand Down Expand Up @@ -114,7 +130,8 @@ public IList<TRunDetail> Execute()
}

} while (_history.Count < _experimentSettings.MaxModels &&
!_experimentSettings.CancellationToken.IsCancellationRequested);
!_experimentSettings.CancellationToken.IsCancellationRequested &&
!_endExperimentWhenAble);

return iterationResults;
}
Expand Down
18 changes: 18 additions & 0 deletions test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Linq;
using Microsoft.ML.Data;
using Microsoft.ML.Runtime;
using Microsoft.ML.TestFramework;
using Microsoft.ML.TestFramework.Attributes;
using Microsoft.ML.TestFrameworkCommon;
Expand Down Expand Up @@ -320,6 +321,23 @@ public void AutoFitWithPresplittedData()

}

[Fact]
public void AutoFitMaxExperimentTimeTest()
{
// 1 Binary classification experiment takes less than 5 seconds.
// System.OperationCanceledException is thrown when ongoing experiment
// is canceled and at least one model has been generated.
var context = new MLContext(1);
var dataPath = DatasetUtil.GetUciAdultDataset();
var columnInference = context.Auto().InferColumns(dataPath, DatasetUtil.UciAdultLabel);
var textLoader = context.Data.CreateTextLoader(columnInference.TextLoaderOptions);
var trainData = textLoader.Load(dataPath);
var experiment = context.Auto()
.CreateBinaryClassificationExperiment(5)
.Execute(trainData, new ColumnInformation() { LabelColumnName = DatasetUtil.UciAdultLabel });
Assert.True((context.Model.GetEnvironment() as ICancelable).IsCanceled);
}

private TextLoader.Options GetLoaderArgs(string labelColumnName, string userIdColumnName, string itemIdColumnName)
{
return new TextLoader.Options()
Expand Down