-
-
Notifications
You must be signed in to change notification settings - Fork 11k
[Chore] Separate out system utilities from vllm.utils #27201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Chore] Separate out system utilities from vllm.utils #27201
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request is a good step in refactoring vllm.utils by moving system-related utilities to a new system_utils.py module. The changes are clean and preserve the public API. I've found one instance of redundant code that should be removed.
c20c03a to
90ee481
Compare
|
Documentation preview: https://vllm--27201.org.readthedocs.build/en/27201/ |
b25b050 to
b17183d
Compare
Move environment, file path, and process management functions to vllm/utils/system_utils.py: - update_environment_variables: Update multiple environment variables - set_env_var: Temporarily set environment variable (context manager) - unique_filepath: Generate unique file paths - set_process_title: Set process title with optional suffix - decorate_logs: Add process name/PID prefix to stdout/stderr - _add_prefix: Internal helper for log decoration This reduces vllm/utils/__init__.py from 1310 to 1220 lines and groups related system-level utilities in a single, cohesive module. Contributes to vllm-project#26900 Signed-off-by: dongbo910220 <1275604947@qq.com>
b17183d to
72a9531
Compare
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request is a good step in refactoring vllm.utils by extracting system-level utilities into a new vllm.utils.system_utils module. The moved functions are now better organized and include some nice improvements, such as safer environment variable handling and checking for optional dependencies.
I've identified one area for improvement in the newly created system_utils.py file. The log decoration logic relies on monkey-patching sys.stdout and sys.stderr, which is a fragile approach. I've suggested a more robust implementation using a wrapper class. This change will make the logging functionality more reliable, especially in diverse environments like testing or when using custom loggers.
| def _add_prefix(file: TextIO, worker_name: str, pid: int) -> None: | ||
| """Add colored prefix to file output for log decoration.""" | ||
| prefix = f"{CYAN}({worker_name} pid={pid}){RESET} " | ||
| file_write = file.write | ||
|
|
||
| def write_with_prefix(s: str): | ||
| if not s: | ||
| return | ||
| if file.start_new_line: # type: ignore[attr-defined] | ||
| file_write(prefix) | ||
| idx = 0 | ||
| while (next_idx := s.find("\n", idx)) != -1: | ||
| next_idx += 1 | ||
| file_write(s[idx:next_idx]) | ||
| if next_idx == len(s): | ||
| file.start_new_line = True # type: ignore[attr-defined] | ||
| return | ||
| file_write(prefix) | ||
| idx = next_idx | ||
| file_write(s[idx:]) | ||
| file.start_new_line = False # type: ignore[attr-defined] | ||
|
|
||
| file.start_new_line = True # type: ignore[attr-defined] | ||
| file.write = write_with_prefix # type: ignore[method-assign] | ||
|
|
||
|
|
||
| def decorate_logs(process_name: str | None = None) -> None: | ||
| """Decorate stdout/stderr with process name and PID prefix.""" | ||
| from vllm.utils import get_mp_context | ||
|
|
||
| if process_name is None: | ||
| process_name = get_mp_context().current_process().name | ||
| pid = os.getpid() | ||
| _add_prefix(sys.stdout, process_name, pid) | ||
| _add_prefix(sys.stderr, process_name, pid) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of decorate_logs and _add_prefix relies on monkey-patching sys.stdout.write and sys.stderr.write, and adding a custom attribute start_new_line to the stream objects. This approach is fragile and can lead to unexpected errors if sys.stdout or sys.stderr are redirected to objects that do not support attribute or method assignment (e.g., in certain testing environments or with custom logging handlers). Additionally, the monkey-patched write method does not return the number of characters written, which violates the TextIO.write interface.
A more robust and cleaner approach is to use a wrapper class for the stream objects. This avoids modifying global objects directly, encapsulates the prefixing logic cleanly, and correctly implements the stream interface. The suggested change refactors this logic into a _LogPrefixStream class, making the log decoration more reliable and maintainable.
class _LogPrefixStream:
"""A stream wrapper that adds a prefix to each line of output."""
def __init__(self, stream: TextIO, prefix: str):
self._stream = stream
self._prefix = prefix
self.start_new_line = True
def write(self, s: str) -> int:
if not s:
return 0
if self.start_new_line:
self._stream.write(self._prefix)
idx = 0
while (next_idx := s.find("\n", idx)) != -1:
next_idx += 1
self._stream.write(s[idx:next_idx])
if next_idx == len(s):
self.start_new_line = True
return len(s)
self._stream.write(self._prefix)
idx = next_idx
self._stream.write(s[idx:])
self.start_new_line = False
return len(s)
def __getattr__(self, name: str):
return getattr(self._stream, name)
def decorate_logs(process_name: str | None = None) -> None:
"""Decorate stdout/stderr with process name and PID prefix."""
from vllm.utils import get_mp_context
if process_name is None:
process_name = get_mp_context().current_process().name
pid = os.getpid()
prefix = f"{CYAN}({process_name} pid={pid}){RESET} "
sys.stdout = _LogPrefixStream(sys.stdout, prefix)
sys.stderr = _LogPrefixStream(sys.stderr, prefix)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gemini didn't flag any missing imports so LGTM, see if the tests pass now
Update serve.py and api_server.py to import from vllm.utils.system_utils instead of vllm.utils for decorate_logs and set_process_title functions. This completes the migration for the system-utils refactor, ensuring all files use the new import paths without relying on backward compatibility shims. Fixes ImportError that would occur without backward compatibility. Signed-off-by: dongbo910220 <1275604947@qq.com>
Head branch was pushed to by a user without write access
|
Hi @DarkLight1337 ,It looks like the Docker build CI is failing. I took a look at the logs and found this error: Error: No solution found when resolving dependencies: apache-tvm-ffi==0.1.0b15 Since this PR only refactors some Python utility functions and doesn't touch any dependency or build configurations, I'm wondering if this might be an unrelated issue from the base branch . |
|
It should be fixed on main now, let me rebase |
|
Hi @DarkLight1337 , After merging the latest This PR only modifies Python code - function locations and import paths. No C++ code or |
|
PTAL at the failing tests |
Update tests/utils_/test_utils.py to import unique_filepath from vllm.utils.system_utils instead of vllm.utils, following the refactoring changes. Signed-off-by: dongbo910220 <1275604947@qq.com>
|
This pull request has merge conflicts that must be resolved before it can be |
Create tests/utils_/test_system_utils.py and move test_unique_filepath from test_utils.py to match the code refactoring where unique_filepath was moved to vllm.utils.system_utils module. Signed-off-by: dongbo910220 <1275604947@qq.com>
|
Please fix merge conflicts |
Resolve conflict in vllm/utils/__init__.py: - Keep optional dependency functions (has_pplx, has_deep_ep, etc.) from HEAD - Remove system utility functions (set_process_title, decorate_logs) added in main branch as they are already in vllm/utils/system_utils.py - Add missing importlib.util import for _has_module function Signed-off-by: dongbo910220 <1275604947@qq.com>
|
You seem to have accidentally added a file called |
Remove the accidentally added @@ file from the repository root. Signed-off-by: dongbo910220 <1275604947@qq.com>
…7201) Signed-off-by: dongbo910220 <1275604947@qq.com> Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
…7201) Signed-off-by: dongbo910220 <1275604947@qq.com> Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk> Signed-off-by: Alberto Perdomo <aperdomo@redhat.com>
…o step_forward * 'step_forward' of https://github.com/raindaywhu/vllm: (148 commits) [Model] Add MoE support for NemotronH (vllm-project#25863) [Metrics] [KVConnector] Add connector prefix cache hit rate stats (vllm-project#26245) [CI] Reorganize entrypoints tests (vllm-project#27403) add SLA information into comparison graph for vLLM Benchmark Suite (vllm-project#25525) [CI/Build] Fix AMD CI: test_cpu_gpu.py (vllm-project#27388) [Bugfix] Fix args settings for guided decoding args (vllm-project#27375) [CI/Build] Fix Prithvi plugin test (vllm-project#27393) [Chore] Remove duplicate `has_` functions in vllm.utils (vllm-project#27372) [Model] Add num_cached_tokens for PoolingRequestOutput (vllm-project#27378) [V1][spec decode] return logprobs for spec decoding (vllm-project#26060) [CORE] Support Prefix Caching with Prompt Embeds (vllm-project#27219) [Bugfix][Core] running queue index leakage exception (vllm-project#26754) [Bugfix] Fix incorrect kv cache metrics in grafana.json (vllm-project#27133) [Bugfix] Fix SLA tuner initialization (vllm-project#27355) [Bugfix] Fix deepseek-ocr multi-image inference and add `merge_by_field_config=True` with tensor schema support (vllm-project#27361) [MLA] Bump FlashMLA (vllm-project#27354) [Chore] Separate out system utilities from vllm.utils (vllm-project#27201) [BugFix] bugfix for Flash Attention MLA with full cuda graph IMA following pr-25490 (vllm-project#27128) [Feature] publisher default set zmq in kv_event config (vllm-project#26915) [Prefix Cache] Use LoRA name for consistent KV-cache block hashing (vllm-project#27211) ...
…7201) Signed-off-by: dongbo910220 <1275604947@qq.com> Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
…7201) Signed-off-by: dongbo910220 <1275604947@qq.com> Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk> Signed-off-by: 0xrushi <6279035+0xrushi@users.noreply.github.com>
…7201) Signed-off-by: dongbo910220 <1275604947@qq.com> Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk> Signed-off-by: 0xrushi <6279035+0xrushi@users.noreply.github.com>
Part of #26900
This PR continues the effort to clean up
vllm.utilsby extracting various system-level helper functions into a new, dedicated modulevllm/utils/system_utils.py.The following functions have been moved:
vllm.utils.update_environment_variables -> vllm.utils.system_utils.update_environment_variablesvllm.utils.set_env_var -> vllm.utils.system_utils.set_env_varvllm.utils.unique_filepath -> vllm.utils.system_utils.unique_filepathvllm.utils.set_process_title -> vllm.utils.system_utils.set_process_titlevllm.utils.decorate_logs -> vllm.utils.system_utils.decorate_logsCC: @DarkLight1337
Test Plan
Test Result