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)27StatusOr<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()41TpuNodeContext::~TpuNodeContext() { 42 tpu::OpsApiFn()->TpuNodeContext_FreeFn(node_context_); 43 } 44 45 /* static */ StopChipHeartbeats()46Status TpuNodeContext::StopChipHeartbeats() { 47 StatusHelper status; 48 tpu::OpsApiFn()->TpuNodeContext_StopChipHeartbeatsFn(status.c_status); 49 return status.status(); 50 } 51 52 /* static */ CloseTpuHost()53Status 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)60Status 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()67TpuPlatformInterface* TpuNodeContext::platform() { 68 return TpuPlatformInterface::GetRegisteredPlatform(); 69 } 70 device_ordinal() const71int TpuNodeContext::device_ordinal() const { return device_ordinal_; } 72 backend() const73xla::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() const82stream_executor::StreamExecutor* TpuNodeContext::stream_executor() const { 83 return backend()->stream_executor(device_ordinal_).ValueOrDie(); 84 } 85 CompactionSupported(int device_ordinal) const86bool TpuNodeContext::CompactionSupported(int device_ordinal) const { 87 return tpu::OpsApiFn()->TpuNodeContext_CompactionSupportedFn(device_ordinal); 88 } 89 90 } // namespace tpu 91 } // namespace tensorflow 92