Skip to content

Commit cc74f81

Browse files
author
Chris Elion
authored
UI for Ray stacks, rename WriteAdapter to ObservationWriter (#3834)
* UI for Ray stacks, rename WriteAdapter to ObservationWriter * move test * changelog and migration
1 parent b73d4dd commit cc74f81

29 files changed

+87
-82
lines changed

Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/SensorBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ public abstract class SensorBase : ISensor
2222

2323
/// <summary>
2424
/// Default implementation of Write interface. This creates a temporary array,
25-
/// calls WriteObservation, and then writes the results to the WriteAdapter.
25+
/// calls WriteObservation, and then writes the results to the ObservationWriter.
2626
/// </summary>
27-
/// <param name="adapter"></param>
27+
/// <param name="writer"></param>
2828
/// <returns>The number of elements written.</returns>
29-
public virtual int Write(WriteAdapter adapter)
29+
public virtual int Write(ObservationWriter writer)
3030
{
3131
// TODO reuse buffer for similar agents, don't call GetObservationShape()
3232
var numFloats = this.ObservationSize();
3333
float[] buffer = new float[numFloats];
3434
WriteObservation(buffer);
3535

36-
adapter.AddRange(buffer);
36+
writer.AddRange(buffer);
3737

3838
return numFloats;
3939
}

com.unity.ml-agents/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ and this project adheres to
7878
added in `CollectObservations()`. (#3825)
7979
- Model updates can now happen asynchronously with environment steps for better performance. (#3690)
8080
- `num_updates` and `train_interval` for SAC were replaced with `steps_per_update`. (#3690)
81+
- `WriteAdapter` was renamed to `ObservationWriter`. If you have a custom `ISensor` implementation,
82+
you will need to change the signature of its `Write()` method. (#3834)
8183

8284
### Bug Fixes
8385

com.unity.ml-agents/Editor/RayPerceptionSensorComponentBaseEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected void OnRayPerceptionInspectorGUI(bool is3d)
3737
// it is not editable during play mode.
3838
EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties());
3939
{
40-
EditorGUILayout.PropertyField(so.FindProperty("m_ObservationStacks"), true);
40+
EditorGUILayout.PropertyField(so.FindProperty("m_ObservationStacks"), new GUIContent("Stacked Raycasts"), true);
4141
}
4242
EditorGUI.EndDisabledGroup();
4343

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,14 @@ public static ObservationProto ToProto(this Observation obs)
227227
}
228228

229229
/// <summary>
230-
/// Generate an ObservationProto for the sensor using the provided WriteAdapter.
230+
/// Generate an ObservationProto for the sensor using the provided ObservationWriter.
231231
/// This is equivalent to producing an Observation and calling Observation.ToProto(),
232232
/// but avoid some intermediate memory allocations.
233233
/// </summary>
234234
/// <param name="sensor"></param>
235-
/// <param name="writeAdapter"></param>
235+
/// <param name="observationWriter"></param>
236236
/// <returns></returns>
237-
public static ObservationProto GetObservationProto(this ISensor sensor, WriteAdapter writeAdapter)
237+
public static ObservationProto GetObservationProto(this ISensor sensor, ObservationWriter observationWriter)
238238
{
239239
var shape = sensor.GetObservationShape();
240240
ObservationProto observationProto = null;
@@ -249,8 +249,8 @@ public static ObservationProto GetObservationProto(this ISensor sensor, WriteAda
249249
floatDataProto.Data.Add(0.0f);
250250
}
251251

252-
writeAdapter.SetTarget(floatDataProto.Data, sensor.GetObservationShape(), 0);
253-
sensor.Write(writeAdapter);
252+
observationWriter.SetTarget(floatDataProto.Data, sensor.GetObservationShape(), 0);
253+
sensor.Write(observationWriter);
254254

255255
observationProto = new ObservationProto
256256
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class RpcCommunicator : ICommunicator
2727

2828
List<string> m_BehaviorNames = new List<string>();
2929
bool m_NeedCommunicateThisStep;
30-
WriteAdapter m_WriteAdapter = new WriteAdapter();
30+
ObservationWriter m_ObservationWriter = new ObservationWriter();
3131
Dictionary<string, SensorShapeValidator> m_SensorShapeValidators = new Dictionary<string, SensorShapeValidator>();
3232
Dictionary<string, List<int>> m_OrderedAgentsRequestingDecisions = new Dictionary<string, List<int>>();
3333

@@ -322,7 +322,7 @@ public void PutObservations(string behaviorName, AgentInfo info, List<ISensor> s
322322
{
323323
foreach (var sensor in sensors)
324324
{
325-
var obsProto = sensor.GetObservationProto(m_WriteAdapter);
325+
var obsProto = sensor.GetObservationProto(m_ObservationWriter);
326326
agentInfoProto.Observations.Add(obsProto);
327327
}
328328
}

com.unity.ml-agents/Runtime/Demonstrations/DemonstrationWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class DemonstrationWriter
2020
DemonstrationMetaData m_MetaData;
2121
Stream m_Writer;
2222
float m_CumulativeReward;
23-
WriteAdapter m_WriteAdapter = new WriteAdapter();
23+
ObservationWriter m_ObservationWriter = new ObservationWriter();
2424

2525
/// <summary>
2626
/// Create a DemonstrationWriter that will write to the specified stream.
@@ -117,7 +117,7 @@ internal void Record(AgentInfo info, List<ISensor> sensors)
117117
var agentProto = info.ToInfoActionPairProto();
118118
foreach (var sensor in sensors)
119119
{
120-
agentProto.AgentInfo.Observations.Add(sensor.GetObservationProto(m_WriteAdapter));
120+
agentProto.AgentInfo.Observations.Add(sensor.GetObservationProto(m_ObservationWriter));
121121
}
122122

123123
agentProto.WriteDelimitedTo(m_Writer);

com.unity.ml-agents/Runtime/Inference/GeneratorImpl.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ internal class VectorObservationGenerator : TensorGenerator.IGenerator
8282
{
8383
readonly ITensorAllocator m_Allocator;
8484
List<int> m_SensorIndices = new List<int>();
85-
WriteAdapter m_WriteAdapter = new WriteAdapter();
85+
ObservationWriter m_ObservationWriter = new ObservationWriter();
8686

8787
public VectorObservationGenerator(ITensorAllocator allocator)
8888
{
@@ -115,8 +115,8 @@ public void Generate(TensorProxy tensorProxy, int batchSize, IEnumerable<AgentIn
115115
foreach (var sensorIndex in m_SensorIndices)
116116
{
117117
var sensor = info.sensors[sensorIndex];
118-
m_WriteAdapter.SetTarget(tensorProxy, agentIndex, tensorOffset);
119-
var numWritten = sensor.Write(m_WriteAdapter);
118+
m_ObservationWriter.SetTarget(tensorProxy, agentIndex, tensorOffset);
119+
var numWritten = sensor.Write(m_ObservationWriter);
120120
tensorOffset += numWritten;
121121
}
122122
Debug.AssertFormat(
@@ -350,7 +350,7 @@ internal class VisualObservationInputGenerator : TensorGenerator.IGenerator
350350
{
351351
readonly int m_SensorIndex;
352352
readonly ITensorAllocator m_Allocator;
353-
WriteAdapter m_WriteAdapter = new WriteAdapter();
353+
ObservationWriter m_ObservationWriter = new ObservationWriter();
354354

355355
public VisualObservationInputGenerator(
356356
int sensorIndex, ITensorAllocator allocator)
@@ -375,8 +375,8 @@ public void Generate(TensorProxy tensorProxy, int batchSize, IEnumerable<AgentIn
375375
}
376376
else
377377
{
378-
m_WriteAdapter.SetTarget(tensorProxy, agentIndex, 0);
379-
sensor.Write(m_WriteAdapter);
378+
m_ObservationWriter.SetTarget(tensorProxy, agentIndex, 0);
379+
sensor.Write(m_ObservationWriter);
380380
}
381381
agentIndex++;
382382
}

com.unity.ml-agents/Runtime/Policies/HeuristicPolicy.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class HeuristicPolicy : IPolicy
1919
bool m_Done;
2020
bool m_DecisionRequested;
2121

22-
WriteAdapter m_WriteAdapter = new WriteAdapter();
22+
ObservationWriter m_ObservationWriter = new ObservationWriter();
2323
NullList m_NullList = new NullList();
2424

2525

@@ -128,8 +128,8 @@ void StepSensors(List<ISensor> sensors)
128128
{
129129
if (sensor.GetCompressionType() == SensorCompressionType.None)
130130
{
131-
m_WriteAdapter.SetTarget(m_NullList, sensor.GetObservationShape(), 0);
132-
sensor.Write(m_WriteAdapter);
131+
m_ObservationWriter.SetTarget(m_NullList, sensor.GetObservationShape(), 0);
132+
sensor.Write(m_ObservationWriter);
133133
}
134134
else
135135
{

com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,16 @@ public byte[] GetCompressedObservation()
9191
}
9292

9393
/// <summary>
94-
/// Writes out the generated, uncompressed image to the provided <see cref="WriteAdapter"/>.
94+
/// Writes out the generated, uncompressed image to the provided <see cref="ObservationWriter"/>.
9595
/// </summary>
96-
/// <param name="adapter">Where the observation is written to.</param>
96+
/// <param name="writer">Where the observation is written to.</param>
9797
/// <returns></returns>
98-
public int Write(WriteAdapter adapter)
98+
public int Write(ObservationWriter writer)
9999
{
100100
using (TimerStack.Instance.Scoped("CameraSensor.WriteToTensor"))
101101
{
102102
var texture = ObservationToTexture(m_Camera, m_Width, m_Height);
103-
var numWritten = Utilities.TextureToTensorProxy(texture, adapter, m_Grayscale);
103+
var numWritten = Utilities.TextureToTensorProxy(texture, writer, m_Grayscale);
104104
DestroyTexture(texture);
105105
return numWritten;
106106
}

com.unity.ml-agents/Runtime/Sensors/ISensor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ public interface ISensor
3131
int[] GetObservationShape();
3232

3333
/// <summary>
34-
/// Write the observation data directly to the <see cref="WriteAdapter"/>.
34+
/// Write the observation data directly to the <see cref="ObservationWriter"/>.
3535
/// This is considered an advanced interface; for a simpler approach, use
3636
/// <see cref="SensorBase"/> and override <see cref="SensorBase.WriteObservation"/> instead.
3737
/// Note that this (and <see cref="GetCompressedObservation"/>) may
3838
/// be called multiple times per agent step, so should not mutate any internal state.
3939
/// </summary>
40-
/// <param name="adapter">Where the observations will be written to.</param>
40+
/// <param name="writer">Where the observations will be written to.</param>
4141
/// <returns>The number of elements written.</returns>
42-
int Write(WriteAdapter adapter);
42+
int Write(ObservationWriter writer);
4343

4444
/// <summary>
4545
/// Return a compressed representation of the observation. For small observations,

0 commit comments

Comments
 (0)