Split Safe_PyObjectPtr into its own header file (that doesn't depend on any other TensorFlow libraries.)

PiperOrigin-RevId: 328967293
Change-Id: I095261934e9adf7d53ac0ef34adf16877ead30cb
This commit is contained in:
Edward Loper 2020-08-28 10:48:38 -07:00 committed by TensorFlower Gardener
parent c413149b9b
commit 4777cb01c4
6 changed files with 92 additions and 17 deletions

View File

@ -678,6 +678,7 @@ tf_python_pybind_extension(
"client/tf_session_helper.h",
"lib/core/numpy.h",
"lib/core/safe_ptr.h",
"lib/core/safe_pyobject_ptr.h",
"//tensorflow/c:headers",
"//tensorflow/c/eager:headers",
"//tensorflow/c/eager:pywrap_required_hdrs",
@ -880,6 +881,7 @@ tf_python_pybind_extension(
hdrs = [
"lib/core/ndarray_tensor.h",
"lib/core/safe_ptr.h",
"lib/core/safe_pyobject_ptr.h",
":py_exception_registry_hdr",
"//tensorflow/c:checkpoint_reader_hdrs",
"//tensorflow/c:headers",
@ -940,12 +942,17 @@ tf_python_pybind_extension(
],
)
# TODO(edloper): Remove unused dependency on safe_ptr. (Blocker: there are
# targets that depend are relying on cpp_python_util to pull in safe_ptr's
# third_party/tensorflow/c:c_api_no_xla dependency, which registers
# ops/gradients, rather than depending on it themselves.)
cc_library(
name = "cpp_python_util",
srcs = ["util/util.cc"],
hdrs = ["util/util.h"],
deps = [
":safe_ptr",
":safe_pyobject_ptr",
"//tensorflow/core:lib",
"//tensorflow/core:lib_internal",
"//third_party/python_runtime:headers",
@ -1000,6 +1007,15 @@ tf_python_pybind_extension(
],
)
cc_library(
name = "safe_pyobject_ptr",
srcs = ["lib/core/safe_pyobject_ptr.cc"],
hdrs = ["lib/core/safe_pyobject_ptr.h"],
deps = [
"//third_party/python_runtime:headers",
],
)
cc_library(
name = "safe_ptr",
srcs = [
@ -1008,6 +1024,7 @@ cc_library(
],
hdrs = ["lib/core/safe_ptr.h"],
deps = [
":safe_pyobject_ptr",
"//tensorflow/c:c_api_no_xla",
"//third_party/python_runtime:headers",
],
@ -1021,6 +1038,7 @@ cc_library(
"lib/core/ndarray_tensor_bridge.h",
"lib/core/numpy.h",
"lib/core/safe_ptr.h",
"lib/core/safe_pyobject_ptr.h",
"//tensorflow/c:headers",
"//tensorflow/c/eager:headers",
],
@ -1633,7 +1651,7 @@ cc_library(
hdrs = ["framework/op_def_util.h"],
deps = [
":cpp_python_util",
":safe_ptr",
":safe_pyobject_ptr",
"//tensorflow/core:protos_all_cc",
"@com_google_absl//absl/strings",
],
@ -1644,6 +1662,8 @@ cc_library(
# depending on that target adds dependencies that register objects; and since the
# extension is built as a shared object in some kokoro tests, this causes those objects
# to get registered multiple times (which fails).
# TODO(edloper): Simplify this, once cpp_python_util is changed to not depend on
# safe_ptr (which transitively depends on third_party/tensorflow/c:c_api_no_xla).
tf_python_pybind_extension(
name = "_op_def_util",
srcs = [
@ -1653,6 +1673,7 @@ tf_python_pybind_extension(
hdrs = [
"framework/op_def_util.h",
"lib/core/safe_ptr.h",
"lib/core/safe_pyobject_ptr.h",
"util/util.h",
"//tensorflow/c:headers",
"//tensorflow/c/eager:headers",
@ -8330,6 +8351,7 @@ tf_python_pybind_extension(
srcs = ["mlir_wrapper.cc"],
hdrs = [
"lib/core/safe_ptr.h",
"lib/core/safe_pyobject_ptr.h",
"//tensorflow/c:headers",
"//tensorflow/c/eager:headers",
"//tensorflow/compiler/mlir/python:pywrap_mlir_hdrs",
@ -8361,6 +8383,7 @@ tf_python_pybind_extension(
srcs = ["tfe_wrapper.cc"],
hdrs = [
"lib/core/safe_ptr.h",
"lib/core/safe_pyobject_ptr.h",
"util/util.h",
":py_exception_registry_hdr",
"//tensorflow/c:headers",
@ -8432,6 +8455,7 @@ tf_python_pybind_extension(
name = "_pywrap_parallel_device",
srcs = [
"lib/core/safe_ptr.h",
"lib/core/safe_pyobject_ptr.h",
"//tensorflow/c:headers",
"//tensorflow/c/eager:headers",
"//tensorflow/c/eager/parallel_device:headers",

View File

@ -17,10 +17,6 @@ limitations under the License.
namespace tensorflow {
Safe_PyObjectPtr make_safe(PyObject* object) {
return Safe_PyObjectPtr(object);
}
Safe_TF_TensorPtr make_safe(TF_Tensor* tensor) {
return Safe_TF_TensorPtr(tensor);
}

View File

@ -16,20 +16,17 @@ limitations under the License.
#ifndef TENSORFLOW_PYTHON_LIB_CORE_SAFE_PTR_H_
#define TENSORFLOW_PYTHON_LIB_CORE_SAFE_PTR_H_
#include <memory>
#include <Python.h>
#include <memory>
#include "tensorflow/c/c_api.h"
#include "tensorflow/c/eager/c_api.h"
#include "tensorflow/python/lib/core/safe_pyobject_ptr.h"
namespace tensorflow {
namespace detail {
struct PyDecrefDeleter {
void operator()(PyObject* p) const { Py_DECREF(p); }
};
struct TFTensorDeleter {
void operator()(TF_Tensor* p) const { TF_DeleteTensor(p); }
};
@ -48,11 +45,6 @@ struct TFBufferDeleter {
} // namespace detail
// Safe container for an owned PyObject. On destruction, the reference count of
// the contained object will be decremented.
using Safe_PyObjectPtr = std::unique_ptr<PyObject, detail::PyDecrefDeleter>;
Safe_PyObjectPtr make_safe(PyObject* o);
// Safe containers for an owned TF_Tensor. On destruction, the tensor will be
// deleted by TF_DeleteTensor.
using Safe_TF_TensorPtr = std::unique_ptr<TF_Tensor, detail::TFTensorDeleter>;

View File

@ -0,0 +1,24 @@
/* 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.
==============================================================================*/
#include "tensorflow/python/lib/core/safe_pyobject_ptr.h"
namespace tensorflow {
Safe_PyObjectPtr make_safe(PyObject* object) {
return Safe_PyObjectPtr(object);
}
} // namespace tensorflow

View File

@ -0,0 +1,39 @@
/* 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_SAFE_PYOBJECT_PTR_H_
#define TENSORFLOW_PYTHON_LIB_CORE_SAFE_PYOBJECT_PTR_H_
#include <Python.h>
#include <memory>
namespace tensorflow {
namespace detail {
struct PyDecrefDeleter {
void operator()(PyObject* p) const { Py_DECREF(p); }
};
} // namespace detail
// Safe container for an owned PyObject. On destruction, the reference count of
// the contained object will be decremented.
using Safe_PyObjectPtr = std::unique_ptr<PyObject, detail::PyDecrefDeleter>;
Safe_PyObjectPtr make_safe(PyObject* o);
} // namespace tensorflow
#endif // TENSORFLOW_PYTHON_LIB_CORE_SAFE_PYOBJECT_PTR_H_

View File

@ -24,7 +24,7 @@ limitations under the License.
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/mutex.h"
#include "tensorflow/python/lib/core/safe_ptr.h"
#include "tensorflow/python/lib/core/safe_pyobject_ptr.h"
namespace tensorflow {
namespace swig {