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 : #include "Coll_reduce_scatter_v_mesh_opbase_pipeline_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollReduceScatterVMeshOpbasePipelineExecutor::CollReduceScatterVMeshOpbasePipelineExecutor(
16 : const HcclDispatcher dispatcher,
17 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
18 0 : : CollReduceScatterVExecutor(dispatcher, topoMatcher)
19 : {
20 0 : DMAReduceFlag_ = true;
21 0 : CCLMemSlice_ = false;
22 0 : }
23 :
24 0 : void CollReduceScatterVMeshOpbasePipelineExecutor::ParseParam(const OpParam& param)
25 : {
26 0 : tag_ = param.tag;
27 0 : aicpuUnfoldMode_ = param.aicpuUnfoldMode;
28 0 : }
29 :
30 0 : HcclResult CollReduceScatterVMeshOpbasePipelineExecutor::CalcStreamNum(u32& streamNum)
31 : {
32 0 : u32 totalStreamNum = topoAttr_.deviceNumPerAggregation + 1U;
33 0 : streamNum = totalStreamNum - 1U;
34 0 : HCCL_INFO("[CollReduceScatterVMeshOpbasePipelineExecutor][CalcStreamNum] tag[%s] streamNum[%u]",
35 : tag_.c_str(), streamNum);
36 0 : return HCCL_SUCCESS;
37 : }
38 :
39 0 : HcclResult CollReduceScatterVMeshOpbasePipelineExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
40 : {
41 0 : TransportMemType inputType = TransportMemType::RESERVED;
42 0 : TransportMemType outputType = TransportMemType::RESERVED;
43 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
44 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
45 0 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
46 0 : return HCCL_SUCCESS;
47 : }
48 :
49 0 : HcclResult CollReduceScatterVMeshOpbasePipelineExecutor::CalcLevel0CommInfo(TransportMemType inputType,
50 : TransportMemType outputType,
51 : std::vector<LevelNSubCommTransport>& opTransport)
52 : {
53 0 : CommParaInfo commParaInfo(COMM_LEVEL0, CommType::COMM_TAG_MESH);
54 0 : commParaInfo.meshSinglePlane = true;
55 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaInfo, opTransport[COMM_LEVEL0], inputType, outputType));
56 0 : return HCCL_SUCCESS;
57 0 : }
58 :
59 : // PipeLine模式下使用Ring算法
60 0 : HcclResult CollReduceScatterVMeshOpbasePipelineExecutor::CalcLevel1CommInfo(TransportMemType inputType,
61 : TransportMemType outputType,
62 : std::vector<LevelNSubCommTransport>& opTransport)
63 : {
64 0 : CommParaInfo commParaInfo(COMM_LEVEL1, CommType::COMM_TAG_RING_INNER);
65 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaInfo, opTransport[COMM_LEVEL1], inputType, outputType));
66 0 : return HCCL_SUCCESS;
67 0 : }
68 :
69 0 : HcclResult CollReduceScatterVMeshOpbasePipelineExecutor::CalcTransportMemType(TransportMemType &inputType,
70 : TransportMemType &outputType)
71 : {
72 0 : inputType = TransportMemType::CCL_INPUT;
73 0 : outputType = TransportMemType::CCL_OUTPUT;
74 0 : HCCL_INFO("[CollReduceScatterVMeshOpbasePipelineExecutor][CalcTransportMemType] tag[%s] inputType[%d],"
75 : " outputType[%d]", tag_.c_str(), inputType, outputType);
76 0 : return HCCL_SUCCESS;
77 : }
78 :
79 0 : bool CollReduceScatterVMeshOpbasePipelineExecutor::IsHugeData(const u64 curSize, const OpParam ¶m)
80 : {
81 0 : bool hugeData = curSize > RDMA_SEND_MAX_SIZE || curSize > SDMA_SEND_MAX_SIZE;
82 0 : return hugeData;
83 : }
84 :
85 0 : HcclResult CollReduceScatterVMeshOpbasePipelineExecutor::Getlevel1CommRank(SubCommInfo& level1CommInfo)
86 : {
87 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
88 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
89 :
90 0 : u32 commIndex = level0CommInfo.localRank; // 找到rank所在的节点间平面
91 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
92 :
93 0 : level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
94 0 : return HCCL_SUCCESS;
95 0 : }
96 :
97 0 : HcclResult CollReduceScatterVMeshOpbasePipelineExecutor::SelectTempAlg(std::unique_ptr<AlgTemplateBase> &level1TempAlg, u32 level1RankSize)
98 : {
99 0 : if (level1RankSize > 1) {
100 0 : level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
101 0 : TemplateType::TEMPLATE_REDUCESCATTER_PIPELINE, dispatcher_);
102 0 : CHK_SMART_PTR_NULL(level1TempAlg);
103 : }
104 0 : return HCCL_SUCCESS;
105 : }
106 :
107 0 : u64 CollReduceScatterVMeshOpbasePipelineExecutor::CalcLoopMaxCount(const u32 unitSize)
108 : {
109 : // 中转内存单次最多能够接受的output count,放开ranksize限制
110 0 : u64 maxCountPerLoop = ((inCCLbufferSize_ / (HCCL_MIN_SLICE_ALIGN_910B * PIPELINE_DEPTH)) \
111 0 : * HCCL_MIN_SLICE_ALIGN_910B - HCCL_MIN_SLICE_ALIGN_910B) / unitSize;
112 0 : HCCL_INFO("[CollReduceScatterVMeshOpbasePipelineExecutor][CalcLoopMaxCount] maxCountPerLoop[%llu]", maxCountPerLoop);
113 0 : return maxCountPerLoop;
114 : }
115 :
116 0 : HcclResult CollReduceScatterVMeshOpbasePipelineExecutor::CalcCurCountsAndCurDispls(const u64 maxTotalCount,
117 : std::vector<u64> &countsLeft, std::vector<u64> &displs, std::vector<u64> &curCounts, std::vector<u64> &curDispls,
118 : bool &finished)
119 : {
120 0 : finished = true;
121 0 : curCounts.resize(countsLeft.size(), 0);
122 0 : curDispls.resize(displs.size(), 0);
123 :
124 : // 先设置本轮的displacements,等于入参displs
125 0 : std::copy(displs.begin(), displs.end(), curDispls.begin());
126 : // 分配好每个rank的counts
127 0 : for (auto i = 0U; i < countsLeft.size(); ++i) {
128 0 : const auto curCount = countsLeft[i] < maxTotalCount ? countsLeft[i] : maxTotalCount;
129 0 : curCounts[i] = curCount;
130 0 : countsLeft[i] -= curCount;
131 0 : displs[i] += curCount;
132 :
133 0 : if(countsLeft[i] != 0) {
134 0 : finished = false;
135 : }
136 : }
137 0 : HCCL_INFO("[%s] Calc CurCountsAndCurDispls finish.", __func__);
138 0 : return HCCL_SUCCESS;
139 : }
140 :
141 0 : HcclResult CollReduceScatterVMeshOpbasePipelineExecutor::KernelRun(const OpParam ¶m, ExecMem &execMem)
142 : {
143 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollReduceScatterVMeshOpbasePipelineExecutor] reducescatterv pipeline run");
144 0 : HcclDataType dataType = param.VDataDes.dataType;
145 0 : const u32 unitSize = SIZE_TABLE[dataType];
146 :
147 : // 先获取 comm level0 \ comm level1 的value
148 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
149 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
150 0 : u32 commIndex = level0CommInfo.localRank;
151 :
152 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
153 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
154 :
155 0 : std::vector<Slice> inputSlices;
156 0 : const auto counts = static_cast<u64*>(param.VDataDes.counts);
157 0 : const auto displs = static_cast<u64*>(param.VDataDes.displs);
158 0 : for (u32 rank = 0; rank < topoAttr_.userRankSize; ++rank) {
159 0 : Slice userslice;
160 0 : userslice.offset = displs[rank] * unitSize;
161 0 : userslice.size = counts[rank] * unitSize;
162 0 : inputSlices.emplace_back(std::move(userslice));
163 : }
164 :
165 0 : u64 reduceAttr = GetReduceAttr(execMem.inputMem, execMem.inputMem, dataType, param.reduceType);
166 :
167 0 : std::unique_ptr<AlgTemplateBase> tempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
168 0 : TemplateType::TEMPLATE_REDUCESCATTER_V_PIPELINE, dispatcher_);
169 0 : CHK_SMART_PTR_NULL(tempAlg);
170 :
171 0 : HcomCollOpInfo opInfo = {"", execMem.inputPtr, execMem.outputPtr, 0, dataType,
172 0 : param.root, param.reduceType};
173 :
174 0 : Stream stream = param.stream;
175 0 : CHK_RET(tempAlg->Prepare(&opInfo, execMem.inputMem, execMem.inputMem.size(), inputSlices, level0CommInfo,
176 : level1CommInfo, stream, algResResp_->slaveStreams, algResResp_->notifiesMain,
177 : algResResp_->notifiesAux, reduceAttr));
178 0 : CHK_RET(tempAlg->RunAsync());
179 :
180 0 : return HCCL_SUCCESS;
181 0 : }
182 :
183 : REGISTER_EXEC("ReduceScatterVMeshOpbasePipelineExecutor", ReduceScatterVMeshOpbasePipeline,
184 : CollReduceScatterVMeshOpbasePipelineExecutor);
185 :
186 : }
|