Skip to content

Commit 3456bb3

Browse files
Ivanidzo4kaTomFinley
authored andcommitted
Respect Marked exception during model loading. (#2574)
* Respect Marked exception during model loading.
1 parent 6196206 commit 3456bb3

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

src/Microsoft.ML.Core/ComponentModel/ComponentCatalog.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,25 @@ internal LoadableClassInfo(LoadableClassAttributeBase attr, MethodInfo getter, C
185185
internal object CreateInstanceCore(object[] ctorArgs)
186186
{
187187
Contracts.Assert(Utils.Size(ctorArgs) == CtorTypes.Length + ((RequireEnvironment) ? 1 : 0));
188-
189-
if (InstanceGetter != null)
188+
try
190189
{
191-
Contracts.Assert(Utils.Size(ctorArgs) == 0);
192-
return InstanceGetter.Invoke(null, null);
190+
if (InstanceGetter != null)
191+
{
192+
Contracts.Assert(Utils.Size(ctorArgs) == 0);
193+
return InstanceGetter.Invoke(null, null);
194+
}
195+
if (Constructor != null)
196+
return Constructor.Invoke(ctorArgs);
197+
if (CreateMethod != null)
198+
return CreateMethod.Invoke(null, ctorArgs);
199+
}
200+
catch (TargetInvocationException ex)
201+
{
202+
if (ex.InnerException != null && ex.InnerException.IsMarked())
203+
throw Contracts.Except(ex, "Error during class instantiation");
204+
else
205+
throw;
193206
}
194-
if (Constructor != null)
195-
return Constructor.Invoke(ctorArgs);
196-
if (CreateMethod != null)
197-
return CreateMethod.Invoke(null, ctorArgs);
198207
throw Contracts.Except("Can't instantiate class '{0}'", Type.Name);
199208
}
200209

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,10 @@ public static TransformerChain<ITransformer> LoadFrom(IHostEnvironment env, Stre
257257
ModelLoadContext.LoadModel<TransformerChain<ITransformer>, SignatureLoadModel>(env, out var transformerChain, rep, LoaderSignature);
258258
return transformerChain;
259259
}
260-
catch
260+
catch (FormatException ex)
261261
{
262+
if (!ex.IsMarked())
263+
throw;
262264
var chain = ModelFileUtils.LoadPipeline(env, stream, new MultiFileSource(null), extractInnerPipe: false);
263265
TransformerChain<ITransformer> transformChain = (chain as CompositeDataLoader).GetTransformer();
264266
var predictor = ModelFileUtils.LoadPredictorOrNull(env, stream);

src/Microsoft.ML.Transforms/LambdaTransform.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ private static ITransformer Create(IHostEnvironment env, ModelLoadContext ctx)
6969
var contractName = ctx.LoadString();
7070

7171
var composition = env.GetCompositionContainer();
72+
if (composition == null)
73+
throw Contracts.Except("Unable to get the MEF composition container");
7274
ITransformer transformer = composition.GetExportedValue<ITransformer>(contractName);
7375
return transformer;
7476
}

test/Microsoft.ML.Tests/Transformers/CustomMappingTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ public void TestCustomTransformer()
6969
TestEstimatorCore(customEst, data);
7070
Assert.True(false, "Cannot work without MEF injection");
7171
}
72-
catch (Exception)
72+
catch (InvalidOperationException ex)
7373
{
74-
// REVIEW: we should have a common mechanism that will make sure this is 'our' exception thrown.
74+
if (!ex.IsMarked())
75+
throw;
7576
}
7677
ML.CompositionContainer = new CompositionContainer(new TypeCatalog(typeof(MyLambda)));
7778
TestEstimatorCore(customEst, data);

0 commit comments

Comments
 (0)