File size: 9,119 Bytes
2bfd19c | 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | #!/usr/bin/env bash
# Continue dev4 hyperparameter sweep and merge with existing dev3 results.
set -euo pipefail
GPU_ID="${CUDA_VISIBLE_DEVICES:-1}"
SWEEP_FRAMES="${SWEEP_FRAMES:-120}"
BEST_DEV3_TAU="${BEST_DEV3_TAU:-0.012}"
PROMPT="${PROMPT:-a woman dancing.}"
BASELINE="/home/dyvm6xra/dyvm6xrauser11/workspace/cz/FlowCache/FlowCache4MAGI-1/outputs/a_woman_dancing_2026-05-19_09-49-14/output_2026-05-19_09-49-14.mp4"
FLOWCACHE_ROOT="/home/dyvm6xra/dyvm6xrauser11/workspace/cz/FlowCache/FlowCache4MAGI-1-dev3-motion"
DETAIL_ROOT="/home/dyvm6xra/dyvm6xrauser11/workspace/cz/FlowCache/FlowCache4MAGI-1-dev4-detail"
SWEEP_ROOT="${SWEEP_ROOT:-$FLOWCACHE_ROOT/outputs/hparam_sweep_20260614_063749}"
REPORT_DIR="$SWEEP_ROOT/report"
RESULTS_CSV="$REPORT_DIR/results.csv"
DEV3_CSV="$REPORT_DIR/dev3_results.csv"
export MASTER_ADDR=localhost
export CUDA_VISIBLE_DEVICES="$GPU_ID"
export PAD_HQ=1 PAD_DURATION=1
export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
export OFFLOAD_T5_CACHE=true OFFLOAD_VAE_CACHE=true
if [ -z "${CONDA_DEFAULT_ENV:-}" ] || [ "${CONDA_DEFAULT_ENV}" != "magi" ]; then
source "${HOME}/miniforge3/etc/profile.d/conda.sh" 2>/dev/null || source "${HOME}/anaconda3/etc/profile.d/conda.sh"
conda activate magi
fi
python3 - <<'PY'
import numpy as np
if int(np.__version__.split(".")[0]) >= 2:
import subprocess
subprocess.check_call(["pip", "install", "-q", "numpy>=1.24,<2.0"])
PY
make_runtime_config() {
python3 - "$1" "$2" <<'PY'
import json, sys
dst, frames = int(sys.argv[2]) if False else sys.argv[1], int(sys.argv[2])
src = "/home/dyvm6xra/dyvm6xrauser11/workspace/cz/FlowCache/FlowCache4MAGI-1-dev3-motion/config/single_run/flowcache_t2v.json"
with open(src) as f:
cfg = json.load(f)
cfg["runtime_config"]["num_frames"] = frames
with open(dst, "w") as f:
json.dump(cfg, f, indent=4)
PY
}
write_yaml() {
python3 - "$1" "${@:2}" <<'PY'
import sys, yaml
path = sys.argv[1]
params = {}
for kv in sys.argv[2:]:
k, v = kv.split("=", 1)
if v.lower() in ("true", "false"):
params[k] = v.lower() == "true"
elif v.replace(".", "", 1).isdigit():
params[k] = float(v) if "." in v else int(v)
else:
params[k] = v
base = {
"warmup_steps": 5, "phase1_steps": 9, "alpha": 0.5,
"discard_nearly_clean_chunk": True, "compress_kv_cache": True,
"total_cache_chunk_nums": 5, "compress_strategy": "token",
"mix_lambda": 0.07, "query_granularity": "frame",
"score_weighting_method": "no_weight", "power": 3,
"log": False, "print_peak_memory": True,
}
base.update(params)
with open(path, "w") as f:
yaml.dump(base, f, default_flow_style=False)
PY
}
run_one() {
local version="$1" run_id="$2" yaml_path="$3" root_dir="$4"
local exp_dir="$SWEEP_ROOT/${version}_${run_id}"
mkdir -p "$exp_dir"
local out="$exp_dir/output.mp4" log="$exp_dir/infer.log" metric="$exp_dir/metrics.json"
export MASTER_PORT=$((6100 + RANDOM % 400))
if [ "$root_dir" = "$DETAIL_ROOT" ]; then
export PYTHONPATH="${DETAIL_ROOT}:${FLOWCACHE_ROOT}"
else
export PYTHONPATH="${FLOWCACHE_ROOT}:${DETAIL_ROOT}"
fi
echo "========== [$version] $run_id (PYTHONPATH=$PYTHONPATH) =========="
local t0 t1 elapsed
t0=$(date +%s)
set +e
( cd "$root_dir" && python3 inference/pipeline/motioncache.py \
--config_file "$RUNTIME_CFG" --mode t2v --prompt "$PROMPT" \
--output_path "$out" --additional_config "$yaml_path" \
--motioncache_metric_stats_path "$metric" 2>&1 | tee "$log" )
local rc=${PIPESTATUS[0]}
set -e
t1=$(date +%s); elapsed=$((t1 - t0))
[ -f "$out" ] && [ "$rc" -eq 0 ] || { echo "FAILED $run_id rc=$rc"; return 1; }
eval_out=$(python3 "$FLOWCACHE_ROOT/tools/eval_run.py" --baseline "$BASELINE" --generated "$out" --log "$log" --metric "$metric" 2>/dev/null || true)
PSNR=NA; SSIM=NA; BLACK=NA; REUSE=NA; PEAK=NA
while IFS='=' read -r k v; do
case "$k" in PSNR) PSNR="$v" ;; SSIM) SSIM="$v" ;; BLACK) BLACK="$v" ;; REUSE) REUSE="$v" ;; PEAK) PEAK="$v" ;; esac
done <<< "$eval_out"
echo "$run_id,$version,$TAU,$ALPHA,$DETAIL_ALPHA,$DETAIL_WINDOW,$COMBINE,$DETAIL_LAM,$PSNR,$SSIM,$BLACK,$REUSE,$elapsed,$PEAK,$out,$log" >> "$RESULTS_CSV"
echo " PSNR=${PSNR}dB reuse=${REUSE}% time=${elapsed}s"
}
# preserve dev3 rows
python3 - "$RESULTS_CSV" "$DEV3_CSV" <<'PY'
import csv, sys, shutil
src, dst = sys.argv[1:3]
rows = list(csv.DictReader(open(src)))
dev3 = [r for r in rows if r["version"].startswith("dev3")]
if dev3:
with open(dst, "w", newline="") as f:
w = csv.DictWriter(f, fieldnames=dev3[0].keys())
w.writeheader(); w.writerows(dev3)
PY
cp "$DEV3_CSV" "$RESULTS_CSV"
RUNTIME_CFG="$SWEEP_ROOT/runtime_${SWEEP_FRAMES}f.json"
make_runtime_config "$RUNTIME_CFG" "$SWEEP_FRAMES"
echo "dev4 sweep tau=$BEST_DEV3_TAU frames=$SWEEP_FRAMES -> $SWEEP_ROOT"
for spec in \
"max|3|0.5|0.5" "max|5|0.5|0.5" "max|3|0.4|0.5" "max|3|0.6|0.5" \
"blend|3|0.5|0.3" "blend|3|0.5|0.5" "blend|3|0.5|0.7" \
"product|3|0.5|0.5" "product|5|0.5|0.5"; do
IFS='|' read -r mode win da lam <<< "$spec"
rid="tau${BEST_DEV3_TAU}_${mode}_w${win}_da${da}_lam${lam}"
y="$SWEEP_ROOT/dev4_${rid}.yaml"
write_yaml "$y" "rel_l1_thresh=$BEST_DEV3_TAU" "detail_alpha=$da" \
"detail_window_size=$win" "weight_combine_mode=$mode" "detail_lambda=$lam"
export TAU="$BEST_DEV3_TAU" ALPHA="0.5" DETAIL_ALPHA="$da" DETAIL_WINDOW="$win" COMBINE="$mode" DETAIL_LAM="$lam"
run_one "dev4" "$rid" "$y" "$DETAIL_ROOT" || true
done
RUNTIME_CFG="$SWEEP_ROOT/runtime_240f.json"
make_runtime_config "$RUNTIME_CFG" 240
read -r y4 da dw cm dl BEST_DEV4_ID <<< "$(python3 - "$RESULTS_CSV" "$SWEEP_ROOT" "$BEST_DEV3_TAU" <<'PY'
import csv, sys, yaml, os
csv_path, sweep_root, tau = sys.argv[1:4]
rows = [r for r in csv.DictReader(open(csv_path)) if r["version"] == "dev4" and r["psnr_db"] not in ("NA", "")]
def score(r):
psnr = float(r["psnr_db"]) if r["psnr_db"] != "inf" else 100.0
return psnr + 0.02 * float(r["reuse_rate_pct"] or 0)
row = max(rows, key=score)
y = {
"rel_l1_thresh": float(tau), "warmup_steps": 5, "phase1_steps": 9, "alpha": 0.5,
"detail_alpha": float(row["detail_alpha"]),
"detail_window_size": int(float(row["detail_window"])),
"weight_combine_mode": row["combine_mode"],
"detail_lambda": float(row["detail_lambda"]),
"discard_nearly_clean_chunk": True, "compress_kv_cache": True,
"total_cache_chunk_nums": 5, "compress_strategy": "token", "mix_lambda": 0.07,
"query_granularity": "frame", "score_weighting_method": "no_weight",
"power": 3, "log": False, "print_peak_memory": True,
}
path = os.path.join(sweep_root, f"dev4_{row['variant']}_full.yaml")
with open(path, "w") as f: yaml.dump(y, f, default_flow_style=False)
print(path, row["detail_alpha"], row["detail_window"], row["combine_mode"], row["detail_lambda"], row["variant"])
PY
)"
export TAU="$BEST_DEV3_TAU" ALPHA="0.5" DETAIL_ALPHA="$da" DETAIL_WINDOW="$dw" COMBINE="$cm" DETAIL_LAM="$dl"
run_one "dev4_full" "${BEST_DEV4_ID}_240f" "$y4" "$DETAIL_ROOT" || true
python3 "$FLOWCACHE_ROOT/tools/generate_comparison_report.py" \
--results "$RESULTS_CSV" --baseline "$BASELINE" \
--output "$REPORT_DIR/comparison_report.md" --sweep_dir "$SWEEP_ROOT"
# write optimal configs
python3 - "$RESULTS_CSV" "$FLOWCACHE_ROOT" "$DETAIL_ROOT" <<'PY'
import csv, sys, yaml, os
csv_path, dev3_root, dev4_root = sys.argv[1:4]
rows = list(csv.DictReader(open(csv_path)))
def score(r):
psnr = float(r["psnr_db"]) if r["psnr_db"] not in ("NA", "inf", "") else -999
if r["psnr_db"] == "inf": psnr = 100
return psnr + 0.02 * float(r["reuse_rate_pct"] or 0)
dev3 = [r for r in rows if r["version"] == "dev3"]
dev4 = [r for r in rows if r["version"] == "dev4"]
full3 = [r for r in rows if r["version"] == "dev3_full"]
full4 = [r for r in rows if r["version"] == "dev4_full"]
if dev3:
b3 = max(dev3, key=score)
y3 = {"rel_l1_thresh": float(b3["tau"]), "alpha": 0.5, "warmup_steps": 5, "phase1_steps": 9,
"discard_nearly_clean_chunk": True, "compress_kv_cache": True, "total_cache_chunk_nums": 5,
"log": True, "print_peak_memory": True}
with open(os.path.join(dev3_root, "yaml_config/single_run/motioncache_config_best.yaml"), "w") as f:
yaml.dump(y3, f, default_flow_style=False)
if dev4:
b4 = max(dev4, key=score)
y4 = {"rel_l1_thresh": float(b4["tau"]), "alpha": 0.5, "warmup_steps": 5, "phase1_steps": 9,
"detail_alpha": float(b4["detail_alpha"]), "detail_window_size": int(float(b4["detail_window"])),
"weight_combine_mode": b4["combine_mode"], "detail_lambda": float(b4["detail_lambda"]),
"discard_nearly_clean_chunk": True, "compress_kv_cache": True, "total_cache_chunk_nums": 5,
"log": True, "print_peak_memory": True}
with open(os.path.join(dev4_root, "yaml_config/single_run/motiondetail_config_best.yaml"), "w") as f:
yaml.dump(y4, f, default_flow_style=False)
print("Wrote best config yaml files")
PY
echo "Done. Report: $REPORT_DIR/comparison_report.md"
|