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 <ios>
12 : #include <iostream>
13 :
14 : #include "log.h"
15 :
16 : #include "ccu_instruction_reduce_scatter_mesh1d_mem2mem.h"
17 : #include "ccu_rank_group.h"
18 : #include "ccu_ctx_creator_registry.h"
19 : #include "ccu_context_reduce_scatter_mesh1d_mem2mem.h"
20 : #include "ccu_temp_reduce_scatter_mesh_1D_mem2mem.h"
21 :
22 : namespace Hccl {
23 : static CcuInstRegister<CcuContextReduceScatterMeshMem2Mem1D> g_registrarReduceScatter(
24 : CcuInstType::CCU_REDUCE_SCATTER_MESH_1D_MEM2MEM);
25 :
26 0 : CcuTempReduceScatterMeshMem2Mem1D::CcuTempReduceScatterMeshMem2Mem1D(
27 : const RankId virtualRank, const u32 tempRankSize,
28 : const std::vector<std::vector<RankId>> &tempVTopo,
29 0 : const std::map<RankId, u32> &tempVirtRankMap)
30 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
31 : {
32 0 : }
33 :
34 0 : CcuTempReduceScatterMeshMem2Mem1D::~CcuTempReduceScatterMeshMem2Mem1D()
35 : {
36 0 : }
37 :
38 0 : void CcuTempReduceScatterMeshMem2Mem1D::InitReduceInfo(const ReduceOp &reduceOp, const DataType &dataType) {
39 0 : reduceOp_ = reduceOp;
40 0 : dataType_ = dataType;
41 0 : }
42 :
43 0 : HcclResult CcuTempReduceScatterMeshMem2Mem1D::CalcRes(AlgTempResReq &tempResReq)
44 : {
45 : // 按照IODienum来确定stream数量,支持1D和2D的template
46 0 : tempResReq.queNum = 1;
47 0 : tempResReq.streamNum = tempResReq.queNum;
48 0 : HCCL_DEBUG("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
49 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
50 0 : return HcclResult::HCCL_SUCCESS;
51 : }
52 :
53 0 : u32 CcuTempReduceScatterMeshMem2Mem1D::CalcScratchMultiple(BufferType input, BufferType output)
54 : {
55 : (void)input;
56 : (void)output;
57 : // one shot场景,scratch Buffer需要是usrIn的rankSize倍
58 0 : return tempRankSize_;
59 : }
60 :
61 0 : HcclResult CcuTempReduceScatterMeshMem2Mem1D::GenExtIns(const TempFuncs &tempFuncs,
62 : const TemplateDataParams &templateDataParams,
63 : const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues)
64 : {
65 0 : CHK_PRT_RET(tempInsQues.empty(),
66 : HCCL_ERROR("[CcuTempReduceScatterMeshMem2Mem1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
67 0 : CHK_PTR_NULL(tempInsQues[0]);
68 0 : opMode_ = tempFuncs.opMode;
69 0 : buffInfo_ = templateDataParams.buffInfo;
70 0 : CcuInstructionReduceScatterMeshMem2Mem1D ccuIns;
71 0 : std::vector<uint64_t> dimSize;
72 0 : dimSize.push_back(tempRankSize_);
73 :
74 0 : uint32_t rankId = myRank_;
75 0 : uint64_t repeatNumTmp = templateDataParams.repeatNum;
76 0 : const CollAlgOperator &op = op_;
77 0 : const std::vector<std::vector<RankId>> &tempVTopo = tempVTopo_;
78 0 : uint64_t inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
79 0 : uint64_t outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
80 0 : uint64_t token; CHK_RET(GetToken(op_, token));
81 0 : uint64_t scratchAddr = BufferTypeToAddr(buffInfo_.scratBuffType) + buffInfo_.scratchBuffBaseOff;
82 0 : uint64_t inputSliceStride = templateDataParams.inputSliceStride;
83 0 : uint64_t inputRepeatStride = templateDataParams.inputRepeatStride;
84 0 : uint64_t outputRepeatStride = templateDataParams.outputRepeatStride;
85 0 : uint64_t normalSliceSize = templateDataParams.sliceSize;
86 0 : uint64_t lastSliceSize = templateDataParams.tailSize;
87 :
88 0 : uint64_t repeatNum = UINT64_MAX - repeatNumTmp;
89 0 : ccuIns.Init(tempVirtRankMap_[rankId], repeatNum, op, tempVTopo, inputAddr, outputAddr, token, scratchAddr, inputSliceStride, inputRepeatStride,
90 : outputRepeatStride, normalSliceSize, lastSliceSize);
91 :
92 0 : HCCL_DEBUG("[CcuTempReduceScatterMeshMem2Mem1D] Run Init: rankId[%u], repeatNum[%u], inputAddr[%llu], "
93 : "outputAddr[%llu], scratchAddr[%llu], inputSliceStride[%llu], "
94 : "inputRepeatStride[%llu], outputRepeatStride[%llu], normalSliceSize[%llu], lastSliceSize[%llu]",
95 : rankId, repeatNum, inputAddr, outputAddr, scratchAddr, inputSliceStride, inputRepeatStride,
96 : outputRepeatStride, normalSliceSize, lastSliceSize);
97 :
98 0 : std::vector<LinkData> links;
99 0 : for (auto &pair : tempLinks) {
100 0 : if (pair.second.empty()) {
101 0 : continue;
102 : }
103 0 : links.push_back(pair.second[0]);
104 : }
105 0 : HCCL_DEBUG("[CcuTempReduceScatterMeshMem2Mem1D] links.size[%zu]", links.size());
106 0 : ccuIns.SetLinks(links);
107 :
108 0 : RankGroup rankGroup;
109 0 : for (auto &peer : tempVTopo_[0]) {
110 0 : rankGroup.AddRank(peer);
111 : }
112 0 : u32 cntCkeNum = 4; //context里要用的前后同步的信号数
113 0 : ccuIns.SetCntCkeNum(cntCkeNum);
114 0 : ccuIns.SetRankGroup(rankGroup);
115 0 : HCCL_DEBUG("CcuTempReduceScatterMeshMem2Mem1D is [%s]", ccuIns.Describe().c_str());
116 0 : ccuIns.Describe();
117 0 : tempInsQues[0]->Append(std::move(std::make_unique<CcuInstructionReduceScatterMeshMem2Mem1D>(ccuIns)));//将一个新创建的 CcuInstructionReduceScatterMesh1DMem2Mem 对象添加到 tempInsQues[0] 管理的指令队列
118 :
119 0 : return HcclResult::HCCL_SUCCESS;
120 0 : }
121 :
122 :
123 0 : HcclResult CcuTempReduceScatterMeshMem2Mem1D::GenExtIns(const RankGraph *rankGraph, const TemplateInfo &tmpInfo,
124 : const std::vector<InsQuePtr> &tempInsQues) const
125 : {
126 : (void)rankGraph;
127 : (void)tmpInfo;
128 : (void)tempInsQues;
129 : // 框架解析aicpuIns,算法的algCompnnetLite在device侧直接调用Run()
130 0 : return HcclResult::HCCL_SUCCESS;
131 : }
132 : } // namespace Hccl
|