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.h"
17 : #include "ccu_rank_group.h"
18 : #include "ccu_ctx_creator_registry.h"
19 : #include "ccu_context_reduce_scatter_mesh1d.h"
20 : #include "ccu_temp_reduce_scatter_mesh_1D.h"
21 :
22 : namespace Hccl {
23 :
24 : static CcuInstRegister<CcuContextReduceScatterMesh1D> g_registrarReduceScatter(
25 : CcuInstType::CCU_REDUCE_SCATTER_MESH_1D_DIRECT);
26 :
27 0 : CcuTempReduceScatterMesh1D::CcuTempReduceScatterMesh1D(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 : CcuTempReduceScatterMesh1D::~CcuTempReduceScatterMesh1D()
35 : {
36 0 : }
37 :
38 0 : void CcuTempReduceScatterMesh1D::InitReduceInfo(const ReduceOp &reduceOp, const DataType &dataType) {
39 0 : reduceOp_ = reduceOp;
40 0 : dataType_ = dataType;
41 0 : }
42 :
43 0 : HcclResult CcuTempReduceScatterMesh1D::CalcSliceInfo(const AllignInfo &allignInfo, const u64 dataSize,
44 : RankSliceInfo &sliceInfoVec)
45 : {
46 0 : std::vector<SliceInfo> tmp(tempVTopo_.size());
47 0 : sliceInfoVec.resize(tempRankSize_, tmp);
48 0 : CHK_RET(CalcRsAgSliceInfoMesh(myRank_, tempRankSize_, allignInfo, dataSize, sliceInfoVec));
49 0 : return HcclResult::HCCL_SUCCESS;
50 0 : }
51 :
52 0 : HcclResult CcuTempReduceScatterMesh1D::CalcRes(AlgTempResReq &tempResReq)
53 : {
54 0 : tempResReq.queNum = 1;
55 0 : tempResReq.streamNum = tempResReq.queNum;
56 0 : HCCL_INFO("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
57 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
58 0 : return HcclResult::HCCL_SUCCESS;
59 : }
60 :
61 0 : uint64_t CcuTempReduceScatterMesh1D::GetMaxSliceSize() const
62 : {
63 0 : return UB_MAX_DATA_SIZE;
64 : }
65 :
66 : /* CCU数据类型校验规则
67 : * Reduce算子:
68 : * 高精度模式,当dataType==outputDataType时,可选类型为FP32、FP16、BF16、UINT8、INT16、INT32;
69 : * 低精度模式,当dataType!=outputDataType时,dataType可选范围HIF8、E4M3、E5M2、INT8;outputDataType可选范围FP32、FP16、BF16;
70 : * 非Reduce算子:任意数据类型,dataType==outputDataType即可。
71 : */
72 0 : void CcuTempReduceScatterMesh1D::CheckCcuDataType() const
73 : {
74 0 : if (op_.opType == OpType::REDUCESCATTER && op_.reduceOp == ReduceOp::SUM) {
75 0 : if (op_.dataType == op_.outputDataType) {
76 : // reduce算子高精度模式
77 0 : HCCL_INFO("HIGH PRECISION");
78 : set<DataType> highPrecisionSupportedInputDataType
79 : = {DataType::FP32, DataType::FP16, DataType::BFP16, DataType::UINT8,
80 0 : DataType::UINT8, DataType::INT16, DataType::INT32};
81 0 : if (highPrecisionSupportedInputDataType.count(op_.dataType) == 0) {
82 0 : THROW<CcuApiException>(StringFormat("Unsupported DataType [%s] For OpType [%s].",
83 0 : op_.dataType.Describe().c_str(), op_.opType.Describe().c_str()));
84 : }
85 0 : } else {
86 : // reduce算子的低精度模式
87 0 : HCCL_INFO("LOW PRECISION");
88 : set<DataType> lowPrecisionSupportedInputDataType
89 0 : = {DataType::HIF8, DataType::FP8E4M3, DataType::FP8E5M2, DataType::INT8};
90 : set<DataType> lowPrecisionSupportedOutputDataType
91 0 : = {DataType::FP32, DataType::FP16, DataType::BFP16};
92 0 : if (lowPrecisionSupportedInputDataType.count(op_.dataType) == 0) {
93 0 : THROW<CcuApiException>(StringFormat("Unsupported Input DataType [%s] For OpType [%s].",
94 0 : op_.dataType.Describe().c_str(), op_.opType.Describe().c_str()));
95 : }
96 0 : if (lowPrecisionSupportedOutputDataType.count(op_.outputDataType) == 0) {
97 0 : THROW<CcuApiException>(StringFormat("Unsupported Output DataType [%s] For OpType [%s].",
98 0 : op_.outputDataType.Describe().c_str(), op_.opType.Describe().c_str()));
99 : }
100 0 : }
101 : } else {
102 0 : if (op_.dataType != op_.outputDataType) {
103 0 : THROW<CcuApiException>(StringFormat("Inconsistent DataType[%s]--OutputDataType[%s] for OpType[%s].",
104 0 : op_.dataType.Describe().c_str(), op_.outputDataType.Describe().c_str(), op_.opType.Describe().c_str()));
105 : }
106 : }
107 0 : HCCL_INFO("CheckCcuDataType Success!");
108 0 : }
109 :
110 :
111 0 : HcclResult CcuTempReduceScatterMesh1D::Run(const TempFuncs &tempFuncs, const RankSliceInfo &sliceInfoVec,
112 : const BuffInfo &buffInfo, const ResLinks &tempLinks,
113 : std::vector<InsQuePtr> &tempInsQues)
114 : {
115 0 : CHK_PRT_RET(tempInsQues.empty(),
116 : HCCL_ERROR("[CcuTempReduceScatterMesh1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
117 0 : CHK_PTR_NULL(tempInsQues[0]);
118 0 : opMode_ = tempFuncs.opMode;
119 0 : buffInfo_ = buffInfo;
120 0 : CcuInstructionReduceScatterMesh1D ccuInsReduceScatterMesh1D;
121 0 : std::vector<uint64_t> dimSize;
122 0 : dimSize.push_back(tempRankSize_);
123 :
124 : uint64_t inputAddr;
125 : uint64_t outputAddr;
126 : uint64_t offset;
127 0 : if (op_.outputDataType == DataType::INVALID) {
128 0 : op_.outputDataType = op_.dataType;
129 : }
130 :
131 0 : uint64_t expandingtimes = DataTypeSizeGet(op_.outputDataType) / DataTypeSizeGet(op_.dataType); // 膨胀的倍数是输出类型/输入类型
132 0 : HCCL_INFO("[CcuTempReduceScatterMesh1D] dataType outputDatatype %s %s", op_.dataType.Describe().c_str(),
133 : op_.outputDataType.Describe().c_str());
134 0 : CheckCcuDataType();
135 0 : if (opMode_ == OpMode::OPBASE) {
136 0 : if (tempFuncs.isForepart) {
137 0 : inputAddr = BufferTypeToAddr(tempFuncs.usrData.usrInSlices[myRank_].GetType());
138 : // 需要加上UserIn的偏移,包含了loop偏移和rank偏移
139 0 : offset = tempFuncs.usrData.usrInSlices[myRank_].GetOffset();
140 : } else {
141 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
142 : // 从inBuff获取数据,只需要加rank偏移
143 0 : offset = sliceInfoVec[myRank_][0].offset;
144 : }
145 0 : if (tempFuncs.isBottom) {
146 0 : outputAddr = BufferTypeToAddr(tempFuncs.usrData.usrOutSlices[0].GetType())
147 0 : + (tempFuncs.usrData.usrOutSlices[0].GetOffset()) * expandingtimes;
148 : } else {
149 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff * expandingtimes;
150 : }
151 : } else {
152 : // 图模式没有tempFuncs.usrData,直接通过buffInfo_来获取输入输出地址
153 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
154 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff + (tempFuncs.usrData.usrOutSlices[0].GetOffset());
155 0 : offset = tempFuncs.usrData.usrInSlices[myRank_].GetOffset();
156 : }
157 0 : uint64_t sliceSize = sliceInfoVec[myRank_][0].size; // 获取本rank需要处理的数据量
158 : uint64_t token;
159 0 : CHK_RET(GetToken(op_, token));
160 0 : ccuInsReduceScatterMesh1D.Init(static_cast<uint32_t>(myRank_), inputAddr, outputAddr, sliceSize, offset, token, op_, tempVTopo_);
161 0 : HCCL_INFO("[CcuTempReduceScatterMesh1D] Run Init: myRank_[%d], dimSize[%llu], inputAddr[%llu],"\
162 : "outputAddr[%llu], sliceSize[%llu], offset[%llu]",
163 : myRank_, dimSize[0], inputAddr, outputAddr, sliceSize, offset);
164 :
165 0 : std::vector<LinkData> links;
166 :
167 0 : for (auto &pair : tempLinks) {
168 0 : if (pair.second.empty()) {
169 0 : continue;
170 : }
171 0 : links.push_back(pair.second[0]);
172 : }
173 0 : HCCL_INFO("[CcuTempReduceScatterMesh1D] links.size[%zu]", links.size());
174 0 : ccuInsReduceScatterMesh1D.SetLinks(links);
175 0 : RankGroup rankGroup;
176 :
177 0 : for (auto &peer : tempVTopo_[0]) {
178 0 : rankGroup.AddRank(peer);
179 : }
180 0 : u32 cntCkeNum = 3;
181 0 : ccuInsReduceScatterMesh1D.SetCntCkeNum(cntCkeNum);
182 0 : ccuInsReduceScatterMesh1D.SetRankGroup(rankGroup);
183 0 : ccuInsReduceScatterMesh1D.Describe();
184 0 : tempInsQues[0]->Append(std::move(std::make_unique<CcuInstructionReduceScatterMesh1D>(ccuInsReduceScatterMesh1D)));
185 :
186 0 : return HcclResult::HCCL_SUCCESS;
187 0 : }
188 :
189 0 : HcclResult CcuTempReduceScatterMesh1D::GenExtIns(const RankGraph *rankGraph, const TemplateInfo &tmpInfo,
190 : const std::vector<InsQuePtr> &tempInsQues) const
191 : {
192 : (void)rankGraph;
193 : (void)tmpInfo;
194 : (void)tempInsQues;
195 : // 框架解析aicpuIns,算法的algCompnnetLite在device侧直接调用Run()
196 0 : return HcclResult::HCCL_SUCCESS;
197 : }
198 :
199 : } // namespace Hccl
|