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_reduce_mesh_1D_twoshot.h"
14 :
15 : namespace Hccl {
16 :
17 0 : AivTempAllReduceMesh1DTwoShot::AivTempAllReduceMesh1DTwoShot(
18 : const RankId virtualRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
19 0 : const std::map<RankId, u32>& tempVirtRankMap)
20 0 : : AivAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
21 : {
22 0 : HCCL_INFO("[AivTempAllReduceMesh1DTwoShot] Init.");
23 0 : }
24 :
25 0 : AivTempAllReduceMesh1DTwoShot::~AivTempAllReduceMesh1DTwoShot() { HCCL_INFO("[AivTempAllReduceMesh1DTwoShot] exit."); }
26 :
27 0 : HcclResult AivTempAllReduceMesh1DTwoShot::CalcRes(AlgTempResReq& tempResReq)
28 : {
29 0 : tempResReq.queNum = 1;
30 0 : tempResReq.streamNum = tempResReq.queNum;
31 0 : HCCL_INFO("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
32 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
33 0 : return HcclResult::HCCL_SUCCESS;
34 : }
35 :
36 0 : u32 AivTempAllReduceMesh1DTwoShot::CalcScratchMultiple(BufferType inBuffType, BufferType outBuffType)
37 : {
38 : (void)inBuffType;
39 : (void)outBuffType;
40 0 : u32 multiplier = 4;
41 0 : return multiplier;
42 : }
43 :
44 0 : HcclResult AivTempAllReduceMesh1DTwoShot::CalNumBlocks(u32& numBlocks, u64 dataSize, u32 numBlocksLimit)
45 : {
46 : (void)dataSize;
47 :
48 0 : if (numBlocksLimit >= (tempRankSize_ + 1)) {
49 0 : u32 coreNumPerRank = numBlocksLimit / (tempRankSize_ + 1);
50 0 : numBlocks = coreNumPerRank * (tempRankSize_ + 1);
51 : } else {
52 : // 如果要用更少的核心可以在这里折算,比如rankSize/2个核心
53 0 : numBlocks = numBlocksLimit;
54 : }
55 0 : return HcclResult::HCCL_SUCCESS;
56 : }
57 :
58 0 : HcclResult AivTempAllReduceMesh1DTwoShot::GenExtIns(
59 : const TempFuncs& tempFuncs, const TemplateDataParams& templateDataParams, const ResLinks& tempLinks,
60 : std::vector<InsQuePtr>& tempInsQues)
61 : {
62 0 : HCCL_INFO("[AivTempAllReduceMesh1DTwoShot] start.");
63 0 : CHK_PRT_RET(
64 : tempInsQues.empty(), HCCL_ERROR("[AivTempAllReduceMesh1DTwoShot] empty queue"), HcclResult::HCCL_E_INTERNAL);
65 0 : CHK_PTR_NULL(tempInsQues[0]);
66 0 : std::vector<LinkData> allLinks;
67 0 : for (auto iter = tempLinks.begin(); iter != tempLinks.end(); ++iter) {
68 0 : allLinks.emplace_back(iter->second.at(0));
69 : }
70 0 : IncSliceId(); // 自动增长sliceId,传入aivTag
71 :
72 0 : AivOpArgs aivAllreduceArgs;
73 0 : aivAllreduceArgs.cmdType = HcclCMDType::HCCL_CMD_ALLREDUCE;
74 0 : aivAllreduceArgs.argsType = KernelArgsType::ARGS_TYPE_TWO_SHOT;
75 0 : aivAllreduceArgs.input = templateDataParams.buffInfo.inBuffBaseOff;
76 0 : aivAllreduceArgs.output = templateDataParams.buffInfo.outBuffBaseOff;
77 0 : aivAllreduceArgs.rank = myRank_;
78 0 : aivAllreduceArgs.rankSize = tempRankSize_;
79 0 : aivAllreduceArgs.count = templateDataParams.sliceSize / DataTypeSizeGet(dataType_);
80 0 : aivAllreduceArgs.dataType = dataType_;
81 0 : aivAllreduceArgs.op = reduceOp_;
82 0 : aivAllreduceArgs.root = root_;
83 0 : aivAllreduceArgs.aivTag = sliceId_; // 传入aivTag,Lauch时重新组装为aivTag
84 0 : aivAllreduceArgs.isOpBase = (tempFuncs.opMode == OpMode::OPBASE);
85 0 : aivAllreduceArgs.xRankSize = tempVTopo_[0].size();
86 0 : CalNumBlocks(aivAllreduceArgs.numBlocks, templateDataParams.sliceSize, op_.numBlocksLimit);
87 0 : HCCL_INFO("[AivTempAllReduceMesh1DTwoShot] Actually use core num[%u]", aivAllreduceArgs.numBlocks);
88 :
89 0 : for (u32 i = 0; i < tempVTopo_[0].size(); i++) {
90 0 : aivAllreduceArgs.topo_[i] = tempVTopo_[0][i];
91 : }
92 :
93 0 : u32 sizeOne = 1, sizeTwo = 1;
94 0 : if (tempVTopo_.size() > sizeOne) {
95 0 : aivAllreduceArgs.yRankSize = tempVTopo_[1].size();
96 0 : for (u32 i = 0; i < tempVTopo_[1].size(); i++) {
97 0 : aivAllreduceArgs.topo_[TOPO_LEN_Y_OFFSET + i] = tempVTopo_[1][i];
98 : }
99 : }
100 0 : if (tempVTopo_.size() > sizeTwo) {
101 0 : aivAllreduceArgs.zRankSize = tempVTopo_[2].size();
102 0 : for (u32 i = 0; i < tempVTopo_[2].size(); i++) {
103 0 : aivAllreduceArgs.topo_[TOPO_LEN_Z_OFFSET + i] = tempVTopo_[2][i];
104 : }
105 : }
106 :
107 0 : aivAllreduceArgs.inputSliceStride = templateDataParams.inputSliceStride;
108 0 : aivAllreduceArgs.outputSliceStride = templateDataParams.outputSliceStride;
109 0 : aivAllreduceArgs.repeatNum = templateDataParams.repeatNum;
110 0 : aivAllreduceArgs.inputRepeatStride = templateDataParams.inputRepeatStride;
111 0 : aivAllreduceArgs.outputRepeatStride = templateDataParams.outputRepeatStride;
112 0 : std::unique_ptr<Instruction> aivInsAllreduceMesh1D = std::make_unique<AivInstruction>(allLinks, aivAllreduceArgs);
113 0 : tempInsQues[0]->Append(std::move(aivInsAllreduceMesh1D));
114 0 : HCCL_INFO("[AivTempAllReduceMesh1DTwoShot] GenExtIns finished");
115 0 : return HcclResult::HCCL_SUCCESS;
116 0 : }
117 :
118 : } // namespace Hccl
|