From 0612f369876d2991008de418e78011b71040a807 Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Fri, 24 Mar 2017 19:12:11 -0800 Subject: Automated rollback of change 151188187 Change: 151201476 --- tensorflow/contrib/cmake/tf_python.cmake | 2 - tensorflow/core/framework/tensor.h | 2 - tensorflow/python/BUILD | 11 ----- tensorflow/python/client/tf_session_helper.cc | 32 +++++++++++- .../python/lib/core/ndarray_tensor_bridge.cc | 57 ---------------------- tensorflow/python/lib/core/ndarray_tensor_bridge.h | 31 ------------ tensorflow/python/lib/core/py_func.cc | 50 ++----------------- 7 files changed, 35 insertions(+), 150 deletions(-) delete mode 100644 tensorflow/python/lib/core/ndarray_tensor_bridge.cc delete mode 100644 tensorflow/python/lib/core/ndarray_tensor_bridge.h diff --git a/tensorflow/contrib/cmake/tf_python.cmake b/tensorflow/contrib/cmake/tf_python.cmake index 02038da7f8..5296feb980 100755 --- a/tensorflow/contrib/cmake/tf_python.cmake +++ b/tensorflow/contrib/cmake/tf_python.cmake @@ -648,8 +648,6 @@ set (pywrap_tensorflow_internal_src "${tensorflow_source_dir}/tensorflow/python/framework/python_op_gen.cc" "${tensorflow_source_dir}/tensorflow/python/lib/core/numpy.h" "${tensorflow_source_dir}/tensorflow/python/lib/core/numpy.cc" - "${tensorflow_source_dir}/tensorflow/python/lib/core/ndarray_tensor_bridge.h" - "${tensorflow_source_dir}/tensorflow/python/lib/core/ndarray_tensor_bridge.cc" "${tensorflow_source_dir}/tensorflow/python/lib/core/py_func.h" "${tensorflow_source_dir}/tensorflow/python/lib/core/py_func.cc" "${tensorflow_source_dir}/tensorflow/python/lib/io/py_record_reader.h" diff --git a/tensorflow/core/framework/tensor.h b/tensorflow/core/framework/tensor.h index 041661cee7..2d5e70cf76 100644 --- a/tensorflow/core/framework/tensor.h +++ b/tensorflow/core/framework/tensor.h @@ -446,8 +446,6 @@ class Tensor { template friend class CreateVariableOp; friend class OpKernelContext; // For access to RefCountIsOne(). - friend class NumpyTensorBuffer; // For access to the private constructor - // taking the buffer. // Creates a tensor with the input datatype, shape and buf. // diff --git a/tensorflow/python/BUILD b/tensorflow/python/BUILD index 468ebe725d..c8d19d4c6d 100644 --- a/tensorflow/python/BUILD +++ b/tensorflow/python/BUILD @@ -164,15 +164,6 @@ cc_library( ], ) -cc_library( - name = "ndarray_tensor_bridge", - srcs = ["lib/core/ndarray_tensor_bridge.cc"], - hdrs = ["lib/core/ndarray_tensor_bridge.h"], - deps = [ - ":numpy_lib", - ], -) - cc_library( name = "kernel_registry", srcs = ["util/kernel_registry.cc"], @@ -189,7 +180,6 @@ cc_library( srcs = ["lib/core/py_func.cc"], hdrs = ["lib/core/py_func.h"], deps = [ - ":ndarray_tensor_bridge", ":numpy_lib", "//tensorflow/core:framework", "//tensorflow/core:lib", @@ -2520,7 +2510,6 @@ tf_cuda_library( hdrs = ["client/tf_session_helper.h"], deps = [ ":construction_fails_op", - ":ndarray_tensor_bridge", ":numpy_lib", ":test_ops_kernels", "//tensorflow/c:c_api", diff --git a/tensorflow/python/client/tf_session_helper.cc b/tensorflow/python/client/tf_session_helper.cc index 9f138b5277..a05268241b 100644 --- a/tensorflow/python/client/tf_session_helper.cc +++ b/tensorflow/python/client/tf_session_helper.cc @@ -24,7 +24,6 @@ limitations under the License. #include "tensorflow/core/graph/equal_graph_def.h" #include "tensorflow/core/lib/core/coding.h" #include "tensorflow/core/platform/types.h" -#include "tensorflow/python/lib/core/ndarray_tensor_bridge.h" namespace tensorflow { @@ -460,6 +459,37 @@ Safe_PyObjectPtr make_safe(PyObject* o) { return Safe_PyObjectPtr(o, Py_DECREF_wrapper); } +// Mutex used to serialize accesses to cached vector of pointers to python +// arrays to be dereferenced. +static mutex* DelayedDecrefLock() { + static mutex* decref_lock = new mutex; + return decref_lock; +} + +// Caches pointers to numpy arrays which need to be dereferenced. +static std::vector* DecrefCache() { + static std::vector* decref_cache = new std::vector; + return decref_cache; +} + +// Destructor passed to TF_NewTensor when it reuses a numpy buffer. Stores a +// pointer to the pyobj in a buffer to be dereferenced later when we're actually +// holding the GIL. +static void DelayedNumpyDecref(void* data, size_t len, void* obj) { + mutex_lock ml(*DelayedDecrefLock()); + DecrefCache()->push_back(obj); +} + +// Actually dereferences cached numpy arrays. REQUIRES being called while +// holding the GIL. +static void ClearDecrefCache() { + mutex_lock ml(*DelayedDecrefLock()); + for (void* obj : *DecrefCache()) { + Py_DECREF(reinterpret_cast(obj)); + } + DecrefCache()->clear(); +} + void TF_Run_wrapper_helper(TF_DeprecatedSession* session, const char* handle, const TF_Buffer* run_options, PyObject* feed_dict, const NameVector& output_names, diff --git a/tensorflow/python/lib/core/ndarray_tensor_bridge.cc b/tensorflow/python/lib/core/ndarray_tensor_bridge.cc deleted file mode 100644 index baba144daf..0000000000 --- a/tensorflow/python/lib/core/ndarray_tensor_bridge.cc +++ /dev/null @@ -1,57 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// Must be included first. -#include "tensorflow/python/lib/core/numpy.h" - -#include - -#include "tensorflow/core/platform/mutex.h" -#include "tensorflow/python/lib/core/ndarray_tensor_bridge.h" - -namespace tensorflow { - -// Mutex used to serialize accesses to cached vector of pointers to python -// arrays to be dereferenced. -static mutex* DelayedDecrefLock() { - static mutex* decref_lock = new mutex; - return decref_lock; -} - -// Caches pointers to numpy arrays which need to be dereferenced. -static std::vector* DecrefCache() { - static std::vector* decref_cache = new std::vector; - return decref_cache; -} - -// Destructor passed to TF_NewTensor when it reuses a numpy buffer. Stores a -// pointer to the pyobj in a buffer to be dereferenced later when we're actually -// holding the GIL. -void DelayedNumpyDecref(void* data, size_t len, void* obj) { - mutex_lock ml(*DelayedDecrefLock()); - DecrefCache()->push_back(obj); -} - -// Actually dereferences cached numpy arrays. REQUIRES being called while -// holding the GIL. -void ClearDecrefCache() { - mutex_lock ml(*DelayedDecrefLock()); - for (void* obj : *DecrefCache()) { - Py_DECREF(reinterpret_cast(obj)); - } - DecrefCache()->clear(); -} - -} // namespace tensorflow diff --git a/tensorflow/python/lib/core/ndarray_tensor_bridge.h b/tensorflow/python/lib/core/ndarray_tensor_bridge.h deleted file mode 100644 index 5890e1328e..0000000000 --- a/tensorflow/python/lib/core/ndarray_tensor_bridge.h +++ /dev/null @@ -1,31 +0,0 @@ -/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ -#ifndef TENSORFLOW_PYTHON_LIB_CORE_NDARRAY_TENSOR_BRIDGE_H_ -#define TENSORFLOW_PYTHON_LIB_CORE_NDARRAY_TENSOR_BRIDGE_H_ - -namespace tensorflow { - -// Destructor passed to TF_NewTensor when it reuses a numpy buffer. Stores a -// pointer to the pyobj in a buffer to be dereferenced later when we're actually -// holding the GIL. Data and len are ignored. -void DelayedNumpyDecref(void* data, size_t len, void* obj); - -// Actually dereferences cached numpy arrays. REQUIRES being called while -// holding the GIL. -void ClearDecrefCache(); - -} // namespace tensorflow - -#endif // TENSORFLOW_PYTHON_LIB_CORE_NDARRAY_TENSOR_BRIDGE_H_ diff --git a/tensorflow/python/lib/core/py_func.cc b/tensorflow/python/lib/core/py_func.cc index 5323c0e728..5700414149 100644 --- a/tensorflow/python/lib/core/py_func.cc +++ b/tensorflow/python/lib/core/py_func.cc @@ -25,7 +25,6 @@ limitations under the License. #include "tensorflow/core/platform/macros.h" #include "tensorflow/core/platform/mutex.h" #include "tensorflow/core/platform/types.h" -#include "tensorflow/python/lib/core/ndarray_tensor_bridge.h" namespace tensorflow { namespace { @@ -228,38 +227,6 @@ Status DoCallPyFunc(PyCall* call) { } // end namespace -// Outside anonymous namespace just to make the friend declaration in -// tensorflow::Tensor apply. -class NumpyTensorBuffer : public TensorBuffer { - public: - NumpyTensorBuffer(PyArrayObject* array, size_t len, void* data) - : array_(array), len_(len), data_(data) {} - - ~NumpyTensorBuffer() override { - // Note: The session::run wrapper is responsible for freeing this while - // holding the GIL. - DelayedNumpyDecref(data_, len_, array_); - } - - void* data() const override { return data_; } - size_t size() const override { return len_; } - TensorBuffer* root_buffer() override { return this; } - void FillAllocationDescription(AllocationDescription* proto) const override { - tensorflow::int64 rb = size(); - proto->set_requested_bytes(rb); - proto->set_allocator_name(tensorflow::cpu_allocator()->Name()); - } - Tensor MakeTensor(DataType dtype, TensorShape shape) { - CHECK_EQ(len_, shape.num_elements() * DataTypeSize(dtype)); - return Tensor(dtype, shape, this); - } - - private: - PyArrayObject* array_; - size_t len_; - void* data_; -}; - Status ConvertNdarrayToTensor(PyObject* obj, Tensor* ret) { PyArrayObject* input = reinterpret_cast(obj); DataType dtype; @@ -300,20 +267,11 @@ Status ConvertNdarrayToTensor(PyObject* obj, Tensor* ret) { } default: { TF_RETURN_IF_ERROR(NumericNpDTypeToTfDType(PyArray_TYPE(input), &dtype)); + Tensor t(dtype, shape); CHECK(DataTypeCanUseMemcpy(dtype)); - if (reinterpret_cast(PyArray_DATA(input)) % - EIGEN_MAX_ALIGN_BYTES != - 0) { - Tensor t(dtype, shape); - StringPiece p = t.tensor_data(); - memcpy(const_cast(p.data()), PyArray_DATA(input), p.size()); - *ret = t; - } else { - NumpyTensorBuffer* buf = new NumpyTensorBuffer( - input, shape.num_elements() * DataTypeSize(dtype), - PyArray_DATA(input)); - *ret = buf->MakeTensor(dtype, shape); - } + StringPiece p = t.tensor_data(); + memcpy(const_cast(p.data()), PyArray_DATA(input), p.size()); + *ret = t; } } return Status::OK(); -- cgit v1.2.3