Skip to content

Commit 6526a01

Browse files
authored
ITransformer derives from ICanSaveModel and explicit implementation for ICanSaveModel (#2431)
1 parent cdc78df commit 6526a01

File tree

140 files changed

+279
-238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+279
-238
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using Microsoft.Data.DataView;
99
using Microsoft.ML.Data;
10+
using Microsoft.ML.Model;
1011

1112
namespace Microsoft.ML.Core.Data
1213
{
@@ -263,7 +264,7 @@ public interface IDataReaderEstimator<in TSource, out TReader>
263264
/// The transformer is a component that transforms data.
264265
/// It also supports 'schema propagation' to answer the question of 'how will the data with this schema look, after you transform it?'.
265266
/// </summary>
266-
public interface ITransformer
267+
public interface ITransformer : ICanSaveModel
267268
{
268269
/// <summary>
269270
/// Schema propagation for transformers.

src/Microsoft.ML.Data/Model/ModelHeader.cs renamed to src/Microsoft.ML.Core/Data/ModelHeader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
namespace Microsoft.ML.Model
1212
{
13-
[StructLayout(LayoutKind.Explicit, Size = ModelHeader.Size)]
13+
[BestFriend]
14+
[StructLayout(LayoutKind.Explicit, Size = Size)]
1415
internal struct ModelHeader
1516
{
1617
/// <summary>

src/Microsoft.ML.Data/Model/ModelLoading.cs renamed to src/Microsoft.ML.Core/Data/ModelLoading.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
namespace Microsoft.ML.Model
1212
{
13+
/// <summary>
14+
/// Signature for a repository based model loader. This is the dual of <see cref="ICanSaveModel"/>.
15+
/// </summary>
16+
[BestFriend]
17+
internal delegate void SignatureLoadModel(ModelLoadContext ctx);
18+
1319
public sealed partial class ModelLoadContext : IDisposable
1420
{
1521
public const string ModelStreamName = "Model.key";

src/Microsoft.ML.Data/Model/Repository.cs renamed to src/Microsoft.ML.Core/Data/Repository.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010

1111
namespace Microsoft.ML.Model
1212
{
13-
/// <summary>
14-
/// Signature for a repository based model loader. This is the dual of ICanSaveModel.
15-
/// </summary>
16-
public delegate void SignatureLoadModel(ModelLoadContext ctx);
17-
1813
/// <summary>
1914
/// For saving a model into a repository.
15+
/// Classes implementing <see cref="ICanSaveModel"/> should do an explicit implementation of <see cref="Save(ModelSaveContext)"/>.
16+
/// Classes inheriting <see cref="ICanSaveModel"/> from a base class should overwrite the function invoked by <see cref="Save(ModelSaveContext)"/>
17+
/// in that base class, if there is one.
2018
/// </summary>
2119
public interface ICanSaveModel
2220
{
@@ -293,6 +291,8 @@ protected Entry AddEntry(string pathEnt, Stream stream)
293291

294292
public sealed class RepositoryWriter : Repository
295293
{
294+
private const string DirTrainingInfo = "TrainingInfo";
295+
296296
private ZipArchive _archive;
297297
private Queue<KeyValuePair<string, Stream>> _closed;
298298

@@ -301,7 +301,7 @@ public static RepositoryWriter CreateNew(Stream stream, IExceptionContext ectx =
301301
Contracts.CheckValueOrNull(ectx);
302302
ectx.CheckValue(stream, nameof(stream));
303303
var rep = new RepositoryWriter(stream, ectx, useFileSystem);
304-
using (var ent = rep.CreateEntry(ModelFileUtils.DirTrainingInfo, "Version.txt"))
304+
using (var ent = rep.CreateEntry(DirTrainingInfo, "Version.txt"))
305305
using (var writer = Utils.OpenWriter(ent.Stream))
306306
writer.WriteLine(typeof(RepositoryWriter).Assembly.GetName().Version);
307307
return rep;

src/Microsoft.ML.Data/DataLoadSave/Binary/BinaryLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ private static Stream OpenStream(string filename)
942942
return OpenStream(files);
943943
}
944944

945-
public void Save(ModelSaveContext ctx)
945+
void ICanSaveModel.Save(ModelSaveContext ctx)
946946
{
947947
_host.CheckValue(ctx, nameof(ctx));
948948
ctx.CheckAtModel();

src/Microsoft.ML.Data/DataLoadSave/CompositeDataLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ private static IDataLoader LoadTransforms(ModelLoadContext ctx, IDataLoader srcL
502502
});
503503
}
504504

505-
public void Save(ModelSaveContext ctx)
505+
void ICanSaveModel.Save(ModelSaveContext ctx)
506506
{
507507
_host.CheckValue(ctx, nameof(ctx));
508508
ctx.CheckAtModel();

src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ public Bindings(ModelLoadContext ctx, TextLoader parent)
834834
OutputSchema = ComputeOutputSchema();
835835
}
836836

837-
public void Save(ModelSaveContext ctx)
837+
internal void Save(ModelSaveContext ctx)
838838
{
839839
Contracts.AssertValue(ctx);
840840

@@ -1283,7 +1283,7 @@ internal static IDataLoader Create(IHostEnvironment env, Arguments args, IMultiS
12831283
internal static IDataView ReadFile(IHostEnvironment env, Arguments args, IMultiStreamSource fileSource)
12841284
=> new TextLoader(env, args, fileSource).Read(fileSource);
12851285

1286-
public void Save(ModelSaveContext ctx)
1286+
void ICanSaveModel.Save(ModelSaveContext ctx)
12871287
{
12881288
_host.CheckValue(ctx, nameof(ctx));
12891289
ctx.CheckAtModel();
@@ -1420,7 +1420,7 @@ public RowCursor[] GetRowCursorSet(IEnumerable<Schema.Column> columnsNeeded, int
14201420
return Cursor.CreateSet(_reader, _files, active, n);
14211421
}
14221422

1423-
public void Save(ModelSaveContext ctx) => _reader.Save(ctx);
1423+
void ICanSaveModel.Save(ModelSaveContext ctx) => ((ICanSaveModel)_reader).Save(ctx);
14241424
}
14251425
}
14261426
}

0 commit comments

Comments
 (0)