Skip to content

Commit 3ad43b3

Browse files
author
Chris Elion
authored
Merge pull request #3627 from Unity-Technologies/release-0.15.0-log-no-model
[MLA-762] Better logging when no model + inference only
2 parents 5f01d16 + 69e1684 commit 3ad43b3

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
lines changed

com.unity.ml-agents/Editor/BehaviorParametersEditor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ void DisplayFailedModelChecks()
106106
if (brainParameters != null)
107107
{
108108
var failedChecks = Inference.BarracudaModelParamLoader.CheckModel(
109-
barracudaModel, brainParameters, sensorComponents);
109+
barracudaModel, brainParameters, sensorComponents, behaviorParameters.behaviorType
110+
);
110111
foreach (var check in failedChecks)
111112
{
112113
if (check != null)

com.unity.ml-agents/Runtime/Inference/BarracudaModelParamLoader.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,22 @@ public static string[] GetOutputNames(Model model)
127127
/// </param>
128128
/// <param name="sensorComponents">Attached sensor components</param>
129129
/// <returns>The list the error messages of the checks that failed</returns>
130-
public static IEnumerable<string> CheckModel(Model model, BrainParameters brainParameters, SensorComponent[] sensorComponents)
130+
public static IEnumerable<string> CheckModel(Model model, BrainParameters brainParameters,
131+
SensorComponent[] sensorComponents, BehaviorType behaviorType = BehaviorType.Default)
131132
{
132133
List<string> failedModelChecks = new List<string>();
133134
if (model == null)
134135
{
135-
failedModelChecks.Add(
136-
"There is no model for this Brain, cannot run inference. " +
137-
"(But can still train)");
136+
var errorMsg = "There is no model for this Brain; cannot run inference. ";
137+
if (behaviorType == BehaviorType.InferenceOnly)
138+
{
139+
errorMsg += "Either assign a model, or change to a different Behavior Type.";
140+
}
141+
else
142+
{
143+
errorMsg += "(But can still train)";
144+
}
145+
failedModelChecks.Add(errorMsg);
138146
return failedModelChecks;
139147
}
140148

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,17 @@ internal IPolicy GeneratePolicy(Func<float[]> heuristic)
142142
case BehaviorType.HeuristicOnly:
143143
return new HeuristicPolicy(heuristic);
144144
case BehaviorType.InferenceOnly:
145+
{
146+
if (m_Model == null)
147+
{
148+
var behaviorType = BehaviorType.InferenceOnly.ToString();
149+
throw new UnityAgentsException(
150+
$"Can't use Behavior Type {behaviorType} without a model. " +
151+
"Either assign a model, or change to a different Behavior Type."
152+
);
153+
}
145154
return new BarracudaPolicy(m_BrainParameters, m_Model, m_InferenceDevice);
155+
}
146156
case BehaviorType.Default:
147157
if (Academy.Instance.IsCommunicatorOn)
148158
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using NUnit.Framework;
2+
using UnityEngine;
3+
using MLAgents;
4+
using MLAgents.Policies;
5+
6+
namespace MLAgents.Tests
7+
{
8+
[TestFixture]
9+
public class BehaviorParameterTests
10+
{
11+
static float[] DummyHeuristic()
12+
{
13+
return null;
14+
}
15+
16+
[Test]
17+
public void TestNoModelInferenceOnlyThrows()
18+
{
19+
var gameObj = new GameObject();
20+
var bp = gameObj.AddComponent<BehaviorParameters>();
21+
bp.behaviorType = BehaviorType.InferenceOnly;
22+
23+
Assert.Throws<UnityAgentsException>(() =>
24+
{
25+
bp.GeneratePolicy(DummyHeuristic);
26+
});
27+
}
28+
}
29+
}

com.unity.ml-agents/Tests/Editor/BehaviorParameterTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.ml-agents/Tests/Editor/PublicAPI/PublicApiValidation.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public void CheckSetupAgent()
102102
var agent = gameObject.AddComponent<PublicApiAgent>();
103103
// Make sure we can set the behavior type correctly after the agent is added
104104
behaviorParams.behaviorType = BehaviorType.InferenceOnly;
105+
// Can't actually create an Agent with InferenceOnly and no model, so change back
106+
behaviorParams.behaviorType = BehaviorType.Default;
105107

106108
// TODO - not internal yet
107109
// var decisionRequester = gameObject.AddComponent<DecisionRequester>();

0 commit comments

Comments
 (0)