File size: 2,580 Bytes
56a465e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | """Build the full Clover-Preview-VL checkpoint: LoRA merged into Clover, vision as model.visual.*, SGLang config.
python build_serving.py --base /workspace/clover --run /workspace/runs/think/best --out /workspace/serve
Serve with Akicou/sglang@clover-vl (architecture CloverMoeVLModelLM, --speculative-algorithm clover).
"""
import argparse
import json
import os
import shutil
import torch
from huggingface_hub import hf_hub_download
from peft import PeftModel
from safetensors.torch import load_file, save_file
from clover_sft.model import CloverForSFT
QWEN = "Qwen/Qwen3.6-35B-A3B"
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--base", required=True)
ap.add_argument("--run", required=True, help="adapter dir with vision.safetensors")
ap.add_argument("--out", required=True)
args = ap.parse_args()
lm = CloverForSFT.from_pretrained(args.base, dtype=torch.bfloat16, device_map="cpu") # CPU: runs beside the GPU evals
lm = PeftModel.from_pretrained(lm, args.run).merge_and_unload()
lm.save_pretrained(args.out, max_shard_size="5GB")
del lm
torch.cuda.empty_cache()
vision = load_file(f"{args.run}/vision.safetensors")
save_file({f"model.visual.{k}": v.contiguous() for k, v in vision.items()},
f"{args.out}/model-vision.safetensors", metadata={"format": "pt"})
index_path = f"{args.out}/model.safetensors.index.json"
index = json.load(open(index_path))
for k in vision:
index["weight_map"][f"model.visual.{k}"] = "model-vision.safetensors"
json.dump(index, open(index_path, "w"), indent=2)
qcfg = json.load(open(hf_hub_download(QWEN, "config.json")))
cfg = json.load(open(f"{args.out}/config.json"))
cfg["architectures"] = ["CloverMoeVLModelLM"]
cfg["vision_config"] = qcfg["vision_config"]
for k in ("image_token_id", "video_token_id", "vision_start_token_id", "vision_end_token_id"):
cfg[k] = qcfg[k]
rope = dict(cfg.get("rope_parameters") or {})
rope.update(mrope_section=[11, 11, 10], mrope_interleaved=True)
cfg["rope_parameters"] = rope
json.dump(cfg, open(f"{args.out}/config.json", "w"), indent=2)
for f in ("tokenizer.json", "tokenizer_config.json", "generation_config.json"):
shutil.copyfile(f"{args.base}/{f}", f"{args.out}/{f}")
for f in ("preprocessor_config.json", "video_preprocessor_config.json", "chat_template.jinja"):
shutil.copyfile(hf_hub_download(QWEN, f), f"{args.out}/{f}")
print("built", args.out, sorted(os.listdir(args.out)))
if __name__ == "__main__":
main()
|