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 :
36 : // 为ReduceScatterNHR1D实现的CCUIns、CCuCtxArg与CCUTaskArg
37 : class CcuCtxArgReduceScatterNHR1D : public CcuCtxArg {
38 : public:
39 0 : explicit CcuCtxArgReduceScatterNHR1D(
40 : const std::vector<uint64_t>& dimSize, uint32_t rankId, uint32_t axisId,
41 : const std::vector<NHRStepInfo> stepInfoVector, const std::map<u32, u32> indexMap, const CollAlgOperator& op,
42 : const std::vector<std::vector<RankId>>& tempVTopo, uint32_t linkNum)
43 0 : : dimSize_(dimSize),
44 0 : rankId_(rankId),
45 0 : axisId_(axisId),
46 0 : stepInfoVector_(stepInfoVector),
47 0 : indexMap_(indexMap),
48 0 : op_(op),
49 0 : tempVTopo_(tempVTopo),
50 0 : linkNum_(linkNum)
51 0 : {}
52 0 : CcuCtxSignature GetCtxSignature() const override
53 : {
54 0 : CcuCtxSignature signature;
55 0 : GenerateCcuCtxSignature(signature, CcuInstType::CCU_REDUCE_SCATTER_NHR_1D_MEM2MEM, op_, tempVTopo_);
56 : // signature.Append<uint8_t>(uint8_t(op.outputDataType));
57 0 : return signature;
58 0 : }
59 : std::vector<uint64_t> dimSize_; // 记录x轴和y轴的卡数
60 : uint64_t rankId_;
61 : uint64_t axisId_; // 记录自己在哪个轴
62 : std::vector<NHRStepInfo> stepInfoVector_; // nhr每一步的信息(发送/接受给谁,发/收哪片数据)
63 : std::map<u32, u32> indexMap_; // 因为想要收发连续的数据,所以会把原rank映射一个虚拟的rank
64 : CollAlgOperator op_;
65 : std::vector<std::vector<RankId>> tempVTopo_;
66 : uint32_t linkNum_; // 因为当前不支持双die,用此变量确认单die还是双die
67 : };
68 :
69 : class CcuTaskArgReduceScatterNHR1D : public CcuTaskArg {
70 : public:
71 0 : explicit CcuTaskArgReduceScatterNHR1D(
72 : uint64_t inputAddr, uint64_t outputAddr, uint64_t token, uint64_t die0Size, uint64_t die1Size,
73 : uint64_t inputSliceStride, uint64_t outputSliceStride, uint64_t inputRepeatStride, uint64_t outputRepeatStride,
74 : uint64_t repeatNum, uint64_t isBottom)
75 0 : : inputAddr_(inputAddr),
76 0 : outputAddr_(outputAddr),
77 0 : token_(token),
78 0 : die0Size_(die0Size),
79 0 : die1Size_(die1Size),
80 0 : inputSliceStride_(inputSliceStride),
81 0 : outputSliceStride_(outputSliceStride),
82 0 : inputRepeatStride_(inputRepeatStride),
83 0 : outputRepeatStride_(outputRepeatStride),
84 0 : repeatNum_(repeatNum),
85 0 : isBottom_(isBottom)
86 0 : {}
87 :
88 : uint64_t inputAddr_;
89 : uint64_t outputAddr_;
90 : uint64_t token_;
91 : uint64_t die0Size_;
92 : uint64_t die1Size_;
93 : uint64_t inputSliceStride_;
94 : uint64_t outputSliceStride_;
95 : uint64_t inputRepeatStride_;
96 : uint64_t outputRepeatStride_;
97 : uint64_t repeatNum_;
98 : uint64_t isBottom_;
99 : };
100 :
101 : class CcuInstructionReduceScatterNHR1D : public CcuInstruction {
102 : public:
103 0 : CcuInstructionReduceScatterNHR1D() : CcuInstruction() {}
104 :
105 0 : void Init(
106 : uint32_t rankId, uint64_t inputAddr, uint64_t outputAddr, uint64_t axisId, uint64_t die0Size, uint64_t die1Size,
107 : uint64_t repeatNum, uint64_t inputSliceStride, uint64_t outputSliceStride, uint64_t inputRepeatStride,
108 : uint64_t outputRepeatStride, std::vector<NHRStepInfo> stepInfoVector, std::map<u32, u32> indexMap,
109 : uint64_t token, CollAlgOperator& op, std::vector<std::vector<RankId>>& tempVTopo, u32 linkNum, bool isBottom)
110 : {
111 0 : u32 maxDimNum = 1;
112 0 : if (tempVTopo.size() != maxDimNum) {
113 0 : THROW<InvalidParamsException>(StringFormat(
114 : "[CcuInstructionReduceScatterNHR1D] tempVTopo size is not 1, size is [%zu].", tempVTopo.size()));
115 : }
116 0 : dimSize_.push_back(tempVTopo[0].size());
117 0 : rankId_ = rankId;
118 0 : inputAddr_ = inputAddr;
119 0 : outputAddr_ = outputAddr;
120 0 : axisId_ = axisId;
121 0 : die0Size_ = die0Size;
122 0 : die1Size_ = die1Size;
123 0 : repeatNum_ = repeatNum;
124 0 : inputSliceStride_ = inputSliceStride;
125 0 : outputSliceStride_ = outputSliceStride;
126 0 : inputRepeatStride_ = inputRepeatStride;
127 0 : outputRepeatStride_ = outputRepeatStride;
128 0 : stepInfoVector_ = stepInfoVector;
129 0 : indexMap_ = indexMap;
130 0 : token_ = token;
131 0 : op_ = op;
132 0 : tempVTopo_ = tempVTopo;
133 0 : linkNum_ = linkNum;
134 0 : isBottom_ = isBottom;
135 0 : return;
136 : }
137 :
138 0 : std::string Describe() const override
139 : {
140 : return StringFormat(
141 0 : "CcuInstructionReduceScatterNHR1D rankId [%u], instType[%s]", rankId_, instType_.Describe().c_str());
142 : }
143 :
144 0 : CcuInstType GetInstType() const override
145 : {
146 0 : HCCL_INFO("CcuInstructionReduceScatterNHR1D instype is CCU_REDUCE_SCATTER_NHR_1D_MEM2MEM.");
147 0 : return instType_;
148 : }
149 :
150 : void SetInstType(CcuInstType instType) { instType_ = instType; }
151 :
152 0 : std::unique_ptr<CcuCtxArg> GetCtxArg() const override
153 : {
154 0 : return std::make_unique<CcuCtxArgReduceScatterNHR1D>(
155 0 : dimSize_, rankId_, axisId_, stepInfoVector_, indexMap_, op_, tempVTopo_, linkNum_);
156 : }
157 :
158 0 : std::unique_ptr<CcuTaskArg> GetTaskArg() const override
159 : {
160 0 : return std::make_unique<CcuTaskArgReduceScatterNHR1D>(
161 0 : inputAddr_, outputAddr_, token_, die0Size_, die1Size_, inputSliceStride_, outputSliceStride_,
162 0 : inputRepeatStride_, outputRepeatStride_, repeatNum_, isBottom_);
163 : }
164 :
165 : private:
166 : CcuInstType instType_ = CcuInstType::CCU_REDUCE_SCATTER_NHR_1D_MEM2MEM;
167 : std::vector<uint64_t> dimSize_;
168 : uint32_t rankId_{0};
169 : uint32_t axisId_{0};
170 : uint64_t inputAddr_{0};
171 : uint64_t outputAddr_{0};
172 : uint64_t die0Size_{0};
173 : uint64_t die1Size_{0};
174 : uint64_t repeatNum_{0};
175 : uint64_t inputSliceStride_{0};
176 : uint64_t outputSliceStride_{0};
177 : uint64_t inputRepeatStride_{0};
178 : uint64_t outputRepeatStride_{0};
179 : uint64_t isBottom_{0};
180 : std::vector<NHRStepInfo> stepInfoVector_;
181 : std::map<u32, u32> indexMap_;
182 : uint64_t token_{0};
183 : CollAlgOperator op_;
184 : std::vector<std::vector<RankId>> tempVTopo_;
185 : uint32_t linkNum_{0};
186 : };
187 : } // namespace Hccl
188 :
189 : #endif // HCCLV2_CCU_INSTRUCTION_REDUCE_SCATTER_NHR_1D_H_
|