Skip to content
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

[AutoML] Pull out Code Gen as separate library plus some changes in CodeGen #4043

Merged
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a22e044
add CodeGen Library
LittleLittleCloud Jul 26, 2019
df78359
rename namespace to ML.CodeGen.*
LittleLittleCloud Jul 26, 2019
0447618
cancel delay sign in CodeGen
LittleLittleCloud Jul 26, 2019
116a490
update based on comment
LittleLittleCloud Jul 29, 2019
b2844f2
remove useless nuget package
LittleLittleCloud Aug 2, 2019
561a125
add ComsumeModel class
LittleLittleCloud Aug 8, 2019
248a448
use consumeModel in CodeGen
LittleLittleCloud Aug 8, 2019
97e9342
use different annotation for different target
LittleLittleCloud Aug 8, 2019
e143c07
target to netstandard2.0
LittleLittleCloud Aug 8, 2019
3c7df9a
remove console output
LittleLittleCloud Aug 8, 2019
b843777
adjust output result
LittleLittleCloud Aug 8, 2019
68a8f20
remove useless variable
LittleLittleCloud Aug 9, 2019
feff43b
update CodeGen name to CodeGenerator
LittleLittleCloud Aug 12, 2019
b3fa154
rebase to latest branch
LittleLittleCloud Aug 15, 2019
ca783fb
fix bug in Normalize function
LittleLittleCloud Aug 15, 2019
a847f05
fix test
LittleLittleCloud Aug 16, 2019
99a6c8b
rename features to featuresList
LittleLittleCloud Aug 19, 2019
5d3ce64
move enum out of class
LittleLittleCloud Aug 19, 2019
4afa173
remove useless items
LittleLittleCloud Aug 19, 2019
b1799b4
remove NLog.config
LittleLittleCloud Aug 19, 2019
3a88866
update generated CSharp file
LittleLittleCloud Aug 19, 2019
dc46363
change wording and delete useless file
LittleLittleCloud Aug 20, 2019
183b07d
using Uppercase in comment
LittleLittleCloud Aug 23, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Microsoft.ML.AutoML.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.InternalCodeAn
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools-local", "tools-local", "{2DEB692B-D982-4301-A074-DAA095658230}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.CodeGenerator", "src\Microsoft.ML.CodeGen\Microsoft.ML.CodeGenerator.csproj", "{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -98,6 +100,18 @@ Global
{954F14B0-BBDF-4A30-811E-2D86FDB5B399}.Release-Intrinsics|Any CPU.Build.0 = Release-Intrinsics|Any CPU
{954F14B0-BBDF-4A30-811E-2D86FDB5B399}.Release-netfx|Any CPU.ActiveCfg = Release-netfx|Any CPU
{954F14B0-BBDF-4A30-811E-2D86FDB5B399}.Release-netfx|Any CPU.Build.0 = Release-netfx|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Debug-Intrinsics|Any CPU.ActiveCfg = Debug-Intrinsics|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Debug-Intrinsics|Any CPU.Build.0 = Debug-Intrinsics|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Debug-netfx|Any CPU.ActiveCfg = Debug-netfx|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Debug-netfx|Any CPU.Build.0 = Debug-netfx|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Release|Any CPU.Build.0 = Release|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Release-Intrinsics|Any CPU.ActiveCfg = Release-Intrinsics|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Release-Intrinsics|Any CPU.Build.0 = Release-Intrinsics|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Release-netfx|Any CPU.ActiveCfg = Release-netfx|Any CPU
{3148A8CF-A686-4A9C-A5C6-C3E41CAAD64D}.Release-netfx|Any CPU.Build.0 = Release-netfx|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.ML.AutoML/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
[assembly: InternalsVisibleTo("mlnet, PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb")]
[assembly: InternalsVisibleTo("mlnet.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010015c01ae1f50e8cc09ba9eac9147cf8fd9fce2cfe9f8dce4f7301c4132ca9fb50ce8cbf1df4dc18dd4d210e4345c744ecb3365ed327efdbc52603faa5e21daa11234c8c4a73e51f03bf192544581ebe107adee3a34928e39d04e524a9ce729d5090bfd7dad9d10c722c0def9ccc08ff0a03790e48bcd1f9b6c476063e1966a1c4")]
[assembly: InternalsVisibleTo("Benchmark, PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb")]
[assembly: InternalsVisibleTo("Microsoft.ML.CodeGenerator, PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb")]
[assembly: InternalsVisibleTo("Microsoft.ML.ModelBuilder, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]
10 changes: 10 additions & 0 deletions src/Microsoft.ML.CodeGen/Assembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.ML.CodeGen.Test, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]
[assembly: InternalsVisibleTo("Microsoft.ML.ModelBuilder, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]
[assembly: InternalsVisibleTo("mlnet, PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb")]
Dmitry-A marked this conversation as resolved.
Show resolved Hide resolved
[assembly:InternalsVisibleTo("mlnet.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010015c01ae1f50e8cc09ba9eac9147cf8fd9fce2cfe9f8dce4f7301c4132ca9fb50ce8cbf1df4dc18dd4d210e4345c744ecb3365ed327efdbc52603faa5e21daa11234c8c4a73e51f03bf192544581ebe107adee3a34928e39d04e524a9ce729d5090bfd7dad9d10c722c0def9ccc08ff0a03790e48bcd1f9b6c476063e1966a1c4")]
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.ML.AutoML;
using Microsoft.ML.CLI.Templates.Console;
using Microsoft.ML.CLI.Utilities;
using Microsoft.ML.CodeGenerator.Templates.Console;
using Microsoft.ML.CodeGenerator.Utilities;

namespace Microsoft.ML.CLI.CodeGenerator.CSharp
namespace Microsoft.ML.CodeGenerator.CSharp
{
internal class CodeGenerator : IProjectGenerator
public class CodeGenerator : IProjectGenerator
{
private readonly Pipeline _pipeline;
private readonly CodeGeneratorSettings _settings;
Expand Down Expand Up @@ -55,6 +55,7 @@ public void GenerateOutput()

Utils.WriteOutputToFiles(modelProjectContents.ModelInputCSFileContent, "ModelInput.cs", dataModelsDir);
Utils.WriteOutputToFiles(modelProjectContents.ModelOutputCSFileContent, "ModelOutput.cs", dataModelsDir);
Utils.WriteOutputToFiles(modelProjectContents.ConsumeModelCSFileContent, "ConsumeModel.cs", dataModelsDir);
Utils.WriteOutputToFiles(modelProjectContents.ModelProjectFileContent, modelProjectName, modelprojectDir);

// Generate ConsoleApp Project
Expand Down Expand Up @@ -106,7 +107,7 @@ private void SetRequiredNugetPackages(IEnumerable<PipelineNode> trainerNodes, re
var predictProgramCSFileContent = GeneratePredictProgramCSFileContent(namespaceValue);
predictProgramCSFileContent = Utils.FormatCode(predictProgramCSFileContent);

var predictProjectFileContent = GeneratPredictProjectFileContent(namespaceValue, includeLightGbmPackage, includeMklComponentsPackage, includeFastTreePackage);
var predictProjectFileContent = GeneratPredictProjectFileContent(_settings.OutputName, includeLightGbmPackage, includeMklComponentsPackage, includeFastTreePackage);

var transformsAndTrainers = GenerateTransformsAndTrainers();
var modelBuilderCSFileContent = GenerateModelBuilderCSFileContent(transformsAndTrainers.Usings, transformsAndTrainers.TrainerMethod, transformsAndTrainers.PreTrainerTransforms, transformsAndTrainers.PostTrainerTransforms, namespaceValue, _pipeline.CacheBeforeTrainer, labelTypeCsharp.Name);
Expand All @@ -115,15 +116,33 @@ private void SetRequiredNugetPackages(IEnumerable<PipelineNode> trainerNodes, re
return (predictProgramCSFileContent, predictProjectFileContent, modelBuilderCSFileContent);
}

internal (string ModelInputCSFileContent, string ModelOutputCSFileContent, string ModelProjectFileContent) GenerateModelProjectContents(string namespaceValue, Type labelTypeCsharp, bool includeLightGbmPackage, bool includeMklComponentsPackage, bool includeFastTreePackage)
internal (string ModelInputCSFileContent, string ModelOutputCSFileContent, string ConsumeModelCSFileContent, string ModelProjectFileContent) GenerateModelProjectContents(string namespaceValue, Type labelTypeCsharp, bool includeLightGbmPackage, bool includeMklComponentsPackage, bool includeFastTreePackage)
{
var classLabels = GenerateClassLabels();

// generate ModelInput.cs
var modelInputCSFileContent = GenerateModelInputCSFileContent(namespaceValue, classLabels);
modelInputCSFileContent = Utils.FormatCode(modelInputCSFileContent);

// generate ModelOutput.cs
var modelOutputCSFileContent = GenerateModelOutputCSFileContent(labelTypeCsharp.Name, namespaceValue);
modelOutputCSFileContent = Utils.FormatCode(modelOutputCSFileContent);

// generate ConsumeModel.cs
var consumeModelCSFileContent = GenerateConsumeModelCSFileContent(namespaceValue);
consumeModelCSFileContent = Utils.FormatCode(consumeModelCSFileContent);
var modelProjectFileContent = GenerateModelProjectFileContent(includeLightGbmPackage, includeMklComponentsPackage, includeFastTreePackage);
return (modelInputCSFileContent, modelOutputCSFileContent, modelProjectFileContent);
return (modelInputCSFileContent, modelOutputCSFileContent, consumeModelCSFileContent, modelProjectFileContent);
}

private string GenerateConsumeModelCSFileContent(string namespaceValue)
{
ConsumeModel consumeModel = new ConsumeModel()
{
Namespace = namespaceValue,
Target = _settings.Target,
};
return consumeModel.TransformText();
}

internal (string Usings, string TrainerMethod, List<string> PreTrainerTransforms, List<string> PostTrainerTransforms) GenerateTransformsAndTrainers()
Expand Down Expand Up @@ -262,13 +281,22 @@ private static string GenerateModelProjectFileContent(bool includeLightGbmPackag

private string GenerateModelOutputCSFileContent(string predictionLabelType, string namespaceValue)
{
ModelOutputClass modelOutputClass = new ModelOutputClass() { TaskType = _settings.MlTask.ToString(), PredictionLabelType = predictionLabelType, Namespace = namespaceValue };
ModelOutputClass modelOutputClass = new ModelOutputClass() {
TaskType = _settings.MlTask.ToString(),
PredictionLabelType = predictionLabelType,
Namespace = namespaceValue,
Target = _settings.Target,
};
return modelOutputClass.TransformText();
}

private string GenerateModelInputCSFileContent(string namespaceValue, IList<string> classLabels)
{
ModelInputClass modelInputClass = new ModelInputClass() { Namespace = namespaceValue, ClassLabels = classLabels };
ModelInputClass modelInputClass = new ModelInputClass() {
Namespace = namespaceValue,
ClassLabels = classLabels,
Target = _settings.Target,
};
return modelInputClass.TransformText();
}
#endregion
Expand All @@ -282,6 +310,8 @@ private static string GeneratPredictProjectFileContent(string namespaceValue, bo

private string GeneratePredictProgramCSFileContent(string namespaceValue)
{
var columns = _columnInferenceResult.TextLoaderOptions.Columns;
var featuresList = columns.Where((str) => str.Name != _settings.LabelName).Select((str) => str.Name).ToList();
PredictProgram predictProgram = new PredictProgram()
{
TaskType = _settings.MlTask.ToString(),
Expand All @@ -293,6 +323,8 @@ private string GeneratePredictProgramCSFileContent(string namespaceValue)
Separator = _columnInferenceResult.TextLoaderOptions.Separators.FirstOrDefault(),
AllowQuoting = _columnInferenceResult.TextLoaderOptions.AllowQuoting,
AllowSparse = _columnInferenceResult.TextLoaderOptions.AllowSparse,
Features = featuresList,
Target = _settings.Target,
};
return predictProgram.TransformText();
}
Expand Down Expand Up @@ -321,6 +353,7 @@ private string GenerateModelBuilderCSFileContent(string usings,
Namespace = namespaceValue,
LabelName = _settings.LabelName,
CacheBeforeTrainer = cacheBeforeTrainer,
Target = _settings.Target,
};

return modelBuilder.TransformText();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 Microsoft.ML.AutoML;

namespace Microsoft.ML.CodeGenerator.CSharp
{
public class CodeGeneratorSettings
{
public CodeGeneratorSettings()
{
// set default value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment style in ML.NET is to begin comment with an uppercase character

Suggested change
// set default value
// Set default value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#resolved

Target = GenerateTarget.Cli;
}

public string LabelName { get; set; }

public string ModelPath { get; set; }

public string OutputName { get; set; }

public string OutputBaseDir { get; set; }

public string TrainDataset { get; set; }

public string TestDataset { get; set; }

public GenerateTarget Target { get; set; }

internal TaskKind MlTask { get; set; }

}

public enum GenerateTarget
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seem to be used only to print the right annotation for the platform. I might recommend just having a string field here for the annotation text.

This would address my other comment: https://github.com/dotnet/machinelearning/pull/4043/files#r317240647

{
ModelBuilder = 0,
Cli = 1,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Microsoft.ML.CLI.CodeGenerator.CSharp
namespace Microsoft.ML.CodeGenerator.CSharp
{
internal static class Symbols
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Text;
using Microsoft.ML.AutoML;

namespace Microsoft.ML.CLI.CodeGenerator.CSharp
namespace Microsoft.ML.CodeGenerator.CSharp
{
/// <summary>
/// Supports generation of code for trainers (Binary,Multi,Regression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

using System;
using Microsoft.ML.AutoML;
using static Microsoft.ML.CLI.CodeGenerator.CSharp.TrainerGenerators;
using static Microsoft.ML.CodeGenerator.CSharp.TrainerGenerators;

namespace Microsoft.ML.CLI.CodeGenerator.CSharp
namespace Microsoft.ML.CodeGenerator.CSharp
{
internal interface ITrainerGenerator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Text;
using Microsoft.ML.AutoML;

namespace Microsoft.ML.CLI.CodeGenerator.CSharp
namespace Microsoft.ML.CodeGenerator.CSharp
{
internal static class TrainerGenerators
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using Microsoft.ML.AutoML;

namespace Microsoft.ML.CLI.CodeGenerator.CSharp
namespace Microsoft.ML.CodeGenerator.CSharp
{
/// <summary>
/// Supports generation of code for trainers (Binary,Multi,Regression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System;
using Microsoft.ML.AutoML;

namespace Microsoft.ML.CLI.CodeGenerator.CSharp
namespace Microsoft.ML.CodeGenerator.CSharp
{
internal interface ITransformGenerator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Text;
using Microsoft.ML.AutoML;

namespace Microsoft.ML.CLI.CodeGenerator.CSharp
namespace Microsoft.ML.CodeGenerator.CSharp
{
internal class Normalizer : TransformGeneratorBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Microsoft.ML.CLI.CodeGenerator
namespace Microsoft.ML.CodeGenerator
{
internal interface IProjectGenerator
{
Expand Down
Loading