xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/tfrt/utils/host_context.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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/compiler/mlir/tfrt/utils/host_context.h"
17 
18 #include <memory>
19 
20 #include "tensorflow/core/platform/logging.h"
21 #include "tfrt/host_context/concurrent_work_queue.h"  // from @tf_runtime
22 #include "tfrt/host_context/diagnostic.h"  // from @tf_runtime
23 #include "tfrt/host_context/host_allocator.h"  // from @tf_runtime
24 #include "tfrt/host_context/host_context.h"  // from @tf_runtime
25 
26 namespace tensorflow {
27 
28 using ::tfrt::HostContext;
29 
30 const char* const kDefaultHostDeviceName =
31     "/job:localhost/replica:0/task:0/device:CPU:0";
32 
CreateSingleThreadedHostContext()33 std::unique_ptr<HostContext> CreateSingleThreadedHostContext() {
34   return std::make_unique<HostContext>(
35       [](const tfrt::DecodedDiagnostic& diag) {
36         LOG(FATAL) << "Runtime error: " << diag.message << "\n";
37       },
38       tfrt::CreateMallocAllocator(), tfrt::CreateSingleThreadedWorkQueue(),
39       kDefaultHostDeviceName);
40 }
41 
CreateMultiThreadedHostContext(int64_t num_threads)42 std::unique_ptr<HostContext> CreateMultiThreadedHostContext(
43     int64_t num_threads) {
44   return std::make_unique<HostContext>(
45       [](const tfrt::DecodedDiagnostic& diag) {
46         LOG(FATAL) << "Runtime error: " << diag.message << "\n";
47       },
48       tfrt::CreateMallocAllocator(),
49       tfrt::CreateMultiThreadedWorkQueue(num_threads,
50                                          /*num_blocking_threads=*/1),
51       kDefaultHostDeviceName);
52 }
53 
54 }  // namespace tensorflow
55