Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 75 additions & 6 deletions deep_ep/utils/envs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# MIT License
#
# Copyright (c) 2025 DeepSeek
# Changes and additions copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import functools
import inspect
import os
Expand All @@ -6,7 +17,7 @@
import subprocess
import torch
import torch.distributed as dist
from typing import Tuple
from typing import Optional, Tuple

# noinspection PyUnresolvedReferences
import deep_ep._C as _C
Expand Down Expand Up @@ -242,17 +253,71 @@ def check_fast_rdma_atomic_support(nic_name: str = _DEFAULT_NIC_NAME) -> bool:
return False


def _get_sysfs_rdma_gbs(nic_name: str) -> float:
"""
Read one RDMA device's link rate from sysfs (`/sys/class/infiniband/<nic>/ports/*/rate`).
Works for any verbs provider (mlx5, EFA's `rdmap*`, ...) without external tools.

Arguments:
nic_name: the NIC device name.

Returns:
gbs: the device's link rate in GB/s (0 if the device or its rate is unavailable).
"""
rate = 0
ports_dir = os.path.join('/sys/class/infiniband', nic_name, 'ports')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 suggestion: sysfs 的 ports//rate 在端口 state 为 DOWN 时也可能报告一个速率。在 EP_NIC_NAME 未设置的『选最快设备』分支里,可能选中一个链路未激活的设备。可选改进:同时读取 ports//state,仅统计 ACTIVE("4: ACTIVE")端口。

🤖 v5

try:
for port in os.listdir(ports_dir):
with open(os.path.join(ports_dir, port, 'rate')) as f:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 suggestion: 正则 r'\s*(\d+)\sGb/sec' 无法匹配带小数的速率(如老式 SDR 1X 的 "2.5 Gb/sec"),此类设备会被解析为 0。可考虑改用 r'\s(\d+(?:.\d+)?)\s*Gb/sec' 并用 float 解析。对现代 HPC 网卡影响可忽略,仅作健壮性建议。

🤖 v5

match = re.match(r'\s*(\d+)\s*Gb/sec', f.read())
if match:
rate = max(rate, int(match.group(1)))
except OSError:
pass
return rate / 8


@functools.lru_cache()
def get_rdma_gbs(nic_name: str = _DEFAULT_NIC_NAME) -> float:
def get_rdma_gbs(nic_name: Optional[str] = None) -> float:
"""
Get the RDMA bandwidth in GB/s, cached.

Probes sysfs first, which covers any verbs provider; `ibstat` is kept as a fallback but
cannot see providers without a umad interface (e.g. EFA). Automatic device discovery
(the fastest device under `/sys/class/infiniband`) applies only when no NIC was named
at all -- neither via the `nic_name` argument nor via `EP_NIC_NAME`; an explicitly
named NIC never falls back to another device.

Arguments:
nic_name: the NIC device name.
nic_name: the NIC device name; None (the default) resolves to `EP_NIC_NAME`,
falling back to `mlx5_0`.

Returns:
gbs: the RDMA bandwidth in GB/s (0 if detection fails).
gbs: the RDMA bandwidth in GB/s.

Raises:
RuntimeError: when no probe can determine a link rate. Set `EP_NIC_NAME` to a
device listed under `/sys/class/infiniband`, or bypass detection by passing
the SM count explicitly (e.g. `--num-sms`).
"""
explicit = nic_name is not None or 'EP_NIC_NAME' in os.environ
if nic_name is None:
nic_name = _DEFAULT_NIC_NAME
gbs = _get_sysfs_rdma_gbs(nic_name)
if gbs > 0:
return gbs

# The un-named default may simply not exist on this fabric; an explicitly named NIC
# (argument or environment) must not fall back silently
if not explicit:
try:
devices = sorted(os.listdir('/sys/class/infiniband'))
except OSError:
devices = []
gbs = max((_get_sysfs_rdma_gbs(device) for device in devices), default=0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 warning: 当 /sys/class/infiniband 下没有任何设备(或所有设备的 rate 都读不到)且后续 ibstat 也失败时,get_rdma_gbs() 仍会返回 0;而 deep_ep/buffers/elastic.py 的 get_theoretical_num_sms() 在 num_rdma_ranks > 1 时会执行 rdma_traffic / rdma_gbs,仍会触发 ZeroDivisionError。既然目标是 survive probe failure,建议在调用处对探测结果为 0 的情况做兜底(例如回退到保守默认带宽或给出明确报错),而不是继续除以 0。

🤖 v4p

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now it will not exit with number 0, it will either give a positive number or report errors.

if gbs > 0:
return gbs

# noinspection PyBroadException
try:
result = subprocess.run(['ibstat'], capture_output=True, text=True, check=True)
Expand All @@ -264,5 +329,9 @@ def get_rdma_gbs(nic_name: str = _DEFAULT_NIC_NAME) -> float:
rate = int(match.group(1))
return rate / 8
except Exception as e:
print(f'Failed to get RDMA connection speed: {e}')
return 0
raise RuntimeError(
f"Could not determine the RDMA link rate for '{nic_name}': no readable rate under "
f"/sys/class/infiniband and `ibstat` failed ({e}). Set EP_NIC_NAME to a device "
f"listed under /sys/class/infiniband, or bypass detection by passing the SM count "
f"explicitly (e.g. `--num-sms`)."
) from e