xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/nccl_all_gather_thunk.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_NCCL_ALL_GATHER_THUNK_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_NCCL_ALL_GATHER_THUNK_H_
18 
19 #include "tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/lhlo/IR/lhlo_ops.h"
20 #include "tensorflow/compiler/xla/service/collective_ops_utils.h"
21 #include "tensorflow/compiler/xla/service/gpu/buffer_allocations.h"
22 #include "tensorflow/compiler/xla/service/gpu/nccl_collective_thunk.h"
23 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
24 #include "tensorflow/compiler/xla/xla_data.pb.h"
25 
26 namespace xla {
27 namespace gpu {
28 
29 struct NcclAllGatherConfig {
30   NcclCollectiveConfig config;
31 };
32 
33 // Thunk that performs a NCCL-based All-Gather among CUDA GPU-based replicas.
34 class NcclAllGatherThunk : public NcclCollectiveThunk {
35  public:
36   NcclAllGatherThunk(ThunkInfo thunk_info, mlir::lmhlo::AllGatherOp op,
37                      std::vector<Buffer> buffers);
38 
39   // Returns whether the given instruction can be lowered to a nccl all-gather
40   // call.
41   static bool CanImplement(mlir::lmhlo::AllGatherOp op);
42 
GetName()43   static const char* GetName() { return "AllGather"; }
44 
IsDegenerate(mlir::lmhlo::AllGatherOp op,int64_t replica_count,int64_t partition_count)45   static bool IsDegenerate(mlir::lmhlo::AllGatherOp op, int64_t replica_count,
46                            int64_t partition_count) {
47     return GetNcclAllGatherConfig(op).config.IsDegenerate(replica_count,
48                                                           partition_count);
49   }
50 
GetGroupMode(mlir::lmhlo::AllGatherOp op)51   static CollectiveOpGroupMode GetGroupMode(mlir::lmhlo::AllGatherOp op) {
52     return GetNcclAllGatherConfig(op).config.group_mode;
53   }
54 
55  protected:
56   Status RunNcclCollective(const ExecuteParams& params,
57                            ncclComm_t comm) override;
58 
config()59   const NcclCollectiveConfig& config() const override { return config_.config; }
60 
61  private:
62   static NcclAllGatherConfig GetNcclAllGatherConfig(
63       mlir::lmhlo::AllGatherOp op);
64 
65   const NcclAllGatherConfig config_;
66   const std::vector<Buffer> buffers_;
67 };
68 
69 Status RunAllGather(std::vector<DeviceBufferPair>& buffers, se::Stream& stream,
70                     ncclComm_t comm);
71 
72 }  // namespace gpu
73 }  // namespace xla
74 
75 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_NCCL_ALL_GATHER_THUNK_H_
76