jegue / code /convert_lora_for_vllm.py
fdemelo's picture
Jegue 0.2: self-contained (backbone files, adapter, heads, HF and vLLM classes), Decision Index 0.3 public suite 59.59
63ae4a0 verified
Raw History Blame Contribute Delete
1 kB
#!/usr/bin/env python3
"""Rename the keys of a text-only PEFT adapter (base_model.model.model.layers.*) to the Qwen3.5 conditional-generation layout vLLM expects
(base_model.model.model.language_model.layers.*). vLLM otherwise DROPS the adapter silently (checked 2026-10-24: outputs equal the base model).
python tools/convert_lora_for_vllm.py SRC_LORA_DIR DST_DIR"""
import json, os, shutil, sys
from safetensors.torch import load_file, save_file
src, dst = sys.argv[1:3]
os.makedirs(dst, exist_ok=True)
sd = load_file(os.path.join(src, "adapter_model.safetensors"))
new = {k.replace("base_model.model.model.layers.", "base_model.model.model.language_model.layers.", 1): v for k, v in sd.items()}
assert len(new) == len(sd) and any("language_model" in k for k in new)
save_file(new, os.path.join(dst, "adapter_model.safetensors"), metadata={"format": "pt"})
shutil.copy(os.path.join(src, "adapter_config.json"), os.path.join(dst, "adapter_config.json"))
print(len(new), "tensors written to", dst)