#!/bin/bash
set -euo pipefail
ROOT=/home/lab/bench/ornith/modular/sglang
export CUDA_VISIBLE_DEVICES=GPU-f4a988cb-c310-b618-5a9c-4462d4d33add
export CUDA_DEVICE_ORDER=PCI_BUS_ID
export OMP_NUM_THREADS=8
export TOKENIZERS_PARALLELISM=false
export HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1
PROFILE=${1:-awq-bf16-eager}
MODEL=${ORNITH_MODEL:-/home/lab/models/ornith-modular/awq-int4-text}
if [[ "$PROFILE" == text-* ]]; then MODEL=/home/lab/models/ornith-modular/original-text; fi
if [[ "$PROFILE" == mlp-int8-* ]]; then MODEL=/home/lab/models/ornith-modular/mlp-int8-text; fi
if [[ "$PROFILE" == full-int8-* ]]; then MODEL=/home/lab/models/ornith-modular/full-int8-text; fi
if [[ "$PROFILE" == vision-* ]]; then MODEL=/home/lab/models/Ornith-1.5-9B-original; fi
CONTEXT=${ORNITH_CONTEXT:-8192}
CONCURRENCY=${ORNITH_CONCURRENCY:-3}
MEMORY=${ORNITH_MEMORY:-0.90}
ARGS=(--model-path "$MODEL" --served-model-name ornith-sglang --host 127.0.0.1 --port "${ORNITH_PORT:-8130}" --dtype "${ORNITH_DTYPE:-bfloat16}" --context-length "$CONTEXT" --mem-fraction-static "$MEMORY" --max-running-requests "$CONCURRENCY" --chunked-prefill-size 2048 --max-mamba-cache-size "${ORNITH_MAMBA_STATES:-18}" --attention-backend triton --mamba-backend triton --tool-call-parser qwen3_coder --reasoning-parser qwen3)
case "$PROFILE" in
  baseline-fp8) export SGLANG_FORCE_FP8_MARLIN=1; ARGS=(--model-path /home/lab/models/Ornith-1.5-9B-original --served-model-name ornith-sglang --host 127.0.0.1 --port "${ORNITH_PORT:-8130}" --quantization fp8 --dtype bfloat16 --context-length 8192 --mem-fraction-static 0.80 --max-running-requests 2 --chunked-prefill-size 1024 --attention-backend triton --mamba-backend triton --tool-call-parser qwen3_coder --reasoning-parser qwen3);;
  awq-bf16-eager) ;;
  text-fp8-eager|text-fp8-graphs) export SGLANG_FORCE_FP8_MARLIN=1; ARGS+=(--quantization fp8);;
  text-fp8-mlp-graphs) export SGLANG_FORCE_FP8_MARLIN=1 SGLANG_FP8_IGNORED_LAYERS=self_attn,linear_attn; ARGS+=(--quantization fp8);;
  text-fp8-mlp-kvfp4-graphs) export SGLANG_FORCE_FP8_MARLIN=1 SGLANG_FP8_IGNORED_LAYERS=self_attn,linear_attn; ARGS+=(--quantization fp8 --kv-cache-dtype fp4_mx_block16);;
  text-fp8-kvfp8-graphs) export SGLANG_FORCE_FP8_MARLIN=1; ARGS+=(--quantization fp8 --kv-cache-dtype fp8_e4m3);;
  text-fp8-kvfp4-graphs) export SGLANG_FORCE_FP8_MARLIN=1; ARGS+=(--quantization fp8 --kv-cache-dtype fp4_mx_block16);;
  text-bf16-graphs) ;;
  text-bf16-kvfp8-graphs) ARGS+=(--kv-cache-dtype fp8_e4m3);;
  vision-bf16-graphs) ;;
  mlp-int8-bf16-graphs) ARGS+=(--quantization compressed-tensors);;
  full-int8-bf16-graphs) ARGS+=(--quantization compressed-tensors);;
  full-int8-kvfp8-graphs) ARGS+=(--quantization compressed-tensors --kv-cache-dtype fp8_e4m3);;
  full-int8-kve5m2-graphs) ARGS+=(--quantization compressed-tensors --kv-cache-dtype fp8_e5m2);;
  mlp-int8-kvfp8-graphs) ARGS+=(--quantization compressed-tensors --kv-cache-dtype fp8_e4m3);;
  mlp-int8-kvfp4-graphs|mlp-int8-kvfp4-eager) ARGS+=(--quantization compressed-tensors --kv-cache-dtype fp4_mx_block16);;
  text-bf16-kvfp4-graphs|text-bf16-kvfp4-eager) ARGS+=(--kv-cache-dtype fp4_mx_block16);;
  awq-fp8-eager|awq-fp8-graphs) ARGS+=(--kv-cache-dtype fp8_e4m3);;
  awq-fp4-eager|awq-fp4-graphs) ARGS+=(--kv-cache-dtype fp4_mx_block16);;
  awq-bf16-graphs) ;;
  dflash-4|dflash-8|dflash-16) ARGS+=(--speculative-algorithm DFLASH --speculative-draft-model-path "${ORNITH_DFLASH_MODEL:-/home/lab/models/ornith-modular/dflash-bf16}" --speculative-draft-model-quantization "${ORNITH_DRAFT_QUANT:-unquant}" --speculative-dflash-block-size "${PROFILE##*-}" --speculative-draft-attention-backend triton --speculative-draft-window-size "${ORNITH_DRAFT_WINDOW:-4096}");;
  mtp-1|mtp-2|mtp-4) STEPS=${PROFILE##*-}; ARGS+=(--speculative-algorithm NEXTN --speculative-draft-model-path "${ORNITH_MTP_MODEL:-/home/lab/models/ornith-modular/original-text}" --speculative-draft-model-quantization unquant --speculative-num-steps "$STEPS" --speculative-eagle-topk 1 --speculative-num-draft-tokens "$((STEPS+1))");;
  standalone) ARGS+=(--speculative-algorithm STANDALONE --speculative-draft-model-path "${ORNITH_DRAFT_MODEL:-/home/lab/models/ornith-modular/draft-08b}" --speculative-draft-model-quantization unquant --speculative-num-steps 3 --speculative-eagle-topk 1 --speculative-num-draft-tokens 4);;
  *) echo "Unknown profile: $PROFILE" >&2; exit 2;;
esac
if [[ -n "${ORNITH_QUANT:-}" && "$PROFILE" != baseline-fp8 && "$PROFILE" != text-fp8-* ]]; then
  ARGS+=(--quantization "$ORNITH_QUANT")
  if [[ "$ORNITH_QUANT" == fp8 ]]; then export SGLANG_FORCE_FP8_MARLIN=1; fi
fi
if [[ "$PROFILE" != baseline-fp8 ]]; then
  ARGS+=(--tokenizer-path /home/lab/models/Ornith-1.5-9B-original)
fi
if [[ -n "${ORNITH_MAX_TOKENS:-}" ]]; then ARGS+=(--max-total-tokens "$ORNITH_MAX_TOKENS"); fi
if [[ "$PROFILE" == *graphs || "${ORNITH_GRAPHS:-0}" == 1 ]]; then
  ARGS+=(--cuda-graph-max-bs-decode 3 --cuda-graph-backend-prefill disabled)
else
  ARGS+=(--cuda-graph-backend-decode disabled --cuda-graph-backend-prefill disabled)
fi
printf 'Starting isolated SGLang profile=%s GPU1 model=%s\n' "$PROFILE" "$MODEL"
printf '%q ' /home/lab/venvs/sglang/bin/python -m sglang.launch_server "${ARGS[@]}"; printf '\n'
exec /home/lab/venvs/sglang/bin/python -m sglang.launch_server "${ARGS[@]}"
