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_INS_ALL_REDUCE_PARALLEL_EXECUTOR_OPT_H
12 : #define HCCLV2_INS_ALL_REDUCE_PARALLEL_EXECUTOR_OPT_H
13 : #include "ins_coll_alg_base.h"
14 :
15 : namespace Hccl {
16 :
17 :
18 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1, typename InsAlgTemplate2, typename InsAlgTemplate3>
19 : class InsAllReduceParallelExecutorV2 : public InsCollAlgBase {
20 : public:
21 : explicit InsAllReduceParallelExecutorV2();
22 : ~InsAllReduceParallelExecutorV2() override;
23 :
24 0 : std::string Describe() const override
25 : {
26 0 : return "Instruction based All Reduce Parallel Executor.";
27 : }
28 :
29 : // HOST 接口
30 : HcclResult Orchestrate(const RankGraph *rankGraph, const CollAlgOperator &op, const CollAlgParams ¶ms,
31 : InsQuePtr insQue) override;
32 : // AICPU 接口
33 : HcclResult Orchestrate(const AlgTopoInfo &topoInfo, const CollAlgOperator &op, const CollAlgParams ¶ms,
34 : ConnectedLinkMgr *linkMgr, InsQuePtr insQue) override;
35 :
36 : HcclResult CalcResOffload(const RankGraph *rankGraph, const u64 &dataSize,
37 : CollOffloadOpResReq &resReq) override;
38 :
39 : HcclResult CalcRes(const RankGraph *rankGraph, CollAlgResReq &algResReq) override;
40 :
41 : private:
42 : HcclResult CalcLocalRankSize();
43 : HcclResult GenInsQues(InsAlgTemplate0 &tempAlgIntraRS, InsAlgTemplate1 &tempAlgInterRS, InsAlgTemplate2 &tempAlgIntraAG, InsAlgTemplate3 &tempAlgInterAG);
44 : void GetParallelDataSplitRate(std::vector<float> &splitDataSize) const;
45 : HcclResult PrepareResForTemplate(const RankGraph *rankGraph, InsAlgTemplate0 &tempAlgIntraRS, InsAlgTemplate1 &tempAlgInterRS, InsAlgTemplate2 &tempAlgIntraAG, InsAlgTemplate3 &tempAlgInterAG);
46 : HcclResult PrepareResForTemplate(ConnectedLinkMgr *linkMgr, InsAlgTemplate0 &tempAlgIntraRS, InsAlgTemplate1 &tempAlgInterRS, InsAlgTemplate2 &tempAlgIntraAG, InsAlgTemplate3 &tempAlgInterAG);
47 :
48 : void GenRSIntraParams0(const u64 dataOffset, const u64 dataCount,
49 : const u64 scratchOff, TemplateDataParams ¶ms) const;
50 :
51 : void GenRSInterParams0(const u64 dataOffset, const u64 dataCount,
52 : const u64 scratchOff, TemplateDataParams ¶ms) const;
53 :
54 : void GenAGInterParams0(const u64 dataOffset, const u64 dataCount,
55 : const u64 scratchOff, TemplateDataParams ¶ms) const;
56 :
57 : void GenAGIntraParams0(const u64 dataOffset, const u64 dataCount,
58 : const u64 scratchOff, TemplateDataParams ¶ms) const;
59 :
60 : void GenRSInterParams1(const u64 dataOffset, const u64 dataCount,
61 : const u64 scratchOff, TemplateDataParams ¶ms) const;
62 :
63 : void GenRSIntraParams1(const u64 dataOffset, const u64 dataCount,
64 : const u64 scratchOff, TemplateDataParams ¶ms) const;
65 :
66 : void GenAGIntraParams1(const u64 dataOffset, const u64 dataCount,
67 : const u64 scratchOff, TemplateDataParams ¶ms) const;
68 :
69 : void GenAGInterParams1(const u64 dataOffset, const u64 dataCount,
70 : const u64 scratchOff, TemplateDataParams ¶ms) const;
71 :
72 0 : inline void InitAlgCommonParams(
73 : InsAlgTemplate0& tempAlgIntraRS,
74 : InsAlgTemplate1& tempAlgInterRS,
75 : InsAlgTemplate2& tempAlgIntraAG,
76 : InsAlgTemplate3& tempAlgInterAG,
77 : const CollAlgOperator& op) const
78 : {
79 0 : tempAlgIntraRS.SetDmaMode(dmaMode_);
80 0 : tempAlgIntraRS.InitReduceInfo(redOp_, dataType_);
81 0 : tempAlgIntraRS.SetCollOp(op);
82 :
83 0 : tempAlgInterRS.SetDmaMode(dmaMode_);
84 0 : tempAlgInterRS.InitReduceInfo(redOp_, dataType_);
85 0 : tempAlgInterRS.SetCollOp(op);
86 :
87 0 : tempAlgIntraAG.SetDmaMode(dmaMode_);
88 0 : tempAlgIntraAG.SetCollOp(op);
89 0 : tempAlgIntraAG.SetDataType(dataType_);
90 :
91 0 : tempAlgInterAG.SetDmaMode(dmaMode_);
92 0 : tempAlgInterAG.SetCollOp(op);
93 0 : tempAlgInterAG.SetDataType(dataType_);
94 0 : }
95 :
96 : // 统一设置 TemplateDataParams 的公共字段
97 0 : inline void SetTemplateDataParams(
98 : TemplateDataParams ¶ms,
99 : BufferType inBuffType, BufferType outBuffType,
100 : u64 inBuffBaseOff, u64 outBuffBaseOff, u64 scratchBuffBaseOff,
101 : u64 sliceSize, u64 inputSliceStride, u64 outputSliceStride,
102 : u32 repeatNum, u64 inputRepeatStride, u64 outputRepeatStride,
103 : u64 tailSize) const
104 : {
105 0 : params.buffInfo.inBuffType = inBuffType;
106 0 : params.buffInfo.outBuffType = outBuffType;
107 0 : params.buffInfo.scratBuffType = BufferType::SCRATCH;
108 0 : params.buffInfo.inBuffBaseOff = inBuffBaseOff;
109 0 : params.buffInfo.outBuffBaseOff = outBuffBaseOff;
110 0 : params.buffInfo.scratchBuffBaseOff = scratchBuffBaseOff;
111 0 : params.sliceSize = sliceSize;
112 0 : params.inputSliceStride = inputSliceStride;
113 0 : params.outputSliceStride = outputSliceStride;
114 0 : params.repeatNum = repeatNum;
115 0 : params.inputRepeatStride = inputRepeatStride;
116 0 : params.outputRepeatStride = outputRepeatStride;
117 0 : params.tailSize = tailSize;
118 0 : }
119 :
120 : // 计算 Gen*Params1 中 intra 函数的公共 dataCountTmp
121 0 : inline u64 CalcDataCountTmp1(u64 dataCount) const
122 : {
123 0 : return (rankIdxLevel1_ != rankSizeLevel1_ - 1)
124 0 : ? dataCount / rankSize_ * rankSizeLevel0_
125 0 : : dataCount - dataCount / rankSize_ * rankSizeLevel0_ * (rankSizeLevel1_ - 1);
126 : }
127 :
128 0 : inline HcclResult CalcQue(AlgTempResReq &resReqIntraRS, AlgTempResReq &resReqInterRS,
129 : AlgTempResReq &resReqIntraAG, AlgTempResReq &resReqInterAG)
130 : {
131 : // 申请算法模板所需资源
132 0 : if(!(resReqIntraRS.queNum > 0 && resReqInterRS.queNum > 0 && resReqIntraAG.queNum > 0 && resReqInterAG.queNum > 0)) {
133 0 : HCCL_ERROR("resReqIntra.queNum and resReqInter.queNum must larger than 0.");
134 0 : return HcclResult::HCCL_E_INTERNAL;
135 : }
136 0 : u32 intraQueNum = std::max(resReqIntraRS.queNum, resReqIntraAG.queNum);
137 0 : u32 interQueNum = std::max(resReqInterRS.queNum, resReqInterAG.queNum);
138 0 : u32 totalQueNum = intraQueNum + interQueNum;
139 0 : CHK_RET(InitQueue(totalQueNum, requiredQue_));
140 0 : for(u32 i = 0 ; i < requiredQue_.size(); i++) {
141 0 : if (i < intraQueNum) {
142 0 : intraQue_.push_back(requiredQue_[i]);
143 : } else {
144 0 : interQue_.push_back(requiredQue_[i]);
145 : }
146 : }
147 0 : HCCL_INFO("LGC requiredQue_.size is [%llu]. intraQue_.size is [%llu]. interQue_.size is [%llu].",
148 : requiredQue_.size(), intraQue_.size(), interQue_.size());
149 0 : syncQueues_.emplace_back(intraQue_[0]);
150 0 : syncQueues_.emplace_back(interQue_[0]);
151 0 : return HCCL_SUCCESS;
152 : }
153 :
154 : uint64_t rankSizeLevel0_{0};
155 : uint64_t rankSizeLevel1_{0};
156 : uint64_t rankSize_{0};
157 :
158 : uint64_t rankIdxLevel0_{0};
159 : uint64_t rankIdxLevel1_{0};
160 :
161 : u64 sliceCount_;
162 :
163 : std::vector<std::vector<RankId>> virtRanks_;
164 : std::vector<std::map<RankId, u32>> virtRankMap_; // map<virtRank, virtRankOrder>
165 : std::vector<std::vector<std::vector<RankId>>> vTopo_;
166 :
167 : std::vector<InsQuePtr> requiredQue_;
168 : std::vector<InsQuePtr> intraQue_;
169 : std::vector<InsQuePtr> interQue_;
170 : std::vector<InsQuePtr> syncQueues_;
171 :
172 : ResLinks intraRSLinks_;
173 : ResLinks interRSLinks_;
174 : ResLinks intraAGLinks_;
175 : ResLinks interAGLinks_;
176 : };
177 :
178 : } // namespace Hccl
179 :
180 : #endif // HCCLV2_INS_ALL_REDUCE_PARALLEL_EXECUTOR_H
|