Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
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
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ portable-atomic = { version = "1.11.0" }
portable-atomic-util = { version = "0.2.4", features = ["alloc"] }

### For the main burn branch. ###
cubecl = { git = "https://github.com/tracel-ai/cubecl", default-features = false, rev = "cdf67a222ce973955fba439bb019139f5b33d40c" }
cubecl-common = { git = "https://github.com/tracel-ai/cubecl", default-features = false, rev = "cdf67a222ce973955fba439bb019139f5b33d40c" }
cubecl-std = { git = "https://github.com/tracel-ai/cubecl", default-features = false, rev = "cdf67a222ce973955fba439bb019139f5b33d40c" }
cubecl = { git = "https://github.com/tracel-ai/cubecl", default-features = false, rev = "b34cffd711e25c1438967c19dc14376de8427df2" }
cubecl-common = { git = "https://github.com/tracel-ai/cubecl", default-features = false, rev = "b34cffd711e25c1438967c19dc14376de8427df2" }
cubecl-std = { git = "https://github.com/tracel-ai/cubecl", default-features = false, rev = "b34cffd711e25c1438967c19dc14376de8427df2" }
### For local development. ###
# cubecl = { path = "../cubecl/crates/cubecl", default-features = false }
# cubecl-common = { path = "../cubecl/crates/cubecl-common", default-features = false }
Expand Down
8 changes: 8 additions & 0 deletions crates/burn-cubecl-fusion/src/shared/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ impl OptimizationBuilder<FuseTrace> for FuseOptimizationBuilder {
}

match op {
OperationIr::Drop(tensor) => {
if self.num_ops == 0 {
self.status = OptimizationStatus::Closed;
return;
}

self.builder.builder.tag_dropped(tensor.id);
}
OperationIr::BaseFloat(ops) => {
if !self.register_base(ops) {
self.status = OptimizationStatus::Closed;
Expand Down
6 changes: 5 additions & 1 deletion crates/burn-cubecl-fusion/src/shared/trace/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use burn_fusion::stream::Context;
use burn_ir::{TensorId, TensorIr};
use cubecl::prelude::*;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, marker::PhantomData};
use std::{
collections::{BTreeMap, HashSet},
marker::PhantomData,
};

#[cfg(feature = "autotune-checks")]
use burn_tensor::TensorData;
Expand Down Expand Up @@ -127,6 +130,7 @@ pub struct FuseResources {
pub inputs_unhandled: Vec<TensorId>,
pub outputs_unhandled: Vec<Arg>,
pub num_reshaped: usize,
pub dropped: HashSet<TensorId>,
}

#[derive(Debug)]
Expand Down
9 changes: 7 additions & 2 deletions crates/burn-cubecl-fusion/src/shared/trace/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,10 @@ impl FuseBlockBuilder {
for entry in local_tensor_ids_output {
let is_read = local_tensor_ids_input.contains(&entry);

if !is_read && !self.local_outputs.contains(&entry.0) {
if !is_read
&& !self.local_outputs.contains(&entry.0)
&& !resources.dropped.contains(&entry.0)
{
let (tensor_id, precision) = entry;
let (tensor, _) = resources.outputs.get(tensor_id).unwrap();
result.insert(precision, tensor.clone());
Expand All @@ -529,7 +532,9 @@ impl FuseBlockBuilder {
// are going to be used after the fused kernel by other operations.
for (tensor, precision) in self.outputs.iter() {
if let TensorStatus::ReadOnly = tensor.status {
result.insert(*precision, tensor.clone());
if !resources.dropped.contains(&tensor.id) {
result.insert(*precision, tensor.clone());
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/burn-cubecl-fusion/src/shared/trace/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
};
use super::{FuseTrace, RegisteredTensors};
use burn_fusion::stream::ScalarId;
use burn_ir::TensorIr;
use burn_ir::{TensorId, TensorIr};
use burn_tensor::{DType, Element};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -36,6 +36,11 @@ impl FuseTraceBuilder {
}
}

/// Tag a tensor as dropped.
pub fn tag_dropped(&mut self, id: TensorId) {
self.resources.dropped.insert(id);
}

/// Register an operation.
pub fn register_operation(&mut self, op: FuseOp) {
self.block_current.ops.push(op);
Expand Down
12 changes: 7 additions & 5 deletions crates/burn-cubecl-fusion/src/shared/trace/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ impl<'a, R: Runtime> InputPlanner<'a, R> {
pub fn run(self, context: &mut Context<'_, CubeFusionHandle<R>>, plan: &mut LaunchPlan<'a, R>) {
for (pos, (tensor_relative, precision)) in self.resources.inputs.iter().enumerate() {
let mut tensor_global = context.tensors.get(&tensor_relative.id).unwrap().clone();
// Important to take the status of the relative graph and not
// the global graph, since the status of the global graph
// might be of a later operation on the same tensor id.
let status = &tensor_relative.status;
let mut handle = context.handles.get_handle(&tensor_global.id, status);
let mut handle = context
.handles
.get_handle(&tensor_global.id, &TensorStatus::ReadOnly);

if let TensorStatus::ReadWrite = tensor_relative.status {
plan.cleared.push(tensor_global.id);
}

self.analyze(plan, pos, tensor_relative, &handle);

Expand Down
10 changes: 10 additions & 0 deletions crates/burn-cubecl-fusion/src/shared/trace/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ impl<'a, R: Runtime> OutputPlanner<'a, R> {
Self::add_layout_info_inputs(block, &plan.handle_inputs);
}
}

// Make sure dropped are correctly executed.
for id in self.resources.dropped.iter() {
if let Some(tensor_global) = context.tensors.get(id) {
context.handles.remove_handle(tensor_global.id);
}
}
for id in plan.cleared.drain(..) {
context.handles.remove_handle(id);
}
}

fn select_reference_from_inputs(
Expand Down
2 changes: 2 additions & 0 deletions crates/burn-cubecl-fusion/src/shared/trace/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub(crate) struct LaunchPlan<'a, R: Runtime> {
pub rank: usize,
pub blocks: Vec<BlockPlan<'a>>,
pub vectorizations: BTreeMap<TensorId, Vect>,
pub cleared: Vec<TensorId>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -135,6 +136,7 @@ impl<R: Runtime> LaunchPlan<'_, R> {
rank,
blocks,
vectorizations: Default::default(),
cleared: Default::default(),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions crates/burn-fusion/src/client/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
FusionBackend, FusionDevice, FusionHandle, FusionRuntime, FusionTensor,
stream::{StreamId, execution::Operation},
};
use burn_ir::{OperationIr, TensorId, TensorIr};
use burn_ir::{OperationIr, TensorIr};
use burn_tensor::{DType, TensorData};

/// Define how to interact with the fusion server.
Expand Down Expand Up @@ -112,6 +112,4 @@ where
) -> FusionTensor<R>
where
B: FusionBackend<FusionRuntime = R>;
/// Drop the tensor with the given [tensor id](TensorId).
fn register_orphan(&self, id: &TensorId);
}
7 changes: 1 addition & 6 deletions crates/burn-fusion/src/client/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
FusionBackend, FusionDevice, FusionHandle, FusionRuntime, FusionServer, FusionTensor,
stream::{StreamId, execution::Operation},
};
use burn_ir::{OperationIr, TensorId, TensorIr};
use burn_ir::{OperationIr, TensorIr};
use burn_tensor::{DType, TensorData};
use spin::Mutex;
use std::sync::Arc;
Expand Down Expand Up @@ -206,11 +206,6 @@ where
FusionTensor::new(id, tensor.shape, tensor.dtype, client, StreamId::current())
}

fn register_orphan(&self, id: &TensorId) {
// TODO: Make drop into a tensor operation so that optimizations can know about it.
self.server.lock().drop_tensor_handle(*id);
}

fn resolve_tensor_float<B>(&self, tensor: FusionTensor<R>) -> B::FloatTensorPrimitive
where
B: FusionBackend<FusionRuntime = R>,
Expand Down
14 changes: 11 additions & 3 deletions crates/burn-fusion/src/search/optimization/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ enum BlockOptimizationStep<O> {
Contiguous {
strategy: ExecutionStrategy<O>,
},
/// Only happen when we fallback on executing a single operation.
Operation {
strategy: ExecutionStrategy<O>,
},
WithHoles {
strategy: ExecutionStrategy<O>,
holes: Vec<usize>,
Expand Down Expand Up @@ -78,6 +82,10 @@ impl<O: NumOperations> BlocksOptimizer<O> {
BlockOptimizationStep::Contiguous { strategy } => {
strategies.push(Box::new(strategy));
}
BlockOptimizationStep::Operation { strategy } => {
strategies.push(Box::new(strategy));
break;
}
BlockOptimizationStep::WithHoles { strategy, holes } => {
strategies.push(Box::new(strategy));

Expand Down Expand Up @@ -143,10 +151,9 @@ impl<O: NumOperations> BlocksOptimizer<O> {
last_index: usize,
ordering_global: &mut Vec<usize>,
) -> BlockOptimizationStep<O> {
ordering_global.append(&mut optimization.ordering);

match optimization.strategy {
ExecutionStrategy::Optimization { opt, ordering } => {
ordering_global.append(&mut optimization.ordering);
let holes = self.find_holes(last_index);

if holes.is_empty() {
Expand All @@ -159,11 +166,12 @@ impl<O: NumOperations> BlocksOptimizer<O> {
}
ExecutionStrategy::Operations { ordering } => {
let min = ordering.iter().min().unwrap();
ordering_global.push(*min);

let strategy = ExecutionStrategy::Operations {
ordering: Arc::new(vec![*min]),
};
BlockOptimizationStep::Contiguous { strategy }
BlockOptimizationStep::Operation { strategy }
}
_ => unreachable!(),
}
Expand Down
4 changes: 0 additions & 4 deletions crates/burn-fusion/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,4 @@ where

id
}

pub fn drop_tensor_handle(&mut self, id: TensorId) {
self.handles.handles_orphan.push(id);
}
}
Loading