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 "ccu_context_reduce_scatter_v_mesh1d_mem2mem.h"
12 : #include "ccu_instruction_reduce_scatter_v_mesh1d_mem2mem.h"
13 :
14 : namespace Hccl {
15 :
16 : constexpr int INPUT_XN_ID = 0;
17 : constexpr int SCRATCH_XN_ID = 1;
18 : constexpr int TOKEN_XN_ID = 2;
19 : constexpr int CKE_IDX_0 = 0;
20 : constexpr int CKE_IDX_1 = 1;
21 : constexpr int CKE_IDX_2 = 2;
22 : constexpr int CKE_IDX_3 = 3;
23 :
24 0 : CcuContextReduceScatterVMeshMem2Mem1D::CcuContextReduceScatterVMeshMem2Mem1D(const CcuCtxArg &arg,
25 : const std::vector<CcuTransport *> &transports,
26 0 : const CcuTransportGroup &group)
27 0 : : CcuContextAlgBase(arg, transports, group)
28 : {
29 0 : const CcuCtxArgReduceScatterVMeshMem2Mem1D *ctxArg = dynamic_cast<const CcuCtxArgReduceScatterVMeshMem2Mem1D *>(&arg);
30 0 : if (ctxArg == nullptr) {
31 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterVMeshMem2Mem1D::ctxArg ptr is null"));
32 : }
33 0 : rankId_ = ctxArg->rankId_;
34 0 : rankSize_ = ctxArg->dimSize_[0];
35 0 : dataType_ = ctxArg->op_.dataType;
36 0 : outputDataType_ = ctxArg->op_.outputDataType;
37 0 : if (outputDataType_ == DataType::INVALID) {
38 0 : outputDataType_ = dataType_;
39 0 : HCCL_INFO("[CcuContextReduceScatterVMeshMem2Mem1D] outputDataType is [INVALID], set outputDataType to[%s]",
40 : outputDataType_.Describe().c_str());
41 : }
42 0 : reduceOp_ = ctxArg->op_.reduceOp;
43 0 : HCCL_INFO("[CcuContextReduceScatterVMeshMem2Mem1D] Init, CtxArgs are rankId[%u], rankSize_[%llu], dataType[%s], "
44 : "outputDataType[%s], reduceOp[%s]", rankId_, rankSize_, dataType_.Describe().c_str(),
45 : outputDataType_.Describe().c_str(), reduceOp_.Describe().c_str());
46 0 : }
47 :
48 0 : void CcuContextReduceScatterVMeshMem2Mem1D::InitResources()
49 : {
50 0 : uint16_t transportIdx = 0;
51 0 : if (transports.size() == 0) {
52 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterVMeshMem2Mem1D transports is empty"));
53 : }
54 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
55 0 : for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
56 0 : if (peerId == rankId_) {
57 0 : input_.push_back(CreateVariable());
58 0 : scratch_.push_back(CreateVariable());
59 0 : token_.push_back(CreateVariable());
60 : } else {
61 0 : HCCL_INFO("[CcuContextReduceScatterVMeshMem2Mem1D] MyRank[%u], PeerId[%llu], TransportId[%u]",
62 : rankId_, peerId, transportIdx);
63 0 : CHK_PRT_RET(transports[transportIdx] == nullptr,
64 : HCCL_ERROR("[CcuContextReduceScatterVMeshMem2Mem1D] Algorithm transport ptr is null"),);
65 0 : input_.push_back(CreateVariable((*transports[transportIdx]), INPUT_XN_ID)); // 获取transport中id=1的Var来传递output
66 0 : scratch_.push_back(CreateVariable((*transports[transportIdx]), SCRATCH_XN_ID));
67 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
68 0 : transportIdx++;
69 : }
70 : }
71 0 : output_ = CreateVariable();
72 0 : scratchInterval_ = CreateVariable();
73 0 : sliceSize_ = CreateVariable();
74 0 : offset_ = CreateVariable();
75 0 : return;
76 : }
77 :
78 0 : void CcuContextReduceScatterVMeshMem2Mem1D::CollectAllRanksSlice(std::vector<CcuRep::Memory>& tmpSrc,
79 : std::vector<CcuRep::Memory>& tmpDst, const CcuRep::MaskSignal &locMask)
80 : {
81 0 : uint16_t allBit = (1 << rankSize_) - 1; // 等待包含自身的全部对端
82 0 : u32 transportId = 0;
83 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
84 0 : if (rankIdx == rankId_) {
85 0 : LocalCopy(tmpDst[rankIdx], tmpSrc[rankIdx],
86 0 : sliceSize_, locMask, 1 << rankIdx);
87 : } else {
88 0 : Read(*transports[transportId], tmpDst[rankIdx], tmpSrc[rankIdx],
89 0 : sliceSize_, locMask, 1 << rankIdx);
90 0 : transportId++;
91 : }
92 : }
93 : // 等读完所有对端
94 0 : LocalWait(locMask, allBit);
95 0 : }
96 :
97 0 : void CcuContextReduceScatterVMeshMem2Mem1D::PrepareReduceScatterVData(std::vector<CcuRep::Memory>& reduceScatterVSrc,
98 : std::vector<CcuRep::Memory>& reduceScatterVDst)
99 : {
100 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
101 0 : reduceScatterVSrc.push_back(CreateMemory());
102 0 : reduceScatterVDst.push_back(CreateMemory());
103 : }
104 :
105 0 : CcuRep::Variable scratchOffset = CreateVariable();
106 0 : scratchOffset = 0;
107 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
108 0 : reduceScatterVSrc[rankIdx].addr = input_[rankIdx];
109 0 : reduceScatterVSrc[rankIdx].addr += offset_;
110 0 : reduceScatterVSrc[rankIdx].token = token_[rankIdx];
111 :
112 0 : reduceScatterVDst[rankIdx].addr = scratch_[rankId_];
113 0 : reduceScatterVDst[rankIdx].addr += scratchOffset;
114 0 : scratchOffset += scratchInterval_;
115 0 : reduceScatterVDst[rankIdx].token = token_[rankId_];
116 : }
117 0 : return;
118 0 : }
119 :
120 0 : void CcuContextReduceScatterVMeshMem2Mem1D::Algorithm()
121 : {
122 0 : HCCL_INFO("[CcuContextReduceScatterVMeshMem2Mem1D] ReduceScatterVMeshMem2Mem1D run");
123 0 : uint16_t selfBit = 1 << rankId_;
124 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
125 :
126 0 : InitResources();
127 :
128 0 : Load(input_[rankId_]);
129 0 : Load(output_);
130 0 : Load(token_[rankId_]);
131 0 : Load(scratch_[rankId_]);
132 0 : Load(scratchInterval_);
133 0 : Load(sliceSize_);
134 0 : Load(offset_);
135 :
136 0 : for (auto t : transports) {
137 0 : WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit);
138 0 : WriteVariableWithSignal(*t, scratch_[rankId_], SCRATCH_XN_ID, CKE_IDX_2, selfBit);
139 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_3, selfBit);
140 : }
141 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit);
142 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit);
143 0 : GroupWait(*transportGroup, CKE_IDX_3, allBit);
144 :
145 0 : CCU_IF(sliceSize_ != 0) {
146 0 : std::vector<CcuRep::Memory> reduceScatterVSrc;
147 0 : std::vector<CcuRep::Memory> reduceScatterVDst;
148 :
149 0 : CcuRep::MaskSignal locMask = CreateMaskSignal();
150 0 : PrepareReduceScatterVData(reduceScatterVSrc, reduceScatterVDst);
151 0 : CollectAllRanksSlice(reduceScatterVSrc, reduceScatterVDst, locMask);
152 :
153 0 : for(uint32_t rankIdx = 1; rankIdx < rankSize_; rankIdx++) {
154 0 : LocalReduce(reduceScatterVDst[0], reduceScatterVDst[rankIdx], sliceSize_, dataType_, reduceOp_, locMask, 1);
155 0 : LocalWait(locMask, 1);
156 : }
157 :
158 0 : CcuRep::Memory outDst = CreateMemory();
159 0 : outDst.addr = output_;
160 0 : outDst.token = token_[rankId_];
161 0 : LocalCopy(outDst, reduceScatterVDst[0], sliceSize_, locMask, 1 << rankId_);
162 0 : LocalWait(locMask, 1 << rankId_);
163 0 : }
164 0 : for (auto t : transports) {
165 0 : RemotePost(*t, CKE_IDX_0, selfBit);
166 : }
167 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit);
168 0 : HCCL_INFO("[CcuContextReduceScatterVMeshMem2Mem1D] ReduceScatterVMeshMem2Mem1D end");
169 0 : return;
170 : }
171 :
172 0 : std::vector<uint64_t> CcuContextReduceScatterVMeshMem2Mem1D::GeneArgs(const CcuTaskArg &arg)
173 : {
174 0 : const CcuTaskArgReduceScatterVMeshMem2Mem1D *taskArg = dynamic_cast<const CcuTaskArgReduceScatterVMeshMem2Mem1D *>(&arg);
175 0 : if (taskArg == nullptr) {
176 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterVMeshMem2Mem1D::taskArg ptr is null"));
177 : }
178 0 : uint64_t inputAddr = taskArg->inputAddr_;
179 0 : uint64_t outputAddr = taskArg->outputAddr_;
180 0 : uint64_t tokenInfo = taskArg->token_;
181 0 : uint64_t scratchAddr = taskArg->scratchAddr_;
182 0 : uint64_t scratchInterval = taskArg->scratchInterval_;
183 0 : uint64_t sliceSize = taskArg->sliceSize_;
184 0 : uint64_t offset = taskArg->offset_;
185 0 : return {inputAddr, outputAddr, tokenInfo, scratchAddr, scratchInterval, sliceSize, offset};
186 : }
187 :
188 : }
|