name: Build HipBLAS ROCm 7.1

on:
  workflow_dispatch: # allows manual triggering
    inputs:
      create_release:
        description: "Create rocm release"
        required: false
        type: boolean
        default: false
      gpu_targets:
        description: "GPU Targets (comma separated)"
        required: false
        type: string
        default: "gfx1201,gfx1200,gfx1100,gfx1101,gfx1151"
  push:
    branches:
      - master
      - ci
    paths:
      [
        ".github/workflows/build-hipblas-rocm71.yml",
        "**/CMakeLists.txt",
        "**/*.h",
        "**/*.hpp",
        "**/*.c",
        "**/*.cpp",
        "**/*.cu",
      ]
  pull_request:
    types: [opened, synchronize, reopened]
    paths:
      [
        ".github/workflows/build-hipblas-rocm71.yml",
        "**/CMakeLists.txt",
        "**/*.h",
        "**/*.hpp",
        "**/*.c",
        "**/*.cpp",
        "**/*.cu",
      ]

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref && github.ref || github.run_id }}
  cancel-in-progress: true

jobs:
  get-default-branch:
    runs-on: self-hosted
    outputs:
      branch: ${{ steps.branch.outputs.branch }}
    steps:
      - name: Get default branch
        id: branch
        run: echo "branch=${{ github.event.repository.default_branch || 'master' }}" >> $GITHUB_OUTPUT

  rocm71-hipblas-build:
    strategy:
      fail-fast: false
      matrix:
        gpu_target: [gfx1151]

    runs-on: self-hosted
    needs: get-default-branch

    env:
      BRANCH_NAME: ${{ needs.get-default-branch.outputs.branch }}
      ROCM_VERSION: "7.1.0"
      # Individual GPU target for this build
      GPU_TARGET: ${{ matrix.gpu_target }}

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Setup ROCm (Ubuntu)
        run: |
          # Remove existing ROCm packages to avoid conflicts
          sudo apt remove --purge -y rocm-* hip-* hipcc rocm-cmake rocm-utils || true
          sudo apt autoremove -y
          
          # Install ROCm 7.1
          sudo apt update
          sudo apt install -y wget gnupg
          
          # Add ROCm GPG key to proper location
          sudo mkdir -p /etc/apt/keyrings
          wget -qO - https://repo.radeon.com/rocm/rocm.gpg.key | sudo gpg --batch --yes --dearmor -o /etc/apt/keyrings/rocm.gpg
          
          # Add both ROCm repositories
          sudo tee /etc/apt/sources.list.d/rocm.list << EOF
          deb [arch=amd64 signed-by=/etc/apt/keyrings/rocm.gpg] https://repo.radeon.com/rocm/apt/7.1 noble main
          deb [arch=amd64 signed-by=/etc/apt/keyrings/rocm.gpg] https://repo.radeon.com/graphics/7.1/ubuntu noble main
          EOF
          
          # Set repository priority
          sudo tee /etc/apt/preferences.d/rocm-pin-600 << EOF
          Package: *
          Pin: release o=repo.radeon.com
          Pin-Priority: 600
          EOF
          
          sudo apt update
          sudo apt install -y \
            rocm-dev \
            clang \
            hip-dev \
            hipblas-dev \
            rocblas-dev \
            cmake \
            ninja-build \
            build-essential \
            logrotate \
            ninja-build

      - name: Cache build
        uses: actions/cache@v4
        with:
          path: |
            ~/.ccache
            build
          key: ${{ runner.os }}-rocm71-${{ matrix.gpu_target }}-${{ hashFiles('**/CMakeLists.txt', '**/*.cpp', '**/*.h') }}
          restore-keys: |
            ${{ runner.os }}-rocm71-${{ matrix.gpu_target }}-

      - name: Build with CMake
        run: |
          # Setup ccache
          sudo apt install -y ccache
          export PATH="/usr/lib/ccache:$PATH"
          
          # Set HIP_PATH environment variable
          export HIP_PATH="/opt/rocm"
          export CMAKE_PREFIX_PATH="/opt/rocm"
          
          # Clean and create build directory
          mkdir -p build
          cd build
          
          echo "Building for GPU target: ${{ matrix.gpu_target }}"
          
          # Configure with ROCm 7.1 and specific GPU target (Ubuntu 24.04 version)
          cmake .. \
            -G "Unix Makefiles" \
            -DCMAKE_C_COMPILER=clang \
            -DCMAKE_CXX_COMPILER=clang++ \
            -DSD_HIPBLAS=ON \
            -DSD_BUILD_SHARED_LIBS=ON \
            -DGGML_NATIVE=OFF \
            -DCMAKE_BUILD_TYPE=Release \
            -DGPU_TARGETS="${{ env.GPU_TARGET }}" \
            -DAMDGPU_TARGETS="${{ env.GPU_TARGET }}"
          
          # Build with all available cores
          cmake --build . --config Release --parallel $(nproc)

      - name: Test HipBLAS build
        run: |
          cd build
          # Test if hipblas libraries are linked correctly
          ldd bin/* 2>/dev/null | grep -i hip || echo "No direct HIP dependencies found"
          ldd bin/* 2>/dev/null | grep -i rocm || echo "No ROCm dependencies found"
          echo "Built binaries for GPU target ${{ matrix.gpu_target }}:"
          ls -la bin/

      - name: Get commit hash
        id: commit
        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' || github.event.inputs.create_release == 'true' }}
        run: |
          echo "short=$(git rev-parse --short=7 HEAD)" >> $GITHUB_OUTPUT

      - name: Package artifacts
        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' || github.event.inputs.create_release == 'true' }}
        run: |
          cd build
          
          # Create package directory
          mkdir -p package/lib package/bin package/include
          
          # Copy binaries
          cp -r bin/* package/bin/ || echo "No binaries to copy"
          
          # Copy ROCm libraries and their dependencies
          if [ -d "/opt/rocm/lib" ]; then
            echo "Copying ROCm libraries..."
            
            # Create rocblas and hipblaslt library directories
            mkdir -p package/lib/rocblas/library
            mkdir -p package/lib/hipblaslt/library
            
            # Copy HipBLAS libraries
            cp /opt/rocm/lib/libhipblas.so* package/lib/ 2>/dev/null || echo "hipblas lib not found"
            cp /opt/rocm/lib/librocblas.so* package/lib/ 2>/dev/null || echo "rocblas lib not found"
            cp /opt/rocm/lib/libhipblaslt.so* package/lib/ 2>/dev/null || echo "hipblaslt lib not found"
            
            # Copy rocblas and hipblaslt library files
            cp -r /opt/rocm/lib/rocblas/library/* package/lib/rocblas/library/ 2>/dev/null || echo "rocblas library files not found"
            cp -r /opt/rocm/lib/hipblaslt/library/* package/lib/hipblaslt/library/ 2>/dev/null || echo "hipblaslt library files not found"
            
            # Copy HIP runtime libraries
            cp /opt/rocm/lib/libamdhip64.so* package/lib/ 2>/dev/null || echo "amdhip64 lib not found"
            cp /opt/rocm/lib/libhiprtc.so* package/lib/ 2>/dev/null || echo "hiprtc lib not found"
            
            # Copy ROCm runtime libraries
            cp /opt/rocm/lib/libhsa-runtime64.so* package/lib/ 2>/dev/null || echo "hsa-runtime64 lib not found"
            cp /opt/rocm/lib/librccl.so* package/lib/ 2>/dev/null || echo "rccl lib not found"
            
            # Copy additional ROCm libraries that might be needed
            cp /opt/rocm/lib/librocm_smi64.so* package/lib/ 2>/dev/null || echo "rocm_smi64 lib not found"
          fi
          
          # Create a launch script that sets up the library path
                    cat > package/bin/run_sd.sh << 'EOF'
          #!/bin/bash
          SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
          export LD_LIBRARY_PATH="$SCRIPT_DIR/../lib:/opt/rocm/lib:$LD_LIBRARY_PATH"
          exec "$SCRIPT_DIR/sd" "$@"
          EOF
                    chmod +x package/bin/run_sd.sh
          
          # Create archive with GPU target in filename
          tar -czf ../sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-linux-rocm71-${{ matrix.gpu_target }}-x64.tar.gz -C package .

      - name: Upload artifacts
        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' || github.event.inputs.create_release == 'true' }}
        uses: actions/upload-artifact@v4
        with:
          name: sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-linux-rocm71-${{ matrix.gpu_target }}-x64
          path: |
            sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-linux-rocm71-${{ matrix.gpu_target }}-x64.tar.gz
          retention-days: 7

      - name: Create Release
        if: ${{ github.event.inputs.create_release == 'true' && github.ref == 'refs/heads/master' }}
        uses: softprops/action-gh-release@v1
        with:
          tag_name: rocm71-${{ matrix.gpu_target }}-${{ steps.commit.outputs.short }}
          name: ROCm 7.1 HipBLAS Build ${{ matrix.gpu_target }} ${{ steps.commit.outputs.short }}
          body: |
            ## ROCm 7.1 HipBLAS Build for ${{ matrix.gpu_target }}
            
            **GPU Target:** `${{ matrix.gpu_target }}`
            **Commit:** ${{ steps.commit.outputs.short }}
            
            Built with:
            - ROCm 7.1.0
            - HipBLAS support
            - Optimized for GPU architecture: ${{ matrix.gpu_target }}
            
            ### Downloads
            - Linux: `sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-linux-rocm71-${{ matrix.gpu_target }}-x64.tar.gz`
          files: |
            sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-linux-rocm71-${{ matrix.gpu_target }}-x64.tar.gz
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 