Skip to content

Commit

Permalink
Wrap std::isnan/std::isinf in the portable operators
Browse files Browse the repository at this point in the history
Passing the `std::` functions directory to unary_ufunc_realhb_to_bool
can cause "error: cannot resolve overloaded function ‘isinf’ based
on conversion to type ‘torch::executor::FunctionRef<bool(double)>’"
in some compilation environments.

Might be because these functions can be templatized, or because they
became constexpr in C++23.
  • Loading branch information
dbort committed Apr 15, 2024
1 parent 96ab9cc commit b5c293c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion kernels/portable/cpu/op_isinf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ namespace torch {
namespace executor {
namespace native {

namespace {
// Passing std::isinf directly to unary_ufunc_realhb_to_bool can cause "error:
// cannot resolve overloaded function ‘isinf’ based on conversion to type
// ‘torch::executor::FunctionRef<bool(double)>’" in some compilation
// environments.
bool isinf_wrapper(double num) {
return std::isinf(num);
}
} // namespace

Tensor& isinf_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
return internal::unary_ufunc_realhb_to_bool(std::isinf, ctx, in, out);
return internal::unary_ufunc_realhb_to_bool(isinf_wrapper, ctx, in, out);
}

} // namespace native
Expand Down
12 changes: 11 additions & 1 deletion kernels/portable/cpu/op_isnan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ namespace torch {
namespace executor {
namespace native {

namespace {
// Passing std::isnan directly to unary_ufunc_realhb_to_bool can cause "error:
// cannot resolve overloaded function ‘isnan’ based on conversion to type
// ‘torch::executor::FunctionRef<bool(double)>’" in some compilation
// environments.
bool isnan_wrapper(double num) {
return std::isnan(num);
}
} // namespace

Tensor& isnan_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
return internal::unary_ufunc_realhb_to_bool(std::isnan, ctx, in, out);
return internal::unary_ufunc_realhb_to_bool(isnan_wrapper, ctx, in, out);
}

} // namespace native
Expand Down

0 comments on commit b5c293c

Please sign in to comment.