-
Notifications
You must be signed in to change notification settings - Fork 382
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
Add f16 support in the wgpu backend #1582
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use crate::codegen::dialect::gpu; | ||
use burn_tensor::Element; | ||
use half::f16; | ||
|
||
/// The base element trait for the jit backend. | ||
pub trait JitElement: | ||
|
@@ -90,5 +91,27 @@ impl JitElement for f32 { | |
} | ||
} | ||
|
||
impl JitElement for f16 { | ||
fn type_name() -> &'static str { | ||
"f16" | ||
} | ||
fn as_bytes(slice: &[Self]) -> &[u8] { | ||
bytemuck::cast_slice(slice) | ||
} | ||
fn from_bytes(bytes: &[u8]) -> &[Self] { | ||
bytemuck::cast_slice(bytes) | ||
} | ||
fn gpu_elem() -> gpu::Elem { | ||
gpu::Elem::Half | ||
} | ||
Comment on lines
+104
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The gpu element would be |
||
fn maximum_value() -> Self { | ||
f16::MAX | ||
} | ||
fn minimum_value() -> Self { | ||
f16::MIN | ||
} | ||
} | ||
|
||
impl FloatElement for f32 {} | ||
impl IntElement for i32 {} | ||
impl FloatElement for f16 {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,11 @@ impl<F: FloatElement, I: IntElement> WgslCompiler<F, I> { | |
self.num_inputs = value.inputs.len(); | ||
self.num_outputs = value.outputs.len(); | ||
|
||
let features = match F::gpu_elem() { | ||
gpu::Elem::Half => vec![wgsl::Feature::ShaderF16], | ||
_ => vec![], | ||
}; | ||
Comment on lines
+81
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would check using |
||
|
||
let instructions = self.compile_scope(&mut value.body); | ||
let extensions = register_extensions(&instructions); | ||
let body = wgsl::Body { | ||
|
@@ -114,6 +119,7 @@ impl<F: FloatElement, I: IntElement> WgslCompiler<F, I> { | |
workgroup_id: self.workgroup_id, | ||
body, | ||
extensions, | ||
features, | ||
} | ||
} | ||
|
||
|
@@ -129,6 +135,7 @@ impl<F: FloatElement, I: IntElement> WgslCompiler<F, I> { | |
fn compile_elem(value: gpu::Elem) -> wgsl::Elem { | ||
match value { | ||
gpu::Elem::Float => F::wgpu_elem(), | ||
gpu::Elem::Half => F::wgpu_elem(), | ||
Comment on lines
137
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line pretty much explains why we don't need |
||
gpu::Elem::Int => I::wgpu_elem(), | ||
gpu::Elem::UInt => wgsl::Elem::U32, | ||
gpu::Elem::Bool => wgsl::Elem::Bool, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to add
Half
here,Float
should cover all float types of all precisions in this context.