Skip to content

Conversation

@dongbo910220
Copy link
Contributor

@dongbo910220 dongbo910220 commented Oct 20, 2025

Part of #26900

This PR continues the effort to clean up vllm.utils by extracting various system-level helper functions into a new, dedicated module vllm/utils/system_utils.py.

The following functions have been moved:

  • vllm.utils.update_environment_variables -> vllm.utils.system_utils.update_environment_variables
  • vllm.utils.set_env_var -> vllm.utils.system_utils.set_env_var
  • vllm.utils.unique_filepath -> vllm.utils.system_utils.unique_filepath
  • vllm.utils.set_process_title -> vllm.utils.system_utils.set_process_title
  • vllm.utils.decorate_logs -> vllm.utils.system_utils.decorate_logs

CC: @DarkLight1337

Test Plan

Test Result

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

@mergify
Copy link

mergify bot commented Oct 20, 2025

Documentation preview: https://vllm--27201.org.readthedocs.build/en/27201/

@mergify mergify bot added documentation Improvements or additions to documentation multi-modality Related to multi-modality (#4194) performance Performance-related issues v1 labels Oct 20, 2025
@dongbo910220 dongbo910220 force-pushed the feature/refactor-system-utils branch 3 times, most recently from b25b050 to b17183d Compare October 20, 2025 18:52
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>
@dongbo910220 dongbo910220 force-pushed the feature/refactor-system-utils branch from b17183d to 72a9531 Compare October 20, 2025 19:44
@DarkLight1337
Copy link
Member

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines +89 to +123
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

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)

Copy link
Member

@DarkLight1337 DarkLight1337 left a 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

@DarkLight1337 DarkLight1337 enabled auto-merge (squash) October 21, 2025 01:12
@github-actions github-actions bot added the ready ONLY add when PR is ready to merge/full CI is needed label Oct 21, 2025
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>
auto-merge was automatically disabled October 21, 2025 21:58

Head branch was pushed to by a user without write access

@dongbo910220 dongbo910220 requested a review from aarnphm as a code owner October 21, 2025 21:58
@mergify mergify bot added the frontend label Oct 21, 2025
@dongbo910220
Copy link
Contributor Author

dongbo910220 commented Oct 22, 2025

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 .

@DarkLight1337
Copy link
Member

It should be fixed on main now, let me rebase

@dongbo910220
Copy link
Contributor Author

dongbo910220 commented Oct 22, 2025

Hi @DarkLight1337 , After merging the latest main branch, the CI failures are still the same error we saw in the previous run.
The persistent error:
All failing tests show the identical error:
ImportError while loading conftest '/vllm-workspace/tests/conftest.py'.
ImportError: /usr/local/lib/python3.12/dist-packages/vllm/_C.abi3.so:
undefined symbol: ZNK3c106SymInt6sym_neERKS0

This PR only modifies Python code - function locations and import paths. No C++ code or
compilation configuration was touched.
Maybe we could wait until this issue is resolved

@DarkLight1337
Copy link
Member

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>
@mergify
Copy link

mergify bot commented Oct 22, 2025

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @dongbo910220.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify bot added the needs-rebase label Oct 22, 2025
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>
@DarkLight1337
Copy link
Member

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>
@DarkLight1337 DarkLight1337 enabled auto-merge (squash) October 22, 2025 17:13
@mergify mergify bot removed the needs-rebase label Oct 22, 2025
@DarkLight1337
Copy link
Member

You seem to have accidentally added a file called @@ at the root

Remove the accidentally added @@ file from the repository root.

Signed-off-by: dongbo910220 <1275604947@qq.com>
@DarkLight1337 DarkLight1337 enabled auto-merge (squash) October 22, 2025 17:19
@DarkLight1337 DarkLight1337 merged commit a0003b5 into vllm-project:main Oct 22, 2025
60 checks passed
usberkeley pushed a commit to usberkeley/vllm that referenced this pull request Oct 23, 2025
…7201)

Signed-off-by: dongbo910220 <1275604947@qq.com>
Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
albertoperdomo2 pushed a commit to albertoperdomo2/vllm that referenced this pull request Oct 23, 2025
…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>
845473182 pushed a commit to raindaywhu/vllm that referenced this pull request Oct 24, 2025
…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)
  ...
kingsmad pushed a commit to kingsmad/vllm that referenced this pull request Oct 25, 2025
…7201)

Signed-off-by: dongbo910220 <1275604947@qq.com>
Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
0xrushi pushed a commit to 0xrushi/vllm that referenced this pull request Oct 26, 2025
…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>
0xrushi pushed a commit to 0xrushi/vllm that referenced this pull request Oct 26, 2025
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation frontend multi-modality Related to multi-modality (#4194) performance Performance-related issues ready ONLY add when PR is ready to merge/full CI is needed v1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants