Skip to content

Commit 1b0577e

Browse files
surfnerdChris Elion
andauthored
Make analytics module an optional dependency. (#5109)
Co-authored-by: Chris Elion <[email protected]>
1 parent 02c5b9a commit 1b0577e

File tree

14 files changed

+112
-34
lines changed

14 files changed

+112
-34
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
OptionalDependencyTestsLinux:
2+
name : LinuxOptionalDependenciesTests
3+
agent:
4+
type: Unity::VM
5+
image: package-ci/ubuntu:stable
6+
flavor: b1.medium
7+
commands:
8+
- |
9+
curl -L https://artifactory.prd.it.unity3d.com/artifactory/api/gpg/key/public | sudo apt-key add -
10+
sudo sh -c "echo 'deb https://artifactory.prd.it.unity3d.com/artifactory/unity-apt-local bionic main' > /etc/apt/sources.list.d/unity.list"
11+
sudo apt update
12+
sudo apt install -y unity-config
13+
npm install upm-ci-utils@stable -g --registry https://artifactory.prd.cds.internal.unity3d.com/artifactory/api/npm/upm-npm
14+
unity-config settings editor-path ./.Editor
15+
unity-config project create opt-deps-test
16+
unity-config project add dependency com.unity.ml-agents/
17+
unity-config project add testable com.unity.ml-agents
18+
unity-config project add dependency [email protected]
19+
unity-config project add dependency [email protected]
20+
unity-config project add dependency [email protected]
21+
unity-config project add dependency [email protected]
22+
upm-ci project test -u 2019.4 --type project-tests --project-path opt-deps-test --package-filter com.unity.ml-agents
23+
artifacts:
24+
logs:
25+
paths:
26+
- "upm-ci~/test-results/**/*"
27+
dependencies:
28+
- .yamato/com.unity.ml-agents-pack.yml#pack
29+
triggers:
30+
cancel_old_ci: true
31+
expression: |
32+
(pull_request.target eq "main" OR
33+
pull_request.target match "release.+") AND
34+
NOT pull_request.draft AND
35+
(pull_request.changes.any match "com.unity.ml-agents/**" OR
36+
pull_request.changes.any match ".yamato/com.unity.ml-agents-test.yml")
37+

.yamato/com.unity.ml-agents-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,4 @@ test_{{ package.name }}_{{ platform.name }}_trunk:
149149
{% endfor %}
150150
{% endfor %}
151151
{% endfor %}
152+

Project/Packages/manifest.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
{
22
"dependencies": {
33
"com.unity.ads": "2.0.8",
4-
"com.unity.analytics": "3.2.3",
54
"com.unity.collab-proxy": "1.2.15",
65
"com.unity.ml-agents": "file:../../com.unity.ml-agents",
76
"com.unity.ml-agents.extensions": "file:../../com.unity.ml-agents.extensions",
87
"com.unity.package-manager-ui": "2.0.13",
9-
"com.unity.purchasing": "2.2.1",
108
"com.unity.textmeshpro": "1.4.1",
119
"com.unity.modules.ai": "1.0.0",
1210
"com.unity.modules.animation": "1.0.0",

com.unity.ml-agents/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to
3232
### Minor Changes
3333
#### com.unity.ml-agents / com.unity.ml-agents.extensions (C#)
3434
- Updated com.unity.barracuda to 1.3.2-preview. (#5084)
35+
- Make com.unity.modules.unityanalytics an optional dependency. (#5109)
3536
- Added 3D Ball to the `com.unity.ml-agents` samples. (#5077)
3637
#### ml-agents / ml-agents-envs / gym-unity (Python)
3738
- The `encoding_size` setting for RewardSignals has been deprecated. Please use `network_settings` instead. (#4982)

com.unity.ml-agents/Runtime/Analytics/InferenceAnalytics.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1+
#if MLA_UNITY_ANALYTICS_MODULE || !UNITY_2019_4_OR_NEWER
2+
#define MLA_UNITY_ANALYTICS_MODULE_ENABLED
3+
#endif
4+
5+
16
using System;
27
using System.Collections.Generic;
8+
using System.Diagnostics;
39
using Unity.Barracuda;
410
using Unity.MLAgents.Actuators;
511
using Unity.MLAgents.Inference;
612
using Unity.MLAgents.Policies;
713
using Unity.MLAgents.Sensors;
814
using UnityEngine;
15+
16+
#if MLA_UNITY_ANALYTICS_MODULE_ENABLED
917
using UnityEngine.Analytics;
18+
#endif
19+
1020

1121
#if UNITY_EDITOR
1222
using UnityEditor;
23+
#if MLA_UNITY_ANALYTICS_MODULE_ENABLED
1324
using UnityEditor.Analytics;
14-
#endif
25+
#endif // MLA_UNITY_ANALYTICS_MODULE_ENABLED
26+
#endif // UNITY_EDITOR
1527

1628

1729
namespace Unity.MLAgents.Analytics
@@ -50,16 +62,19 @@ static bool EnableAnalytics()
5062
return true;
5163
}
5264

53-
#if UNITY_EDITOR
65+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
5466
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey, k_EventVersion);
55-
#else
67+
if (result == AnalyticsResult.Ok)
68+
{
69+
s_EventRegistered = true;
70+
}
71+
#elif MLA_UNITY_ANALYTICS_MODULE_ENABLED
5672
AnalyticsResult result = AnalyticsResult.UnsupportedPlatform;
57-
#endif
5873
if (result == AnalyticsResult.Ok)
5974
{
6075
s_EventRegistered = true;
6176
}
62-
77+
#endif
6378
if (s_EventRegistered && s_SentModels == null)
6479
{
6580
s_SentModels = new HashSet<NNModel>();
@@ -89,6 +104,7 @@ public static bool IsAnalyticsEnabled()
89104
/// <param name="actionSpec">ActionSpec for the Agent. Used to generate information about the action space.</param>
90105
/// <param name="actuators">List of IActuators for the Agent. Used to generate information about the action space.</param>
91106
/// <returns></returns>
107+
[Conditional("MLA_UNITY_ANALYTICS_MODULE_ENABLED")]
92108
public static void InferenceModelSet(
93109
NNModel nnModel,
94110
string behaviorName,
@@ -117,7 +133,7 @@ IList<IActuator> actuators
117133
var data = GetEventForModel(nnModel, behaviorName, inferenceDevice, sensors, actionSpec, actuators);
118134
// Note - to debug, use JsonUtility.ToJson on the event.
119135
// Debug.Log(JsonUtility.ToJson(data, true));
120-
#if UNITY_EDITOR
136+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
121137
if (AnalyticsUtils.s_SendEditorAnalytics)
122138
{
123139
EditorAnalytics.SendEventWithLimit(k_EventName, data, k_EventVersion);

com.unity.ml-agents/Runtime/Analytics/TrainingAnalytics.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
#if MLA_UNITY_ANALYTICS_MODULE || !UNITY_2019_4_OR_NEWER
2+
#define MLA_UNITY_ANALYTICS_MODULE_ENABLED
3+
#endif
4+
15
using System;
26
using System.Collections.Generic;
7+
using System.Diagnostics;
38
using Unity.MLAgents.Actuators;
49
using Unity.MLAgents.Sensors;
510
using UnityEngine;
11+
#if MLA_UNITY_ANALYTICS_MODULE_ENABLED
612
using UnityEngine.Analytics;
13+
#if UNITY_EDITOR
14+
using UnityEditor.Analytics;
15+
#endif
16+
#endif
717

818
#if UNITY_EDITOR
919
using UnityEditor;
10-
using UnityEditor.Analytics;
1120
#endif
1221

1322
namespace Unity.MLAgents.Analytics
@@ -56,22 +65,22 @@ internal class TrainingAnalytics
5665

5766
static bool EnableAnalytics()
5867
{
68+
#if MLA_UNITY_ANALYTICS_MODULE_ENABLED
5969
if (s_EventsRegistered)
6070
{
6171
return true;
6272
}
63-
6473
foreach (var eventName in s_EventNames)
6574
{
6675
#if UNITY_EDITOR
6776
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(eventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey);
68-
#else
69-
AnalyticsResult result = AnalyticsResult.UnsupportedPlatform;
70-
#endif
7177
if (result != AnalyticsResult.Ok)
7278
{
7379
return false;
7480
}
81+
#else
82+
return false;
83+
#endif // UNITY_EDITOR
7584
}
7685
s_EventsRegistered = true;
7786

@@ -83,13 +92,17 @@ static bool EnableAnalytics()
8392
}
8493

8594
return s_EventsRegistered;
95+
#else
96+
return false;
97+
#endif // MLA_UNITY_ANALYTICS_MODULE_ENABLED
8698
}
8799

88100
/// <summary>
89101
/// Cache information about the trainer when it becomes available in the RpcCommunicator.
90102
/// </summary>
91103
/// <param name="communicationVersion"></param>
92104
/// <param name="packageVersion"></param>
105+
[Conditional("MLA_UNITY_ANALYTICS_MODULE_ENABLED")]
93106
public static void SetTrainerInformation(string packageVersion, string communicationVersion)
94107
{
95108
s_TrainerPackageVersion = packageVersion;
@@ -98,13 +111,14 @@ public static void SetTrainerInformation(string packageVersion, string communica
98111

99112
public static bool IsAnalyticsEnabled()
100113
{
101-
#if UNITY_EDITOR
114+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
102115
return EditorAnalytics.enabled;
103116
#else
104117
return false;
105118
#endif
106119
}
107120

121+
[Conditional("MLA_UNITY_ANALYTICS_MODULE_ENABLED")]
108122
public static void TrainingEnvironmentInitialized(TrainingEnvironmentInitializedEvent tbiEvent)
109123
{
110124
if (!IsAnalyticsEnabled())
@@ -126,16 +140,15 @@ public static void TrainingEnvironmentInitialized(TrainingEnvironmentInitialized
126140
// Debug.Log(
127141
// $"Would send event {k_TrainingEnvironmentInitializedEventName} with body {JsonUtility.ToJson(tbiEvent, true)}"
128142
// );
129-
#if UNITY_EDITOR
143+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
130144
if (AnalyticsUtils.s_SendEditorAnalytics)
131145
{
132146
EditorAnalytics.SendEventWithLimit(k_TrainingEnvironmentInitializedEventName, tbiEvent);
133147
}
134-
#else
135-
return;
136148
#endif
137149
}
138150

151+
[Conditional("MLA_UNITY_ANALYTICS_MODULE_ENABLED")]
139152
public static void RemotePolicyInitialized(
140153
string fullyQualifiedBehaviorName,
141154
IList<ISensor> sensors,
@@ -164,13 +177,11 @@ IList<IActuator> actuators
164177
// Debug.Log(
165178
// $"Would send event {k_RemotePolicyInitializedEventName} with body {JsonUtility.ToJson(data, true)}"
166179
// );
167-
#if UNITY_EDITOR
180+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
168181
if (AnalyticsUtils.s_SendEditorAnalytics)
169182
{
170183
EditorAnalytics.SendEventWithLimit(k_RemotePolicyInitializedEventName, data);
171184
}
172-
#else
173-
return;
174185
#endif
175186
}
176187

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

200+
[Conditional("MLA_UNITY_ANALYTICS_MODULE_ENABLED")]
189201
public static void TrainingBehaviorInitialized(TrainingBehaviorInitializedEvent tbiEvent)
190202
{
191203
if (!IsAnalyticsEnabled())
@@ -211,7 +223,7 @@ public static void TrainingBehaviorInitialized(TrainingBehaviorInitializedEvent
211223
// Debug.Log(
212224
// $"Would send event {k_TrainingBehaviorInitializedEventName} with body {JsonUtility.ToJson(tbiEvent, true)}"
213225
// );
214-
#if UNITY_EDITOR
226+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
215227
if (AnalyticsUtils.s_SendEditorAnalytics)
216228
{
217229
EditorAnalytics.SendEventWithLimit(k_TrainingBehaviorInitializedEventName, tbiEvent);

com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
using UnityEngine;
77
using System.Runtime.CompilerServices;
88
using Unity.MLAgents.Actuators;
9-
using Unity.MLAgents.Analytics;
109
using Unity.MLAgents.Sensors;
1110
using Unity.MLAgents.Demonstrations;
1211
using Unity.MLAgents.Policies;
1312

13+
using Unity.MLAgents.Analytics;
1414

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

527527
#region Analytics
528-
529528
internal static TrainingEnvironmentInitializedEvent ToTrainingEnvironmentInitializedEvent(
530529
this TrainingEnvironmentInitialized inputProto)
531530
{
@@ -569,7 +568,6 @@ internal static TrainingBehaviorInitializedEvent ToTrainingBehaviorInitializedEv
569568
NumNetworkHiddenUnits = inputProto.NumNetworkHiddenUnits,
570569
};
571570
}
572-
573571
#endregion
574572

575573
}

com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
using System.Linq;
1313
using UnityEngine;
1414
using Unity.MLAgents.Actuators;
15-
using Unity.MLAgents.Analytics;
1615
using Unity.MLAgents.CommunicatorObjects;
1716
using Unity.MLAgents.Sensors;
1817
using Unity.MLAgents.SideChannels;
1918
using Google.Protobuf;
2019

20+
using Unity.MLAgents.Analytics;
21+
2122
namespace Unity.MLAgents
2223
{
2324
/// Responsible for communication with External using gRPC.
@@ -144,7 +145,6 @@ out input
144145

145146
var pythonPackageVersion = initializationInput.RlInitializationInput.PackageVersion;
146147
var pythonCommunicationVersion = initializationInput.RlInitializationInput.CommunicationVersion;
147-
148148
TrainingAnalytics.SetTrainerInformation(pythonPackageVersion, pythonCommunicationVersion);
149149

150150
var communicationIsCompatible = CheckCommunicationVersionsAreCompatible(

com.unity.ml-agents/Runtime/Policies/RemotePolicy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
12
using System.Collections.Generic;
23
using Unity.MLAgents.Actuators;
3-
using Unity.MLAgents.Analytics;
44
using Unity.MLAgents.Sensors;
5+
using Unity.MLAgents.Analytics;
56

67

78
namespace Unity.MLAgents.Policies

com.unity.ml-agents/Runtime/Unity.ML-Agents.asmdef

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,12 @@
1515
"Grpc.Core.dll"
1616
],
1717
"autoReferenced": true,
18-
"defineConstraints": []
18+
"defineConstraints": [],
19+
"versionDefines": [
20+
{
21+
"name": "com.unity.modules.unityanalytics",
22+
"expression": "1.0.0",
23+
"define": "MLA_UNITY_ANALYTICS_MODULE"
24+
}
25+
]
1926
}

0 commit comments

Comments
 (0)