Skip to content
Permalink
Browse files Browse the repository at this point in the history
Fix check failure in Unbatch Op kernel by checking whether input argu…
…ment is a scalar before trying to extract value.

PiperOrigin-RevId: 461660186
  • Loading branch information
ishark authored and tensorflower-gardener committed Jul 18, 2022
1 parent 36effa9 commit 4419d10
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tensorflow/core/kernels/batch_kernels.cc
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include "tensorflow/core/framework/op_requires.h"
#include "tensorflow/core/framework/resource_mgr.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/tensor_util.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/kernels/batching_util/adaptive_shared_batch_scheduler.h"
Expand Down Expand Up @@ -654,6 +655,12 @@ class UnbatchResource : public ResourceBase {
batch_index_t.shape().dim_size(1), ".");
}

if (!TensorShapeUtils::IsScalar(context->input(2).shape())) {
return errors::InvalidArgument(
"Input id should be scalar; "
"Got: ",
context->input(2).DebugString(), ".");
}
const int64_t batch_key = context->input(2).scalar<int64_t>()();
const bool nonempty_input = batch_index_t.dim_size(0) > 0;

Expand Down
20 changes: 20 additions & 0 deletions tensorflow/python/ops/batch_ops_test.py
Expand Up @@ -236,6 +236,26 @@ def worker():
self.assertEqual(thread_results[0], [2])
self.assertEqual(main_results[0], [3])

def testUnbatchInvalidIdArg(self):
"""Tests that unbatch work together."""
if context.executing_eagerly():
batched_tensor = constant_op.constant(
value=np.random.random(size=(3, 3, 1)), dtype=dtypes.float64)
batched_index = constant_op.constant(
value=np.random.randint(0, 100, size=(3, 3, 1)), dtype=dtypes.int64)
arg_id = constant_op.constant(
value=np.random.randint(0, 100, size=(3, 3, 1)), dtype=dtypes.int64)

with self.assertRaisesRegex(errors.InvalidArgumentError,
"Input id should be scalar;"):
batch_ops.unbatch(
batched_tensor=batched_tensor,
batch_index=batched_index,
id=arg_id,
timeout_micros=50,
container="",
shared_name="")

def testBatchDecoratedWithCapturedInput(self):
"""Tests that the batch_function decorator works."""
if context.executing_eagerly():
Expand Down

0 comments on commit 4419d10

Please sign in to comment.