Skip to content

portable arg{max,min}: optimize update check #8863

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 4 commits into from
Mar 4, 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
5 changes: 4 additions & 1 deletion kernels/portable/cpu/op_argmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ Tensor& argmax_out(
for (const auto out_ix : c10::irange(out.numel())) {
std::tuple<CTYPE, long> acc = reduce_over_dim<CTYPE>(
[](CTYPE v, long ix, CTYPE acc_val, long acc_ix) {
if (!std::isnan(acc_val) && (std::isnan(v) || v > acc_val)) {
// the below condition as written is equivalent to
// !isnan(accval) && (isnan(v) || v > acc_val). See
// argument in op_argmin.cpp.
if (!std::isnan(acc_val) && !(v <= acc_val)) {
acc_val = v;
acc_ix = ix;
}
Expand Down
12 changes: 11 additions & 1 deletion kernels/portable/cpu/op_argmin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@ Tensor& argmin_out(
for (const auto out_ix : c10::irange(out.numel())) {
std::tuple<CTYPE, long> acc = reduce_over_dim<CTYPE>(
[](CTYPE v, long ix, CTYPE acc_val, long acc_ix) {
if (!std::isnan(acc_val) && (std::isnan(v) || v < acc_val)) {
// the below condition as written is equivalent to !isnan(accval) &&
// (isnan(v) || v < acc_val). cases:
// - if neither acc_val nor v is NaN, !(v >= acc_val) is
// trivially equivalent to v < acc_val.
// - if acc_val is NaN, the whole thing is trivially false.
// - if acc_val is not NaN and v is NaN, then v >= acc_val
// - is false because all comparisons involving NaN are
// - false, so the result is true. The result is trivially
// - true for the above condition that uses isnan(v) as
// - well.
if (!std::isnan(acc_val) && !(v >= acc_val)) {
acc_val = v;
acc_ix = ix;
}
Expand Down
13 changes: 13 additions & 0 deletions kernels/test/op_argmax_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,16 @@ TEST_F(OpArgmaxTest, SanityCheckNullDim) {
EXPECT_TENSOR_EQ(out, expected);
// clang-format on
}

TEST_F(OpArgmaxTest, FirstNaNWins) {
TensorFactory<ScalarType::Float> tf_float;
Tensor in = tf_float.make({4}, {1, NAN, -4, NAN});

TensorFactory<ScalarType::Long> tf_long;
Tensor out = tf_long.zeros({});
Tensor expected = tf_long.make({}, {1});

Tensor ret = op_argmax_out(in, {}, false, out);
EXPECT_TENSOR_EQ(out, ret);
EXPECT_TENSOR_EQ(out, expected);
}
13 changes: 13 additions & 0 deletions kernels/test/op_argmin_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,16 @@ TEST_F(OpArgminTest, SanityCheckNullDim) {
EXPECT_TENSOR_EQ(out, expected);
// clang-format on
}

TEST_F(OpArgminTest, FirstNaNWins) {
TensorFactory<ScalarType::Float> tf_float;
Tensor in = tf_float.make({4}, {1, NAN, -4, NAN});

TensorFactory<ScalarType::Long> tf_long;
Tensor out = tf_long.zeros({});
Tensor expected = tf_long.make({}, {1});

Tensor ret = op_argmin_out(in, {}, false, out);
EXPECT_TENSOR_EQ(out, ret);
EXPECT_TENSOR_EQ(out, expected);
}
Loading