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 "hccl_aiv_utils.h"
12 : #include "aiv_ins.h"
13 : #include "aiv_temp_all_to_all_mesh_1D.h"
14 : #include "executor_utils.h"
15 :
16 : namespace Hccl {
17 :
18 0 : AivTempAlltoAllMesh1D::AivTempAlltoAllMesh1D(
19 : const RankId virtualRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
20 0 : const std::map<RankId, u32>& tempVirtRankMap)
21 0 : : AivAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
22 0 : {}
23 :
24 0 : AivTempAlltoAllMesh1D::~AivTempAlltoAllMesh1D() {}
25 :
26 0 : u32 AivTempAlltoAllMesh1D::CalcScratchMultiple(BufferType inBuffType, BufferType outBuffType)
27 : {
28 : (void)inBuffType;
29 : (void)outBuffType;
30 : // 单算子和图模式一致,AlltoAll的usrIn、scratchBuffer,usrOut大小一致
31 0 : return 1;
32 : }
33 :
34 0 : HcclResult AivTempAlltoAllMesh1D::CalNumBlocks(u32& numBlocks, u64 dataSize, u32 numBlocksLimit)
35 : {
36 : (void)dataSize;
37 0 : HCCL_INFO("[AivTempAlltoAllMesh1D] Limit core num[%u]", numBlocksLimit);
38 :
39 : // 小于1的场景
40 0 : if (numBlocksLimit < 1) {
41 0 : numBlocks = numBlocksLimit;
42 0 : return HcclResult::HCCL_SUCCESS;
43 : }
44 :
45 0 : if (numBlocksLimit >= tempRankSize_) {
46 0 : numBlocks = numBlocksLimit / tempRankSize_ * tempRankSize_;
47 : } else {
48 0 : u32 rankPerCore = (tempRankSize_ + numBlocksLimit - 1) / numBlocksLimit; // 向上取整
49 0 : numBlocks = (tempRankSize_ + rankPerCore - 1) / rankPerCore; // 向上取整
50 : }
51 :
52 0 : HCCL_INFO("[AivTempAlltoAllMesh1D] Actually use core num[%u]", numBlocks);
53 0 : return HcclResult::HCCL_SUCCESS;
54 : }
55 :
56 0 : HcclResult AivTempAlltoAllMesh1D::CalcRes(AlgTempResReq& tempResReq)
57 : {
58 0 : tempResReq.queNum = 1;
59 0 : tempResReq.streamNum = tempResReq.queNum;
60 0 : HCCL_INFO("[AivTempAlltoAllMesh1D] Calculate resource, stream number is[%u],", tempResReq.streamNum);
61 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
62 0 : return HcclResult::HCCL_SUCCESS;
63 : }
64 :
65 0 : HcclResult AivTempAlltoAllMesh1D::GenExtIns(
66 : const TempFuncs& tempFuncs, const TemplateDataParams& templateDataParams, const ResLinks& tempLinks,
67 : std::vector<InsQuePtr>& tempInsQues)
68 : {
69 0 : HCCL_INFO("[AivTempAlltoAllMesh1D] Run algorithm start: rank[%d]", myRank_);
70 0 : CHK_PRT_RET(tempInsQues.empty(), HCCL_ERROR("[AivTempAlltoAllMesh1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
71 0 : CHK_PTR_NULL(tempInsQues[0]);
72 0 : std::vector<LinkData> allLinks;
73 0 : for (auto iter = tempLinks.begin(); iter != tempLinks.end(); ++iter) {
74 0 : allLinks.emplace_back(iter->second.at(0));
75 : }
76 :
77 0 : IncSliceId(); // 自动增长sliceId,传入aivTag
78 :
79 0 : AivOpArgs aivAlltoAllArgs;
80 0 : aivAlltoAllArgs.cmdType = HcclCMDType::HCCL_CMD_ALLTOALL;
81 0 : aivAlltoAllArgs.input = templateDataParams.buffInfo.inBuffBaseOff;
82 0 : aivAlltoAllArgs.output = templateDataParams.buffInfo.outBuffBaseOff;
83 0 : aivAlltoAllArgs.rank = u32(myRank_);
84 0 : aivAlltoAllArgs.rankSize = tempRankSize_;
85 0 : aivAlltoAllArgs.count = templateDataParams.sliceSize / DataTypeSizeGet(dataType_);
86 0 : aivAlltoAllArgs.dataType = dataType_;
87 0 : aivAlltoAllArgs.op = reduceOp_;
88 0 : aivAlltoAllArgs.root = root_;
89 0 : aivAlltoAllArgs.aivTag = sliceId_; // 传入aivTag,Lauch时重新组装为aivTag
90 0 : aivAlltoAllArgs.isOpBase = (tempFuncs.opMode == OpMode::OPBASE);
91 0 : aivAlltoAllArgs.xRankSize = tempVTopo_[0].size();
92 0 : aivAlltoAllArgs.yRankSize = 0;
93 0 : aivAlltoAllArgs.zRankSize = 0;
94 0 : u64 dataSize = op_.dataCount * DataTypeSizeGet(dataType_);
95 0 : CHK_RET(CalNumBlocks(aivAlltoAllArgs.numBlocks, dataSize, op_.numBlocksLimit));
96 0 : for (u32 i = 0; i < tempVTopo_[0].size(); i++) {
97 0 : aivAlltoAllArgs.topo_[i] = tempVTopo_[0][i];
98 : }
99 0 : if (tempVTopo_.size() > 1) {
100 0 : aivAlltoAllArgs.yRankSize = tempVTopo_[1].size();
101 0 : for (u32 i = 0; i < tempVTopo_[1].size(); i++) {
102 0 : aivAlltoAllArgs.topo_[TOPO_LEN_Y_OFFSET + i] = tempVTopo_[1][i];
103 : }
104 : }
105 0 : if (tempVTopo_.size() == MAX_DIM_NUM) {
106 0 : aivAlltoAllArgs.zRankSize = tempVTopo_[MAX_DIM_NUM - 1].size();
107 0 : for (u32 i = 0; i < tempVTopo_[MAX_DIM_NUM - 1].size(); i++) {
108 0 : aivAlltoAllArgs.topo_[TOPO_LEN_Z_OFFSET + i] = tempVTopo_[MAX_DIM_NUM - 1][i];
109 : }
110 : }
111 :
112 0 : aivAlltoAllArgs.inputSliceStride = templateDataParams.inputSliceStride;
113 0 : aivAlltoAllArgs.outputSliceStride = templateDataParams.outputSliceStride;
114 0 : aivAlltoAllArgs.repeatNum = templateDataParams.repeatNum;
115 0 : aivAlltoAllArgs.inputRepeatStride = templateDataParams.inputRepeatStride;
116 0 : aivAlltoAllArgs.outputRepeatStride = templateDataParams.outputRepeatStride;
117 :
118 0 : std::unique_ptr<Instruction> aivInsAlltoAllMesh1D = std::make_unique<AivInstruction>(allLinks, aivAlltoAllArgs);
119 :
120 0 : tempInsQues[0]->Append(std::move(aivInsAlltoAllMesh1D));
121 :
122 0 : HCCL_INFO("[AivTempAlltoAllMesh1D] Run algorithm end: rank[%d]", myRank_);
123 0 : return HcclResult::HCCL_SUCCESS;
124 0 : }
125 :
126 : } // namespace Hccl
|