xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/pjrt/pjrt_client.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/pjrt_client.h"
17 
18 #include <string>
19 #include <utility>
20 
21 #include "absl/base/casts.h"
22 #include "absl/strings/substitute.h"
23 #include "tensorflow/compiler/xla/util.h"
24 
25 namespace xla {
26 
ToProto() const27 StatusOr<CompileOptionsProto> CompileOptions::ToProto() const {
28   // TODO(b/240299401): Implement.
29   return Unimplemented("Unimplemented!");
30 }
31 
CompileOptionsFromProto(const CompileOptionsProto & input)32 StatusOr<CompileOptions> CompileOptionsFromProto(
33     const CompileOptionsProto& input) {
34   // TODO(b/240299401): Implement.
35   return Unimplemented("Unimplemented!");
36 }
37 
38 PjRtBuffer::ExternalReference::~ExternalReference() = default;
39 
UnsafeBufferPointer(PjRtBuffer * buffer)40 StatusOr<std::uintptr_t> PjRtClient::UnsafeBufferPointer(PjRtBuffer* buffer) {
41   if (buffer->on_device_shape().IsTuple()) {
42     return Unimplemented(
43         "unsafe_buffer_pointer is not implemented for tuple buffers.");
44   }
45 
46   TF_ASSIGN_OR_RETURN(
47       std::unique_ptr<PjRtBuffer::ExternalReference> external_reference_hold,
48       buffer->AcquireExternalReference());
49   const void* ptr = external_reference_hold->OpaqueDeviceMemoryDataPointer();
50   return absl::bit_cast<std::uintptr_t>(ptr);
51 }
52 
~MultiSliceConfig()53 MultiSliceConfig::~MultiSliceConfig() {}
54 
DebugString() const55 std::string CompiledMemoryStats::DebugString() const {
56   return absl::Substitute(
57       "CompiledMemoryStats("
58       "generated_code_size_in_bytes=$0, "
59       "argument_size_in_bytes=$1, "
60       "output_size_in_bytes=$2, "
61       "alias_size_in_bytes=$3, "
62       "temp_size_in_bytes=$4)",
63       generated_code_size_in_bytes, argument_size_in_bytes,
64       output_size_in_bytes, alias_size_in_bytes, temp_size_in_bytes);
65 }
66 
AddChunk(PjRtChunk chunk)67 Status CopyToDeviceStream::AddChunk(PjRtChunk chunk) {
68   absl::MutexLock lock(&mu_);
69   if (current_bytes_ >= total_bytes_) {
70     return xla::Status(tensorflow::error::Code::FAILED_PRECONDITION,
71                        "Stream is already complete");
72   }
73   current_bytes_ += chunk.size();
74   if (current_bytes_ > total_bytes_) {
75     return xla::Status(tensorflow::error::Code::FAILED_PRECONDITION,
76                        absl::StrCat("Stream byte size mismatch: ",
77                                     current_bytes_, " > ", total_bytes_));
78   }
79 
80   buffered_chunks_.push_back(std::move(chunk));
81   return OkStatus();
82 }
83 
ConsumeNextChunk()84 std::optional<PjRtChunk> CopyToDeviceStream::ConsumeNextChunk() {
85   absl::MutexLock lock(&mu_);
86   if (buffered_chunks_.empty() && current_bytes_ >= total_bytes_) {
87     return std::nullopt;
88   }
89   mu_.Await(absl::Condition(
90       +[](std::deque<PjRtChunk>* buffered_chunks) {
91         return !buffered_chunks->empty();
92       },
93       &buffered_chunks_));
94   PjRtChunk chunk = std::move(buffered_chunks_.front());
95   buffered_chunks_.pop_front();
96   return std::move(chunk);
97 }
98 
99 // Defining the first virtual non-pure method, which is usually the virtual
100 // destructor, makes it a key function. This reduces the program size and takes
101 // fewer linker resources.
102 PjRtHostMemoryForDeviceManager::~PjRtHostMemoryForDeviceManager() = default;
103 
104 }  // namespace xla
105