Skip to content

Internalize ITransformTemplate #2334

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion src/Microsoft.ML.Data/Commands/ScoreCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace Microsoft.ML.Data
{
using TScorerFactory = IComponentFactory<IDataView, ISchemaBoundMapper, RoleMappedSchema, IDataScorerTransform>;

public interface IDataScorerTransform : IDataTransform, ITransformTemplate
[BestFriend]
internal interface IDataScorerTransform : IDataTransform, ITransformTemplate
{
}

Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.ML.Data/Data/IDataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public interface IDataTransform : IDataView, ICanSaveModel
/// Data transforms need to be able to apply themselves to a different input IDataView.
/// This interface allows them to implement custom rebinding logic.
Copy link
Contributor

Choose a reason for hiding this comment

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

logic [](start = 65, length = 5)

Until we can delete this altogether, it may be worthwhile to add a note here to explain that the preferred idioms is that things work via the new ITransformer mechanism, which is the successor of this sort of idiom. I might not suggest adding the [Obsolete] tag -- that's a bit heaviweight -- just something in the XML docstring here like, "yeah, yeah, this is here, but we're trying to get rid of it, don't write new code with it."

/// </summary>
public interface ITransformTemplate : IDataTransform
[BestFriend]
internal interface ITransformTemplate : IDataTransform
{
// REVIEW: re-apply operation should support shallow schema modification,
// like renaming source and destination columns.
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Data/DataView/RowToRowMapperTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public Row GetRow(Row input, Func<int, bool> active)
}
}

public IDataTransform ApplyToData(IHostEnvironment env, IDataView newSource)
IDataTransform ITransformTemplate.ApplyToData(IHostEnvironment env, IDataView newSource)
{
Contracts.CheckValue(env, nameof(env));

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Data/Scorers/BinaryClassifierScorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private protected override void SaveAsOnnxCore(OnnxContext ctx)
}
}

public override IDataTransform ApplyToData(IHostEnvironment env, IDataView newSource)
private protected override IDataTransform ApplyToDataCore(IHostEnvironment env, IDataView newSource)
{
Contracts.CheckValue(env, nameof(env));
env.CheckValue(newSource, nameof(newSource));
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Data/Scorers/ClusteringScorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private protected override void SaveCore(ModelSaveContext ctx)
base.SaveCore(ctx);
}

public override IDataTransform ApplyToData(IHostEnvironment env, IDataView newSource)
private protected override IDataTransform ApplyToDataCore(IHostEnvironment env, IDataView newSource)
{
Contracts.CheckValue(env, nameof(env));
Contracts.CheckValue(newSource, nameof(newSource));
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.ML.Data/Scorers/GenericScorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ internal GenericScorer(IHostEnvironment env, ScorerArgumentsBase args, IDataView
}

/// <summary>
/// Constructor for <see cref="ApplyToData"/> method.
/// Constructor for <see cref="ApplyToDataCore"/> method.
/// </summary>
private GenericScorer(IHostEnvironment env, GenericScorer transform, IDataView data)
: base(env, data, RegistrationName, transform.Bindable)
Expand Down Expand Up @@ -254,7 +254,7 @@ protected override bool WantParallelCursors(Func<int, bool> predicate)
return _bindings.AnyNewColumnsActive(predicate);
}

public override IDataTransform ApplyToData(IHostEnvironment env, IDataView newSource)
private protected override IDataTransform ApplyToDataCore(IHostEnvironment env, IDataView newSource)
{
Host.CheckValue(env, nameof(env));
Host.CheckValue(newSource, nameof(newSource));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ private protected override void SaveCore(ModelSaveContext ctx)
base.SaveCore(ctx);
}

public override IDataTransform ApplyToData(IHostEnvironment env, IDataView newSource)
private protected override IDataTransform ApplyToDataCore(IHostEnvironment env, IDataView newSource)
{
Contracts.CheckValue(env, nameof(env));
Contracts.CheckValue(newSource, nameof(newSource));
Expand Down
10 changes: 6 additions & 4 deletions src/Microsoft.ML.Data/Scorers/RowToRowScorerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ public sealed override void Save(ModelSaveContext ctx)
[BestFriend]
private protected abstract void SaveCore(ModelSaveContext ctx);

/// <summary>
/// For the ITransformTemplate implementation.
/// </summary>
public abstract IDataTransform ApplyToData(IHostEnvironment env, IDataView newSource);
private protected abstract IDataTransform ApplyToDataCore(IHostEnvironment env, IDataView newSource);

IDataTransform ITransformTemplate.ApplyToData(IHostEnvironment env, IDataView newSource)
=> ApplyToDataCore(env, newSource);
internal IDataView ApplyToData(IHostEnvironment env, IDataView newSource)
=> ApplyToDataCore(env, newSource);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need this second method? IDataTransform derives from IDataView.


/// <summary>
/// Derived classes provide the specific bindings object.
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Data/Transforms/ColumnSelecting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ public Row GetRow(Row input, Func<int, bool> active)
return new RowImpl(input, _mapper);
}

public IDataTransform ApplyToData(IHostEnvironment env, IDataView newSource)
IDataTransform ITransformTemplate.ApplyToData(IHostEnvironment env, IDataView newSource)
Copy link
Contributor

Choose a reason for hiding this comment

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

ITransformTemplate [](start = 27, length = 18)

I agree with you that this is probably the best policy since it even discourages internal code from using this now deprecated idiom, but just in case you are curious, it is not necessary to do this here since this nested class (SelectColumnsDataTransform) is itself not public.

=> new SelectColumnsDataTransform(env, _transform, new Mapper(_transform, newSource.Schema), newSource);
}

Expand Down
6 changes: 2 additions & 4 deletions src/Microsoft.ML.Data/Transforms/SkipTakeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ private SkipTakeFilter(long skip, long take, IHostEnvironment env, IDataView inp
_take = take;
}

public IDataTransform ApplyToData(IHostEnvironment env, IDataView newSource)
{
return new SkipTakeFilter(_skip, _take, env, newSource);
}
IDataTransform ITransformTemplate.ApplyToData(IHostEnvironment env, IDataView newSource)
=> new SkipTakeFilter(_skip, _take, env, newSource);

public static SkipTakeFilter Create(IHostEnvironment env, Arguments args, IDataView input)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.TimeSeries/SequentialTransformerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public override void Save(ModelSaveContext ctx)
_parent.Save(ctx);
}

public IDataTransform ApplyToData(IHostEnvironment env, IDataView newSource)
IDataTransform ITransformTemplate.ApplyToData(IHostEnvironment env, IDataView newSource)
{
return new SequentialDataTransform(Contracts.CheckRef(env, nameof(env)).Register("SequentialDataTransform"), _parent, newSource, _mapper);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.ML.Transforms/LambdaTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ namespace Microsoft.ML.Transforms
/// <summary>
/// Utility class for creating transforms easily.
/// </summary>
public static class LambdaTransform
[BestFriend]
internal static class LambdaTransform
{
/// <summary>
/// A delegate type to create a persistent transform, utilized by the creation functions
Expand All @@ -33,7 +34,7 @@ public static class LambdaTransform
/// <param name="input">The dataview this transform should be persisted on</param>
/// <returns>A transform of the input data, as parameterized by the binary input
/// stream</returns>
public delegate ITransformTemplate LoadDelegate(BinaryReader reader, IHostEnvironment env, IDataView input);
internal delegate ITransformTemplate LoadDelegate(BinaryReader reader, IHostEnvironment env, IDataView input);

internal const string LoaderSignature = "CustomTransformer";

Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.ML.Transforms/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.RServerScoring.NeuralNetworks" + InternalPublicKey.Value)]
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.Runtime.TextAnalytics" + InternalPublicKey.Value)]
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.RServerScoring.TextAnalytics" + InternalPublicKey.Value)]
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.TimeSeries" + PublicKey.Value)]
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.Tests" + PublicKey.TestValue)]

[assembly: WantsToBeBestFriends]
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Transforms/StatefulFilterTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public RowCursor[] GetRowCursorSet(IEnumerable<Schema.Column> columnsNeeded, int

public IDataView Source => _source;

public IDataTransform ApplyToData(IHostEnvironment env, IDataView newSource)
IDataTransform ITransformTemplate.ApplyToData(IHostEnvironment env, IDataView newSource)
{
Contracts.CheckValue(env, nameof(env));
env.CheckValue(newSource, nameof(newSource));
Expand Down