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