Skip to content

Commit 68e8534

Browse files
committed
Issue apache#45083 - Adding HalfFloat kernels for is_nan, is_inf, is_finite, negate, sign.
1 parent 6e94ba7 commit 68e8534

5 files changed

Lines changed: 112 additions & 0 deletions

File tree

‎cpp/src/arrow/compute/kernels/base_arithmetic_internal.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "arrow/type_traits.h"
2626
#include "arrow/util/decimal.h"
2727
#include "arrow/util/int_util_overflow.h"
28+
#include "arrow/util/float16.h"
2829
#include "arrow/util/macros.h"
2930

3031
namespace arrow {
@@ -489,6 +490,19 @@ struct Negate {
489490
}
490491
};
491492

493+
// Can't tell the difference between HalfFloatType and UInt16Type in
494+
// enable_if_unsigned_integer_value above, so seperate operator needed
495+
struct NegateHalfFloat {
496+
template <typename T, typename Arg>
497+
static constexpr enable_if_unsigned_integer_value<T> Call(KernelContext*, Arg arg,
498+
Status*) {
499+
static_assert(std::is_same<T, Arg>::value, "");
500+
using ::arrow::util::Float16;
501+
auto val = Float16::FromBits(static_cast<uint16_t>(arg));
502+
return (-val).bits();
503+
}
504+
};
505+
492506
struct NegateChecked {
493507
template <typename T, typename Arg>
494508
static enable_if_signed_integer_value<Arg, T> Call(KernelContext*, Arg arg,
@@ -652,6 +666,18 @@ struct Sign {
652666
}
653667
};
654668

669+
struct SignHalfFloat {
670+
template <typename T, typename Arg>
671+
static constexpr enable_if_unsigned_integer_value<Arg, T> Call(KernelContext*, Arg arg,
672+
Status*) {
673+
using ::arrow::util::Float16;
674+
auto val = Float16::FromBits(static_cast<uint16_t>(arg));
675+
return val.is_nan() ? arg :
676+
(val.is_zero() ? Float16(0).bits() :
677+
(val.signbit() ? Float16(-1).bits() : Float16(1).bits()));
678+
}
679+
};
680+
655681
struct Max {
656682
template <typename T, typename Arg0, typename Arg1>
657683
static constexpr enable_if_not_floating_value<T> Call(KernelContext*, Arg0 arg0,

‎cpp/src/arrow/compute/kernels/scalar_arithmetic.cc‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,14 @@ void AddDecimalBinaryKernels(const std::string& name, ScalarFunction* func) {
687687
DCHECK_OK(func->AddKernel({in_type256, in_type256}, out_type, exec256));
688688
}
689689

690+
template <typename Op>
691+
void AddHalfFloatUnaryKernel(ScalarFunction* func) {
692+
OutputType out_type(FirstType);
693+
auto in_type = InputType(Type::HALF_FLOAT);
694+
auto exec = ScalarUnaryNotNull<HalfFloatType, HalfFloatType, Op>::Exec;
695+
DCHECK_OK(func->AddKernel({in_type}, out_type, exec));
696+
}
697+
690698
// Generate a kernel given an arithmetic functor
691699
template <template <typename...> class KernelGenerator, typename OutType, typename Op>
692700
ArrayKernelExec GenerateArithmeticWithFixedIntOutType(detail::GetTypeId get_id) {
@@ -1695,6 +1703,7 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) {
16951703
// ----------------------------------------------------------------------
16961704
auto negate = MakeUnaryArithmeticFunction<Negate>("negate", negate_doc);
16971705
AddDecimalUnaryKernels<Negate>(negate.get());
1706+
AddHalfFloatUnaryKernel<NegateHalfFloat>(negate.get());
16981707

16991708
// Add neg(duration) -> duration
17001709
for (auto unit : TimeUnit::values()) {
@@ -1755,6 +1764,10 @@ void RegisterScalarArithmetic(FunctionRegistry* registry) {
17551764
auto exec = ScalarUnary<Int8Type, Int64Type, Sign>::Exec;
17561765
DCHECK_OK(sign->AddKernel({duration(unit)}, int8(), std::move(exec)));
17571766
}
1767+
{
1768+
auto exec = ScalarUnary<HalfFloatType, HalfFloatType, SignHalfFloat>::Exec;
1769+
DCHECK_OK(sign->AddKernel({InputType(Type::HALF_FLOAT)}, float16(), std::move(exec)));
1770+
}
17581771
DCHECK_OK(registry->AddFunction(std::move(sign)));
17591772

17601773
// ----------------------------------------------------------------------

‎cpp/src/arrow/compute/kernels/scalar_arithmetic_test.cc‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <algorithm>
2222
#include <cmath>
23+
#include <limits>
2324
#include <memory>
2425
#include <string>
2526
#include <utility>
@@ -35,6 +36,7 @@
3536
#include "arrow/type_traits.h"
3637
#include "arrow/util/bit_util.h"
3738
#include "arrow/util/checked_cast.h"
39+
#include "arrow/util/float16.h"
3840
#include "arrow/util/math_constants.h"
3941
#include "arrow/util/string.h"
4042

@@ -2926,6 +2928,22 @@ TYPED_TEST(TestUnaryArithmeticFloating, Sign) {
29262928
this->AssertUnaryOp(sign, this->MakeScalar(max), this->MakeScalar(1));
29272929
}
29282930

2931+
TEST(TestUnaryArithmeticHalfFloat, Negate) {
2932+
CheckScalar("negate", {ArrayFromJSON(float16(), "[1.0, -2.5, null, Inf]")},
2933+
ArrayFromJSON(float16(), "[-1.0, 2.5, null, -Inf]"));
2934+
2935+
// Negating actual uint16s wraps around
2936+
// TODO does this need to be portable?
2937+
CheckScalar("negate", {ArrayFromJSON(uint16(), "[0, 1]")},
2938+
ArrayFromJSON(uint16(), "[0, 65535]"));
2939+
2940+
}
2941+
2942+
TEST(TestUnaryArithmeticHalfFloat, Sign) {
2943+
CheckScalar("sign", {ArrayFromJSON(float16(), "[0, 1.0, -2.5, NaN, null, Inf, -Inf]")},
2944+
ArrayFromJSON(float16(), "[0, 1, -1, NaN, null, 1, -1]"));
2945+
}
2946+
29292947
} // namespace
29302948
} // namespace compute
29312949
} // namespace arrow

‎cpp/src/arrow/compute/kernels/scalar_validity.cc‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
// under the License.
1717

1818
#include <cmath>
19+
#include <iostream> // remove when testing done
1920

2021
#include "arrow/compute/api_scalar.h"
2122
#include "arrow/compute/kernels/common_internal.h"
2223

2324
#include "arrow/util/bit_util.h"
2425
#include "arrow/util/bitmap_ops.h"
26+
#include "arrow/util/float16.h"
2527
#include "arrow/util/logging_internal.h"
2628

2729
namespace arrow {
@@ -60,13 +62,25 @@ Status IsValidExec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
6062
struct IsFiniteOperator {
6163
template <typename OutType, typename InType>
6264
static constexpr OutType Call(KernelContext*, const InType& value, Status*) {
65+
// this is HalfFloatType, because int types call a different operator
66+
if constexpr(std::is_same_v<InType, uint16_t>) {
67+
using ::arrow::util::Float16;
68+
return Float16::FromBits(static_cast<uint16_t>(value)).is_finite();
69+
}
70+
6371
return std::isfinite(value);
6472
}
6573
};
6674

6775
struct IsInfOperator {
6876
template <typename OutType, typename InType>
6977
static constexpr OutType Call(KernelContext*, const InType& value, Status*) {
78+
// this is HalfFloatType, because int types call a different operator
79+
if constexpr(std::is_same_v<InType, uint16_t>) {
80+
using ::arrow::util::Float16;
81+
return Float16::FromBits(static_cast<uint16_t>(value)).is_infinity();
82+
}
83+
7084
return std::isinf(value);
7185
}
7286
};
@@ -122,6 +136,13 @@ Status IsNullExec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
122136
struct IsNanOperator {
123137
template <typename OutType, typename InType>
124138
static constexpr OutType Call(KernelContext*, const InType& value, Status*) {
139+
// this is HalfFloatType, because int types call a different operator
140+
if constexpr(std::is_same_v<InType, uint16_t>) {
141+
using ::arrow::util::Float16;
142+
auto val = Float16::FromBits(static_cast<uint16_t>(value));
143+
return val.is_nan();
144+
}
145+
125146
return std::isnan(value);
126147
}
127148
};
@@ -161,6 +182,7 @@ std::shared_ptr<ScalarFunction> MakeIsFiniteFunction(std::string name, FunctionD
161182

162183
AddFloatValidityKernel<FloatType, IsFiniteOperator>(float32(), func.get());
163184
AddFloatValidityKernel<DoubleType, IsFiniteOperator>(float64(), func.get());
185+
AddFloatValidityKernel<HalfFloatType, IsFiniteOperator>(float16(), func.get());
164186

165187
for (const auto& ty : IntTypes()) {
166188
DCHECK_OK(func->AddKernel({InputType(ty->id())}, boolean(), ConstBoolExec<true>));
@@ -180,6 +202,7 @@ std::shared_ptr<ScalarFunction> MakeIsInfFunction(std::string name, FunctionDoc
180202

181203
AddFloatValidityKernel<FloatType, IsInfOperator>(float32(), func.get());
182204
AddFloatValidityKernel<DoubleType, IsInfOperator>(float64(), func.get());
205+
AddFloatValidityKernel<HalfFloatType, IsInfOperator>(float16(), func.get());
183206

184207
for (const auto& ty : IntTypes()) {
185208
DCHECK_OK(func->AddKernel({InputType(ty->id())}, boolean(), ConstBoolExec<false>));
@@ -199,6 +222,7 @@ std::shared_ptr<ScalarFunction> MakeIsNanFunction(std::string name, FunctionDoc
199222

200223
AddFloatValidityKernel<FloatType, IsNanOperator>(float32(), func.get());
201224
AddFloatValidityKernel<DoubleType, IsNanOperator>(float64(), func.get());
225+
AddFloatValidityKernel<HalfFloatType, IsNanOperator>(float16(), func.get());
202226

203227
for (const auto& ty : IntTypes()) {
204228
DCHECK_OK(func->AddKernel({InputType(ty->id())}, boolean(), ConstBoolExec<false>));

‎cpp/src/arrow/compute/kernels/scalar_validity_test.cc‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "arrow/type_traits.h"
2727
#include "arrow/util/bitmap_reader.h"
2828
#include "arrow/util/checked_cast.h"
29+
#include "arrow/util/float16.h"
2930

3031
namespace arrow {
3132
namespace compute {
@@ -106,6 +107,16 @@ TEST(TestValidityKernels, IsFinite) {
106107
CheckScalar("is_finite",
107108
{ArrayFromJSON(duration(TimeUnit::SECOND), "[0, 1, 42, null]")},
108109
ArrayFromJSON(boolean(), "[true, true, true, null]"));
110+
CheckScalar("is_finite", {ArrayFromJSON(float32(), "[1.0, NaN, Inf, null]")},
111+
ArrayFromJSON(boolean(), "[true, false, false, null]"));
112+
CheckScalar("is_finite", {ArrayFromJSON(float64(), "[1.0, NaN, Inf, null]")},
113+
ArrayFromJSON(boolean(), "[true, false, false, null]"));
114+
CheckScalar("is_finite", {ArrayFromJSON(float16(), "[1.0, NaN, Inf, null]")},
115+
ArrayFromJSON(boolean(), "[true, false, false, null]"));
116+
117+
// Verifies that actual uint16s don't use IsFiniteOperator
118+
CheckScalar("is_finite", {ArrayFromJSON(uint16(), "[1, null]")},
119+
ArrayFromJSON(boolean(), "[true, null]"));
109120
}
110121

111122
TEST(TestValidityKernels, IsInf) {
@@ -121,6 +132,16 @@ TEST(TestValidityKernels, IsInf) {
121132
ArrayFromJSON(boolean(), "[null, null, null, null]"));
122133
CheckScalar("is_inf", {ArrayFromJSON(duration(TimeUnit::SECOND), "[0, 1, 42, null]")},
123134
ArrayFromJSON(boolean(), "[false, false, false, null]"));
135+
CheckScalar("is_inf", {ArrayFromJSON(float32(), "[1.0, NaN, Inf, null]")},
136+
ArrayFromJSON(boolean(), "[false, false, true, null]"));
137+
CheckScalar("is_inf", {ArrayFromJSON(float64(), "[1.0, NaN, Inf, null]")},
138+
ArrayFromJSON(boolean(), "[false, false, true, null]"));
139+
CheckScalar("is_inf", {ArrayFromJSON(float16(), "[1.0, NaN, Inf, null]")},
140+
ArrayFromJSON(boolean(), "[false, false, true, null]"));
141+
142+
// Verifies that actual uint16s don't use IsNanOperator
143+
CheckScalar("is_inf", {ArrayFromJSON(uint16(), "[1, null]")},
144+
ArrayFromJSON(boolean(), "[false, null]"));
124145
}
125146

126147
TEST(TestValidityKernels, IsNan) {
@@ -136,6 +157,16 @@ TEST(TestValidityKernels, IsNan) {
136157
ArrayFromJSON(boolean(), "[null, null, null, null]"));
137158
CheckScalar("is_nan", {ArrayFromJSON(duration(TimeUnit::SECOND), "[0, 1, 42, null]")},
138159
ArrayFromJSON(boolean(), "[false, false, false, null]"));
160+
CheckScalar("is_nan", {ArrayFromJSON(float32(), "[0.0, 1.0, NaN, 2.5, null]")},
161+
ArrayFromJSON(boolean(), "[false, false, true, false, null]"));
162+
CheckScalar("is_nan", {ArrayFromJSON(float64(), "[0.0, 1.0, NaN, 2.5, null]")},
163+
ArrayFromJSON(boolean(), "[false, false, true, false, null]"));
164+
CheckScalar("is_nan", {ArrayFromJSON(float16(), "[1.2, NaN, 2.5, NaN, 6, null]")},
165+
ArrayFromJSON(boolean(), "[false, true, false, true, false, null]"));
166+
167+
// Verifies that actual uint16s don't use IsNanOperator
168+
CheckScalar("is_nan", {ArrayFromJSON(uint16(), "[1, null]")},
169+
ArrayFromJSON(boolean(), "[false, null]"));
139170
}
140171

141172
TEST(TestValidityKernels, IsValidIsNullNullType) {

0 commit comments

Comments
 (0)