#!/bin/bash
# Build v4 - incremental rebuild after C10_WARP_SIZE fix
set -e

cd /home/fabian/pytorch-build

export HSA_OVERRIDE_GFX_VERSION=10.1.0
export HSA_ENABLE_SDMA=0
export HIP_VISIBLE_DEVICES=0
export ROCM_PATH=/opt/rocm
export HIP_PATH=/opt/rocm
export PYTORCH_ROCM_ARCH=gfx1010
export USE_ROCM=1
export USE_CUDA=0
export USE_FLASH_ATTENTION=0
export USE_MEM_EFF_ATTENTION=0
export USE_AOTRITON=0
export USE_TRITON=0
export MAX_JOBS=8
export CMAKE_POLICY_VERSION_MINIMUM=3.5
export CMAKE_PREFIX_PATH=/opt/rocm
export PYTHON_EXECUTABLE=/usr/bin/python3.11

LOG=/home/fabian/pytorch-build/build_v4.log
echo "============================================" | tee "$LOG"
echo " BUILD v4 STARTED at $(date)" | tee -a "$LOG"
echo " Incremental rebuild after C10_WARP_SIZE fix" | tee -a "$LOG"
echo "============================================" | tee -a "$LOG"

# Incremental build - ninja will only recompile changed files
cd build
cmake --build . --target install 2>&1 | tee -a "$LOG"

echo "============================================" | tee -a "$LOG"
echo " BUILD v4 FINISHED at $(date)" | tee -a "$LOG"
echo "============================================" | tee -a "$LOG"

# Back to source root for wheel
cd /home/fabian/pytorch-build
/usr/bin/python3.11 setup.py bdist_wheel 2>&1 | tee -a "$LOG"

WHL=$(ls dist/*.whl 2>/dev/null | head -1)
if [ -n "$WHL" ]; then
    echo "SUCCESS: Wheel at $WHL" | tee -a "$LOG"
    ls -lh "$WHL" | tee -a "$LOG"
else
    echo "ERROR: No wheel produced!" | tee -a "$LOG"
fi
