team-qbits / training /merge_adapter.py
ovinduG's picture
Upload folder using huggingface_hub
e55c8a4 verified
Raw History Blame Contribute Delete
4.65 kB
#!/usr/bin/env python
"""
Merge a CPT LoRA adapter into its base model and save a plain 16-bit model folder.
Needed when the CPT run was uploaded as an ADAPTER (adapter_config.json + adapter_model.safetensors)
rather than as merged weights. After merging, the folder loads like any ordinary model, so
sft_gemma4_mcq.py can train on it with no changes:
python merge_adapter.py --adapter ovinduG/gemma4-e4b-sinhala-cpt-d3-c3ucsc \
--out /workspace/models/cpt_d3c3_merged
python sft_gemma4_mcq.py --train sft_train.jsonl --val sft_val.jsonl \
--model /workspace/models/cpt_d3c3_merged --out /workspace/outputs/mcq_sft_cpt_d3c3
Why merge instead of training the adapter directly: loading the adapter gives a PeftModel, and the
SFT script then adds a SECOND LoRA on top. Saving/merging that stack is error-prone, and the
parameter count for the 8B rule stops being obvious. Merging folds the CPT LoRA into the existing
weight matrices, so the result is still ~7.996B parameters.
Needs the GPU free (stop any training job first) and ~16 GB of free disk for the output.
"""
import argparse
import json
import os
import struct
import sys
from pathlib import Path
os.environ.setdefault("HF_HUB_ENABLE_HF_TRANSFER", "1")
LIMIT = 8_000_000_000 # shared-task parameter cap
def adapter_info(src):
"""Print what we are about to merge: base model and LoRA rank, read from adapter_config.json."""
cfg = None
p = Path(src) / "adapter_config.json"
if p.exists():
cfg = json.loads(p.read_text(encoding="utf-8"))
else:
try:
from huggingface_hub import hf_hub_download
cfg = json.loads(Path(hf_hub_download(src, "adapter_config.json")).read_text(encoding="utf-8"))
except Exception as e: # noqa: BLE001
print(f"[warn] could not read adapter_config.json ({type(e).__name__}: {e}); "
"if this is already a merged model you do not need this script")
return None
print(f"[adapter] base model : {cfg.get('base_model_name_or_path')}")
print(f"[adapter] LoRA r={cfg.get('r')} alpha={cfg.get('lora_alpha')} "
f"rslora={cfg.get('use_rslora')} targets={cfg.get('target_modules')}")
return cfg
def count_params(folder):
"""Sum parameter counts from safetensors headers (no need to load the weights)."""
total = 0
for f in sorted(Path(folder).glob("*.safetensors")):
with open(f, "rb") as fh:
n = struct.unpack("<Q", fh.read(8))[0]
head = json.loads(fh.read(n))
for name, meta in head.items():
if name == "__metadata__":
continue
shape = meta.get("shape") or []
c = 1
for d in shape:
c *= d
total += c
return total
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--adapter", required=True, help="HF repo id or local dir of the CPT LoRA adapter")
ap.add_argument("--out", required=True, help="local folder to write the merged 16-bit model to")
ap.add_argument("--max_seq_length", type=int, default=2048)
args = ap.parse_args()
if Path(args.out).exists() and list(Path(args.out).glob("*.safetensors")):
print(f"[skip] {args.out} already holds safetensors; delete it to rebuild")
n = count_params(args.out)
print(f"[check] parameters: {n:,} -> {'OK, under 8B' if n <= LIMIT else 'OVER THE 8B LIMIT'}")
return
adapter_info(args.adapter)
from unsloth import FastModel # imported late, as in the other scripts
print(f"[load] {args.adapter} (base weights download on first use, ~16 GB)")
model, tok = FastModel.from_pretrained(model_name=args.adapter, max_seq_length=args.max_seq_length,
dtype=None, load_in_4bit=False)
print(f"[merge] writing merged 16-bit model to {args.out}")
model.save_pretrained_merged(args.out, tok, save_method="merged_16bit")
files = sorted(p.name for p in Path(args.out).iterdir())
print(f"[done] {len(files)} files: {', '.join(files)}")
n = count_params(args.out)
print(f"[check] parameters: {n:,} -> {'OK, under 8B' if n <= LIMIT else 'OVER THE 8B LIMIT'}")
for need in ["config.json", "tokenizer.json", "tokenizer_config.json"]:
if need not in files:
print(f"[warn] {need} is missing from {args.out}; loading it may fail")
if not any(f.endswith(".safetensors") for f in files):
sys.exit("no safetensors were written: the merge failed")
if __name__ == "__main__":
main()