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 // Classes for keeping track of on-device state for TPUs. 17 18 #ifndef TENSORFLOW_COMPILER_XRT_XRT_TPU_DEVICE_H_ 19 #define TENSORFLOW_COMPILER_XRT_XRT_TPU_DEVICE_H_ 20 21 #include "tensorflow/compiler/xla/client/local_client.h" 22 #include "tensorflow/core/framework/op_kernel.h" 23 #include "tensorflow/core/framework/resource_mgr.h" 24 #include "tensorflow/stream_executor/tpu/tpu_node_context.h" 25 26 namespace tensorflow { 27 28 // This accessor is used for XLA TPU. It uses the distributed TPU compilation 29 // cache infrastructure which it accesses via the TPU_SYSTEM resource manager. 30 class XRTTpuDeviceAccessor { 31 public: 32 static Status GetResourceManager(OpKernelContext* ctx, ResourceMgr** rm); 33 34 class ScopedRef { 35 public: ScopedRef()36 ScopedRef() {} ~ScopedRef()37 ~ScopedRef() {} 38 39 ScopedRef(const ScopedRef&) = delete; 40 ScopedRef& operator=(const ScopedRef&) = delete; 41 42 // Returns the XLA device properties from the TpuNodeContext object 43 // protected by this ScopedRef. backend()44 xla::Backend* backend() { return node_context_->backend(); } device_ordinal()45 int device_ordinal() { return ordinal_; } 46 47 private: 48 // XRTTpuDeviceAccessor::InitScopedRef is the only way to initialize 49 // ScopedRef. 50 friend class XRTTpuDeviceAccessor; 51 52 Status Acquire(int device_ordinal); 53 54 Status Acquire(OpKernelContext* ctx); 55 56 std::unique_ptr<tpu::TpuNodeContext> node_context_; 57 int ordinal_ = 0; 58 }; 59 60 static Status InitScopedRef(OpKernelContext* ctx, int device_ordinal, 61 ScopedRef* scoped_ref); 62 63 static Status InitScopedRef(OpKernelContext* ctx, ScopedRef* scoped_ref); 64 }; 65 66 } // namespace tensorflow 67 68 #endif // TENSORFLOW_COMPILER_XRT_XRT_TPU_DEVICE_H_ 69