#!/bin/bash
# Launch ComfyUI with all BC-250 patches (v12)
cd /home/fabian/ComfyUI
source venv/bin/activate

ulimit -l unlimited 2>/dev/null

# BC-250 environment
export HSA_OVERRIDE_GFX_VERSION=10.1.0
export HSA_ENABLE_SDMA=0
export HIP_VISIBLE_DEVICES=0
export OMP_NUM_THREADS=$(nproc)
export MKL_NUM_THREADS=$(nproc)
export MIOPEN_FIND_MODE=3
export BC250_SOFTMAX_THRESHOLD=4096
export PYTORCH_HIP_ALLOC_CONF=garbage_collection_threshold:0.8
export TORCH_BLAS_PREFER_HIPBLASLT=0

# Kill any existing
pkill -9 -f "python.*main.py" 2>/dev/null
sleep 2

echo "[$(date)] Starting ComfyUI v12: highvram + pytorch-attn + bf16-kill"
nohup python main.py \
    --listen 0.0.0.0 \
    --port 8188 \
    --highvram \
    --use-pytorch-cross-attention \
    > /home/fabian/comfyui8.log 2>&1 &
disown

echo "PID: $!"
sleep 5

# Verify startup
if pgrep -f "python.*main.py" > /dev/null; then
    echo "ComfyUI started. Monitoring log..."
    tail -20 /home/fabian/comfyui8.log
else
    echo "FAILED TO START"
    cat /home/fabian/comfyui8.log
fi
