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