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.h"
17 : #include "ccu_rank_group.h"
18 : #include "ccu_ctx_creator_registry.h"
19 : #include "ccu_context_all_reduce_mesh1d.h"
20 : #include "ccu_temp_all_reduce_mesh_1D.h"
21 :
22 : namespace Hccl {
23 : static CcuInstRegister<CcuContextAllReduceMesh1D> g_registrarAllReduce(CcuInstType::CCU_ALL_REDUCE_MESH_1D_DIRECT);
24 :
25 0 : CcuTempAllReduceMesh1D::CcuTempAllReduceMesh1D(
26 : const RankId virtualRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
27 0 : const std::map<RankId, u32>& tempVirtRankMap)
28 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
29 0 : {}
30 :
31 0 : CcuTempAllReduceMesh1D::~CcuTempAllReduceMesh1D() {}
32 :
33 0 : void CcuTempAllReduceMesh1D::InitReduceInfo(const ReduceOp& reduceOp, const DataType& dataType)
34 : {
35 0 : reduceOp_ = reduceOp;
36 0 : dataType_ = dataType;
37 0 : }
38 :
39 : HcclResult
40 0 : CcuTempAllReduceMesh1D::CalcSliceInfo(const AllignInfo& allignInfo, const u64 dataSize, RankSliceInfo& sliceInfoVec)
41 : {
42 0 : CHK_RET(CalcSliceInfoAllReduce(allignInfo, tempRankSize_, dataSize, sliceInfoVec));
43 0 : return HcclResult::HCCL_SUCCESS;
44 : }
45 :
46 0 : HcclResult CcuTempAllReduceMesh1D::CalcRes(AlgTempResReq& tempResReq)
47 : {
48 0 : tempResReq.queNum = 1;
49 0 : tempResReq.streamNum = tempResReq.queNum;
50 0 : HCCL_INFO("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
51 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
52 0 : return HcclResult::HCCL_SUCCESS;
53 : }
54 :
55 : /* CCU数据类型校验规则
56 : * Reduce算子:
57 : * 高精度模式,当dataType==outputDataType时,可选类型为FP32、FP16、BF16、UINT8、INT16、INT32;
58 : * 低精度模式,当dataType!=outputDataType时,dataType可选范围HIF8、E4M3、E5M2、INT8;outputDataType可选范围FP32、FP16、BF16;
59 : * 非Reduce算子:任意数据类型,dataType==outputDataType即可。
60 : */
61 0 : HcclResult CcuTempAllReduceMesh1D::CheckCcuDataType() const
62 : {
63 0 : if (op_.dataType == op_.outputDataType) {
64 : // allreduce算子高精度模式
65 0 : HCCL_INFO("HIGH PRECISION");
66 : set<DataType> highPrecisionSupportedInputDataType
67 0 : = {DataType::FP32, DataType::FP16, DataType::BFP16, DataType::UINT8, DataType::INT16, DataType::INT32};
68 0 : if (highPrecisionSupportedInputDataType.count(op_.dataType) == 0) {
69 0 : HCCL_ERROR(
70 : "Unsupported DataType [%s] For OpType [%s].", op_.dataType.Describe().c_str(),
71 : op_.opType.Describe().c_str());
72 0 : return HcclResult::HCCL_E_PARA;
73 : }
74 0 : } else if (op_.outputDataType != DataType::INVALID) {
75 : // allreduce算子低精度模式
76 0 : HCCL_INFO("LOW PRECISION");
77 0 : HCCL_ERROR(
78 : "Unsupported LOW PRECISION, Output DataType [%s] For OpType [%s].", op_.outputDataType.Describe().c_str(),
79 : op_.opType.Describe().c_str());
80 0 : return HcclResult::HCCL_E_PARA;
81 : }
82 0 : HCCL_INFO("CheckCcuDataType Success!");
83 0 : return HcclResult::HCCL_SUCCESS;
84 : }
85 :
86 0 : HcclResult CcuTempAllReduceMesh1D::Run(
87 : const TempFuncs& tempFuncs, const RankSliceInfo& sliceInfoVec, const BuffInfo& buffInfo, const ResLinks& tempLinks,
88 : std::vector<InsQuePtr>& tempInsQues)
89 : {
90 0 : CHK_PRT_RET(tempInsQues.empty(), HCCL_ERROR("[CcuTempAllReduceMesh1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
91 0 : CHK_PTR_NULL(tempInsQues[0]);
92 0 : opMode_ = tempFuncs.opMode;
93 0 : buffInfo_ = buffInfo;
94 0 : CcuInstructionAllReduceMesh1D ccuInsAllReduceMesh1D;
95 0 : std::vector<uint64_t> dimSize;
96 0 : dimSize.push_back(tempRankSize_);
97 : uint64_t inputAddr;
98 : uint64_t outputAddr;
99 :
100 0 : if (op_.outputDataType == DataType::INVALID) {
101 0 : op_.outputDataType = op_.dataType;
102 : }
103 0 : CHK_RET(CheckCcuDataType());
104 0 : if (opMode_ == OpMode::OPBASE) {
105 0 : if (tempFuncs.isForepart) {
106 0 : inputAddr = BufferTypeToAddr(tempFuncs.usrData.usrInSlices[0].GetType())
107 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
108 : } else {
109 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff;
110 : }
111 0 : if (tempFuncs.isBottom) {
112 0 : outputAddr = BufferTypeToAddr(tempFuncs.usrData.usrOutSlices[0].GetType())
113 0 : + tempFuncs.usrData.usrOutSlices[0].GetOffset();
114 : } else {
115 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff;
116 : }
117 : } else {
118 0 : inputAddr = BufferTypeToAddr(buffInfo_.inBuffType) + buffInfo_.inBuffBaseOff
119 0 : + tempFuncs.usrData.usrInSlices[0].GetOffset();
120 0 : outputAddr = BufferTypeToAddr(buffInfo_.outBuffType) + buffInfo_.outBuffBaseOff
121 0 : + tempFuncs.usrData.usrOutSlices[0].GetOffset();
122 : }
123 0 : HCCL_INFO("inputAddr[%llu], outputAddr[%llu]", inputAddr, outputAddr);
124 :
125 0 : uint64_t sliceSize = sliceInfoVec[myRank_][0].size; // 获取本rank需要处理的数据量
126 0 : uint64_t offset = sliceInfoVec[myRank_][0].offset; // 自己需要 reduce 的数据基于 inputAddr 的偏移
127 : uint64_t token;
128 0 : CHK_RET(GetToken(op_, token));
129 0 : ccuInsAllReduceMesh1D.Init(
130 0 : static_cast<uint32_t>(myRank_), inputAddr, outputAddr, sliceSize, offset, token, op_, tempVTopo_);
131 0 : HCCL_INFO(
132 : "[CcuTempAllReduceMesh1D] Run Init: myRank_[%d], dimSize[%llu], inputAddr[%llu], outputAddr[%llu],"
133 : "sliceSize[%llu], offset[%llu]",
134 : myRank_, dimSize[0], inputAddr, outputAddr, sliceSize, offset);
135 :
136 0 : std::vector<LinkData> links;
137 0 : for (auto& pair : tempLinks) {
138 0 : if (pair.second.empty()) {
139 0 : continue;
140 : }
141 0 : links.push_back(pair.second[0]);
142 : }
143 0 : HCCL_INFO("[CcuTempAllReduceMesh1D] links.size[%zu]", links.size());
144 0 : ccuInsAllReduceMesh1D.SetLinks(links);
145 :
146 0 : RankGroup rankGroup;
147 :
148 0 : for (auto& peer : tempVTopo_[0]) {
149 0 : rankGroup.AddRank(peer);
150 : }
151 0 : u32 cntCkeNum = 4;
152 0 : ccuInsAllReduceMesh1D.SetCntCkeNum(cntCkeNum);
153 0 : ccuInsAllReduceMesh1D.SetRankGroup(rankGroup);
154 0 : HCCL_INFO("CCUInsAllReducemesh1D is [%s]", ccuInsAllReduceMesh1D.Describe().c_str());
155 0 : tempInsQues[0]->Append(std::move(std::make_unique<CcuInstructionAllReduceMesh1D>(ccuInsAllReduceMesh1D)));
156 :
157 0 : return HcclResult::HCCL_SUCCESS;
158 0 : }
159 : } // namespace Hccl
|