• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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/pjrt/tracked_tfrt_cpu_device_buffer.h"
17 
18 #include "absl/base/casts.h"
19 #include "tfrt/host_context/async_value_ref.h"  // from @tf_runtime
20 
21 namespace xla {
22 
TrackedTfrtCpuDeviceBuffer(bool is_tuple,absl::InlinedVector<std::shared_ptr<MaybeOwningCpuMemory>,4> buffers,absl::InlinedVector<tfrt::AsyncValueRef<CpuEvent>,4> definition_events,std::function<void ()> on_delete_callback)23 TrackedTfrtCpuDeviceBuffer::TrackedTfrtCpuDeviceBuffer(
24     bool is_tuple,
25     absl::InlinedVector<std::shared_ptr<MaybeOwningCpuMemory>, 4> buffers,
26     absl::InlinedVector<tfrt::AsyncValueRef<CpuEvent>, 4> definition_events,
27     std::function<void()> on_delete_callback)
28     : is_tuple_(is_tuple),
29       buffers_(std::move(buffers)),
30       definition_events_(std::move(definition_events)),
31       on_delete_callback_(std::move(on_delete_callback)) {
32   if (is_tuple) {
33     size_t index_table_byte_size = buffers_.size() * sizeof(void*);
34     tuple_index_table_ =
35         MaybeOwningCpuMemory::AllocateShared(index_table_byte_size);
36     uintptr_t* index_table =
37         reinterpret_cast<uintptr_t*>(tuple_index_table_->data());
38     for (int i = 0; i < buffers_.size(); ++i) {
39       index_table[i] = absl::bit_cast<uintptr_t>(buffers_[i]->data());
40     }
41   }
42 }
43 
~TrackedTfrtCpuDeviceBuffer()44 TrackedTfrtCpuDeviceBuffer::~TrackedTfrtCpuDeviceBuffer() {
45   ReleaseDeviceMemory();
46   if (on_delete_callback_) {
47     on_delete_callback_();
48   }
49 }
50 
Buffer(const ShapeIndex & shape_index)51 std::shared_ptr<MaybeOwningCpuMemory> TrackedTfrtCpuDeviceBuffer::Buffer(
52     const ShapeIndex& shape_index) {
53   if (shape_index.empty()) {
54     // shape_index={}
55     if (is_tuple_) return tuple_index_table_;
56     return buffers_[0];
57   }
58   // shape_index={i}
59   CHECK(is_tuple_);
60   CHECK_EQ(shape_index.size(), 1) << "nested tuple not supported";
61   return buffers_[shape_index[0]];
62 }
63 
AddUsageEvents(absl::Span<tfrt::AsyncValueRef<CpuEvent>> events)64 void TrackedTfrtCpuDeviceBuffer::AddUsageEvents(
65     absl::Span<tfrt::AsyncValueRef<CpuEvent>> events) {
66   usage_events_.reserve(usage_events_.size() + events.size());
67   for (auto& ev : events) {
68     usage_events_.push_back(std::move(ev));
69   }
70 }
71 
72 absl::InlinedVector<std::shared_ptr<MaybeOwningCpuMemory>, 4>
ConsumeBuffers()73 TrackedTfrtCpuDeviceBuffer::ConsumeBuffers() {
74   return std::move(buffers_);
75 }
76 
77 absl::InlinedVector<tfrt::AsyncValueRef<CpuEvent>, 4>
LockUseAndTransferUsageEvents()78 TrackedTfrtCpuDeviceBuffer::LockUseAndTransferUsageEvents() {
79   return std::move(usage_events_);
80 }
81 
ReleaseDeviceMemory()82 void TrackedTfrtCpuDeviceBuffer::ReleaseDeviceMemory() {
83   tuple_index_table_.reset();
84   buffers_.clear();
85   definition_events_.clear();
86   usage_events_.clear();
87 }
88 
89 }  // namespace xla
90