xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/stream_executor/tpu/tpu_node_context.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 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/stream_executor/tpu/tpu_node_context.h"
17 
18 #include "tensorflow/core/tpu/tpu_api.h"
19 
20 namespace tensorflow {
21 namespace tpu {
22 
23 using stream_executor::port::Status;
24 using stream_executor::port::StatusOr;
25 
26 /*static*/
Create(int device_ordinal)27 StatusOr<std::unique_ptr<TpuNodeContext>> TpuNodeContext::Create(
28     int device_ordinal) {
29   StatusHelper status;
30   XLA_TpuNodeContext* node_context =
31       tpu::OpsApiFn()->TpuNodeContext_CreateFn(device_ordinal, status.c_status);
32   if (!status.status().ok()) {
33     // TpuNodeContext_CreateFn allocates a new XLA_TpuNodeContext regardless of
34     // status. It needs to be freed if it's not given to a TpuNodeContext below.
35     tpu::OpsApiFn()->TpuNodeContext_FreeFn(node_context);
36     return status.status();
37   }
38   return std::make_unique<TpuNodeContext>(device_ordinal, node_context);
39 }
40 
~TpuNodeContext()41 TpuNodeContext::~TpuNodeContext() {
42   tpu::OpsApiFn()->TpuNodeContext_FreeFn(node_context_);
43 }
44 
45 /* static */
StopChipHeartbeats()46 Status TpuNodeContext::StopChipHeartbeats() {
47   StatusHelper status;
48   tpu::OpsApiFn()->TpuNodeContext_StopChipHeartbeatsFn(status.c_status);
49   return status.status();
50 }
51 
52 /* static */
CloseTpuHost()53 Status TpuNodeContext::CloseTpuHost() {
54   StatusHelper status;
55   tpu::OpsApiFn()->TpuNodeContext_CloseTpuHostFn(status.c_status);
56   return status.status();
57 }
58 
59 /* static */
Initialize(int device_ordinal)60 Status TpuNodeContext::Initialize(int device_ordinal) {
61   StatusHelper status;
62   tpu::OpsApiFn()->TpuNodeContext_InitializeFn(device_ordinal, status.c_status);
63   return status.status();
64 }
65 
66 /* static */
platform()67 TpuPlatformInterface* TpuNodeContext::platform() {
68   return TpuPlatformInterface::GetRegisteredPlatform();
69 }
70 
device_ordinal() const71 int TpuNodeContext::device_ordinal() const { return device_ordinal_; }
72 
backend() const73 xla::Backend* TpuNodeContext::backend() const {
74   static xla::Backend* backend =
75       xla::Backend::CreateBackend(
76           xla::BackendOptions().set_platform(platform()))
77           .ValueOrDie()
78           .release();
79   return backend;
80 }
81 
stream_executor() const82 stream_executor::StreamExecutor* TpuNodeContext::stream_executor() const {
83   return backend()->stream_executor(device_ordinal_).ValueOrDie();
84 }
85 
CompactionSupported(int device_ordinal) const86 bool TpuNodeContext::CompactionSupported(int device_ordinal) const {
87   return tpu::OpsApiFn()->TpuNodeContext_CompactionSupportedFn(device_ordinal);
88 }
89 
90 }  // namespace tpu
91 }  // namespace tensorflow
92