name: Concrete Python Tests (Linux)

on:
  workflow_dispatch:
    inputs:
      instance_id:
        description: 'Instance ID'
        type: string
      instance_image_id:
        description: 'Instance AMI ID'
        type: string
      instance_type:
        description: 'Instance product type'
        type: string
      runner_name:
        description: 'Action runner name'
        type: string
      request_id:
        description: 'Slab request ID'
        type: string

# concurrency:
#   group: concrete_python_tests_linux-${{ github.ref }}
#   cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

env:
  DOCKER_IMAGE_TEST: ghcr.io/zama-ai/concrete-compiler
  CUDA_PATH: /usr/local/cuda-12.3
  GCC_VERSION: 11
  GLIB_VER: 2_28

jobs:
  BuildConcreteCompilerAndTestConcretePythonInLinux:
    name: Build Concrete Compiler and Test Concrete Python in Linux
    runs-on: ${{ github.event.inputs.runner_name }}
    if: ${{ !cancelled() }}
    steps:
      - name: Log instance configuration
        run: |
          echo "IDs: ${{ inputs.instance_id }}"
          echo "AMI: ${{ inputs.instance_image_id }}"
          echo "Type: ${{ inputs.instance_type }}"
          echo "Request ID: ${{ inputs.request_id }}"

      - name: Set up SSH agent
        uses: webfactory/ssh-agent@v0.7.0
        with:
          ssh-private-key: ${{ secrets.CONCRETE_CI_SSH_PRIVATE }}

      - name: Set up GitHub environment
        run: |
          echo "HOME=/home/ubuntu" >> "${GITHUB_ENV}"
          #echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK)" >> "${GITHUB_ENV}"
          echo "SSH_AUTH_SOCK_DIR=$(dirname $SSH_AUTH_SOCK)" >> "${GITHUB_ENV}"
          
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: recursive
          token: ${{ secrets.CONCRETE_ACTIONS_TOKEN }}

      - name: Create build directory
        run: mkdir build

      - name: Setup rust toolchain for concrete-cpu
        uses: ./.github/workflows/setup_rust_toolchain_for_concrete_cpu

      - name: Build bindings
        uses: addnab/docker-run-action@v3
        if: ${{ !contains(inputs.instance_type, 'p3') }}
        id: build-compiler-bindings
        with:
          registry: ghcr.io
          image: ${{ env.DOCKER_IMAGE_TEST }}
          username: ${{ secrets.GHCR_LOGIN }}
          password: ${{ secrets.GHCR_PASSWORD }}
          options: >-
            -v ${{ github.workspace }}:/concrete
            -v ${{ github.workspace }}/build:/build
            -v ${{ env.SSH_AUTH_SOCK }}:/ssh.socket
            -e SSH_AUTH_SOCK=/ssh.socket
          shell: bash
          run: |
            rustup toolchain install nightly-2024-01-31
            set -e
            rm -rf /build/*

            dnf -y install graphviz graphviz-devel

            cd /concrete/frontends/concrete-python
            make venv
            source .venv/bin/activate
            
            cd /concrete/compilers/concrete-compiler/compiler
            make BUILD_DIR=/build DATAFLOW_EXECUTION_ENABLED=ON CCACHE=ON Python3_EXECUTABLE=$(which python3) python-bindings
            
            echo "Debug: ccache statistics (after the build):"
            ccache -s

      - name: Test
        uses: addnab/docker-run-action@v3
        if: ${{ !contains(inputs.instance_type, 'p3') }}
        with:
          registry: ghcr.io
          image: ${{ env.DOCKER_IMAGE_TEST }}
          username: ${{ secrets.GHCR_LOGIN }}
          password: ${{ secrets.GHCR_PASSWORD }}
          options: >-
            -v ${{ github.workspace }}:/concrete
            -v ${{ github.workspace }}/build:/build
          shell: bash
          run: |
            set -e

            dnf -y install graphviz graphviz-devel
            
            cd /concrete/frontends/concrete-python
            make venv
            source .venv/bin/activate

            export COMPILER_BUILD_DIRECTORY=/build
            
            mkdir ./KeySetCache
            KEY_CACHE_DIRECTORY=./KeySetCache make pytest
            
      - name: Build bindings gpu
        uses: addnab/docker-run-action@v3
        if: ${{ contains(inputs.instance_type, 'p3') }}
        id: build-compiler-bindings-gpu
        with:
          registry: ghcr.io
          image: ${{ env.DOCKER_IMAGE_TEST }}
          username: ${{ secrets.GHCR_LOGIN }}
          password: ${{ secrets.GHCR_PASSWORD }}
          options: >-
            -v ${{ github.workspace }}:/concrete
            -v ${{ github.workspace }}/build:/build
            -v ${{ github.workspace }}/wheels:/wheels
            -v ${{ env.SSH_AUTH_SOCK }}:/ssh.socket
            -e SSH_AUTH_SOCK=/ssh.socket
            --gpus all
          shell: bash
          run: |
            set -e
            rm -rf /build/*

            cd /concrete/frontends/concrete-python
            make venv
            source .venv/bin/activate
            
            cd /concrete/compilers/concrete-compiler/compiler
            make BUILD_DIR=/build CCACHE=ON DATAFLOW_EXECUTION_ENABLED=ON Python3_EXECUTABLE=$(which python3) CUDA_SUPPORT=ON CUDA_PATH=${{ env.CUDA_PATH }} python-bindings
            
            echo "Debug: ccache statistics (after the build):"
            ccache -s

      - name: Test gpu
        uses: addnab/docker-run-action@v3
        if: ${{ contains(inputs.instance_type, 'p3') }}
        with:
          registry: ghcr.io
          image: ${{ env.DOCKER_IMAGE_TEST }}
          username: ${{ secrets.GHCR_LOGIN }}
          password: ${{ secrets.GHCR_PASSWORD }}
          options: >-
            -v ${{ github.workspace }}:/concrete
            -v ${{ github.workspace }}/build:/build
            -v ${{ github.workspace }}/wheels:/wheels
            --gpus all
          shell: bash
          run: |
            set -e
            
            cd /concrete/frontends/concrete-python
            make venv
            source .venv/bin/activate

            export COMPILER_BUILD_DIRECTORY=/build
            KEY_CACHE_DIRECTORY=/tmp/KeySetCache mkdir ./KeySetCache
            make pytest-gpu
            
            chmod -R ugo+rwx /tmp/KeySetCache
