From 13b3abaa6a896e65fc9a11d8aedc83afd80804ee Mon Sep 17 00:00:00 2001 From: Yida Weng <84164537+YidaWeng@users.noreply.github.com> Date: Thu, 1 Oct 2026 18:17:55 +0800 Subject: [PATCH] Fix EP_BUFFER_DEBUG=0 enabling Python-side debug output Parse EP_BUFFER_DEBUG with int() before truth-testing, matching the documented 0/1 contract, C++ get_env, and every other EP_* boolean env flag in the package. The raw string '0' is truthy in Python, so the documented off value incorrectly enabled the two print paths in EPBuffer. Add a host-side regression test that does not require CUDA or the compiled extension. Fixes #718 --- deep_ep/buffers/ep.py | 4 +- tests/utils/test_ep_buffer_debug_env.py | 72 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 tests/utils/test_ep_buffer_debug_env.py diff --git a/deep_ep/buffers/ep.py b/deep_ep/buffers/ep.py index 9b8a95364..6a74c2154 100644 --- a/deep_ep/buffers/ep.py +++ b/deep_ep/buffers/ep.py @@ -306,7 +306,7 @@ def __init__(self, num_max_tokens_per_rank, hidden, num_topk, use_fp8_dispatch, allow_hybrid_mode, allow_multiple_reduction) - if os.environ.get('EP_BUFFER_DEBUG', 0): + if int(os.environ.get('EP_BUFFER_DEBUG', 0)): print(f'Initializing EP buffer with {num_bytes} bytes at rank EP {group.rank()}/{group.size()}') self.num_bytes = num_bytes @@ -532,7 +532,7 @@ def get_expected_topk(num_groups: int) -> float: num_sms = min(num_sms, num_device_sms // 2 * 2) # Summary - if os.environ.get('EP_BUFFER_DEBUG', 0): + if int(os.environ.get('EP_BUFFER_DEBUG', 0)): print(f'EP SM approximation: ' f'{sm_read=}, {sm_write=}, {rdma_traffic=}, {nvlink_traffic=}, ' f'{rdma_gbs=}, {nvlink_gbs=}, ' diff --git a/tests/utils/test_ep_buffer_debug_env.py b/tests/utils/test_ep_buffer_debug_env.py new file mode 100644 index 000000000..cb24778ce --- /dev/null +++ b/tests/utils/test_ep_buffer_debug_env.py @@ -0,0 +1,72 @@ +"""Regression for #718: EP_BUFFER_DEBUG=0 must disable Python-side debug output. + +Host-side only. Does not import `deep_ep` (that path needs the compiled extension +and NCCL). Validates the same `int(os.environ.get(...))` idiom used at the two +call sites in `deep_ep/buffers/ep.py`, and checks those call sites still wrap +the lookup in `int()`. +""" + +from __future__ import annotations + +import ast +import os +from pathlib import Path + + +def _ep_buffer_debug_enabled() -> bool: + # Same expression as deep_ep/buffers/ep.py after the fix for #718. + return bool(int(os.environ.get('EP_BUFFER_DEBUG', 0))) + + +def test_ep_buffer_debug_truth_table() -> None: + cases = [ + (None, False), # unset → default 0 + ('0', False), + ('1', True), + ('2', True), + ] + original = os.environ.get('EP_BUFFER_DEBUG') + try: + for value, expected in cases: + if value is None: + os.environ.pop('EP_BUFFER_DEBUG', None) + else: + os.environ['EP_BUFFER_DEBUG'] = value + assert _ep_buffer_debug_enabled() is expected, f'EP_BUFFER_DEBUG={value!r}' + finally: + if original is None: + os.environ.pop('EP_BUFFER_DEBUG', None) + else: + os.environ['EP_BUFFER_DEBUG'] = original + + +def test_ep_py_call_sites_use_int() -> None: + """Both EP_BUFFER_DEBUG guards in ep.py must convert with int() before testing.""" + ep_py = Path(__file__).resolve().parents[2] / 'deep_ep' / 'buffers' / 'ep.py' + tree = ast.parse(ep_py.read_text()) + matches = 0 + for node in ast.walk(tree): + if not isinstance(node, ast.Call): + continue + # Match: int(os.environ.get('EP_BUFFER_DEBUG', ...)) + if not isinstance(node.func, ast.Name) or node.func.id != 'int': + continue + if len(node.args) != 1: + continue + inner = node.args[0] + if not isinstance(inner, ast.Call): + continue + if not (isinstance(inner.func, ast.Attribute) and inner.func.attr == 'get'): + continue + if not inner.args: + continue + key = inner.args[0] + if isinstance(key, ast.Constant) and key.value == 'EP_BUFFER_DEBUG': + matches += 1 + assert matches == 2, f'expected 2 int(os.environ.get("EP_BUFFER_DEBUG", ...)) sites, found {matches}' + + +if __name__ == '__main__': + test_ep_buffer_debug_truth_table() + test_ep_py_call_sites_use_int() + print('test_ep_buffer_debug_env: ok')