#!/bin/bash
# Start vLLM on Ascend NPU with Qwen2.5-32B-Instruct
# Uses 2 NPUs (tensor parallel) for 32B model
# Serves OpenAI-compatible API on port 8000

MODEL_PATH="/home/data/hf_models/Qwen/Qwen2.5-32B-Instruct"

# Check if model exists
if [ ! -d "$MODEL_PATH" ]; then
    # Try modelscope cache path
    MODEL_PATH=$(find /home/data/hf_models -name "config.json" -path "*Qwen2.5-32B*" -exec dirname {} \; 2>/dev/null | head -1)
    if [ -z "$MODEL_PATH" ]; then
        echo "Model not found! Download it first."
        exit 1
    fi
fi

echo "Model path: $MODEL_PATH"
echo "Starting vLLM on NPU..."

docker run -d \
    --name vllm-qwen32b \
    --restart unless-stopped \
    --device /dev/davinci0 \
    --device /dev/davinci1 \
    --device /dev/davinci_manager \
    --device /dev/devmm_svm \
    --device /dev/hisi_hdc \
    -v /usr/local/dcmi:/usr/local/dcmi \
    -v /usr/local/Ascend/driver:/usr/local/Ascend/driver \
    -v /home/data:/home/data \
    -p 8000:8000 \
    mineru:npu-vllm-latest \
    python3 -m vllm.entrypoints.openai.api_server \
        --model "$MODEL_PATH" \
        --served-model-name qwen2.5-32b \
        --host 0.0.0.0 \
        --port 8000 \
        --tensor-parallel-size 2 \
        --max-model-len 8192 \
        --trust-remote-code \
        --dtype auto

echo "vLLM container started. Check logs: docker logs -f vllm-qwen32b"
echo "API endpoint: http://localhost:8000/v1"
