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