@echo off
setlocal enabledelayedexpansion

echo [*] Cleaning up previous processes...
taskkill /F /IM llama-server.exe /T >nul 2>&1
taskkill /F /IM llama-cli.exe /T >nul 2>&1

echo ==============================================================
echo [THE REAL KING] Qwen 35B MoE Server - V1 WINNER
echo Optimized for: Intel i5-12400F + 32GB RAM
echo Targets: AVX-VNNI, FA3, TurboQuant, 6 P-Cores
echo ==============================================================

:: --- CONFIGURATION ---
SET "ROOT_DIR=C:\Users\vatutin_n_a\sch-qwen\"
SET "BIN_DIR=%ROOT_DIR%llama-cpp-turbo\build_turbo\bin\Release"
SET "MODELS_DIR=%ROOT_DIR%models"

:: --- AUTO-DETECT ANY GGUF MODEL ---
set "MODEL_PATH="
for %%f in ("%MODELS_DIR%\*.gguf") do (
    set "filename=%%~nxf"
    if "!filename:~0,6!" neq "mmproj" (
        set "MODEL_PATH=%%f"
        goto :found
    )
)
:found
if not defined MODEL_PATH (
    echo [!] ERROR: No model found!
    pause
    exit /b 1
)

:: --- AUTO-DETECT VISION PROJECTOR ---
set "MMPROJ_PATH="
for %%f in ("%MODELS_DIR%\mmproj*.gguf") do (
    set "MMPROJ_PATH=%%f"
    goto :mmproj_found
)
:mmproj_found

echo Found model: %MODEL_PATH%
if defined MMPROJ_PATH (echo Found vision projector: %MMPROJ_PATH%)

:: --- LAUNCH ---
echo [*] Launching at maximum V1 efficiency...
echo [Settings]: 6 Threads, Priority: High, FA3: On, KV: Turbo3/4, ub: 1024

cd /d "%ROOT_DIR%"

set "ARGS=-m "%MODEL_PATH%""
if defined MMPROJ_PATH (set ARGS=!ARGS! --mmproj "%MMPROJ_PATH%")

"%BIN_DIR%\llama-server.exe" ^
    !ARGS! ^
    --host 0.0.0.0 --port 8080 ^
    -c 32000 ^
    -t 6 ^
    -b 2048 ^
    -ub 1024 ^
    --flash-attn on ^
    --cache-type-k turbo3 ^
    --cache-type-v turbo4 ^
    --no-mmap ^
    --prio 2 ^
    -np 1 --slots

pause
