From bac53dbc02dc54e73bd2932ade0b94120a8202e1 Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Mon, 12 Apr 2021 10:52:51 -0700 Subject: [PATCH 1/2] avoid empty set iteration, avoid Debug.AssertFormat --- .../Runtime/Actuators/ActuatorManager.cs | 11 ++++--- com.unity.ml-agents/Runtime/Agent.cs | 14 +++++--- .../Runtime/Sensors/SensorShapeValidator.cs | 32 +++++++++++-------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/com.unity.ml-agents/Runtime/Actuators/ActuatorManager.cs b/com.unity.ml-agents/Runtime/Actuators/ActuatorManager.cs index de0f239d68..1ff35557d9 100644 --- a/com.unity.ml-agents/Runtime/Actuators/ActuatorManager.cs +++ b/com.unity.ml-agents/Runtime/Actuators/ActuatorManager.cs @@ -174,10 +174,13 @@ static void UpdateActionArray(ActionSegment sourceActionBuffer, ActionSegm } else { - Debug.AssertFormat(sourceActionBuffer.Length == destination.Length, - "sourceActionBuffer: {0} is a different size than destination: {1}.", - sourceActionBuffer.Length, - destination.Length); + if (sourceActionBuffer.Length != destination.Length) + { + Debug.AssertFormat(sourceActionBuffer.Length == destination.Length, + "sourceActionBuffer: {0} is a different size than destination: {1}.", + sourceActionBuffer.Length, + destination.Length); + } Array.Copy(sourceActionBuffer.Array, sourceActionBuffer.Offset, diff --git a/com.unity.ml-agents/Runtime/Agent.cs b/com.unity.ml-agents/Runtime/Agent.cs index 6c056c541a..fd0377f870 100644 --- a/com.unity.ml-agents/Runtime/Agent.cs +++ b/com.unity.ml-agents/Runtime/Agent.cs @@ -574,9 +574,12 @@ void NotifyAgentDone(DoneReason doneReason) m_Brain?.RequestDecision(m_Info, sensors); // We also have to write any to any DemonstationStores so that they get the "done" flag. - foreach (var demoWriter in DemonstrationWriters) + if (DemonstrationWriters.Count != 0) { - demoWriter.Record(m_Info, sensors); + foreach (var demoWriter in DemonstrationWriters) + { + demoWriter.Record(m_Info, sensors); + } } ResetSensors(); @@ -1082,9 +1085,12 @@ void SendInfoToBrain() } // If we have any DemonstrationWriters, write the AgentInfo and sensors to them. - foreach (var demoWriter in DemonstrationWriters) + if (DemonstrationWriters.Count != 0) { - demoWriter.Record(m_Info, sensors); + foreach (var demoWriter in DemonstrationWriters) + { + demoWriter.Record(m_Info, sensors); + } } } diff --git a/com.unity.ml-agents/Runtime/Sensors/SensorShapeValidator.cs b/com.unity.ml-agents/Runtime/Sensors/SensorShapeValidator.cs index 217e886e75..495d46bdad 100644 --- a/com.unity.ml-agents/Runtime/Sensors/SensorShapeValidator.cs +++ b/com.unity.ml-agents/Runtime/Sensors/SensorShapeValidator.cs @@ -25,23 +25,29 @@ public void ValidateSensors(List sensors) else { // Check for compatibility with the other Agents' Sensors - // TODO make sure this only checks once per agent - Debug.AssertFormat( - m_SensorShapes.Count == sensors.Count, - "Number of Sensors must match. {0} != {1}", - m_SensorShapes.Count, - sensors.Count - ); + if (m_SensorShapes.Count != sensors.Count) + { + Debug.AssertFormat( + m_SensorShapes.Count == sensors.Count, + "Number of Sensors must match. {0} != {1}", + m_SensorShapes.Count, + sensors.Count + ); + } for (var i = 0; i < Mathf.Min(m_SensorShapes.Count, sensors.Count); i++) { var cachedSpec = m_SensorShapes[i]; var sensorSpec = sensors[i].GetObservationSpec(); - Debug.AssertFormat( - cachedSpec.Shape == sensorSpec.Shape, - "Sensor shapes must match. {0} != {1}", - cachedSpec.Shape, - sensorSpec.Shape - ); + if (cachedSpec.Shape != sensorSpec.Shape) + { + Debug.AssertFormat( + cachedSpec.Shape == sensorSpec.Shape, + "Sensor shapes must match. {0} != {1}", + cachedSpec.Shape, + sensorSpec.Shape + ); + + } } } } From 0fcfb46685f4908f8b77c70251507cfeab147d09 Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Mon, 12 Apr 2021 10:58:25 -0700 Subject: [PATCH 2/2] changelog --- com.unity.ml-agents/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.ml-agents/CHANGELOG.md b/com.unity.ml-agents/CHANGELOG.md index b565c88127..37dce2b9bf 100755 --- a/com.unity.ml-agents/CHANGELOG.md +++ b/com.unity.ml-agents/CHANGELOG.md @@ -61,6 +61,7 @@ determine whether `Agent.RequestDecision()` and `Agent.RequestAction()` are call - Fixed a bug where sensors and actuators could get sorted inconsistently on different systems to different Culture settings. Unfortunately, this may require retraining models if it changes the resulting order of the sensors or actuators on your system. (#5194) +- Removed additional memory allocations that were occurring due to assert messages and iterating of DemonstrationRecorders. (#5246) #### ml-agents / ml-agents-envs / gym-unity (Python) - Fixed an issue which was causing increased variance when using LSTMs. Also fixed an issue with LSTM when used with POCA and `sequence_length` < `time_horizon`. (#5206) - Fixed a bug where the SAC replay buffer would not be saved out at the end of a run, even if `save_replay_buffer` was enabled. (#5205)