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_temp_reduce_mesh_1D_two_shot_mem2mem.h"
17 : #include "alg_data_trans_wrapper.h"
18 : #include "ccu_instruction_reduce_mesh1d_two_shot_mem2mem.h"
19 : #include "ccu_rank_group.h"
20 : #include "ccu_ctx_creator_registry.h"
21 : #include "ccu_context_reduce_mesh1d_two_shot_mem2mem.h"
22 :
23 : namespace Hccl {
24 :
25 : static CcuInstRegister<CcuContextReduceMeshTwoShotMem2Mem1D> registrarReduce(CcuInstType::CCU_REDUCE_MESH_1D_TWO_SHOT_MEM2MEM);
26 :
27 0 : CcuTempReduceMeshTwoShotMem2Mem1D::CcuTempReduceMeshTwoShotMem2Mem1D(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 : CcuTempReduceMeshTwoShotMem2Mem1D::~CcuTempReduceMeshTwoShotMem2Mem1D()
35 : {
36 0 : }
37 :
38 0 : void CcuTempReduceMeshTwoShotMem2Mem1D::InitReduceInfo(const ReduceOp &reduceOp, const DataType &dataType)
39 : {
40 0 : reduceOp_ = reduceOp;
41 0 : dataType_ = dataType;
42 0 : }
43 :
44 0 : HcclResult CcuTempReduceMeshTwoShotMem2Mem1D::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 : "[CcuTempReduceMeshTwoShotMem2Mem1D] 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 CcuTempReduceMeshTwoShotMem2Mem1D::CalcRes(AlgTempResReq &tempResReq)
71 : {
72 0 : tempResReq.queNum = 1;
73 0 : tempResReq.streamNum = tempResReq.queNum + 1; // 多申请一个 stream 给 ccuInsGroup
74 0 : HCCL_INFO("[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 : HcclResult CcuTempReduceMeshTwoShotMem2Mem1D::GenExtIns(const TempFuncs &tempFuncs,
80 : const TemplateDataParams &templateDataParams,
81 : const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues)
82 : {
83 0 : buffInfo_ = templateDataParams.buffInfo;
84 0 : opMode_ = tempFuncs.opMode;
85 0 : CcuInstructionReduceMeshTwoShotMem2Mem1D ccuIns;
86 0 : std::vector<uint64_t> dimSize;
87 0 : dimSize.push_back(tempRankSize_);
88 0 : RankSliceInfo sliceInfoVec;
89 0 : CHK_RET(CalcSlice(templateDataParams.sliceSize, sliceInfoVec));
90 :
91 0 : uint32_t rankId = tempVirtRankMap_[myRank_];
92 0 : uint32_t rootId = tempVirtRankMap_[rootId_];
93 :
94 0 : const CollAlgOperator &op = op_;
95 0 : const std::vector<std::vector<RankId>> &tempVTopo = tempVTopo_;
96 0 : uint64_t inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
97 0 : uint64_t outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
98 0 : uint64_t scratchAddr = BufferTypeToAddr(buffInfo_.scratBuffType) + buffInfo_.scratchBuffBaseOff;
99 : uint64_t token;
100 0 : CHK_RET(GetToken(op_, token));
101 0 : uint64_t normalSliceSize = sliceInfoVec[0][0].size;
102 0 : uint64_t lastSliceSize = sliceInfoVec[tempRankSize_ - 1][0].size;
103 0 : uint64_t mySliceSize = sliceInfoVec[rankId][0].size;
104 :
105 0 : ccuIns.Init(rankId, rootId, op, tempVTopo, inputAddr, outputAddr, scratchAddr, token,
106 : normalSliceSize, lastSliceSize, mySliceSize);
107 :
108 0 : HCCL_INFO("[CcuTempReduceMeshTwoShotMem2Mem1D] Run Init: rankId[%u], rootId[%u], inputAddr[%llu], outputAddr[%llu],"
109 : "scratchAddr[%llu], normalSliceSize[%llu], lastSliceSize[%llu], mySliceSize[%llu]",
110 : rankId, rootId, inputAddr, outputAddr, scratchAddr, normalSliceSize, lastSliceSize, mySliceSize);
111 :
112 0 : std::vector<LinkData> links;
113 0 : for (auto &pair : tempLinks) {
114 0 : if (pair.second.empty()) {
115 0 : continue;
116 : }
117 0 : links.push_back(pair.second[0]);
118 : }
119 0 : HCCL_DEBUG("[CcuTempReduceMeshTwoShotMem2Mem1D] links.size[%llu]", links.size());
120 0 : ccuIns.SetLinks(links);
121 :
122 0 : RankGroup rankGroup;
123 0 : for (auto &peer : tempVTopo_[0]) {
124 0 : rankGroup.AddRank(peer);
125 : }
126 0 : u32 cntCkeNum = 4;
127 0 : ccuIns.SetCntCkeNum(cntCkeNum);
128 0 : ccuIns.SetRankGroup(rankGroup);
129 0 : HCCL_DEBUG("CcuTempReduceMeshTwoShotMem2Mem1D is [%s]", ccuIns.Describe().c_str());
130 0 : tempInsQues[0]->Append(std::move(std::make_unique<CcuInstructionReduceMeshTwoShotMem2Mem1D>(ccuIns)));
131 :
132 0 : return HcclResult::HCCL_SUCCESS;
133 0 : }
134 :
135 0 : HcclResult CcuTempReduceMeshTwoShotMem2Mem1D::GenExtIns(const RankGraph *rankGraph,
136 : const TemplateInfo &tmpInfo,
137 : const std::vector<InsQuePtr> &tempInsQues) const
138 : {
139 : (void)rankGraph;
140 : (void)tmpInfo;
141 : (void)tempInsQues;
142 : // 框架解析aicpuIns,算法的algCompnnetLite在device侧直接调用Run()
143 0 : return HcclResult::HCCL_SUCCESS;
144 : }
145 :
146 : } // namespace Hccl
|