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 : #ifndef ALG_OPERATOR_BASE_H
12 : #define ALG_OPERATOR_BASE_H
13 :
14 : #include <vector>
15 : #include "hccl_impl.h"
16 : #include "parallel_task_loader.h"
17 : #include "dispatcher.h"
18 : #include "ccl_buffer_manager.h"
19 : #include "hccl_trace_info.h"
20 : #include "device_capacity.h"
21 : #include "topo_matcher.h"
22 :
23 : #include "coll_alg_param.h"
24 : #include "coll_executor_base.h"
25 : #include "coll_alg_utils.h"
26 : #include "alg_configurator.h"
27 : #include "hccl_aiv.h"
28 : #include "config_log.h"
29 :
30 : namespace hccl {
31 : struct PreProcessMetaInfo {
32 : HcclCMDType opType;
33 : std::vector<u64> inputData;
34 : u64 inputSize;
35 : u64 outputSize;
36 : };
37 :
38 : class CollAlgOperator {
39 : public:
40 : CollAlgOperator(AlgConfigurator* algConfigurator, CCLBufferManager &cclBufferManager,
41 : HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher> &topoMatcher, HcclCMDType opType);
42 99 : virtual ~CollAlgOperator() = default;
43 :
44 : virtual HcclResult SelectAlg(const std::string& tag,
45 : const OpParam& param, std::string& algName, std::string& newTag);
46 : virtual HcclResult SelectAlg(const std::string& tag,
47 : const OpParam& param, std::string& algName, std::string& newTag, const ResourceLimit &limit);
48 : HcclResult SelectAlg(const std::string& tag, const OpParam ¶m, const ResourceLimit &limit,
49 : std::string &algName, AlgDesc &algDesc, std::string &newTag);
50 : virtual HcclResult CalcResRequest(const std::string& algName,
51 : const OpParam& param, AlgResourceRequest& resourceRequest);
52 : virtual HcclResult Orchestrate(const std::string& algName,
53 : OpParam& param, AlgResourceResponse& algResource);
54 : // AIV判断是否需要拷贝通信域信息到device上
55 : HcclResult PrepareCommInfoToDevice(const std::string& algName, AlgResourceResponse& algResource);
56 : virtual HcclResult GetAdjInfo(const std::string& algName, OpParam& param, AlgResourceResponse& algResource, AdjInfo& nslbAdjInfo);
57 : // batchsendrecv判断是否需要增量建链
58 : HcclResult CalcIncreLinkRequest(const std::string& algName, const OpParam& param, std::set<u32>& ranksHasLinked,
59 : AlgResourceRequest& resourceRequest, bool& needIncreLink);
60 : AlgType GetAlgType();
61 : void SetLegacyHcclImpl(std::unique_ptr<hcclImpl> &impl);
62 : HcclResult SetAlgOpContext(AlgOpContext algOpContext);
63 : HcclResult SetRetryEnable(bool retryEnable);
64 : HcclResult GetAivExecParam(std::string& algName, const OpParam& param,
65 : AlgResourceResponse& algRes, AivSuperKernelArgs &args);
66 : HcclResult CalNumBlocks(std::string& algName, const OpParam& param, u32 &numBlocks, int32_t aivCoreLimit = 0);
67 : HcclResult SetAivClearEnable(bool aivClearEnable);
68 : bool SupportRetryWithInplaceCheck(
69 : const HcclCMDType &opType, OpParam ¶m, std::string& algName, u8 &isInplaceStatus,
70 : InplaceSupportRetryStatus &inPlaceSupportRetryStatus);
71 : HcclResult GetNumBlocks(u32& numBlocks);
72 : HcclResult SetNumBlocks(const u32& numBlocks);
73 : HcclResult GetCache(HcclCacheInfo& cacheInfo);
74 : HcclResult SetOpCounter(const OpCounterInfo& opCounter);
75 : u32 CalcOptimalIntraRingsize(u64 count, HcclDataType dataType, HcclCMDType opType);
76 : HcclResult SetRmaInfo(void* rmaInfo);
77 : HcclResult GetOpExpansionStr(const OpParam ¶m, const AlgDesc &algDesc, std::string &opExpansionStr);
78 : protected:
79 : std::string GenerateNewTagByAlgTypeLevel1(std::string tag, std::string algTypeLevel1Tag) const;
80 : u32 CalcContextNumForPipeline(HcclCMDType hcclCMDType);
81 : HcclResult AutoSelectAlgTypeLevel1(HcclCMDType hcclCMDType, u64 countSize, u64 cclBufferSize,
82 : std::string &algTypeLevel1Tag, bool isInlineReduce = false,
83 : bool isRdmaReduce = false, bool isAivMode = false);
84 : bool SingleMeshInlineReduce(void *inputPtr, void *outputPtr, HcclDataType dataType, HcclReduceOp op);
85 : bool Is2U2PInfer();
86 : bool IsMultiMeshInlineReduce(void *inputPtr, void *outputPtr, HcclDataType dataType, HcclReduceOp op);
87 : bool Is910BSingleMesh();
88 : bool NeedCreateSingleMeshPlane(const bool isInlineReduce);
89 : virtual HcclResult SetExecutorAttr(const OpParam& param);
90 : HcclResult SelectAlgforAHC(u64 dataSize, AHCOpType ahcOpType);
91 : HcclResult AHCAlgSelect(AlgTypeLevel1 &algType, std::vector<std::vector<std::vector<u32>>> &globalSubGroups,
92 : std::map<AHCConcOpType, TemplateType> &ahcAlgOption, AHCAlgSelectParam &ahcAlgSelectParam);
93 : HcclResult AHCAlgOptionSelect(const AlgTypeLevel1 &algType, std::vector<std::vector<std::vector<u32>>> &globalSubGroups,
94 : std::map<AHCConcOpType, TemplateType> &ahcAlgOption, const AHCAlgSelectParam &ahcAlgSelectParam);
95 :
96 : bool IsNeedStrictMode(const OpParam& param);
97 : bool CheckStrictCondition(const OpParam& param) const;
98 :
99 : AlgType algType_; // 算法类型
100 : TopoType topoType_;
101 : bool isAlgoLevel1Default_ = false;
102 : bool isHaveCpuRank_;
103 : bool inlineReduceSwitchOn_;
104 : std::string identifier_;
105 : OpMode opMode;
106 :
107 : AlgConfigurator* algConfigurator_ = nullptr;
108 : CCLBufferManager &cclBufferManager_;
109 :
110 : u32 serverNum_;
111 : u32 moduleNum_;
112 : u32 superPodNum_;
113 : u32 deviceNumPerServer_;
114 : u32 deviceNumPerAggregation_;
115 : bool multiModuleDiffDeviceNumMode_;
116 : bool multiSuperPodDiffServerNumMode_;
117 : bool multiSuperPodDiffDeviceNumMode_;
118 : u32 meshAggregationRankSize_;
119 : bool isDiffDeviceModule_;
120 : bool isDiffDeviceType_;
121 : u32 gcdDeviceNumPerAggregation_;
122 : bool isSingleMeshAggregation_ = false;
123 : bool meshSinglePlane_ = false;
124 : bool isAllRankSamePlane_ = false;
125 : bool is310PDuoCard_;
126 : bool isCommon310P3DUO_;
127 : s32 hccsPortNum_;
128 : bool isSupportRdmaLite_ = false; // 是否支持rdma lite
129 : bool isSupportHccsAndSio_ = false; // 是否支持hccs,sio并行
130 : bool useSuperPodMode_ = false;
131 : bool isARSDoubleRing_ = true;
132 : u32 userRank_; // 本group中的userrank
133 : u32 realUserRank_; // world group中的userrank
134 : u32 userRankSize_;
135 : u32 devicePhyId_;
136 : s32 deviceLogicId_;
137 : DevType deviceType_;
138 : std::vector<u32> nicList_;
139 : std::unordered_map<u32, u32> pairLinkCounter_; // server内所有device间的链路类型计数
140 : hcclImpl* hcclImpl_ = nullptr;
141 : std::unique_ptr<CollExecutorBase> executor_;
142 : HcclDispatcher dispatcher_;
143 : std::unique_ptr<TopoMatcher> &topoMatcher_;
144 : HcclWorkflowMode workflowMode_;
145 : bool retryEnable_ = false;
146 : bool aivClearEnable_ = false;
147 : AlgOpContext algOpContext_;
148 : OpCounterInfo opCounter_;
149 : private:
150 : virtual HcclResult SelectAlgoTypeForReduceScatter(float delay, u64 recvCurSize, float bandWidth,
151 : AlgTypeLevel1 &algType);
152 : HcclResult SelectAlgoTypeForAllGather(float delay, u64 sendCurSize, float bandWidth,
153 : AlgTypeLevel1 &algType);
154 : HcclResult SelectAlgoTypeForAllGatherV(float delay, u64 sendCurSize, float bandWidth,
155 : AlgTypeLevel1 &algType);
156 : HcclResult SelectAlgoTypeForGather(float delay, u64 sendCurSize, float bandWidth,
157 : AlgTypeLevel1 &algType);
158 : HcclResult SelectAlgoTypeForAllReduce(float delay, u64 curSize, float bandWidth,
159 : AlgTypeLevel1 &algType);
160 : HcclResult SelectAlgoTypeForBroadcast(float delay, u64 curSize, float bandWidth,
161 : AlgTypeLevel1 &algType);
162 : HcclResult SelectAlgoTypeForReduce(float delay, u64 curSize, float bandWidth,
163 : AlgTypeLevel1 &algType);
164 : HcclResult SelectAlgFor91093WithCoreLimit(const OpParam ¶m, const ResourceLimit &limit,
165 : std::string &algName);
166 :
167 : HcclResult AppendTag(const AlgTypeLevel1 &algTypeLevel1, std::string &tag);
168 : HcclResult SelectAlgoForComm(HcclCMDType hcclCMDType, float delay, u64 curSize, float bandWidth,
169 : AlgTypeLevel1 &algType);
170 : HcclResult GetDefaultAlgoLevel1V2(HcclCMDType hcclCMDType, u64 curSize, u64 cclBufferSize,
171 : AlgTypeLevel1 &algType, bool isInlineReduce = false, bool isRdmaReduce = false, bool isAivMode = false);
172 : void SetAlgoAttr(AlgConfigurator* algConfigurator);
173 : void SetTopoAttr(AlgConfigurator* algConfigurator);
174 :
175 : std::map<HcclCMDType, std::function<HcclResult(float, u64, float, AlgTypeLevel1 &)>> selectFuncMap_ = {
176 : {HcclCMDType::HCCL_CMD_REDUCE_SCATTER,
177 : std::bind(&CollAlgOperator::SelectAlgoTypeForReduceScatter, this,
178 : std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)},
179 : {HcclCMDType::HCCL_CMD_ALLGATHER,
180 : std::bind(&CollAlgOperator::SelectAlgoTypeForAllGather, this,
181 : std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)},
182 : {HcclCMDType::HCCL_CMD_ALLREDUCE,
183 : std::bind(&CollAlgOperator::SelectAlgoTypeForAllReduce, this,
184 : std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)},
185 : {HcclCMDType::HCCL_CMD_ALLGATHER_V,
186 : std::bind(&CollAlgOperator::SelectAlgoTypeForAllGatherV, this,
187 : std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)},
188 : };
189 : };
190 : } // namespace hccl
191 :
192 : #endif /** __ALG_OPERATOR_BASE_H__ */
|