Skip to content

Commit cc7f05f

Browse files
A. Unique TensorFlowertensorflower-gardener
authored andcommitted
Restrict the use of OUT_OF_RANGE to what it was intended to.
Clarify that OUT_OF_RANGE is raised only when reaching the end of input for interable contents. Change the few places where we incorrectly raised OUT_OF_RANGE to raise ILLEGAL_ARGUMENT instead. This will make code that catches the OUT_OF_RANGE exception more robust as it won't get confused by spurious uses of the exception class. Change: 119560848
1 parent 6e8e7ca commit cc7f05f

10 files changed

Lines changed: 20 additions & 20 deletions

File tree

tensorflow/core/framework/function.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,17 +678,17 @@ Status FunctionCallFrame::GetRetvals(std::vector<Tensor>* rets) const {
678678

679679
Status FunctionCallFrame::GetArg(int index, Tensor* val) const {
680680
if (index < 0 || static_cast<size_t>(index) >= args_.size()) {
681-
return errors::OutOfRange("GetArg ", index, " is not within [0, ",
682-
args_.size(), ")");
681+
return errors::InvalidArgument("GetArg ", index, " is not within [0, ",
682+
args_.size(), ")");
683683
}
684684
*val = args_[index];
685685
return Status::OK();
686686
}
687687

688688
Status FunctionCallFrame::SetRetval(int index, const Tensor& val) {
689689
if (index < 0 || static_cast<size_t>(index) >= rets_.size()) {
690-
return errors::OutOfRange("SetRetval ", index, " is not within [0, ",
691-
rets_.size(), ")");
690+
return errors::InvalidArgument("SetRetval ", index, " is not within [0, ",
691+
rets_.size(), ")");
692692
}
693693
if (val.dtype() != ret_types_[index]) {
694694
return errors::InvalidArgument(

tensorflow/core/framework/function_test.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ TEST(FunctionCallFrame, Void_Void) {
563563
auto a = test::AsTensor<float>({100});
564564
HasError(frame.SetArgs({a}), "Invalid argument");
565565
Tensor v;
566-
HasError(frame.GetArg(0, &v), "Out of range");
567-
HasError(frame.SetRetval(0, v), "Out of range");
566+
HasError(frame.GetArg(0, &v), "Invalid argument");
567+
HasError(frame.SetRetval(0, v), "Invalid argument");
568568
std::vector<Tensor> rets;
569569
TF_EXPECT_OK(frame.GetRetvals(&rets));
570570
EXPECT_EQ(rets.size(), 0);
@@ -581,16 +581,16 @@ TEST(FunctionCallFrame, Float_Float_Float) {
581581
TF_EXPECT_OK(frame.SetArgs({a, b}));
582582

583583
Tensor v;
584-
HasError(frame.GetArg(-1, &v), "Out of range");
585-
HasError(frame.GetArg(2, &v), "Out of range");
584+
HasError(frame.GetArg(-1, &v), "Invalid argument");
585+
HasError(frame.GetArg(2, &v), "Invalid argument");
586586
TF_EXPECT_OK(frame.GetArg(0, &v));
587587
test::ExpectTensorEqual<float>(a, v);
588588
TF_EXPECT_OK(frame.GetArg(1, &v));
589589
test::ExpectTensorEqual<float>(b, v);
590590

591591
v = test::AsTensor<float>({-100});
592-
HasError(frame.SetRetval(-1, v), "Out of range");
593-
HasError(frame.SetRetval(1, v), "Out of range");
592+
HasError(frame.SetRetval(-1, v), "Invalid argument");
593+
HasError(frame.SetRetval(1, v), "Invalid argument");
594594
HasError(frame.SetRetval(0, test::AsTensor<int64>({-100})),
595595
"Invalid argument: Expects ret[0] to be float");
596596

tensorflow/core/framework/numeric_op.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class BinaryElementWiseOp : public BinaryOp<T> {
9999
#undef NDIM_CASE
100100

101101
default:
102-
context->SetStatus(errors::OutOfRange(
102+
context->SetStatus(errors::InvalidArgument(
103103
"We only handle up to Tensor::dims() up to 8, not ", a.dims()));
104104
break;
105105
}

tensorflow/core/kernels/listdiff_op.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ListDiffOp : public OpKernel {
7373
for (int i = 0, p = 0; i < x_size; ++i) {
7474
if (y_set.count(Tx(i)) == 0) {
7575
OP_REQUIRES(context, p < out_size,
76-
errors::OutOfRange(
76+
errors::InvalidArgument(
7777
"Tried to set output index ", p,
7878
" when output Tensor only had ", out_size,
7979
" elements. Check that your "

tensorflow/core/kernels/reduction_ops_common.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ Status ReductionHelper::Simplify(const Tensor& data, const Tensor& axis,
6363
for (int64 i = 0; i < axis.NumElements(); ++i) {
6464
const int32 index = axis_vec(i);
6565
if (index < 0 || index >= data.dims()) {
66-
return errors::OutOfRange("Invalid reduction dimension (", index,
67-
" for input with ", data.dims(),
68-
" dimension(s)");
66+
return errors::InvalidArgument("Invalid reduction dimension (", index,
67+
" for input with ", data.dims(),
68+
" dimension(s)");
6969
}
7070
bitmap[index] = true;
7171
}

tensorflow/core/kernels/summary_op.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class SummaryHistoOp : public OpKernel {
8989
T v = flat(i);
9090
if (!std::isfinite(v)) {
9191
c->SetStatus(
92-
errors::OutOfRange("Nan in summary histogram for: ", name()));
92+
errors::InvalidArgument("Nan in summary histogram for: ", name()));
9393
break;
9494
}
9595
histo.Add(static_cast<double>(v));

tensorflow/core/lib/core/error_codes.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ enum Code {
9999
// ABORTED, and UNAVAILABLE.
100100
ABORTED = 10;
101101

102-
// Operation was attempted past the valid range. E.g., seeking or
102+
// Operation tried to iterate past the valid input range. E.g., seeking or
103103
// reading past end of file.
104104
//
105105
// Unlike INVALID_ARGUMENT, this error indicates a problem that may

tensorflow/core/ops/logging_ops.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ The generated
8989
[`Summary`](https://www.tensorflow.org/code/tensorflow/core/framework/summary.proto)
9090
has one summary value containing a histogram for `values`.
9191
92-
This op reports an `OutOfRange` error if any value is not finite.
92+
This op reports an `InvalidArgument` error if any value is not finite.
9393
9494
tag: Scalar. Tag to use for the `Summary.Value`.
9595
values: Any shape. Values to use to build the histogram.

tensorflow/python/framework/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def __init__(self, node_def, op, message):
328328

329329

330330
class OutOfRangeError(OpError):
331-
"""Raised when an operation executed past the valid range.
331+
"""Raised when an operation iterates past the valid input range.
332332
333333
This exception is raised in "end-of-file" conditions, such as when a
334334
[`queue.dequeue()`](../../api_docs/python/io_ops.md#QueueBase.dequeue)

tensorflow/python/ops/logging_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def histogram_summary(tag, values, collections=None, name=None):
9494
[`Summary`](https://www.tensorflow.org/code/tensorflow/core/framework/summary.proto)
9595
has one summary value containing a histogram for `values`.
9696
97-
This op reports an `OutOfRange` error if any value is not finite.
97+
This op reports an `InvalidArgument` error if any value is not finite.
9898
9999
Args:
100100
tag: A `string` `Tensor`. 0-D. Tag to use for the summary value.

0 commit comments

Comments
 (0)