Skip to content

Support Half/BFloat16 in pdist_forward #7852

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

Merged
merged 1 commit into from
Jan 23, 2025
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
2 changes: 1 addition & 1 deletion kernels/portable/cpu/op_pdist_forward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Tensor& _pdist_forward_out(
ScalarType in_type = in.scalar_type();
constexpr auto name = "_pdist_forward.out";

ET_SWITCH_FLOAT_TYPES(
ET_SWITCH_FLOATHBF16_TYPES(
in_type, ctx, name, CTYPE, [&] { pdist<CTYPE>(in, out, p); });

return out;
Expand Down
87 changes: 55 additions & 32 deletions kernels/test/op_pdist_forward_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,68 @@ class OpPdistForwardOutTest : public ::testing::Test {
// first.
torch::executor::runtime_init();
}
};

TEST_F(OpPdistForwardOutTest, SmokeTest) {
TensorFactory<ScalarType::Float> tfFloat;
template <ScalarType DTYPE>
void test_dtype() {
TensorFactory<DTYPE> tf;

Tensor in = tf.make({4, 5}, {0, 1, 2, 3, 5, 4, 3, 2, -1, 5,
1, 1, -2, 1, 5, 4, 3, 2, -1, 5});
Tensor out = tf.zeros({6});

Tensor in = tfFloat.make(
{4, 5}, {0, 1, 2, 3, 5, 4, 3, 2, -1, 5, 1, 1, -2, 1, 5, 4, 3, 2, -1, 5});
Tensor out = tfFloat.zeros({6});
Tensor l0 = tf.make({6}, {3., 3., 3., 4., 0., 4.});
op_pdist_forward_out(in, 0.0, out);
EXPECT_TENSOR_CLOSE(out, l0);

Tensor l0 = tfFloat.make({6}, {3., 3., 3., 4., 0., 4.});
op_pdist_forward_out(in, 0.0, out);
EXPECT_TENSOR_CLOSE(out, l0);
Tensor l0p5 = tf.make(
{6},
{29.31370926, 19.48528290, 29.31370926, 43.03986740, 0.0, 43.03986740});
op_pdist_forward_out(in, 0.5, out);
if (DTYPE == ScalarType::Half || DTYPE == ScalarType::BFloat16) {
EXPECT_TENSOR_CLOSE_WITH_TOL(
out,
l0p5,
1e-2,
executorch::runtime::testing::internal::kDefaultAtol);
} else {
EXPECT_TENSOR_CLOSE(out, l0p5);
}

Tensor l0p5 = tfFloat.make(
{6},
{29.31370926, 19.48528290, 29.31370926, 43.03986740, 0.0, 43.03986740});
op_pdist_forward_out(in, 0.5, out);
EXPECT_TENSOR_CLOSE(out, l0p5);
Tensor l1 = tf.make({6}, {10., 7., 10., 11., 0., 11.});
op_pdist_forward_out(in, 1.0, out);
EXPECT_TENSOR_CLOSE(out, l1);

Tensor l1 = tfFloat.make({6}, {10., 7., 10., 11., 0., 11.});
op_pdist_forward_out(in, 1.0, out);
EXPECT_TENSOR_CLOSE(out, l1);
Tensor l1p5 = tf.make(
{6}, {7.07743692, 5.19140196, 7.07743692, 7.08359480, 0.0, 7.08359480});
op_pdist_forward_out(in, 1.5, out);
if (DTYPE == ScalarType::Half || DTYPE == ScalarType::BFloat16) {
EXPECT_TENSOR_CLOSE_WITH_TOL(
out,
l1p5,
1e-2,
executorch::runtime::testing::internal::kDefaultAtol);
} else {
EXPECT_TENSOR_CLOSE(out, l1p5);
}

Tensor l1p5 = tfFloat.make(
{6}, {7.07743692, 5.19140196, 7.07743692, 7.08359480, 0.0, 7.08359480});
op_pdist_forward_out(in, 1.5, out);
EXPECT_TENSOR_CLOSE(out, l1p5);
Tensor l2 =
tf.make({6}, {6.0, 4.58257580, 6.0, 5.74456263, 0.0, 5.74456263});
op_pdist_forward_out(in, 2.0, out);
EXPECT_TENSOR_CLOSE(out, l2);

Tensor l2 =
tfFloat.make({6}, {6.0, 4.58257580, 6.0, 5.74456263, 0.0, 5.74456263});
op_pdist_forward_out(in, 2.0, out);
EXPECT_TENSOR_CLOSE(out, l2);
Tensor l3 = tf.make(
{6}, {5.14256334, 4.17933941, 5.14256334, 4.74745941, 0.0, 4.74745941});
op_pdist_forward_out(in, 3.0, out);
EXPECT_TENSOR_CLOSE(out, l3);

Tensor l3 = tfFloat.make(
{6}, {5.14256334, 4.17933941, 5.14256334, 4.74745941, 0.0, 4.74745941});
op_pdist_forward_out(in, 3.0, out);
EXPECT_TENSOR_CLOSE(out, l3);
Tensor linf = tf.make({6}, {4., 4., 4., 4., 0., 4.});
op_pdist_forward_out(in, INFINITY, out);
EXPECT_TENSOR_CLOSE(out, linf);
}
};

Tensor linf = tfFloat.make({6}, {4., 4., 4., 4., 0., 4.});
op_pdist_forward_out(in, INFINITY, out);
EXPECT_TENSOR_CLOSE(out, linf);
TEST_F(OpPdistForwardOutTest, SmokeTest) {
#define TEST_ENTRY(ctype, dtype) test_dtype<ScalarType::dtype>();
ET_FORALL_FLOATHBF16_TYPES(TEST_ENTRY)
#undef TEST_ENTRY
}
Loading