-
Notifications
You must be signed in to change notification settings - Fork 4.3k
In-Editor Analytics for inference #4677
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 7 commits
0a3b1c2
0c4c9d8
99a5325
ea8dec8
333cc87
2db88ff
b7748da
362ed03
0f24a16
59c4b38
405c4ef
140632e
add10c4
e85fd50
8c07e5e
9b3ece7
2ba6d35
79c4741
b577ec2
3b7f742
b9a4aca
de08a28
c69589e
b8cec80
f4ebc34
6a2af46
c958b8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Unity.MLAgents.Actuators; | ||
using Unity.MLAgents.Sensors; | ||
|
||
namespace Unity.MLAgents.Analytics | ||
{ | ||
internal struct InferenceEvent | ||
{ | ||
public string BehaviorName; | ||
public string BarracudaModelSource; | ||
public string BarracudaModelVersion; | ||
public string BarracudaModelProducer; | ||
public string BarracudaPackageVersion; | ||
public int InferenceDevice; | ||
public List<EventObservationSpec> ObservationSpecs; | ||
public EventActionSpec ActionSpec; | ||
public int MemorySize; | ||
public string ModelHash; | ||
} | ||
|
||
/// <summary> | ||
/// Simplified version of ActionSpec struct for use in analytics | ||
/// </summary> | ||
internal struct EventActionSpec | ||
{ | ||
public int NumContinuousActions; | ||
public int NumDiscreteActions; | ||
public int[] BranchSizes; | ||
|
||
public static EventActionSpec FromActionSpec(ActionSpec actionSpec) | ||
{ | ||
var branchSizes = actionSpec.BranchSizes ?? Array.Empty<int>(); | ||
return new EventActionSpec | ||
{ | ||
NumContinuousActions = actionSpec.NumContinuousActions, | ||
NumDiscreteActions = actionSpec.NumDiscreteActions, | ||
BranchSizes = branchSizes, | ||
}; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Simplified summary of Agent observations for use in analytics | ||
/// </summary> | ||
internal struct EventObservationSpec | ||
{ | ||
public string SensorName; | ||
public int[] ObservationShape; | ||
chriselion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public static EventObservationSpec FromSensor(ISensor sensor) | ||
{ | ||
return new EventObservationSpec | ||
{ | ||
SensorName = sensor.GetName(), | ||
ObservationShape = sensor.GetObservationShape(), | ||
}; | ||
} | ||
} | ||
} |
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,180 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Unity.Barracuda; | ||
using Unity.MLAgents.Actuators; | ||
using Unity.MLAgents.Inference; | ||
using Unity.MLAgents.Policies; | ||
using Unity.MLAgents.Sensors; | ||
using UnityEditor; | ||
using UnityEditor.Analytics; | ||
chriselion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using UnityEngine; | ||
using UnityEngine.Analytics; | ||
|
||
namespace Unity.MLAgents.Analytics | ||
{ | ||
internal class InferenceAnalytics | ||
{ | ||
static bool s_EventRegistered = false; | ||
const int k_MaxEventsPerHour = 1000; | ||
chriselion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const int k_MaxNumberOfElements = 1000; | ||
const string k_VendorKey = "unity.ml-agents"; | ||
const string k_EventName = "InferenceModelSet"; | ||
|
||
private static HashSet<NNModel> s_SentModels; | ||
|
||
static bool EnableAnalytics() | ||
{ | ||
if (s_EventRegistered) | ||
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. This is adapted from the example here (internal link) |
||
{ | ||
return true; | ||
} | ||
|
||
if (s_SentModels == null) | ||
{ | ||
s_SentModels = new HashSet<NNModel>(); | ||
} | ||
|
||
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey); | ||
if (result == AnalyticsResult.Ok) | ||
{ | ||
s_EventRegistered = true; | ||
} | ||
|
||
return s_EventRegistered; | ||
} | ||
|
||
public static void InferenceModelSet( | ||
NNModel nnModel, | ||
string behaviorName, | ||
InferenceDevice inferenceDevice, | ||
IList<ISensor> sensors, | ||
ActionSpec actionSpec | ||
) | ||
{ | ||
// The event shouldn't be able to report if this is disabled but if we know we're not going to report | ||
// Lets early out and not waste time gathering all the data | ||
if (!EditorAnalytics.enabled) | ||
return; | ||
|
||
if (!EnableAnalytics()) | ||
return; | ||
|
||
chriselion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var added = s_SentModels.Add(nnModel); | ||
|
||
if (!added) | ||
{ | ||
// We previously added this model. Exit so we don't resend. | ||
return; | ||
} | ||
|
||
var data = GetEventForModel(nnModel, behaviorName, inferenceDevice, sensors, actionSpec); | ||
//EditorAnalytics.SendEventWithLimit(k_EventName, data); | ||
} | ||
|
||
static InferenceEvent GetEventForModel( | ||
NNModel nnModel, | ||
string behaviorName, | ||
InferenceDevice inferenceDevice, | ||
IList<ISensor> sensors, | ||
ActionSpec actionSpec | ||
) | ||
{ | ||
var barracudaModel = ModelLoader.Load(nnModel); | ||
var inferenceEvent = new InferenceEvent(); | ||
inferenceEvent.BehaviorName = behaviorName; | ||
inferenceEvent.BarracudaModelSource = barracudaModel.IrSource; | ||
inferenceEvent.BarracudaModelVersion = barracudaModel.IrVersion; | ||
inferenceEvent.BarracudaModelProducer = barracudaModel.ProducerName; | ||
inferenceEvent.MemorySize = (int)barracudaModel.GetTensorByName(TensorNames.MemorySize)[0]; | ||
inferenceEvent.InferenceDevice = (int)inferenceDevice; | ||
|
||
if (barracudaModel.ProducerName == "Script") | ||
{ | ||
// .nn files don't have these fields set correctly. Assign some placeholder values. | ||
inferenceEvent.BarracudaModelSource = "NN"; | ||
inferenceEvent.BarracudaModelProducer = "tf2bc.py"; | ||
} | ||
|
||
#if UNITY_2019_3_OR_NEWER | ||
var barracudaPackageInfo = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(Tensor).Assembly); | ||
inferenceEvent.BarracudaPackageVersion = barracudaPackageInfo.version; | ||
#else | ||
inferenceEvent.BarracudaPackageVersion = "unknown"; | ||
#endif | ||
|
||
inferenceEvent.ActionSpec = EventActionSpec.FromActionSpec(actionSpec); | ||
inferenceEvent.ObservationSpecs = new List<EventObservationSpec>(sensors.Count); | ||
foreach (var sensor in sensors) | ||
{ | ||
inferenceEvent.ObservationSpecs.Add(EventObservationSpec.FromSensor(sensor)); | ||
} | ||
|
||
inferenceEvent.ModelHash = GetModelHash(barracudaModel); | ||
return inferenceEvent; | ||
chriselion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
internal class FNVHash | ||
chriselion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
const ulong kFNV_prime = 1099511628211; | ||
const ulong kFNV_offset_basis = 14695981039346656037; | ||
private const int kMaxBytes = 1024; | ||
|
||
public ulong hash; | ||
|
||
public FNVHash() | ||
{ | ||
hash = kFNV_offset_basis; | ||
} | ||
|
||
public void Append(float[] values) | ||
{ | ||
// Limit the max number of float bytes that we hash for performance. | ||
// This increases the chance of a collision, but this should still be extremely rare. | ||
var bytesToHash = Mathf.Min(kMaxBytes, Buffer.ByteLength(values)); | ||
for (var i = 0; i < bytesToHash; i++) | ||
{ | ||
var b = Buffer.GetByte(values, i); | ||
Update(b); | ||
} | ||
} | ||
|
||
public void Append(string value) | ||
{ | ||
foreach (var c in value) | ||
{ | ||
Update((byte)c); | ||
} | ||
} | ||
|
||
private void Update(byte b) | ||
{ | ||
hash *= kFNV_prime; | ||
hash ^= b; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return hash.ToString(); | ||
} | ||
} | ||
|
||
static string GetModelHash(Model barracudaModel) | ||
{ | ||
// Pre-2020 versions of Unity don't have Hash128.Append() (can only hash strings) | ||
// For these versions, we'll use a simple FNV-1 hash. | ||
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function | ||
#if UNITY_2020_1_OR_NEWER | ||
var hash = new Hash128(); | ||
#else | ||
var hash = new FNVHash(); | ||
#endif | ||
foreach (var layer in barracudaModel.layers) | ||
{ | ||
hash.Append(layer.name); | ||
hash.Append(layer.weights); | ||
} | ||
|
||
return hash.ToString(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.