Skip to content

Commit ae4d85c

Browse files
authored
Forward logs of Experiment's sub MLContexts to main MLContext (#5554)
* Forward logs of Experiment's sub MLContexts to main MLContext * Adressed reviews
1 parent e3597cd commit ae4d85c

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/Microsoft.ML.AutoML/Experiment/Experiment.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,29 @@ private void MainContextCanceledEvent(object state)
8585
}
8686
}
8787

88+
private void RelayCurrentContextLogsToLogger(object sender, LoggingEventArgs e)
89+
{
90+
// Relay logs that are generated by the current MLContext to the Experiment class's
91+
// _logger.
92+
switch (e.Kind)
93+
{
94+
case ChannelMessageKind.Trace:
95+
_logger.Trace(e.Message);
96+
break;
97+
case ChannelMessageKind.Info:
98+
_logger.Info(e.Message);
99+
break;
100+
case ChannelMessageKind.Warning:
101+
_logger.Warning(e.Message);
102+
break;
103+
case ChannelMessageKind.Error:
104+
_logger.Error(e.Message);
105+
break;
106+
default:
107+
throw new NotImplementedException($"{nameof(ChannelMessageKind)}.{e.Kind} is not yet implemented.");
108+
}
109+
}
110+
88111
public IList<TRunDetail> Execute()
89112
{
90113
var iterationResults = new List<TRunDetail>();
@@ -129,6 +152,7 @@ public IList<TRunDetail> Execute()
129152
// context is canceled to stop further model training. The cancellation of the main MLContext
130153
// a user has instantiated is not desirable, thus additional MLContexts are used.
131154
_currentModelMLContext = _newContextSeedGenerator == null ? new MLContext() : new MLContext(_newContextSeedGenerator.Next());
155+
_currentModelMLContext.Log += RelayCurrentContextLogsToLogger;
132156
var pipeline = PipelineSuggester.GetNextInferredPipeline(_currentModelMLContext, _history, _datasetColumnInfo, _task,
133157
_optimizingMetricInfo.IsMaximizing, _experimentSettings.CacheBeforeTrainer, _logger, _trainerAllowList);
134158
// break if no candidates returned, means no valid pipeline available

test/Microsoft.ML.AutoML.Tests/AutoFitTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,40 @@ namespace Microsoft.ML.AutoML.Test
2121
{
2222
public class AutoFitTests : BaseTestClass
2323
{
24+
// Marker necessary for AutoFitContextLogTest to ensure that the wanted logs
25+
// from Experiment's sub MLContexts were relayed to the main calling MLContext.
26+
bool _markerAutoFitContextLogTest;
2427
public AutoFitTests(ITestOutputHelper output) : base(output)
2528
{
2629
}
2730

31+
private void MlContextLog(object sender, LoggingEventArgs e)
32+
{
33+
// Log containing ImageClassificationTrainer will only come from AutoML's sub
34+
// contexts.
35+
if (!_markerAutoFitContextLogTest && e.Message.Contains("[Source=ImageClassificationTrainer;"))
36+
_markerAutoFitContextLogTest = true;
37+
}
38+
39+
[TensorFlowFact]
40+
public void AutoFitContextLogTest()
41+
{
42+
// This test confirms that logs produced from contexts made during AutoML experiment
43+
// runs are correctly relayed to the main Experiment MLContext.
44+
_markerAutoFitContextLogTest = false;
45+
var context = new MLContext(1);
46+
context.Log += MlContextLog;
47+
var datasetPath = DatasetUtil.GetFlowersDataset();
48+
var columnInference = context.Auto().InferColumns(datasetPath, "Label");
49+
var textLoader = context.Data.CreateTextLoader(columnInference.TextLoaderOptions);
50+
var trainData = textLoader.Load(datasetPath);
51+
var result = context.Auto()
52+
.CreateMulticlassClassificationExperiment(15)
53+
.Execute(trainData, columnInference.ColumnInformation);
54+
Assert.True(_markerAutoFitContextLogTest, "Image classification trainer logs from Experiment's sub contexts" +
55+
"were not relayed to the main MLContext.");
56+
}
57+
2858
[Fact]
2959
public void AutoFitBinaryTest()
3060
{

0 commit comments

Comments
 (0)