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