From b2b1c7181fe1ed116883ef677dbd3ada403b32fa Mon Sep 17 00:00:00 2001 From: Sergei Lebedev Date: Thu, 11 Jul 2019 06:09:44 -0700 Subject: [PATCH] Use self->status in EagerTensor_numpy and EagerTensor_getbuffer There is no reason to create a new status on each call. PiperOrigin-RevId: 257594441 --- tensorflow/python/eager/pywrap_tensor.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tensorflow/python/eager/pywrap_tensor.cc b/tensorflow/python/eager/pywrap_tensor.cc index 3c129fe80bd..ab512e9e630 100644 --- a/tensorflow/python/eager/pywrap_tensor.cc +++ b/tensorflow/python/eager/pywrap_tensor.cc @@ -657,10 +657,11 @@ static PyObject* EagerTensor_copy_to_device(EagerTensor* self, PyObject* args, // other. // Note that if `self` is not on CPU, we raise an Exception. static PyObject* EagerTensor_numpy(EagerTensor* self) { - auto status = tensorflow::make_safe(TF_NewStatus()); - auto* py_array = TFE_TensorHandleToNumpy(self->handle, status.get()); - if (MaybeRaiseExceptionFromTFStatus(status.get(), PyExc_ValueError)) { + auto* py_array = TFE_TensorHandleToNumpy(self->handle, self->status); + if (MaybeRaiseExceptionFromTFStatus(self->status, PyExc_ValueError)) { Py_XDECREF(py_array); + // Cleanup self->status before returning. + TF_SetStatus(self->status, TF_OK, ""); return nullptr; } else { return PyArray_Return(reinterpret_cast(py_array)); @@ -745,13 +746,14 @@ static int EagerTensor_getbuffer(EagerTensor* self, Py_buffer* view, return -1; } - auto status = tensorflow::make_safe(TF_NewStatus()); // TensorHandleToNumpy is zero-copy for everything but DT_RESOURCE and // DT_STRING so the following is only slightly slower than a NumPy-free // implementation. auto py_array = tensorflow::make_safe( - TFE_TensorHandleToNumpy(self->handle, status.get())); - if (MaybeRaiseExceptionFromTFStatus(status.get(), PyExc_BufferError)) { + TFE_TensorHandleToNumpy(self->handle, self->status)); + if (MaybeRaiseExceptionFromTFStatus(self->status, PyExc_BufferError)) { + // Cleanup self->status before returning. + TF_SetStatus(self->status, TF_OK, ""); return -1; } if (PyObject_GetBuffer(py_array.get(), view, flags) < 0) {