Skip to content

[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 5 commits into from
Jan 29, 2021
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ minutes to
For any other questions or feedback, connect directly with the ML-Agents team at
ml-agents@unity3d.com.

## Privacy

In order to improve the developer experience for Unity ML-Agents Toolkit, we have added in-editor analytics.
Please refer to "Information that is passively collected by Unity" in the
[Unity Privacy Policy](https://unity3d.com/legal/privacy-policy).

## License

[Apache License 2.0](LICENSE)
10 changes: 10 additions & 0 deletions com.unity.ml-agents/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to
[Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Minor Changes
#### com.unity.ml-agents (C#)
In order to improve the developer experience for Unity ML-Agents Toolkit, we have added in-editor analytics.
Please refer to "Information that is passively collected by Unity" in the
[Unity Privacy Policy](https://unity3d.com/legal/privacy-policy).

### Bug Fixes
#### com.unity.ml-agents (C#)


## [1.0.6] - 2020-11-13
### Minor Changes
Expand Down
4 changes: 4 additions & 0 deletions com.unity.ml-agents/Documentation~/com.unity.ml-agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ If you are new to the Unity ML-Agents package, or have a question after reading
the documentation, you can checkout our [GitHUb Repository], which also includes
a number of ways to [connect with us] including our [ML-Agents Forum].

In order to improve the developer experience for Unity ML-Agents Toolkit, we have added in-editor analytics.
Please refer to "Information that is passively collected by Unity" in the
[Unity Privacy Policy](https://unity3d.com/legal/privacy-policy).

[unity ML-Agents Toolkit]: https://github.com/Unity-Technologies/ml-agents/tree/release_2_verified_docs
[unity inference engine]: https://docs.unity3d.com/Packages/com.unity.barracuda@latest/index.html
[package manager documentation]: https://docs.unity3d.com/Manual/upm-ui-install.html
Expand Down
8 changes: 8 additions & 0 deletions com.unity.ml-agents/Runtime/Analytics.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions com.unity.ml-agents/Runtime/Analytics/AnalyticsUtils.cs
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;
}
}
}
}
3 changes: 3 additions & 0 deletions com.unity.ml-agents/Runtime/Analytics/AnalyticsUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

180 changes: 180 additions & 0 deletions com.unity.ml-agents/Runtime/Analytics/Events.cs
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

yum yum

{
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;
}
}
}
3 changes: 3 additions & 0 deletions com.unity.ml-agents/Runtime/Analytics/Events.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading