Skip to content

Commit 160b0df

Browse files
authored
Renamed TlcEnvironment to Console. Also introduced LocalEnvironment (#923)
1 parent a8b844c commit 160b0df

File tree

85 files changed

+350
-203
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+350
-203
lines changed

src/Microsoft.ML.Api/TypedCursor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ public static ICursorable<TRow> AsCursorable<TRow>(this IDataView data, bool ign
658658
where TRow : class, new()
659659
{
660660
// REVIEW: Take an env as a parameter.
661-
var env = new TlcEnvironment();
661+
var env = new ConsoleEnvironment();
662662
return data.AsCursorable<TRow>(env, ignoreMissingColumns, schemaDefinition);
663663
}
664664

@@ -699,7 +699,7 @@ public static IEnumerable<TRow> AsEnumerable<TRow>(this IDataView data, bool reu
699699
where TRow : class, new()
700700
{
701701
// REVIEW: Take an env as a parameter.
702-
var env = new TlcEnvironment();
702+
var env = new ConsoleEnvironment();
703703
return data.AsEnumerable<TRow>(env, reuseRowObject, ignoreMissingColumns, schemaDefinition);
704704
}
705705
}

src/Microsoft.ML.Core/Data/ProgressReporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Microsoft.ML.Runtime.Data
1717
public static class ProgressReporting
1818
{
1919
/// <summary>
20-
/// The progress channel for <see cref="TlcEnvironment"/>.
20+
/// The progress channel for <see cref="ConsoleEnvironment"/>.
2121
/// This is coupled with a <see cref="ProgressTracker"/> that aggregates all events and returns them on demand.
2222
/// </summary>
2323
public sealed class ProgressChannel : IProgressChannel

src/Microsoft.ML.Core/Environment/TlcEnvironment.cs renamed to src/Microsoft.ML.Core/Environment/ConsoleEnvironment.cs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ namespace Microsoft.ML.Runtime.Data
1515
{
1616
using Stopwatch = System.Diagnostics.Stopwatch;
1717

18-
public sealed class TlcEnvironment : HostEnvironmentBase<TlcEnvironment>
18+
public sealed class ConsoleEnvironment : HostEnvironmentBase<ConsoleEnvironment>
1919
{
2020
public const string ComponentHistoryKey = "ComponentHistory";
2121

2222
private sealed class ConsoleWriter
2323
{
2424
private readonly object _lock;
25-
private readonly TlcEnvironment _parent;
25+
private readonly ConsoleEnvironment _parent;
2626
private readonly TextWriter _out;
2727
private readonly TextWriter _err;
2828

@@ -34,7 +34,7 @@ private sealed class ConsoleWriter
3434
private const int _maxDots = 50;
3535
private int _dots;
3636

37-
public ConsoleWriter(TlcEnvironment parent, TextWriter outWriter, TextWriter errWriter)
37+
public ConsoleWriter(ConsoleEnvironment parent, TextWriter outWriter, TextWriter errWriter)
3838
{
3939
Contracts.AssertValue(parent);
4040
Contracts.AssertValue(outWriter);
@@ -331,7 +331,7 @@ private bool PrintDot()
331331
private sealed class Channel : ChannelBase
332332
{
333333
public readonly Stopwatch Watch;
334-
public Channel(TlcEnvironment root, ChannelProviderBase parent, string shortName,
334+
public Channel(ConsoleEnvironment root, ChannelProviderBase parent, string shortName,
335335
Action<IMessageSource, ChannelMessage> dispatch)
336336
: base(root, parent, shortName, dispatch)
337337
{
@@ -358,20 +358,36 @@ protected override void DisposeCore()
358358
private volatile ConsoleWriter _consoleWriter;
359359
private readonly MessageSensitivity _sensitivityFlags;
360360

361-
public TlcEnvironment(int? seed = null, bool verbose = false,
361+
/// <summary>
362+
/// Create an ML.NET <see cref="IHostEnvironment"/> for local execution, with console feedback.
363+
/// </summary>
364+
/// <param name="seed">Random seed. Set to <c>null</c> for a non-deterministic environment.</param>
365+
/// <param name="verbose">Set to <c>true</c> for fully verbose logging.</param>
366+
/// <param name="sensitivity">Allowed message sensitivity.</param>
367+
/// <param name="conc">Concurrency level. Set to 1 to run single-threaded. Set to 0 to pick automatically.</param>
368+
/// <param name="outWriter">Text writer to print normal messages to.</param>
369+
/// <param name="errWriter">Text writer to print error messages to.</param>
370+
public ConsoleEnvironment(int? seed = null, bool verbose = false,
362371
MessageSensitivity sensitivity = MessageSensitivity.All, int conc = 0,
363372
TextWriter outWriter = null, TextWriter errWriter = null)
364373
: this(RandomUtils.Create(seed), verbose, sensitivity, conc, outWriter, errWriter)
365374
{
366375
}
367376

377+
// REVIEW: do we really care about custom random? If we do, let's make this ctor public.
368378
/// <summary>
369-
/// This takes ownership of the random number generator.
379+
/// Create an ML.NET environment for local execution, with console feedback.
370380
/// </summary>
371-
public TlcEnvironment(IRandom rand, bool verbose = false,
381+
/// <param name="rand">An custom source of randomness to use in the environment.</param>
382+
/// <param name="verbose">Set to <c>true</c> for fully verbose logging.</param>
383+
/// <param name="sensitivity">Allowed message sensitivity.</param>
384+
/// <param name="conc">Concurrency level. Set to 1 to run single-threaded. Set to 0 to pick automatically.</param>
385+
/// <param name="outWriter">Text writer to print normal messages to.</param>
386+
/// <param name="errWriter">Text writer to print error messages to.</param>
387+
private ConsoleEnvironment(IRandom rand, bool verbose = false,
372388
MessageSensitivity sensitivity = MessageSensitivity.All, int conc = 0,
373389
TextWriter outWriter = null, TextWriter errWriter = null)
374-
: base(rand, verbose, conc, nameof(TlcEnvironment))
390+
: base(rand, verbose, conc, nameof(ConsoleEnvironment))
375391
{
376392
Contracts.CheckValueOrNull(outWriter);
377393
Contracts.CheckValueOrNull(errWriter);
@@ -401,7 +417,7 @@ protected override IFileHandle CreateTempFileCore(IHostEnvironment env, string s
401417
return base.CreateTempFileCore(env, suffix, "TLC_" + prefix);
402418
}
403419

404-
protected override IHost RegisterCore(HostEnvironmentBase<TlcEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
420+
protected override IHost RegisterCore(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
405421
{
406422
Contracts.AssertValue(rand);
407423
Contracts.AssertValueOrNull(parentFullName);
@@ -413,15 +429,15 @@ protected override IHost RegisterCore(HostEnvironmentBase<TlcEnvironment> source
413429
protected override IChannel CreateCommChannel(ChannelProviderBase parent, string name)
414430
{
415431
Contracts.AssertValue(parent);
416-
Contracts.Assert(parent is TlcEnvironment);
432+
Contracts.Assert(parent is ConsoleEnvironment);
417433
Contracts.AssertNonEmpty(name);
418434
return new Channel(this, parent, name, GetDispatchDelegate<ChannelMessage>());
419435
}
420436

421437
protected override IPipe<TMessage> CreatePipe<TMessage>(ChannelProviderBase parent, string name)
422438
{
423439
Contracts.AssertValue(parent);
424-
Contracts.Assert(parent is TlcEnvironment);
440+
Contracts.Assert(parent is ConsoleEnvironment);
425441
Contracts.AssertNonEmpty(name);
426442
return new Pipe<TMessage>(parent, name, GetDispatchDelegate<TMessage>());
427443
}
@@ -439,11 +455,11 @@ internal IDisposable RedirectChannelOutput(TextWriter newOutWriter, TextWriter n
439455

440456
private sealed class OutputRedirector : IDisposable
441457
{
442-
private readonly TlcEnvironment _root;
458+
private readonly ConsoleEnvironment _root;
443459
private ConsoleWriter _oldConsoleWriter;
444460
private readonly ConsoleWriter _newConsoleWriter;
445461

446-
public OutputRedirector(TlcEnvironment env, TextWriter newOutWriter, TextWriter newErrWriter)
462+
public OutputRedirector(ConsoleEnvironment env, TextWriter newOutWriter, TextWriter newErrWriter)
447463
{
448464
Contracts.AssertValue(env);
449465
Contracts.AssertValue(newOutWriter);
@@ -467,7 +483,7 @@ public void Dispose()
467483

468484
private sealed class Host : HostBase
469485
{
470-
public Host(HostEnvironmentBase<TlcEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
486+
public Host(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
471487
: base(source, shortName, parentFullName, rand, verbose, conc)
472488
{
473489
IsCancelled = source.IsCancelled;
@@ -489,7 +505,7 @@ protected override IPipe<TMessage> CreatePipe<TMessage>(ChannelProviderBase pare
489505
return new Pipe<TMessage>(parent, name, GetDispatchDelegate<TMessage>());
490506
}
491507

492-
protected override IHost RegisterCore(HostEnvironmentBase<TlcEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
508+
protected override IHost RegisterCore(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
493509
{
494510
return new Host(source, shortName, parentFullName, rand, verbose, conc);
495511
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
7+
namespace Microsoft.ML.Runtime.Data
8+
{
9+
using Stopwatch = System.Diagnostics.Stopwatch;
10+
11+
/// <summary>
12+
/// An ML.NET environment for local execution.
13+
/// </summary>
14+
public sealed class LocalEnvironment : HostEnvironmentBase<LocalEnvironment>
15+
{
16+
private sealed class Channel : ChannelBase
17+
{
18+
public readonly Stopwatch Watch;
19+
public Channel(LocalEnvironment root, ChannelProviderBase parent, string shortName,
20+
Action<IMessageSource, ChannelMessage> dispatch)
21+
: base(root, parent, shortName, dispatch)
22+
{
23+
Watch = Stopwatch.StartNew();
24+
Dispatch(this, new ChannelMessage(ChannelMessageKind.Trace, MessageSensitivity.None, "Channel started"));
25+
}
26+
27+
public override void Done()
28+
{
29+
Watch.Stop();
30+
ChannelFinished();
31+
base.Done();
32+
}
33+
34+
private void ChannelFinished()
35+
=> Dispatch(this, new ChannelMessage(ChannelMessageKind.Trace, MessageSensitivity.None, "Channel finished. Elapsed { 0:c }.", Watch.Elapsed));
36+
37+
protected override void DisposeCore()
38+
{
39+
if (IsActive)
40+
{
41+
ChannelFinished();
42+
Watch.Stop();
43+
}
44+
45+
Dispatch(this, new ChannelMessage(ChannelMessageKind.Trace, MessageSensitivity.None, "Channel disposed"));
46+
base.DisposeCore();
47+
}
48+
}
49+
50+
/// <summary>
51+
/// Create an ML.NET <see cref="IHostEnvironment"/> for local execution.
52+
/// </summary>
53+
/// <param name="seed">Random seed. Set to <c>null</c> for a non-deterministic environment.</param>
54+
/// <param name="conc">Concurrency level. Set to 1 to run single-threaded. Set to 0 to pick automatically.</param>
55+
public LocalEnvironment(int? seed = null, int conc = 0)
56+
: base(RandomUtils.Create(seed), verbose: false, conc)
57+
{
58+
}
59+
60+
/// <summary>
61+
/// Add a custom listener to the messages of ML.NET components.
62+
/// </summary>
63+
public void AddListener(Action<IMessageSource, ChannelMessage> listener)
64+
=> AddListener<ChannelMessage>(listener);
65+
66+
/// <summary>
67+
/// Remove a previously added a custom listener.
68+
/// </summary>
69+
public void RemoveListener(Action<IMessageSource, ChannelMessage> listener)
70+
=> RemoveListener<ChannelMessage>(listener);
71+
72+
protected override IFileHandle CreateTempFileCore(IHostEnvironment env, string suffix = null, string prefix = null)
73+
=> base.CreateTempFileCore(env, suffix, "Local_" + prefix);
74+
75+
protected override IHost RegisterCore(HostEnvironmentBase<LocalEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
76+
{
77+
Contracts.AssertValue(rand);
78+
Contracts.AssertValueOrNull(parentFullName);
79+
Contracts.AssertNonEmpty(shortName);
80+
Contracts.Assert(source == this || source is Host);
81+
return new Host(source, shortName, parentFullName, rand, verbose, conc);
82+
}
83+
84+
protected override IChannel CreateCommChannel(ChannelProviderBase parent, string name)
85+
{
86+
Contracts.AssertValue(parent);
87+
Contracts.Assert(parent is LocalEnvironment);
88+
Contracts.AssertNonEmpty(name);
89+
return new Channel(this, parent, name, GetDispatchDelegate<ChannelMessage>());
90+
}
91+
92+
protected override IPipe<TMessage> CreatePipe<TMessage>(ChannelProviderBase parent, string name)
93+
{
94+
Contracts.AssertValue(parent);
95+
Contracts.Assert(parent is LocalEnvironment);
96+
Contracts.AssertNonEmpty(name);
97+
return new Pipe<TMessage>(parent, name, GetDispatchDelegate<TMessage>());
98+
}
99+
100+
private sealed class Host : HostBase
101+
{
102+
public Host(HostEnvironmentBase<LocalEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
103+
: base(source, shortName, parentFullName, rand, verbose, conc)
104+
{
105+
IsCancelled = source.IsCancelled;
106+
}
107+
108+
protected override IChannel CreateCommChannel(ChannelProviderBase parent, string name)
109+
{
110+
Contracts.AssertValue(parent);
111+
Contracts.Assert(parent is Host);
112+
Contracts.AssertNonEmpty(name);
113+
return new Channel(Root, parent, name, GetDispatchDelegate<ChannelMessage>());
114+
}
115+
116+
protected override IPipe<TMessage> CreatePipe<TMessage>(ChannelProviderBase parent, string name)
117+
{
118+
Contracts.AssertValue(parent);
119+
Contracts.Assert(parent is Host);
120+
Contracts.AssertNonEmpty(name);
121+
return new Pipe<TMessage>(parent, name, GetDispatchDelegate<TMessage>());
122+
}
123+
124+
protected override IHost RegisterCore(HostEnvironmentBase<LocalEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc)
125+
{
126+
return new Host(source, shortName, parentFullName, rand, verbose, conc);
127+
}
128+
}
129+
}
130+
131+
}

src/Microsoft.ML.FastTree/FastTree.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ private protected FastTreeTrainerBase(IHostEnvironment env, TArgs args)
110110
ParallelTraining = Args.ParallelTrainer != null ? Args.ParallelTrainer.CreateComponent(env) : new SingleTrainer();
111111
ParallelTraining.InitEnvironment();
112112
// REVIEW: CLR 4.6 has a bug that is only exposed in Scope, and if we trigger GC.Collect in scope environment
113-
// with memory consumption more than 5GB, GC get stuck in infinite loop. So for now let's call GC only if we call things from TlcEnvironment.
114-
AllowGC = (env is HostEnvironmentBase<TlcEnvironment>);
113+
// with memory consumption more than 5GB, GC get stuck in infinite loop. So for now let's call GC only if we call things from ConsoleEnvironment.
114+
AllowGC = (env is HostEnvironmentBase<ConsoleEnvironment>);
115115
Tests = new List<Test>();
116116

117117
InitializeThreads(numThreads);

src/Microsoft.ML.Legacy/LearningPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public PredictionModel<TInput, TOutput> Train<TInput, TOutput>()
161161
where TInput : class
162162
where TOutput : class, new()
163163
{
164-
using (var environment = new TlcEnvironment(seed: _seed, conc: _conc))
164+
using (var environment = new ConsoleEnvironment(seed: _seed, conc: _conc))
165165
{
166166
Experiment experiment = environment.CreateExperiment();
167167
ILearningPipelineStep step = null;

src/Microsoft.ML.Legacy/LearningPipelineDebugProxy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal sealed class LearningPipelineDebugProxy
2525
private const int MaxSlotNamesToDisplay = 100;
2626

2727
private readonly LearningPipeline _pipeline;
28-
private readonly TlcEnvironment _environment;
28+
private readonly ConsoleEnvironment _environment;
2929
private IDataView _preview;
3030
private Exception _pipelineExecutionException;
3131
private PipelineItemDebugColumn[] _columns;
@@ -39,7 +39,7 @@ public LearningPipelineDebugProxy(LearningPipeline pipeline)
3939
_pipeline = new LearningPipeline();
4040

4141
// use a ConcurrencyFactor of 1 so other threads don't need to run in the debugger
42-
_environment = new TlcEnvironment(conc: 1);
42+
_environment = new ConsoleEnvironment(conc: 1);
4343

4444
foreach (ILearningPipelineItem item in pipeline)
4545
{

src/Microsoft.ML.Legacy/Models/BinaryClassificationEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public sealed partial class BinaryClassificationEvaluator
2424
/// </returns>
2525
public BinaryClassificationMetrics Evaluate(PredictionModel model, ILearningPipelineLoader testData)
2626
{
27-
using (var environment = new TlcEnvironment())
27+
using (var environment = new ConsoleEnvironment())
2828
{
2929
environment.CheckValue(model, nameof(model));
3030
environment.CheckValue(testData, nameof(testData));

src/Microsoft.ML.Legacy/Models/ClassificationEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed partial class ClassificationEvaluator
2525
/// </returns>
2626
public ClassificationMetrics Evaluate(PredictionModel model, ILearningPipelineLoader testData)
2727
{
28-
using (var environment = new TlcEnvironment())
28+
using (var environment = new ConsoleEnvironment())
2929
{
3030
environment.CheckValue(model, nameof(model));
3131
environment.CheckValue(testData, nameof(testData));

src/Microsoft.ML.Legacy/Models/ClusterEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public sealed partial class ClusterEvaluator
2424
/// </returns>
2525
public ClusterMetrics Evaluate(PredictionModel model, ILearningPipelineLoader testData)
2626
{
27-
using (var environment = new TlcEnvironment())
27+
using (var environment = new ConsoleEnvironment())
2828
{
2929
environment.CheckValue(model, nameof(model));
3030
environment.CheckValue(testData, nameof(testData));

0 commit comments

Comments
 (0)