1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/compiler/xla/python/python_ref_manager.h"
17
18 #include "absl/container/inlined_vector.h"
19
20 namespace xla {
21
22 namespace py = pybind11;
23
ManagedPyObjects(PythonRefManager * manager,absl::Span<pybind11::object> objects)24 PythonRefManager::ManagedPyObjects::ManagedPyObjects(
25 PythonRefManager* manager, absl::Span<pybind11::object> objects)
26 : manager_(manager) {
27 objects_.reserve(objects.size());
28 for (pybind11::object& object : objects) {
29 objects_.push_back(std::move(object));
30 }
31 }
32
~ManagedPyObjects()33 PythonRefManager::ManagedPyObjects::~ManagedPyObjects() {
34 if (manager_) {
35 absl::MutexLock lock(&manager_->mu_);
36 for (pybind11::object& object : objects_) {
37 manager_->python_garbage_.push_back(std::move(object));
38 }
39 }
40 }
41
42 std::shared_ptr<PythonRefManager::ManagedPyObjects>
ManageReference(py::object object)43 PythonRefManager::ManageReference(py::object object) {
44 return std::make_shared<ManagedPyObjects>(this,
45 absl::Span<py::object>(&object, 1));
46 }
47
48 std::shared_ptr<PythonRefManager::ManagedPyObjects>
ManageReferences(absl::Span<py::object> objects)49 PythonRefManager::ManageReferences(absl::Span<py::object> objects) {
50 return std::make_shared<ManagedPyObjects>(this, objects);
51 }
52
AddGarbage(absl::Span<py::object> garbage)53 void PythonRefManager::AddGarbage(absl::Span<py::object> garbage) {
54 absl::MutexLock lock(&mu_);
55 for (py::object& o : garbage) {
56 python_garbage_.push_back(std::move(o));
57 }
58 }
59
AddGarbage(absl::Span<std::pair<PyCodeObject *,int> const> garbage)60 void PythonRefManager::AddGarbage(
61 absl::Span<std::pair<PyCodeObject*, int> const> garbage) {
62 absl::MutexLock lock(&mu_);
63 for (const auto& o : garbage) {
64 python_garbage_.push_back(py::reinterpret_steal<py::object>(
65 reinterpret_cast<PyObject*>(o.first)));
66 }
67 }
68
CollectGarbage()69 void PythonRefManager::CollectGarbage() {
70 // TODO(phawkins): we should CHECK(PyGILState_Check());
71 std::deque<pybind11::object> garbage;
72 {
73 absl::MutexLock lock(&mu_);
74 garbage.swap(python_garbage_);
75 }
76 // We defer deleting garbage until the lock is released. It's possible that
77 // deleting garbage will lead to more Python garbage being added; if we held
78 // the lock we would deadlock because absl::Mutex is not reentrant.
79 }
80
GlobalPyRefManager()81 PythonRefManager* GlobalPyRefManager() {
82 static PythonRefManager* static_ref_manager = new PythonRefManager();
83 return static_ref_manager;
84 }
85
86 } // namespace xla
87