Files
stemengine_old/backend/scripts/probe_bs_adapter_feasibility.py
2026-03-26 15:14:41 +09:00

137 lines
5.1 KiB
Python

#!/usr/bin/env python3
"""Probe compatibility of candidate BS-family checkpoints with current BSRoformer runtime.
This script tests whether each candidate can be:
1) constructed with backend.services.bs_roformer_separator._build_bsroformer_model
2) loaded with torch.load + model.load_state_dict(strict=False)
It is intended as a quick feasibility gate before integrating new BS architecture
IDs into production routing.
"""
from __future__ import annotations
import argparse
import json
import traceback
from pathlib import Path
from urllib.request import urlretrieve
import torch
import yaml
from omegaconf import OmegaConf
from backend.services.bs_roformer_separator import _build_bsroformer_model
CANDIDATES = {
"bs_polarformer": {
"upstream_tag": "v1.0.20",
"weights_url": "https://github.com/ZFTurbo/Music-Source-Separation-Training/releases/download/v1.0.20/model_bs_polarformer_float16.ckpt",
"config_url": "https://github.com/ZFTurbo/Music-Source-Separation-Training/releases/download/v1.0.20/model_bs_polarformer_float16.yaml",
},
"bs_mamba2": {
"upstream_tag": "v1.0.19",
"weights_url": "https://github.com/ZFTurbo/Music-Source-Separation-Training/releases/download/v1.0.19/bs_mamba2_vocals.ckpt",
"config_url": "https://github.com/ZFTurbo/Music-Source-Separation-Training/releases/download/v1.0.19/config_bs_mamba2_vocals.yaml",
},
"bs_conformer": {
"upstream_tag": "v1.0.18",
"weights_url": "https://github.com/ZFTurbo/Music-Source-Separation-Training/releases/download/v1.0.18/fused_model_bs_conformer_sdr_9.18.ckpt",
"config_url": "https://github.com/ZFTurbo/Music-Source-Separation-Training/releases/download/v1.0.18/config_musdb18_bs_conformer_infer.yaml",
},
}
def _download_if_needed(url: str, target_dir: Path) -> Path:
target = target_dir / Path(url).name
if not target.exists():
print(f"[probe] downloading {target.name} ...")
urlretrieve(url, target)
return target
def main() -> int:
parser = argparse.ArgumentParser(description="Probe BS adapter feasibility")
parser.add_argument(
"--work-dir",
default="/app/media/jobs/qa_adapter_probe",
help="Working directory for downloaded checkpoints/configs",
)
parser.add_argument(
"--json-out",
default="",
help="Optional explicit JSON output path (default: <work-dir>/probe_results.json)",
)
args = parser.parse_args()
work_dir = Path(args.work_dir)
work_dir.mkdir(parents=True, exist_ok=True)
json_out = Path(args.json_out) if args.json_out else (work_dir / "probe_results.json")
rows: list[dict] = []
for candidate, meta in CANDIDATES.items():
row: dict = {
"candidate": candidate,
"upstream_tag": meta["upstream_tag"],
"constructor": "not_run",
"state_load": "not_run",
}
cand_dir = work_dir / candidate
cand_dir.mkdir(parents=True, exist_ok=True)
try:
ckpt_path = _download_if_needed(meta["weights_url"], cand_dir)
cfg_path = _download_if_needed(meta["config_url"], cand_dir)
row["checkpoint"] = str(ckpt_path)
row["config"] = str(cfg_path)
cfg_raw = yaml.load(cfg_path.read_text(encoding="utf-8"), Loader=yaml.UnsafeLoader)
cfg = OmegaConf.create(cfg_raw)
model_cfg = OmegaConf.to_container(cfg.model, resolve=True)
row["config_keys"] = sorted(model_cfg.keys())
try:
model = _build_bsroformer_model(model_cfg, config_path=cfg_path)
row["constructor"] = "ok"
except Exception as exc:
row["constructor"] = "fail"
row["constructor_error"] = str(exc)
rows.append(row)
print(f"[probe] {candidate}: constructor FAIL")
continue
state = torch.load(str(ckpt_path), map_location="cpu", weights_only=False)
if isinstance(state, dict) and "state_dict" in state:
state = state["state_dict"]
try:
load_result = model.load_state_dict(state, strict=False)
row["state_load"] = "ok"
row["missing_keys"] = len(getattr(load_result, "missing_keys", []))
row["unexpected_keys"] = len(getattr(load_result, "unexpected_keys", []))
except Exception as exc:
row["state_load"] = "fail"
row["state_load_error"] = str(exc)
rows.append(row)
print(f"[probe] {candidate}: constructor={row['constructor']}, state_load={row['state_load']}")
except Exception as exc:
row["probe_error"] = str(exc)
row["traceback"] = traceback.format_exc(limit=2)
rows.append(row)
print(f"[probe] {candidate}: probe ERROR")
json_out.parent.mkdir(parents=True, exist_ok=True)
json_out.write_text(json.dumps(rows, indent=2), encoding="utf-8")
print(f"[probe] wrote report: {json_out}")
return 0
if __name__ == "__main__":
raise SystemExit(main())