Skip to content

Commit

Permalink
Bugfix for the metal elu kernel. (huggingface#2490)
Browse files Browse the repository at this point in the history
* Bugfix for the metal elu kernel.

* Add a test.
  • Loading branch information
LaurentMazare authored Sep 21, 2024
1 parent af21040 commit 844d45c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions candle-core/tests/tensor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ fn unary_op(device: &Device) -> Result<()> {
tensor.sign()?.to_vec1::<f32>()?,
[-1., -1., -1., 0., 0., 1., 1., 1., 1.]
);
let tensor = Tensor::new(&[-1.0f32, 0., -2., 3.], device)?;
let y = tensor.elu(2.)?;
assert_eq!(
test_utils::to_vec1_round(&y, 4)?,
[-1.2642, 0.0000, -1.7293, 3.0000]
);
// This test failed on metal prior to the following PR:
// https://github.com/huggingface/candle/pull/2490
let y = tensor.reshape((2, 2))?.t()?.elu(2.)?.flatten_all()?;
assert_eq!(
test_utils::to_vec1_round(&y, 4)?,
[-1.2642, -1.7293, 0.0000, 3.0000]
);
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion candle-metal-kernels/src/affine.metal
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ kernel void FN_NAME##_strided( \
return; \
} \
const TYPENAME x = input[get_strided_index(id, num_dims, dims, strides)]; \
output[id] = TYPENAME((x > 0)?x: mul * exp(x - 1)); \
output[id] = TYPENAME((x > 0)?x: mul * (exp(x) - 1)); \
} \


Expand Down

0 comments on commit 844d45c

Please sign in to comment.