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