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 "ip_address.h"
18 : #include "dma_mode.h"
19 : #include "env_func.h"
20 : #include "cfg_field.h"
21 :
22 : namespace Hccl {
23 :
24 : class BaseConfig {
25 : public:
26 : virtual void Parse() = 0;
27 : };
28 :
29 : // Host网卡配置
30 : class EnvHostNicConfig : public BaseConfig {
31 : public:
32 : void Parse() override;
33 : const IpAddress &GetControlIfIp() const;
34 : u32 GetIfBasePort() const;
35 : const SocketIfName &GetSocketIfName() const;
36 : bool GetWhitelistDisable() const;
37 : const std::string &GetWhiteListFile() const;
38 : const std::vector<SocketPortRange> &GetHostSocketPortRange() const;
39 : const std::vector<SocketPortRange> &GetDeviceSocketPortRange() const;
40 :
41 : private:
42 : static constexpr u32 HCCL_INVALIED_IF_BASE_PORT = 65536; // HCCL默认无效端口号
43 : static constexpr u32 HCCL_INVALIED_IF_BASE_PORT_MAX = 65520; // HCCL端口号最大值
44 : static constexpr u32 HCCL_INVALIED_IF_BASE_PORT_MIN = 1024; // HCCL端口号最小值
45 :
46 : CfgField<IpAddress> hcclIfIp{"HCCL_IF_IP", {}, Str2T<IpAddress>};
47 : CfgField<u32> hcclIfBasePort{"HCCL_IF_BASE_PORT", u32(HCCL_INVALIED_IF_BASE_PORT), Str2T<u32>,
48 : CHK_RANGE_CLOSED<u32>(HCCL_INVALIED_IF_BASE_PORT_MIN, HCCL_INVALIED_IF_BASE_PORT_MAX)};
49 : CfgField<SocketIfName> hcclSocketIfName{"HCCL_SOCKET_IFNAME", SocketIfName({}, false, false), CastSocketIfName};
50 : CfgField<bool> whitelistDisable{"HCCL_WHITELIST_DISABLE", true, CastBin2Bool};
51 : CfgField<std::string> hcclWhiteListFile{"HCCL_WHITELIST_FILE", "", Str2T<std::string>, CheckFilePath, SetRealPath};
52 : CfgField<std::vector<SocketPortRange>> hcclHostSocketPortRange{"HCCL_HOST_SOCKET_PORT_RANGE", {},
53 3 : [] (const std::string &s) -> std::vector<SocketPortRange> { return CastSocketPortRange(s, "HCCL_HOST_SOCKET_PORT_RANGE"); }};
54 : CfgField<std::vector<SocketPortRange>> hcclDeviceSocketPortRange{"HCCL_NPU_SOCKET_PORT_RANGE", {},
55 0 : [] (const std::string &s) -> std::vector<SocketPortRange> { return CastSocketPortRange(s, "HCCL_NPU_SOCKET_PORT_RANGE"); }};
56 : };
57 :
58 : // Socket公共配置
59 : class EnvSocketConfig : public BaseConfig {
60 : public:
61 : void Parse() override;
62 : s32 GetSocketFamily() const;
63 : s32 GetLinkTimeOut() const;
64 :
65 : private:
66 : static constexpr s32 HCCL_LINK_TIME_OUT_S = 120; // HCCL 默认的建链超时时间设置为120s
67 : static constexpr s32 HCCL_MIN_LINK_TIME_OUT_S = 120; // HCCL 建链最小超时时间设置为120s
68 : static constexpr s32 HCCL_MAX_LINK_TIME_OUT_S = (120 * 60); // HCCL 最大建链超时时间设置为120*60s
69 :
70 : CfgField<s32> hcclSocketFamily{"HCCL_SOCKET_FAMILY", -1, CastSocketFamily};
71 : CfgField<s32> linkTimeOut{"HCCL_CONNECT_TIMEOUT", s32(HCCL_LINK_TIME_OUT_S), Str2T<s32>,
72 : CHK_RANGE_CLOSED<s32>(HCCL_MIN_LINK_TIME_OUT_S, HCCL_MAX_LINK_TIME_OUT_S)};
73 : };
74 :
75 : // RTS配置
76 : class EnvRtsConfig : public BaseConfig {
77 : public:
78 : void Parse() override;
79 : u32 GetExecTimeOut() const;
80 : double GetAivExecTimeOut() const;
81 :
82 : private:
83 : static constexpr s32 NOTIFY_DEFAULT_WAIT_TIME = 27 * 68; // notifywait默认1836等待时长
84 : static constexpr s32 AIV_TIMEOUT_DEFAULT = 1091;
85 :
86 : CfgField<u32> execTimeOut{
87 : "HCCL_EXEC_TIMEOUT",
88 : static_cast<u32>(NOTIFY_DEFAULT_WAIT_TIME),
89 7 : [](const std::string& s) -> u32 {
90 7 : static std::regex validFormat(R"(^\d+(\.\d{1,2})?$)");
91 7 : if (!std::regex_match(s, validFormat)) {
92 1 : THROW<InvalidParamsException>(StringFormat(
93 : "Invalid config value, execTimeOutStr[%s], up to two decimal places",
94 : s.c_str()));
95 : }
96 6 : return String2T<u32>(s);
97 : },
98 : CheckExecTimeOut,
99 : ProcExecTimeOut
100 : };
101 :
102 : CfgField<double> aivExecTimeOut{
103 : "HCCL_EXEC_TIMEOUT",
104 : double(AIV_TIMEOUT_DEFAULT),
105 1 : [](const std::string& s) -> double {
106 1 : static std::regex validFormat(R"(^\d+(\.\d{1,2})?$)");
107 1 : if (!std::regex_match(s, validFormat)) {
108 0 : THROW<InvalidParamsException>(StringFormat(
109 : "Invalid config value, execTimeOutStr[%s], up to two decimal places",
110 : s.c_str()));
111 : }
112 1 : return String2T<double>(s);
113 : },
114 : nullptr,
115 : nullptr
116 : };
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 :
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{"HCCL_RDMA_TC", u32(HCCL_RDMA_TC_DEFAULT), Str2T<u32>,
154 : CHK_RANGE_CLOSED<u32>(HCCL_RDMA_TC_MIN, HCCL_RDMA_TC_MAX), CheckRDMATrafficClass};
155 : CfgField<u32> rdmaServerLevel{"HCCL_RDMA_SL", u32(HCCL_RDMA_SL_DEFAULT), Str2T<u32>,
156 : CHK_RANGE_CLOSED<u32>(HCCL_RDMA_SL_MIN, HCCL_RDMA_SL_MAX)};
157 : CfgField<u32> rdmaTimeOut{"HCCL_RDMA_TIMEOUT", u32(HCCL_RDMA_TIMEOUT_DEFAULT), Str2T<u32>,
158 : CheckRdmaTimeout, ProcRdmaTimeout};
159 : CfgField<u32> rdmaRetryCnt{"HCCL_RDMA_RETRY_CNT", u32(HCCL_RDMA_RETRY_CNT_DEFAULT), Str2T<u32>,
160 : CHK_RANGE_CLOSED<u32>(HCCL_RDMA_RETRY_CNT_MIN, HCCL_RDMA_RETRY_CNT_MAX)};
161 : CfgField<u32> uboeTimeOut{"HCCL_UBOE_TIMEOUT", u32(HCCL_UBOE_TIMEOUT_DEFAULT), Str2T<u32>,
162 : CHK_RANGE_CLOSED<u32>(HCCL_UBOE_TIMEOUT_MIN, HCCL_UBOE_TIMEOUT_MAX)};
163 : CfgField<u32> ubTimeOut{"HCCL_UB_TIMEOUT", u32(HCCL_UB_TIMEOUT_DEFAULT), Str2T<u32>,
164 : CHK_RANGE_CLOSED<u32>(HCCL_UB_TIMEOUT_MIN, HCCL_UB_TIMEOUT_MAX)};
165 : CfgField<u32> queueNum{"HCCL_RDMA_QPS_PER_CONNECTION", u32(1), Str2T<u32>,
166 : CHK_RANGE_CLOSED<u32>(1, 32)};
167 : CfgField<u32> multiQpThreshold{"HCCL_MULTI_QP_THRESHOLD", u32(512 * 1024), Str2T<u32>,
168 : CHK_RANGE_CLOSED<u32>(1, 8192), ConvertUnitQpThreshold};
169 : };
170 :
171 : // 算法配置
172 : class EnvAlgoConfig : public BaseConfig {
173 : public:
174 : void Parse() override;
175 : const std::string &GetPrimQueueGenName() const;
176 : const std::map<OpType, std::vector<HcclAlgoType>> &GetAlgoConfig() const;
177 : u64 GetBuffSize() const;
178 : HcclAccelerator GetHcclAccelerator() const;
179 : bool GetDeterministic() const;
180 :
181 : private:
182 : static constexpr u32 HCCL_CCL_COMM_DEFAULT_BUFFER_SIZE = 200;
183 : static constexpr u32 HCCL_CCL_COMM_BUFFER_MIN = 1;
184 : static constexpr u64 HCCL_CCL_COMM_FIXED_CALC_BUFFER_SIZE = (1 * 1024 * 1024);
185 :
186 : CfgField<std::string> primQueueGenName{"PRIM_QUEUE_GEN_NAME", "", Str2T<std::string>};
187 :
188 : CfgField<std::map<OpType, std::vector<HcclAlgoType>>> hcclAlgoConfig{
189 : "HCCL_ALGO", std::map<OpType, std::vector<HcclAlgoType>> (), SetHcclAlgoConfig};
190 :
191 : CfgField<u64> bufferSize{"HCCL_BUFFSIZE", HCCL_CCL_COMM_DEFAULT_BUFFER_SIZE *HCCL_CCL_COMM_FIXED_CALC_BUFFER_SIZE,
192 2 : Str2T<u64>, CHK_RANGE_CLOSED<u64>(HCCL_CCL_COMM_BUFFER_MIN, ULLONG_MAX), [](u64 &i) {
193 2 : i *= HCCL_CCL_COMM_FIXED_CALC_BUFFER_SIZE;
194 2 : }};
195 : CfgField<HcclAccelerator> hcclAccelerator_{"HCCL_OP_EXPANSION_MODE", HcclAccelerator::AICPU_TS,
196 : CastHcclAccelerator};
197 : };
198 :
199 : // 日志/DFX配置
200 : class EnvLogConfig : public BaseConfig {
201 : public:
202 : void Parse() override;
203 : bool GetEntryLogEnable() const;
204 : const std::string &GetCannVersion() const;
205 : const DfsConfig &GetDfsConfig() const;
206 : private:
207 : CfgField<bool> entryLogEnable{"HCCL_ENTRY_LOG_ENABLE", false, CastBin2Bool};
208 : CfgField<std::string> cannVersion{"LD_LIBRARY_PATH", "", CastCannVersion};
209 : CfgField<DfsConfig> dfsConfig{"HCCL_DFS_CONFIG", DfsConfig(true, true, 0), CastDfsConfig};
210 : };
211 :
212 : // 绕路使能环境变量
213 : class EnvDetourConfig : public BaseConfig {
214 : public:
215 : void Parse() override;
216 : virtual HcclDetourType GetDetourType() const;
217 :
218 : private:
219 : CfgField<HcclDetourType> detourType{"HCCL_DETOUR", HcclDetourType::HCCL_DETOUR_DISABLE, CastDetourType};
220 : };
221 :
222 : } // namespace Hccl
223 :
224 : #endif // HCCLV2_BASE_CONFIG_H
|