#!/bin/bash
# Launch the ComfyUI backend (headless) for the Qwen-Image-Edit API.
# gfx906 (MI50) has no flash-attention, so use the pytorch cross-attention path.
set -e
# env.sh resolves BASE/COMFY/VENV (and keeps the venv off NTFS). Portable
# across hosts (tour: /media/tour/APPS/comfyui, hubby: /home/hubby/comfyui).
source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/env.sh"
cd "$COMFY"
source "$VENV/bin/activate"

# MI50 / Vega20 is happiest in fp16; avoid bf16 emulation.
export PYTORCH_HIP_ALLOC_CONF="expandable_segments:True,garbage_collection_threshold:0.8"
export HSA_ENABLE_SDMA=0

# Split cross-attention chunks the attention matmul -> much lower peak VRAM,
# which is what lets the 20B Q8 edit model + reference-image sequence fit in 32GB.
# --lowvram offloads models to CPU RAM when not in use, preventing OOM.
exec python main.py \
    --listen 127.0.0.1 \
    --port 8188 \
    --use-split-cross-attention \
    --lowvram \
    "$@"
