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_all_reduce_mesh1d_mem2mem.h"
17 : #include "ccu_rank_group.h"
18 : #include "ccu_ctx_creator_registry.h"
19 : #include "ccu_context_all_reduce_mesh1d_mem2mem.h"
20 : #include "ccu_temp_all_reduce_mesh_1D_mem2mem.h"
21 :
22 : namespace Hccl {
23 :
24 : static CcuInstRegister<CcuContextAllReduceMeshMem2Mem1D>
25 : g_registrarAllReduce(CcuInstType::CCU_ALL_REDUCE_MESH_1D_MEM2MEM);
26 :
27 0 : CcuTempAllReduceMeshMem2Mem1D::CcuTempAllReduceMeshMem2Mem1D(
28 : const RankId virtualRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
29 0 : const std::map<RankId, u32>& tempVirtRankMap)
30 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
31 0 : {}
32 :
33 0 : CcuTempAllReduceMeshMem2Mem1D::~CcuTempAllReduceMeshMem2Mem1D() {}
34 :
35 0 : void CcuTempAllReduceMeshMem2Mem1D::InitReduceInfo(const ReduceOp& reduceOp, const DataType& dataType)
36 : {
37 0 : reduceOp_ = reduceOp;
38 0 : dataType_ = dataType;
39 0 : }
40 :
41 0 : HcclResult CcuTempAllReduceMeshMem2Mem1D::CalcSlice(const u64 dataSize, RankSliceInfo& sliceInfoVec)
42 : {
43 0 : std::vector<SliceInfo> tmp(tempVTopo_.size());
44 0 : sliceInfoVec.resize(tempRankSize_, tmp);
45 :
46 0 : u64 unitAllignSize = DataTypeSizeGet(dataType_);
47 0 : u64 chunkSize = RoundUp(dataSize, (tempRankSize_ * unitAllignSize)) * unitAllignSize;
48 0 : HCCL_INFO(
49 : "chunkSize[%llu], dataSize[%llu], tempRankSize_[%u], unitAllignSize[%llu]", chunkSize, dataSize, tempRankSize_,
50 : unitAllignSize);
51 0 : u64 accumOff = 0;
52 0 : for (u32 rankIdx = 0; rankIdx < tempRankSize_; rankIdx++) {
53 0 : u64 currChunkSize = ((dataSize - accumOff) > chunkSize) ? chunkSize : (dataSize - accumOff);
54 0 : SliceInfo slice = {accumOff, currChunkSize};
55 0 : sliceInfoVec[rankIdx][0] = slice;
56 0 : accumOff += currChunkSize;
57 : }
58 :
59 0 : CHK_PRT_RET(
60 : (sliceInfoVec[tempRankSize_ - 1][0].offset + sliceInfoVec[tempRankSize_ - 1][0].size != dataSize),
61 : HCCL_ERROR(
62 : "[CcuTempAllReduceMeshMem2Mem1D] chunkSize:[%llu], Rank:[%d], SliceInfo calculation error!", chunkSize,
63 : myRank_),
64 : HcclResult::HCCL_E_INTERNAL);
65 0 : return HcclResult::HCCL_SUCCESS;
66 0 : }
67 :
68 0 : HcclResult CcuTempAllReduceMeshMem2Mem1D::CalcRes(AlgTempResReq& tempResReq)
69 : {
70 0 : tempResReq.queNum = 1;
71 0 : tempResReq.streamNum = tempResReq.queNum;
72 0 : HCCL_DEBUG("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
73 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
74 0 : return HcclResult::HCCL_SUCCESS;
75 : }
76 :
77 0 : u32 CcuTempAllReduceMeshMem2Mem1D::CalcScratchMultiple(BufferType input, BufferType output)
78 : {
79 : (void)input;
80 : (void)output;
81 0 : return tempRankSize_;
82 : }
83 :
84 0 : HcclResult CcuTempAllReduceMeshMem2Mem1D::GenExtIns(
85 : const TempFuncs& tempFuncs, const TemplateDataParams& templateDataParams, const ResLinks& tempLinks,
86 : std::vector<InsQuePtr>& tempInsQues)
87 : {
88 0 : CHK_PRT_RET(
89 : tempInsQues.empty(), HCCL_ERROR("[CcuTempAllReduceMeshMem2Mem1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
90 0 : CHK_PTR_NULL(tempInsQues[0]);
91 0 : opMode_ = tempFuncs.opMode;
92 0 : buffInfo_ = templateDataParams.buffInfo;
93 0 : CcuInstructionAllReduceMeshMem2Mem1D ccuIns;
94 0 : std::vector<uint64_t> dimSize;
95 0 : dimSize.push_back(tempRankSize_);
96 :
97 0 : if (templateDataParams.sliceSize == 0) {
98 0 : HCCL_INFO("[CcuTempAllReduceMeshMem2Mem1D] dataCount == 0, Template Run Ends.");
99 0 : return HCCL_SUCCESS;
100 : }
101 :
102 0 : RankSliceInfo sliceInfoVec;
103 0 : CHK_RET(CalcSlice(templateDataParams.sliceSize, sliceInfoVec));
104 :
105 0 : uint32_t virtRankId = tempVirtRankMap_[myRank_];
106 0 : const CollAlgOperator& op = op_;
107 0 : const std::vector<std::vector<RankId>>& tempVTopo = tempVTopo_;
108 0 : uint64_t inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
109 0 : uint64_t outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
110 : uint64_t token;
111 0 : CHK_RET(GetToken(op_, token));
112 0 : uint64_t scratchAddr = BufferTypeToAddr(buffInfo_.scratBuffType) + buffInfo_.scratchBuffBaseOff;
113 0 : uint64_t inputSliceStride = templateDataParams.inputSliceStride;
114 0 : uint64_t outputSliceStride = templateDataParams.outputSliceStride;
115 0 : uint64_t inputRepeatStride = templateDataParams.inputRepeatStride;
116 0 : uint64_t outputRepeatStride = templateDataParams.outputRepeatStride;
117 0 : uint64_t normalSliceSize = sliceInfoVec[0][0].size;
118 0 : uint64_t lastSliceSize = sliceInfoVec[tempRankSize_ - 1][0].size;
119 0 : uint64_t mySliceSize = sliceInfoVec[virtRankId][0].size;
120 0 : uint64_t isInputOutputEqual = (inputAddr == outputAddr) ? 1 : 0;
121 :
122 0 : ccuIns.Init(
123 : virtRankId, op, tempVTopo, inputAddr, outputAddr, token, scratchAddr, inputSliceStride, outputSliceStride,
124 : inputRepeatStride, outputRepeatStride, normalSliceSize, lastSliceSize, mySliceSize, isInputOutputEqual);
125 :
126 0 : HCCL_DEBUG(
127 : "[CcuTempAllReduceMeshMem2Mem1D] Run Init: virtRankId[%u], inputAddr[%llu], "
128 : "outputAddr[%llu], scratchAddr[%llu], inputSliceStride[%llu], outputSliceStride[%llu], "
129 : "inputRepeatStride[%llu], outputRepeatStride[%llu], normalSliceSize[%llu], lastSliceSize[%llu], "
130 : "mySliceSize[%llu], isInputOutputEqual[%llu]",
131 : virtRankId, inputAddr, outputAddr, scratchAddr, inputSliceStride, outputSliceStride, inputRepeatStride,
132 : outputRepeatStride, normalSliceSize, lastSliceSize, mySliceSize, isInputOutputEqual);
133 :
134 0 : std::vector<LinkData> links;
135 0 : for (auto& pair : tempLinks) {
136 0 : if (pair.second.empty()) {
137 0 : continue;
138 : }
139 0 : links.push_back(pair.second[0]);
140 : }
141 0 : HCCL_DEBUG("[CcuTempAllReduceMeshMem2Mem1D] localRank[%d], links.size[%zu]", myRank_, links.size());
142 0 : ccuIns.SetLinks(links);
143 :
144 0 : RankGroup rankGroup;
145 0 : for (auto& peer : tempVTopo_[0]) {
146 0 : rankGroup.AddRank(peer);
147 : }
148 0 : u32 cntCkeNum = 4;
149 :
150 0 : ccuIns.SetCntCkeNum(cntCkeNum);
151 0 : ccuIns.SetRankGroup(rankGroup);
152 0 : HCCL_DEBUG("CCUInsAllReduceMeshMem2Mem1D is [%s]", ccuIns.Describe().c_str());
153 0 : tempInsQues[0]->Append(std::move(std::make_unique<CcuInstructionAllReduceMeshMem2Mem1D>(ccuIns)));
154 :
155 0 : return HcclResult::HCCL_SUCCESS;
156 0 : }
157 :
158 0 : HcclResult CcuTempAllReduceMeshMem2Mem1D::GenExtIns(
159 : const RankGraph* rankGraph, const TemplateInfo& tmpInfo, const std::vector<InsQuePtr>& tempInsQues) const
160 : {
161 : (void)rankGraph;
162 : (void)tmpInfo;
163 : (void)tempInsQues;
164 : // 框架解析aicpuIns,算法的algCompnnetLite在device侧直接调用Run()
165 0 : return HcclResult::HCCL_SUCCESS;
166 : }
167 :
168 : } // namespace Hccl
|