-
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 4 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 Int64 ModelHash; //TODO ? | ||
} | ||
|
||
/// <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,97 @@ | ||
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.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"; | ||
|
||
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; | ||
} | ||
|
||
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 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.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)); | ||
} | ||
|
||
return inferenceEvent; | ||
chriselion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
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.