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 "coll_all_reduce_mid_count_for_910_93_executor.h"
12 :
13 : namespace hccl {
14 1 : CollAllReduceMidCountFor91093Executor::CollAllReduceMidCountFor91093Executor(
15 1 : const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher)
16 1 : : CollAllReduceExecutor(dispatcher, topoMatcher)
17 : {
18 1 : DMAReduceFlag_ = true;
19 0 : desc_.level1SupportedAlgos = {
20 : AlgTypeLevel1::ALG_LEVEL1_NHR,
21 1 : };
22 0 : desc_.level2SupportedAlgos = {
23 : AlgTypeLevel2::ALG_LEVEL2_NHR,
24 1 : };
25 1 : }
26 :
27 1 : HcclResult CollAllReduceMidCountFor91093Executor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
28 : {
29 1 : TransportMemType inputType = TransportMemType::RESERVED;
30 1 : TransportMemType outputType = TransportMemType::RESERVED;
31 1 : CHK_RET(CalcTransportMemType(inputType, outputType));
32 1 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
33 1 : CHK_RET(CalcLevel2CommInfo(inputType, outputType, opTransport));
34 1 : return HCCL_SUCCESS;
35 : }
36 :
37 1 : HcclResult CollAllReduceMidCountFor91093Executor::CalcTransportMemType(
38 : TransportMemType& inputType, TransportMemType& outputType) const
39 : {
40 1 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
41 1 : inputType = TransportMemType::CCL_INPUT;
42 1 : outputType = TransportMemType::CCL_OUTPUT;
43 : } else {
44 0 : HCCL_ERROR("AllReduceMidCountFor91093Executor do not support offload mode");
45 0 : return HCCL_E_UNAVAIL;
46 : }
47 1 : HCCL_INFO(
48 : "[CollAllReduceMidCountFor91093Executor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d]",
49 : tag_.c_str(), inputType, outputType);
50 1 : return HCCL_SUCCESS;
51 : }
52 :
53 1 : HcclResult CollAllReduceMidCountFor91093Executor::CalcLevel1CommInfo(
54 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
55 : {
56 1 : CommParaInfo commParaCombineL1(COMM_COMBINE_L1, CommType::COMM_TAG_NONUNIFORM_HIERARCHICAL_RING);
57 1 : CHK_RET(CalcCommPlaneInfo(tag_, commParaCombineL1, opTransport[COMM_COMBINE_L1], inputType, outputType));
58 1 : return HCCL_SUCCESS;
59 1 : }
60 :
61 1 : HcclResult CollAllReduceMidCountFor91093Executor::CalcLevel2CommInfo(
62 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
63 : {
64 1 : CommParaInfo commParaLevel2(COMM_LEVEL2, CommType::COMM_TAG_NONUNIFORM_HIERARCHICAL_RING);
65 1 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel2, opTransport[COMM_LEVEL2], inputType, outputType));
66 1 : return HCCL_SUCCESS;
67 1 : }
68 :
69 1 : u64 CollAllReduceMidCountFor91093Executor::CalcLoopMaxCount(const u64 cclBuffSize, const u32 unitSize)
70 : {
71 1 : u64 maxCountPerLoop = cclBuffSize / HCCL_MIN_SLICE_ALIGN_910_93 / unitSize * HCCL_MIN_SLICE_ALIGN_910_93;
72 1 : return maxCountPerLoop;
73 : }
74 :
75 0 : HcclResult CollAllReduceMidCountFor91093Executor::KernelRun(const OpParam& param, ExecMem& execMem)
76 : {
77 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] The MidCountFor91093Executor starts, topoType_[%u]", __func__, topoType_);
78 :
79 0 : u32 unitSize = 0;
80 0 : CHK_RET(SalGetDataTypeSize(param.GetDataType(), unitSize));
81 :
82 : // 获取 level1 打平级通信域
83 0 : CHK_RET(CheckCommSize(COMM_COMBINE_L1, COMM_INDEX_0 + 1));
84 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_COMBINE_L1, COMM_INDEX_0);
85 :
86 : // 获取 level2 级通信域
87 0 : CHK_RET(CheckCommSize(COMM_LEVEL2, COMM_INDEX_0 + 1));
88 0 : SubCommInfo level2CommInfo = GetSubCommInfo(COMM_LEVEL2, COMM_INDEX_0);
89 :
90 0 : const u32 level1RankSize = level1CommInfo.localRankSize;
91 0 : const u32 level2RankSize = level2CommInfo.localRankSize;
92 0 : u64 inputMemSize = execMem.count * unitSize;
93 0 : const u32 SINGLERANK = 1;
94 :
95 0 : if (DMAReduceFlag_) {
96 0 : DeviceMem srcMem = DeviceMem::create(static_cast<u8*>(execMem.inputPtr), inputMemSize);
97 0 : DeviceMem dstMem = execMem.inputMem.range(0, inputMemSize);
98 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstMem, srcMem, const_cast<Stream&>(param.stream)));
99 0 : HCCL_DEBUG("copy from user in to ccl in.");
100 0 : }
101 :
102 0 : u64 reduceAttr = GetReduceAttr(execMem.inputMem, execMem.outputMem, param.DataDes.dataType, param.reduceType);
103 :
104 : // step1: run nhr ont shot in level1
105 0 : if (level1RankSize > SINGLERANK) {
106 0 : std::unique_ptr<AlgTemplateBase> level1tempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
107 0 : TemplateType::TEMPLATE_ALL_REDUCE_NHR_ONESHOT, dispatcher_);
108 0 : CHK_SMART_PTR_NULL(level1tempAlg);
109 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] Run TEMPLATE_ALL_REDUCE_NHR_ONESHOT in COMM_COMBINE_L1/COMM_LEVEL2", __func__);
110 0 : HCCL_INFO("AllReduce mid count: using nhr algo intra-server.");
111 :
112 0 : CHK_RET(level1tempAlg->Prepare(reduceAttr));
113 0 : level1tempAlg->CloseBarrier();
114 :
115 0 : CHK_RET(level1tempAlg->Prepare(
116 : execMem.inputMem, execMem.outputMem, execMem.outputMem, execMem.count, param.DataDes.dataType, param.stream,
117 : param.reduceType, LEVEL0_BRIDGE_RANK_ID, std::vector<Slice>(0), 0));
118 :
119 0 : CHK_RET(level1tempAlg->RegisterProfiler(
120 : (level1RankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level1CommInfo.localRank, PROF_STAGE_0,
121 : HCCL_EXEC_STEP_NOT_SET, param.stream));
122 :
123 0 : CHK_RET(RunTemplate(level1tempAlg, level1CommInfo));
124 0 : }
125 :
126 : // 数据回拷
127 0 : if (level1RankSize > SINGLERANK && level2RankSize > SINGLERANK) {
128 0 : DeviceMem srcMem = execMem.outputMem.range(0, inputMemSize);
129 0 : DeviceMem dstMem = execMem.inputMem.range(0, inputMemSize);
130 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstMem, srcMem, const_cast<Stream&>(param.stream)));
131 0 : }
132 :
133 : // step2: run nhr ont shot in level2
134 0 : if (level2RankSize > SINGLERANK) {
135 0 : std::unique_ptr<AlgTemplateBase> level2tempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
136 0 : TemplateType::TEMPLATE_ALL_REDUCE_NHR_ONESHOT, dispatcher_);
137 0 : CHK_SMART_PTR_NULL(level2tempAlg);
138 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] Run TEMPLATE_ALL_REDUCE_NHR_ONESHOT in COMM_COMBINE_L1/COMM_LEVEL2", __func__);
139 0 : HCCL_INFO("AllReduce mid count: using nhr algo intra-server.");
140 :
141 0 : CHK_RET(level2tempAlg->Prepare(reduceAttr));
142 0 : level2tempAlg->CloseBarrier();
143 :
144 0 : CHK_RET(level2tempAlg->Prepare(
145 : execMem.inputMem, execMem.outputMem, execMem.outputMem, execMem.count, param.DataDes.dataType, param.stream,
146 : param.reduceType, LEVEL0_BRIDGE_RANK_ID, std::vector<Slice>(0), 0));
147 :
148 0 : CHK_RET(level2tempAlg->RegisterProfiler(
149 : (level2RankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level2CommInfo.localRank, PROF_STAGE_1,
150 : HCCL_EXEC_STEP_NOT_SET, param.stream));
151 :
152 0 : CHK_RET(RunTemplate(level2tempAlg, level2CommInfo));
153 0 : }
154 :
155 0 : if (DMAReduceFlag_) {
156 0 : DeviceMem srcMem = execMem.outputMem.range(0, inputMemSize);
157 0 : DeviceMem dstMem = DeviceMem::create(static_cast<u8*>(execMem.outputPtr), inputMemSize);
158 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstMem, srcMem, const_cast<Stream&>(param.stream)));
159 0 : HCCL_DEBUG("copy from ccl out to user out.");
160 0 : }
161 :
162 0 : HCCL_INFO("AllReduce mid count run success");
163 0 : return HCCL_SUCCESS;
164 0 : }
165 :
166 : REGISTER_EXEC("AllReduceMidCountFor91093Executor", AllReduceMidCountFor91093, CollAllReduceMidCountFor91093Executor);
167 : } // namespace hccl
|