Skip to content

inference - fill 0s for done Agents #3232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/GeneratorImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,30 @@ public void Generate(TensorProxy tensorProxy, int batchSize, IEnumerable<AgentIn
var agentIndex = 0;
foreach (var info in infos)
{
var tensorOffset = 0;
// Write each sensor consecutively to the tensor
foreach (var sensorIndex in m_SensorIndices)
if (info.agentInfo.done)
{
var sensor = info.sensors[sensorIndex];
m_WriteAdapter.SetTarget(tensorProxy, agentIndex, tensorOffset);
var numWritten = sensor.Write(m_WriteAdapter);
tensorOffset += numWritten;
// If the agent is done, we might have a stale reference to the sensors
// e.g. a dependent object might have been disposed.
// To avoid this, just fill observation with zeroes instead of calling sensor.Write.
TensorUtils.FillTensorBatch(tensorProxy, agentIndex, 0.0f);
}
else
{
var tensorOffset = 0;
// Write each sensor consecutively to the tensor
foreach (var sensorIndex in m_SensorIndices)
{
var sensor = info.sensors[sensorIndex];
m_WriteAdapter.SetTarget(tensorProxy, agentIndex, tensorOffset);
var numWritten = sensor.Write(m_WriteAdapter);
tensorOffset += numWritten;
}
Debug.AssertFormat(
tensorOffset == vecObsSizeT,
"mismatch between vector observation size ({0}) and number of observations written ({1})",
vecObsSizeT, tensorOffset
);
}
Debug.AssertFormat(
tensorOffset == vecObsSizeT,
"mismatch between vector observation size ({0}) and number of observations written ({1})",
vecObsSizeT, tensorOffset
);

agentIndex++;
}
Expand Down Expand Up @@ -356,8 +366,19 @@ public void Generate(TensorProxy tensorProxy, int batchSize, IEnumerable<AgentIn
foreach (var infoSensorPair in infos)
{
var sensor = infoSensorPair.sensors[m_SensorIndex];
m_WriteAdapter.SetTarget(tensorProxy, agentIndex, 0);
sensor.Write(m_WriteAdapter);
if (infoSensorPair.agentInfo.done)
{
// If the agent is done, we might have a stale reference to the sensors
// e.g. a dependent object might have been disposed.
// To avoid this, just fill observation with zeroes instead of calling sensor.Write.
TensorUtils.FillTensorBatch(tensorProxy, agentIndex, 0.0f);
}
else
{
m_WriteAdapter.SetTarget(tensorProxy, agentIndex, 0);
sensor.Write(m_WriteAdapter);

}
agentIndex++;
}
}
Expand Down
23 changes: 23 additions & 0 deletions UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ public static TensorProxy TensorProxyFromBarracuda(Tensor src, string nameOverri
};
}

/// <summary>
/// Fill a specific batch of a TensorProxy with a given value
/// </summary>
/// <param name="tensorProxy"></param>
/// <param name="batch">The batch index to fill.</param>
/// <param name="fillValue"></param>
public static void FillTensorBatch(TensorProxy tensorProxy, int batch, float fillValue)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could do this as a method on TensorProxy, but it follows the convention in FillTensorWithRandomNormal (below)

{
var height = tensorProxy.data.height;
var width = tensorProxy.data.width;
var channels = tensorProxy.data.channels;
for (var h = 0; h < height; h++)
{
for (var w = 0; w < width; w++)
{
for (var c = 0; c < channels; c++)
{
tensorProxy.data[batch, h, w, c] = fillValue;
}
}
}
}

/// <summary>
/// Fill a pre-allocated Tensor with random numbers
/// </summary>
Expand Down