@@ -27,61 +27,28 @@ internal sealed class TransformWrapper : ITransformer
2727
2828 private readonly IHost _host ;
2929 private readonly IDataView _xf ;
30- private readonly bool _allowSave ;
3130 private readonly bool _isRowToRowMapper ;
32- private readonly bool _useLastTransformOnly ;
3331
34- public TransformWrapper ( IHostEnvironment env , IDataView xf , bool allowSave = false , bool useLastTransformOnly = false )
32+ public TransformWrapper ( IHostEnvironment env , IDataView xf )
3533 {
3634 Contracts . CheckValue ( env , nameof ( env ) ) ;
3735 _host = env . Register ( nameof ( TransformWrapper ) ) ;
3836 _host . CheckValue ( xf , nameof ( xf ) ) ;
3937 _xf = xf ;
40- _allowSave = allowSave ;
4138 _isRowToRowMapper = IsChainRowToRowMapper ( _xf ) ;
42- _useLastTransformOnly = useLastTransformOnly ;
4339 }
4440
4541 public DataViewSchema GetOutputSchema ( DataViewSchema inputSchema )
4642 {
4743 _host . CheckValue ( inputSchema , nameof ( inputSchema ) ) ;
4844
4945 var dv = new EmptyDataView ( _host , inputSchema ) ;
50- var output = _useLastTransformOnly ? ApplyTransformUtils . ApplyTransformToData ( _host , ( IDataTransform ) _xf , dv ) :
51- ApplyTransformUtils . ApplyAllTransformsToData ( _host , _xf , dv ) ;
46+ var output = ApplyTransformUtils . ApplyTransformToData ( _host , ( IDataTransform ) _xf , dv ) ;
5247
5348 return output . Schema ;
5449 }
5550
56- void ICanSaveModel . Save ( ModelSaveContext ctx )
57- {
58- if ( ! _allowSave )
59- throw _host . Except ( "Saving is not permitted." ) ;
60- ctx . CheckAtModel ( ) ;
61- ctx . SetVersionInfo ( GetVersionInfo ( ) ) ;
62-
63- var dataPipe = _xf ;
64- var transforms = new List < IDataTransform > ( ) ;
65- while ( dataPipe is IDataTransform xf )
66- {
67- // REVIEW: a malicious user could construct a loop in the Source chain, that would
68- // cause this method to iterate forever (and throw something when the list overflows). There's
69- // no way to insulate from ALL malicious behavior.
70- transforms . Add ( xf ) ;
71- dataPipe = xf . Source ;
72- Contracts . AssertValue ( dataPipe ) ;
73- }
74- transforms . Reverse ( ) ;
75-
76- ctx . SaveSubModel ( "Loader" , c => BinaryLoader . SaveInstance ( _host , c , dataPipe . Schema ) ) ;
77-
78- ctx . Writer . Write ( transforms . Count ) ;
79- for ( int i = 0 ; i < transforms . Count ; i ++ )
80- {
81- var dirName = string . Format ( TransformDirTemplate , i ) ;
82- ctx . SaveModel ( transforms [ i ] , dirName ) ;
83- }
84- }
51+ void ICanSaveModel . Save ( ModelSaveContext ctx ) => throw _host . Except ( "Saving is not permitted." ) ;
8552
8653 private static VersionInfo GetVersionInfo ( )
8754 {
@@ -100,7 +67,6 @@ private TransformWrapper(IHostEnvironment env, ModelLoadContext ctx)
10067 Contracts . CheckValue ( env , nameof ( env ) ) ;
10168 _host = env . Register ( nameof ( TransformWrapper ) ) ;
10269 _host . CheckValue ( ctx , nameof ( ctx ) ) ;
103- _allowSave = true ;
10470 ctx . CheckAtModel ( GetVersionInfo ( ) ) ;
10571 int n = ctx . Reader . ReadInt32 ( ) ;
10672 _host . CheckDecode ( n >= 0 ) ;
@@ -119,8 +85,7 @@ private TransformWrapper(IHostEnvironment env, ModelLoadContext ctx)
11985 _isRowToRowMapper = IsChainRowToRowMapper ( _xf ) ;
12086 }
12187
122- public IDataView Transform ( IDataView input ) => _useLastTransformOnly ? ApplyTransformUtils . ApplyTransformToData ( _host , ( IDataTransform ) _xf , input ) :
123- ApplyTransformUtils . ApplyAllTransformsToData ( _host , _xf , input ) ;
88+ public IDataView Transform ( IDataView input ) => ApplyTransformUtils . ApplyTransformToData ( _host , ( IDataTransform ) _xf , input ) ;
12489
12590 private static bool IsChainRowToRowMapper ( IDataView view )
12691 {
@@ -137,30 +102,8 @@ private static bool IsChainRowToRowMapper(IDataView view)
137102 IRowToRowMapper ITransformer . GetRowToRowMapper ( DataViewSchema inputSchema )
138103 {
139104 _host . CheckValue ( inputSchema , nameof ( inputSchema ) ) ;
140- var input = new EmptyDataView ( _host , inputSchema ) ;
141- IDataView chain ;
142- if ( _useLastTransformOnly )
143- {
144- chain = ApplyTransformUtils . ApplyTransformToData ( _host , ( IDataTransform ) _xf , input ) ;
145- return new CompositeRowToRowMapper ( inputSchema , new [ ] { ( IRowToRowMapper ) chain } ) ;
146- }
147- else
148- {
149- var revMaps = new List < IRowToRowMapper > ( ) ;
150- for ( chain = ApplyTransformUtils . ApplyAllTransformsToData ( _host , _xf , input ) ;
151- chain is IDataTransform xf ;
152- chain = xf . Source )
153- {
154- // Everything in the chain ought to be a row mapper.
155- _host . Assert ( xf is IRowToRowMapper ) ;
156- revMaps . Add ( ( IRowToRowMapper ) xf ) ;
157- }
158-
159- // The walkback should have ended at the input.
160- Contracts . Assert ( chain == input ) ;
161- revMaps . Reverse ( ) ;
162- return new CompositeRowToRowMapper ( inputSchema , revMaps . ToArray ( ) ) ;
163- }
105+ return new CompositeRowToRowMapper ( inputSchema ,
106+ new [ ] { ( IRowToRowMapper ) ApplyTransformUtils . ApplyTransformToData ( _host , ( IDataTransform ) _xf , new EmptyDataView ( _host , inputSchema ) ) } ) ;
164107 }
165108 }
166109
0 commit comments