Skip to content

Commit 64fa713

Browse files
Cherry picking #5283 and #5519 into 2.0.1-verified-patch to fix gRPC for mac m1 (MLA 2259) (#5602)
* Only update the native library and the c# calls that need to be made to clean up channels. (#5283) * Fix Mac backcompat test (#5519) Co-authored-by: Chris Goy <[email protected]>
1 parent 0f548f1 commit 64fa713

File tree

5 files changed

+36
-30
lines changed

5 files changed

+36
-30
lines changed

.yamato/training-backcompat-tests.yml

+6-6
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:

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

+24-7
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
}

ml-agents/tests/yamato/training_int_tests.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import sys
66
import subprocess
77
import time
8-
from typing import Any
98

109
from .yamato_utils import (
1110
find_executables,
@@ -14,7 +13,6 @@
1413
run_standalone_build,
1514
init_venv,
1615
override_config_file,
17-
override_legacy_config_file,
1816
checkout_csharp_version,
1917
undo_git_checkout,
2018
)
@@ -26,7 +24,7 @@ def run_training(python_version: str, csharp_version: str) -> bool:
2624
print(
2725
f"Running training with python={python_version or latest} and c#={csharp_version or latest}"
2826
)
29-
output_dir = "models" if python_version else "results"
27+
output_dir = "results"
3028
onnx_file_expected = f"./{output_dir}/{run_id}/3DBall.onnx"
3129

3230
if os.path.exists(onnx_file_expected):
@@ -70,17 +68,11 @@ def run_training(python_version: str, csharp_version: str) -> bool:
7068
# Copy the default training config but override the max_steps parameter,
7169
# and reduce the batch_size and buffer_size enough to ensure an update step happens.
7270
yaml_out = "override.yaml"
73-
if python_version:
74-
overrides: Any = {"max_steps": 100, "batch_size": 10, "buffer_size": 10}
75-
override_legacy_config_file(
76-
python_version, "config/trainer_config.yaml", yaml_out, **overrides
77-
)
78-
else:
79-
overrides = {
80-
"hyperparameters": {"batch_size": 10, "buffer_size": 10},
81-
"max_steps": 100,
82-
}
83-
override_config_file("config/ppo/3DBall.yaml", yaml_out, overrides)
71+
overrides = {
72+
"hyperparameters": {"batch_size": 10, "buffer_size": 10},
73+
"max_steps": 100,
74+
}
75+
override_config_file("config/ppo/3DBall.yaml", yaml_out, overrides)
8476

8577
log_output_path = f"{get_base_output_path()}/training.log"
8678
env_path = os.path.join(get_base_output_path(), standalone_player_path)

ml-agents/tests/yamato/yamato_utils.py

-3
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,10 @@ def init_venv(
134134
pip_commands = ["--upgrade pip", "--upgrade setuptools"]
135135
if mlagents_python_version:
136136
# install from pypi
137-
if platform != "darwin":
138-
raise RuntimeError("Yamato can only run tensorflow on mac platforms!")
139137
pip_commands += [
140138
f"mlagents=={mlagents_python_version}",
141139
f"gym-unity=={mlagents_python_version}",
142140
# TODO build these and publish to internal pypi
143-
"~/tensorflow_pkg/tensorflow-2.0.0-cp37-cp37m-macosx_10_14_x86_64.whl",
144141
"tf2onnx==1.6.1",
145142
]
146143
else:

0 commit comments

Comments
 (0)