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

Fix/wgpu/tanh #1090

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions burn-wgpu/src/codegen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ use std::fmt::Display;
pub enum Function {
Powf(Elem),
Erf(Elem),
#[cfg(target_os = "macos")]
SafeTanh(Elem),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a feature flag for macos

}

impl Display for Function {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Function::Powf(elem) => format_powf(f, elem),
Function::Erf(elem) => format_erf(f, elem),
#[cfg(target_os = "macos")]
Function::SafeTanh(elem) => format_safe_tanh(f, elem),
}
}
}
Expand Down Expand Up @@ -69,3 +73,19 @@ fn erf(x: {elem}) -> {elem} {{
"
))
}

#[cfg(target_os = "macos")]
fn format_safe_tanh(f: &mut core::fmt::Formatter<'_>, elem: &Elem) -> core::fmt::Result {
f.write_fmt(format_args!(
"
/// Metal has a weird numerical behaviour with tanh for inputs over 43.0
fn safe_tanh(x: {elem}) -> {elem} {{
if x > 43.0 {{
return 1.0;
}} else {{
return tanh(x);
}}
}}
"
))
}
4 changes: 4 additions & 0 deletions burn-wgpu/src/codegen/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ impl ElemWiseKernelCodegen<BodyPhase> {
Operator::Erf { input: _, out: _ } => {
register_function(Function::Erf(Elem::F32));
}
#[cfg(target_os = "macos")]
Operator::Tanh { input: _, out: _ } => {
register_function(Function::SafeTanh(Elem::F32))
}
_ => {}
}
self.operations.push(ops.clone());
Expand Down
7 changes: 6 additions & 1 deletion burn-wgpu/src/codegen/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ impl Display for Operator {
Operator::Cos { input, out } => f.write_fmt(format_args!("let {out} = cos({input});")),
Operator::Sin { input, out } => f.write_fmt(format_args!("let {out} = sin({input});")),
Operator::Tanh { input, out } => {
f.write_fmt(format_args!("let {out} = tanh({input});"))
#[cfg(target_os = "macos")]
let result = f.write_fmt(format_args!("let {out} = safe_tanh({input});"));
#[cfg(not(target_os = "macos"))]
let result = f.write_fmt(format_args!("let {out} = tanh({input});"));

result
}
Operator::Erf { input, out } => f.write_fmt(format_args!("let {out} = erf({input});")),
Operator::Recip { input, out } => {
Expand Down
Loading