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_tail_block.h"
12 : #include "ccu_instruction_reduce_tail_block.h"
13 :
14 : namespace Hccl {
15 :
16 : constexpr int INPUT_XN_ID = 0;
17 : constexpr int OUTPUT_XN_ID = 1;
18 : constexpr int TOKEN_XN_ID = 2;
19 :
20 : constexpr uint64_t TAIL_BLOCK_LOOP_NUM = 64;
21 : constexpr uint64_t MISSION_NUM_2 = 2;
22 :
23 0 : CcuContextReduceTailBlock::CcuContextReduceTailBlock(
24 0 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
25 0 : : CcuContextAlgBase(arg, transports, group)
26 : {
27 0 : const CcuCtxArgReduceTailBlock* ctxArg = dynamic_cast<const CcuCtxArgReduceTailBlock*>(&arg);
28 0 : if (ctxArg == nullptr) {
29 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceTailBlock::ctxArg ptr is null"));
30 : }
31 0 : rankId_ = ctxArg->rankId_;
32 0 : rankSize_ = ctxArg->dimSize_[0];
33 0 : notifySignal_ = ctxArg->notifySignal_;
34 0 : reduceOp_ = ctxArg->op_.reduceOp;
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 : "[CcuContextReduceTailBlock] outputDataType is [INVALID], set outputDataType to[%s]",
41 : outputDataType_.Describe().c_str());
42 : }
43 :
44 0 : HCCL_INFO(
45 : "[CcuContextReduceTailBlock] Init, CtxArgs are rankId[%u], rankSize[%llu], notifySignal[%s], "
46 : "reduceOp[%s], dataType[%s], outputDataType[%s]",
47 : rankId_, rankSize_, notifySignal_.c_str(), reduceOp_.Describe().c_str(), dataType_.Describe().c_str(),
48 : outputDataType_.Describe().c_str());
49 0 : }
50 :
51 0 : void CcuContextReduceTailBlock::Algorithm()
52 : {
53 0 : HCCL_INFO("[CcuContextReduceTailBlock] Algorithm start");
54 0 : InitResource();
55 0 : ExportVariables();
56 0 : SyncMainBlock(0); // 与住任务进行前同步, 主 mission 激活尾块 mission
57 :
58 0 : DoGroupReduce();
59 :
60 0 : SyncMainBlock(1); // 与住任务进行后同步, 通知主 mission 尾块处理完成
61 0 : HCCL_INFO("[CcuContextReduceTailBlock] Algorithm end");
62 0 : return;
63 : }
64 :
65 0 : void CcuContextReduceTailBlock::SyncMainBlock(uint32_t ctxSignalIndex)
66 : {
67 0 : HCCL_INFO("[CcuContextReduceTailBlock] SyncMainBlock start, ctxSignalIndex[%u]", ctxSignalIndex);
68 0 : LocalCtxPost(mainBlockCtxSignal_, 1 << (1 + ctxSignalIndex * MISSION_NUM_2));
69 0 : LocalWait(tailBlockCtxSignal_, 1 << (0 + ctxSignalIndex * MISSION_NUM_2));
70 0 : HCCL_INFO("[CcuContextReduceTailBlock] SyncMainBlock end");
71 0 : }
72 :
73 0 : void CcuContextReduceTailBlock::InitResource()
74 : {
75 0 : HCCL_INFO("[CcuContextReduceTailBlock] InitResource start");
76 : // init resource
77 0 : output_ = CreateVariable();
78 0 : uint16_t transportIdx = 0;
79 0 : if (transports.size() == 0) {
80 0 : THROW<NullPtrException>(StringFormat("CcuContextReduceTailBlock transports is empty"));
81 : }
82 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
83 0 : for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
84 0 : if (peerId == rankId_) {
85 0 : input_.push_back(CreateVariable());
86 0 : token_.push_back(CreateVariable());
87 : } else {
88 0 : HCCL_INFO(
89 : "[CcuContextReduceTailBlock] MyRank[%u], PeerId[%llu], TransportId[%u]", rankId_, peerId, transportIdx);
90 0 : CHK_PRT_RET(
91 : transports[transportIdx] == nullptr,
92 : HCCL_ERROR("[CcuContextReduceTailBlock] Algorithm transport ptr is null"), );
93 0 : input_.push_back(CreateVariable((*transports[transportIdx]), INPUT_XN_ID)); // 获取transport中id=1的Var
94 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
95 0 : transportIdx++;
96 : }
97 : }
98 :
99 0 : inputOffset_ = CreateVariable();
100 0 : outputOffset_ = CreateVariable();
101 0 : groupOpSize_ = CreateGroupOpSize();
102 0 : tailBlockCtxSignal_ = CreateMaskSignal();
103 :
104 : // init loop param
105 0 : AllocGoResource(TAIL_BLOCK_LOOP_NUM);
106 0 : HCCL_INFO("[CcuContextReduceTailBlock] InitResource end");
107 : }
108 :
109 0 : void CcuContextReduceTailBlock::DoGroupReduce()
110 : {
111 0 : HCCL_INFO("[CcuContextReduceTailBlock] DoGroupReduce start");
112 : // 初始化地址寄存器
113 0 : std::vector<CcuRep::Memory> reduceSrc;
114 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
115 0 : reduceSrc.push_back(CreateMemory());
116 : }
117 0 : CcuRep::Memory reduceDst = CreateMemory();
118 :
119 : // 填充地址
120 0 : uint32_t dstId = 0;
121 0 : uint32_t curId = 0;
122 : // SRC
123 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
124 0 : if (rankIdx != rankId_) {
125 0 : curId = dstId;
126 0 : dstId++;
127 : } else {
128 0 : curId = rankSize_ - 1;
129 : }
130 0 : reduceSrc[curId].addr = input_[rankIdx];
131 0 : reduceSrc[curId].addr += inputOffset_;
132 0 : reduceSrc[curId].token = token_[rankIdx];
133 : }
134 :
135 : // DST
136 0 : reduceDst.addr = output_;
137 0 : reduceDst.addr += outputOffset_;
138 0 : reduceDst.token = token_[rankId_];
139 :
140 : // 执行 reduce 操作
141 0 : GroupReduce(transports, reduceDst, reduceSrc, groupOpSize_, outputDataType_, dataType_, reduceOp_);
142 0 : HCCL_INFO("[CcuContextReduceTailBlock] DoGroupReduce end");
143 0 : return;
144 0 : }
145 :
146 0 : void CcuContextReduceTailBlock::ExportVariables()
147 : {
148 0 : HCCL_INFO("[CcuContextReduceTailBlock] ExportVariables start");
149 0 : ExportVariable(input_[rankId_], notifySignal_ + "_Input_Reduce_Tail_Block");
150 0 : ExportVariable(output_, notifySignal_ + "_Output_Reduce_Tail_Block");
151 0 : ExportVariable(token_[rankId_], notifySignal_ + "_Token_Reduce_Tail_Block");
152 0 : ExportVariable(inputOffset_, notifySignal_ + "_Input_Offset_Reduce_Tail_Block");
153 0 : ExportVariable(outputOffset_, notifySignal_ + "_Output_Offset_Reduce_Tail_Block");
154 0 : ExportVariable(groupOpSize_.addrOffset, notifySignal_ + "_AddrOffset_Reduce_Tail_Block");
155 0 : ExportVariable(groupOpSize_.loopParam, notifySignal_ + "_LoopParam_Reduce_Tail_Block");
156 0 : ExportVariable(groupOpSize_.parallelParam, notifySignal_ + "_ParallelParam_Reduce_Tail_Block");
157 0 : ExportVariable(groupOpSize_.residual, notifySignal_ + "_Residual_Reduce_Tail_Block");
158 :
159 0 : ExportMaskSignal(tailBlockCtxSignal_, notifySignal_ + "_CtxSync_Reduce_Tail_Block");
160 0 : mainBlockCtxSignal_ = ImportMaskSignal(notifySignal_ + "_CtxSync_Main_Block");
161 0 : HCCL_INFO("[CcuContextReduceTailBlock] ExportVariables end");
162 0 : }
163 :
164 0 : std::vector<uint64_t> CcuContextReduceTailBlock::GeneArgs(const CcuTaskArg& arg)
165 : {
166 : (void)arg; // MC2 要求尾块处理不能使用 CTX_ARG,参数需要通过主任务传入
167 0 : HCCL_INFO("[CcuContextReduceTailBlock] GeneArgs, TailBlock, no need to gene args");
168 0 : return {};
169 : }
170 : } // namespace Hccl
|