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

New autodiff graph memory management strategy #1698

Merged
merged 9 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP Debug
  • Loading branch information
nathanielsimard committed Apr 22, 2024
commit 73a330a6d4070afbe16577830f906f2453bde1df
4 changes: 4 additions & 0 deletions backend-comparison/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 88,10 @@ name = "custom-gelu"
path = "benches/custom_gelu.rs"
harness = false

[[bench]]
name = "autodiff"
harness = false

[[bin]]
name = "burnbench"
path = "src/bin/burnbench.rs"
81 changes: 81 additions & 0 deletions backend-comparison/benches/autodiff.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,81 @@
use backend_comparison::persistence::save;
use burn::{
module::Module,
nn,
tensor::{
backend::{AutodiffBackend, Backend},
Distribution, Tensor,
},
};
use burn_common::benchmark::{run_benchmark, Benchmark};

pub struct AutodiffOverheadBenchmark<B: AutodiffBackend> {
config: nn::LstmConfig,
lstm: nn::Lstm<B>,
device: B::Device,
}

impl<B: AutodiffBackend> Benchmark for AutodiffOverheadBenchmark<B> {
type Args = Tensor<B, 3>;

fn name(&self) -> String {
"autodiff_overhead".into()
}

fn shapes(&self) -> Vec<Vec<usize>> {
vec![]
}

fn execute(&self, input: Self::Args) {
for _ in 0..20 {
let input = input.clone().detach();
let mut cell = input.clone();
let lstm = self.lstm.clone().fork(&input.device());

for i in 0..10 {
let (cells, _) = lstm.forward(input.clone(), None);
cell = cell cells;
}

cell.backward();
}
}

fn prepare(&self) -> Self::Args {
let shape = [1, 3, self.config.d_hidden];
Tensor::random(shape, Distribution::Default, &self.device)
}

fn sync(&self) {
B::sync(&self.device)
}
}

#[allow(dead_code)]
fn bench<B: Backend>(
device: &B::Device,
feature_name: &str,
url: Option<&str>,
token: Option<&str>,
) {
let config = nn::LstmConfig::new(3, 3, true);
let lstm = config.init(device);
let benchmark = AutodiffOverheadBenchmark::<burn::backend::Autodiff<B>> {
lstm,
config,
device: device.clone(),
};

save::<B>(
vec![run_benchmark(benchmark)],
device,
feature_name,
url,
token,
)
.unwrap();
}

fn main() {
backend_comparison::bench_on_backend!();
}
2 changes: 2 additions & 0 deletions backend-comparison/src/burnbenchapp/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 98,8 @@ enum BenchmarkValues {
MaxPool2d,
#[strum(to_string = "load-record")]
LoadRecord,
#[strum(to_string = "autodiff")]
Autodiff,
}

pub fn execute() {
Expand Down
10 changes: 5 additions & 5 deletions crates/burn-autodiff/src/runtime/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 15,9 @@ pub trait AutodiffClient: Send Clone {
}

/// Client implementation in used.
#[cfg(feature = "std")]
pub type AutodiffClientImpl = super::mspc::ChannelClient;

/// Client implementation in used.
#[cfg(not(feature = "std"))]
// #[cfg(feature = "std")]
// pub type AutodiffClientImpl = super::mspc::ChannelClient;
//
// /// Client implementation in used.
// #[cfg(not(feature = "std"))]
pub type AutodiffClientImpl = super::mutex::MutexClient;
30 changes: 16 additions & 14 deletions crates/burn-autodiff/src/runtime/memory_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 11,8 @@ use std::{
pub struct GraphMemoryManagement {
graphs: HashMap<GraphId, GraphState>,
owned: HashSet<GraphId>,
nodes: HashMap<NodeRefCount, Vec<NodeID>>,
leafs: HashSet<NodeID>,
}

#[derive(new, Hash, PartialEq, Eq, Clone, Copy, Debug)]
Expand Down Expand Up @@ -78,7 80,7 @@ impl GraphMemoryManagement {
pub fn find_orphan_graphs(&self) -> Vec<GraphId> {
self.owned
.iter()
.filter(|id| self.is_orphan(id))
// .filter(|id| self.is_orphan(id))
.copied()
.collect()
}
Expand Down Expand Up @@ -174,19 176,19 @@ impl core::fmt::Display for GraphMemoryManagement {
self.owned.len(),
self.graphs.len()
))?;
for (id, state) in self.graphs.iter() {
f.write_fmt(format_args!("Graph {} => ", id.node.value))?;
match state {
GraphState::Merged(id) => f.write_fmt(format_args!("Merged {}", id.node.value))?,
GraphState::Owned(nodes) => {
f.write_str("Owned")?;
for node in nodes {
f.write_fmt(format_args!(" {}", node.value))?;
}
}
}
f.write_str("\n")?;
}
// for (id, state) in self.graphs.iter() {
// f.write_fmt(format_args!("Graph {} => ", id.node.value))?;
// match state {
// GraphState::Merged(id) => f.write_fmt(format_args!("Merged {}", id.node.value))?,
// GraphState::Owned(nodes) => {
// f.write_str("Owned")?;
// for node in nodes {
// f.write_fmt(format_args!(" {}", node.value))?;
// }
// }
// }
// f.write_str("\n")?;
// }

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions crates/burn-autodiff/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 2,10 @@ mod client;
mod memory_management;
mod server;

#[cfg(not(feature = "std"))]
// #[cfg(not(feature = "std"))]
pub mod mutex;

#[cfg(feature = "std")]
pub mod mspc;
// #[cfg(feature = "std")]
// pub mod mspc;

pub use client::*;
36 changes: 26 additions & 10 deletions crates/burn-autodiff/src/runtime/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 8,8 @@ use crate::{
};
use std::collections::HashMap;

static GRAPH_MM_ACTIVATED: bool = true;

#[derive(Default)]
pub struct AutodiffServer {
steps: HashMap<NodeID, StepBoxed>,
Expand All @@ -20,7 22,9 @@ impl AutodiffServer {
let parents = step.parents();
let node_id = *rc.as_ref();

self.memory_management.register(rc, parents);
if GRAPH_MM_ACTIVATED {
self.memory_management.register(rc, parents);
}

self.steps.insert(node_id, step);
self.actions_builder.insert(node_id, actions);
Expand All @@ -33,20 37,30 @@ impl AutodiffServer {
);
let builder = self.actions_builder.remove(&node_id).unwrap();


let orphans = self.memory_management.find_orphan_graphs();

let (tape, builder) = self.build_tape(node_id, step, builder);
let checkpointer = builder.build(&self.steps);

let gradients = Self::execute_steps(tape, grads, checkpointer);

// Cleanup
let mut on_free_graph = |node_id: &NodeID| {
self.steps.remove(node_id);
self.actions_builder.remove(node_id);
};
if !GRAPH_MM_ACTIVATED {
self.steps.clear();
self.actions_builder.clear();
} else {
// Cleanup
let mut on_free_graph = |node_id: &NodeID| {
self.steps.remove(node_id);
self.actions_builder.remove(node_id);
};

for graph_id in orphans {
self.memory_management
.free_graph(graph_id, &mut on_free_graph);
}

for graph_id in self.memory_management.find_orphan_graphs() {
self.memory_management
.free_graph(graph_id, &mut on_free_graph);
println!("{}", self.memory_management);
}

gradients
Expand All @@ -63,7 77,9 @@ impl AutodiffServer {
.collect::<Vec<_>>();

BreadthFirstSearch.traverse(node, node_step, &mut self.steps, |id, step| {
self.memory_management.free_node(id);
if GRAPH_MM_ACTIVATED {
self.memory_management.free_node(id);
}

let depth = step.depth();
if depth == 0 {
Expand Down