xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/nccl_all_to_all_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_TO_ALL_THUNK_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_NCCL_ALL_TO_ALL_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 NcclAllToAllConfig {
30   NcclCollectiveConfig config;
31   bool has_split_dimension;
32 };
33 
34 // Thunk that performs a NCCL-based All-to-All among CUDA GPU-based replicas.
35 class NcclAllToAllThunk : public NcclCollectiveThunk {
36  public:
37   NcclAllToAllThunk(ThunkInfo thunk_info, mlir::lmhlo::AllToAllOp op,
38                     std::vector<Buffer> buffers);
39 
40   // Returns whether the given instruction can be lowered to a nccl all-to-all
41   // call.
42   static bool CanImplement(mlir::lmhlo::AllToAllOp op);
43 
GetName()44   static const char* GetName() { return "AllToAll"; }
IsDegenerate(mlir::lmhlo::AllToAllOp op,int64_t replica_count,int64_t partition_count)45   static bool IsDegenerate(mlir::lmhlo::AllToAllOp op, int64_t replica_count,
46                            int64_t partition_count) {
47     return GetNcclAllToAllConfig(op).config.IsDegenerate(replica_count,
48                                                          partition_count);
49   }
50 
GetGroupMode(mlir::lmhlo::AllToAllOp op)51   static CollectiveOpGroupMode GetGroupMode(mlir::lmhlo::AllToAllOp op) {
52     return GetNcclAllToAllConfig(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 NcclAllToAllConfig GetNcclAllToAllConfig(mlir::lmhlo::AllToAllOp op);
63 
64   const NcclAllToAllConfig config_;
65   const std::vector<Buffer> buffers_;
66 };
67 
68 Status RunAllToAll(bool has_split_dimension,
69                    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_TO_ALL_THUNK_H_
76