Skip to content

Commit 14dadfe

Browse files
authored
Loading old model files is broken. (#1290)
* Loading old model files is broken. Older model files are failing to load due to FpTail checks in the model validation code. These checks weren't happening in the old model loading code, and they now fail on some older models. Fix this by returning early when it is an older model, and there are no strings. This preserves the old behavior. Fix #1289
1 parent 9157cea commit 14dadfe

File tree

4 files changed

+82
-12
lines changed

4 files changed

+82
-12
lines changed

build/Dependencies.props

+30-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
<Project>
2+
3+
<!-- Core Product Dependencies -->
24
<PropertyGroup>
3-
<GoogleProtobufPackageVersion>3.5.1</GoogleProtobufPackageVersion>
45
<NewtonsoftJsonPackageVersion>10.0.3</NewtonsoftJsonPackageVersion>
5-
<ParquetDotNetPackageVersion>2.1.3</ParquetDotNetPackageVersion>
6-
<SystemThreadingTasksDataflowPackageVersion>4.8.0</SystemThreadingTasksDataflowPackageVersion>
76
<SystemCodeDomPackageVersion>4.4.0</SystemCodeDomPackageVersion>
7+
<SystemCollectionsImmutableVersion>1.5.0</SystemCollectionsImmutableVersion>
8+
<SystemMemoryVersion>4.5.1</SystemMemoryVersion>
89
<SystemReflectionEmitLightweightPackageVersion>4.3.0</SystemReflectionEmitLightweightPackageVersion>
9-
<PublishSymbolsPackageVersion>1.0.0-beta-62824-02</PublishSymbolsPackageVersion>
10+
<SystemThreadingTasksDataflowPackageVersion>4.8.0</SystemThreadingTasksDataflowPackageVersion>
11+
</PropertyGroup>
12+
13+
<!-- Other/Non-Core Product Dependencies -->
14+
<PropertyGroup>
15+
<GoogleProtobufPackageVersion>3.5.1</GoogleProtobufPackageVersion>
1016
<LightGBMPackageVersion>2.2.1.1</LightGBMPackageVersion>
17+
<MicrosoftMLScoring>1.1.0</MicrosoftMLScoring>
1118
<MlNetMklDepsPackageVersion>0.0.0.7</MlNetMklDepsPackageVersion>
19+
<ParquetDotNetPackageVersion>2.1.3</ParquetDotNetPackageVersion>
1220
<SystemDrawingCommonPackageVersion>4.5.0</SystemDrawingCommonPackageVersion>
13-
<BenchmarkDotNetVersion>0.11.1</BenchmarkDotNetVersion>
21+
<SystemIOFileSystemAccessControl>4.5.0</SystemIOFileSystemAccessControl>
22+
<SystemSecurityPrincipalWindows>4.5.0</SystemSecurityPrincipalWindows>
1423
<TensorFlowVersion>1.10.0</TensorFlowVersion>
15-
<SystemCollectionsImmutableVersion>1.5.0</SystemCollectionsImmutableVersion>
16-
<SystemMemoryVersion>4.5.1</SystemMemoryVersion>
24+
</PropertyGroup>
25+
26+
<!-- Code Analyzer Dependencies -->
27+
<PropertyGroup>
1728
<MicrosoftCodeAnalysisCSharpVersion>2.9.0</MicrosoftCodeAnalysisCSharpVersion>
1829
<MicrosoftCSharpVersion>4.5.0</MicrosoftCSharpVersion>
1930
<SystemCompositionVersion>1.2.0</SystemCompositionVersion>
20-
<MicrosoftMLScoring>1.1.0</MicrosoftMLScoring>
21-
<SystemIOFileSystemAccessControl>4.5.0</SystemIOFileSystemAccessControl>
22-
<SystemSecurityPrincipalWindows>4.5.0</SystemSecurityPrincipalWindows>
2331
</PropertyGroup>
32+
33+
<!-- Build/infrastructure Dependencies -->
34+
<PropertyGroup>
35+
<PublishSymbolsPackageVersion>1.0.0-beta-62824-02</PublishSymbolsPackageVersion>
36+
</PropertyGroup>
37+
38+
<!-- Test-only Dependencies -->
39+
<PropertyGroup>
40+
<BenchmarkDotNetVersion>0.11.1</BenchmarkDotNetVersion>
41+
<MicrosoftMLTestModelsPackageVersion>0.0.2-test</MicrosoftMLTestModelsPackageVersion>
42+
</PropertyGroup>
43+
2444
</Project>

src/Microsoft.ML.Data/Model/ModelHeader.cs

+12
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,18 @@ public static bool TryValidate(ref ModelHeader header, BinaryReader reader, long
483483
{
484484
// No strings.
485485
strings = null;
486+
487+
if (header.VerWritten < VerAssemblyNameSupported)
488+
{
489+
// Before VerAssemblyNameSupported, if there were no strings in the model,
490+
// validation ended here. Specifically the FpTail checks below were skipped.
491+
// There are earlier versions of models that don't have strings, and 'reader' is
492+
// not at FpTail at this point.
493+
// Preserve the previous behavior by returning early here.
494+
loaderAssemblyName = null;
495+
ex = null;
496+
return true;
497+
}
486498
}
487499
else
488500
{

test/Microsoft.ML.Core.Tests/Microsoft.ML.Core.Tests.csproj

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<DefineConstants>CORECLR</DefineConstants>
44
</PropertyGroup>
5-
5+
66
<ItemGroup>
77
<ProjectReference Include="..\..\src\Microsoft.ML.HalLearners\Microsoft.ML.HalLearners.csproj" />
88
<ProjectReference Include="..\..\src\Microsoft.ML.Api\Microsoft.ML.Api.csproj" />
@@ -22,7 +22,11 @@
2222
<ProjectReference Include="..\..\src\Microsoft.ML.Legacy\Microsoft.ML.Legacy.csproj" />
2323
<ProjectReference Include="..\Microsoft.ML.TestFramework\Microsoft.ML.TestFramework.csproj" />
2424
<ProjectReference Include="..\Microsoft.ML.Tests\Microsoft.ML.Tests.csproj" />
25-
<PackageReference Include="MlNetMklDeps" Version="$(MlNetMklDepsPackageVersion)" />
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<PackageReference Include="MlNetMklDeps" Version="$(MlNetMklDepsPackageVersion)" />
29+
<PackageReference Include="Microsoft.ML.TestModels" Version="$(MicrosoftMLTestModelsPackageVersion)" />
2630
</ItemGroup>
2731

2832
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.ML.Runtime.Data;
6+
using Microsoft.ML.Runtime.Model;
7+
using Microsoft.ML.TestFramework;
8+
using System.IO;
9+
using Xunit;
10+
11+
namespace Microsoft.ML.Runtime.RunTests
12+
{
13+
public class TestModelLoad
14+
{
15+
/// <summary>
16+
/// Tests loading a model file that was saved using an older version still loads correctly.
17+
/// </summary>
18+
[Fact]
19+
public void LoadOriginalBinaryLoaderModel()
20+
{
21+
using (var env = new LocalEnvironment()
22+
.AddStandardComponents())
23+
using (var modelStream = File.OpenRead(Path.Combine("TestModels", "BinaryLoader-v3.11.0.0.zip")))
24+
using (var rep = RepositoryReader.Open(modelStream, env))
25+
{
26+
IDataLoader result = ModelFileUtils.LoadLoader(env, rep, new MultiFileSource(null), true);
27+
28+
Assert.Equal(2, result.Schema.ColumnCount);
29+
Assert.Equal("Image", result.Schema[0].Name);
30+
Assert.Equal("Class", result.Schema[1].Name);
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)