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 #include "tensorflow/compiler/xla/pjrt/cpu_device.h"
17
18 #include "absl/strings/str_cat.h"
19 #include "tensorflow/compiler/xla/client/client_library.h"
20 #include "tensorflow/compiler/xla/pjrt/pjrt_stream_executor_client.h"
21 #include "tensorflow/compiler/xla/service/platform_util.h"
22
23 namespace xla {
24
25 static const char kCpuPlatformName[] = "cpu";
26
CpuDevice(int id,std::unique_ptr<LocalDeviceState> local_device_state)27 CpuDevice::CpuDevice(int id,
28 std::unique_ptr<LocalDeviceState> local_device_state)
29 : PjRtStreamExecutorDevice(id, std::move(local_device_state),
30 /*device_kind=*/kCpuPlatformName) {}
31
GetCpuClient(bool asynchronous)32 StatusOr<std::unique_ptr<PjRtClient>> GetCpuClient(bool asynchronous) {
33 TF_ASSIGN_OR_RETURN(se::Platform * platform,
34 PlatformUtil::GetPlatform("Host"));
35 if (platform->VisibleDeviceCount() <= 0) {
36 return FailedPrecondition("CPU platform has no visible devices.");
37 }
38 LocalClientOptions options;
39 options.set_platform(platform);
40 TF_ASSIGN_OR_RETURN(LocalClient * client,
41 ClientLibrary::GetOrCreateLocalClient(options));
42
43 std::vector<std::unique_ptr<PjRtStreamExecutorDevice>> devices;
44 for (int i = 0; i < client->device_count(); ++i) {
45 se::StreamExecutorConfig config;
46 config.ordinal = i;
47 // 8MiB stacks seem to be necessary for running LAPACK/OpenBLAS
48 // computations.
49 config.device_options.non_portable_tags["host_thread_stack_size_in_bytes"] =
50 absl::StrCat(8192 * 1024);
51 TF_ASSIGN_OR_RETURN(se::StreamExecutor * executor,
52 platform->GetExecutor(config));
53 auto device_state = absl::make_unique<LocalDeviceState>(
54 executor, client, LocalDeviceState::kSynchronous, asynchronous,
55 /*allow_event_reuse=*/false);
56 auto device = absl::make_unique<CpuDevice>(i, std::move(device_state));
57 devices.push_back(std::move(device));
58 }
59
60 return std::unique_ptr<PjRtClient>(std::make_unique<PjRtStreamExecutorClient>(
61 kCpuName, client, std::move(devices), /*task_id=*/0,
62 /*allocator=*/nullptr, /*host_memory_allocator=*/nullptr,
63 /*should_stage_host_to_device_transfers=*/false,
64 /*gpu_run_options=*/nullptr));
65 }
66
67 } // namespace xla
68