Files

71 lines
2.2 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""幂等地把 pplx 的权重前缀映射补丁装进镜像固定目录。
本仓库只有**一个**补丁:`hf_to_vllm_mapper` 的裸 `language_model.*`
→ `language_model.model.*` 映射。
为什么必须进镜像(不能只改参数)
--------------------------------
`WeightsMapper` 是 `Qwen3_5ForConditionalGeneration` 的**类属性**,
只能在 import 期改。不打 → `load_weights` 报
`ValueError: There is no module or parameter named 'embed_tokens' in Qwen3_5ForCausalLM`
(2026-11-02 `pplxflash_real.log` / `pplxflash_tp4.log`)。
与 biren166m 那份的区别
------------------------
BR166M 用 `orig_to_new_regex`(vLLM 0.21.0 才有);BI-150 的 vLLM 0.17.0
没有 regex,且 substr 会二次替换已经正确的 `model.language_model.*`,
所以这里用「继承 WeightsMapper 覆写 _map_name」。
幂等性
------
- 目标已存在且 md5 相同 → `[skip]`
- 内容不同 → 覆盖并打印 `[ok]`
- 写不进去(只读挂载)→ 报错退出,**不静默跳过**
"""
from __future__ import annotations
import hashlib
import os
import shutil
import sys
SRC = os.environ.get("PPLX_PATCH_SRC", "/opt/pplx/pplx-prefix-mapper-sitecustomize.py")
DST_DIR = os.environ.get("PPLX_PATCH_DIR", "/opt/pplx-patch")
DST = os.path.join(DST_DIR, "sitecustomize.py")
def md5_of(path: str) -> str:
h = hashlib.md5()
with open(path, "rb") as fh:
for chunk in iter(lambda: fh.read(65536), b""):
h.update(chunk)
return h.hexdigest()
def main() -> int:
if not os.path.isfile(SRC):
print(f"ERROR: 补丁源文件不存在:{SRC}", file=sys.stderr)
return 1
src_md5 = md5_of(SRC)
if os.path.isfile(DST) and md5_of(DST) == src_md5:
print(f"[skip] pplx 补丁已是目标版本 ({DST}, md5={src_md5})")
return 0
try:
os.makedirs(DST_DIR, exist_ok=True)
shutil.copyfile(SRC, DST)
except OSError as exc:
print(f"ERROR: 无法写入 {DST}: {exc}", file=sys.stderr)
print(" 如果 /opt/pplx-patch 是只读挂载,请改用 PPLX_PATCH_DIR。", file=sys.stderr)
return 1
print(f"[ok] pplx 补丁已安装到 {DST} (md5={src_md5})")
return 0
if __name__ == "__main__":
raise SystemExit(main())