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