Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef HCCLV2_CCU_INSTRUCTION_REDUCE_SCATTER_NHR_1D_H_
12 : #define HCCLV2_CCU_INSTRUCTION_REDUCE_SCATTER_NHR_1D_H_
13 :
14 : #include "template_utils.h"
15 : #include "instruction.h"
16 : #include "ins_queue.h"
17 : #include "ccu_context_utils.h"
18 : #include "ccu_ctx_signature.h"
19 : #include "ccu_ins.h"
20 : #include "ccu_rank_group.h"
21 :
22 : namespace Hccl {
23 :
24 : using NHRStepInfo = struct NHRStepInfo {
25 : u32 step = 0;
26 : u32 myRank = 0;
27 : u32 nSlices;
28 : u32 toRank = 0;
29 : u32 fromRank = 0;
30 : std::vector<u32> txSliceIdxs;
31 : std::vector<u32> rxSliceIdxs;
32 :
33 0 : NHRStepInfo() : nSlices(0)
34 : {
35 0 : }
36 : };
37 :
38 : // 为ReduceScatterNHR1D实现的CCUIns、CCuCtxArg与CCUTaskArg
39 : class CcuCtxArgReduceScatterNHR1D : public CcuCtxArg {
40 : public:
41 0 : explicit CcuCtxArgReduceScatterNHR1D(const std::vector<uint64_t> &dimSize, uint32_t rankId, uint32_t axisId,
42 : const std::vector<NHRStepInfo> stepInfoVector, const std::map<u32, u32> indexMap,
43 : const CollAlgOperator &op, const std::vector<std::vector<RankId>> &tempVTopo, uint32_t linkNum)
44 0 : : dimSize_(dimSize), rankId_(rankId), axisId_(axisId), stepInfoVector_(stepInfoVector),
45 0 : indexMap_(indexMap), op_(op), tempVTopo_(tempVTopo), linkNum_(linkNum)
46 : {
47 0 : }
48 0 : CcuCtxSignature GetCtxSignature() const override
49 : {
50 0 : CcuCtxSignature signature;
51 0 : GenerateCcuCtxSignature(signature, CcuInstType::CCU_REDUCE_SCATTER_NHR_1D_MEM2MEM, op_, tempVTopo_);
52 : // signature.Append<uint8_t>(uint8_t(op.outputDataType));
53 0 : return signature;
54 0 : }
55 : std::vector<uint64_t> dimSize_; // 记录x轴和y轴的卡数
56 : uint64_t rankId_;
57 : uint64_t axisId_; // 记录自己在哪个轴
58 : std::vector<NHRStepInfo> stepInfoVector_; // nhr每一步的信息(发送/接受给谁,发/收哪片数据)
59 : std::map<u32, u32> indexMap_; // 因为想要收发连续的数据,所以会把原rank映射一个虚拟的rank
60 : CollAlgOperator op_;
61 : std::vector<std::vector<RankId>> tempVTopo_;
62 : uint32_t linkNum_; // 因为当前不支持双die,用此变量确认单die还是双die
63 : };
64 :
65 : class CcuTaskArgReduceScatterNHR1D : public CcuTaskArg {
66 : public:
67 0 : explicit CcuTaskArgReduceScatterNHR1D(uint64_t inputAddr, uint64_t outputAddr, uint64_t token, uint64_t die0Size,
68 : uint64_t die1Size, uint64_t inputSliceStride, uint64_t outputSliceStride,
69 : uint64_t inputRepeatStride, uint64_t outputRepeatStride, uint64_t repeatNum, uint64_t isBottom)
70 0 : : inputAddr_(inputAddr), outputAddr_(outputAddr),
71 0 : token_(token), die0Size_(die0Size), die1Size_(die1Size), inputSliceStride_(inputSliceStride),
72 0 : outputSliceStride_(outputSliceStride), inputRepeatStride_(inputRepeatStride),
73 0 : outputRepeatStride_(outputRepeatStride), repeatNum_(repeatNum), isBottom_(isBottom)
74 : {
75 0 : }
76 :
77 : uint64_t inputAddr_;
78 : uint64_t outputAddr_;
79 : uint64_t token_;
80 : uint64_t die0Size_;
81 : uint64_t die1Size_;
82 : uint64_t inputSliceStride_;
83 : uint64_t outputSliceStride_;
84 : uint64_t inputRepeatStride_;
85 : uint64_t outputRepeatStride_;
86 : uint64_t repeatNum_;
87 : uint64_t isBottom_;
88 : };
89 :
90 : class CcuInstructionReduceScatterNHR1D : public CcuInstruction {
91 : public:
92 0 : CcuInstructionReduceScatterNHR1D() : CcuInstruction()
93 : {
94 0 : }
95 :
96 0 : void Init(uint32_t rankId, uint64_t inputAddr, uint64_t outputAddr,
97 : uint64_t axisId, uint64_t die0Size, uint64_t die1Size, uint64_t repeatNum, uint64_t inputSliceStride,
98 : uint64_t outputSliceStride, uint64_t inputRepeatStride, uint64_t outputRepeatStride,
99 : std::vector<NHRStepInfo> stepInfoVector, std::map<u32, u32> indexMap, uint64_t token, CollAlgOperator &op,
100 : std::vector<std::vector<RankId>> &tempVTopo, u32 linkNum, bool isBottom)
101 : {
102 0 : u32 maxDimNum = 1;
103 0 : if (tempVTopo.size() != maxDimNum) {
104 0 : THROW<InvalidParamsException>(StringFormat(
105 : "[CcuInstructionReduceScatterNHR1D] tempVTopo size is not 1, size is [%zu].", tempVTopo.size()));
106 : }
107 0 : dimSize_.push_back(tempVTopo[0].size());
108 0 : rankId_ = rankId;
109 0 : inputAddr_ = inputAddr;
110 0 : outputAddr_ = outputAddr;
111 0 : axisId_ = axisId;
112 0 : die0Size_ = die0Size;
113 0 : die1Size_ = die1Size;
114 0 : repeatNum_ = repeatNum;
115 0 : inputSliceStride_ = inputSliceStride;
116 0 : outputSliceStride_ = outputSliceStride;
117 0 : inputRepeatStride_ = inputRepeatStride;
118 0 : outputRepeatStride_ = outputRepeatStride;
119 0 : stepInfoVector_ = stepInfoVector;
120 0 : indexMap_ = indexMap;
121 0 : token_ = token;
122 0 : op_ = op;
123 0 : tempVTopo_ = tempVTopo;
124 0 : linkNum_ = linkNum;
125 0 : isBottom_ = isBottom;
126 0 : return;
127 : }
128 :
129 0 : std::string Describe() const override
130 : {
131 0 : return StringFormat("CcuInstructionReduceScatterNHR1D rankId [%u], instType[%s]", rankId_,
132 0 : instType_.Describe().c_str());
133 : }
134 :
135 0 : CcuInstType GetInstType() const override
136 : {
137 0 : HCCL_INFO("CcuInstructionReduceScatterNHR1D instype is CCU_REDUCE_SCATTER_NHR_1D_MEM2MEM.");
138 0 : return instType_;
139 : }
140 :
141 : void SetInstType(CcuInstType instType)
142 : {
143 : instType_ = instType;
144 : }
145 :
146 0 : std::unique_ptr<CcuCtxArg> GetCtxArg() const override
147 : {
148 0 : return std::make_unique<CcuCtxArgReduceScatterNHR1D>(dimSize_, rankId_, axisId_, stepInfoVector_,
149 0 : indexMap_, op_, tempVTopo_, linkNum_);
150 : }
151 :
152 0 : std::unique_ptr<CcuTaskArg> GetTaskArg() const override
153 : {
154 0 : return std::make_unique<CcuTaskArgReduceScatterNHR1D>(inputAddr_, outputAddr_,
155 0 : token_, die0Size_, die1Size_, inputSliceStride_, outputSliceStride_,
156 0 : inputRepeatStride_, outputRepeatStride_, repeatNum_, isBottom_);
157 : }
158 :
159 : private:
160 : CcuInstType instType_ = CcuInstType::CCU_REDUCE_SCATTER_NHR_1D_MEM2MEM;
161 : std::vector<uint64_t> dimSize_;
162 : uint32_t rankId_{0};
163 : uint32_t axisId_{0};
164 : uint64_t inputAddr_{0};
165 : uint64_t outputAddr_{0};
166 : uint64_t die0Size_{0};
167 : uint64_t die1Size_{0};
168 : uint64_t repeatNum_{0};
169 : uint64_t inputSliceStride_{0};
170 : uint64_t outputSliceStride_{0};
171 : uint64_t inputRepeatStride_{0};
172 : uint64_t outputRepeatStride_{0};
173 : uint64_t isBottom_{0};
174 : std::vector<NHRStepInfo> stepInfoVector_;
175 : std::map<u32, u32> indexMap_;
176 : uint64_t token_{0};
177 : CollAlgOperator op_;
178 : std::vector<std::vector<RankId>> tempVTopo_;
179 : uint32_t linkNum_{0};
180 : };
181 : } // namespace Hccl
182 :
183 : #endif // HCCLV2_CCU_INSTRUCTION_REDUCE_SCATTER_NHR_1D_H_
|