-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Stats SideChannel (for custom TensorBoard metrics) #3660
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
Changes from all commits
1dfa14e
21de227
8d8a382
c98edf8
80a3e74
32f08b4
9e93bd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
namespace MLAgents.SideChannels | ||
{ | ||
/// <summary> | ||
/// Determines the behavior of how multiple stats within the same summary period are combined. | ||
/// </summary> | ||
public enum StatAggregationMethod | ||
{ | ||
/// <summary> | ||
/// Values within the summary period are averaged before reporting. | ||
/// Note that values from the same C# environment in the same step may replace each other. | ||
/// </summary> | ||
Average = 0, | ||
|
||
/// <summary> | ||
/// Only the most recent value is reported. | ||
/// To avoid conflicts between multiple environments, the ML Agents environment will only | ||
/// keep stats from worker index 0. | ||
/// </summary> | ||
MostRecent = 1 | ||
} | ||
|
||
/// <summary> | ||
/// Add stats (key-value pairs) for reporting. The ML Agents environment will send these to a StatsReporter | ||
/// instance, which means the values will appear in the Tensorboard summary, as well as trainer gauges. | ||
/// Note that stats are only written every summary_frequency steps; See <see cref="StatAggregationMethod"/> | ||
/// for options on how multiple values are handled. | ||
/// </summary> | ||
public class StatsSideChannel : SideChannel | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would TensorBoardMetricsSideChannel be a more appropriate name? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It won't just be for TB - with this change it'll show up anywhere Python stats are written (e.g. CSV, timers, and perhaps later the Console). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I kept the name generic but called out TensorBoard in the comments. I'll also add an example of this in the "Using TensorBoard" docs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, let's then document the different ways that adding a stat here will bubble up on the Python side. I find the term stats odd since TB refers to metrics, but I also can't see a better alternative (esp that it does align with timer stats) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the comments below up here and clarified a bit. |
||
{ | ||
const string k_StatsSideChannelDefaultId = "a1d8f7b7-cec8-50f9-b78b-d3e165a78520"; | ||
|
||
/// <summary> | ||
/// Initializes the side channel with the provided channel ID. | ||
/// The constructor is internal because only one instance is | ||
/// supported at a time, and is created by the Academy. | ||
/// </summary> | ||
internal StatsSideChannel() | ||
{ | ||
ChannelId = new Guid(k_StatsSideChannelDefaultId); | ||
} | ||
|
||
/// <summary> | ||
/// Add a stat value for reporting. This will appear in the Tensorboard summary and trainer gauges. | ||
chriselion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// You can nest stats in Tensorboard with "/". | ||
/// Note that stats are only written to Tensorboard each summary_frequency steps; if a stat is | ||
/// received multiple times, only the most recent version is used. | ||
/// To avoid conflicts between multiple environments, only stats from worker index 0 are used. | ||
/// </summary> | ||
/// <param name="key">The stat name.</param> | ||
/// <param name="value">The stat value. You can nest stats in Tensorboard by using "/". </param> | ||
/// <param name="aggregationMethod">How multiple values should be treated.</param> | ||
public void AddStat( | ||
string key, float value, StatAggregationMethod aggregationMethod = StatAggregationMethod.Average | ||
) | ||
{ | ||
using (var msg = new OutgoingMessage()) | ||
{ | ||
msg.WriteString(key); | ||
msg.WriteFloat32(value); | ||
msg.WriteInt32((int)aggregationMethod); | ||
QueueMessageToSend(msg); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void OnMessageReceived(IncomingMessage msg) | ||
{ | ||
throw new UnityAgentsException("StatsSideChannel should never receive messages."); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from mlagents_envs.side_channel import SideChannel, IncomingMessage | ||
import uuid | ||
from typing import Dict, Tuple | ||
from enum import Enum | ||
|
||
|
||
# Determines the behavior of how multiple stats within the same summary period are combined. | ||
class StatsAggregationMethod(Enum): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a better place for this? It needs to live in ml-agents-envs (unless we move the SideChannel?) but it feels strange importing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this actually seems the least obtrusive since the StatsReporter lives in |
||
# Values within the summary period are averaged before reporting. | ||
AVERAGE = 0 | ||
|
||
# Only the most recent value is reported. | ||
MOST_RECENT = 1 | ||
|
||
|
||
class StatsSideChannel(SideChannel): | ||
""" | ||
Side channel that receives (string, float) pairs from the environment, so that they can eventually | ||
be passed to a StatsReporter. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
# >>> uuid.uuid5(uuid.NAMESPACE_URL, "com.unity.ml-agents/StatsSideChannel") | ||
# UUID('a1d8f7b7-cec8-50f9-b78b-d3e165a78520') | ||
super().__init__(uuid.UUID("a1d8f7b7-cec8-50f9-b78b-d3e165a78520")) | ||
|
||
self.stats: Dict[str, Tuple[float, StatsAggregationMethod]] = {} | ||
|
||
def on_message_received(self, msg: IncomingMessage) -> None: | ||
chriselion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Receive the message from the environment, and save it for later retrieval. | ||
:param msg: | ||
:return: | ||
""" | ||
key = msg.read_string() | ||
val = msg.read_float32() | ||
agg_type = StatsAggregationMethod(msg.read_int32()) | ||
|
||
self.stats[key] = (val, agg_type) | ||
|
||
def get_and_reset_stats(self) -> Dict[str, Tuple[float, StatsAggregationMethod]]: | ||
""" | ||
Returns the current stats, and resets the internal storage of the stats. | ||
:return: | ||
""" | ||
s = self.stats | ||
self.stats = {} | ||
return s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vincentpierre @surfnerd I think we had talked about this before. It seems like a clean way to get access to sidechannels without adding a custom method to Academy for each one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can replace Academy.FloatProperties with this later if we're happy with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's replace Academy.Instance.FloatProperty then. Do you want to do it in this PR or make another one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do it in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://jira.unity3d.com/browse/MLA-799