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