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 #ifndef TENSORFLOW_STREAM_EXECUTOR_TPU_TPU_NODE_CONTEXT_H_ 17 #define TENSORFLOW_STREAM_EXECUTOR_TPU_TPU_NODE_CONTEXT_H_ 18 19 #include <string> 20 21 #include "absl/memory/memory.h" 22 #include "tensorflow/compiler/xla/service/backend.h" 23 #include "tensorflow/compiler/xla/service/stream_pool.h" 24 #include "tensorflow/compiler/xla/service/transfer_manager.h" 25 #include "tensorflow/core/framework/op_kernel.h" 26 #include "tensorflow/core/platform/macros.h" 27 #include "tensorflow/core/tpu/tpu_ops_c_api.h" 28 #include "tensorflow/stream_executor/device_memory_allocator.h" 29 #include "tensorflow/stream_executor/lib/status.h" 30 #include "tensorflow/stream_executor/lib/statusor.h" 31 #include "tensorflow/stream_executor/tpu/status_helper.h" 32 #include "tensorflow/stream_executor/tpu/tpu_platform_interface.h" 33 34 namespace tensorflow { 35 namespace tpu { 36 37 // A TpuNodeContext object represents a specific TPU node (core). The static 38 // class methods represent host-wide actions. 39 // 40 // First call Initialize in a freshly reset system. Then call Create to talk to 41 // individual nodes. 42 class TpuNodeContext final { 43 public: 44 using Status = stream_executor::port::Status; 45 template <typename T> 46 using StatusOr = stream_executor::port::StatusOr<T>; 47 48 static StatusOr<std::unique_ptr<TpuNodeContext>> Create(int device_ordinal); 49 TpuNodeContext(int device_ordinal,XLA_TpuNodeContext * node_context)50 explicit TpuNodeContext(int device_ordinal, XLA_TpuNodeContext* node_context) 51 : device_ordinal_(device_ordinal), node_context_(node_context) { 52 CHECK_NE(node_context, nullptr); 53 } 54 ~TpuNodeContext(); 55 56 static Status StopChipHeartbeats(); 57 58 static Status CloseTpuHost(); 59 60 static Status Initialize(int device_ordinal); 61 62 static TpuPlatformInterface* platform(); 63 64 int device_ordinal() const; 65 66 xla::Backend* backend() const; 67 68 stream_executor::StreamExecutor* stream_executor() const; 69 70 private: 71 const int device_ordinal_; 72 XLA_TpuNodeContext* const node_context_; 73 74 TF_DISALLOW_COPY_AND_ASSIGN(TpuNodeContext); 75 }; 76 77 } // namespace tpu 78 } // namespace tensorflow 79 80 #endif // TENSORFLOW_STREAM_EXECUTOR_TPU_TPU_NODE_CONTEXT_H_ 81