1 /* Copyright 2018 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. 17 18 #ifndef TENSORFLOW_COMPILER_XRT_XRT_DEVICE_H_ 19 #define TENSORFLOW_COMPILER_XRT_XRT_DEVICE_H_ 20 21 #include "tensorflow/compiler/xla/client/local_client.h" 22 #include "tensorflow/compiler/xrt/xrt_compilation_cache.h" 23 #include "tensorflow/core/framework/op_kernel.h" 24 #include "tensorflow/core/framework/resource_mgr.h" 25 26 namespace tensorflow { 27 28 // This accessor is used for XLA CPU/GPU. It uses the device resource manager, 29 // so e.g., on multi-GPU setups the compilation cache will not be shared across 30 // devices. 31 class XRTGenericDeviceAccessor { 32 public: 33 static Status GetResourceManager(OpKernelContext* ctx, ResourceMgr** rm); 34 35 static xla::StatusOr<RefPtr<XRTCompilationCache>> GetOrCreateCompilationCache( 36 OpKernelContext* ctx, int64 max_number_of_entries); 37 38 // We use a ScopedRef pattern here even though it's not strictly necessary, 39 // just so that templated uses of this and the TPU accessor class will be as 40 // similar as possible. 41 class ScopedRef { 42 public: ScopedRef()43 ScopedRef() {} ~ScopedRef()44 ~ScopedRef() {} 45 46 ScopedRef(const ScopedRef&) = delete; 47 ScopedRef& operator=(const ScopedRef&) = delete; 48 49 // Returns the XLA device protected by this ScopedRef. client()50 xla::LocalClient* client() const { return client_; } backend()51 xla::Backend* backend() { return client_->mutable_backend(); } device_ordinal()52 int device_ordinal() const { return ordinal_; } 53 54 private: 55 // XRTGenericDeviceAccessor::InitScopedRef is the only way to initialize 56 // ScopedRef. 57 friend class XRTGenericDeviceAccessor; 58 Acquire(xla::LocalClient * client,int ordinal)59 void Acquire(xla::LocalClient* client, int ordinal) { 60 client_ = client; 61 ordinal_ = ordinal; 62 } 63 64 xla::LocalClient* client_ = nullptr; 65 int ordinal_ = 0; 66 }; 67 68 static Status InitScopedRef(OpKernelContext* ctx, int device_ordinal, 69 ScopedRef* scoped_ref); 70 71 static Status InitScopedRef(OpKernelContext* ctx, ScopedRef* scoped_ref); 72 }; 73 74 } // namespace tensorflow 75 76 #endif // TENSORFLOW_COMPILER_XRT_XRT_DEVICE_H_ 77