Skip to content

Commit 0311ab2

Browse files
committed
Add capabilities checks bewteen C# and Python codebases.
1 parent cc74f81 commit 0311ab2

39 files changed

+565
-250
lines changed

com.unity.ml-agents/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ and this project adheres to
8080
- `num_updates` and `train_interval` for SAC were replaced with `steps_per_update`. (#3690)
8181
- `WriteAdapter` was renamed to `ObservationWriter`. If you have a custom `ISensor` implementation,
8282
you will need to change the signature of its `Write()` method. (#3834)
83+
- `UnityRLCapabilities` was added to help inform users when RL features are mismatched between C# and Python packages. (#3831)
8384

8485
### Bug Fixes
8586

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void FixedUpdate()
4242
/// Access the Academy singleton through the <see cref="Instance"/>
4343
/// property. The Academy instance is initialized the first time it is accessed (which will
4444
/// typically be by the first <see cref="Agent"/> initialized in a scene).
45-
///
45+
///
4646
/// At initialization, the Academy attempts to connect to the Python training process through
4747
/// the external communicator. If successful, the training process can train <see cref="Agent"/>
4848
/// instances. When you set an agent's <see cref="BehaviorParameters.behaviorType"/> setting
@@ -131,6 +131,8 @@ public bool IsCommunicatorOn
131131
// Random seed used for inference.
132132
int m_InferenceSeed;
133133

134+
// The Capabilities of the RL trainer.
135+
134136
/// <summary>
135137
/// Set the random seed used for inference. This should be set before any Agents are added
136138
/// to the scene. The seed is passed to the ModelRunner constructor, and incremented each
@@ -141,6 +143,12 @@ public int InferenceSeed
141143
set { m_InferenceSeed = value; }
142144
}
143145

146+
/// <summary>
147+
/// Returns the RLCapabilities of the python client that the unity process is connected to.
148+
/// </summary>
149+
internal UnityRLCapabilities Capabilities { get; set; }
150+
151+
144152
// The Academy uses a series of events to communicate with agents
145153
// to facilitate synchronization. More specifically, it ensures
146154
// that all the agents perform their steps in a consistent order (i.e. no
@@ -354,12 +362,16 @@ void InitializeEnvironment()
354362
UnityEngine.Random.InitState(unityRlInitParameters.seed);
355363
// We might have inference-only Agents, so set the seed for them too.
356364
m_InferenceSeed = unityRlInitParameters.seed;
365+
Capabilities = unityRlInitParameters.TrainerCapabilities;
366+
Capabilities.WarnOnPythonMissingBaseRLCapabilities();
357367
}
358-
catch
368+
catch (Exception e)
359369
{
360370
Debug.Log($"" +
361371
$"Couldn't connect to trainer on port {port} using API version {k_ApiVersion}. " +
362-
"Will perform inference instead."
372+
"Will perform inference instead." +
373+
"Original Exception Message:" +
374+
$"{e.Message}"
363375
);
364376
Communicator = null;
365377
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,5 +280,21 @@ public static ObservationProto GetObservationProto(this ISensor sensor, Observat
280280
return observationProto;
281281
}
282282
#endregion
283+
284+
public static UnityRLCapabilities ToRLCapabilities(this UnityRLCapabilitiesProto proto)
285+
{
286+
return new UnityRLCapabilities
287+
{
288+
BaseRLCapabilities = proto.BaseRLCapabilities
289+
};
290+
}
291+
292+
public static UnityRLCapabilitiesProto ToProto(this UnityRLCapabilities rlCaps)
293+
{
294+
return new UnityRLCapabilitiesProto
295+
{
296+
BaseRLCapabilities = rlCaps.BaseRLCapabilities
297+
};
298+
}
283299
}
284300
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ internal struct CommunicatorInitParameters
2727
/// The version of the communication API.
2828
/// </summary>
2929
public string unityCommunicationVersion;
30+
31+
/// <summary>
32+
/// The RL capabilities of the C# codebase.
33+
/// </summary>
34+
public UnityRLCapabilities CSharpCapabilities;
3035
}
3136
internal struct UnityRLInitParameters
3237
{
@@ -44,6 +49,11 @@ internal struct UnityRLInitParameters
4449
/// The version of the communication API that python is using.
4550
/// </summary>
4651
public string pythonCommunicationVersion;
52+
53+
/// <summary>
54+
/// The RL capabilities of the Trainer codebase.
55+
/// </summary>
56+
public UnityRLCapabilities TrainerCapabilities;
4757
}
4858
internal struct UnityRLInputParameters
4959
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using UnityEngine;
2+
3+
namespace MLAgents
4+
{
5+
/// <summary>
6+
/// A struct holding the capabilities flags for Reinforcement Learning across C# and the Trainer codebase. This
7+
/// struct will be used to inform users if and when they are using C# / Trainer features that are mismatched.
8+
/// </summary>
9+
internal struct UnityRLCapabilities
10+
{
11+
internal bool BaseRLCapabilities;
12+
13+
/// <summary>
14+
/// Will print a warning to the console if Python does not support base capabilities and will
15+
/// return <value>true</value> if the warning was printed.
16+
/// </summary>
17+
/// <returns></returns>
18+
public bool WarnOnPythonMissingBaseRLCapabilities()
19+
{
20+
if (BaseRLCapabilities)
21+
{
22+
return false;
23+
}
24+
Debug.LogWarning("Unity has connected to a Training process that does not support" +
25+
"Base Reinforcement Learning Capabilities. Please make sure you have the" +
26+
" latest training codebase installed for this version of the ML-Agents package.");
27+
return true;
28+
}
29+
30+
}
31+
}

com.unity.ml-agents/Runtime/Communicator/UnityRLCapabilities.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.ml-agents/Runtime/Grpc/CommunicatorObjects/AgentAction.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

com.unity.ml-agents/Runtime/Grpc/CommunicatorObjects/AgentInfo.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

com.unity.ml-agents/Runtime/Grpc/CommunicatorObjects/AgentInfoActionPair.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

com.unity.ml-agents/Runtime/Grpc/CommunicatorObjects/BrainParameters.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)