name: Build and Test

on:
  workflow_call:
    inputs:
      runner:
        required: true
        type: string
        description: The runner selected to run on
      devices:
        required: true
        type: string
        description: The devices selected to run on
      image:
        required: true
        type: string
        description: The docker iamge which will be loaded

jobs:
  build-and-test:
    runs-on: ${{ inputs.runner }}
    defaults:
      run:
        shell: bash
    container:
      image: ${{ inputs.image }}
      env:
        HOME: /root
      volumes:
        - /usr/local/dcmi:/usr/local/dcmi
        - /usr/local/bin/npu-smi:/usr/local/bin/npu-smi
        - /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/
        - /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info
        - /root/codes/:/root/codes
      options: --network host ${{ inputs.devices }} --device /dev/davinci_manager --device /dev/devmm_svm --device /dev/hisi_hdc
    steps:
      - name: Set environment
        run: |
          source /root/.bashrc && conda activate torch_npu
      - name: Prepare the codes
        run: |
          \cp -rf /root/codes/ /root/build
      - name: Compile torch
        working-directory: /root/build/pytorch/pytorch
        run: |
          pip3 install -r requirements.txt -i https://mirrors.huaweicloud.com/repository/pypi/simple
          export _GLIBCXX_USE_CXX11_ABI=1
          export USE_CUDA=0
          export USE_XNNPACK=0
          python setup.py develop
      - name: Compile and install torch_npu
        working-directory: /root/build/npu/pytorch
        run: |
          pip3 install -r requirements.txt -i https://mirrors.huaweicloud.com/repository/pypi/simple
          bash ci/build.sh --python=3.8
          pip3 install dist/torch_npu*.whl
      - name: Do the test
        working-directory: /root/build
        run: |
          pip3 install -r npu/pytorch/test/requirements.txt -i https://mirrors.huaweicloud.com/repository/pypi/simple --no-deps
          python npu/pytorch/ci/access_control_test.py