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(const CcuCtxArg &arg,
23 : const std::vector<CcuTransport *> &transports,
24 0 : const CcuTransportGroup &group)
25 0 : : CcuContextAlgBase(arg, transports, group)
26 : {
27 0 : const CcuCtxArgReduceScatterVMesh1D *ctxArg = dynamic_cast<const CcuCtxArgReduceScatterVMesh1D *>(&arg);
28 0 : if (ctxArg == nullptr) {
29 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterVMesh1D::ctxArg ptr is null"));
30 : }
31 0 : rankId_ = ctxArg->rankId_;
32 0 : rankSize_ = ctxArg->dimSize_[0];
33 0 : dataType = ctxArg->op_.dataType;
34 0 : outputDataType = ctxArg->op_.outputDataType;
35 0 : if (outputDataType == DataType::INVALID) {
36 0 : outputDataType = dataType;
37 0 : HCCL_INFO("[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("[CcuContextReduceScatterVMesh1D] MyRank[%u], PeerId[%llu], TransportId[%u]", rankId_, peerId,
66 : transportIdx);
67 0 : CHK_PRT_RET(transports[transportIdx] == nullptr || transportIdx >= transports.size(),
68 : HCCL_ERROR("[CcuContextReduceScatterVMesh1D] Algorithm transport ptr is null or transportIdx is out of bounds"),);
69 0 : input_.push_back(
70 0 : CreateVariable((*transports[transportIdx]), INPUT_XN_ID)); // 获取transport中id=1的Var来传递output
71 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
72 0 : transportIdx++;
73 : }
74 : }
75 0 : mySliceOffset_ = CreateVariable();
76 0 : groupOpSize_ = CreateGroupOpSize();
77 0 : output_ = CreateVariable();
78 :
79 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
80 0 : nMemory_.push_back(CreateMemory());
81 : }
82 0 : oneMemory_ = CreateMemory();
83 0 : return;
84 : }
85 :
86 0 : void CcuContextReduceScatterVMesh1D::PreSync()
87 : {
88 0 : uint16_t selfBit = 1 << rankId_;
89 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
90 0 : for (auto t : transports) {
91 0 : WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit); // index = 1,传递output信息
92 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_2, selfBit); // index = 2,传递token信息
93 : }
94 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit); // index = 1,传递output信息
95 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit); // index = 2,传递token信息
96 0 : return;
97 : }
98 :
99 0 : void CcuContextReduceScatterVMesh1D::PostSync()
100 : {
101 0 : uint16_t selfBit = 1 << rankId_;
102 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
103 0 : for (auto t : transports) {
104 0 : RemotePost(*t, CKE_IDX_0, selfBit);
105 : }
106 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit);
107 0 : }
108 :
109 0 : void CcuContextReduceScatterVMesh1D::DoGroupReduce()
110 : {
111 0 : std::vector<CcuRep::Memory> &src = nMemory_;
112 0 : CcuRep::Memory &dst = oneMemory_;
113 :
114 0 : dst.addr = output_;
115 0 : dst.token = token_[rankId_];
116 0 : uint32_t dstId = 0;
117 0 : uint32_t curId = 0;
118 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
119 0 : if (rankIdx != rankId_) {
120 0 : curId = dstId;
121 0 : dstId++;
122 : } else {
123 0 : curId = rankSize_ - 1;
124 : }
125 0 : src[curId].addr = input_[rankIdx];
126 0 : src[curId].addr += mySliceOffset_;
127 0 : src[curId].token = token_[rankIdx];
128 : }
129 :
130 0 : GroupReduce(transports, dst, src, groupOpSize_, dataType, outputDataType, reduceOp_);
131 0 : }
132 :
133 0 : void CcuContextReduceScatterVMesh1D::Algorithm()
134 : {
135 0 : HCCL_INFO("[CcuContextReduceScatterVMesh1D] ReduceScatterVMesh1D run");
136 0 : InitResources();
137 0 : LoadArgs();
138 0 : PreSync();
139 0 : DoGroupReduce();
140 0 : PostSync();
141 0 : HCCL_INFO("[CcuContextReduceScatterVMesh1D] ReduceScatterVMesh1D end");
142 0 : return;
143 : }
144 :
145 0 : std::vector<uint64_t> CcuContextReduceScatterVMesh1D::GeneArgs(const CcuTaskArg &arg)
146 : {
147 0 : const CcuTaskArgReduceScatterVMesh1D *taskArg = dynamic_cast<const CcuTaskArgReduceScatterVMesh1D *>(&arg);
148 0 : if (taskArg == nullptr) {
149 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterVMesh1D::taskArg ptr is null"));
150 : }
151 0 : uint64_t inputAddr = taskArg->inputAddr_;
152 0 : uint64_t outputAddr = taskArg->outputAddr_;
153 0 : uint64_t tokenInfo = taskArg->token_;
154 0 : uint64_t mySliceInputOffset = taskArg->offset_;
155 0 : uint64_t mySliceSize = taskArg->sliceSize_;
156 0 : auto goSize = CalGoSize(mySliceSize);
157 0 : std::vector<uint64_t> args = {inputAddr, outputAddr, tokenInfo, mySliceInputOffset};
158 0 : for (auto &item : goSize) {
159 0 : args.push_back(item);
160 : }
161 0 : return args;
162 0 : }
163 : } // namespace Hccl
|