forked from tracel-ai/burn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fusion] Support multi-precision fusion (tracel-ai#1718)
- Loading branch information
1 parent
6b14bb8
commit 5d959e2
Showing
46 changed files
with
2,039 additions
and
1,434 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,94 @@ | ||
use burn_tensor::backend::BackendBridge; | ||
|
||
use crate::{Fusion, FusionBackend}; | ||
use crate::{ | ||
client::FusionClient, stream::execution::Operation, Fusion, FusionBackend, FusionRuntime, | ||
}; | ||
use burn_tensor::{ | ||
backend::BackendBridge, | ||
ops::FloatTensor, | ||
repr::{ | ||
BaseOperationDescription, HandleContainer, OperationDescription, UnaryOperationDescription, | ||
}, | ||
Element, | ||
}; | ||
use std::marker::PhantomData; | ||
|
||
#[derive(Debug)] | ||
/// Fusion bridge. | ||
pub struct PrecisionBridge; | ||
pub struct PrecisionBridge<B: FusionBackend> { | ||
_backend: PhantomData<B>, | ||
} | ||
|
||
impl<B: FusionBackend> BackendBridge<Fusion<B>> for PrecisionBridge { | ||
type Target = Fusion<B>; | ||
impl<R, BInput, BTarget> BackendBridge<Fusion<BInput>> for PrecisionBridge<BTarget> | ||
where | ||
BInput: FusionBackend<FusionRuntime = R>, | ||
BTarget: FusionBackend<FusionRuntime = R>, | ||
R: FusionRuntime + 'static, | ||
{ | ||
type Target = Fusion<BTarget>; | ||
|
||
fn into_target<const D: usize>( | ||
tensor: burn_tensor::ops::FloatTensor<Fusion<B>, D>, | ||
tensor: FloatTensor<Fusion<BInput>, D>, | ||
_device: Option<burn_tensor::Device<Self::Target>>, | ||
) -> burn_tensor::ops::FloatTensor<Self::Target, D> { | ||
tensor | ||
) -> FloatTensor<Self::Target, D> { | ||
cast::<R, BInput, BTarget, D>(tensor) | ||
} | ||
|
||
fn from_target<const D: usize>( | ||
tensor: burn_tensor::ops::FloatTensor<Self::Target, D>, | ||
_device: Option<burn_tensor::Device<Fusion<B>>>, | ||
) -> burn_tensor::ops::FloatTensor<Fusion<B>, D> { | ||
tensor | ||
tensor: FloatTensor<Self::Target, D>, | ||
_device: Option<burn_tensor::Device<Fusion<BInput>>>, | ||
) -> FloatTensor<Fusion<BInput>, D> { | ||
cast::<R, BTarget, BInput, D>(tensor) | ||
} | ||
} | ||
|
||
fn cast<R, BInput, BTarget, const D: usize>( | ||
input: FloatTensor<Fusion<BInput>, D>, | ||
) -> FloatTensor<Fusion<BTarget>, D> | ||
where | ||
BInput: FusionBackend<FusionRuntime = R>, | ||
BTarget: FusionBackend<FusionRuntime = R>, | ||
R: FusionRuntime + 'static, | ||
{ | ||
#[derive(new)] | ||
struct Cast<R: FusionRuntime, BInput: FusionBackend, BTarget: FusionBackend, const D: usize> { | ||
desc: UnaryOperationDescription, | ||
_bi: PhantomData<BInput>, | ||
_bt: PhantomData<BTarget>, | ||
_runtime: PhantomData<R>, | ||
} | ||
|
||
impl<const D: usize, R, BInput, BTarget> Operation<BTarget::FusionRuntime> | ||
for Cast<R, BInput, BTarget, D> | ||
where | ||
BInput: FusionBackend<FusionRuntime = R>, | ||
BTarget: FusionBackend<FusionRuntime = R>, | ||
R: FusionRuntime, | ||
{ | ||
fn execute( | ||
self: Box<Self>, | ||
handles: &mut HandleContainer<<BTarget::FusionRuntime as FusionRuntime>::FusionHandle>, | ||
) { | ||
let input = handles.get_float_tensor::<BInput, D>(&self.desc.input); | ||
let output = BInput::cast_float(input, BTarget::FloatElem::dtype()); | ||
|
||
handles.register_handle(self.desc.out.id, output); | ||
} | ||
} | ||
|
||
let stream = input.stream; | ||
let out = input | ||
.client | ||
.tensor_uninitialized(input.shape.clone(), BTarget::FloatElem::dtype()); | ||
|
||
let desc = UnaryOperationDescription { | ||
input: input.into_description(), | ||
out: out.to_description_out(), | ||
}; | ||
|
||
out.client.register( | ||
vec![stream], | ||
OperationDescription::BaseFloat(BaseOperationDescription::Cast(desc.clone())), | ||
Cast::<R, BInput, BTarget, D>::new(desc), | ||
); | ||
|
||
out | ||
} |
Oops, something went wrong.