ConsDrive / scripts /run_bench2drive_tmux.sh
ZenoNing's picture
Upload ConsDrive v2 source (part 2)
314ecfb verified
Raw History Blame Contribute Delete
7.85 kB
#!/usr/bin/env bash
set -euo pipefail
export CARLA_ROOT="${CARLA_ROOT:-/public/ZLCODE/carla}"
SESSION_NAME="${SESSION_NAME:-b2d_eval}"
CONDA_ENV="${CONDA_ENV:-drivevla}"
CONDA_SH="${CONDA_SH:-$HOME/miniconda3/etc/profile.d/conda.sh}"
CHECKPOINT="${CHECKPOINT:-outputs/register_tp_small_seed_42/checkpoints/last.ckpt}"
ROUTES="${ROUTES:-leaderboard/data/bench2drive220.xml}"
RESULT_ROOT="${RESULT_ROOT:-eval_results/Bench2Drive/register_tp_small_seed_42_tmux}"
GPUS="${GPUS:-0,1,2,3,4,5,6,7}"
CARLA_GPU_RANKS="${CARLA_GPU_RANKS:-${CARLA_GPUS:-$GPUS}}"
WORKERS="${WORKERS:-8}"
BASE_PORT="${BASE_PORT:-20000}"
BASE_TM_PORT="${BASE_TM_PORT:-30000}"
PORT_STRIDE="${PORT_STRIDE:-50}"
TRAFFIC_MANAGER_SEED="${TRAFFIC_MANAGER_SEED:-1}"
TIMEOUT="${TIMEOUT:-600}"
MAX_ROUTE_RETRIES="${MAX_ROUTE_RETRIES:-3}"
RETRY_WAIT="${RETRY_WAIT:-15}"
CARLA_DEATH_GRACE="${CARLA_DEATH_GRACE:-90}"
REPETITIONS="${REPETITIONS:-1}"
AGENT_FILE="${AGENT_FILE:-team_code/agent_drivevla.py}"
CARLA_ROOT_ARG=()
CONFIG_ARG=()
OVERWRITE_ARG=()
VISUALIZE_ARG=()
GATING_ARG=()
SHADOW_ARG=()
if [[ -n "${CARLA_ROOT:-}" ]]; then
CARLA_ROOT_ARG=(--carla-root "$CARLA_ROOT")
fi
if [[ -n "${CONFIG:-}" ]]; then
CONFIG_ARG=(--config "$CONFIG")
fi
if [[ "${OVERWRITE:-0}" == "1" || "${OVERWRITE:-}" == "true" ]]; then
OVERWRITE_ARG=(--overwrite)
fi
if [[ "${VISUALIZE_TRAJECTORIES:-0}" == "1" || "${VISUALIZE_TRAJECTORIES:-}" == "true" ]]; then
VISUALIZE_ARG=(--visualize-trajectories)
fi
if [[ "${CONSEQUENCE_GATING:-0}" == "1" || "${CONSEQUENCE_GATING:-}" == "true" ]]; then
GATING_ARG=(--consequence-gating)
if [[ -n "${GATING_ALGORITHM:-}" ]]; then
GATING_ARG+=(--gating-algorithm "$GATING_ALGORITHM")
fi
if [[ -n "${GATING_RISK_THRESHOLD:-}" ]]; then
GATING_ARG+=(--gating-risk-threshold "$GATING_RISK_THRESHOLD")
fi
if [[ -n "${GATING_SPEED_SCALES:-}" ]]; then
GATING_ARG+=(--gating-speed-scales "$GATING_SPEED_SCALES")
fi
if [[ -n "${GATING_AGGREGATE:-}" ]]; then
GATING_ARG+=(--gating-aggregate "$GATING_AGGREGATE")
fi
if [[ -n "${GATING_HYSTERESIS_DOWN:-}" ]]; then
GATING_ARG+=(--gating-hysteresis-down-steps "$GATING_HYSTERESIS_DOWN")
fi
if [[ -n "${GATING_HYSTERESIS_UP:-}" ]]; then
GATING_ARG+=(--gating-hysteresis-up-steps "$GATING_HYSTERESIS_UP")
fi
if [[ -n "${GATING_MIN_SPEED:-}" ]]; then
GATING_ARG+=(--gating-min-speed "$GATING_MIN_SPEED")
fi
fi
if [[ "${RISK_SHADOW_LOG:-0}" == "1" || "${RISK_SHADOW_LOG:-}" == "true" ]]; then
SHADOW_ARG=(--risk-shadow-log)
fi
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$REPO_ROOT"
if ! command -v tmux >/dev/null 2>&1; then
echo "tmux is not installed or not on PATH." >&2
exit 1
fi
if [[ ! -f "$CONDA_SH" ]]; then
echo "Cannot find conda activation script: $CONDA_SH" >&2
echo "Set CONDA_SH=/path/to/conda.sh or edit scripts/run_bench2drive_tmux.sh" >&2
exit 1
fi
IFS=',' read -r -a GPU_ARRAY <<< "$GPUS"
if (( WORKERS > ${#GPU_ARRAY[@]} )); then
echo "WORKERS=$WORKERS exceeds number of GPUS in GPUS=$GPUS" >&2
exit 1
fi
IFS=',' read -r -a CARLA_GPU_ARRAY <<< "$CARLA_GPU_RANKS"
if (( WORKERS > ${#CARLA_GPU_ARRAY[@]} )); then
echo "WORKERS=$WORKERS exceeds number of CARLA_GPU_RANKS in CARLA_GPU_RANKS=$CARLA_GPU_RANKS" >&2
exit 1
fi
if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
echo "tmux session '$SESSION_NAME' already exists. Attach with: tmux attach -t $SESSION_NAME" >&2
exit 1
fi
mkdir -p "$RESULT_ROOT/logs"
mapfile -t ROUTE_SUBSETS < <(
python - "$ROUTES" "$WORKERS" <<'PY'
import sys
import xml.etree.ElementTree as ET
routes_file = sys.argv[1]
workers = int(sys.argv[2])
route_ids = [route.attrib["id"] for route in ET.parse(routes_file).iter("route")]
chunks = [[] for _ in range(workers)]
for idx, route_id in enumerate(route_ids):
chunks[idx % workers].append(route_id)
for chunk in chunks:
print(",".join(chunk))
PY
)
tmux new-session -d -s "$SESSION_NAME" -n monitor
tmux set-option -t "$SESSION_NAME" remain-on-exit on
tmux send-keys -t "$SESSION_NAME:monitor" \
"source '$CONDA_SH' && conda activate '$CONDA_ENV' && cd '$REPO_ROOT' && echo 'Bench2Drive tmux session: $SESSION_NAME' && echo 'Conda env: $CONDA_ENV' && echo 'Logs: $RESULT_ROOT/logs' && watch -n 10 \"python scripts/monitor_bench2drive.py '$RESULT_ROOT'\"" C-m
for ((worker=0; worker<WORKERS; worker++)); do
gpu="${GPU_ARRAY[$worker]}"
carla_gpu_rank="${CARLA_GPU_ARRAY[$worker]}"
if [[ "$carla_gpu_rank" == "0" ]]; then
carla_graphicsadapter="none"
else
carla_graphicsadapter=$((carla_gpu_rank + 1))
fi
subset="${ROUTE_SUBSETS[$worker]}"
worker_name="w$(printf '%02d' "$worker")_g${gpu}_cr${carla_gpu_rank}"
worker_label="worker_$(printf '%02d' "$worker")_gpu_${gpu}_carla_rank_${carla_gpu_rank}"
result_file="$RESULT_ROOT/bench2drive220_${worker_label}.json"
agent_config_file="$RESULT_ROOT/agent_config_${worker_label}.json"
retry_state_file="$RESULT_ROOT/retry_state_${worker_label}.json"
debug_checkpoint_file="$RESULT_ROOT/live_results_${worker_label}.txt"
log_path="$RESULT_ROOT/logs/${worker_name}.log"
port=$((BASE_PORT + worker * PORT_STRIDE))
tm_port=$((BASE_TM_PORT + worker * PORT_STRIDE))
cmd=(
python eval_bench2drive.py
--checkpoint "$CHECKPOINT"
--routes "$ROUTES"
--routes-subset "$subset"
--result-dir "$RESULT_ROOT"
--result-file "$result_file"
--agent-config-file "$agent_config_file"
--retry-state-file "$retry_state_file"
--debug-checkpoint-file "$debug_checkpoint_file"
--port "$port"
--traffic-manager-port "$tm_port"
--traffic-manager-seed "$TRAFFIC_MANAGER_SEED"
--timeout "$TIMEOUT"
--max-route-retries "$MAX_ROUTE_RETRIES"
--retry-wait "$RETRY_WAIT"
--carla-death-grace "$CARLA_DEATH_GRACE"
--repetitions "$REPETITIONS"
--gpu-rank "$carla_gpu_rank"
--agent-file "$AGENT_FILE"
"${CARLA_ROOT_ARG[@]}"
"${CONFIG_ARG[@]}"
"${OVERWRITE_ARG[@]}"
"${VISUALIZE_ARG[@]}"
"${GATING_ARG[@]}"
"${SHADOW_ARG[@]}"
)
launcher_dir="$RESULT_ROOT/scripts"
mkdir -p "$launcher_dir"
launcher_script="$launcher_dir/run_${worker_name}.sh"
{
echo '#!/usr/bin/env bash'
echo 'set -eo pipefail'
echo "source '$CONDA_SH'"
echo 'set +u'
echo "conda activate '$CONDA_ENV'"
echo 'set -u'
echo "cd '$REPO_ROOT'"
echo "export CUDA_VISIBLE_DEVICES='$gpu'"
echo 'export OMP_NUM_THREADS="${OMP_NUM_THREADS:-8}"'
printf 'exec '
printf '%q ' "${cmd[@]}"
echo
} > "$launcher_script"
chmod +x "$launcher_script"
tmux new-window -t "$SESSION_NAME" -n "$worker_name"
tmux send-keys -t "$SESSION_NAME:$worker_name" \
"set -o pipefail && mkdir -p '$RESULT_ROOT' '$RESULT_ROOT/logs' && echo 'conda env: $CONDA_ENV' | tee -a '$log_path' && which python | tee -a '$log_path' && python -V | tee -a '$log_path' && echo \"CUDA_VISIBLE_DEVICES=\$CUDA_VISIBLE_DEVICES CARLA_GPU_RANK=$carla_gpu_rank CARLA_GRAPHICSADAPTER=$carla_graphicsadapter OMP_NUM_THREADS=\$OMP_NUM_THREADS MAX_ROUTE_RETRIES=$MAX_ROUTE_RETRIES\" | tee -a '$log_path' && echo 'launcher: $launcher_script' | tee -a '$log_path' && cat '$launcher_script' | tee -a '$log_path' && bash '$launcher_script' 2>&1 | tee -a '$log_path'; worker_status=\${PIPESTATUS[0]}; echo \"WORKER_EXIT=\$worker_status\" | tee -a '$log_path'; exit \$worker_status" C-m
echo "worker=$worker cuda_gpu=$gpu carla_gpu_rank=$carla_gpu_rank graphicsadapter=$carla_graphicsadapter routes=$(awk -F',' '{print NF}' <<< "$subset") port=$port tm_port=$tm_port result=$result_file launcher=$launcher_script log=$log_path"
done
echo
echo "Started tmux session: $SESSION_NAME"
echo "Conda env: $CONDA_ENV"
echo "Attach: tmux attach -t $SESSION_NAME"
echo "List windows: tmux list-windows -t $SESSION_NAME"
echo "Kill all workers: tmux kill-session -t $SESSION_NAME"