Skip to content

Commit

Permalink
Support Half/BFloat16 in sign (#7866)
Browse files Browse the repository at this point in the history
Partial fix for #7748.
  • Loading branch information
swolchok authored Jan 23, 2025
1 parent 1947734 commit bfccee3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion kernels/portable/cpu/op_sign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Tensor& sign_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
if (in.scalar_type() == exec_aten::ScalarType::Bool) {
memcpy(out.mutable_data_ptr(), in.const_data_ptr(), in.nbytes());
} else {
ET_SWITCH_REAL_TYPES(in.scalar_type(), ctx, "sign.out", CTYPE, [&] {
ET_SWITCH_REALHBF16_TYPES(in.scalar_type(), ctx, "sign.out", CTYPE, [&] {
apply_unary_map_fn(
[](const CTYPE val_in) {
if (std::isnan(val_in)) {
Expand Down
29 changes: 19 additions & 10 deletions kernels/test/op_sign_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,31 @@ class OpSignTest : public OperatorTest {
Tensor& op_sign_out(const Tensor& self, Tensor& out) {
return torch::executor::aten::sign_outf(context_, self, out);
}

template <typename CTYPE, ScalarType DTYPE>
void test_et_dtype() {
TensorFactory<DTYPE> tf;

const auto infinity = std::numeric_limits<CTYPE>::infinity();
const auto nan = std::numeric_limits<CTYPE>::quiet_NaN();
Tensor in = tf.make({1, 7}, {-infinity, -3., -1.5, 0., 1.5, nan, infinity});
Tensor out = tf.zeros({1, 7});
Tensor expected = tf.make({1, 7}, {-1., -1., -1., 0., 1., nan, 1.});

Tensor ret = op_sign_out(in, out);

EXPECT_TENSOR_EQ(out, ret);
EXPECT_TENSOR_CLOSE(out, expected);
}
};

TEST_F(OpSignTest, ETSanityCheckFloat) {
if (torch::executor::testing::SupportedFeatures::get()->is_aten) {
GTEST_SKIP() << "ATen returns 0 on NAN input";
}
TensorFactory<ScalarType::Float> tf;

Tensor in = tf.make({1, 7}, {-INFINITY, -3., -1.5, 0., 1.5, NAN, INFINITY});
Tensor out = tf.zeros({1, 7});
Tensor expected = tf.make({1, 7}, {-1., -1., -1., 0., 1., NAN, 1.});

Tensor ret = op_sign_out(in, out);

EXPECT_TENSOR_EQ(out, ret);
EXPECT_TENSOR_CLOSE(out, expected);
#define TEST_ENTRY(ctype, dtype) test_et_dtype<ctype, ScalarType::dtype>();
ET_FORALL_FLOATHBF16_TYPES(TEST_ENTRY);
#undef TEST_ENTRY
}

TEST_F(OpSignTest, ATenSanityCheckFloat) {
Expand Down

0 comments on commit bfccee3

Please sign in to comment.