diff --git a/docker/scripts/patch_vllm_platform.py b/docker/scripts/patch_vllm_platform.py index 8dbeccf..d047ed0 100644 --- a/docker/scripts/patch_vllm_platform.py +++ b/docker/scripts/patch_vllm_platform.py @@ -29,14 +29,14 @@ def patch6_init_platform_fallback(): f = os.path.join(VLLM_DIR, 'platforms', '__init__.py') lines = open(f).read().splitlines(keepends=True) - marker = "'vllm.platforms.rocm.RocmPlatform'" + # 兼容单/双引号两种写法(vllm 不同版本可能不同) return_idx = None for i, ln in enumerate(lines): - if marker in ln and 'return' in ln: + if 'RocmPlatform' in ln and 'return' in ln and 'is_rocm' in ln: return_idx = i break if return_idx is None: - print('Patch 6: return statement with RocmPlatform not found; skipping.') + print('Patch 6: RocmPlatform return statement not found; skipping.') return if 'torch.version.hip is not None' in ''.join(lines): @@ -207,50 +207,27 @@ def ensure_vllm_installed(): def install_sitecustomize_force_rocm(): - """安装 sitecustomize.py 运行时兜底:每个 Python 进程启动时, - 若 current_platform 落到 UnspecifiedPlatform 但 torch 检测到 ROCm/HIP, - 则强制替换为 RocmPlatform 实例。 - - vllm main 的 is_rocm 检测依赖 amdsmi,ROCm 7.2 容器内 amdsmi 缺失, - 补丁 6 的 torch.version.hip 兜底理论上已够,但子进程加载时序、 - 环境差异等可能导致检测仍失败。本 sitecustomize 作为最后一道兜底, - 确保任何 worker 进程都能拿到 device_type='cuda' 的平台。 + """已弃用:sitecustomize 时机问题(current_platform 是 lazy init, + sitecustomize 触发提前 resolve 时补丁 6 尚未应用)导致兜底无效。 + 保留空壳仅为兼容旧调用,实际不做任何事。 + 平台检测统一由补丁 6(rocm_platform_plugin torch.version.hip 兜底)解决。 """ + # 清理历史遗留的 sitecustomize 片段(幂等) site_packages = sysconfig.get_paths().get('purelib') - if not site_packages: - print('Patch 8: cannot locate site-packages; skipping sitecustomize.') - return - sc = os.path.join(site_packages, 'sitecustomize.py') - - marker = '# mineru-rocm: force rocm platform' - existing = '' - if os.path.exists(sc): - existing = open(sc).read() - if marker in existing: - print('Patch 8: sitecustomize force-rocm already installed.') - return - - snippet = ( - f"\n{marker}\n" - "try:\n" - " import torch as _t\n" - " if getattr(_t.version, 'hip', None) is not None:\n" - " import vllm.platforms as _vp\n" - " _cp = getattr(_vp, 'current_platform', None)\n" - " if _cp is not None and type(_cp).__name__ == 'UnspecifiedPlatform':\n" - " from vllm.platforms.rocm import RocmPlatform as _RP\n" - " _rp = _RP()\n" - " # vllm main 用模块级 __setattr__ 接管 current_platform 赋值\n" - " try:\n" - " _vp.current_platform = _rp\n" - " except Exception:\n" - " _vp._current_platform = _rp\n" - "except Exception:\n" - " pass\n" - ) - with open(sc, 'a') as fp: - fp.write(snippet) - print(f'Patch 8: sitecustomize force-rocm appended to {sc}.') + if site_packages: + sc = os.path.join(site_packages, 'sitecustomize.py') + marker = '# mineru-rocm: force rocm platform' + if os.path.exists(sc) and marker in open(sc).read(): + # 重写文件,移除我们的片段 + c = open(sc).read() + # 片段从 marker 行开始到文件末尾 + idx = c.find(marker) + # 回退到 marker 前的换行 + while idx > 0 and c[idx - 1] == '\n': + idx -= 1 + c = c[:idx].rstrip() + '\n' + open(sc, 'w').write(c) + print('Patch 8: removed legacy sitecustomize force-rocm snippet.') def main():