-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Renamed TlcEnvironment to Console. Also introduced LocalEnvironment #923
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
Changes from all commits
28a17e3
dbe5a71
047f90a
f9a91fa
10a8a68
0f6bca4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// 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; | ||
|
||
namespace Microsoft.ML.Runtime.Data | ||
{ | ||
using Stopwatch = System.Diagnostics.Stopwatch; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What other kind of Stopwatch is there? Can we just have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Of course we could relax it. That practice dates from the times before we had channels, where literally every component instantiated stopwatches to time themselves, and there were lots of namespace collisions, and so lots of cost. Now that we do have channels, few things produce their own stopwatches -- use of System.Diagnostics is quite limited. So the amount of pain that would be generated now by your suggestion would be much less. In reply to: 218094176 [](ancestors = 218094176) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
/// <summary> | ||
/// An ML.NET environment for local execution. | ||
/// </summary> | ||
public sealed class LocalEnvironment : HostEnvironmentBase<LocalEnvironment> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have some tests that cover this class. #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
private sealed class Channel : ChannelBase | ||
{ | ||
public readonly Stopwatch Watch; | ||
public Channel(LocalEnvironment root, ChannelProviderBase parent, string shortName, | ||
Action<IMessageSource, ChannelMessage> dispatch) | ||
: base(root, parent, shortName, dispatch) | ||
{ | ||
Watch = Stopwatch.StartNew(); | ||
Dispatch(this, new ChannelMessage(ChannelMessageKind.Trace, MessageSensitivity.None, "Channel started")); | ||
} | ||
|
||
public override void Done() | ||
{ | ||
Watch.Stop(); | ||
ChannelFinished(); | ||
base.Done(); | ||
} | ||
|
||
private void ChannelFinished() | ||
=> Dispatch(this, new ChannelMessage(ChannelMessageKind.Trace, MessageSensitivity.None, "Channel finished. Elapsed { 0:c }.", Watch.Elapsed)); | ||
|
||
protected override void DisposeCore() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI - I logged #930 to implement the Dispose Pattern correctly on these types. #Resolved |
||
{ | ||
if (IsActive) | ||
{ | ||
ChannelFinished(); | ||
Watch.Stop(); | ||
} | ||
|
||
Dispatch(this, new ChannelMessage(ChannelMessageKind.Trace, MessageSensitivity.None, "Channel disposed")); | ||
base.DisposeCore(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Create an ML.NET <see cref="IHostEnvironment"/> for local execution. | ||
/// </summary> | ||
/// <param name="seed">Random seed. Set to <c>null</c> for a non-deterministic environment.</param> | ||
/// <param name="conc">Concurrency level. Set to 1 to run single-threaded. Set to 0 to pick automatically.</param> | ||
public LocalEnvironment(int? seed = null, int conc = 0) | ||
: base(RandomUtils.Create(seed), verbose: false, conc) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Add a custom listener to the messages of ML.NET components. | ||
/// </summary> | ||
public void AddListener(Action<IMessageSource, ChannelMessage> listener) | ||
=> AddListener<ChannelMessage>(listener); | ||
|
||
/// <summary> | ||
/// Remove a previously added a custom listener. | ||
/// </summary> | ||
public void RemoveListener(Action<IMessageSource, ChannelMessage> listener) | ||
=> RemoveListener<ChannelMessage>(listener); | ||
|
||
protected override IFileHandle CreateTempFileCore(IHostEnvironment env, string suffix = null, string prefix = null) | ||
=> base.CreateTempFileCore(env, suffix, "Local_" + prefix); | ||
|
||
protected override IHost RegisterCore(HostEnvironmentBase<LocalEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc) | ||
{ | ||
Contracts.AssertValue(rand); | ||
Contracts.AssertValueOrNull(parentFullName); | ||
Contracts.AssertNonEmpty(shortName); | ||
Contracts.Assert(source == this || source is Host); | ||
return new Host(source, shortName, parentFullName, rand, verbose, conc); | ||
} | ||
|
||
protected override IChannel CreateCommChannel(ChannelProviderBase parent, string name) | ||
{ | ||
Contracts.AssertValue(parent); | ||
Contracts.Assert(parent is LocalEnvironment); | ||
Contracts.AssertNonEmpty(name); | ||
return new Channel(this, parent, name, GetDispatchDelegate<ChannelMessage>()); | ||
} | ||
|
||
protected override IPipe<TMessage> CreatePipe<TMessage>(ChannelProviderBase parent, string name) | ||
{ | ||
Contracts.AssertValue(parent); | ||
Contracts.Assert(parent is LocalEnvironment); | ||
Contracts.AssertNonEmpty(name); | ||
return new Pipe<TMessage>(parent, name, GetDispatchDelegate<TMessage>()); | ||
} | ||
|
||
private sealed class Host : HostBase | ||
{ | ||
public Host(HostEnvironmentBase<LocalEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc) | ||
: base(source, shortName, parentFullName, rand, verbose, conc) | ||
{ | ||
IsCancelled = source.IsCancelled; | ||
} | ||
|
||
protected override IChannel CreateCommChannel(ChannelProviderBase parent, string name) | ||
{ | ||
Contracts.AssertValue(parent); | ||
Contracts.Assert(parent is Host); | ||
Contracts.AssertNonEmpty(name); | ||
return new Channel(Root, parent, name, GetDispatchDelegate<ChannelMessage>()); | ||
} | ||
|
||
protected override IPipe<TMessage> CreatePipe<TMessage>(ChannelProviderBase parent, string name) | ||
{ | ||
Contracts.AssertValue(parent); | ||
Contracts.Assert(parent is Host); | ||
Contracts.AssertNonEmpty(name); | ||
return new Pipe<TMessage>(parent, name, GetDispatchDelegate<TMessage>()); | ||
} | ||
|
||
protected override IHost RegisterCore(HostEnvironmentBase<LocalEnvironment> source, string shortName, string parentFullName, IRandom rand, bool verbose, int? conc) | ||
{ | ||
return new Host(source, shortName, parentFullName, rand, verbose, conc); | ||
} | ||
} | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it really only TlcEnvironment specific or can be used in any HostEnvironmentBase descendant? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it could be used elsewhere, but I'm not sure, and I would rather not try now.
In reply to: 217869313 [](ancestors = 217869313)