Skip to content

Commit afb0d90

Browse files
committed
Bucketize: add tests for inf inputs and boundaries
1 parent 8137a40 commit afb0d90

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

kernels/test/op_bucketize_test.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <executorch/runtime/core/exec_aten/exec_aten.h>
55
#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h>
66
#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h>
7-
87
#include <gtest/gtest.h>
8+
#include <limits>
99

1010
using namespace ::testing;
1111
using executorch::aten::Scalar;
@@ -78,6 +78,21 @@ TEST_F(OpBucketizeScalarTest, ScalarEmptyBoundaries) {
7878
EXPECT_TENSOR_EQ(out, expected);
7979
}
8080

81+
TEST_F(OpBucketizeScalarTest, ScalarInfInput) {
82+
TensorFactory<ScalarType::Long> tf_out;
83+
TensorFactory<ScalarType::Int> tf_bound;
84+
85+
Scalar value = std::numeric_limits<float>::infinity();
86+
Tensor boundaries = tf_bound.make({5}, {0, 2, 4, 6, 8});
87+
Tensor expected = tf_out.make({}, {5});
88+
Tensor out = tf_out.zeros({});
89+
90+
Tensor ret = op_bucketize_out(value, boundaries, false, true, out);
91+
92+
EXPECT_TENSOR_EQ(ret, expected);
93+
EXPECT_TENSOR_EQ(out, expected);
94+
}
95+
8196
TEST_F(OpBucketizeScalarTest, ScalarBoundaryTypes) {
8297
test_bucketize_bound_types();
8398
}
@@ -327,6 +342,42 @@ TEST_F(OpBucketizeTest, EmptyAll) {
327342
EXPECT_TENSOR_EQ(out, expected);
328343
}
329344

345+
TEST_F(OpBucketizeTest, InfInput) {
346+
TensorFactory<ScalarType::Long> tf_out;
347+
TensorFactory<ScalarType::Float> tf_dtype;
348+
349+
Tensor values = tf_dtype.make(
350+
{2},
351+
{-std::numeric_limits<float>::infinity(),
352+
std::numeric_limits<float>::infinity()});
353+
Tensor boundaries = tf_dtype.make({5}, {0, 3, 5, 7, 9});
354+
Tensor expected = tf_out.make({2}, {0, 5});
355+
Tensor out = tf_out.zeros({2});
356+
357+
Tensor ret = op_bucketize_out(values, boundaries, false, true, out);
358+
359+
EXPECT_TENSOR_EQ(ret, expected);
360+
EXPECT_TENSOR_EQ(out, expected);
361+
}
362+
363+
TEST_F(OpBucketizeTest, InfBoundaries) {
364+
TensorFactory<ScalarType::Long> tf_out;
365+
TensorFactory<ScalarType::Float> tf_dtype;
366+
367+
Tensor values = tf_dtype.make({2, 2}, {1, 4, 6, 8});
368+
Tensor boundaries = tf_dtype.make(
369+
{2},
370+
{-std::numeric_limits<float>::infinity(),
371+
std::numeric_limits<float>::infinity()});
372+
Tensor expected = tf_out.ones({2, 2});
373+
Tensor out = tf_out.zeros({2, 2});
374+
375+
Tensor ret = op_bucketize_out(values, boundaries, false, true, out);
376+
377+
EXPECT_TENSOR_EQ(ret, expected);
378+
EXPECT_TENSOR_EQ(out, expected);
379+
}
380+
330381
TEST_F(OpBucketizeTest, BoundariesNDFails) {
331382
TensorFactory<ScalarType::Long> tf_out;
332383
TensorFactory<ScalarType::Float> tf_dtype;

kernels/test/test_bucketize.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,29 @@ def test_bucketize_tensor_empty_input(self):
152152
model, (x, bounds), "test_bucketize_tensor_empty_input.pte"
153153
)
154154

155+
def test_bucketize_tensor_inf_input(self):
156+
"""Test bucketize.Tensor_out: (Tensor, Tensor, bool, bool) -> Tensor."""
157+
model = BucketizeModule(out_int32=False, right=False)
158+
x = torch.tensor([-torch.inf, torch.inf], dtype=torch.float)
159+
bounds = torch.tensor([0, 3, 5, 7, 9], dtype=torch.float)
160+
self._run_and_compare(model, (x, bounds), "test_bucketize_tensor_inf_input.pte")
161+
162+
def test_bucketize_tensor_inf_boundary(self):
163+
"""Test bucketize.Tensor_out: (Tensor, Tensor, bool, bool) -> Tensor."""
164+
model = BucketizeModule(out_int32=False, right=False)
165+
x = torch.tensor([[1, 2, 3, 4]], dtype=torch.float)
166+
bounds = torch.tensor([-torch.inf, torch.inf], dtype=torch.float)
167+
self._run_and_compare(
168+
model, (x, bounds), "test_bucketize_tensor_inf_boundary.pte"
169+
)
170+
171+
def test_bucketize_scalar_inf_input(self):
172+
"""Test bucketize.Tensor_out: (Tensor, Tensor, bool, bool) -> Tensor."""
173+
model = BucketizeModule(out_int32=False, right=False)
174+
x = torch.inf
175+
bounds = torch.tensor([0, 3, 5, 7, 9], dtype=torch.float)
176+
self._run_and_compare(model, (x, bounds), "test_bucketize_scalar_inf_input.pte")
177+
155178

156179
if __name__ == "__main__":
157180
unittest.main()

0 commit comments

Comments
 (0)