-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Barracuda inference for hybrid actions #4611
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
dongruoping
merged 14 commits into
develop-hybrid-actions-csharp
from
develop-hybrid-inference
Nov 5, 2020
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bdc3ef4
barracuda inference for hybrid actions
1a9ec75
Add barracuda model extension methods to handle deprecated action output
303bb94
add method summaries
fa7a4a8
replace deprecated checks with extension method
8bdece5
fix tests
8a15f3b
move deprecated fields
3761d64
fix bug
cd899a2
Add new model and tests to ParameterLoaderTest
1f78b1b
add comments
a16a2d0
early out in tensorApplier if model is null
79a92ef
remove unused
b4bbd27
yamato triggers - run on non-master PRs
5de688b
fix tests
99d4247
Merge branch 'develop-hybrid-inference' of https://github.com/Unity-T…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
com.unity.ml-agents/Runtime/Inference/BarracudaModelExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using Unity.Barracuda; | ||
|
||
namespace Unity.MLAgents.Inference | ||
{ | ||
/// <summary> | ||
/// Barracuda Model extension methods. | ||
/// </summary> | ||
internal static class BarracudaModelExtensions | ||
{ | ||
/// <summary> | ||
/// Check if the model has continuous action outputs. | ||
/// </summary> | ||
/// <param name="model"> | ||
/// The Barracuda engine model for loading static parameters. | ||
/// </param> | ||
/// <returns>True if the model has continuous action outputs.</returns> | ||
public static bool HasContinuousOutputs(this Model model) | ||
{ | ||
if (model.UseDeprecated()) | ||
{ | ||
return (int)model.GetTensorByName(TensorNames.IsContinuousControlDeprecated)[0] > 0; | ||
} | ||
else | ||
{ | ||
return model.outputs.Contains(TensorNames.ContinuousActionOutput) && | ||
(int)model.GetTensorByName(TensorNames.ContinuousActionOutputShape)[0] > 0; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Continuous action output size of the model. | ||
/// </summary> | ||
/// <param name="model"> | ||
/// The Barracuda engine model for loading static parameters. | ||
/// </param> | ||
/// <returns>Size of continuous action output.</returns> | ||
public static int ContinuousOutputSize(this Model model) | ||
{ | ||
if (model.UseDeprecated()) | ||
{ | ||
return (int)model.GetTensorByName(TensorNames.IsContinuousControlDeprecated)[0] > 0 ? | ||
(int)model.GetTensorByName(TensorNames.ActionOutputShapeDeprecated)[0] : 0; | ||
} | ||
else | ||
{ | ||
return (int)model.GetTensorByName(TensorNames.ContinuousActionOutputShape)[0]; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Continuous action output tensor name of the model. | ||
/// </summary> | ||
/// <param name="model"> | ||
/// The Barracuda engine model for loading static parameters. | ||
/// </param> | ||
/// <returns>Tensor name of continuous action output.</returns> | ||
public static string ContinuousOutputName(this Model model) | ||
{ | ||
if (model.UseDeprecated()) | ||
{ | ||
return TensorNames.ActionOutputDeprecated; | ||
} | ||
else | ||
{ | ||
return TensorNames.ContinuousActionOutput; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Check if the model has discrete action outputs. | ||
/// </summary> | ||
/// <param name="model"> | ||
/// The Barracuda engine model for loading static parameters. | ||
/// </param> | ||
/// <returns>True if the model has discrete action outputs.</returns> | ||
public static bool HasDiscreteOutputs(this Model model) | ||
{ | ||
if (model.UseDeprecated()) | ||
{ | ||
return (int)model.GetTensorByName(TensorNames.IsContinuousControlDeprecated)[0] == 0; | ||
} | ||
else | ||
{ | ||
return model.outputs.Contains(TensorNames.DiscreteActionOutput) && | ||
(int)model.GetTensorByName(TensorNames.DiscreteActionOutputShape)[0] > 0; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Discrete action output size of the model. This is equal to the sum of the branch sizes. | ||
/// </summary> | ||
/// <param name="model"> | ||
/// The Barracuda engine model for loading static parameters. | ||
/// </param> | ||
/// <returns>Size of discrete action output.</returns> | ||
public static int DiscreteOutputSize(this Model model) | ||
{ | ||
if (model.UseDeprecated()) | ||
{ | ||
return (int)model.GetTensorByName(TensorNames.IsContinuousControlDeprecated)[0] > 0 ? | ||
0 : (int)model.GetTensorByName(TensorNames.ActionOutputShapeDeprecated)[0]; | ||
} | ||
else | ||
{ | ||
return (int)model.GetTensorByName(TensorNames.DiscreteActionOutputShape)[0]; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Discrete action output tensor name of the model. | ||
/// </summary> | ||
/// <param name="model"> | ||
/// The Barracuda engine model for loading static parameters. | ||
/// </param> | ||
/// <returns>Tensor name of discrete action output.</returns> | ||
public static string DiscreteOutputName(this Model model) | ||
{ | ||
if (model.UseDeprecated()) | ||
{ | ||
return TensorNames.ActionOutputDeprecated; | ||
} | ||
else | ||
{ | ||
return TensorNames.DiscreteActionOutput; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Check if the model uses deprecated output fields and should be handled differently. | ||
/// </summary> | ||
/// <param name="model"> | ||
/// The Barracuda engine model for loading static parameters. | ||
/// </param> | ||
/// <returns>True if the model uses deprecated output fields.</returns> | ||
public static bool UseDeprecated(this Model model) | ||
{ | ||
return !model.outputs.Contains(TensorNames.ContinuousActionOutput) && | ||
!model.outputs.Contains(TensorNames.DiscreteActionOutput); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.