version: 2.1

# How to test the Linux jobs:
#   - Install CircleCI local CLI: https://circleci.com/docs/2.0/local-cli/
#   - circleci config process .circleci/config.yml > gen.yml && circleci local execute -c gen.yml --job binary_linux_wheel_py3.8
#     - Replace binary_linux_wheel_py3.8 with the name of the job you want to test.
#       Job names are 'name:' key.

executors:
  windows-cpu:
    machine:
      resource_class: windows.xlarge
      image: windows-server-2019-vs2019:stable
      shell: bash.exe

  windows-gpu:
    machine:
      resource_class: windows.gpu.nvidia.medium
      image: windows-server-2019-nvidia:stable
      shell: bash.exe

commands:
  generate_cache_key:
    description: "Generates a cache key file that changes daily"
    steps:
      - run:
          name: Generate cache key
          command: echo "$(date +"%Y-%m-%d")" > .cachekey
  designate_upload_channel:
    description: "inserts the correct upload channel into ${BASH_ENV}"
    steps:
      - run:
          name: adding UPLOAD_CHANNEL to BASH_ENV
          command: |
            our_upload_channel=nightly
            # On tags upload to test instead
            if [[ -n "${CIRCLE_TAG}" ]] || [[ ${CIRCLE_BRANCH} =~ release/* ]]; then
              our_upload_channel=test
            fi
            echo "export UPLOAD_CHANNEL=${our_upload_channel}" >> ${BASH_ENV}
  load_conda_channel_flags:
    description: "Determines whether we need extra conda channels"
    steps:
      - run:
          name: Adding CONDA_CHANNEL_FLAGS to BASH_ENV
          command: |
              CONDA_CHANNEL_FLAGS=""
              # formerly used to add conda-forge flags for Python 3.9, reserving the mechanism for future python upgrades
  windows_install_cuda:
    description: "Install desired CUDA version on Windows runners"
    steps:
      - run:
          name: Install CUDA
          command: |
              packaging/windows/internal/cuda_install.bat

binary_common: &binary_common
  parameters:
    # Edit these defaults to do a release
    build_version:
      description: "version number of release binary; by default, build a nightly"
      type: string
      default: ""
    pytorch_version:
      description: "PyTorch version to build against; by default, use a nightly"
      type: string
      default: ""
    # Don't edit these
    python_version:
      description: "Python version to build against (e.g., 3.8)"
      type: string
    cuda_version:
      description: "CUDA version to build against (e.g., cpu, cu101)"
      type: string
      default: "cpu"
    wheel_docker_image:
      description: "Wheel only: what docker image to use"
      type: string
      default: "pytorch/manylinux-cuda116"
    conda_docker_image:
      description: "Conda only: what docker image to use"
      type: string
      default: "pytorch/conda-builder:cuda116"
  environment: &environment
    PYTHON_VERSION: << parameters.python_version >>
    BUILD_VERSION: << parameters.build_version >>
    PYTORCH_VERSION: << parameters.pytorch_version >>
    CU_VERSION: << parameters.cuda_version >>
    MACOSX_DEPLOYMENT_TARGET: 10.9

smoke_test_common: &smoke_test_common
  <<: *binary_common
  docker:
    - image: pytorch/torchaudio_unittest_base:smoke_test-20220425
  resource_class: large

jobs:
  circleci_consistency:
    docker:
      - image: cimg/python:3.8
    steps:
      - checkout
      - run:
          command: |
            pip install --user --progress-bar off jinja2 pyyaml
            python .circleci/regenerate.py
            git diff --exit-code || (echo ".circleci/config.yml not in sync with config.yml.in! Run .circleci/regenerate.py to update config"; exit 1)

  lint_python_and_config:
    docker:
      - image: circleci/python:3.8
    steps:
      - checkout
      - run:
          name: Install pre-commit
          command: pip install --user --progress-bar off pre-commit
      - run:
          name: Install pre-commit hooks
          command: pre-commit install-hooks
      - run:
          name: Lint Python code and config files
          command: pre-commit run --all-files
      - run:
          name: Required lint modifications
          when: always
          command: git --no-pager diff --color=always

  download_third_parties:
    docker:
      - image: "pytorch/torchaudio_unittest_base:manylinux"
    resource_class: small
    steps:
      - checkout
      - generate_cache_key
      - restore_cache:

          keys:
            - tp-nix-v2-{{ checksum ".cachekey" }}

      - run:
          command: |
              mkdir -p third_party/archives/
              wget --no-clobber --directory-prefix=third_party/archives/ $(awk '/URL /{print $2}' third_party/*/CMakeLists.txt)
      - save_cache:

          key: tp-nix-v2-{{ checksum ".cachekey" }}

          paths:
            - third_party/archives
      - persist_to_workspace:
          root: third_party
          paths:
            - archives

  build_ffmpeg_linux:
    <<: *binary_common
    docker:
      - image: << parameters.wheel_docker_image >>
    resource_class: 2xlarge+
    steps:
      - checkout
      - generate_cache_key
      - restore_cache:

          keys:
            - ffmpeg-linux-v0-{{ checksum ".cachekey" }}

      - run:
          command: |
            export FFMPEG_ROOT=${PWD}/third_party/ffmpeg
            if [[ ! -d ${FFMPEG_ROOT} ]]; then
                packaging/ffmpeg/build.sh
            fi
      - save_cache:

          key: ffmpeg-linux-v0-{{ checksum ".cachekey" }}

          paths:
            - third_party/ffmpeg
      - persist_to_workspace:
          root: third_party
          paths:
            - ffmpeg

  build_ffmpeg_macos:
    <<: *binary_common
    macos:
      xcode: "14.0"
    steps:
      - checkout
      - generate_cache_key
      - restore_cache:

          keys:
            - ffmpeg-macos-v0-{{ checksum ".cachekey" }}

      - run:
          command: |
            export FFMPEG_ROOT=${PWD}/third_party/ffmpeg
            if [[ ! -d ${FFMPEG_ROOT} ]]; then
                packaging/ffmpeg/build.sh
            fi
      - save_cache:

          key: ffmpeg-macos-v0-{{ checksum ".cachekey" }}

          paths:
            - third_party/ffmpeg
      - persist_to_workspace:
          root: third_party
          paths:
            - ffmpeg

  build_ffmpeg_windows:
    <<: *binary_common
    machine:
      resource_class: windows.xlarge
      image: windows-server-2019-vs2019:stable
      # Note:
      # Unlike other Windows job, this job uses cmd.exe as shell because
      # we need to invoke bash.exe from msys2 in ffmpeg build process, and doing so
      # from different installation of bash.exe (the one from the VM) cause issue
      shell: cmd.exe
    steps:
      - checkout
      - run: date /t > .cachekey
      - restore_cache:

          keys:
            - ffmpeg-windows-{{ checksum ".cachekey" }}

      - run: packaging\ffmpeg\build.bat
      - save_cache:

          key: ffmpeg-windows-{{ checksum ".cachekey" }}

          paths:
            - third_party/ffmpeg
      - persist_to_workspace:
          root: third_party
          paths:
            - ffmpeg

  unittest_linux_cpu:
    <<: *binary_common
    docker:
      - image: pytorch/torchaudio_unittest_base:manylinux-20210121
    resource_class: 2xlarge+
    steps:
      - checkout
      - attach_workspace:
          at: third_party
      - designate_upload_channel
      - load_conda_channel_flags
      - run:
          name: Setup
          command: .circleci/unittest/linux/scripts/setup_env.sh
      - run:
          name: Install torchaudio
          command: .circleci/unittest/linux/scripts/install.sh
          environment:
              USE_FFMPEG: true
      - run:
          name: Run tests
          command: .circleci/unittest/linux/scripts/run_test.sh
          environment:
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CUDA: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_HW_ACCEL: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_ON_PYTHON_310: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_AUDIO_OUT_DEVICE: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MACOS: true
      - store_test_results:
          path: test-results
      - store_artifacts:
          path: test/htmlcov

  unittest_windows_cpu:
    <<: *binary_common
    executor:
      name: windows-cpu
    steps:
      - checkout
      - designate_upload_channel
      - load_conda_channel_flags
      - run:
          name: Setup
          command: .circleci/unittest/windows/scripts/setup_env.sh
      - run:
          name: Install torchaudio
          command: .circleci/unittest/windows/scripts/install.sh
          environment:
              USE_FFMPEG: true
      - run:
          name: Run tests
          command: .circleci/unittest/windows/scripts/run_test.sh
          environment:
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_APPLY_CMVN_SLIDING: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_FBANK_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_KALDI_PITCH_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_MFCC_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_SPECTROGRAM_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_SOX: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CUDA: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_HW_ACCEL: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_KALDI: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_SOX: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_ON_PYTHON_310: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MOD_sentencepiece: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_AUDIO_OUT_DEVICE: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MACOS: true
      - store_test_results:
          path: test-results
      - store_artifacts:
          path: test/htmlcov

  unittest_windows_gpu:
    <<: *binary_common
    executor:
      name: windows-gpu
    environment:
      <<: *environment
      CUDA_VERSION: "11.7"
    steps:
      - checkout
      - designate_upload_channel
      - load_conda_channel_flags
      - run:
          name: Setup
          command: .circleci/unittest/windows/scripts/setup_env.sh
      - run:
          name: Install CUDA
          command: packaging/windows/internal/cuda_install.bat
      - run:
          name: Update CUDA driver
          command: packaging/windows/internal/driver_update.bat
      - run:
          name: Install torchaudio
          command: .circleci/unittest/windows/scripts/install.sh
          environment:
              USE_FFMPEG: true
      - run:
          name: Run tests
          command: .circleci/unittest/windows/scripts/run_test.sh
          environment:
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_APPLY_CMVN_SLIDING: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_FBANK_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_KALDI_PITCH_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_MFCC_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_SPECTROGRAM_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_SOX: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_KALDI: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_SOX: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_ON_PYTHON_310: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MOD_sentencepiece: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_CUDA_SMALL_MEMORY: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_AUDIO_OUT_DEVICE: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MACOS: true
      - store_test_results:
          path: test-results
      - store_artifacts:
          path: test/htmlcov

  unittest_macos_cpu:
    <<: *binary_common
    macos:
      xcode: "14.0"
    resource_class: large
    steps:
      - checkout
      - load_conda_channel_flags
      - attach_workspace:
          at: third_party
      - designate_upload_channel
      - run:
          name: Setup
          command: .circleci/unittest/linux/scripts/setup_env.sh
      - run:
          name: Install torchaudio
          command: .circleci/unittest/linux/scripts/install.sh
          environment:
              USE_FFMPEG: true
              USE_OPENMP: false
      - run:
          name: Run tests
          command: .circleci/unittest/linux/scripts/run_test.sh
          environment:
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_APPLY_CMVN_SLIDING: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_FBANK_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_KALDI_PITCH_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_MFCC_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CMD_COMPUTE_SPECTROGRAM_FEATS: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_CUDA: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_HW_ACCEL: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_QUANTIZATION: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_ON_PYTHON_310: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MOD_sentencepiece: true
              TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_AUDIO_OUT_DEVICE: true
      - store_test_results:
          path: test-results
      - store_artifacts:
          path: test/htmlcov

  stylecheck:
    <<: *binary_common
    docker:
      - image: "pytorch/torchaudio_unittest_base:manylinux"
    resource_class: medium
    steps:
      - checkout
      - designate_upload_channel
      - load_conda_channel_flags
      - run:
          name: Setup
          command: .circleci/unittest/linux/scripts/setup_env.sh
      - run:
          name: Run style check
          command: .circleci/unittest/linux/scripts/run_style_checks.sh

workflows:
  lint:
    jobs:
      - lint_python_and_config
  unittest:
    jobs:
      - circleci_consistency
      - download_third_parties:
          name: download_third_parties
      - unittest_linux_cpu:
          cuda_version: cpu
          name: unittest_linux_cpu_py3.8
          python_version: '3.8'
          requires:
          - download_third_parties
      - stylecheck:
          cuda_version: cpu
          name: stylecheck_py3.8
          python_version: '3.8'
      - unittest_linux_cpu:
          cuda_version: cpu
          name: unittest_linux_cpu_py3.9
          python_version: '3.9'
          requires:
          - download_third_parties
      - unittest_linux_cpu:
          cuda_version: cpu
          name: unittest_linux_cpu_py3.10
          python_version: '3.10'
          requires:
          - download_third_parties
      - unittest_linux_cpu:
          cuda_version: cpu
          name: unittest_linux_cpu_py3.11
          python_version: '3.11'
          requires:
          - download_third_parties
      - unittest_windows_cpu:
          cuda_version: cpu
          name: unittest_windows_cpu_py3.8
          python_version: '3.8'
          requires:
          - download_third_parties
      - unittest_windows_gpu:
          cuda_version: cu118
          name: unittest_windows_gpu_py3.8
          python_version: '3.8'
          requires:
          - download_third_parties
      - unittest_macos_cpu:
          cuda_version: cpu
          name: unittest_macos_cpu_py3.8
          python_version: '3.8'
          requires:
          - download_third_parties
