@echo off
SET "CMAKE_BIN=C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"
SET "ROOT_DIR=C:\Users\vatutin_n_a\sch-qwen"

echo ==============================================================
echo Building Optimized llama.cpp for Intel i5-12400F
echo Targeting: AVX2, AVX-VNNI, TurboQuant, Flash Attention 3
echo ==============================================================

cd /d "%ROOT_DIR%"

if not exist "llama-cpp-turbo" (
    echo [1/3] Cloning TurboQuant-ready llama.cpp...
    git clone --depth 1 https://github.com/TheTom/llama-cpp-turboquant.git llama-cpp-turbo
) else (
    echo [1/3] llama-cpp-turbo already exists, checking for updates...
    cd llama-cpp-turbo
    git pull
    cd ..
)

cd "%ROOT_DIR%\llama-cpp-turbo"
if not exist "build_turbo" mkdir build_turbo
cd build_turbo

echo [2/3] Configuring with FA3, AVX-VNNI and TurboQuant...
"%CMAKE_BIN%" .. -G "Visual Studio 17 2022" -A x64 ^
    -DCMAKE_BUILD_TYPE=Release ^
    -DGGML_NATIVE=ON ^
    -DGGML_AVX2=ON ^
    -DGGML_AVX_VNNI=ON ^
    -DGGML_FLASH_ATTN=ON ^
    -DGGML_CPU_ALL_QUANTS=ON ^
    -DLLAMA_FATAL_WARNINGS=OFF

if %ERRORLEVEL% NEQ 0 (
    echo Configuration failed!
    pause
    exit /b %ERRORLEVEL%
)

echo [3/3] Building (Parallel - 12 threads)...
"%CMAKE_BIN%" --build . --config Release -j 12

if %ERRORLEVEL% NEQ 0 (
    echo Build failed!
    pause
    exit /b %ERRORLEVEL%
)

echo.
echo ==============================================================
echo Build Complete! 
echo Binaries located in: %ROOT_DIR%\llama-cpp-turbo\build_turbo\bin\Release
echo ==============================================================
pause
