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_instruction_all_gather_mesh1d_mem2mem.h"
17 : #include "ccu_rank_group.h"
18 : #include "ccu_ctx_creator_registry.h"
19 : #include "ccu_context_all_gather_mesh1d_mem2mem.h"
20 : #include "ccu_temp_all_gather_mesh_1D_mem2mem.h"
21 :
22 : namespace Hccl {
23 :
24 : static CcuInstRegister<CcuContextAllGatherMeshMem2Mem1D>
25 : g_registrarAllGather(CcuInstType::CCU_ALLGATHER_MESH_1D_MEM2MEM);
26 :
27 0 : CcuTempAllGatherMeshMem2Mem1D::CcuTempAllGatherMeshMem2Mem1D(
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 0 : {}
32 :
33 0 : CcuTempAllGatherMeshMem2Mem1D::~CcuTempAllGatherMeshMem2Mem1D() {}
34 :
35 0 : HcclResult CcuTempAllGatherMeshMem2Mem1D::CalcRes(AlgTempResReq& tempResReq)
36 : {
37 0 : tempResReq.queNum = 1;
38 0 : tempResReq.streamNum = tempResReq.queNum;
39 0 : HCCL_INFO("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
40 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
41 0 : return HcclResult::HCCL_SUCCESS;
42 : }
43 :
44 0 : HcclResult CcuTempAllGatherMeshMem2Mem1D::CalcSliceInfo(
45 : const AllignInfo& allignInfo, const u64 dataSize, RankSliceInfo& sliceInfoVec)
46 : {
47 0 : std::vector<SliceInfo> tmp(tempVTopo_.size());
48 0 : sliceInfoVec.resize(tempRankSize_, tmp);
49 :
50 0 : CHK_RET(CalcRsAgSliceInfoMesh(myRank_, tempRankSize_, allignInfo, dataSize, sliceInfoVec));
51 :
52 0 : return HcclResult::HCCL_SUCCESS;
53 0 : }
54 :
55 0 : uint64_t CcuTempAllGatherMeshMem2Mem1D::GetMaxSliceSize() const { return UB_MAX_DATA_SIZE; }
56 :
57 0 : void CcuTempAllGatherMeshMem2Mem1D::GetAddrsAndOffset(
58 : const TempFuncs& tempFuncs, const RankSliceInfo& sliceInfoVec, uint64_t& inputAddr, uint64_t& outputAddr,
59 : uint64_t& offset)
60 : {
61 0 : if (opMode_ == OpMode::OPBASE) {
62 0 : if (tempFuncs.isForepart) {
63 0 : inputAddr = BufferTypeToAddr(tempFuncs.usrData.usrInSlices[0].GetType())
64 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
65 : } else {
66 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
67 : }
68 0 : if (tempFuncs.isBottom) {
69 0 : outputAddr = BufferTypeToAddr(tempFuncs.usrData.usrOutSlices[0].GetType());
70 : // 需要加上 UserOUt 的偏移,包含了 loop 偏移和 rank 偏移
71 0 : offset = tempFuncs.usrData.usrOutSlices[myRank_].GetOffset();
72 : } else {
73 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
74 : // 从 inBuff 获取数据,只需要加上 rank 偏移
75 0 : offset = sliceInfoVec[myRank_][0].offset;
76 : }
77 : } else {
78 : // 图模式没有 tempFuncs.usrData,直接通过 buffInfo_ 获取输入输出地址
79 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff
80 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
81 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
82 0 : offset = tempFuncs.usrData.usrOutSlices[myRank_].GetOffset();
83 : }
84 :
85 0 : return;
86 : }
87 :
88 0 : HcclResult CcuTempAllGatherMeshMem2Mem1D::Run(
89 : const TempFuncs& tempFuncs, const RankSliceInfo& sliceInfoVec, const BuffInfo& buffInfo, const ResLinks& tempLinks,
90 : std::vector<InsQuePtr>& tempInsQues)
91 : {
92 0 : CHK_PRT_RET(
93 : tempInsQues.empty(), HCCL_ERROR("[CcuTempAllGatherMeshMem2Mem1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
94 0 : CHK_PTR_NULL(tempInsQues[0]);
95 0 : opMode_ = tempFuncs.opMode;
96 0 : buffInfo_ = buffInfo;
97 :
98 0 : CcuInstructionAllGatherMeshMem2Mem1D ccuInsAllGatherMeshMem2Mem1D;
99 :
100 0 : std::vector<uint64_t> dimSize;
101 0 : dimSize.push_back(tempRankSize_);
102 :
103 : uint64_t inputAddr;
104 : uint64_t outputAddr;
105 : uint64_t offset;
106 0 : GetAddrsAndOffset(tempFuncs, sliceInfoVec, inputAddr, outputAddr, offset);
107 :
108 0 : uint64_t sliceSize = sliceInfoVec[myRank_][0].size; // 获取本rank需要处理的数据量
109 : uint64_t token;
110 0 : CHK_RET(GetToken(op_, token));
111 0 : ccuInsAllGatherMeshMem2Mem1D.Init(
112 0 : static_cast<uint32_t>(myRank_), inputAddr, outputAddr, sliceSize, offset, token, op_, tempVTopo_);
113 0 : HCCL_INFO(
114 : "[CcuTempAllGatherMeshMem2Mem1D] Run Init: myRank_[%d], dimSize[%llu], inputAddr[%llu],"
115 : "outputAddr[%llu], sliceSize[%llu], offset[%llu]",
116 : myRank_, dimSize[0], inputAddr, outputAddr, sliceSize, offset);
117 :
118 0 : std::vector<LinkData> links;
119 0 : for (auto& pair : tempLinks) {
120 0 : if (pair.second.empty()) {
121 0 : continue;
122 : }
123 0 : links.push_back(pair.second[0]);
124 : }
125 0 : HCCL_INFO("[CcuTempAllGatherMeshMem2Mem1D] links.size[%zu]", links.size());
126 0 : ccuInsAllGatherMeshMem2Mem1D.SetLinks(links);
127 :
128 0 : RankGroup rankGroup;
129 0 : for (auto& peer : tempVTopo_[0]) {
130 0 : rankGroup.AddRank(peer);
131 : }
132 0 : u32 cntCkeNum = 3;
133 0 : ccuInsAllGatherMeshMem2Mem1D.SetCntCkeNum(cntCkeNum);
134 0 : ccuInsAllGatherMeshMem2Mem1D.SetRankGroup(rankGroup);
135 0 : HCCL_INFO("CcuTempAllGatherMeshMem2Mem1D is [%s]", ccuInsAllGatherMeshMem2Mem1D.Describe().c_str());
136 0 : ccuInsAllGatherMeshMem2Mem1D.Describe();
137 0 : tempInsQues[0]->Append(
138 0 : std::move(std::make_unique<CcuInstructionAllGatherMeshMem2Mem1D>(ccuInsAllGatherMeshMem2Mem1D)));
139 :
140 0 : return HcclResult::HCCL_SUCCESS;
141 0 : }
142 :
143 : } // namespace Hccl
|