Prhub

#39100 [Deprecation] Deprecate cprofile and cprofile_context

原始 PR 作者 yewentao256 合并时间 2026-04-21 11:25 文件变更 2 提交数 1 评论 4 代码增减 +12 / -2

执行摘要

弃用 vLLM 内部未使用的 cProfile 辅助函数,引导用户直接使用 Python 原生模块。

根据 PR 描述,cprofilecprofile_context 在 vLLM 内部完全没有被使用。但由于存在相关文档,直接删除可能影响用户,因此决定先进行弃用,为后续移除提供过渡期。

此 PR 变更简单直接,无需深入技术分析。对于维护者,值得关注的点在于其遵循了“先弃用后移除”的良好实践,为外部用户提供了明确的迁移路径和版本时间线(v0.21)。

讨论亮点

Review 讨论非常简短。作者 @yewentao256 在 Issue 评论中说明此部分“完全不使用,移除风险低”,并请求 @DarkLight1337 协助合并。@DarkLight1337 最终批准了 PR。没有出现关于弃用策略、替代方案或兼容性的技术争议。

实现拆解

  1. 源码弃用标记:在 vllm/utils/profiling.py 中,为 cprofile_contextcprofile 函数添加了 @deprecated 装饰器,并导入了 typing_extensions.deprecated。装饰器消息明确指出函数将在 v0.21 版本移除,并建议用户直接使用 Python 的 cProfile 模块。
  2. 文档同步更新:在 docs/contributing/profiling.md 中,将关于 vllm.utils.profiling 辅助函数的说明从“已弃用旧导入路径”更新为“辅助函数已弃用,将在 v0.21 移除,请直接使用 Python 的 cProfile 模块”。
  3. 无测试或配置改动:本次变更不涉及功能逻辑修改,仅为弃用标记和文档更新,因此没有配套的测试、配置或部署改动。
文件 模块 状态 重要度
vllm/utils/profiling.py 工具模块 modified 4.94
docs/contributing/profiling.md 文档 modified 1.9

关键符号

cprofile_context cprofile

关键源码片段

vllm/utils/profiling.py deprecation-marking

这是被弃用函数的源码定义文件,添加弃用装饰器是核心变更。

from typing_extensions import deprecated # 新增导入,用于添加弃用警告
​
​
@deprecated(
    "vllm.utils.profiling.cprofile_context() is deprecated and will be removed "
    "in v0.21. Use Python's cProfile module directly instead."
) # 弃用装饰器,明确移除版本和替代方案
@contextlib.contextmanager
def cprofile_context(save_file: str | None = None):
    """Run a cprofile    Args:
        save_file: path to save the profile result. "1" or
            None will result in printing to stdout.
    """
    import cProfile
    prof = cProfile.Profile()
    prof.enable()
    try:
        yield
    finally:
        prof.disable()
        if save_file and save_file != "1":
            prof.dump_stats(save_file)
        else:
            prof.print_stats(sort="cumtime")
​
​
@deprecated(
    "vllm.utils.profiling.cprofile() is deprecated and will be removed in "
    "v0.21. Use Python's cProfile module directly instead."
) # 装饰器应用于装饰器函数本身
def cprofile(save_file: str | None = None, enabled: bool = True):
    """Decorator to profile a Python method using cProfile."""
    def decorator(func: Callable):
        @wraps(func)
        def wrapper(*args: Any, **kwargs: Any):
            if not enabled:
                return func(*args, **kwargs)
            with cprofile_context(save_file): # 内部仍调用已弃用的 context manager
                return func(*args, **kwargs)
        return wrapper
    return decorator

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

技术风险极低

  • 回归风险:无。函数实现未改变,仅添加了弃用警告。如果外部用户代码依赖这些函数,在 v0.21 移除前仍可正常工作,但会收到弃用警告。
  • 兼容性风险:低。弃用警告可能影响依赖这些函数的用户脚本的日志输出,但不会破坏其功能。
  • 性能与安全风险:无。变更不涉及运行时逻辑。

影响范围有限,程度轻微

  • 对用户:使用 vllm.utils.profiling.cprofilecprofile_context 的用户将在调用时收到弃用警告,需要计划迁移至 Python 原生 cProfile。由于函数在 vLLM 内部未使用,受影响的用户群体预计很小。
  • 对系统:无功能或性能影响。
  • 对团队:清理了未使用的代码,简化了代码库维护负担,并为未来移除做好了准备。
外部依赖警告

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论