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