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 #include "tensorflow/core/tfrt/common/global_state.h" 16 17 #include <utility> 18 19 #include "absl/memory/memory.h" 20 #include "tensorflow/core/platform/mutex.h" 21 #include "tfrt/host_context/concurrent_work_queue.h" // from @tf_runtime 22 #include "tfrt/host_context/host_allocator.h" // from @tf_runtime 23 #include "tfrt/host_context/host_context.h" // from @tf_runtime 24 25 namespace tensorflow { 26 namespace tfrt_global { 27 namespace { 28 GetStaticHostContext()29tfrt::HostContext* GetStaticHostContext() { 30 static ::tfrt::HostContext* host_context = [] { 31 // Create host context. 32 auto decoded_diagnostic_handler = 33 [&](const ::tfrt::DecodedDiagnostic& diag) { abort(); }; 34 std::unique_ptr<::tfrt::ConcurrentWorkQueue> work_queue = 35 ::tfrt::CreateMultiThreadedWorkQueue(/*num_threads=*/4, 36 /*num_blocking_threads=*/64); 37 std::unique_ptr<::tfrt::HostAllocator> host_allocator = 38 ::tfrt::CreateMallocAllocator(); 39 return new ::tfrt::HostContext(decoded_diagnostic_handler, 40 std::move(host_allocator), 41 std::move(work_queue)); 42 }(); 43 return host_context; 44 } 45 46 } // namespace 47 48 /*static*/ ::tfrt::HostContext* GlobalHostContext::host_ctx_ = nullptr; 49 Set(::tfrt::HostContext * host_ctx)50/*static*/ void GlobalHostContext::Set(::tfrt::HostContext* host_ctx) { 51 host_ctx_ = host_ctx; 52 } 53 Get()54/*static*/ ::tfrt::HostContext* GlobalHostContext::Get() { 55 // If HostContext is explicitly injected at context creation, use it here. 56 if (host_ctx_) return host_ctx_; 57 58 // Otherwise we assume it is running TFRT TF OpKernels, and currently it is 59 // implicitly created. 60 return GetStaticHostContext(); 61 } 62 63 } // namespace tfrt_global 64 } // namespace tensorflow 65