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 "reduce_operator.h"
12 :
13 : namespace hccl {
14 :
15 1 : ReduceOperator::ReduceOperator(AlgConfigurator* algConfigurator, CCLBufferManager &cclBufferManager,
16 1 : HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher> &topoMatcher)
17 1 : : CollAlgOperator(algConfigurator, cclBufferManager, dispatcher, topoMatcher, HcclCMDType::HCCL_CMD_REDUCE)
18 : {
19 3 : if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR || algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR_V1 || algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NB ||
20 3 : algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_PIPELINE) {
21 0 : HCCL_WARNING("[ReduceOperator][ReduceOperator] nonuniform-hierachical-ring and nonuniform-bruck and pipeline " \
22 : "algorithms do not support Reduce yet, reset algo to halving-doubling");
23 0 : algType_.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_HD;
24 : }
25 3 : }
26 :
27 8 : ReduceOperator::~ReduceOperator()
28 : {
29 8 : }
30 :
31 2 : HcclResult ReduceOperator::SelectAlg(const std::string &tag, const OpParam ¶m, std::string &algName,
32 : std::string &newTag)
33 : {
34 2 : HcclResult ret = HCCL_SUCCESS;
35 :
36 2 : if (userRankSize_ == 1) {
37 0 : algName = "ReduceSingleExecutor";
38 0 : HCCL_INFO("[SelectAlg] Reduce SelectAlg is algName [%s]", algName.c_str());
39 0 : return HCCL_SUCCESS;
40 : }
41 :
42 2 : newTag = param.tag;
43 4 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_HD
44 0 : || algType_.algoLevel2 == AlgTypeLevel2::ALG_LEVEL2_HD)) {
45 4 : std::string appendTag = "";
46 2 : u32 serverNumPerSuperPod = superPodNum_ == 0 ? moduleNum_ : moduleNum_ / superPodNum_;
47 2 : HCCL_DEBUG("[ReduceOperator][SelectAlg]serverNumPerSuperPod is %u", serverNumPerSuperPod);
48 4 : if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_HD) {
49 4 : u32 part1Size = FACTOR_TWO * (serverNumPerSuperPod - (1 << static_cast<u32>(log2(serverNumPerSuperPod))));
50 4 : u32 rootId = param.root / deviceNumPerAggregation_ % serverNumPerSuperPod;
51 4 : appendTag += "L1_" + std::to_string((rootId >= part1Size) || ((rootId % FACTOR_TWO) == 0));
52 : }
53 4 : HCCL_DEBUG("[ReduceOperator]SelectAlg for algoLevel1");
54 4 : if (algType_.algoLevel2 == AlgTypeLevel2::ALG_LEVEL2_HD) {
55 0 : u32 part1Size = FACTOR_TWO * (superPodNum_ - (1 << static_cast<u32>(log2(superPodNum_))));
56 0 : u32 rootId = param.root / deviceNumPerAggregation_ / serverNumPerSuperPod;
57 0 : appendTag += (appendTag.empty() ? "L2_" : "_L2_") + std::to_string((rootId >= part1Size) || ((rootId % FACTOR_TWO) == 0));
58 : }
59 4 : HCCL_DEBUG("[ReduceOperator][SelectAlg]tag is [%s]", tag);
60 4 : newTag = newTag + '_' + appendTag;
61 4 : if (GetExternalInputHcclEnableEntryLog() && param.opBaseAtraceInfo != nullptr) {
62 0 : CHK_RET(param.opBaseAtraceInfo->SavealgtypeTraceInfo(appendTag, param.tag));
63 : }
64 4 : }
65 :
66 4 : if (isDiffDeviceType_) {
67 0 : HCCL_ERROR("[ReduceOperator][SelectAlg] Reduce not support diffDeviceType");
68 0 : return HCCL_E_NOT_SUPPORT;
69 4 : } else if (deviceType_ == DevType::DEV_TYPE_910) {
70 0 : ret = SelectAlgfor910A(param, algName);
71 4 : } else if (deviceType_ == DevType::DEV_TYPE_910B) {
72 4 : ret = SelectAlgfor910B(param, algName);
73 0 : } else if (deviceType_ == DevType::DEV_TYPE_910_93) {
74 0 : ret = SelectAlgfor91093(param, algName);
75 : } else {
76 0 : HCCL_ERROR("ReduceOperator[SelectAlg] device type[%d] is out of range for selector.", deviceType_);
77 0 : return HCCL_E_NOT_SUPPORT;
78 : }
79 4 : CHK_PRT_RET(ret != HCCL_SUCCESS,
80 : HCCL_ERROR("[ReduceSelector][SelectAlg]tag[%s], reduce failed, return[%d]", tag.c_str(), ret), ret);
81 :
82 4 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
83 4 : auto level1Iter = HCCL_ALGO_LEVEL1_NAME_MAP.find(algType_.algoLevel1);
84 2 : CHK_PRT_RET(level1Iter == HCCL_ALGO_LEVEL1_NAME_MAP.end(), HCCL_ERROR("level1: algType1[%u] is invalid.",
85 : algType_.algoLevel1), HCCL_E_INTERNAL);
86 2 : newTag = newTag + level1Iter->second + algName;
87 : }
88 4 : newTag += (param.aicpuUnfoldMode ? "_device" : "_host");
89 4 : return ret;
90 : }
91 :
92 0 : HcclResult ReduceOperator::SelectAlgfor910A(const OpParam& param, std::string& algName)
93 : {
94 : (void) param;
95 0 : bool isMeshTopo = topoType_ == TopoType::TOPO_TYPE_4P_MESH || topoType_ == TopoType::TOPO_TYPE_2P_MESH;
96 0 : bool isRingTopo = topoType_ == TopoType::TOPO_TYPE_NP_SINGLE_RING || topoType_ == TopoType::TOPO_TYPE_8P_RING;
97 :
98 0 : if (isMeshTopo) {
99 0 : algName = "ReduceMeshExecutor";
100 0 : } else if (isRingTopo) {
101 0 : algName = "ReduceRingPlusHd";
102 : } else {
103 0 : algName = "ReduceComm";
104 : }
105 :
106 0 : HCCL_INFO("[SelectAlgfor910A] reduce SelectAlgfor910A is algName[%s].", algName.c_str());
107 0 : return HCCL_SUCCESS;
108 : }
109 :
110 4 : HcclResult ReduceOperator::SelectAlgfor910B(const OpParam& param, std::string& algName)
111 : {
112 : (void) param;
113 0 : bool isMeshTopo = topoType_ == TopoType::TOPO_TYPE_NP_MESH || topoType_ == TopoType::TOPO_TYPE_4P_MESH ||
114 4 : topoType_ == TopoType::TOPO_TYPE_2P_MESH || topoType_ == TopoType::TOPO_TYPE_1P_MESH;
115 4 : bool isRingTopo = topoType_ == TopoType::TOPO_TYPE_NP_SINGLE_RING;
116 :
117 4 : if (isMeshTopo) {
118 4 : algName = "ReduceMeshExecutor";
119 0 : } else if (isRingTopo) {
120 0 : algName = "ReduceRingPlusHd";
121 : } else {
122 0 : algName = "ReduceComm";
123 : }
124 :
125 4 : HCCL_INFO("[SelectAlgfor910B] reduce SelectAlgfor910B is algName [%s].", algName.c_str());
126 4 : return HCCL_SUCCESS;
127 : }
128 :
129 0 : HcclResult ReduceOperator::SelectAlgfor91093(const OpParam& param, std::string& algName)
130 : {
131 0 : bool isRingTopo = topoType_ == TopoType::TOPO_TYPE_NP_SINGLE_RING;
132 0 : bool isDoubleRingTopo = topoType_ == TopoType::TOPO_TYPE_NP_DOUBLE_RING;
133 0 : u32 unitSize = SIZE_TABLE[param.DataDes.dataType];
134 0 : u64 dataSize = param.DataDes.count * unitSize; // 单位:字节
135 0 : if (dataSize >= cclBufferManager_.GetInCCLbufferSize()) {
136 0 : HCCL_WARNING("The current inCCLbufferSize is [%llu] bytes, change the HCCL_BUFFSIZE environment variable "\
137 : "to be greater than the current data volume[%llu] bytes to improve the performance of the 91093 environment.",
138 : cclBufferManager_.GetInCCLbufferSize(), dataSize);
139 : }
140 :
141 0 : if (multiModuleDiffDeviceNumMode_ || multiSuperPodDiffServerNumMode_) {
142 0 : algName = "ReduceComm";
143 0 : } else if (isRingTopo || isDoubleRingTopo) {
144 0 : algName = "ReduceRingFor91093Executor";
145 : } else {
146 0 : algName = "ReduceComm";
147 : }
148 0 : if ((!(algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_RING) &&
149 0 : !(algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_HD)) ||
150 0 : (!(algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_HD) && topoMatcher_->GetTopoInfo().superPodNum > 1) ||
151 0 : !(algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_WHOLE_RING)) {
152 0 : algType_.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_RING;
153 0 : HCCL_WARNING("[ReduceOperator][SelectAlgfor91093][Superpod] inter-server only support ring yet, "\
154 : "default is algType=RING.");
155 : }
156 0 : HCCL_INFO("[SelectAlgfor91093] reduce SelectAlgfor91093 is algName [%s].", algName.c_str());
157 0 : return HCCL_SUCCESS;
158 : }
159 :
160 : REGISTER_OP(HcclCMDType::HCCL_CMD_REDUCE, Reduce, ReduceOperator);
161 :
162 : }
|