Skip to content

Commit d69ce6c

Browse files
author
Chris Elion
authored
[MLA-1981] Don't allow connections to newer python trainers (#5370)
1 parent 70bac63 commit d69ce6c

File tree

7 files changed

+90
-13
lines changed

7 files changed

+90
-13
lines changed

Project/Packages/manifest.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"com.unity.analytics": "3.2.3",
55
"com.unity.collab-proxy": "1.2.15",
66
"com.unity.ml-agents": "file:../../com.unity.ml-agents",
7-
"com.unity.package-manager-ui": "2.0.8",
7+
"com.unity.package-manager-ui": "2.0.13",
88
"com.unity.package-validation-suite": "0.7.15-preview",
9-
"com.unity.purchasing": "2.0.3",
9+
"com.unity.purchasing": "2.2.1",
1010
"com.unity.textmeshpro": "1.4.1",
1111
"com.unity.modules.ai": "1.0.0",
1212
"com.unity.modules.animation": "1.0.0",
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
m_EditorVersion: 2018.4.17f1
1+
m_EditorVersion: 2018.4.32f1

com.unity.ml-agents/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ and this project adheres to
1010
### Bug Fixes
1111
#### com.unity.ml-agents (C#)
1212
- Fixed a null reference exception that occurred when loading an ONNX model file that was generated with a new
13-
version of the Python trainer (0.26.0 or newer).
13+
version of the Python trainer (0.26.0 or newer). (#5350)
14+
- Added checked to prevent training with incompatible versions of the python trainer (0.26.0 or newer). (#5370)
1415

1516
## [1.0.7] - 2021-03-04
1617
### Minor Changes

com.unity.ml-agents/Documentation~/com.unity.ml-agents.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Manager documentation].
4848
To install the companion Python package to enable training behaviors, follow the
4949
[installation instructions] on our [GitHub repository]. It is strongly recommended that you
5050
use the Python package that corresponds to this release (version 0.16.1) for the best experience;
51-
versions between 0.16.1 and 0.20.0 are supported.
51+
versions between 0.16.1 and 0.20.0 are supported. Versions after 0.25.1 cannot be used.
5252

5353
## Requirements
5454

com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs

+52-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Linq;
10+
using System.Text.RegularExpressions;
1011
using UnityEngine;
1112
using Unity.MLAgents.Analytics;
1213
using Unity.MLAgents.CommunicatorObjects;
@@ -99,6 +100,41 @@ internal static bool CheckCommunicationVersionsAreCompatible(
99100
}
100101

101102
internal static bool CheckPythonPackageVersionIsCompatible(string pythonLibraryVersion)
103+
{
104+
// Try to extract the numerical values from the pythonLibraryVersion, e.g. remove the ".dev0" suffix
105+
var versionMatch = Regex.Match(pythonLibraryVersion, @"[0-9]+\.[0-9]+\.[0-9]");
106+
if (versionMatch.Success)
107+
{
108+
pythonLibraryVersion = versionMatch.Value;
109+
}
110+
111+
Version pythonVersion;
112+
try
113+
{
114+
pythonVersion = new Version(pythonLibraryVersion);
115+
}
116+
catch
117+
{
118+
// Unparseable version.
119+
// Anything like 0.42.0.dev0 should have been caught with the regex above, so anything here
120+
// is totally bogus. For now, ignore these and let CheckPythonPackageVersionIsSupported handle it.
121+
return true;
122+
}
123+
124+
if (pythonVersion > PythonTrainerVersions.s_MaxCompatibleVersion)
125+
{
126+
return false;
127+
}
128+
return true;
129+
}
130+
131+
/// <summary>
132+
/// Check if the package is in the supported range. Note that some versions might be unsupported but
133+
/// still compatible.
134+
/// </summary>
135+
/// <param name="pythonLibraryVersion"></param>
136+
/// <returns></returns>
137+
internal static bool CheckPythonPackageVersionIsSupported(string pythonLibraryVersion)
102138
{
103139
Version pythonVersion;
104140
try
@@ -107,7 +143,7 @@ internal static bool CheckPythonPackageVersionIsCompatible(string pythonLibraryV
107143
}
108144
catch
109145
{
110-
// Unparseable - this also catches things like "0.20.0-dev0" which we don't want to support
146+
// Unparseable - this also catches things like "0.20.0.dev0" which we don't want to support
111147
return false;
112148
}
113149

@@ -182,7 +218,21 @@ public UnityRLInitParameters Initialize(CommunicatorInitParameters initParameter
182218
throw new UnityAgentsException("ICommunicator.Initialize() failed.");
183219
}
184220

185-
var packageVersionSupported = CheckPythonPackageVersionIsCompatible(pythonPackageVersion);
221+
var packageVersionCompatible = CheckPythonPackageVersionIsCompatible(pythonPackageVersion);
222+
if (!packageVersionCompatible)
223+
{
224+
Debug.LogErrorFormat(
225+
"Python package version ({0}) will produce model files that are incompatible with this " +
226+
"version of the com.unity.ml-agents Unity package. Please downgrade to a Python package " +
227+
"between {1} and {2}, or update to a new version of com.unity.ml-agents.",
228+
pythonPackageVersion,
229+
PythonTrainerVersions.s_MinSupportedVersion,
230+
PythonTrainerVersions.s_MaxSupportedVersion
231+
);
232+
throw new UnityAgentsException("Incompatible trainer version.");
233+
}
234+
235+
var packageVersionSupported = CheckPythonPackageVersionIsSupported(pythonPackageVersion);
186236
if (!packageVersionSupported)
187237
{
188238
Debug.LogWarningFormat(

com.unity.ml-agents/Runtime/Constants.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ internal enum MenuGroup
1313

1414
internal static class PythonTrainerVersions
1515
{
16-
// The python package version must be >= s_MinSupportedVersion
16+
// The python package version should be >= s_MinSupportedVersion
1717
// and <= s_MaxSupportedVersion.
1818
internal static Version s_MinSupportedVersion = new Version("0.16.1");
1919
internal static Version s_MaxSupportedVersion = new Version("0.20.0");
20+
21+
// Any version > to this is known to be incompatible and we will block training.
22+
// Covers any patch to the release before the 2.0.0 package release.
23+
internal static Version s_MaxCompatibleVersion = new Version("0.25.999");
2024
}
2125

2226
}

com.unity.ml-agents/Tests/Editor/Communicator/RpcCommunicatorTests.cs

+27-5
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,39 @@ public void TestCheckCommunicationVersionsAreCompatible()
5454
[Test]
5555
public void TestCheckPythonPackageVersionIsCompatible()
5656
{
57-
Assert.IsFalse(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.13.37")); // too low
58-
Assert.IsFalse(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.42.0")); // too high
57+
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.13.37")); // low is OK
58+
Assert.IsFalse(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.26.0")); // too high
5959

6060
// These are fine
6161
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.16.1"));
6262
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.17.17"));
63-
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.20.0"));
63+
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.25.0"));
64+
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.25.1"));
65+
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.25.2"));
66+
67+
// "dev" strings should get removed before parsing
68+
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.17.0.dev0"));
69+
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.25.0.dev0"));
70+
Assert.IsFalse(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.26.0.dev0"));
71+
72+
// otherwise unparseable - keep support for these
73+
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsCompatible("the airspeed velocity of an unladen swallow"));
74+
}
75+
76+
[Test]
77+
public void TestCheckPythonPackageVersionIsSupported()
78+
{
79+
Assert.IsFalse(RpcCommunicator.CheckPythonPackageVersionIsSupported("0.13.37")); // too low
80+
Assert.IsFalse(RpcCommunicator.CheckPythonPackageVersionIsSupported("0.42.0")); // too high
81+
82+
// These are fine
83+
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsSupported("0.16.1"));
84+
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsSupported("0.17.17"));
85+
Assert.IsTrue(RpcCommunicator.CheckPythonPackageVersionIsSupported("0.20.0"));
6486

6587
// "dev" string or otherwise unparseable
66-
Assert.IsFalse(RpcCommunicator.CheckPythonPackageVersionIsCompatible("0.17.0-dev0"));
67-
Assert.IsFalse(RpcCommunicator.CheckPythonPackageVersionIsCompatible("oh point seventeen point oh"));
88+
Assert.IsFalse(RpcCommunicator.CheckPythonPackageVersionIsSupported("0.17.0.dev0"));
89+
Assert.IsFalse(RpcCommunicator.CheckPythonPackageVersionIsSupported("oh point seventeen point oh"));
6890
}
6991
}
7092
}

0 commit comments

Comments
 (0)