convert_to_vllm.py only does three things, and all three can be done at runtime:
1. text_config.model_type kormo -> llama
=> CONFIG_MAPPING.register("kormo", LlamaConfig)
2. text_config.architectures KORMoForCausalLM -> LlamaForCausalLM
=> ModelRegistry.register_model("KORMoForCausalLM", LlamaForCausalLM)
3. weight keys pre_attention_layernorm -> input_layernorm,
pre_mlp_layernorm -> post_attention_layernorm
=> LlavaOnevisionForConditionalGeneration.hf_to_vllm_mapper
|= WeightsMapper(orig_to_new_substr=...)
The re-shard step needs nothing at all (vLLM reads single-file and sharded
checkpoints alike). Verified on the checkpoint: 805 tensors -> 805 tensors and
rename(src_keys) == converted_keys is True, i.e. the conversion is pure renaming.
So the model directory can stay read-only and untouched: the image carries one
sitecustomize.py that also covers the two things the original dir needs and the
converted dir does not:
4. TokenizersBackend -> PreTrainedTokenizerFast
the original tokenizer_config.json names a transformers-5 class that tf4
(vLLM 0.17/0.21) does not know; tokenizer.json is standard fast data.
5. SigLIP require_post_norm=True (unchanged behaviour from v0.1.0)
Two new real bugs found while testing the image with a real screenshot:
6. get_placeholder_str must return "<|image|>" (= config.image_token_index
125041), not vLLM's default "<image>". "<image>" is not a single token in
the KORMo vocab (tokenizes to [27, 12177, 29]), so every image request
failed with: Failed to apply prompt replacement for mm_items['image'][0].
This affects the CONVERTED dir too -- 2026-11-20 never exercised images.
7. SigLIP position_ids is a persistent=False CPU buffer; weight loading does
not move it, so on non-CUDA backends embedding() hits a device mismatch.
Move it to pixel_values.device in forward.
Verified with the built image (kormo11b-bi150:orig-patch, TP4, original dir):
/health 200 (115 s cold start), /v1/models ['llm']
Loading weights took 2.92 s, missing weights 0
patch applied: config/mapper/mm_placeholder/postnorm/registry/siglip_pos/tokenizer
text chat OK
image request -> fin=stop tool=finished "The title of the current page is
'ZEBRA-42 Search Portal'." (that string is in the screenshot)
click request -> fin=stop tool=click x=500 y=308
Also note in the README: the official protocol prompt is ~3585 tokens
(system 717 + tools 2855 + observation), so MAX_MODEL_LEN must be raised
(16384) or every protocol request returns HTTP 400 context-length.
103 lines
4.3 KiB
Python
103 lines
4.3 KiB
Python
"""把 KORMo-Web-11B 的 vLLM 补丁装进镜像固定目录。
|
||
|
||
**只有一份 sitecustomize.py**。`PYTHONPATH` 上不允许出现第二份同名的
|
||
`sitecustomize` —— 后导入的那份会覆盖先导入的,表现为"补丁时灵时不灵"。
|
||
|
||
这份补丁同时覆盖 **原始目录** 与 **转换后目录**:
|
||
|
||
① CONFIG_MAPPING.register("kormo", LlamaConfig)
|
||
原仓 text_config.model_type='kormo' 由此解析;转换后目录是 llama,
|
||
这条命中不了,无害。
|
||
② ModelRegistry.register_model("KORMoForCausalLM", LlamaForCausalLM)
|
||
原仓 text_config.architectures=[...KORMoForCausalLM] 由此派发;同上。
|
||
③ LlavaOnevisionForConditionalGeneration.hf_to_vllm_mapper
|
||
+= {".pre_attention_layernorm": ".input_layernorm",
|
||
".pre_mlp_layernorm": ".post_attention_layernorm"}
|
||
原仓权重的两个 key 名在 load_weights 时改名,**不改文件**;
|
||
转换后目录的 key 已经是新名,这两条命中不了,无害。
|
||
④ TokenizersBackend -> PreTrainedTokenizerFast
|
||
原仓 tokenizer_config.json 的 tokenizer_class 是 transformers 5 才有的类;
|
||
tokenizer.json 本身是标准 fast tokenizer 数据。
|
||
⑤ SigLIP require_post_norm=True
|
||
vLLM 的 LLaVA-OneVision 默认丢 SigLIP 的 post_layernorm,
|
||
而 KORMo 训练用的是 post-layernorm 之后的特征。
|
||
⑥ get_placeholder_str -> "<|image|>"(= config.image_token_index 125041)
|
||
vLLM 默认返回 "<image>",它在 KORMo 词表里会被切成 [27, 12177, 29],
|
||
于是带图请求直接 500:
|
||
Failed to apply prompt replacement for mm_items['image'][0]
|
||
这一条**原仓与转换后目录都需要**(转换脚本不改词表)。
|
||
⑦ SigLIP position_ids 跟随 pixel_values 的设备
|
||
position_ids 是 persistent=False 的 buffer,权重加载不会搬它;
|
||
在非 CUDA 后端(如 Biren SUPA)上会 device mismatch 崩。
|
||
⑧ KORMO_MM_ATTN=vllm 时删除 vllm_br 的 MMEncoderAttention.forward_oot
|
||
仅 Biren 需要;实测仍会 500(厂商 SDPA 的 die 切分约束),留作开关。
|
||
|
||
为什么不直接挂模型目录里的那份
|
||
--------------------------------
|
||
模型目录是**只读挂载**(AGENTS.md §8),且用户可能换模型目录、换 revision。
|
||
补丁必须是镜像的一部分,才能保证"同一镜像 = 同一行为"。
|
||
|
||
为什么是 sitecustomize 而不是普通 import
|
||
----------------------------------------
|
||
`init_vision_tower_for_llava` 在 `llava_onevision` 模块 import 时被引用,
|
||
只能在 import 期改;而且 vLLM 的 EngineCore 是**子进程**,
|
||
必须让补丁对子进程也生效 —— `sitecustomize` + `PYTHONPATH` 是唯一可靠路径。
|
||
|
||
幂等性
|
||
------
|
||
- 目标已存在且 md5 相同 → `[skip]`
|
||
- 内容不同 → 覆盖并打印 `[ok]`
|
||
- 写不进去(只读挂载)→ 报错退出,**不静默跳过**
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import hashlib
|
||
import os
|
||
import shutil
|
||
import sys
|
||
|
||
SRC = os.environ.get("KORMO_PATCH_SRC", "/opt/kormo/kormo-engine-sitecustomize.py")
|
||
DST_DIR = os.environ.get("KORMO_PATCH_DIR", "/opt/kormo-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] kormo-engine 已是目标版本 ({DST}, md5={src_md5})")
|
||
return 0
|
||
|
||
os.makedirs(DST_DIR, exist_ok=True)
|
||
try:
|
||
shutil.copyfile(SRC, DST)
|
||
except OSError as exc:
|
||
print(f"ERROR: 无法写入 {DST}: {exc}", file=sys.stderr)
|
||
print(" 如果目标目录是只读挂载,请改用 KORMO_PATCH_DIR 指向可写目录。",
|
||
file=sys.stderr)
|
||
return 1
|
||
|
||
if md5_of(DST) != src_md5:
|
||
print(f"ERROR: 复制后 md5 不一致({DST})", file=sys.stderr)
|
||
return 1
|
||
|
||
print(f"[ok] kormo-engine 已安装到 {DST} (md5={src_md5})")
|
||
print(" 覆盖:config / registry / 权重改名 / tokenizer / post_norm / 图像占位符")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|