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 <ios>
12 : #include <iostream>
13 :
14 : #include "log.h"
15 :
16 : #include "ccu_ins_group.h"
17 : #include "ccu_instruction_all_reduce_mesh1d_one_shot.h"
18 : #include "ccu_rank_group.h"
19 : #include "ccu_ctx_creator_registry.h"
20 : #include "ccu_context_all_reduce_mesh1d_one_shot.h"
21 : #include "ccu_temp_all_reduce_mesh_1D_one_shot.h"
22 : #include "ccu_context_reduce_tail_block.h"
23 :
24 : namespace Hccl {
25 : static CcuInstRegister<CcuContextAllReduceMesh1DOneShot>
26 : g_registrarAllReduce(CcuInstType::CCU_ALL_REDUCE_MESH_1D_ONE_SHOT_DIRECT);
27 :
28 : static CcuInstRegister<CcuContextReduceTailBlock> g_registrarReduceTailBlock(CcuInstType::CCU_REDUCE_TAILBLOCK_DIRECT);
29 :
30 0 : CcuTempAllReduceMesh1DOneShot::CcuTempAllReduceMesh1DOneShot(
31 : const RankId virtualRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
32 0 : const std::map<RankId, u32>& tempVirtRankMap)
33 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
34 0 : {}
35 :
36 0 : CcuTempAllReduceMesh1DOneShot::~CcuTempAllReduceMesh1DOneShot() {}
37 :
38 0 : void CcuTempAllReduceMesh1DOneShot::InitReduceInfo(const ReduceOp& reduceOp, const DataType& dataType)
39 : {
40 0 : reduceOp_ = reduceOp;
41 0 : dataType_ = dataType;
42 0 : }
43 :
44 0 : HcclResult CcuTempAllReduceMesh1DOneShot::CalcSliceInfo(
45 : const AllignInfo& allignInfo, const u64 dataSize, RankSliceInfo& sliceInfoVec)
46 : {
47 : (void)allignInfo;
48 : SliceInfo basicSlice;
49 0 : basicSlice.offset = 0;
50 0 : basicSlice.size = dataSize;
51 0 : HCCL_INFO("[CcuTempAllReduceMesh1DOneShot][CalcSliceInfo] basicSlice.size[%u]", basicSlice.size);
52 0 : std::vector<SliceInfo> singleRankSliceInfoVector{basicSlice};
53 0 : sliceInfoVec.resize(tempRankSize_, singleRankSliceInfoVector);
54 0 : return HcclResult::HCCL_SUCCESS;
55 0 : }
56 :
57 0 : HcclResult CcuTempAllReduceMesh1DOneShot::CalcRes(AlgTempResReq& tempResReq)
58 : {
59 0 : tempResReq.queNum = 1;
60 0 : tempResReq.streamNum = tempResReq.queNum + 1; // 多申请一个 stream 给 ccuInsGroup
61 0 : HCCL_INFO("[CcuTempAllReduceMesh1DOneShot][CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
62 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
63 0 : return HcclResult::HCCL_SUCCESS;
64 : }
65 :
66 0 : HcclResult CcuTempAllReduceMesh1DOneShot::Run(
67 : const TempFuncs& tempFuncs, const RankSliceInfo& sliceInfoVec, const BuffInfo& buffInfo, const ResLinks& tempLinks,
68 : std::vector<InsQuePtr>& tempInsQues)
69 : {
70 0 : HCCL_INFO("[CcuTempAllReduceMesh1DOneShot][Run] start");
71 0 : CHK_PRT_RET(
72 : tempInsQues.empty(), HCCL_ERROR("[CcuTempAllReduceMesh1DOneShot] empty queue"), HcclResult::HCCL_E_INTERNAL);
73 0 : CHK_PTR_NULL(tempInsQues[0]);
74 0 : opMode_ = tempFuncs.opMode;
75 0 : buffInfo_ = buffInfo;
76 0 : CHK_PRT_RET(
77 : myRank_ == INVALID_RANKID, HCCL_ERROR("[CcuTempAllReduceMesh1DOneShot][Run]myRank[%d] is invalid", myRank_),
78 : HcclResult::HCCL_E_INTERNAL);
79 :
80 0 : uint32_t rankId = static_cast<uint32_t>(myRank_);
81 :
82 : uint64_t inputAddr;
83 : uint64_t outputAddr;
84 0 : CHK_RET(CalcInputOutputAddr(tempFuncs, inputAddr, outputAddr));
85 :
86 0 : std::vector<LinkData> links;
87 0 : CHK_RET(PrepareLinks(tempLinks, links));
88 :
89 : uint64_t token;
90 0 : CHK_RET(GetToken(op_, token));
91 :
92 0 : uint64_t totalSliceSize = sliceInfoVec[myRank_][0].size; // 本rank需要处理的数据量
93 :
94 0 : u32 cntCkeNum = 4;
95 :
96 0 : RankGroup rankGroup;
97 0 : CHK_RET(PrepareRankGroup(rankGroup));
98 :
99 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
100 :
101 0 : std::string notifySignal = "AllReduceMesh1DOneShot_TailBlock";
102 : // 添加主拓展指令
103 0 : CcuInstructionAllReduceMesh1DOneShot ccuInstructionAllReduceMesh1DOneShot;
104 0 : ccuInstructionAllReduceMesh1DOneShot.Init(
105 0 : rankId, inputAddr, outputAddr, totalSliceSize, token, notifySignal, op_, tempVTopo_);
106 0 : ccuInstructionAllReduceMesh1DOneShot.SetLinks(links);
107 0 : ccuInstructionAllReduceMesh1DOneShot.SetCntCkeNum(cntCkeNum);
108 0 : ccuInstructionAllReduceMesh1DOneShot.SetRankGroup(rankGroup);
109 0 : tempInsQues[0]->Append(
110 0 : std::move(std::make_unique<CcuInstructionAllReduceMesh1DOneShot>(ccuInstructionAllReduceMesh1DOneShot)));
111 0 : HCCL_INFO("[CcuTempAllReduceMesh1DOneShot][Run] end");
112 0 : return HcclResult::HCCL_SUCCESS;
113 0 : }
114 :
115 0 : HcclResult CcuTempAllReduceMesh1DOneShot::CalcInputOutputAddr(
116 : const TempFuncs& tempFuncs, uint64_t& inputAddr, uint64_t& outputAddr)
117 : {
118 0 : HCCL_INFO("[CcuTempAllReduceMesh1DOneShot][CalcInputOutputAddr] start");
119 0 : if (opMode_ == OpMode::OPBASE) {
120 0 : if (tempFuncs.isBottom) {
121 0 : outputAddr = BufferTypeToAddr(tempFuncs.usrData.usrOutSlices[0].GetType())
122 0 : + tempFuncs.usrData.usrOutSlices[0].GetOffset();
123 : } else {
124 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
125 : }
126 0 : if (tempFuncs.isForepart) {
127 0 : inputAddr = BufferTypeToAddr(tempFuncs.usrData.usrInSlices[0].GetType())
128 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
129 : } else {
130 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
131 : }
132 : } else {
133 : // 图模式
134 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
135 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff
136 0 : + tempFuncs.usrData.usrOutSlices[0].GetOffset();
137 : }
138 0 : HCCL_INFO(
139 : "[CcuTempAllReduceMesh1DOneShot][CalcInputOutputAddr] end, inputAddr[%llu], outputAddr[%llu]", inputAddr,
140 : outputAddr);
141 0 : return HcclResult::HCCL_SUCCESS;
142 : }
143 :
144 0 : HcclResult CcuTempAllReduceMesh1DOneShot::PrepareLinks(const ResLinks& tempLinks, std::vector<LinkData>& links) const
145 : {
146 0 : for (auto& pair : tempLinks) {
147 0 : if (pair.second.empty()) {
148 0 : continue;
149 : }
150 0 : links.push_back(pair.second[0]);
151 : }
152 0 : HCCL_INFO("[CcuTempAllReduceMesh1DOneShot][PrepareLinks] end, links.size[%zu]", links.size());
153 0 : return HcclResult::HCCL_SUCCESS;
154 : }
155 :
156 0 : HcclResult CcuTempAllReduceMesh1DOneShot::PrepareRankGroup(RankGroup& rankGroup)
157 : {
158 0 : for (auto& peer : tempVTopo_[0]) {
159 0 : rankGroup.AddRank(peer);
160 : }
161 0 : return HcclResult::HCCL_SUCCESS;
162 : }
163 : } // namespace Hccl
|