name: backend-ncnn

on:
  push:
    paths-ignore:
      - "demo/**"
      - "tools/**"

  pull_request:
    paths-ignore:
      - "demo/**"
      - "tools/**"
      - "docs/**"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test_onnx2ncnn:
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        python-version: [3.7]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: 'recursive'
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          sudo apt update
          sudo apt install wget gcc-multilib g++-multilib wget libprotobuf-dev protobuf-compiler
          python -m pip install cmake onnx
      - name: Install ncnn
        run: |
          wget https://github.com/Tencent/ncnn/archive/refs/tags/20220420.tar.gz
          tar xf 20220420.tar.gz
          pushd ncnn-20220420
          mkdir build && pushd build
          cmake -DCMAKE_INSTALL_PREFIX=$(pwd)/../install -DNCNN_BUILD_TESTS=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF ..
          cmake --build . -j2
          make install
          popd && popd
      - name: Install mmdeploy with ncnn backend
        run: |
          mkdir -p build && pushd build
          export LD_LIBRARY_PATH=/home/runner/work/mmdeploy/mmdeploy/ncnn-20220420/install/lib/:$LD_LIBRARY_PATH
          cmake -DMMDEPLOY_TARGET_BACKENDS=ncnn -Dncnn_DIR=/home/runner/work/mmdeploy/mmdeploy/ncnn-20220420/install/lib/cmake/ncnn/ ..
          make mmdeploy_onnx2ncnn -j2
          popd
      - name: Test onnx2ncnn
        run: |
          echo $(pwd)
          ln -s build/bin/mmdeploy_onnx2ncnn ./
          python .github/scripts/test_onnx2ncnn.py --run 1

  build_ncnn:
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        python-version: [3.7]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: 'recursive'
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install mmdeploy
        run: |
          python -m pip install torch==1.8.2 torchvision==0.9.2 --extra-index-url https://download.pytorch.org/whl/lts/1.8/cpu
          python -m pip install mmcv-lite
          python tools/scripts/build_ubuntu_x64_ncnn.py 8
          python -c 'import mmdeploy.apis.ncnn as ncnn_api; assert ncnn_api.is_available(with_custom_ops=True)'

  test_ncnn_ptq:
    runs-on: [self-hosted, linux-3090]
    container:
      image: openmmlab/mmdeploy:ubuntu20.04-cuda11.3
      options: "--gpus=all --ipc=host"
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Install dependencies
        run: |
          apt-get update
          apt-get install ninja-build -y
          python3 -V
          python3 -m pip install openmim
          python3 -m pip install -r requirements.txt
          python3 -m mim install $(cat requirements/codebases.txt | grep mmpretrain)
          python3 -m pip install numpy==1.22.0
          python3 -m pip list
      - name: Install mmdeploy
        run: |
          rm -rf .eggs && python3 -m pip install -e .
          python3 tools/check_env.py
      - name: Install ppq
        run: |
          git clone -b v0.6.6 --depth 1 https://github.com/openppl-public/ppq
          cd ppq
          python3 -m pip install -r requirements.txt
          python3 setup.py install
      - name: Test ncnn + ppq pipeline
        run: |
          export PYTHONPATH=${PWD}/ppq:${PYTHONPATH}
          export LD_LIBRARY_PATH="/root/workspace/mmdeploy/build/lib:${LD_LIBRARY_PATH}"
          export LD_LIBRARY_PATH="/root/workspace/mmdeploy/mmdeploy/lib:${LD_LIBRARY_PATH}"
          export work_dir=./work_dir
          mkdir -p $work_dir
          export model_cfg=$work_dir/resnet18_8xb32_in1k.py
          export deploy_cfg=configs/mmpretrain/classification_ncnn-int8_static.py
          export checkpoint=$work_dir/resnet18_8xb32_in1k_20210831-fbbb1da6.pth
          export input_img=tests/data/tiger.jpeg
          python3 -m mim download mmpretrain --config resnet18_8xb32_in1k --dest $work_dir
          python3 tools/torch2onnx.py $deploy_cfg $model_cfg $checkpoint $input_img --work-dir $work_dir
          wget https://github.com/open-mmlab/mmdeploy/releases/download/v0.1.0/dataset.tar
          tar xvf dataset.tar
          python3 tools/onnx2ncnn_quant_table.py \
            --onnx $work_dir/end2end.onnx \
            --deploy-cfg $deploy_cfg \
            --model-cfg $model_cfg \
            --out-onnx $work_dir/quant.onnx \
            --out-table $work_dir/ncnn.table \
            --image-dir ./dataset
          ls -sha $work_dir/quant.onnx
          cat $work_dir/ncnn.table
