-
Notifications
You must be signed in to change notification settings - Fork 4.3k
[Verified] Backport analytics events #4892
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace Unity.MLAgents.Analytics | ||
{ | ||
internal static class AnalyticsUtils | ||
{ | ||
/// <summary> | ||
/// Hash a string to remove PII or secret info before sending to analytics | ||
/// </summary> | ||
/// <param name="s"></param> | ||
/// <returns>A string containing the Hash128 of the input string.</returns> | ||
public static string Hash(string s) | ||
{ | ||
var behaviorNameHash = Hash128.Compute(s); | ||
return behaviorNameHash.ToString(); | ||
} | ||
|
||
internal static bool s_SendEditorAnalytics = true; | ||
|
||
/// <summary> | ||
/// Helper class to temporarily disable sending analytics from unit tests. | ||
/// </summary> | ||
internal class DisableAnalyticsSending : IDisposable | ||
{ | ||
private bool m_PreviousSendEditorAnalytics; | ||
|
||
public DisableAnalyticsSending() | ||
{ | ||
m_PreviousSendEditorAnalytics = s_SendEditorAnalytics; | ||
s_SendEditorAnalytics = false; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
s_SendEditorAnalytics = m_PreviousSendEditorAnalytics; | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Unity.MLAgents.Policies; | ||
using Unity.MLAgents.Sensors; | ||
|
||
namespace Unity.MLAgents.Analytics | ||
{ | ||
internal struct InferenceEvent | ||
{ | ||
/// <summary> | ||
/// Hash of the BehaviorName. | ||
/// </summary> | ||
public string BehaviorName; | ||
public string BarracudaModelSource; | ||
public string BarracudaModelVersion; | ||
public string BarracudaModelProducer; | ||
public string BarracudaPackageVersion; | ||
/// <summary> | ||
/// Whether inference is performed on CPU (0) or GPU (1). | ||
/// </summary> | ||
public int InferenceDevice; | ||
public List<EventObservationSpec> ObservationSpecs; | ||
public EventActionSpec ActionSpec; | ||
public int MemorySize; | ||
public long TotalWeightSizeBytes; | ||
public string ModelHash; | ||
} | ||
|
||
/// <summary> | ||
/// Simplified version of ActionSpec struct for use in analytics | ||
/// </summary> | ||
[Serializable] | ||
internal struct EventActionSpec | ||
{ | ||
public int NumContinuousActions; | ||
public int NumDiscreteActions; | ||
public int[] BranchSizes; | ||
|
||
public static EventActionSpec FromBrainParameters(BrainParameters brainParameters) | ||
{ | ||
if (brainParameters.VectorActionSpaceType == SpaceType.Continuous) | ||
{ | ||
return new EventActionSpec | ||
{ | ||
NumContinuousActions = brainParameters.NumActions, | ||
NumDiscreteActions = 0, | ||
BranchSizes = Array.Empty<int>(), | ||
}; | ||
} | ||
else | ||
{ | ||
return new EventActionSpec | ||
{ | ||
NumContinuousActions = 0, | ||
NumDiscreteActions = brainParameters.NumActions, | ||
BranchSizes = brainParameters.VectorActionSize, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Information about one dimension of an observation. | ||
/// </summary> | ||
[Serializable] | ||
internal struct EventObservationDimensionInfo | ||
{ | ||
public int Size; | ||
public int Flags; | ||
} | ||
|
||
/// <summary> | ||
/// Simplified summary of Agent observations for use in analytics | ||
/// </summary> | ||
[Serializable] | ||
internal struct EventObservationSpec | ||
{ | ||
public string SensorName; | ||
public string CompressionType; | ||
public int BuiltInSensorType; | ||
public EventObservationDimensionInfo[] DimensionInfos; | ||
|
||
public static EventObservationSpec FromSensor(ISensor sensor) | ||
{ | ||
var shape = sensor.GetObservationShape(); | ||
var dimInfos = new EventObservationDimensionInfo[shape.Length]; | ||
for (var i = 0; i < shape.Length; i++) | ||
{ | ||
dimInfos[i].Size = shape[i]; | ||
// TODO copy flags when we have them | ||
} | ||
|
||
var builtInSensorType = sensor.GetBuiltInSensorType(); | ||
|
||
return new EventObservationSpec | ||
{ | ||
SensorName = sensor.GetName(), | ||
CompressionType = sensor.GetCompressionType().ToString(), | ||
BuiltInSensorType = (int)builtInSensorType, | ||
DimensionInfos = dimInfos, | ||
}; | ||
} | ||
} | ||
|
||
internal struct RemotePolicyInitializedEvent | ||
{ | ||
public string TrainingSessionGuid; | ||
/// <summary> | ||
/// Hash of the BehaviorName. | ||
/// </summary> | ||
public string BehaviorName; | ||
public List<EventObservationSpec> ObservationSpecs; | ||
public EventActionSpec ActionSpec; | ||
|
||
/// <summary> | ||
/// This will be the same as TrainingEnvironmentInitializedEvent if available, but | ||
/// TrainingEnvironmentInitializedEvent maybe not always be available with older trainers. | ||
/// </summary> | ||
public string MLAgentsEnvsVersion; | ||
public string TrainerCommunicationVersion; | ||
} | ||
|
||
// These were added as part of a new interface in https://github.com/Unity-Technologies/ml-agents/pull/4871/ | ||
// Since we can't add a new interface in a patch release, we'll detect the type of the sensor and return | ||
// the enum accordingly | ||
internal enum BuiltInSensorType | ||
{ | ||
Unknown = 0, | ||
VectorSensor = 1, | ||
// Note that StackingSensor actually returns the wrapped sensor's type | ||
StackingSensor = 2, | ||
RayPerceptionSensor = 3, | ||
// ReflectionSensor = 4, // Added after 1.0.x | ||
CameraSensor = 5, | ||
RenderTextureSensor = 6, | ||
// BufferSensor = 7, // Added after 1.0.x | ||
// PhysicsBodySensor = 8, // In extensions package | ||
// Match3Sensor = 9, // In extensions package | ||
// GridSensor = 10 // In extensions package | ||
} | ||
|
||
/// <summary> | ||
/// Helper methods to be shared by all classes that implement <see cref="ISensor"/>. | ||
/// </summary> | ||
internal static class BuiltInSensorExtensions | ||
{ | ||
/// <summary> | ||
/// Get the total number of elements in the ISensor's observation (i.e. the product of the | ||
/// shape elements). | ||
/// </summary> | ||
/// <param name="sensor"></param> | ||
/// <returns></returns> | ||
public static BuiltInSensorType GetBuiltInSensorType(this ISensor sensor) | ||
{ | ||
if (sensor as VectorSensor != null) | ||
{ | ||
return BuiltInSensorType.VectorSensor; | ||
} | ||
if (sensor as RayPerceptionSensor != null) | ||
{ | ||
return BuiltInSensorType.RayPerceptionSensor; | ||
} | ||
if (sensor as CameraSensor != null) | ||
{ | ||
return BuiltInSensorType.CameraSensor; | ||
} | ||
if (sensor as RenderTextureSensor != null) | ||
{ | ||
return BuiltInSensorType.RenderTextureSensor; | ||
} | ||
var stackingSensor = sensor as StackingSensor; | ||
if (stackingSensor != null) | ||
{ | ||
// Recurse on the wrapped sensor | ||
return stackingSensor.GetWrappedSensor().GetBuiltInSensorType() ; | ||
} | ||
return BuiltInSensorType.Unknown; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
yum yum