-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[MLA-1724] Reduce use of IEnumerable during inference #4887
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 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 |
---|---|---|
|
@@ -24,12 +24,14 @@ internal class ModelRunner | |
TensorApplier m_TensorApplier; | ||
|
||
NNModel m_Model; | ||
string m_ModelName; | ||
InferenceDevice m_InferenceDevice; | ||
IWorker m_Engine; | ||
bool m_Verbose = false; | ||
string[] m_OutputNames; | ||
IReadOnlyList<TensorProxy> m_InferenceInputs; | ||
IReadOnlyList<TensorProxy> m_InferenceOutputs; | ||
List<TensorProxy> m_InferenceOutputs; | ||
Dictionary<string, Tensor> m_InputsByName; | ||
Dictionary<int, List<float>> m_Memories = new Dictionary<int, List<float>>(); | ||
|
||
SensorShapeValidator m_SensorShapeValidator = new SensorShapeValidator(); | ||
|
@@ -56,6 +58,7 @@ public ModelRunner( | |
{ | ||
Model barracudaModel; | ||
m_Model = model; | ||
m_ModelName = model.name; | ||
m_InferenceDevice = inferenceDevice; | ||
m_TensorAllocator = new TensorCachingAllocator(); | ||
if (model != null) | ||
|
@@ -84,6 +87,8 @@ public ModelRunner( | |
seed, m_TensorAllocator, m_Memories, barracudaModel); | ||
m_TensorApplier = new TensorApplier( | ||
actionSpec, seed, m_TensorAllocator, m_Memories, barracudaModel); | ||
m_InputsByName = new Dictionary<string, Tensor>(); | ||
m_InferenceOutputs = new List<TensorProxy>(); | ||
} | ||
|
||
public InferenceDevice InferenceDevice | ||
|
@@ -96,15 +101,14 @@ public NNModel Model | |
get { return m_Model; } | ||
} | ||
|
||
static Dictionary<string, Tensor> PrepareBarracudaInputs(IEnumerable<TensorProxy> infInputs) | ||
void PrepareBarracudaInputs(IReadOnlyList<TensorProxy> infInputs) | ||
{ | ||
var inputs = new Dictionary<string, Tensor>(); | ||
foreach (var inp in infInputs) | ||
m_InputsByName.Clear(); | ||
for (var i = 0; i < infInputs.Count; i++) | ||
{ | ||
inputs[inp.name] = inp.data; | ||
var inp = infInputs[i]; | ||
m_InputsByName[inp.name] = inp.data; | ||
} | ||
|
||
return inputs; | ||
} | ||
|
||
public void Dispose() | ||
|
@@ -114,16 +118,14 @@ public void Dispose() | |
m_TensorAllocator?.Reset(false); | ||
} | ||
|
||
List<TensorProxy> FetchBarracudaOutputs(string[] names) | ||
void FetchBarracudaOutputs(string[] names) | ||
{ | ||
var outputs = new List<TensorProxy>(); | ||
m_InferenceOutputs.Clear(); | ||
foreach (var n in names) | ||
{ | ||
var output = m_Engine.PeekOutput(n); | ||
outputs.Add(TensorUtils.TensorProxyFromBarracuda(output, n)); | ||
m_InferenceOutputs.Add(TensorUtils.TensorProxyFromBarracuda(output, n)); | ||
} | ||
|
||
return outputs; | ||
} | ||
|
||
public void PutObservations(AgentInfo info, List<ISensor> sensors) | ||
|
@@ -169,31 +171,33 @@ public void DecideBatch() | |
} | ||
|
||
Profiler.BeginSample("ModelRunner.DecideAction"); | ||
Profiler.BeginSample(m_ModelName); | ||
|
||
Profiler.BeginSample($"MLAgents.{m_Model.name}.GenerateTensors"); | ||
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. These were allocating for the string concatenation, and |
||
Profiler.BeginSample($"GenerateTensors"); | ||
// Prepare the input tensors to be feed into the engine | ||
m_TensorGenerator.GenerateTensors(m_InferenceInputs, currentBatchSize, m_Infos); | ||
Profiler.EndSample(); | ||
|
||
Profiler.BeginSample($"MLAgents.{m_Model.name}.PrepareBarracudaInputs"); | ||
var inputs = PrepareBarracudaInputs(m_InferenceInputs); | ||
Profiler.BeginSample($"PrepareBarracudaInputs"); | ||
PrepareBarracudaInputs(m_InferenceInputs); | ||
Profiler.EndSample(); | ||
|
||
// Execute the Model | ||
Profiler.BeginSample($"MLAgents.{m_Model.name}.ExecuteGraph"); | ||
m_Engine.Execute(inputs); | ||
Profiler.BeginSample($"ExecuteGraph"); | ||
m_Engine.Execute(m_InputsByName); | ||
Profiler.EndSample(); | ||
|
||
Profiler.BeginSample($"MLAgents.{m_Model.name}.FetchBarracudaOutputs"); | ||
m_InferenceOutputs = FetchBarracudaOutputs(m_OutputNames); | ||
Profiler.BeginSample($"FetchBarracudaOutputs"); | ||
FetchBarracudaOutputs(m_OutputNames); | ||
Profiler.EndSample(); | ||
|
||
Profiler.BeginSample($"MLAgents.{m_Model.name}.ApplyTensors"); | ||
Profiler.BeginSample($"ApplyTensors"); | ||
// Update the outputs | ||
m_TensorApplier.ApplyTensors(m_InferenceOutputs, m_OrderedAgentsRequestingDecisions, m_LastActionsReceived); | ||
Profiler.EndSample(); | ||
|
||
Profiler.EndSample(); | ||
Profiler.EndSample(); // end name | ||
Profiler.EndSample(); // end ModelRunner.DecideAction | ||
|
||
m_Infos.Clear(); | ||
|
||
|
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed this and FetchBarracudaOutputs to reuse their results between frames.