Skip to content

Commit dab03d2

Browse files
committed
Fix portable library build on Windows
ghstack-source-id: f13950b ghstack-comment-id: 3172145648 Pull-Request: #13260
1 parent 3483895 commit dab03d2

File tree

12 files changed

+57
-16
lines changed

12 files changed

+57
-16
lines changed

kernels/portable/cpu/op_amax.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <c10/util/irange.h>
1010
#include <cmath>
1111

12+
#include <executorch/kernels/portable/cpu/util/math_util.h>
1213
#include <executorch/kernels/portable/cpu/util/reduce_util.h>
1314
#include <executorch/runtime/kernel/kernel_includes.h>
1415
#include <executorch/runtime/platform/assert.h>
@@ -51,7 +52,7 @@ Tensor& amax_out(
5152
for (const auto out_ix : c10::irange(begin, end)) {
5253
out_data[out_ix] = plan.execute<CTYPE>(
5354
[](CTYPE v, CTYPE max_v) {
54-
return std::isnan(v) || v > max_v ? v : max_v;
55+
return utils::isnan_override(v) || v > max_v ? v : max_v;
5556
},
5657
out_ix);
5758
}

kernels/portable/cpu/op_amin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <c10/util/irange.h>
99
#include <cmath>
1010

11+
#include <executorch/kernels/portable/cpu/util/math_util.h>
1112
#include <executorch/kernels/portable/cpu/util/reduce_util.h>
1213
#include <executorch/runtime/kernel/kernel_includes.h>
1314
#include <executorch/runtime/platform/assert.h>
@@ -50,7 +51,7 @@ Tensor& amin_out(
5051
for (const auto out_ix : c10::irange(begin, end)) {
5152
out_data[out_ix] = plan.execute<CTYPE>(
5253
[](CTYPE v, CTYPE min_v) {
53-
return std::isnan(v) || v < min_v ? v : min_v;
54+
return utils::isnan_override(v) || v < min_v ? v : min_v;
5455
},
5556
out_ix);
5657
}

kernels/portable/cpu/op_argmax.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cmath>
1111
#include <tuple>
1212

13+
#include <executorch/kernels/portable/cpu/util/math_util.h>
1314
#include <executorch/kernels/portable/cpu/util/reduce_util.h>
1415
#include <executorch/runtime/kernel/kernel_includes.h>
1516
#include <executorch/runtime/platform/assert.h>
@@ -55,7 +56,7 @@ Tensor& argmax_out(
5556
// the below condition as written is equivalent to
5657
// !isnan(accval) && (isnan(v) || v > acc_val). See
5758
// argument in op_argmin.cpp.
58-
if (!std::isnan(acc_val) && !(v <= acc_val)) {
59+
if (!utils::isnan_override(acc_val) && !(v <= acc_val)) {
5960
acc_val = v;
6061
acc_ix = ix;
6162
}

kernels/portable/cpu/op_argmin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cmath>
1111
#include <tuple>
1212

13+
#include <executorch/kernels/portable/cpu/util/math_util.h>
1314
#include <executorch/kernels/portable/cpu/util/reduce_util.h>
1415
#include <executorch/runtime/kernel/kernel_includes.h>
1516
#include <executorch/runtime/platform/assert.h>
@@ -62,7 +63,7 @@ Tensor& argmin_out(
6263
// - false, so the result is true. The result is trivially
6364
// - true for the above condition that uses isnan(v) as
6465
// - well.
65-
if (!std::isnan(acc_val) && !(v >= acc_val)) {
66+
if (!utils::isnan_override(acc_val) && !(v >= acc_val)) {
6667
acc_val = v;
6768
acc_ix = ix;
6869
}

kernels/portable/cpu/op_max.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cmath>
1111
#include <tuple>
1212

13+
#include <executorch/kernels/portable/cpu/util/math_util.h>
1314
#include <executorch/kernels/portable/cpu/util/reduce_util.h>
1415
#include <executorch/runtime/kernel/kernel_includes.h>
1516
#include <executorch/runtime/platform/assert.h>
@@ -88,8 +89,8 @@ std::tuple<Tensor&, Tensor&> max_out(
8889
for (const auto out_ix : c10::irange(begin, end)) {
8990
std::tuple<CTYPE, long> acc = reduce_over_dim<CTYPE>(
9091
[](CTYPE v, long ix, CTYPE acc_val, long acc_ix) {
91-
if (!std::isnan(acc_val) &&
92-
(std::isnan(v) || v > acc_val)) {
92+
if (!utils::isnan_override(acc_val) &&
93+
(utils::isnan_override(v) || v > acc_val)) {
9394
acc_val = v;
9495
acc_ix = ix;
9596
}
@@ -132,7 +133,7 @@ max_unary_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
132133
data_out[0] = lower_bound<CTYPE_OUT>();
133134
for (const auto i : c10::irange(in.numel())) {
134135
CTYPE_OUT val = static_cast<CTYPE_OUT>(data_in[i]);
135-
if (std::isnan(val)) {
136+
if (utils::isnan_override(val)) {
136137
data_out[0] = val;
137138
break;
138139
}

kernels/portable/cpu/op_min.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cmath>
1111
#include <tuple>
1212

13+
#include <executorch/kernels/portable/cpu/util/math_util.h>
1314
#include <executorch/kernels/portable/cpu/util/reduce_util.h>
1415
#include <executorch/runtime/kernel/kernel_includes.h>
1516
#include <executorch/runtime/platform/assert.h>
@@ -88,8 +89,8 @@ std::tuple<Tensor&, Tensor&> min_out(
8889
for (const auto out_ix : c10::irange(begin, end)) {
8990
std::tuple<CTYPE, long> acc = reduce_over_dim<CTYPE>(
9091
[](CTYPE v, long ix, CTYPE acc_val, long acc_ix) {
91-
if (!std::isnan(acc_val) &&
92-
(std::isnan(v) || v < acc_val)) {
92+
if (!utils::isnan_override(acc_val) &&
93+
(utils::isnan_override(v) || v < acc_val)) {
9394
acc_val = v;
9495
acc_ix = ix;
9596
}
@@ -132,7 +133,7 @@ min_unary_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
132133
data_out[0] = upper_bound<CTYPE_OUT>();
133134
for (const auto i : c10::irange(in.numel())) {
134135
CTYPE_OUT val = static_cast<CTYPE_OUT>(data_in[i]);
135-
if (std::isnan(val)) {
136+
if (utils::isnan_override(val)) {
136137
data_out[0] = val;
137138
break;
138139
}

kernels/portable/cpu/op_relu.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cmath>
1010

1111
#include <executorch/kernels/portable/cpu/util/functional_util.h>
12+
#include <executorch/kernels/portable/cpu/util/math_util.h>
1213
#include <executorch/runtime/kernel/kernel_includes.h>
1314
#include <executorch/runtime/platform/assert.h>
1415

@@ -45,7 +46,9 @@ Tensor& relu_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
4546
ET_SWITCH_REALHBF16_TYPES(in.scalar_type(), ctx, "relu.out", CTYPE, [&]() {
4647
apply_unary_map_fn(
4748
[](const CTYPE val_in) {
48-
return (std::isnan(val_in) || val_in >= CTYPE(0)) ? val_in : CTYPE(0);
49+
return (utils::isnan_override(val_in) || val_in >= CTYPE(0))
50+
? val_in
51+
: CTYPE(0);
4952
},
5053
in.const_data_ptr<CTYPE>(),
5154
out.mutable_data_ptr<CTYPE>(),

kernels/portable/cpu/op_sign.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cstring>
1111

1212
#include <executorch/kernels/portable/cpu/util/functional_util.h>
13+
#include <executorch/kernels/portable/cpu/util/math_util.h>
1314
#include <executorch/runtime/kernel/kernel_includes.h>
1415
#include <executorch/runtime/platform/assert.h>
1516

@@ -42,7 +43,7 @@ Tensor& sign_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
4243
ET_SWITCH_REALHBF16_TYPES(in.scalar_type(), ctx, "sign.out", CTYPE, [&] {
4344
apply_unary_map_fn(
4445
[](const CTYPE val_in) {
45-
if (std::isnan(val_in)) {
46+
if (utils::isnan_override(val_in)) {
4647
return val_in;
4748
} else {
4849
return static_cast<CTYPE>((val_in > 0) - (val_in < 0));

kernels/portable/cpu/op_topk.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <cmath>
1111
#include <tuple>
1212

13+
#include <executorch/kernels/portable/cpu/util/math_util.h>
14+
#include <executorch/runtime/core/exec_aten/exec_aten.h>
1315
#include <executorch/runtime/kernel/kernel_includes.h>
1416

1517
namespace torch {
@@ -62,7 +64,7 @@ bool float_less_than(T x, T y) {
6264
if constexpr (std::is_integral_v<T>) {
6365
return x < y;
6466
}
65-
return (!std::isnan(x) && std::isnan(y)) || x < y;
67+
return (!utils::isnan_override(x) && utils::isnan_override(y)) || x < y;
6668
}
6769

6870
template <typename CTYPE, typename elem_t = std::pair<CTYPE, int64_t>>

kernels/portable/cpu/util/math_util.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88

99
#pragma once
1010

11+
#include <executorch/runtime/core/exec_aten/exec_aten.h>
12+
1113
#if defined(ET_USE_PYTORCH_HEADERS) && ET_USE_PYTORCH_HEADERS
1214
#include <ATen/cpu/vec/vec.h>
1315
#endif
1416

17+
#include <type_traits>
18+
1519
namespace torch {
1620
namespace executor {
1721
namespace native {
@@ -29,7 +33,8 @@ template <
2933
typename std::enable_if<std::is_integral<INT_T>::value, bool>::type = true>
3034
INT_T floor_divide(INT_T a, INT_T b) {
3135
const auto quot = a / b;
32-
if (std::signbit(a) == std::signbit(b)) {
36+
// MSVC does not like signbit on integral types.
37+
if ((a < 0) == (b < 0)) {
3338
return quot;
3439
}
3540
const auto rem = a % b;
@@ -52,6 +57,20 @@ FLOAT_T floor_divide(FLOAT_T a, FLOAT_T b) {
5257
return div;
5358
}
5459

60+
/**
61+
* A wrapper around std::isnan that works with MSVC. When building with MSVC,
62+
* std::isnan calls with integer inputs fail to compile due to ambiguous
63+
* overload resolution.
64+
*/
65+
template <typename T>
66+
bool isnan_override(T a) {
67+
if constexpr (!std::is_integral_v<T>) {
68+
return std::isnan(a);
69+
} else {
70+
return false;
71+
}
72+
}
73+
5574
/**
5675
* Override min/max so we can emulate PyTorch's behavior with NaN entries.
5776
*/

0 commit comments

Comments
 (0)