Skip to content
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
17 changes: 17 additions & 0 deletions tensorflow/core/kernels/searchsorted_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/kernels/fill_functor.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/types.h"

Expand Down Expand Up @@ -115,6 +116,14 @@ class UpperBoundOp : public OpKernel {
auto output = output_t->template flat<OutType>();
const auto sorted_inputs = sorted_inputs_t.template flat<T>();
const auto values = values_t.template flat<T>();

// For empty inputs, all values will be placed at the zeroth position.
if (sorted_inputs.size() == 0) {
functor::SetZeroFunctor<Device, OutType> set_zero;
set_zero(ctx->eigen_device<Device>(), output);
return;
}

OP_REQUIRES_OK(
ctx, functor::UpperBoundFunctor<Device, T, OutType>::Compute(
ctx, sorted_inputs, values, sorted_inputs_t.dim_size(0),
Expand Down Expand Up @@ -160,6 +169,14 @@ class LowerBoundOp : public OpKernel {
auto output = output_t->template flat<OutType>();
const auto sorted_inputs = sorted_inputs_t.template flat<T>();
const auto values = values_t.template flat<T>();

// For empty inputs, all values will be placed at the zeroth position.
if (sorted_inputs.size() == 0) {
functor::SetZeroFunctor<Device, OutType> set_zero;
set_zero(ctx->eigen_device<Device>(), output);
return;
}

OP_REQUIRES_OK(
ctx, functor::LowerBoundFunctor<Device, T, OutType>::Compute(
ctx, sorted_inputs, values, sorted_inputs_t.dim_size(0),
Expand Down
11 changes: 11 additions & 0 deletions tensorflow/python/kernel_tests/array_ops/array_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,17 @@ def testZeroValueSize(self):
side=side,
out_type=dtype), array_ops.zeros([2, 0], dtype))

def testZeroInputSize(self):
dtype = dtypes.int32
for side in ("left", "right"):
with self.subTest(side=side):
self.assertAllEqual(
array_ops.searchsorted(
array_ops.ones([2, 0]),
array_ops.ones([2, 3]),
side=side,
out_type=dtype), array_ops.zeros([2, 3], dtype))

def testInt64(self):

@def_function.function
Expand Down