name: rknn

on:
  push:
    branches:
      - master
      - ci-rknn-2

  pull_request:
    branches:
      - master

  workflow_dispatch:

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

jobs:
  generate_build_matrix:
    if: github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa'
    # see https://github.com/pytorch/pytorch/pull/50633
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Generating build matrix
        id: set-matrix
        run: |
          # outputting for debugging purposes
          python ./.github/scripts/docker/generate_build_matrix.py --torch-version=2.4.0 --python-version=3.10
          MATRIX=$(python ./.github/scripts/docker/generate_build_matrix.py --torch-version=2.4.0 --python-version=3.10)
          echo "::set-output name=matrix::${MATRIX}"
  rknn:
    needs: generate_build_matrix
    name: py${{ matrix.python-version }} torch${{ matrix.torch-version }} v${{ matrix.version }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        ${{ fromJson(needs.generate_build_matrix.outputs.matrix) }}

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup Python
        if: false
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Export ONNX model
        uses: addnab/docker-run-action@v3
        with:
            image: ghcr.io/${{ github.repository_owner }}/icefall:cpu-py${{ matrix.python-version }}-torch${{ matrix.torch-version }}-v${{ matrix.version }}
            options: |
              --volume ${{ github.workspace }}/:/icefall
            shell: bash
            run: |
              cat /etc/*release
              lsb_release -a
              uname -a
              python3 --version
              export PYTHONPATH=/icefall:$PYTHONPATH
              cd /icefall
              git config --global --add safe.directory /icefall

              python3 -m torch.utils.collect_env
              python3 -m k2.version
              pip list


              # Install rknn
              curl -SL -O https://huggingface.co/csukuangfj/rknn-toolkit2/resolve/main/rknn_toolkit2-2.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
              pip install ./*.whl "numpy<=1.26.4"
              pip list | grep rknn
              echo "---"
              pip list
              echo "---"

              .github/scripts/librispeech/ASR/run_rknn.sh

      - name: Display rknn models
        shell: bash
        run: |
          ls -lh

          ls -lh rknn-models/*
          echo "----"
          ls -lh rknn-models-small/*

      - name: Collect results (small)
        shell: bash
        run: |
          for platform in rk3562 rk3566 rk3568 rk3576 rk3588; do
            dst=sherpa-onnx-$platform-streaming-zipformer-small-bilingual-zh-en-2023-02-16
            mkdir $dst
            mkdir $dst/test_wavs
            src=rknn-models-small/$platform

            cp -v $src/*.rknn $dst/
            cp -v $src/tokens.txt $dst/
            cp -v $src/*.wav $dst/test_wavs/
            ls -lh $dst
            tar cjfv $dst.tar.bz2 $dst
            rm -rf $dst
          done

      - name: Collect results
        shell: bash
        run: |
          for platform in rk3562 rk3566 rk3568 rk3576 rk3588; do
            dst=sherpa-onnx-$platform-streaming-zipformer-bilingual-zh-en-2023-02-20
            mkdir $dst
            mkdir $dst/test_wavs
            src=rknn-models/$platform

            cp -v $src/*.rknn $dst/
            cp -v $src/tokens.txt $dst/
            cp -v $src/*.wav $dst/test_wavs/
            ls -lh $dst
            tar cjfv $dst.tar.bz2 $dst
            rm -rf $dst
          done

      - name: Display results
        shell: bash
        run: |
          ls -lh *rk*.tar.bz2

      - name: Release to GitHub
        uses: svenstaro/upload-release-action@v2
        with:
          file_glob: true
          overwrite: true
          file: sherpa-onnx-*.tar.bz2
          repo_name: k2-fsa/sherpa-onnx
          repo_token: ${{ secrets.UPLOAD_GH_SHERPA_ONNX_TOKEN }}
          tag: asr-models

      - name: Upload model to huggingface
        if: github.event_name == 'push'
        env:
          HF_TOKEN: ${{ secrets.HF_TOKEN }}
        uses: nick-fields/retry@v3
        with:
          max_attempts: 20
          timeout_seconds: 200
          shell: bash
          command: |
            git config --global user.email "csukuangfj@gmail.com"
            git config --global user.name "Fangjun Kuang"

            rm -rf huggingface
            export GIT_LFS_SKIP_SMUDGE=1

            git clone https://huggingface.co/csukuangfj/sherpa-onnx-rknn-models huggingface
            cd huggingface

            git fetch
            git pull
            git merge -m "merge remote" --ff origin main
            dst=streaming-asr
            mkdir -p $dst
            rm -fv $dst/*
            cp ../*rk*.tar.bz2 $dst/

            ls -lh $dst
            git add .
            git status
            git commit -m "update models"
            git status

            git push https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-rknn-models main || true
            rm -rf huggingface
