#!/usr/bin/env bash
# New machine only. Foreground process; Ctrl+C stops only this launcher.
set -euo pipefail
mode=${1:-}
: "${ATLAS_GPU_UUID:?Set the free GPU UUID of the replica}"
: "${ATLAS_MODEL_DIR:?Set the verified local checkpoint directory}"
test -f "$ATLAS_MODEL_DIR/config.json"
case "$mode" in
  vllm-nf4) port=8140; test -x /home/lab/venvs/vllm/bin/vllm;;
  sglang-int8) port=8130; test -x /home/lab/venvs/sglang/bin/python
    : "${ATLAS_TOKENIZER_DIR:?Set the original local tokenizer directory}"
    test -f "$ATLAS_TOKENIZER_DIR/tokenizer.json";;
  *) echo 'Uso: launch_engine.sh vllm-nf4 | sglang-int8' >&2; exit 2;;
esac
used=$(nvidia-smi -i "$ATLAS_GPU_UUID" --query-gpu=memory.used --format=csv,noheader,nounits)
if [[ ! "$used" =~ ^[0-9]+$ ]] || (( used > 1024 )); then
  echo 'GPU ocupada; no se detiene ni reemplaza el proceso existente.' >&2; exit 1
fi
if ss -Hln "sport = :$port" | grep -q .; then
  echo 'Puerto ocupado; no se inicia otro servidor.' >&2; exit 1
fi
export CUDA_VISIBLE_DEVICES="$ATLAS_GPU_UUID" CUDA_DEVICE_ORDER=PCI_BUS_ID
export HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1 TOKENIZERS_PARALLELISM=false
export OMP_NUM_THREADS=8 VLLM_NO_USAGE_STATS=1
if [[ "$mode" == vllm-nf4 ]]; then
  exec /home/lab/venvs/vllm/bin/vllm serve "$ATLAS_MODEL_DIR" \
    --served-model-name ornith-vllm --host 127.0.0.1 --port "$port" \
    --quantization bitsandbytes --load-format bitsandbytes --dtype bfloat16 \
    --max-model-len 8192 --gpu-memory-utilization 0.80 --max-num-seqs 2 \
    --max-num-batched-tokens 2048 --enforce-eager \
    --limit-mm-per-prompt '{"image":0,"video":0}' \
    --enable-auto-tool-choice --tool-call-parser qwen3_xml --reasoning-parser qwen3
else
  unset SGLANG_FORCE_FP8_MARLIN SGLANG_FP8_IGNORED_LAYERS
  exec /home/lab/venvs/sglang/bin/python -m sglang.launch_server \
    --model-path "$ATLAS_MODEL_DIR" --tokenizer-path "$ATLAS_TOKENIZER_DIR" \
    --served-model-name ornith-sglang --host 127.0.0.1 --port "$port" \
    --quantization compressed-tensors --dtype bfloat16 --context-length 8192 \
    --max-total-tokens 65536 --mem-fraction-static 0.85 --max-running-requests 3 \
    --max-mamba-cache-size 18 --chunked-prefill-size 2048 \
    --attention-backend triton --mamba-backend triton \
    --tool-call-parser qwen3_coder --reasoning-parser qwen3 \
    --cuda-graph-max-bs-decode 3 --cuda-graph-backend-prefill disabled
fi
