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