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.
feat cube support Array (tracel-ai#1907)
- Loading branch information
1 parent
14d1bbb
commit d50bac1
Showing
10 changed files
with
229 additions
and
13 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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
authors = [] | ||
name = "gelu" | ||
publish = false | ||
edition.workspace = true | ||
license.workspace = true | ||
version.workspace = true | ||
|
||
[features] | ||
default = ["wgpu"] | ||
cuda = ["burn-cuda"] | ||
wgpu = ["burn-wgpu"] | ||
|
||
[dependencies] | ||
burn-cube = { path = "../../crates/burn-cube", version = "0.14.0" } | ||
burn-cuda = { path = "../../crates/burn-cuda", version = "0.14.0", optional = true } | ||
burn-wgpu = { path = "../../crates/burn-wgpu", version = "0.14.0", optional = true } |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fn main() { | ||
#[cfg(feature = "cuda")] | ||
gelu::launch::<burn_cuda::CudaRuntime>(&Default::default()); | ||
#[cfg(feature = "wgpu")] | ||
gelu::launch::<burn_wgpu::WgpuRuntime>(&Default::default()); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use burn_cube::prelude::*; | ||
|
||
#[cube(launch)] | ||
fn gelu<F: Float>(input: Array<F>, mut output: Array<F>) { | ||
if ABSOLUTE_POS < input.len() { | ||
output[ABSOLUTE_POS] = gelu_scalar::<F>(input[ABSOLUTE_POS]); | ||
} | ||
} | ||
|
||
#[cube] | ||
fn gelu_scalar<F: Float>(x: F) -> F { | ||
x * (F::new(1.0) + F::erf(x / F::sqrt(F::new(2.0)))) / F::new(2.0) | ||
} | ||
|
||
pub fn launch<R: Runtime>(device: &R::Device) { | ||
let client = R::client(device); | ||
println!("Executing gelu with runtime {:?}", R::name()); | ||
|
||
let input = &[-1., 0., 1., 5.]; | ||
let input_handle = client.create(f32::as_bytes(input)); | ||
let output_handle = client.empty(input.len() * core::mem::size_of::<f32>()); | ||
|
||
gelu_launch::<F32, R>( | ||
client.clone(), | ||
CubeCount::new(1, 1, 1), | ||
KernelSettings::default(), | ||
ArrayHandle::new(&input_handle, input.len()), | ||
ArrayHandle::new(&output_handle, input.len()), | ||
); | ||
|
||
let output = client.read(output_handle.binding()).read_sync().unwrap(); | ||
let output = f32::from_bytes(&output); | ||
|
||
// Should be [-0.1587, 0.0000, 0.8413, 5.0000] | ||
println!("{output:?}"); | ||
} |