#!/bin/bash
# KVC admission control parameter sweep on Qwen3-30B
# 5 experiments, ~35 min each, ~3 hours total
set -euo pipefail
cd "$(dirname "$0")/.."

MODEL=/mnt/kzlin/workflow/pd-hybrid/simm-swe-bench/models/Qwen3-30B-A3B-Instruct-2507
TRACE=outputs/qwen35-swebench-50sess.jsonl
OUTPUT=outputs/qwen3-30b-exps
VENV_PYTHON=.venv/bin/python

run_kvc() {
  local label=$1
  local inflight=$2
  local min_turn=$3

  echo "=== [$label] inflight=$inflight min_turn=$min_turn === $(date)"
  PYTHONPATH=src:third_party/sglang/python \
  $VENV_PYTHON -m agentic_pd_hybrid.cli benchmark-live \
    --trace $TRACE \
    --output-root $OUTPUT \
    --mechanism kvcache-centric \
    --policy default \
    --model-path $MODEL \
    --prefill-workers 1 --decode-workers 1 \
    --prefill-tp-size 4 --decode-tp-size 4 \
    --prefill-gpu-ids 0,1,2,3 --decode-gpu-ids 4,5,6,7 \
    --transfer-backend mooncake \
    --gpu-budget 8 \
    --time-scale 10 \
    --session-sample-rate 1.0 \
    --target-duration-s 100000 \
    --concurrency-limit 32 \
    --timeout-s 900 \
    --request-timeout-s 300 \
    --kvcache-admission-mode worker \
    --kvcache-seed-min-turn-id $min_turn \
    --kvcache-seed-max-inflight-decode $inflight \
    --kvcache-prefill-backup-policy release-after-transfer \
    --kvcache-prefill-priority-eviction
  echo "=== [$label] DONE === $(date)"
  echo ""
}

# C1: inflight=8, min-turn=2
run_kvc "C1" 8 2

# C2: inflight=16, min-turn=2
run_kvc "C2" 16 2

# C3: inflight=-1 (disabled), min-turn=2
run_kvc "C3" -1 2

# C4: inflight=8, min-turn=1
run_kvc "C4" 8 1

# C5: inflight=-1 (disabled), min-turn=1
run_kvc "C5" -1 1

echo "=== ALL SWEEP EXPERIMENTS DONE === $(date)"
