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.h"
12 : #include "ccu_instruction_reduce_scatter_v_mesh1d.h"
13 :
14 : namespace Hccl {
15 :
16 : constexpr int INPUT_XN_ID = 0;
17 : constexpr int TOKEN_XN_ID = 2;
18 : constexpr int CKE_IDX_0 = 0;
19 : constexpr int CKE_IDX_1 = 1;
20 : constexpr int CKE_IDX_2 = 2;
21 :
22 0 : CcuContextReduceScatterVMesh1D::CcuContextReduceScatterVMesh1D(
23 0 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
24 0 : : CcuContextAlgBase(arg, transports, group)
25 : {
26 0 : const CcuCtxArgReduceScatterVMesh1D* ctxArg = dynamic_cast<const CcuCtxArgReduceScatterVMesh1D*>(&arg);
27 0 : if (ctxArg == nullptr) {
28 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterVMesh1D::ctxArg ptr is null"));
29 : }
30 0 : rankId_ = ctxArg->rankId_;
31 0 : rankSize_ = ctxArg->dimSize_[0];
32 0 : dataType = ctxArg->op_.dataType;
33 0 : outputDataType = ctxArg->op_.outputDataType;
34 0 : if (outputDataType == DataType::INVALID) {
35 0 : outputDataType = dataType;
36 0 : HCCL_INFO(
37 : "[CcuContextReduceScatterVMesh1D] outputDataType is [INVALID], set outputDataType to[%s]",
38 : outputDataType.Describe().c_str());
39 : }
40 0 : reduceOp_ = ctxArg->op_.reduceOp;
41 0 : }
42 :
43 0 : void CcuContextReduceScatterVMesh1D::LoadArgs()
44 : {
45 0 : Load(input_[rankId_]);
46 0 : Load(output_);
47 0 : Load(token_[rankId_]);
48 0 : Load(mySliceOffset_);
49 0 : Load(groupOpSize_);
50 0 : return;
51 : }
52 :
53 0 : void CcuContextReduceScatterVMesh1D::InitResources()
54 : {
55 0 : uint16_t transportIdx = 0;
56 0 : if (transports.size() == 0) {
57 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterVMesh1D transports is empty"));
58 : }
59 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
60 0 : for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
61 0 : if (peerId == rankId_) {
62 0 : input_.push_back(CreateVariable());
63 0 : token_.push_back(CreateVariable());
64 : } else {
65 0 : HCCL_INFO(
66 : "[CcuContextReduceScatterVMesh1D] MyRank[%u], PeerId[%llu], TransportId[%u]", rankId_, peerId,
67 : transportIdx);
68 0 : CHK_PRT_RET(
69 : transports[transportIdx] == nullptr || transportIdx >= transports.size(),
70 : HCCL_ERROR("[CcuContextReduceScatterVMesh1D] Algorithm transport ptr is null or transportIdx is out of "
71 : "bounds"), );
72 0 : input_.push_back(
73 0 : CreateVariable((*transports[transportIdx]), INPUT_XN_ID)); // 获取transport中id=1的Var来传递output
74 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
75 0 : transportIdx++;
76 : }
77 : }
78 0 : mySliceOffset_ = CreateVariable();
79 0 : groupOpSize_ = CreateGroupOpSize();
80 0 : output_ = CreateVariable();
81 :
82 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
83 0 : nMemory_.push_back(CreateMemory());
84 : }
85 0 : oneMemory_ = CreateMemory();
86 0 : return;
87 : }
88 :
89 0 : void CcuContextReduceScatterVMesh1D::PreSync()
90 : {
91 0 : uint16_t selfBit = 1 << rankId_;
92 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
93 0 : for (auto t : transports) {
94 0 : WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit); // index = 1,传递output信息
95 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_2, selfBit); // index = 2,传递token信息
96 : }
97 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit); // index = 1,传递output信息
98 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit); // index = 2,传递token信息
99 0 : return;
100 : }
101 :
102 0 : void CcuContextReduceScatterVMesh1D::PostSync()
103 : {
104 0 : uint16_t selfBit = 1 << rankId_;
105 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
106 0 : for (auto t : transports) {
107 0 : RemotePost(*t, CKE_IDX_0, selfBit);
108 : }
109 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit);
110 0 : }
111 :
112 0 : void CcuContextReduceScatterVMesh1D::DoGroupReduce()
113 : {
114 0 : std::vector<CcuRep::Memory>& src = nMemory_;
115 0 : CcuRep::Memory& dst = oneMemory_;
116 :
117 0 : dst.addr = output_;
118 0 : dst.token = token_[rankId_];
119 0 : uint32_t dstId = 0;
120 0 : uint32_t curId = 0;
121 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
122 0 : if (rankIdx != rankId_) {
123 0 : curId = dstId;
124 0 : dstId++;
125 : } else {
126 0 : curId = rankSize_ - 1;
127 : }
128 0 : src[curId].addr = input_[rankIdx];
129 0 : src[curId].addr += mySliceOffset_;
130 0 : src[curId].token = token_[rankIdx];
131 : }
132 :
133 0 : GroupReduce(transports, dst, src, groupOpSize_, dataType, outputDataType, reduceOp_);
134 0 : }
135 :
136 0 : void CcuContextReduceScatterVMesh1D::Algorithm()
137 : {
138 0 : HCCL_INFO("[CcuContextReduceScatterVMesh1D] ReduceScatterVMesh1D run");
139 0 : InitResources();
140 0 : LoadArgs();
141 0 : PreSync();
142 0 : DoGroupReduce();
143 0 : PostSync();
144 0 : HCCL_INFO("[CcuContextReduceScatterVMesh1D] ReduceScatterVMesh1D end");
145 0 : return;
146 : }
147 :
148 0 : std::vector<uint64_t> CcuContextReduceScatterVMesh1D::GeneArgs(const CcuTaskArg& arg)
149 : {
150 0 : const CcuTaskArgReduceScatterVMesh1D* taskArg = dynamic_cast<const CcuTaskArgReduceScatterVMesh1D*>(&arg);
151 0 : if (taskArg == nullptr) {
152 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterVMesh1D::taskArg ptr is null"));
153 : }
154 0 : uint64_t inputAddr = taskArg->inputAddr_;
155 0 : uint64_t outputAddr = taskArg->outputAddr_;
156 0 : uint64_t tokenInfo = taskArg->token_;
157 0 : uint64_t mySliceInputOffset = taskArg->offset_;
158 0 : uint64_t mySliceSize = taskArg->sliceSize_;
159 0 : auto goSize = CalGoSize(mySliceSize);
160 0 : std::vector<uint64_t> args = {inputAddr, outputAddr, tokenInfo, mySliceInputOffset};
161 0 : for (auto& item : goSize) {
162 0 : args.push_back(item);
163 : }
164 0 : return args;
165 0 : }
166 : } // namespace Hccl
|