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(
28 : const RankId virtualRank, const u32 tempRankSize, 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>(
34 0 : StringFormat("[CcuTempAlltoAllVMesh2Die] Rank[%d], Invalid rankSize[%u].", myRank_, tempRankSize_));
35 : }
36 :
37 0 : if (tempVTopo_.size() != 1) {
38 0 : THROW<InvalidParamsException>(StringFormat(
39 : "[CcuTempAlltoAllVMesh2Die] Rank[%d], Invalid tempVTopo size[%u].", 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 : void CcuTempAlltoAllVMesh2Die::SetA2ASendRecvInfo(const A2ASendRecvInfo& sendRecvInfo)
53 : {
54 0 : localSendRecvInfo_ = sendRecvInfo;
55 0 : return;
56 : }
57 :
58 0 : HcclResult CcuTempAlltoAllVMesh2Die::CalcRes(AlgTempResReq& tempResReq)
59 : {
60 0 : tempResReq.queNum = 1; // 只申请一个insQue,填充一个insGroup,由框架将其中的ins放在多个stream上
61 0 : tempResReq.streamNum = tempResReq.queNum + 1; // 多申请一个 stream 给 ccuInsGroup
62 :
63 0 : HCCL_INFO(
64 : "[CcuTempAlltoAllVMesh2Die] Rank[%d] requiredQueNum[%u] VtopoSize[%u], VtopoSize0[%u].", myRank_,
65 : tempResReq.queNum, tempVTopo_.size(), tempVTopo_[0].size());
66 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
67 :
68 0 : return HcclResult::HCCL_SUCCESS;
69 : }
70 :
71 0 : HcclResult CcuTempAlltoAllVMesh2Die::FillLinks(const ResLinks& tempLinks)
72 : {
73 0 : for (const auto& pair : tempLinks) {
74 0 : CHK_PRT_RET(
75 : 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(
82 : "[CcuTempAlltoAllVMesh2Die][Run] Rank[%d] insert link to peerRank[%d] in links(Die0).", myRank_,
83 : pair.first);
84 0 : } else if (dieId == 1) {
85 0 : HCCL_INFO(
86 : "[CcuTempAlltoAllVMesh2Die][Run] Rank[%d] insert link to peerRank[%d] in links(Die1).", myRank_,
87 : pair.first);
88 : } else {
89 0 : HCCL_ERROR(
90 : "[CcuTempAlltoAllVMesh2Die] Rank[%d]--peerRank[%d], Unexpected dieId[%u] in link.", myRank_,
91 : pair.first, dieId);
92 0 : return HcclResult::HCCL_E_PARA;
93 : }
94 0 : links_[dieId].emplace_back(linkData);
95 0 : rankGroup_[dieId].AddRank(pair.first);
96 : }
97 : }
98 0 : rankGroup_[0].AddRank(myRank_); // keep myRank_ at last, sync with context
99 0 : rankGroup_[1].AddRank(myRank_);
100 0 : uint32_t minLinks = std::min(links_[0].size(), links_[1].size());
101 0 : uint32_t maxLinks = std::max(links_[0].size(), links_[1].size());
102 0 : CHK_PRT_RET(
103 : minLinks + 1 != maxLinks,
104 : HCCL_ERROR(
105 : "[CcuTempAlltoAllVMesh2Die] Rank[%d], Unexpected links size, minLinks+1[%u] != maxLinks[%u].", myRank_,
106 : minLinks + 1, maxLinks),
107 : HcclResult::HCCL_E_PARA);
108 0 : return HcclResult::HCCL_SUCCESS;
109 : }
110 :
111 0 : HcclResult CcuTempAlltoAllVMesh2Die::Run(
112 : const TempFuncs& tempFuncs, const RankSliceInfo& sliceInfoVec, const BuffInfo& buffInfo, const ResLinks& tempLinks,
113 : std::vector<InsQuePtr>& tempInsQues)
114 : {
115 : (void)tempFuncs;
116 : (void)sliceInfoVec;
117 : (void)buffInfo;
118 :
119 0 : CHK_PRT_RET(
120 : tempInsQues.empty(), HCCL_ERROR("[CcuTempAlltoAllVMesh2Die][Run] tempInsQues is empty."),
121 : HcclResult::HCCL_E_INTERNAL);
122 :
123 0 : CHK_PRT_RET(
124 : op_.inputMem == nullptr || op_.outputMem == nullptr,
125 : HCCL_ERROR(
126 : "[CcuTempAlltoAllVMesh2Die][Run] Rank[%d] inputmem[%p] or outputmem[%p] is null", myRank_,
127 : op_.inputMem.get(), op_.outputMem.get()),
128 : HcclResult::HCCL_E_PTR);
129 :
130 0 : uint64_t inputAddr = op_.inputMem == nullptr ? 0 : op_.inputMem->GetAddr();
131 0 : uint64_t outputAddr = op_.outputMem == nullptr ? 0 : op_.outputMem->GetAddr();
132 0 : uint64_t scratchAddr = op_.scratchMem == nullptr ? 0 : op_.scratchMem->GetAddr();
133 :
134 0 : HCCL_INFO(
135 : "[CcuTempAlltoAllVMesh2Die][Run] Rank[%d], input[%#llx], output[%#llx], scratch[%#llx], dataType[%d], "
136 : "sendType[%d].",
137 : myRank_, inputAddr, outputAddr, scratchAddr, op_.dataType, op_.all2AllVDataDes.sendType);
138 :
139 : // 分别记录两个Die上的link,构造rankGroup
140 0 : CHK_RET(FillLinks(tempLinks));
141 :
142 : uint64_t token;
143 0 : CHK_RET(GetToken(op_, token));
144 :
145 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
146 0 : for (uint32_t axisId = 0; axisId < 2; axisId++) { // 2Die算法,需要执行两次
147 0 : CcuInstructionAllToAllVMesh2Die ins = CcuInstructionAllToAllVMesh2Die(op_, dimSize_, tempVTopo_);
148 0 : bool withMyRank = links_[axisId].size() < links_[1 - axisId].size();
149 0 : ins.Init(myRank_, withMyRank, inputAddr, outputAddr, scratchAddr, token, localSendRecvInfo_);
150 0 : ins.SetLinks(links_[axisId]);
151 0 : ins.SetRankGroup(rankGroup_[axisId]);
152 0 : const u32 ckeNum = 5;
153 0 : ins.SetCntCkeNum(ckeNum);
154 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionAllToAllVMesh2Die>(ins)));
155 0 : }
156 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
157 :
158 0 : return HcclResult::HCCL_SUCCESS;
159 0 : }
160 :
161 : } // namespace Hccl
|