Line data Source code
1 : /**
2 : * Copyright (c) 2026 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_rank_group.h"
17 : #include "ccu_ctx_creator_registry.h"
18 : #include "ccu_ins_group.h"
19 : #include "ccu_context_reduce_scatter_mesh1d_2die.h"
20 : #include "ccu_temp_reduce_scatter_mesh_1D_2die.h"
21 : #include "alg_data_trans_wrapper.h"
22 :
23 : namespace Hccl {
24 :
25 : static CcuInstRegister<CcuContextReduceScatterMesh1D2Die>
26 : g_registrarReduceScatter(CcuInstType::CCU_REDUCE_SCATTER_MESH_1D_2DIE);
27 :
28 0 : CcuTempReduceScatterMesh1D2Die::CcuTempReduceScatterMesh1D2Die(
29 : const RankId virtualRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
30 0 : const std::map<RankId, u32>& tempVirtRankMap)
31 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
32 0 : {}
33 :
34 0 : CcuTempReduceScatterMesh1D2Die::~CcuTempReduceScatterMesh1D2Die() {}
35 :
36 0 : u32 CcuTempReduceScatterMesh1D2Die::CalcScratchMultiple(BufferType inBuffType, BufferType outBuffType)
37 : {
38 : (void)inBuffType;
39 : (void)outBuffType;
40 0 : return 2;
41 : }
42 :
43 0 : void CcuTempReduceScatterMesh1D2Die::InitReduceInfo(const ReduceOp& reduceOp, const DataType& dataType)
44 : {
45 0 : reduceOp_ = reduceOp;
46 0 : dataType_ = dataType;
47 0 : }
48 :
49 0 : HcclResult CcuTempReduceScatterMesh1D2Die::CalcRes(AlgTempResReq& tempResReq)
50 : {
51 0 : tempResReq.queNum = 1;
52 0 : tempResReq.streamNum = tempResReq.queNum + 1;
53 0 : HCCL_INFO("[CcuTempReduceScatterMesh1D2Die][CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
54 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
55 0 : return HcclResult::HCCL_SUCCESS;
56 : }
57 :
58 0 : uint64_t CcuTempReduceScatterMesh1D2Die::GetMaxSliceSize() const
59 : {
60 0 : u64 msSize = 4 * 1024;
61 0 : u64 loopNum = 64;
62 0 : u64 maxIterNum = 8192;
63 0 : return msSize * loopNum * maxIterNum; // 2G
64 : }
65 :
66 0 : HcclResult CcuTempReduceScatterMesh1D2Die::GenExtIns(
67 : const TempFuncs& tempFuncs, TemplateDataParams& tempAlgParams, const ResLinks& tempLinks,
68 : std::vector<InsQuePtr>& tempInsQues)
69 : {
70 : (void)tempFuncs;
71 0 : HCCL_INFO(
72 : "[CcuTempReduceScatterMesh1D2Die] sliceSize[%llu], inputSliceStride[%llu], outputSliceStride[%llu], "
73 : "repeatNum[%llu], inputRepeatStride[%llu], outputRepeatStride[%llu], tailSize[%llu]",
74 : tempAlgParams.sliceSize, tempAlgParams.inputSliceStride, tempAlgParams.outputSliceStride,
75 : tempAlgParams.repeatNum, tempAlgParams.inputRepeatStride, tempAlgParams.outputRepeatStride,
76 : tempAlgParams.tailSize);
77 0 : CHK_PRT_RET(
78 : tempInsQues.empty(), HCCL_ERROR("[CcuTempReduceScatterMesh1D2Die] empty queue"), HcclResult::HCCL_E_INTERNAL);
79 0 : CHK_PTR_NULL(tempInsQues[0]);
80 0 : uint64_t inputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.inBuffType) + tempAlgParams.buffInfo.inBuffBaseOff;
81 0 : uint64_t outputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.outBuffType) + tempAlgParams.buffInfo.outBuffBaseOff;
82 : uint64_t scratchAddr
83 0 : = BufferTypeToAddr(tempAlgParams.buffInfo.scratBuffType) + tempAlgParams.buffInfo.scratchBuffBaseOff;
84 0 : uint64_t inputSliceStride = tempAlgParams.inputSliceStride;
85 0 : uint64_t outputSliceStride = tempAlgParams.outputSliceStride;
86 0 : u64 sliceSize = tempAlgParams.sliceSize;
87 :
88 : uint64_t token;
89 0 : CHK_RET(GetToken(op_, token));
90 0 : HCCL_INFO(
91 : "[CcuTempReduceScatterMesh1D2Die] inputAddr[%llu], outputAddr[%llu], scratchAddr0[%llu], "
92 : "inputSliceStride[%llu], outputSliceStride[%llu]",
93 : inputAddr, outputAddr, scratchAddr, inputSliceStride, outputSliceStride);
94 :
95 0 : u32 dieNum = 2;
96 0 : std::vector<std::vector<LinkData>> linksForDie(dieNum);
97 0 : std::vector<RankGroup> rankGroupForDie(dieNum);
98 0 : for (auto& pair : tempLinks) {
99 0 : const RankId rank = pair.first;
100 0 : const std::vector<LinkData>& links = pair.second;
101 0 : if (links.size() == 0) {
102 0 : continue;
103 : }
104 0 : const LinkData& link = links[0];
105 0 : u32 dieIdx = link.GetLocalDieId();
106 0 : linksForDie[dieIdx].push_back(link);
107 0 : rankGroupForDie[dieIdx].AddRank(rank);
108 : }
109 0 : for (auto& rankGroup : rankGroupForDie) {
110 0 : rankGroup.AddRank(myRank_);
111 : }
112 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
113 0 : for (u32 die = 0; die < dieNum; die++) {
114 0 : bool rmtReduceWithMyRank = linksForDie[die].size() > linksForDie[1 - die].size() ? false : true;
115 0 : CcuInstructionReduceScatterMesh1D2Die ccuInstruction;
116 0 : ccuInstruction.Init(
117 0 : tempVirtRankMap_[myRank_], op_, rmtReduceWithMyRank, tempVTopo_, inputAddr, outputAddr, scratchAddr,
118 : sliceSize, token);
119 0 : ccuInstruction.SetLinks(linksForDie[die]);
120 0 : ccuInstruction.SetRankGroup(rankGroupForDie[die]);
121 0 : ccuInstruction.SetCntCkeNum(3);
122 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionReduceScatterMesh1D2Die>(ccuInstruction)));
123 0 : }
124 0 : tempInsQues[0]->Append(std::move(insGroupPtr));
125 :
126 0 : DataSlice srcSlice(tempAlgParams.buffInfo.scratBuffType, tempAlgParams.buffInfo.scratchBuffBaseOff, sliceSize);
127 0 : DataSlice dstSlice(tempAlgParams.buffInfo.outBuffType, tempAlgParams.buffInfo.outBuffBaseOff, sliceSize);
128 0 : LocalReduce(tempInsQues[0], srcSlice, dstSlice, op_.dataType, op_.reduceOp);
129 :
130 0 : HCCL_INFO("[CcuTempReduceScatterMesh1D2Die] Template Run for all steps Ends.");
131 0 : return HcclResult::HCCL_SUCCESS;
132 0 : }
133 : } // namespace Hccl
|