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(
23 0 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
24 0 : : CcuContextAlgBase(arg, transports, group)
25 : {
26 0 : const CcuCtxArgReduceScatterMesh1D* ctxArg = dynamic_cast<const CcuCtxArgReduceScatterMesh1D*>(&arg);
27 0 : if (ctxArg == nullptr) {
28 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterMesh1D::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 : "[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(
60 : "[CcuContextReduceScatterMesh1D] MyRank[%u], PeerId[%llu], TransportId[%u]", rankId_, peerId,
61 : transportIdx);
62 0 : CHK_PRT_RET(
63 : transports[transportIdx] == nullptr,
64 : HCCL_ERROR("[CcuContextReduceScatterMesh1D] Algorithm transport ptr is null"), );
65 0 : input_.push_back(
66 0 : CreateVariable((*transports[transportIdx]), INPUT_XN_ID)); // 获取transport中id=1的Var来传递output
67 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
68 0 : transportIdx++;
69 : }
70 : }
71 0 : offset_ = CreateVariable();
72 0 : groupOpSize_ = CreateGroupOpSize();
73 :
74 0 : Load(input_[rankId_]);
75 0 : Load(output_[0]);
76 0 : Load(token_[rankId_]);
77 0 : Load(offset_);
78 0 : Load(groupOpSize_);
79 0 : for (auto t : transports) {
80 0 : WriteVariableWithSignal(*t, input_[rankId_], INPUT_XN_ID, CKE_IDX_1, selfBit); // index = 1,传递output信息
81 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_2, selfBit); // index = 2,传递token信息
82 : }
83 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit); // index = 1,传递output信息
84 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit); // index = 2,传递token信息
85 :
86 0 : std::vector<CcuRep::Memory> src;
87 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
88 0 : src.push_back(CreateMemory());
89 : }
90 0 : CcuRep::Memory dst = CreateMemory();
91 0 : dst.addr = output_[0];
92 0 : dst.token = token_[rankId_];
93 0 : uint32_t dstId = 0;
94 0 : uint32_t curId = 0;
95 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
96 0 : if (rankIdx != rankId_) {
97 0 : curId = dstId;
98 0 : dstId++;
99 : } else {
100 0 : curId = rankSize_ - 1;
101 : }
102 0 : src[curId].addr = input_[rankIdx];
103 0 : src[curId].addr += offset_;
104 0 : src[curId].token = token_[rankIdx];
105 : }
106 :
107 0 : GroupReduce(transports, dst, src, groupOpSize_, dataType, outputDataType, reduceOp_);
108 :
109 0 : for (auto t : transports) {
110 0 : RemotePost(*t, CKE_IDX_0, selfBit);
111 : }
112 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit);
113 0 : HCCL_INFO("[CcuContextReduceScatterMesh1D] ReduceScatterMesh1D end");
114 0 : return;
115 0 : }
116 :
117 0 : std::vector<uint64_t> CcuContextReduceScatterMesh1D::GeneArgs(const CcuTaskArg& arg)
118 : {
119 0 : const CcuTaskArgReduceScatterMesh1D* taskArg = dynamic_cast<const CcuTaskArgReduceScatterMesh1D*>(&arg);
120 0 : if (taskArg == nullptr) {
121 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceScatterMesh1D::taskArg ptr is null"));
122 : }
123 0 : uint64_t inputAddr = taskArg->inputAddr_;
124 0 : uint64_t outputAddr = taskArg->outputAddr_;
125 0 : uint64_t tokenInfo = taskArg->token_;
126 0 : uint64_t offset = taskArg->offset_;
127 0 : uint64_t sliceSize = taskArg->sliceSize_;
128 0 : auto goSize = CalGoSize(sliceSize);
129 0 : return {inputAddr, outputAddr, tokenInfo, offset, goSize[0], goSize[1], goSize[2], goSize[3]};
130 0 : }
131 : } // namespace Hccl
|