Replace some CHECKs with returning an error Status
The public functions already return Status objects, so there is no need to use CHECKs when we could signal an error more safely.
PiperOrigin-RevId: 275201449
Change-Id: Ibcfeee67e21f36f287dff1526e39b53db2641fa2
diff --git a/tensorflow/python/lib/core/ndarray_tensor.cc b/tensorflow/python/lib/core/ndarray_tensor.cc
index bce79b1..50e90e3 100644
--- a/tensorflow/python/lib/core/ndarray_tensor.cc
+++ b/tensorflow/python/lib/core/ndarray_tensor.cc
@@ -321,23 +321,26 @@
// Determine the dimensions of a numpy ndarray to be created to represent an
// output Tensor.
-gtl::InlinedVector<npy_intp, 4> GetPyArrayDimensionsForTensor(
- const TF_Tensor* tensor, tensorflow::int64* nelems) {
+Status GetPyArrayDimensionsForTensor(const TF_Tensor* tensor,
+ gtl::InlinedVector<npy_intp, 4>* dims,
+ tensorflow::int64* nelems) {
+ dims->clear();
const int ndims = TF_NumDims(tensor);
- gtl::InlinedVector<npy_intp, 4> dims(ndims);
if (TF_TensorType(tensor) == TF_RESOURCE) {
- CHECK_EQ(ndims, 0)
- << "Fetching of non-scalar resource tensors is not supported.";
- dims.push_back(TF_TensorByteSize(tensor));
- *nelems = dims[0];
+ if (ndims != 0) {
+ return errors::InvalidArgument(
+ "Fetching of non-scalar resource tensors is not supported.");
+ }
+ dims->push_back(TF_TensorByteSize(tensor));
+ *nelems = dims->back();
} else {
*nelems = 1;
for (int i = 0; i < ndims; ++i) {
- dims[i] = TF_Dim(tensor, i);
- *nelems *= dims[i];
+ dims->push_back(TF_Dim(tensor, i));
+ *nelems *= dims->back();
}
}
- return dims;
+ return Status::OK();
}
// Determine the type description (PyArray_Descr) of a numpy ndarray to be
@@ -418,8 +421,8 @@
TF_Tensor* moved = tensor.release();
int64 nelems = -1;
- gtl::InlinedVector<npy_intp, 4> dims =
- GetPyArrayDimensionsForTensor(moved, &nelems);
+ gtl::InlinedVector<npy_intp, 4> dims;
+ TF_RETURN_IF_ERROR(GetPyArrayDimensionsForTensor(moved, &dims, &nelems));
return ArrayFromMemory(
dims.size(), dims.data(), TF_TensorData(moved),
static_cast<DataType>(dtype), [moved] { TF_DeleteTensor(moved); },
@@ -437,16 +440,18 @@
return Status::OK();
}
int64 nelems = -1;
- gtl::InlinedVector<npy_intp, 4> dims =
- GetPyArrayDimensionsForTensor(tensor.get(), &nelems);
+ gtl::InlinedVector<npy_intp, 4> dims;
+ TF_RETURN_IF_ERROR(
+ GetPyArrayDimensionsForTensor(tensor.get(), &dims, &nelems));
// If the type is neither string nor resource we can reuse the Tensor memory.
TF_Tensor* original = tensor.get();
TF_Tensor* moved = TF_TensorMaybeMove(tensor.release());
if (moved != nullptr) {
- if (ArrayFromMemory(dims.size(), dims.data(), TF_TensorData(moved),
- static_cast<DataType>(TF_TensorType(moved)),
- [moved] { TF_DeleteTensor(moved); }, out_ndarray)
+ if (ArrayFromMemory(
+ dims.size(), dims.data(), TF_TensorData(moved),
+ static_cast<DataType>(TF_TensorType(moved)),
+ [moved] { TF_DeleteTensor(moved); }, out_ndarray)
.ok()) {
return Status::OK();
}