-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Added Microsoft.ML.Benchmarks Project #62
Changes from all commits
1088d68
0f5ce0c
1647ef5
2b163cf
335ead1
cf144ab
63fcb8a
8db49d0
0a281b2
b9b91e8
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,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<LangVersion>7.2</LangVersion> | ||
<StartupObject>Microsoft.ML.Benchmarks.Program</StartupObject> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Remove="BenchmarkDotNet.Artifacts\**" /> | ||
<EmbeddedResource Remove="BenchmarkDotNet.Artifacts\**" /> | ||
<None Remove="BenchmarkDotNet.Artifacts\**" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.10.14" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Microsoft.ML.StandardLearners\Microsoft.ML.StandardLearners.csproj" /> | ||
<ProjectReference Include="..\..\src\Microsoft.ML\Microsoft.ML.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<NativeAssemblyReference Include="CpuMathNative" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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 BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Diagnosers; | ||
using BenchmarkDotNet.Jobs; | ||
using BenchmarkDotNet.Running; | ||
using BenchmarkDotNet.Columns; | ||
using BenchmarkDotNet.Reports; | ||
using BenchmarkDotNet.Toolchains.CsProj; | ||
using BenchmarkDotNet.Toolchains.InProcess; | ||
using System; | ||
using System.IO; | ||
using Microsoft.ML.Models; | ||
using Microsoft.ML.Runtime.Api; | ||
using Microsoft.ML.Trainers; | ||
using Microsoft.ML.Transforms; | ||
using Microsoft.ML.Benchmarks; | ||
|
||
namespace Microsoft.ML.Benchmarks | ||
{ | ||
class Program | ||
{ | ||
/// <summary> | ||
/// execute dotnet run -c Release and choose the benchmarks you want to run | ||
/// </summary> | ||
/// <param name="args"></param> | ||
static void Main(string[] args) | ||
{ | ||
BenchmarkSwitcher | ||
.FromAssembly(typeof(Program).Assembly) | ||
.Run(null, CreateClrVsCoreConfig()); | ||
} | ||
|
||
private static IConfig CreateClrVsCoreConfig() | ||
{ | ||
var config = DefaultConfig.Instance.With( | ||
Job.ShortRun. | ||
With(InProcessToolchain.Instance)). | ||
With(new ClassificationMetricsColumn("AccuracyMacro", "Macro-average accuracy of the model")). | ||
With(MemoryDiagnoser.Default); | ||
return config; | ||
} | ||
|
||
internal static string GetDataPath(string name) | ||
=> Path.GetFullPath(Path.Combine(_dataRoot, name)); | ||
|
||
static readonly string _dataRoot; | ||
static Program() | ||
{ | ||
var currentAssemblyLocation = new FileInfo(typeof(Program).Assembly.Location); | ||
var rootDir = currentAssemblyLocation.Directory.Parent.Parent.Parent.Parent.FullName; | ||
_dataRoot = Path.Combine(rootDir, "test", "data"); | ||
} | ||
} | ||
|
||
public class ClassificationMetricsColumn : IColumn | ||
{ | ||
string _metricName; | ||
string _legend; | ||
|
||
public ClassificationMetricsColumn(string metricName, string legend) | ||
{ | ||
_metricName = metricName; | ||
_legend = legend; | ||
} | ||
|
||
public string ColumnName => _metricName; | ||
public string Id => _metricName; | ||
public string Legend => _legend; | ||
public bool IsNumeric => true; | ||
public bool IsDefault(Summary summary, Benchmark benchmark) => true; | ||
public bool IsAvailable(Summary summary) => true; | ||
public bool AlwaysShow => true; | ||
public ColumnCategory Category => ColumnCategory.Custom; | ||
public int PriorityInCategory => 1; | ||
public UnitType UnitType => UnitType.Dimensionless; | ||
|
||
public string GetValue(Summary summary, Benchmark benchmark, ISummaryStyle style) | ||
{ | ||
var property = typeof(ClassificationMetrics).GetProperty(_metricName); | ||
return property.GetValue(StochasticDualCoordinateAscentClassifierBench.s_metrics).ToString(); | ||
} | ||
public string GetValue(Summary summary, Benchmark benchmark) => GetValue(summary, benchmark, null); | ||
|
||
public override string ToString() => ColumnName; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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 BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Running; | ||
using Microsoft.ML.Models; | ||
using Microsoft.ML.Runtime.Api; | ||
using Microsoft.ML.Trainers; | ||
using Microsoft.ML.Transforms; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.ML.Benchmarks | ||
{ | ||
public class StochasticDualCoordinateAscentClassifierBench | ||
{ | ||
internal static ClassificationMetrics s_metrics; | ||
private static PredictionModel<IrisData, IrisPrediction> s_trainedModel; | ||
private static string s_dataPath; | ||
private static IrisData[][] s_batches; | ||
private static readonly int[] s_batchSizes = new int[] { 1, 2, 5 }; | ||
private readonly Random r = new Random(0); | ||
private readonly static IrisData s_example = new IrisData() | ||
{ | ||
SepalLength = 3.3f, | ||
SepalWidth = 1.6f, | ||
PetalLength = 0.2f, | ||
PetalWidth = 5.1f, | ||
}; | ||
|
||
[Benchmark] | ||
public PredictionModel<IrisData, IrisPrediction> TrainIris() => TrainCore(); | ||
|
||
[Benchmark] | ||
public float[] PredictIris() => s_trainedModel.Predict(s_example).PredictedLabels; | ||
|
||
[Benchmark] | ||
public IEnumerable<IrisPrediction> PredictIrisBatchOf1() => s_trainedModel.Predict(s_batches[0]); | ||
[Benchmark] | ||
public IEnumerable<IrisPrediction> PredictIrisBatchOf2() => s_trainedModel.Predict(s_batches[1]); | ||
[Benchmark] | ||
public IEnumerable<IrisPrediction> PredictIrisBatchOf5() => s_trainedModel.Predict(s_batches[2]); | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
s_dataPath = Program.GetDataPath("iris.txt"); | ||
s_trainedModel = TrainCore(); | ||
IrisPrediction prediction = s_trainedModel.Predict(s_example); | ||
|
||
var testData = new TextLoader<IrisData>(s_dataPath, useHeader: true, separator: "tab"); | ||
var evaluator = new ClassificationEvaluator(); | ||
s_metrics = evaluator.Evaluate(s_trainedModel, testData); | ||
|
||
s_batches = new IrisData[s_batchSizes.Length][]; | ||
for (int i = 0; i < s_batches.Length; i++) | ||
{ | ||
var batch = new IrisData[s_batchSizes[i]]; | ||
s_batches[i] = batch; | ||
for (int bi = 0; bi < batch.Length; bi++) | ||
{ | ||
batch[bi] = s_example; | ||
} | ||
} | ||
} | ||
|
||
private static PredictionModel<IrisData, IrisPrediction> TrainCore() | ||
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. Is this method a duplicate of 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. Yeah, they used to be different, but now after all the tweaks they become identical. I will remove one of them. |
||
{ | ||
var pipeline = new LearningPipeline(); | ||
|
||
pipeline.Add(new TextLoader<IrisData>(s_dataPath, useHeader: true, separator: "tab")); | ||
pipeline.Add(new ColumnConcatenator(outputColumn: "Features", | ||
"SepalLength", "SepalWidth", "PetalLength", "PetalWidth")); | ||
|
||
pipeline.Add(new StochasticDualCoordinateAscentClassifier()); | ||
|
||
PredictionModel<IrisData, IrisPrediction> model = pipeline.Train<IrisData, IrisPrediction>(); | ||
return model; | ||
} | ||
|
||
public class IrisData | ||
{ | ||
[Column("0")] | ||
public float Label; | ||
|
||
[Column("1")] | ||
public float SepalLength; | ||
|
||
[Column("2")] | ||
public float SepalWidth; | ||
|
||
[Column("3")] | ||
public float PetalLength; | ||
|
||
[Column("4")] | ||
public float PetalWidth; | ||
} | ||
|
||
public class IrisPrediction | ||
{ | ||
[ColumnName("Score")] | ||
public float[] PredictedLabels; | ||
} | ||
} | ||
} |
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.
Why is this an EXE and not a test?
I wonder would it be easier if we just have such tests as regular unit tests? from a dev perpective, we have nice tools to run and compare results for tests. #Pending
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.
BenchmarkDotNet (BDN) test are exes. I am pretty sure they cannot be dlls that are run as part of unit tests. I think @adamsitnik is working on infrastructure that will let us run the tests in the outer loop (daily runs) #Resolved