Skip to content

Commit 7b4cd0f

Browse files
committed
Merge branch 'main' into exp-petting-registry
2 parents f275dd5 + 37a6aa2 commit 7b4cd0f

File tree

39 files changed

+486
-108
lines changed

39 files changed

+486
-108
lines changed

.yamato/training-backcompat-tests.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11

2-
test_mac_backcompat_2020.1:
3-
{% capture editor_version %}2020.1{% endcapture %}
2+
test_mac_backcompat_2019.4:
3+
{% capture editor_version %}2019.4{% endcapture %}
44
{% capture csharp_backcompat_version %}1.0.0{% endcapture %}
55
# This test has to run on mac because it requires the custom build of tensorflow without AVX
66
# Test against 2020.1 because 2020.2 has to run against package version 1.2.0
77
name: Test Mac Backcompat Training {{ editor_version }}
88
agent:
99
type: Unity::VM::osx
10-
image: ml-agents/ml-agents-bokken-mac:0.1.4-492264
10+
image: ml-agents/ml-agents-bokken-mac:0.1.5-853758
1111
flavor: b1.small
1212
variables:
1313
UNITY_VERSION: {{ editor_version }}
1414
commands:
1515
- |
16-
python3 -m venv venv && source venv/bin/activate
16+
python -m venv venv && source venv/bin/activate
1717
python -m pip install pyyaml --index-url https://artifactory.prd.it.unity3d.com/artifactory/api/pypi/pypi/simple
1818
python -m pip install unity-downloader-cli --index-url https://artifactory.prd.it.unity3d.com/artifactory/api/pypi/pypi/simple --upgrade
1919
unity-downloader-cli -u {{ editor_version }} -c editor --wait --fast
@@ -23,9 +23,9 @@ test_mac_backcompat_2020.1:
2323
python -u -m ml-agents.tests.yamato.standalone_build_tests --build-target=mac
2424
python -u -m ml-agents.tests.yamato.training_int_tests --csharp {{ csharp_backcompat_version }}
2525
- |
26-
python3 -m venv venv_old && source venv_old/bin/activate
26+
python -m venv venv_old && source venv_old/bin/activate
2727
python -m pip install pyyaml --index-url https://artifactory.prd.it.unity3d.com/artifactory/api/pypi/pypi/simple
28-
python -u -m ml-agents.tests.yamato.training_int_tests --python 0.16.0
28+
python -u -m ml-agents.tests.yamato.training_int_tests --python 0.24.0
2929
triggers:
3030
cancel_old_ci: true
3131
recurring:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ package by replying to
178178
Your opinion matters a great deal to us. Only by hearing your thoughts on the
179179
Unity ML-Agents Toolkit can we continue to improve and grow. Please take a few
180180
minutes to
181-
[let us know about it](https://github.com/Unity-Technologies/ml-agents/issues/1454).
181+
[let us know about it](https://unitysoftware.co1.qualtrics.com/jfe/form/SV_55pQKCZ578t0kbc).
182182

183183
For any other questions or feedback, connect directly with the ML-Agents team at
184184
ml-agents@unity3d.com.

com.unity.ml-agents.extensions/Tests/Runtime/Input/InputActuatorComponentTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public override void Setup()
4949
public override void TearDown()
5050
{
5151
m_ActuatorComponent.CleanupActionAsset();
52+
var objects = GameObject.FindObjectsOfType<GameObject>();
53+
foreach (var o in objects)
54+
{
55+
UnityEngine.Object.DestroyImmediate(o);
56+
}
5257
base.TearDown();
5358
}
5459

com.unity.ml-agents/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,45 @@ and this project adheres to
88

99
## [Unreleased]
1010
### Major Changes
11+
1112
#### com.unity.ml-agents / com.unity.ml-agents.extensions (C#)
13+
1214
#### ml-agents / ml-agents-envs / gym-unity (Python)
15+
1316
### Minor Changes
17+
1418
#### com.unity.ml-agents / com.unity.ml-agents.extensions (C#)
19+
- Added the capacity to initialize behaviors from any checkpoint and not just the latest one (#5525)
20+
1521
#### ml-agents / ml-agents-envs / gym-unity (Python)
22+
- Set gym version in gym-unity to gym release 0.20.0
23+
- Added support for having `beta`, `epsilon`, and `learning rate` on separate schedules (affects only PPO and POCA). (#5538)
24+
1625
### Bug Fixes
26+
1727
#### com.unity.ml-agents / com.unity.ml-agents.extensions (C#)
28+
1829
#### ml-agents / ml-agents-envs / gym-unity (Python)
1930
- Fixed a bug in multi-agent cooperative training where agents might not receive all of the states of
2031
terminated teammates. (#5441)
2132
- Fixed wrong attribute name in argparser for torch device option (#5433)(#5467)
2233
- Fixed conflicting CLI and yaml options regarding resume & initialize_from (#5495)
34+
- Fixed failing tests for gym-unity due to gym 0.20.0 release (#5540)
35+
- Fixed a bug in VAIL where the variational bottleneck was not properly passing gradients (#5546)
2336
- Added minimal analytics collection to LL-API (#5511)
37+
2438
## [2.1.0-exp.1] - 2021-06-09
2539
### Minor Changes
2640
#### com.unity.ml-agents / com.unity.ml-agents.extensions (C#)
2741
- update Barracuda to 2.0.0-pre.3. (#5385)
2842
- Fixed NullReferenceException when adding Behavior Parameters with no Agent. (#5382)
2943
- Add stacking option in Editor for `VectorSensorComponent`. (#5376)
44+
3045
#### ml-agents / ml-agents-envs / gym-unity (Python)
3146
- Lock cattrs dependency version to 1.6. (#5397)
3247
- Added a fully connected visual encoder for environments with very small image inputs. (#5351)
3348
- Colab notebooks illustrating the use of the Python API are now part of the repository. (#5399)
49+
3450
### Bug Fixes
3551
#### com.unity.ml-agents / com.unity.ml-agents.extensions (C#)
3652
- RigidBodySensorComponent now displays a warning if it's used in a way that won't generate useful observations. (#5387)

com.unity.ml-agents/Runtime/Analytics/AnalyticsUtils.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
11
using System;
2+
using System.Text;
3+
using System.Security.Cryptography;
24
using UnityEngine;
35

46
namespace Unity.MLAgents.Analytics
57
{
8+
69
internal static class AnalyticsUtils
710
{
11+
/// <summary>
12+
/// Conversion function from byte array to hex string
13+
/// </summary>
14+
/// <param name="array"></param>
15+
/// <returns>A byte array to be hex encoded.</returns>
16+
private static string ToHexString(byte[] array)
17+
{
18+
StringBuilder hex = new StringBuilder(array.Length * 2);
19+
foreach (byte b in array)
20+
{
21+
hex.AppendFormat("{0:x2}", b);
22+
}
23+
return hex.ToString();
24+
}
25+
826
/// <summary>
927
/// Hash a string to remove PII or secret info before sending to analytics
1028
/// </summary>
11-
/// <param name="s"></param>
12-
/// <returns>A string containing the Hash128 of the input string.</returns>
13-
public static string Hash(string s)
29+
/// <param name="key"></param>
30+
/// <returns>A string containing the key to be used for HMAC encoding.</returns>
31+
/// <param name="value"></param>
32+
/// <returns>A string containing the value to be encoded.</returns>
33+
public static string Hash(string key, string value)
1434
{
15-
var behaviorNameHash = Hash128.Compute(s);
16-
return behaviorNameHash.ToString();
35+
string hash;
36+
UTF8Encoding encoder = new UTF8Encoding();
37+
using (HMACSHA256 hmac = new HMACSHA256(encoder.GetBytes(key)))
38+
{
39+
Byte[] hmBytes = hmac.ComputeHash(encoder.GetBytes(value));
40+
hash = ToHexString(hmBytes);
41+
}
42+
return hash;
1743
}
1844

1945
internal static bool s_SendEditorAnalytics = true;

com.unity.ml-agents/Runtime/Analytics/Events.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ internal struct TrainingEnvironmentInitializedEvent
156156
public string TorchDeviceType;
157157
public int NumEnvironments;
158158
public int NumEnvironmentParameters;
159+
public string RunOptions;
159160
}
160161

161162
[Flags]
@@ -188,5 +189,6 @@ internal struct TrainingBehaviorInitializedEvent
188189
public string VisualEncoder;
189190
public int NumNetworkLayers;
190191
public int NumNetworkHiddenUnits;
192+
public string Config;
191193
}
192194
}

com.unity.ml-agents/Runtime/Analytics/InferenceAnalytics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ IList<IActuator> actuators
156156
var inferenceEvent = new InferenceEvent();
157157

158158
// Hash the behavior name so that there's no concern about PII or "secret" data being leaked.
159-
inferenceEvent.BehaviorName = AnalyticsUtils.Hash(behaviorName);
159+
inferenceEvent.BehaviorName = AnalyticsUtils.Hash(k_VendorKey, behaviorName);
160160

161161
inferenceEvent.BarracudaModelSource = barracudaModel.IrSource;
162162
inferenceEvent.BarracudaModelVersion = barracudaModel.IrVersion;

com.unity.ml-agents/Runtime/Analytics/TrainingAnalytics.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,21 @@ internal static string ParseBehaviorName(string fullyQualifiedBehaviorName)
192192
return fullyQualifiedBehaviorName.Substring(0, lastQuestionIndex);
193193
}
194194

195+
internal static TrainingBehaviorInitializedEvent SanitizeTrainingBehaviorInitializedEvent(TrainingBehaviorInitializedEvent tbiEvent)
196+
{
197+
// Hash the behavior name if the message version is from an older version of ml-agents that doesn't do trainer-side hashing.
198+
// We'll also, for extra safety, verify that the BehaviorName is the size of the expected SHA256 hash.
199+
// Context: The config field was added at the same time as trainer side hashing, so messages including it should already be hashed.
200+
if (tbiEvent.Config.Length == 0 || tbiEvent.BehaviorName.Length != 64)
201+
{
202+
tbiEvent.BehaviorName = AnalyticsUtils.Hash(k_VendorKey, tbiEvent.BehaviorName);
203+
}
204+
205+
return tbiEvent;
206+
}
207+
195208
[Conditional("MLA_UNITY_ANALYTICS_MODULE")]
196-
public static void TrainingBehaviorInitialized(TrainingBehaviorInitializedEvent tbiEvent)
209+
public static void TrainingBehaviorInitialized(TrainingBehaviorInitializedEvent rawTbiEvent)
197210
{
198211
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
199212
if (!IsAnalyticsEnabled())
@@ -202,6 +215,7 @@ public static void TrainingBehaviorInitialized(TrainingBehaviorInitializedEvent
202215
if (!EnableAnalytics())
203216
return;
204217

218+
var tbiEvent = SanitizeTrainingBehaviorInitializedEvent(rawTbiEvent);
205219
var behaviorName = tbiEvent.BehaviorName;
206220
var added = s_SentTrainingBehaviorInitialized.Add(behaviorName);
207221

@@ -211,9 +225,7 @@ public static void TrainingBehaviorInitialized(TrainingBehaviorInitializedEvent
211225
return;
212226
}
213227

214-
// Hash the behavior name so that there's no concern about PII or "secret" data being leaked.
215228
tbiEvent.TrainingSessionGuid = s_TrainingSessionGuid.ToString();
216-
tbiEvent.BehaviorName = AnalyticsUtils.Hash(tbiEvent.BehaviorName);
217229

218230
// Note - to debug, use JsonUtility.ToJson on the event.
219231
// Debug.Log(
@@ -236,7 +248,7 @@ IList<IActuator> actuators
236248
var remotePolicyEvent = new RemotePolicyInitializedEvent();
237249

238250
// Hash the behavior name so that there's no concern about PII or "secret" data being leaked.
239-
remotePolicyEvent.BehaviorName = AnalyticsUtils.Hash(behaviorName);
251+
remotePolicyEvent.BehaviorName = AnalyticsUtils.Hash(k_VendorKey, behaviorName);
240252

241253
remotePolicyEvent.TrainingSessionGuid = s_TrainingSessionGuid.ToString();
242254
remotePolicyEvent.ActionSpec = EventActionSpec.FromActionSpec(actionSpec);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ internal static TrainingEnvironmentInitializedEvent ToTrainingEnvironmentInitial
501501
TorchDeviceType = inputProto.TorchDeviceType,
502502
NumEnvironments = inputProto.NumEnvs,
503503
NumEnvironmentParameters = inputProto.NumEnvironmentParameters,
504+
RunOptions = inputProto.RunOptions,
504505
};
505506
}
506507

@@ -530,6 +531,7 @@ internal static TrainingBehaviorInitializedEvent ToTrainingBehaviorInitializedEv
530531
VisualEncoder = inputProto.VisualEncoder,
531532
NumNetworkLayers = inputProto.NumNetworkLayers,
532533
NumNetworkHiddenUnits = inputProto.NumNetworkHiddenUnits,
534+
Config = inputProto.Config,
533535
};
534536
}
535537

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define MLA_SUPPORTED_TRAINING_PLATFORM
33
#endif
44

5-
# if MLA_SUPPORTED_TRAINING_PLATFORM
5+
#if MLA_SUPPORTED_TRAINING_PLATFORM
66
using Grpc.Core;
77
#if UNITY_EDITOR
88
using UnityEditor;
@@ -50,6 +50,7 @@ internal class RpcCommunicator : ICommunicator
5050

5151
/// The Unity to External client.
5252
UnityToExternalProto.UnityToExternalProtoClient m_Client;
53+
Channel m_Channel;
5354

5455
/// <summary>
5556
/// Initializes a new instance of the RPCCommunicator class.
@@ -140,6 +141,7 @@ out input
140141
Debug.Log($"Unexpected exception when trying to initialize communication: {ex}");
141142
}
142143
initParametersOut = new UnityRLInitParameters();
144+
NotifyQuitAndShutDownChannel();
143145
return false;
144146
}
145147

@@ -181,6 +183,8 @@ out input
181183

182184
UpdateEnvironmentWithInput(input.RlInput);
183185
initParametersOut = initializationInput.RlInitializationInput.ToUnityRLInitParameters();
186+
// Be sure to shut down the grpc channel when the application is quitting.
187+
Application.quitting += NotifyQuitAndShutDownChannel;
184188
return true;
185189
#else
186190
initParametersOut = new UnityRLInitParameters();
@@ -217,9 +221,9 @@ void UpdateEnvironmentWithInput(UnityRLInputProto rlInput)
217221
UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInputProto unityInput)
218222
{
219223
m_IsOpen = true;
220-
var channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);
224+
m_Channel = new Channel($"localhost:{port}", ChannelCredentials.Insecure);
221225

222-
m_Client = new UnityToExternalProto.UnityToExternalProtoClient(channel);
226+
m_Client = new UnityToExternalProto.UnityToExternalProtoClient(m_Channel);
223227
var result = m_Client.Exchange(WrapMessage(unityOutput, 200));
224228
var inputMessage = m_Client.Exchange(WrapMessage(null, 200));
225229
unityInput = inputMessage.UnityInput;
@@ -229,11 +233,24 @@ UnityInputProto Initialize(int port, UnityOutputProto unityOutput, out UnityInpu
229233
if (result.Header.Status != 200 || inputMessage.Header.Status != 200)
230234
{
231235
m_IsOpen = false;
232-
QuitCommandReceived?.Invoke();
236+
NotifyQuitAndShutDownChannel();
233237
}
234238
return result.UnityInput;
235239
}
236240

241+
void NotifyQuitAndShutDownChannel()
242+
{
243+
QuitCommandReceived?.Invoke();
244+
try
245+
{
246+
m_Channel.ShutdownAsync().Wait();
247+
}
248+
catch (Exception)
249+
{
250+
// do nothing
251+
}
252+
}
253+
237254
#endregion
238255

239256
#region Destruction
@@ -269,7 +286,7 @@ void SendCommandEvent(CommandProto command)
269286
{
270287
case CommandProto.Quit:
271288
{
272-
QuitCommandReceived?.Invoke();
289+
NotifyQuitAndShutDownChannel();
273290
return;
274291
}
275292
case CommandProto.Reset:
@@ -456,7 +473,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
456473
// Not sure if the quit command is actually sent when a
457474
// non 200 message is received. Notify that we are indeed
458475
// quitting.
459-
QuitCommandReceived?.Invoke();
476+
NotifyQuitAndShutDownChannel();
460477
return message.UnityInput;
461478
}
462479
catch (Exception ex)
@@ -488,7 +505,7 @@ UnityInputProto Exchange(UnityOutputProto unityOutput)
488505
}
489506

490507
m_IsOpen = false;
491-
QuitCommandReceived?.Invoke();
508+
NotifyQuitAndShutDownChannel();
492509
return null;
493510
}
494511
}

0 commit comments

Comments
 (0)