name: quantize

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_ncnn_PTQ:
    runs-on: ubuntu-18.04
    container:
      image: pytorch/pytorch:1.8.0-cuda11.1-cudnn8-devel

    strategy:
      matrix:
        python-version: [3.7]
        torch: [1.8.0+cu111]
        include:
          - torch: 1.8.0+cu111
            torch_version: torch1.8
            torchvision: 0.9.0+cu111

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install system dependencies
        run: |
          apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A4B469963BF863CC
          apt-get update && apt-get install -y wget ffmpeg libsm6 libxext6 git ninja-build libglib2.0-0 libxrender-dev python${{matrix.python-version}}-dev
          apt-get clean
          rm -rf /var/lib/apt/lists/*
      - name: Install PyTorch
        run: python -m pip install torch==${{matrix.torch}} torchvision==${{matrix.torchvision}} -f https://download.pytorch.org/whl/torch_stable.html
      - name: Install dependencies
        run: |
          python -V

          python -m pip install -r requirements.txt
          python -m pip install -U numpy

      - name: Install mmcls
        run: |
          python -m pip install openmim
          python -m mim install $(cat requirements/codebases.txt | grep mmcls)
          git clone --depth 1 --branch 1.x https://github.com/open-mmlab/mmclassification.git ~/mmclassification
          cd ~/mmclassification && python -m pip install . && cd -
      - name: Install ppq
        run: |
          python -m pip install protobuf==3.20.0
          git clone https://github.com/openppl-public/ppq ~/ppq
          cd ~/ppq && git checkout edbecf44c7b203515640e4f4119c000a1b66b33a
          python -m pip install -r requirements.txt
          python setup.py install
          cd -
      - name: Run tests
        run: |
          echo $(pwd)
          python .github/scripts/quantize_to_ncnn.py
