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_mesh1d.h"
12 : #include "ccu_instruction_reduce_scatter_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 : CcuContextReduceScatterMesh1D::CcuContextReduceScatterMesh1D(const CcuCtxArg &arg,
23 : const std::vector<CcuTransport *> &transports,
24 0 : const CcuTransportGroup &group)
25 0 : : CcuContextAlgBase(arg, transports, group)
26 : {
27 0 : const CcuCtxArgReduceScatterMesh1D *ctxArg = dynamic_cast<const CcuCtxArgReduceScatterMesh1D *>(&arg);
28 0 : if (ctxArg == nullptr) {
29 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterMesh1D::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("[CcuContextReduceScatterMesh1D] 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 CcuContextReduceScatterMesh1D::Algorithm()
44 : {
45 0 : HCCL_INFO("[CcuContextReduceScatterMesh1D] ReduceScatterMesh1D run");
46 0 : uint16_t selfBit = 1 << rankId_;
47 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
48 0 : output_.push_back(CreateVariable());
49 0 : uint16_t transportIdx = 0;
50 0 : if (transports.size() == 0) {
51 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterMesh1D transports is empty"));
52 : }
53 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
54 0 : for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
55 0 : if (peerId == rankId_) {
56 0 : input_.push_back(CreateVariable());
57 0 : token_.push_back(CreateVariable());
58 : } else {
59 0 : HCCL_INFO("[CcuContextReduceScatterMesh1D] MyRank[%u], PeerId[%llu], TransportId[%u]",
60 : rankId_, peerId, transportIdx);
61 0 : CHK_PRT_RET(transports[transportIdx] == nullptr,
62 : HCCL_ERROR("[CcuContextReduceScatterMesh1D] Algorithm transport ptr is null"),);
63 0 : input_.push_back(CreateVariable((*transports[transportIdx]), INPUT_XN_ID)); // 获取transport中id=1的Var来传递output
64 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
65 0 : transportIdx++;
66 : }
67 : }
68 0 : offset_ = CreateVariable();
69 0 : groupOpSize_ = CreateGroupOpSize();
70 :
71 0 : Load(input_[rankId_]);
72 0 : Load(output_[0]);
73 0 : Load(token_[rankId_]);
74 0 : Load(offset_);
75 0 : Load(groupOpSize_);
76 0 : for (auto t : transports) {
77 0 : WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit); // index = 1,传递output信息
78 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_2, selfBit); // index = 2,传递token信息
79 : }
80 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit); // index = 1,传递output信息
81 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit); // index = 2,传递token信息
82 :
83 0 : std::vector<CcuRep::Memory> src;
84 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
85 0 : src.push_back(CreateMemory());
86 : }
87 0 : CcuRep::Memory dst = CreateMemory();
88 0 : dst.addr = output_[0];
89 0 : dst.token = token_[rankId_];
90 0 : uint32_t dstId = 0;
91 0 : uint32_t curId = 0;
92 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
93 0 : if (rankIdx != rankId_) {
94 0 : curId = dstId;
95 0 : dstId++;
96 : } else {
97 0 : curId = rankSize_ - 1;
98 : }
99 0 : src[curId].addr = input_[rankIdx];
100 0 : src[curId].addr += offset_;
101 0 : src[curId].token = token_[rankIdx];
102 : }
103 :
104 0 : GroupReduce(transports, dst, src, groupOpSize_, dataType, outputDataType, reduceOp_);
105 :
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 : HCCL_INFO("[CcuContextReduceScatterMesh1D] ReduceScatterMesh1D end");
111 0 : return;
112 0 : }
113 :
114 0 : std::vector<uint64_t> CcuContextReduceScatterMesh1D::GeneArgs(const CcuTaskArg &arg)
115 : {
116 0 : const CcuTaskArgReduceScatterMesh1D *taskArg = dynamic_cast<const CcuTaskArgReduceScatterMesh1D *>(&arg);
117 0 : if (taskArg == nullptr) {
118 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterMesh1D::taskArg ptr is null"));
119 : }
120 0 : uint64_t inputAddr = taskArg->inputAddr_;
121 0 : uint64_t outputAddr = taskArg->outputAddr_;
122 0 : uint64_t tokenInfo = taskArg->token_;
123 0 : uint64_t offset = taskArg->offset_;
124 0 : uint64_t sliceSize = taskArg->sliceSize_;
125 0 : auto goSize = CalGoSize(sliceSize);
126 0 : return {inputAddr, outputAddr, tokenInfo, offset, goSize[0], goSize[1], goSize[2], goSize[3]};
127 0 : }
128 : }
|