Skip to content

Make analytics module an optional dependency. #5109

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 16 commits into from
Mar 15, 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
37 changes: 37 additions & 0 deletions .yamato/com.unity.ml-agents-optional-dep-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
OptionalDependencyTestsLinux:
name : LinuxOptionalDependenciesTests
agent:
type: Unity::VM
image: package-ci/ubuntu:stable
flavor: b1.medium
commands:
- |
curl -L https://artifactory.prd.it.unity3d.com/artifactory/api/gpg/key/public | sudo apt-key add -
sudo sh -c "echo 'deb https://artifactory.prd.it.unity3d.com/artifactory/unity-apt-local bionic main' > /etc/apt/sources.list.d/unity.list"
sudo apt update
sudo apt install -y unity-config
npm install upm-ci-utils@stable -g --registry https://artifactory.prd.cds.internal.unity3d.com/artifactory/api/npm/upm-npm
unity-config settings editor-path ./.Editor
unity-config project create opt-deps-test
unity-config project add dependency com.unity.ml-agents/
unity-config project add testable com.unity.ml-agents
unity-config project add dependency [email protected]
unity-config project add dependency [email protected]
unity-config project add dependency [email protected]
unity-config project add dependency [email protected]
upm-ci project test -u 2019.4 --type project-tests --project-path opt-deps-test --package-filter com.unity.ml-agents
artifacts:
logs:
paths:
- "upm-ci~/test-results/**/*"
dependencies:
- .yamato/com.unity.ml-agents-pack.yml#pack
triggers:
cancel_old_ci: true
expression: |
(pull_request.target eq "main" OR
pull_request.target match "release.+") AND
NOT pull_request.draft AND
(pull_request.changes.any match "com.unity.ml-agents/**" OR
pull_request.changes.any match ".yamato/com.unity.ml-agents-test.yml")

1 change: 1 addition & 0 deletions .yamato/com.unity.ml-agents-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,4 @@ test_{{ package.name }}_{{ platform.name }}_trunk:
{% endfor %}
{% endfor %}
{% endfor %}

2 changes: 0 additions & 2 deletions Project/Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"dependencies": {
"com.unity.ads": "2.0.8",
"com.unity.analytics": "3.2.3",
"com.unity.collab-proxy": "1.2.15",
"com.unity.ml-agents": "file:../../com.unity.ml-agents",
"com.unity.ml-agents.extensions": "file:../../com.unity.ml-agents.extensions",
"com.unity.package-manager-ui": "2.0.13",
"com.unity.purchasing": "2.2.1",
"com.unity.textmeshpro": "1.4.1",
"com.unity.modules.ai": "1.0.0",
"com.unity.modules.animation": "1.0.0",
Expand Down
1 change: 1 addition & 0 deletions com.unity.ml-agents/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and this project adheres to
### Minor Changes
#### com.unity.ml-agents / com.unity.ml-agents.extensions (C#)
- Updated com.unity.barracuda to 1.3.2-preview. (#5084)
- Make com.unity.modules.unityanalytics an optional dependency. (#5109)
- Added 3D Ball to the `com.unity.ml-agents` samples. (#5077)
#### ml-agents / ml-agents-envs / gym-unity (Python)
- The `encoding_size` setting for RewardSignals has been deprecated. Please use `network_settings` instead. (#4982)
Expand Down
28 changes: 22 additions & 6 deletions com.unity.ml-agents/Runtime/Analytics/InferenceAnalytics.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
#if MLA_UNITY_ANALYTICS_MODULE || !UNITY_2019_4_OR_NEWER
#define MLA_UNITY_ANALYTICS_MODULE_ENABLED
#endif


using System;
using System.Collections.Generic;
using System.Diagnostics;
using Unity.Barracuda;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Inference;
using Unity.MLAgents.Policies;
using Unity.MLAgents.Sensors;
using UnityEngine;

#if MLA_UNITY_ANALYTICS_MODULE_ENABLED
using UnityEngine.Analytics;
#endif


#if UNITY_EDITOR
using UnityEditor;
#if MLA_UNITY_ANALYTICS_MODULE_ENABLED
using UnityEditor.Analytics;
#endif
#endif // MLA_UNITY_ANALYTICS_MODULE_ENABLED
#endif // UNITY_EDITOR


namespace Unity.MLAgents.Analytics
Expand Down Expand Up @@ -50,16 +62,19 @@ static bool EnableAnalytics()
return true;
}

#if UNITY_EDITOR
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey, k_EventVersion);
#else
if (result == AnalyticsResult.Ok)
{
s_EventRegistered = true;
}
#elif MLA_UNITY_ANALYTICS_MODULE_ENABLED
AnalyticsResult result = AnalyticsResult.UnsupportedPlatform;
#endif
if (result == AnalyticsResult.Ok)
{
s_EventRegistered = true;
}

#endif
if (s_EventRegistered && s_SentModels == null)
{
s_SentModels = new HashSet<NNModel>();
Expand Down Expand Up @@ -89,6 +104,7 @@ public static bool IsAnalyticsEnabled()
/// <param name="actionSpec">ActionSpec for the Agent. Used to generate information about the action space.</param>
/// <param name="actuators">List of IActuators for the Agent. Used to generate information about the action space.</param>
/// <returns></returns>
[Conditional("MLA_UNITY_ANALYTICS_MODULE_ENABLED")]
public static void InferenceModelSet(
NNModel nnModel,
string behaviorName,
Expand Down Expand Up @@ -117,7 +133,7 @@ IList<IActuator> actuators
var data = GetEventForModel(nnModel, behaviorName, inferenceDevice, sensors, actionSpec, actuators);
// Note - to debug, use JsonUtility.ToJson on the event.
// Debug.Log(JsonUtility.ToJson(data, true));
#if UNITY_EDITOR
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
if (AnalyticsUtils.s_SendEditorAnalytics)
{
EditorAnalytics.SendEventWithLimit(k_EventName, data, k_EventVersion);
Expand Down
38 changes: 25 additions & 13 deletions com.unity.ml-agents/Runtime/Analytics/TrainingAnalytics.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
#if MLA_UNITY_ANALYTICS_MODULE || !UNITY_2019_4_OR_NEWER
#define MLA_UNITY_ANALYTICS_MODULE_ENABLED
#endif

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
using UnityEngine;
#if MLA_UNITY_ANALYTICS_MODULE_ENABLED
using UnityEngine.Analytics;
#if UNITY_EDITOR
using UnityEditor.Analytics;
#endif
#endif

#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Analytics;
#endif

namespace Unity.MLAgents.Analytics
Expand Down Expand Up @@ -56,22 +65,22 @@ internal class TrainingAnalytics

static bool EnableAnalytics()
{
#if MLA_UNITY_ANALYTICS_MODULE_ENABLED
if (s_EventsRegistered)
{
return true;
}

foreach (var eventName in s_EventNames)
{
#if UNITY_EDITOR
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(eventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey);
#else
AnalyticsResult result = AnalyticsResult.UnsupportedPlatform;
#endif
if (result != AnalyticsResult.Ok)
{
return false;
}
#else
return false;
#endif // UNITY_EDITOR
}
s_EventsRegistered = true;

Expand All @@ -83,13 +92,17 @@ static bool EnableAnalytics()
}

return s_EventsRegistered;
#else
return false;
#endif // MLA_UNITY_ANALYTICS_MODULE_ENABLED
}

/// <summary>
/// Cache information about the trainer when it becomes available in the RpcCommunicator.
/// </summary>
/// <param name="communicationVersion"></param>
/// <param name="packageVersion"></param>
[Conditional("MLA_UNITY_ANALYTICS_MODULE_ENABLED")]
public static void SetTrainerInformation(string packageVersion, string communicationVersion)
{
s_TrainerPackageVersion = packageVersion;
Expand All @@ -98,13 +111,14 @@ public static void SetTrainerInformation(string packageVersion, string communica

public static bool IsAnalyticsEnabled()
{
#if UNITY_EDITOR
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
return EditorAnalytics.enabled;
#else
return false;
#endif
}

[Conditional("MLA_UNITY_ANALYTICS_MODULE_ENABLED")]
public static void TrainingEnvironmentInitialized(TrainingEnvironmentInitializedEvent tbiEvent)
{
if (!IsAnalyticsEnabled())
Expand All @@ -126,16 +140,15 @@ public static void TrainingEnvironmentInitialized(TrainingEnvironmentInitialized
// Debug.Log(
// $"Would send event {k_TrainingEnvironmentInitializedEventName} with body {JsonUtility.ToJson(tbiEvent, true)}"
// );
#if UNITY_EDITOR
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
if (AnalyticsUtils.s_SendEditorAnalytics)
{
EditorAnalytics.SendEventWithLimit(k_TrainingEnvironmentInitializedEventName, tbiEvent);
}
#else
return;
#endif
}

[Conditional("MLA_UNITY_ANALYTICS_MODULE_ENABLED")]
public static void RemotePolicyInitialized(
string fullyQualifiedBehaviorName,
IList<ISensor> sensors,
Expand Down Expand Up @@ -164,13 +177,11 @@ IList<IActuator> actuators
// Debug.Log(
// $"Would send event {k_RemotePolicyInitializedEventName} with body {JsonUtility.ToJson(data, true)}"
// );
#if UNITY_EDITOR
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
if (AnalyticsUtils.s_SendEditorAnalytics)
{
EditorAnalytics.SendEventWithLimit(k_RemotePolicyInitializedEventName, data);
}
#else
return;
#endif
}

Expand All @@ -186,6 +197,7 @@ internal static string ParseBehaviorName(string fullyQualifiedBehaviorName)
return fullyQualifiedBehaviorName.Substring(0, lastQuestionIndex);
}

[Conditional("MLA_UNITY_ANALYTICS_MODULE_ENABLED")]
public static void TrainingBehaviorInitialized(TrainingBehaviorInitializedEvent tbiEvent)
{
if (!IsAnalyticsEnabled())
Expand All @@ -211,7 +223,7 @@ public static void TrainingBehaviorInitialized(TrainingBehaviorInitializedEvent
// Debug.Log(
// $"Would send event {k_TrainingBehaviorInitializedEventName} with body {JsonUtility.ToJson(tbiEvent, true)}"
// );
#if UNITY_EDITOR
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
if (AnalyticsUtils.s_SendEditorAnalytics)
{
EditorAnalytics.SendEventWithLimit(k_TrainingBehaviorInitializedEventName, tbiEvent);
Expand Down
4 changes: 1 addition & 3 deletions com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
using UnityEngine;
using System.Runtime.CompilerServices;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Analytics;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Demonstrations;
using Unity.MLAgents.Policies;

using Unity.MLAgents.Analytics;

[assembly: InternalsVisibleTo("Unity.ML-Agents.Editor")]
[assembly: InternalsVisibleTo("Unity.ML-Agents.Editor.Tests")]
Expand Down Expand Up @@ -525,7 +525,6 @@ internal static bool IsTrivialMapping(ISensor sensor)
}

#region Analytics

internal static TrainingEnvironmentInitializedEvent ToTrainingEnvironmentInitializedEvent(
this TrainingEnvironmentInitialized inputProto)
{
Expand Down Expand Up @@ -569,7 +568,6 @@ internal static TrainingBehaviorInitializedEvent ToTrainingBehaviorInitializedEv
NumNetworkHiddenUnits = inputProto.NumNetworkHiddenUnits,
};
}

#endregion

}
Expand Down
4 changes: 2 additions & 2 deletions com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
using System.Linq;
using UnityEngine;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Analytics;
using Unity.MLAgents.CommunicatorObjects;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.SideChannels;
using Google.Protobuf;

using Unity.MLAgents.Analytics;

namespace Unity.MLAgents
{
/// Responsible for communication with External using gRPC.
Expand Down Expand Up @@ -144,7 +145,6 @@ out input

var pythonPackageVersion = initializationInput.RlInitializationInput.PackageVersion;
var pythonCommunicationVersion = initializationInput.RlInitializationInput.CommunicationVersion;

TrainingAnalytics.SetTrainerInformation(pythonPackageVersion, pythonCommunicationVersion);

var communicationIsCompatible = CheckCommunicationVersionsAreCompatible(
Expand Down
3 changes: 2 additions & 1 deletion com.unity.ml-agents/Runtime/Policies/RemotePolicy.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

using System.Collections.Generic;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Analytics;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Analytics;


namespace Unity.MLAgents.Policies
Expand Down
9 changes: 8 additions & 1 deletion com.unity.ml-agents/Runtime/Unity.ML-Agents.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,12 @@
"Grpc.Core.dll"
],
"autoReferenced": true,
"defineConstraints": []
"defineConstraints": [],
"versionDefines": [
{
"name": "com.unity.modules.unityanalytics",
"expression": "1.0.0",
"define": "MLA_UNITY_ANALYTICS_MODULE"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
using UnityEngine;
using Unity.Barracuda;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Analytics;
using Unity.MLAgents.Policies;
using Unity.MLAgents.Analytics;
using UnityEditor;


namespace Unity.MLAgents.Tests.Analytics
{
[TestFixture]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using NUnit.Framework;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Analytics;
using Unity.MLAgents.CommunicatorObjects;
using Unity.MLAgents.Demonstrations;
using Unity.MLAgents.Policies;
using Unity.MLAgents.Sensors;

using Unity.MLAgents.Analytics;
using Unity.MLAgents.CommunicatorObjects;

namespace Unity.MLAgents.Tests
{
[TestFixture]
Expand Down Expand Up @@ -171,7 +172,6 @@ public void TestIsTrivialMapping()
sparseChannelSensor.Mapping = new[] { 0, 0, 0, 1, 1, 1 };
Assert.AreEqual(GrpcExtensions.IsTrivialMapping(sparseChannelSensor), false);
}

[Test]
public void TestDefaultTrainingEvents()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [
{
"name": "com.unity.modules.unityanalytics",
"expression": "1.0.0",
"define": "MLA_UNITY_ANALYTICS_MODULE"
}
]
}
Loading