Skip to content

Commit

Permalink
Fix autodiff memory leak (#2347)
Browse files Browse the repository at this point in the history
* Fix autodiff memory leak

* Cleanup
  • Loading branch information
nathanielsimard authored Oct 9, 2024
1 parent 8ec6bf4 commit 0592af0
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions crates/burn-autodiff/src/runtime/memory_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,26 @@ impl GraphMemoryManagement {

// Replace leaves by the new ones and delete everything not useful anymore
mem::swap(&mut self.leaves, &mut new_leaves);

self.clear_unused_roots(&mut deletables);

self.statuses.clear();
for node_to_delete in deletables {
self.nodes.remove(&node_to_delete);
on_free_graph(&node_to_delete)
}
}

fn clear_unused_roots(&mut self, to_delete: &mut Vec<NodeID>) {
for (id, parents) in self.nodes.iter() {
let is_useful = matches!(self.statuses.get(id), Some(NodeMemoryStatus::Useful));

if !is_useful && Arc::strong_count(id) == 1 && parents.is_empty() {
to_delete.push(*id.as_ref())
}
}
}

fn unavailable_propagation(&mut self, node_id: NodeID) -> NodeMemoryStatus {
// If already visited
if let Some(status) = self.statuses.get(&node_id) {
Expand Down

0 comments on commit 0592af0

Please sign in to comment.