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_ && !objects_.empty()) {
35 manager_->AddGarbage(absl::MakeSpan(objects_));
36 }
37 }
38
39 std::shared_ptr<PythonRefManager::ManagedPyObjects>
ManageReference(py::object object)40 PythonRefManager::ManageReference(py::object object) {
41 return std::make_shared<ManagedPyObjects>(this,
42 absl::Span<py::object>(&object, 1));
43 }
44
45 std::shared_ptr<PythonRefManager::ManagedPyObjects>
ManageReferences(absl::Span<py::object> objects)46 PythonRefManager::ManageReferences(absl::Span<py::object> objects) {
47 return std::make_shared<ManagedPyObjects>(this, objects);
48 }
49
AddGarbage(absl::Span<py::object> garbage)50 void PythonRefManager::AddGarbage(absl::Span<py::object> garbage) {
51 absl::MutexLock lock(&mu_);
52 // We want to collect arbitrary python garbage (e.g., buffers) aggressively.
53 garbage_count_.fetch_add(100, std::memory_order_relaxed);
54 for (py::object& o : garbage) {
55 python_garbage_.push_back(std::move(o));
56 }
57 }
58
AddGarbage(absl::Span<std::pair<PyCodeObject *,int> const> garbage)59 void PythonRefManager::AddGarbage(
60 absl::Span<std::pair<PyCodeObject*, int> const> garbage) {
61 absl::MutexLock lock(&mu_);
62 // We don't care about collecting stack frame objects often. We grab a lot of
63 // tracebacks and the code objects are most likely live for the entire
64 // process.
65 garbage_count_.fetch_add(1, std::memory_order_relaxed);
66 for (const auto& o : garbage) {
67 python_garbage_.push_back(py::reinterpret_steal<py::object>(
68 reinterpret_cast<PyObject*>(o.first)));
69 }
70 }
71
CollectGarbage()72 void PythonRefManager::CollectGarbage() {
73 // TODO(phawkins): we should CHECK(PyGILState_Check());
74 std::deque<pybind11::object> garbage;
75 {
76 absl::MutexLock lock(&mu_);
77 garbage_count_ = 0;
78 garbage.swap(python_garbage_);
79 }
80 // We defer deleting garbage until the lock is released. It's possible that
81 // deleting garbage will lead to more Python garbage being added; if we held
82 // the lock we would deadlock because absl::Mutex is not reentrant.
83 }
84
GlobalPyRefManager()85 PythonRefManager* GlobalPyRefManager() {
86 static PythonRefManager* static_ref_manager = new PythonRefManager();
87 return static_ref_manager;
88 }
89
90 } // namespace xla
91