Skip to content

Commit 6d83f5d

Browse files
ericstjasp2286
authored andcommitted
Improve native build and mark our official build as CFS Clean (dotnet#7516)
* Pass additional settings when compiling native binaries * Mark our build as CFSClean (cherry picked from commit 2cdb7e0)
1 parent 694bc60 commit 6d83f5d

File tree

11 files changed

+488
-26
lines changed

11 files changed

+488
-26
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using Microsoft.ML.Data;
8+
using Microsoft.ML;
89

910
namespace Microsoft.ML.Runtime;
1011

@@ -115,6 +116,11 @@ internal interface IHostEnvironmentInternal : IHostEnvironment
115116
T GetOptionOrDefault<T>(string name);
116117

117118
bool RemoveOption(string name);
119+
120+
/// <summary>
121+
/// Global random source underpinning this environment.
122+
/// </summary>
123+
IRandomSource RandomSource { get; }
118124
}
119125

120126
/// <summary>
@@ -129,6 +135,11 @@ public interface IHost : IHostEnvironment
129135
/// generators are NOT thread safe.
130136
/// </summary>
131137
Random Rand { get; }
138+
139+
/// <summary>
140+
/// The random source backing <see cref="Rand"/>.
141+
/// </summary>
142+
IRandomSource RandomSource { get; }
132143
}
133144

134145
/// <summary>

src/Microsoft.ML.Core/Environment/ConsoleEnvironment.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

55
using System;
66
using System.IO;
77
using System.Linq;
88
using System.Threading;
9+
using Microsoft.ML;
10+
911

1012
namespace Microsoft.ML.Runtime;
1113

@@ -359,14 +361,16 @@ protected override void Dispose(bool disposing)
359361
/// </summary>
360362
/// <param name="seed">Random seed. Set to <c>null</c> for a non-deterministic environment.</param>
361363
/// <param name="verbose">Set to <c>true</c> for fully verbose logging.</param>
364+
/// <param name="randomSource">Optional random source backing this environment.</param>
362365
/// <param name="sensitivity">Allowed message sensitivity.</param>
363366
/// <param name="outWriter">Text writer to print normal messages to.</param>
364367
/// <param name="errWriter">Text writer to print error messages to.</param>
365368
/// <param name="testWriter">Optional TextWriter to write messages if the host is a test environment.</param>
366369
public ConsoleEnvironment(int? seed = null, bool verbose = false,
370+
IRandomSource randomSource = null,
367371
MessageSensitivity sensitivity = MessageSensitivity.All,
368372
TextWriter outWriter = null, TextWriter errWriter = null, TextWriter testWriter = null)
369-
: base(seed, verbose, nameof(ConsoleEnvironment))
373+
: base(seed, verbose, randomSource, nameof(ConsoleEnvironment))
370374
{
371375
Contracts.CheckValueOrNull(outWriter);
372376
Contracts.CheckValueOrNull(errWriter);
@@ -391,13 +395,14 @@ private void PrintMessage(IMessageSource src, ChannelMessage msg)
391395
Root._consoleWriter.PrintMessage(src, msg);
392396
}
393397

394-
protected override IHost RegisterCore(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, Random rand, bool verbose)
398+
protected override IHost RegisterCore(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, Random rand, IRandomSource randomSource, bool verbose)
395399
{
396400
Contracts.AssertValue(rand);
401+
Contracts.AssertValue(randomSource);
397402
Contracts.AssertValueOrNull(parentFullName);
398403
Contracts.AssertNonEmpty(shortName);
399404
Contracts.Assert(source == this || source is Host);
400-
return new Host(source, shortName, parentFullName, rand, verbose);
405+
return new Host(source, shortName, parentFullName, rand, randomSource, verbose);
401406
}
402407

403408
protected override IChannel CreateCommChannel(ChannelProviderBase parent, string name)
@@ -462,8 +467,8 @@ public void Dispose()
462467

463468
private sealed class Host : HostBase
464469
{
465-
public Host(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, Random rand, bool verbose)
466-
: base(source, shortName, parentFullName, rand, verbose)
470+
public Host(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, Random rand, IRandomSource randomSource, bool verbose)
471+
: base(source, shortName, parentFullName, rand, randomSource, verbose)
467472
{
468473
IsCanceled = source.IsCanceled;
469474
}
@@ -484,9 +489,11 @@ protected override IPipe<TMessage> CreatePipe<TMessage>(ChannelProviderBase pare
484489
return new Pipe<TMessage>(parent, name, GetDispatchDelegate<TMessage>());
485490
}
486491

487-
protected override IHost RegisterCore(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, Random rand, bool verbose)
492+
protected override IHost RegisterCore(HostEnvironmentBase<ConsoleEnvironment> source, string shortName, string parentFullName, Random rand, IRandomSource randomSource, bool verbose)
488493
{
489-
return new Host(source, shortName, parentFullName, rand, verbose);
494+
return new Host(source, shortName, parentFullName, rand, randomSource, verbose);
490495
}
491496
}
492497
}
498+
499+

src/Microsoft.ML.Core/Environment/HostEnvironmentBase.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

55
using System;
66
using System.Collections.Concurrent;
77
using System.Collections.Generic;
88
using System.IO;
9+
using Microsoft.ML;
10+
using Microsoft.ML.Internal.Utilities;
911

1012
namespace Microsoft.ML.Runtime;
1113

@@ -121,8 +123,8 @@ public abstract class HostBase : HostEnvironmentBase<TEnv>, IHost
121123

122124
public Random Rand => _rand;
123125

124-
public HostBase(HostEnvironmentBase<TEnv> source, string shortName, string parentFullName, Random rand, bool verbose)
125-
: base(source, rand, verbose, shortName, parentFullName)
126+
public HostBase(HostEnvironmentBase<TEnv> source, string shortName, string parentFullName, Random rand, IRandomSource randomSource, bool verbose)
127+
: base(source, rand, randomSource ?? new RandomSourceAdapter(rand), verbose, shortName, parentFullName)
126128
{
127129
Depth = source.Depth + 1;
128130
}
@@ -140,7 +142,8 @@ public HostBase(HostEnvironmentBase<TEnv> source, string shortName, string paren
140142
{
141143
_children.RemoveAll(r => r.TryGetTarget(out IHost _) == false);
142144
Random rand = (seed.HasValue) ? RandomUtils.Create(seed.Value) : RandomUtils.Create(_rand);
143-
host = RegisterCore(this, name, Master?.FullName, rand, verbose ?? Verbose);
145+
IRandomSource randomSource = new RandomSourceAdapter(rand);
146+
host = RegisterCore(this, name, Master?.FullName, rand, randomSource, verbose ?? Verbose);
144147
if (!IsCanceled)
145148
_children.Add(new WeakReference<IHost>(host));
146149
}
@@ -338,6 +341,8 @@ public void RemoveListener(Action<IMessageSource, TMessage> listenerFunc)
338341
protected Dictionary<string, object> Options { get; } = [];
339342
#pragma warning restore MSML_NoInstanceInitializers
340343

344+
public IRandomSource RandomSource => _randomSource;
345+
341346
protected readonly TEnv Root;
342347
// This is non-null iff this environment was a fork of another. Disposing a fork
343348
// doesn't free temp files. That is handled when the master is disposed.
@@ -348,6 +353,7 @@ public void RemoveListener(Action<IMessageSource, TMessage> listenerFunc)
348353

349354
// The random number generator for this host.
350355
private readonly Random _rand;
356+
private readonly IRandomSource _randomSource;
351357

352358
public int? Seed { get; }
353359

@@ -369,11 +375,22 @@ public void RemoveListener(Action<IMessageSource, TMessage> listenerFunc)
369375
/// The main constructor.
370376
/// </summary>
371377
protected HostEnvironmentBase(int? seed, bool verbose,
378+
IRandomSource randomSource = null,
372379
string shortName = null, string parentFullName = null)
373380
: base(shortName, parentFullName, verbose)
374381
{
375382
Seed = seed;
376-
_rand = RandomUtils.Create(Seed);
383+
if (randomSource is null)
384+
{
385+
var baseRandom = RandomUtils.Create(Seed);
386+
_rand = baseRandom;
387+
_randomSource = new RandomSourceAdapter(baseRandom);
388+
}
389+
else
390+
{
391+
_randomSource = randomSource;
392+
_rand = randomSource as Random ?? new RandomFromRandomSource(randomSource);
393+
}
377394
ListenerDict = new ConcurrentDictionary<Type, Dispatcher>();
378395
ProgressTracker = new ProgressReporting.ProgressTracker(this);
379396
_cancelLock = new object();
@@ -385,13 +402,14 @@ protected HostEnvironmentBase(int? seed, bool verbose,
385402
/// <summary>
386403
/// This constructor is for forking.
387404
/// </summary>
388-
protected HostEnvironmentBase(HostEnvironmentBase<TEnv> source, Random rand, bool verbose,
405+
protected HostEnvironmentBase(HostEnvironmentBase<TEnv> source, Random rand, IRandomSource randomSource, bool verbose,
389406
string shortName = null, string parentFullName = null)
390407
: base(shortName, parentFullName, verbose)
391408
{
392409
Contracts.CheckValue(source, nameof(source));
393410
Contracts.CheckValueOrNull(rand);
394-
_rand = rand ?? RandomUtils.Create();
411+
_randomSource = randomSource ?? (rand != null ? new RandomSourceAdapter(rand) : new RandomSourceAdapter(RandomUtils.Create()));
412+
_rand = rand ?? (_randomSource as Random ?? new RandomFromRandomSource(_randomSource));
395413
_cancelLock = new object();
396414

397415
// This fork shares some stuff with the master.
@@ -419,7 +437,8 @@ public IHost Register(string name, int? seed = null, bool? verbose = null)
419437
{
420438
_children.RemoveAll(r => r.TryGetTarget(out IHost _) == false);
421439
Random rand = (seed.HasValue) ? RandomUtils.Create(seed.Value) : RandomUtils.Create(_rand);
422-
host = RegisterCore(this, name, Master?.FullName, rand, verbose ?? Verbose);
440+
IRandomSource randomSource = new RandomSourceAdapter(rand);
441+
host = RegisterCore(this, name, Master?.FullName, rand, randomSource, verbose ?? Verbose);
423442

424443
// Need to manually copy over the parameters
425444
//((IHostEnvironmentInternal)host).Seed = this.Seed;
@@ -433,7 +452,7 @@ public IHost Register(string name, int? seed = null, bool? verbose = null)
433452
}
434453

435454
protected abstract IHost RegisterCore(HostEnvironmentBase<TEnv> source, string shortName,
436-
string parentFullName, Random rand, bool verbose);
455+
string parentFullName, Random rand, IRandomSource randomSource, bool verbose);
437456

438457
public IProgressChannel StartProgressChannel(string name)
439458
{
@@ -659,3 +678,8 @@ public bool RemoveOption(string name)
659678
return Options.Remove(name);
660679
}
661680
}
681+
682+
683+
684+
685+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.Internal.Utilities
8+
{
9+
/// <summary>
10+
/// Optional extension for RNG engines that can produce bulk sequences efficiently.
11+
/// </summary>
12+
internal interface IRandomBulkSource
13+
{
14+
/// <summary>Fills <paramref name="destination"/> with independent U[0,1) doubles.</summary>
15+
void NextDoubles(Span<double> destination);
16+
17+
/// <summary>Fills <paramref name="destination"/> with independent uint values covering the full 32-bit range.</summary>
18+
void NextUInt32(Span<uint> destination);
19+
}
20+
}
21+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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
8+
{
9+
/// <summary>
10+
/// Abstraction for RNG engines that expose the standard <see cref="Random"/> surface.
11+
/// </summary>
12+
public interface IRandomSource
13+
{
14+
int Next();
15+
int Next(int maxValue);
16+
int Next(int minValue, int maxValue);
17+
18+
long NextInt64();
19+
long NextInt64(long maxValue);
20+
long NextInt64(long minValue, long maxValue);
21+
22+
double NextDouble();
23+
float NextSingle();
24+
25+
void NextBytes(Span<byte> buffer);
26+
}
27+
}
28+

0 commit comments

Comments
 (0)