Line data Source code
1 :
2 : /**
3 : * Copyright (c) 2026 Huawei Technologies Co., Ltd.
4 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5 : * CANN Open Software License Agreement Version 2.0 (the "License").
6 : * Please refer to the License for details. You may not use this file except in compliance with the License.
7 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9 : * See LICENSE in the root of the software repository for the full text of the License.
10 : */
11 :
12 : #include "ccu_temp_all_to_all_v_mesh_2Die.h"
13 :
14 : #include <ios>
15 : #include <iostream>
16 :
17 : #include "log.h"
18 : #include "ccu_rank_group.h"
19 : #include "ccu_ctx_creator_registry.h"
20 : #include "ccu_ins_group.h"
21 : #include "ccu_context_all_to_all_v_mesh2die.h"
22 :
23 : namespace Hccl {
24 :
25 : static CcuInstRegister<CcuContextAllToAllVMesh2Die> registerAlltoAllV2Die(CcuInstType::CCU_ALLTOALLV_MESH_2DIE_DIRECT);
26 :
27 0 : CcuTempAlltoAllVMesh2Die::CcuTempAlltoAllVMesh2Die(const RankId virtualRank, const u32 tempRankSize,
28 : const std::vector<std::vector<RankId>> &tempVTopo,
29 0 : const std::map<RankId, u32> &tempVirtRankMap)
30 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
31 : {
32 0 : if (tempRankSize_ <= 1 || tempRankSize_ % 2 != 0) {
33 0 : THROW<InvalidParamsException>(StringFormat("[CcuTempAlltoAllVMesh2Die] Rank[%d], Invalid rankSize[%u].",
34 : myRank_, tempRankSize_));
35 : }
36 :
37 0 : if (tempVTopo_.size() != 1) {
38 0 : THROW<InvalidParamsException>(StringFormat("[CcuTempAlltoAllVMesh2Die] Rank[%d], Invalid tempVTopo size[%u].",
39 : myRank_, tempVTopo_.size()));
40 : }
41 0 : if (tempVTopo_[0].size() != tempRankSize_) {
42 0 : THROW<InvalidParamsException>(StringFormat(
43 : "[CcuTempAlltoAllVMesh2Die] Rank[%d], Invalid tempVTopo[0] size[%u] is not equal to rankSize[%u].", myRank_,
44 0 : tempVTopo_[0].size(), tempRankSize_));
45 : }
46 : // 填充框内的维度大小
47 0 : dimSize_.push_back(tempRankSize_);
48 0 : }
49 :
50 0 : CcuTempAlltoAllVMesh2Die::~CcuTempAlltoAllVMesh2Die()
51 : {
52 0 : }
53 :
54 0 : void CcuTempAlltoAllVMesh2Die::SetA2ASendRecvInfo(const A2ASendRecvInfo &sendRecvInfo)
55 : {
56 0 : localSendRecvInfo_ = sendRecvInfo;
57 0 : return;
58 : }
59 :
60 0 : HcclResult CcuTempAlltoAllVMesh2Die::CalcRes(AlgTempResReq &tempResReq)
61 : {
62 0 : tempResReq.queNum = 1; // 只申请一个insQue,填充一个insGroup,由框架将其中的ins放在多个stream上
63 0 : tempResReq.streamNum = tempResReq.queNum + 1; // 多申请一个 stream 给 ccuInsGroup
64 :
65 0 : HCCL_INFO("[CcuTempAlltoAllVMesh2Die] Rank[%d] requiredQueNum[%u] VtopoSize[%u], VtopoSize0[%u].",
66 : myRank_, tempResReq.queNum, tempVTopo_.size(), tempVTopo_[0].size());
67 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
68 :
69 0 : return HcclResult::HCCL_SUCCESS;
70 : }
71 :
72 0 : HcclResult CcuTempAlltoAllVMesh2Die::FillLinks(const ResLinks &tempLinks)
73 : {
74 0 : for (const auto &pair : tempLinks) {
75 0 : CHK_PRT_RET(pair.second.empty(), // ESL环境上暂只有直连链路
76 : HCCL_ERROR("[CcuTempAlltoAllVMesh2Die] Rank[%d]--peerRank[%d] no link.", myRank_, pair.first),
77 : HcclResult::HCCL_E_PARA);
78 0 : for (const auto &linkData : pair.second) {
79 0 : const u32 dieId = linkData.GetLocalDieId();
80 0 : if (dieId == 0) {
81 0 : HCCL_INFO("[CcuTempAlltoAllVMesh2Die][Run] Rank[%d] insert link to peerRank[%d] in links(Die0).",
82 : myRank_, pair.first);
83 0 : } else if (dieId == 1) {
84 0 : HCCL_INFO("[CcuTempAlltoAllVMesh2Die][Run] Rank[%d] insert link to peerRank[%d] in links(Die1).",
85 : myRank_, pair.first);
86 : } else {
87 0 : HCCL_ERROR("[CcuTempAlltoAllVMesh2Die] Rank[%d]--peerRank[%d], Unexpected dieId[%u] in link.",
88 : myRank_, pair.first, dieId);
89 0 : return HcclResult::HCCL_E_PARA;
90 : }
91 0 : links_[dieId].emplace_back(linkData);
92 0 : rankGroup_[dieId].AddRank(pair.first);
93 : }
94 : }
95 0 : rankGroup_[0].AddRank(myRank_); // keep myRank_ at last, sync with context
96 0 : rankGroup_[1].AddRank(myRank_);
97 0 : uint32_t minLinks = std::min(links_[0].size(), links_[1].size());
98 0 : uint32_t maxLinks = std::max(links_[0].size(), links_[1].size());
99 0 : CHK_PRT_RET(minLinks + 1 != maxLinks,
100 : HCCL_ERROR("[CcuTempAlltoAllVMesh2Die] Rank[%d], Unexpected links size, minLinks+1[%u] != maxLinks[%u].",
101 : myRank_, minLinks + 1, maxLinks), HcclResult::HCCL_E_PARA);
102 0 : return HcclResult::HCCL_SUCCESS;
103 : }
104 :
105 0 : HcclResult CcuTempAlltoAllVMesh2Die::Run(const TempFuncs &tempFuncs, const RankSliceInfo &sliceInfoVec,
106 : const BuffInfo &buffInfo, const ResLinks &tempLinks,
107 : std::vector<InsQuePtr> &tempInsQues)
108 : {
109 : (void)tempFuncs;
110 : (void)sliceInfoVec;
111 : (void)buffInfo;
112 :
113 0 : CHK_PRT_RET(tempInsQues.empty(),
114 : HCCL_ERROR("[CcuTempAlltoAllVMesh2Die][Run] tempInsQues is empty."), HcclResult::HCCL_E_INTERNAL);
115 :
116 0 : CHK_PRT_RET(op_.inputMem == nullptr || op_.outputMem == nullptr,
117 : HCCL_ERROR("[CcuTempAlltoAllVMesh2Die][Run] Rank[%d] inputmem[%p] or outputmem[%p] is null", myRank_,
118 : op_.inputMem.get(), op_.outputMem.get()), HcclResult::HCCL_E_PTR);
119 :
120 0 : uint64_t inputAddr = op_.inputMem == nullptr ? 0 : op_.inputMem->GetAddr();
121 0 : uint64_t outputAddr = op_.outputMem == nullptr ? 0 : op_.outputMem->GetAddr();
122 0 : uint64_t scratchAddr = op_.scratchMem == nullptr ? 0 : op_.scratchMem->GetAddr();
123 :
124 0 : HCCL_INFO("[CcuTempAlltoAllVMesh2Die][Run] Rank[%d], input[%#llx], output[%#llx], scratch[%#llx], dataType[%d], "
125 : "sendType[%d].", myRank_, inputAddr, outputAddr, scratchAddr, op_.dataType, op_.all2AllVDataDes.sendType);
126 :
127 : // 分别记录两个Die上的link,构造rankGroup
128 0 : CHK_RET(FillLinks(tempLinks));
129 :
130 : uint64_t token;
131 0 : CHK_RET(GetToken(op_, token));
132 :
133 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
134 0 : for (uint32_t axisId = 0; axisId < 2; axisId++) { // 2Die算法,需要执行两次
135 0 : CcuInstructionAllToAllVMesh2Die ins = CcuInstructionAllToAllVMesh2Die(op_, dimSize_, tempVTopo_);
136 0 : bool withMyRank = links_[axisId].size() < links_[1 - axisId].size();
137 0 : ins.Init(myRank_, withMyRank, inputAddr, outputAddr, scratchAddr, token, localSendRecvInfo_);
138 0 : ins.SetLinks(links_[axisId]);
139 0 : ins.SetRankGroup(rankGroup_[axisId]);
140 0 : const u32 ckeNum = 5;
141 0 : ins.SetCntCkeNum(ckeNum);
142 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionAllToAllVMesh2Die>(ins)));
143 0 : }
144 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
145 :
146 0 : return HcclResult::HCCL_SUCCESS;
147 0 : }
148 :
149 : } // namespace Hccl
|