Skip to content
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

Merged
merged 17 commits into from
Oct 25, 2019
Merged

Develop remove memories #2795

merged 17 commits into from
Oct 25, 2019

Conversation

vincentpierre
Copy link
Contributor

In this pull request :

  • Memories are no longer being communicated between C# and Python
  • Removed memories from AgentInfo and AgentAction
  • Memories are now stored in the python Policy on Python
  • Memories are now stored in the ModelRunner on C#

@vincentpierre vincentpierre self-assigned this Oct 24, 2019
Copy link
Contributor

@ervteng ervteng left a 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();
Copy link
Contributor

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,
Copy link
Contributor

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;
Copy link
Contributor

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):
Copy link
Contributor

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 = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about id

Copy link
Contributor

@chriselion chriselion left a 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;
Copy link
Contributor

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.

Copy link
Contributor

@surfnerd surfnerd Oct 25, 2019

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.

Copy link
Contributor

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);

?

Copy link
Contributor

@surfnerd surfnerd Oct 25, 2019

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);
Copy link
Contributor

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?

Copy link
Contributor

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] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: dead code

@@ -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 },
Copy link
Contributor

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(
[
Copy link
Contributor

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.

Copy link
Contributor

@chriselion chriselion left a 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).

@chriselion
Copy link
Contributor

Looks like bair links are down again. OK to merge with that test failing.

@vincentpierre vincentpierre merged commit 58cee7e into develop Oct 25, 2019
@delete-merged-branch delete-merged-branch bot deleted the develop-remove-memories branch October 25, 2019 17:56
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants