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 : #ifndef HCCLV2_BASE_CONFIG_H
11 : #define HCCLV2_BASE_CONFIG_H
12 :
13 : #include <string>
14 : #include <vector>
15 : #include <functional>
16 : #include <climits>
17 : #include <regex>
18 : #include "ip_address.h"
19 : #include "dma_mode.h"
20 : #include "env_func.h"
21 : #include "cfg_field.h"
22 :
23 : namespace Hccl {
24 :
25 : class BaseConfig {
26 : public:
27 : virtual void Parse() = 0;
28 : };
29 :
30 : // Host网卡配置
31 : class EnvHostNicConfig : public BaseConfig {
32 : public:
33 : void Parse() override;
34 : const IpAddress& GetControlIfIp() const;
35 : u32 GetIfBasePort() const;
36 : const SocketIfName& GetSocketIfName() const;
37 : bool GetWhitelistDisable() const;
38 : const std::string& GetWhiteListFile() const;
39 : const std::vector<SocketPortRange>& GetHostSocketPortRange() const;
40 : const std::vector<SocketPortRange>& GetDeviceSocketPortRange() const;
41 :
42 : private:
43 : static constexpr u32 HCCL_INVALIED_IF_BASE_PORT = 65536; // HCCL默认无效端口号
44 : static constexpr u32 HCCL_INVALIED_IF_BASE_PORT_MAX = 65520; // HCCL端口号最大值
45 : static constexpr u32 HCCL_INVALIED_IF_BASE_PORT_MIN = 1024; // HCCL端口号最小值
46 :
47 : CfgField<IpAddress> hcclIfIp{"HCCL_IF_IP", {}, Str2T<IpAddress>};
48 : CfgField<u32> hcclIfBasePort{
49 : "HCCL_IF_BASE_PORT", u32(HCCL_INVALIED_IF_BASE_PORT), Str2T<u32>,
50 : CHK_RANGE_CLOSED<u32>(HCCL_INVALIED_IF_BASE_PORT_MIN, HCCL_INVALIED_IF_BASE_PORT_MAX)};
51 : CfgField<SocketIfName> hcclSocketIfName{"HCCL_SOCKET_IFNAME", SocketIfName({}, false, false), CastSocketIfName};
52 : CfgField<bool> whitelistDisable{"HCCL_WHITELIST_DISABLE", true, CastBin2Bool};
53 : CfgField<std::string> hcclWhiteListFile{"HCCL_WHITELIST_FILE", "", Str2T<std::string>, CheckFilePath, SetRealPath};
54 : CfgField<std::vector<SocketPortRange>> hcclHostSocketPortRange{
55 1 : "HCCL_HOST_SOCKET_PORT_RANGE", {}, [](const std::string& s) -> std::vector<SocketPortRange> {
56 2 : return CastSocketPortRange(s, "HCCL_HOST_SOCKET_PORT_RANGE");
57 : }};
58 : CfgField<std::vector<SocketPortRange>> hcclDeviceSocketPortRange{
59 0 : "HCCL_NPU_SOCKET_PORT_RANGE", {}, [](const std::string& s) -> std::vector<SocketPortRange> {
60 0 : return CastSocketPortRange(s, "HCCL_NPU_SOCKET_PORT_RANGE");
61 : }};
62 : };
63 :
64 : // Socket公共配置
65 : class EnvSocketConfig : public BaseConfig {
66 : public:
67 : void Parse() override;
68 : s32 GetSocketFamily() const;
69 : s32 GetLinkTimeOut() const;
70 :
71 : private:
72 : static constexpr s32 HCCL_LINK_TIME_OUT_S = 120; // HCCL 默认的建链超时时间设置为120s
73 : static constexpr s32 HCCL_MIN_LINK_TIME_OUT_S = 120; // HCCL 建链最小超时时间设置为120s
74 : static constexpr s32 HCCL_MAX_LINK_TIME_OUT_S = (120 * 60); // HCCL 最大建链超时时间设置为120*60s
75 :
76 : CfgField<s32> hcclSocketFamily{"HCCL_SOCKET_FAMILY", -1, CastSocketFamily};
77 : CfgField<s32> linkTimeOut{
78 : "HCCL_CONNECT_TIMEOUT", s32(HCCL_LINK_TIME_OUT_S), Str2T<s32>,
79 : CHK_RANGE_CLOSED<s32>(HCCL_MIN_LINK_TIME_OUT_S, HCCL_MAX_LINK_TIME_OUT_S)};
80 : };
81 :
82 : // RTS配置
83 : class EnvRtsConfig : public BaseConfig {
84 : public:
85 : void Parse() override;
86 : u32 GetExecTimeOut() const;
87 : double GetAivExecTimeOut() const;
88 :
89 : private:
90 : static constexpr s32 NOTIFY_DEFAULT_WAIT_TIME = 27 * 68; // notifywait默认1836等待时长
91 : static constexpr s32 AIV_TIMEOUT_DEFAULT = 1091;
92 :
93 : CfgField<u32> execTimeOut{
94 : "HCCL_EXEC_TIMEOUT", static_cast<u32>(NOTIFY_DEFAULT_WAIT_TIME),
95 7 : [](const std::string& s) -> u32 {
96 7 : static std::regex validFormat(R"(^\d+(\.\d{1,2})?$)");
97 7 : if (!std::regex_match(s, validFormat)) {
98 1 : THROW<InvalidParamsException>(
99 2 : StringFormat("Invalid config value, execTimeOutStr[%s], up to two decimal places", s.c_str()));
100 : }
101 6 : return String2T<u32>(s);
102 : },
103 : CheckExecTimeOut, ProcExecTimeOut};
104 :
105 : CfgField<double> aivExecTimeOut{
106 : "HCCL_EXEC_TIMEOUT", double(AIV_TIMEOUT_DEFAULT),
107 1 : [](const std::string& s) -> double {
108 1 : static std::regex validFormat(R"(^\d+(\.\d{1,2})?$)");
109 1 : if (!std::regex_match(s, validFormat)) {
110 0 : THROW<InvalidParamsException>(
111 0 : StringFormat("Invalid config value, execTimeOutStr[%s], up to two decimal places", s.c_str()));
112 : }
113 1 : return String2T<double>(s);
114 : },
115 : nullptr, nullptr};
116 : };
117 :
118 : // RDMA配置
119 : class EnvRdmaConfig : public BaseConfig {
120 : public:
121 : void Parse() override;
122 : u32 GetRdmaTrafficClass() const;
123 : u32 GetRdmaServerLevel() const;
124 : u32 GetRdmaTimeOut() const;
125 : u32 GetRdmaRetryCnt() const;
126 : u32 GetUboeTimeOut() const;
127 : u32 GetUbTimeOut() const;
128 : u32 GetRdmaQueueNum() const;
129 : u32 GetRdmaMultiQpThreshold() const;
130 : const MultiQpSrcPortConfig& GetMultiQpSrcPortConfig() const;
131 :
132 : static constexpr u32 HCCL_RDMA_TC_DEFAULT = 132; // 默认的traffic class为132(33*4)
133 : static constexpr u32 HCCL_RDMA_SL_DEFAULT = 4; // 默认的server level为4
134 : static constexpr u32 HCCL_RDMA_TIMEOUT_DEFAULT = 20; // 默认的TIMEOUT配置为20(对应时间4.096*2^20us)
135 : static constexpr u32 HCCL_RDMA_RETRY_CNT_DEFAULT = 7; // 默认的Retry Cnt为7
136 : static constexpr u32 HCCL_RDMA_TC_MIN = 0; // rdma traffic class最小值为0
137 : static constexpr u32 HCCL_RDMA_TC_MAX = 255; // rdma traffic class最大值为255
138 : static constexpr u32 HCCL_RDMA_SL_MIN = 0; // rdma server level最小值为0
139 : static constexpr u32 HCCL_RDMA_SL_MAX = 7; // rdma server level最大值为7
140 : static constexpr u32 HCCL_RDMA_TIMEOUT_MIN = 0; // rdma timeout最小值为0
141 : static constexpr u32 HCCL_RDMA_TIMEOUT_MAX = 31; // rdma timeout最大值为31
142 : static constexpr u32 HCCL_RDMA_RETRY_CNT_MIN = 1; // rdma Retry Cnt最小值为1
143 : static constexpr u32 HCCL_RDMA_RETRY_CNT_MAX = 7; // rdma Retry Cnt最大值为7
144 :
145 : static constexpr u32 HCCL_UBOE_TIMEOUT_DEFAULT = 16; // UBOE默认TIMEOUT为16(对应8s)
146 : static constexpr u32 HCCL_UBOE_TIMEOUT_MIN = 0; // UBOE TIMEOUT最小值为0
147 : static constexpr u32 HCCL_UBOE_TIMEOUT_MAX = 31; // UBOE TIMEOUT最大值为31
148 : static constexpr u32 HCCL_UB_TIMEOUT_DEFAULT = 8; // UB默认TIMEOUT为8(对应1s)
149 : static constexpr u32 HCCL_UB_TIMEOUT_MIN = 0; // UB TIMEOUT最小值为0
150 : static constexpr u32 HCCL_UB_TIMEOUT_MAX = 31; // UB TIMEOUT最大值为31
151 :
152 : private:
153 : CfgField<u32> rdmaTrafficClass{
154 : "HCCL_RDMA_TC", u32(HCCL_RDMA_TC_DEFAULT), Str2T<u32>,
155 : CHK_RANGE_CLOSED<u32>(HCCL_RDMA_TC_MIN, HCCL_RDMA_TC_MAX), CheckRDMATrafficClass};
156 : CfgField<u32> rdmaServerLevel{
157 : "HCCL_RDMA_SL", u32(HCCL_RDMA_SL_DEFAULT), Str2T<u32>,
158 : CHK_RANGE_CLOSED<u32>(HCCL_RDMA_SL_MIN, HCCL_RDMA_SL_MAX)};
159 : CfgField<u32> rdmaTimeOut{
160 : "HCCL_RDMA_TIMEOUT", u32(HCCL_RDMA_TIMEOUT_DEFAULT), Str2T<u32>, CheckRdmaTimeout, ProcRdmaTimeout};
161 : CfgField<u32> rdmaRetryCnt{
162 : "HCCL_RDMA_RETRY_CNT", u32(HCCL_RDMA_RETRY_CNT_DEFAULT), Str2T<u32>,
163 : CHK_RANGE_CLOSED<u32>(HCCL_RDMA_RETRY_CNT_MIN, HCCL_RDMA_RETRY_CNT_MAX)};
164 : CfgField<u32> uboeTimeOut{
165 : "HCCL_UBOE_TIMEOUT", u32(HCCL_UBOE_TIMEOUT_DEFAULT), Str2T<u32>,
166 : CHK_RANGE_CLOSED<u32>(HCCL_UBOE_TIMEOUT_MIN, HCCL_UBOE_TIMEOUT_MAX)};
167 : CfgField<u32> ubTimeOut{
168 : "HCCL_UB_TIMEOUT", u32(HCCL_UB_TIMEOUT_DEFAULT), Str2T<u32>,
169 : CHK_RANGE_CLOSED<u32>(HCCL_UB_TIMEOUT_MIN, HCCL_UB_TIMEOUT_MAX)};
170 : CfgField<u32> queueNum{"HCCL_RDMA_QPS_PER_CONNECTION", u32(1), Str2T<u32>, CHK_RANGE_CLOSED<u32>(1, 32)};
171 : CfgField<u32> multiQpThreshold{
172 : "HCCL_MULTI_QP_THRESHOLD", u32(512 * 1024), Str2T<u32>, CHK_RANGE_CLOSED<u32>(1, 8192), ConvertUnitQpThreshold};
173 : void ParseMultiQpSrcPortConfig();
174 : HcclResult OpenMultiQpConfigFile(std::ifstream& inFile);
175 : HcclResult ParseConfigContent(std::ifstream& inFile, MultiQpSrcPortConfig& config);
176 : HcclResult ParseLineToIpPairAndPortPart(
177 : const std::string& lineInfo, u32 lineCnt, const std::string& lineAvator, std::string& ipPairKey,
178 : std::string& portPart);
179 : HcclResult ParseSrcPortsFromPortPart(
180 : const std::string& portPart, u32 lineCnt, const std::string& lineAvator, std::vector<std::uint16_t>& ports);
181 : void LogMultiQpSrcPortConfig() const;
182 : CfgField<std::string> qpPortConfigPath{
183 : "HCCL_RDMA_QP_PORT_CONFIG_PATH", "", Str2T<std::string>, CheckFilePath, SetRealPath};
184 : MultiQpSrcPortConfig multiQpSrcPortConfig_;
185 : };
186 :
187 : // 算法配置
188 : class EnvAlgoConfig : public BaseConfig {
189 : public:
190 : void Parse() override;
191 : const std::string& GetPrimQueueGenName() const;
192 : const std::map<OpType, std::vector<HcclAlgoType>> GetAlgoConfig() const;
193 : u64 GetBuffSize() const;
194 : HcclAccelerator GetHcclAccelerator() const;
195 : bool GetDeterministic() const;
196 :
197 : private:
198 : static constexpr u32 HCCL_CCL_COMM_DEFAULT_BUFFER_SIZE = 200;
199 : static constexpr u32 HCCL_CCL_COMM_BUFFER_MIN = 1;
200 : static constexpr u64 HCCL_CCL_COMM_FIXED_CALC_BUFFER_SIZE = (1 * 1024 * 1024);
201 :
202 : CfgField<std::string> primQueueGenName{"PRIM_QUEUE_GEN_NAME", "", Str2T<std::string>};
203 :
204 : CfgField<std::map<OpType, std::vector<HcclAlgoType>>> hcclAlgoConfig{
205 : "HCCL_ALGO", std::map<OpType, std::vector<HcclAlgoType>>(), SetHcclAlgoConfig};
206 :
207 : CfgField<u64> bufferSize{
208 : "HCCL_BUFFSIZE", HCCL_CCL_COMM_DEFAULT_BUFFER_SIZE* HCCL_CCL_COMM_FIXED_CALC_BUFFER_SIZE, Str2T<u64>,
209 2 : CHK_RANGE_CLOSED<u64>(HCCL_CCL_COMM_BUFFER_MIN, ULLONG_MAX), [](u64& i) {
210 2 : i *= HCCL_CCL_COMM_FIXED_CALC_BUFFER_SIZE;
211 2 : }};
212 : CfgField<HcclAccelerator> hcclAccelerator_{
213 : "HCCL_OP_EXPANSION_MODE", HcclAccelerator::AICPU_TS, CastHcclAccelerator};
214 : };
215 :
216 : // 日志/DFX配置
217 : class EnvLogConfig : public BaseConfig {
218 : public:
219 : void Parse() override;
220 : bool GetEntryLogEnable() const;
221 : const std::string& GetCannVersion() const;
222 : const DfsConfig& GetDfsConfig() const;
223 :
224 : private:
225 : CfgField<bool> entryLogEnable{"HCCL_ENTRY_LOG_ENABLE", false, CastBin2Bool};
226 : CfgField<std::string> cannVersion{"LD_LIBRARY_PATH", "", CastCannVersion};
227 : CfgField<DfsConfig> dfsConfig{"HCCL_DFS_CONFIG", DfsConfig(true, true, 0), CastDfsConfig};
228 : };
229 :
230 : // 绕路使能环境变量
231 : class EnvDetourConfig : public BaseConfig {
232 : public:
233 : void Parse() override;
234 : virtual HcclDetourType GetDetourType() const;
235 :
236 : private:
237 : CfgField<HcclDetourType> detourType{"HCCL_DETOUR", HcclDetourType::HCCL_DETOUR_DISABLE, CastDetourType};
238 : };
239 :
240 : } // namespace Hccl
241 :
242 : #endif // HCCLV2_BASE_CONFIG_H
|