diff --git a/.ci/docker/build.sh b/.ci/docker/build.sh index 16b62b81784..01baa544bea 100755 --- a/.ci/docker/build.sh +++ b/.ci/docker/build.sh @@ -41,6 +41,10 @@ case "${IMAGE_NAME}" in QNN_SDK=yes CLANG_VERSION=12 ;; + executorch-ubuntu-22.04-cadence-sdk) + CADENCE_SDK=yes + CLANG_VERSION=12 + ;; executorch-ubuntu-22.04-mediatek-sdk) MEDIATEK_SDK=yes CLANG_VERSION=12 @@ -81,6 +85,7 @@ docker build \ --build-arg "BUILD_DOCS=${BUILD_DOCS}" \ --build-arg "ARM_SDK=${ARM_SDK:-}" \ --build-arg "QNN_SDK=${QNN_SDK:-}" \ + --build-arg "CADENCE_SDK=${CADENCE_SDK:-}" \ --build-arg "MEDIATEK_SDK=${MEDIATEK_SDK:-}" \ --build-arg "ANDROID_NDK_VERSION=${ANDROID_NDK_VERSION:-}" \ -f "${OS}"/Dockerfile \ diff --git a/.ci/docker/common/install_cadence.sh b/.ci/docker/common/install_cadence.sh new file mode 100644 index 00000000000..03fb31e902d --- /dev/null +++ b/.ci/docker/common/install_cadence.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +set -ex + +# Double check if the cadence version is set, +# TODO: set and read DSP names from secrets +[ -n "${CADENCE_SDK}" ] diff --git a/.ci/docker/ubuntu/Dockerfile b/.ci/docker/ubuntu/Dockerfile index 5efa0825b96..b4a9cd3795f 100644 --- a/.ci/docker/ubuntu/Dockerfile +++ b/.ci/docker/ubuntu/Dockerfile @@ -82,6 +82,13 @@ ARG ARM_SDK ARG QNN_SDK +ARG CADENCE_SDK +# Install cadence if needed +COPY ./common/install_cadence.sh install_cadence.sh +RUN if [ -n "${CADENCE_SDK}" ]; then bash ./install_cadence.sh; fi +RUN rm install_cadence.sh + + ARG MEDIATEK_SDK USER ci-user diff --git a/.ci/scripts/build_cadence_fusionG3.sh b/.ci/scripts/build_cadence_fusionG3.sh new file mode 100644 index 00000000000..b0e1e777065 --- /dev/null +++ b/.ci/scripts/build_cadence_fusionG3.sh @@ -0,0 +1,92 @@ +#!/bin/bash +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +set -euo pipefail + +unset CMAKE_PREFIX_PATH +unset XTENSA_CORE +export XTENSA_CORE=FCV_FG3GP +git submodule sync +git submodule update --init +./backends/cadence/install_requirements.sh +./install_executorch.sh + +rm -rf cmake-out + +STEPWISE_BUILD=false + +if $STEPWISE_BUILD; then + echo "Building ExecuTorch" + CXXFLAGS="-fno-exceptions -fno-rtti" cmake -DCMAKE_INSTALL_PREFIX=cmake-out \ + -DCMAKE_TOOLCHAIN_FILE=./backends/cadence/cadence.cmake \ + -DCMAKE_BUILD_TYPE=Release \ + -DEXECUTORCH_ENABLE_EVENT_TRACER=OFF \ + -DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \ + -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=ON \ + -DEXECUTORCH_BUILD_PTHREADPOOL=OFF \ + -DEXECUTORCH_BUILD_CPUINFO=OFF \ + -DEXECUTORCH_ENABLE_LOGGING=ON \ + -DEXECUTORCH_USE_DL=OFF \ + -DEXECUTORCH_BUILD_CADENCE=OFF \ + -DFLATC_EXECUTABLE="$(which flatc)" \ + -DHAVE_FNMATCH_H=OFF \ + -Bcmake-out . + + echo "Building any Cadence-specific binaries on top" + CXXFLAGS="-fno-exceptions -fno-rtti" cmake -DBUCK2="$BUCK" \ + -DCMAKE_TOOLCHAIN_FILE=/home/zonglinpeng/ws/zonglinpeng/executorch/backends/cadence/cadence.cmake \ + -DCMAKE_INSTALL_PREFIX=cmake-out \ + -DCMAKE_BUILD_TYPE=Release \ + -DEXECUTORCH_BUILD_HOST_TARGETS=ON \ + -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=ON \ + -DEXECUTORCH_BUILD_PTHREADPOOL=OFF \ + -DEXECUTORCH_BUILD_CADENCE=ON \ + -DFLATC_EXECUTABLE="$(which flatc)" \ + -DEXECUTORCH_ENABLE_LOGGING=ON \ + -DEXECUTORCH_ENABLE_PROGRAM_VERIFICATION=ON \ + -DEXECUTORCH_USE_DL=OFF \ + -DBUILD_EXECUTORCH_PORTABLE_OPS=ON \ + -DEXECUTORCH_BUILD_KERNELS_CUSTOM=OFF \ + -DPYTHON_EXECUTABLE=python3 \ + -DEXECUTORCH_FUSION_G3_OPT=ON \ + -DEXECUTORCH_BUILD_GFLAGS=ON \ + -DHAVE_FNMATCH_H=OFF \ + -Bcmake-out/backends/cadence \ + backends/cadence + cmake --build cmake-out/backends/cadence -j8 +else + echo "Building Cadence toolchain with ExecuTorch packages" + cmake_prefix_path="${PWD}/cmake-out/lib/cmake/ExecuTorch;${PWD}/cmake-out/third-party/gflags" + CXXFLAGS="-fno-exceptions -fno-rtti" cmake -DBUCK2="$BUCK" \ + -DCMAKE_PREFIX_PATH="${cmake_prefix_path}" \ + -DHAVE_SYS_STAT_H=ON \ + -DCMAKE_TOOLCHAIN_FILE=./backends/cadence/cadence.cmake \ + -DCMAKE_INSTALL_PREFIX=cmake-out \ + -DCMAKE_BUILD_TYPE=Release \ + -DEXECUTORCH_BUILD_HOST_TARGETS=ON \ + -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=ON \ + -DEXECUTORCH_BUILD_PTHREADPOOL=OFF \ + -DEXECUTORCH_BUILD_CPUINFO=OFF \ + -DEXECUTORCH_BUILD_FLATC=OFF \ + -DEXECUTORCH_BUILD_CADENCE=ON \ + -DFLATC_EXECUTABLE="$(which flatc)" \ + -DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \ + -DEXECUTORCH_ENABLE_LOGGING=ON \ + -DEXECUTORCH_ENABLE_PROGRAM_VERIFICATION=ON \ + -DEXECUTORCH_USE_DL=OFF \ + -DBUILD_EXECUTORCH_PORTABLE_OPS=ON \ + -DEXECUTORCH_BUILD_KERNELS_CUSTOM=OFF \ + -DPYTHON_EXECUTABLE=python3 \ + -DEXECUTORCH_FUSION_G3_OPT=ON \ + -DHAVE_FNMATCH_H=OFF \ + -Bcmake-out + cmake --build cmake-out --target install --config Release -j8 +fi + +echo "Run simple model to verify cmake build" +python3 -m examples.portable.scripts.export --model_name="add" +xt-run --turbo cmake-out/executor_runner --model_path=add.pte diff --git a/.ci/scripts/build_cadence_hifi4.sh b/.ci/scripts/build_cadence_hifi4.sh new file mode 100644 index 00000000000..50fd5f032ab --- /dev/null +++ b/.ci/scripts/build_cadence_hifi4.sh @@ -0,0 +1,90 @@ +#!/bin/bash +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +set -euo pipefail + +unset CMAKE_PREFIX_PATH +unset XTENSA_CORE +export XTENSA_CORE=nxp_rt600_RI23_11_newlib +git submodule sync +git submodule update --init +./backends/cadence/install_requirements.sh +./install_executorch.sh + +rm -rf cmake-out + +STEPWISE_BUILD=false + +if $STEPWISE_BUILD; then + echo "Building ExecuTorch" + CXXFLAGS="-fno-exceptions -fno-rtti" cmake -DCMAKE_INSTALL_PREFIX=cmake-out \ + -DCMAKE_TOOLCHAIN_FILE=./backends/cadence/cadence.cmake \ + -DCMAKE_BUILD_TYPE=Release \ + -DEXECUTORCH_ENABLE_EVENT_TRACER=OFF \ + -DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \ + -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=OFF \ + -DEXECUTORCH_BUILD_PTHREADPOOL=OFF \ + -DEXECUTORCH_BUILD_CPUINFO=OFF \ + -DEXECUTORCH_ENABLE_LOGGING=ON \ + -DEXECUTORCH_USE_DL=OFF \ + -DEXECUTORCH_BUILD_CADENCE=OFF \ + -DFLATC_EXECUTABLE="$(which flatc)" \ + -Bcmake-out . + + echo "Building any Cadence-specific binaries on top" + CXXFLAGS="-fno-exceptions -fno-rtti" cmake -DBUCK2="$BUCK" \ + -DCMAKE_TOOLCHAIN_FILE=./backends/cadence/cadence.cmake \ + -DCMAKE_INSTALL_PREFIX=cmake-out \ + -DCMAKE_BUILD_TYPE=Release \ + -DEXECUTORCH_BUILD_HOST_TARGETS=ON \ + -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=ON \ + -DEXECUTORCH_BUILD_PTHREADPOOL=OFF \ + -DEXECUTORCH_BUILD_CADENCE=ON \ + -DFLATC_EXECUTABLE="$(which flatc)" \ + -DEXECUTORCH_ENABLE_LOGGING=ON \ + -DEXECUTORCH_ENABLE_PROGRAM_VERIFICATION=ON \ + -DEXECUTORCH_USE_DL=OFF \ + -DBUILD_EXECUTORCH_PORTABLE_OPS=ON \ + -DEXECUTORCH_BUILD_KERNELS_CUSTOM=OFF \ + -DPYTHON_EXECUTABLE=python3 \ + -DEXECUTORCH_NNLIB_OPT=ON \ + -DEXECUTORCH_BUILD_GFLAGS=ON \ + -DHAVE_FNMATCH_H=OFF \ + -Bcmake-out/backends/cadence \ + backends/cadence + cmake --build cmake-out/backends/cadence -j8 +else + echo "Building Cadence toolchain with ExecuTorch packages" + cmake_prefix_path="${PWD}/cmake-out/lib/cmake/ExecuTorch;${PWD}/cmake-out/third-party/gflags" + CXXFLAGS="-fno-exceptions -fno-rtti" cmake -DBUCK2="$BUCK" \ + -DCMAKE_PREFIX_PATH="${cmake_prefix_path}" \ + -DCMAKE_TOOLCHAIN_FILE=./backends/cadence/cadence.cmake \ + -DCMAKE_INSTALL_PREFIX=cmake-out \ + -DCMAKE_BUILD_TYPE=Release \ + -DEXECUTORCH_BUILD_HOST_TARGETS=ON \ + -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=ON \ + -DEXECUTORCH_BUILD_PTHREADPOOL=OFF \ + -DEXECUTORCH_BUILD_CPUINFO=OFF \ + -DEXECUTORCH_BUILD_FLATC=OFF \ + -DEXECUTORCH_BUILD_CADENCE=ON \ + -DFLATC_EXECUTABLE="$(which flatc)" \ + -DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \ + -DEXECUTORCH_ENABLE_LOGGING=ON \ + -DEXECUTORCH_ENABLE_PROGRAM_VERIFICATION=ON \ + -DEXECUTORCH_USE_DL=OFF \ + -DBUILD_EXECUTORCH_PORTABLE_OPS=ON \ + -DEXECUTORCH_BUILD_KERNELS_CUSTOM=OFF \ + -DPYTHON_EXECUTABLE=python3 \ + -DEXECUTORCH_NNLIB_OPT=ON \ + -DHAVE_FNMATCH_H=OFF \ + -Bcmake-out + cmake --build cmake-out --target install --config Release -j8 +fi + +echo "Run simple model to verify cmake build" +python3 -m examples.portable.scripts.export --model_name="add" +xt-run --turbo cmake-out/executor_runner --model_path=add.pte diff --git a/.ci/scripts/build_cadence_runner.sh b/.ci/scripts/build_cadence_runner.sh new file mode 100755 index 00000000000..a8f44719dc7 --- /dev/null +++ b/.ci/scripts/build_cadence_runner.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +# Builds cadence_runner and prints its path. + +set -euo pipefail + +SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +readonly SCRIPT_DIR + +readonly EXECUTORCH_ROOT="${SCRIPT_DIR}/../.." + +# Allow overriding the number of build jobs. Default to 9. +export CMAKE_BUILD_PARALLEL_LEVEL="${CMAKE_BUILD_PARALLEL_LEVEL:-9}" + +main() { + cd "${EXECUTORCH_ROOT}" + + rm -rf cmake-out + CXXFLAGS="-fno-exceptions -fno-rtti" cmake -DCMAKE_INSTALL_PREFIX=cmake-out \ + -DCMAKE_BUILD_TYPE=Release \ + -DEXECUTORCH_BUILD_DEVTOOLS=ON \ + -DEXECUTORCH_ENABLE_EVENT_TRACER=ON \ + -DEXECUTORCH_ENABLE_LOGGING=ON \ + -Bcmake-out . + cmake --build cmake-out --target install --config Release -j16 + + local example_dir=backends/cadence + local build_dir="cmake-out/${example_dir}" + local cmake_prefix_path="${PWD}/cmake-out/lib/cmake/ExecuTorch;${PWD}/cmake-out/third-party/gflags" + rm -rf ${build_dir} + CXXFLAGS="-fno-exceptions -fno-rtti" cmake -DCMAKE_PREFIX_PATH="${cmake_prefix_path}" \ + -DCMAKE_BUILD_TYPE=Release \ + -DEXECUTORCH_CADENCE_CPU_RUNNER=ON \ + -DEXECUTORCH_ENABLE_LOGGING=ON \ + -B"${build_dir}" \ + "${example_dir}" + cmake --build "${build_dir}" --config Release -j16 + + local runner="${PWD}/${build_dir}/cadence_runner" + if [[ ! -f "${runner}" ]]; then + echo "ERROR: Failed to build ${build_dir}/cadence_runner" >&2 + exit 1 + else + echo "Built ${build_dir}/cadence_runner" + fi +} + +main "$@" diff --git a/.github/workflows/cadence-build.yml b/.github/workflows/cadence-build.yml new file mode 100644 index 00000000000..e0ad68b2a19 --- /dev/null +++ b/.github/workflows/cadence-build.yml @@ -0,0 +1,111 @@ +name: cadence-build + +on: + push: + branches: + - main + - release/* + pull_request: + paths: + - .ci/scripts/setup-cadence.sh + - .github/workflows/cadence-build.yml + - install_executorch.sh + - backends/cadence/** + - examples/cadence/** + workflow_dispatch: +# schedule: +# - cron: '0 10 * * *' # Runs daily at 2 AM PST + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}-${{ github.event_name == 'workflow_dispatch' }}-${{ github.event_name == 'schedule' }} + cancel-in-progress: true + +jobs: + setup-env: + name: setup-env + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.10 + uses: actions/setup-python@v3 + with: + python-version: "3.10" + + set-envir-vars: + name: set-envir-vars + runs-on: ubuntu-22.04 + needs: setup-env + outputs: + version: ${{ steps.set_version.outputs.version }} + steps: + - name: Set VERSION variable + id: set_version + shell: bash + run: | + VERSION="0.5.0.$(TZ='PST8PDT' date +%Y%m%d)" + echo "version=$VERSION" >> "$GITHUB_OUTPUT" + + setup-cadence: + uses: pytorch/test-infra/.github/workflows/linux_job_v2.yml@main + needs: set-envir-vars + permissions: + id-token: write + contents: read + strategy: + fail-fast: false + with: + runner: linux.2xlarge + docker-image: executorch-ubuntu-22.04-cadence-sdk + submodules: 'true' + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} + script: | + # The generic Linux job chooses to use base env, not the one setup by the image + CONDA_ENV=$(conda env list --json | jq -r ".envs | .[-1]") + conda activate "${CONDA_ENV}" + + PYTHON_EXECUTABLE=python bash .ci/scripts/setup-linux.sh "cmake" + + # install cadence requirements + bash backends/cadence/install_requirements.sh + + + dummy-custom-build: + name: dummy-custom-build + needs: setup-cadence + runs-on: ubuntu-22.04 + env: + CADENCE_VERSION: secret! + steps: + - name: run cadence cpu build script + shell: bash + run: | + .ci/scripts/build_cadence_runner.sh + - name: run cadence op test + shell: bash + run: | + python -m examples.cadence.operators.quantized_linear_op + + # g3-build: + # # NB: Don't run this on fork PRs because they won't have access to the secret and would fail anyway + # if: ${{ !github.event.pull_request.head.repo.fork }} + # runs-on: ubuntu-22.04 + # hifi-build: + # # NB: Don't run this on fork PRs because they won't have access to the secret and would fail anyway + # if: ${{ !github.event.pull_request.head.repo.fork }} + # runs-on: ubuntu-22.04 + # mv130-build: + # # NB: Don't run this on fork PRs because they won't have access to the secret and would fail anyway + # if: ${{ !github.event.pull_request.head.repo.fork }} + # runs-on: ubuntu-22.04 + # hifi-build: + # # NB: Don't run this on fork PRs because they won't have access to the secret and would fail anyway + # if: ${{ !github.event.pull_request.head.repo.fork }} + # runs-on: ubuntu-22.04 + # # uses: pytorch/test-infra/.github/workflows/macos_job.yml@main + # # secrets: inherit + # with: + # runner: linux.2xlarge + # docker-image: executorch-ubuntu-22.04-cadence-sdk + # fetch-depth: 0 + # ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} + # timeout: 90 diff --git a/.github/workflows/docker-builds.yml b/.github/workflows/docker-builds.yml index 73af9842a20..0976db5f4ac 100644 --- a/.github/workflows/docker-builds.yml +++ b/.github/workflows/docker-builds.yml @@ -38,6 +38,7 @@ jobs: - docker-image-name: executorch-ubuntu-22.04-linter - docker-image-name: executorch-ubuntu-22.04-arm-sdk - docker-image-name: executorch-ubuntu-22.04-qnn-sdk + - docker-image-name: executorch-ubuntu-22.04-cadence-sdk - docker-image-name: executorch-ubuntu-22.04-mediatek-sdk - docker-image-name: executorch-ubuntu-22.04-clang12-android env: diff --git a/.github/workflows/pull.yml b/.github/workflows/pull.yml index b599f2fdc67..d09a84c8ea9 100644 --- a/.github/workflows/pull.yml +++ b/.github/workflows/pull.yml @@ -361,6 +361,33 @@ jobs: contents: read needs: test-llama-runner-linux + cadence-cpu: + name: cadence-cpu + uses: pytorch/test-infra/.github/workflows/linux_job_v2.yml@main + permissions: + id-token: write + contents: read + strategy: + fail-fast: false + with: + runner: linux.2xlarge + docker-image: executorch-ubuntu-22.04-cadence-sdk + submodules: 'true' + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} + timeout: 90 + script: | + # The generic Linux job chooses to use base env, not the one setup by the image + CONDA_ENV=$(conda env list --json | jq -r ".envs | .[-1]") + conda activate "${CONDA_ENV}" + + PYTHON_EXECUTABLE=python bash .ci/scripts/setup-linux.sh "cmake" + + # install cadence requirements + bash backends/cadence/install_requirements.sh + + # run python unittest + python -m examples.cadence.models.resnet50 + unittest: uses: ./.github/workflows/_unittest.yml permissions: @@ -480,7 +507,7 @@ jobs: # Setup install_requirements for llama PYTHON_EXECUTABLE=python bash examples/models/llama/install_requirements.sh - + # Test static llama weight sharing and accuracy PYTHON_EXECUTABLE=python bash .ci/scripts/test_qnn_static_llama.sh