xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/gpu_executable_run_options.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/service/gpu/gpu_executable_run_options.h"
17 
18 #include "absl/algorithm/container.h"
19 #include "tensorflow/compiler/xla/status_macros.h"
20 
21 namespace xla {
22 namespace gpu {
23 
NcclCliqueKey(std::vector<GlobalDeviceId> devices)24 NcclCliqueKey::NcclCliqueKey(std::vector<GlobalDeviceId> devices)
25     : devices_(std::move(devices)) {}
26 
ToString() const27 std::string NcclCliqueKey::ToString() const {
28   return GlobalDeviceIdsToString(devices_);
29 }
30 
set_gpu_global_device_ids(std::optional<std::vector<GlobalDeviceId>> gpu_global_device_ids)31 GpuExecutableRunOptions& GpuExecutableRunOptions::set_gpu_global_device_ids(
32     std::optional<std::vector<GlobalDeviceId>> gpu_global_device_ids) {
33   gpu_global_device_ids_ = std::move(gpu_global_device_ids);
34   return *this;
35 }
36 
37 const std::optional<std::vector<GlobalDeviceId>>&
gpu_global_device_ids() const38 GpuExecutableRunOptions::gpu_global_device_ids() const {
39   return gpu_global_device_ids_;
40 }
41 
set_nccl_unique_id_callback(NcclUniqueIdCallback nccl_unique_id_callback)42 GpuExecutableRunOptions& GpuExecutableRunOptions::set_nccl_unique_id_callback(
43     NcclUniqueIdCallback nccl_unique_id_callback) {
44   nccl_unique_id_callback_ = std::move(nccl_unique_id_callback);
45   return *this;
46 }
47 
nccl_unique_id_callback() const48 const NcclUniqueIdCallback& GpuExecutableRunOptions::nccl_unique_id_callback()
49     const {
50   return nccl_unique_id_callback_;
51 }
52 
NcclExecuteParams(const ServiceExecutableRunOptions & run_options,se::Stream * stream)53 NcclExecuteParams::NcclExecuteParams(
54     const ServiceExecutableRunOptions& run_options, se::Stream* stream)
55     : stream(stream),
56       run_id(run_options.run_options().run_id()),
57       device_assn(run_options.run_options().device_assignment()) {
58   const GpuExecutableRunOptions* gpu_options =
59       run_options.run_options().gpu_executable_run_options();
60   gpu_global_device_ids = gpu_options && gpu_options->gpu_global_device_ids()
61                               ? &*gpu_options->gpu_global_device_ids()
62                               : nullptr;
63   nccl_unique_id_callback =
64       gpu_options && gpu_options->nccl_unique_id_callback()
65           ? &gpu_options->nccl_unique_id_callback()
66           : nullptr;
67 }
68 
GetGlobalDeviceId() const69 StatusOr<GlobalDeviceId> NcclExecuteParams::GetGlobalDeviceId() const {
70   int64_t local_device_ordinal = stream->parent()->device_ordinal();
71   if (gpu_global_device_ids) {
72     TF_RET_CHECK(0 <= local_device_ordinal &&
73                  local_device_ordinal < gpu_global_device_ids->size());
74     return (*gpu_global_device_ids)[local_device_ordinal];
75   } else {
76     // No local -> global mapping was provided; assume the identity mapping.
77     return GlobalDeviceId(local_device_ordinal);
78   }
79 }
80 
81 }  // namespace gpu
82 }  // namespace xla
83