-
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
Develop remove memories #2795
Develop remove memories #2795
Conversation
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.
🚢 🇮🇹 - Okay, except for the failing protobuf test
@@ -56,7 +61,7 @@ public interface IApplier | |||
m_Dict[TensorNames.ActionOutput] = | |||
new DiscreteActionOutputApplier(bp.vectorActionSize, seed, allocator); | |||
} | |||
m_Dict[TensorNames.RecurrentOutput] = new MemoryOutputApplier(); | |||
// m_Dict[TensorNames.RecurrentOutput] = new MemoryOutputApplier(); |
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.
dead code
/// <param name="memories"> | ||
/// The memories of all the agents | ||
/// </param> | ||
void Apply(TensorProxy tensorProxy, |
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.
Rather than pass this everywhere, can the two classes that actually need it (BarracudaRecurrentInputGenerator and BarracudaMemoryOutputApplier) save a reference to it?
@@ -18,7 +19,7 @@ public enum InferenceDevice | |||
public class BarracudaPolicy : IPolicy | |||
{ | |||
|
|||
protected IBatchedDecisionMaker m_BatchedDecisionMaker; | |||
protected ModelRunner m_BatchedDecisionMaker; |
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.
Rename to m_ModelRunner
?
def save_memories(self, agent_ids, memory_matrix): | ||
if not isinstance(memory_matrix, np.ndarray): | ||
return | ||
for index, id in enumerate(agent_ids): |
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.
nit: agent_id
instead of id
(id()
is a built-in function)
@@ -56,6 +56,7 @@ def __init__(self, seed, brain, trainer_parameters): | |||
self.seed = seed | |||
self.brain = brain | |||
self.use_recurrent = trainer_parameters["use_recurrent"] | |||
self.memory_dict = {} |
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.
self.memory_dict = {} | |
self.memory_dict: Dict[int, np.ndarray] = {} |
@@ -169,6 +177,24 @@ def make_empty_memory(self, num_agents): | |||
""" | |||
return np.zeros((num_agents, self.m_size)) | |||
|
|||
def save_memories(self, agent_ids, memory_matrix): |
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.
def save_memories(self, agent_ids, memory_matrix): | |
def save_memories(self, agent_ids: List[int], memory_matrix: np.ndarray) -> None: |
for index, id in enumerate(agent_ids): | ||
self.memory_dict[id] = memory_matrix[index, :] | ||
|
||
def retrieve_memories(self, agent_ids): |
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.
def retrieve_memories(self, agent_ids): | |
def retrieve_memories(self, agent_ids: List[int]): |
|
||
def retrieve_memories(self, agent_ids): | ||
memory_matrix = np.zeros((len(agent_ids), self.m_size)) | ||
for index, id in enumerate(agent_ids): |
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.
Same comment about id
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.
Some of it's optional, but definite need to fix the id in ... .keys()
@@ -187,7 +193,7 @@ public void Apply(TensorProxy tensorProxy, IEnumerable<Agent> agents) | |||
|
|||
foreach (var agent in agents) | |||
{ | |||
var memory = agent.GetMemoriesAction(); | |||
var memory = m_Memories.ContainsKey(agent.Info.id) ? m_Memories[agent.Info.id] : null; |
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.
@surfnerd is there a better way to do this (in one lookup)? We were trying m_Memories.ElementAtOrDefault(id).Value but that wasn't behaving as expected.
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.
ElementAtOrDefault
takes an index as a parameter. That probably explains the unexpected behavior. You can always use TryGetValue
which will set the out value
parameter as the default
value if it is not in the Dictionary
.
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.
🤦♂ So
List<float> memories = null;
m_Memories.TryGetValue(agent.Info.id, memories);
?
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.
yeah, you could combine it with the if statement down below as well.
List<float> memory = null;
if (!m_Memories.TryGetValue(agent.Info.id, out memory)
|| memory.Count < memorySize * m_MemoriesCount)
{
...
}
...
m_TensorGenerator = new TensorGenerator(brainParameters, seed, m_TensorAllocator, barracudaModel); | ||
m_TensorApplier = new TensorApplier(brainParameters, seed, m_TensorAllocator, barracudaModel); | ||
m_TensorGenerator = new TensorGenerator( | ||
brainParameters, seed, m_TensorAllocator, ref m_Memories, barracudaModel); |
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.
Don't think we need ref
here now, right?
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.
➕
@@ -51,16 +55,16 @@ public interface IGenerator | |||
new SequenceLengthGenerator(allocator); | |||
m_Dict[TensorNames.VectorObservationPlacholder] = | |||
new VectorObservationGenerator(allocator); | |||
m_Dict[TensorNames.RecurrentInPlaceholder] = | |||
new RecurrentInputGenerator(allocator); | |||
// m_Dict[TensorNames.RecurrentInPlaceholder] = |
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.
nit: dead code
UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/GeneratorImpl.cs
Outdated
Show resolved
Hide resolved
@@ -44,8 +45,8 @@ public void ApplyContinuousActionOutput() | |||
{ | |||
var inputTensor = new TensorProxy() | |||
{ | |||
shape = new long[] {2, 3}, | |||
data = new Tensor(2, 3, new float[] {1, 2, 3, 4, 5, 6}) | |||
shape = new long[] { 2, 3 }, |
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.
gahhh. whitespace noise
return ActionInfo([], [], [], None) | ||
|
||
self.remove_memories( | ||
[ |
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.
If you wanted to be a bit more pythonic, I think you could do
[
agent
for agent, done in zip(brain_info.agents, brain_info.local_done)
if done
]
but no worries if you like the current way better.
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.
Overall this looks great, just some minor feedback.
It would be good to run a before-and-after timing on a simple training scene like Hallway to so we can brag about any speedups in the release notes (and make sure it didn't somehow get worse).
UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorApplier.cs
Outdated
Show resolved
Hide resolved
UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorGenerator.cs
Outdated
Show resolved
Hide resolved
Looks like bair links are down again. OK to merge with that test failing. |
In this pull request :