|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +# Builds the ExecuTorch ESP-IDF component the way a project using the published |
| 9 | +# component gets it: generates and packs the component, adds the archive to a |
| 10 | +# file:// component mirror, and builds the idf_component_consumer project, which |
| 11 | +# resolves executorch from that mirror, with ESP-IDF's own Python environment |
| 12 | +# (no ExecuTorch) and with source, model and build paths that have a space. |
| 13 | +# Each build runs the smoke model on the emulator; the ESP32-S3 and ESP32-C6 |
| 14 | +# builds also run a Cadence-quantized streaming wake word model frame by frame |
| 15 | +# against PyTorch and a quantized mul on the esp_nn:: op library, and the |
| 16 | +# ESP32-S3 build an ESP-NN delegate program. |
| 17 | +# ESPRESSIF_IDF_VERSION selects the ESP-IDF release: v6.1 builds every target |
| 18 | +# an emulator runs (QEMU for ESP32, esp-emu for the rest), other releases |
| 19 | +# ESP32-S3 and ESP32-C6. Also lints the component's manifest. |
| 20 | + |
| 21 | +set -euo pipefail |
| 22 | + |
| 23 | +et_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) |
| 24 | +idf_version="${ESPRESSIF_IDF_VERSION:-v6.1}" |
| 25 | +build_dir="${ESPRESSIF_BUILD_DIR:-${et_root}/cmake-out-esp-idf-consumer-${idf_version}}" |
| 26 | +export ESPRESSIF_TOOLS_DIR="${ESPRESSIF_TOOLS_DIR:-${RUNNER_TEMP:-/tmp}/executorch-espressif-${idf_version}}" |
| 27 | +mkdir -p "${build_dir}" |
| 28 | +build_dir=$(cd "${build_dir}" && pwd) |
| 29 | +cd "${et_root}" |
| 30 | + |
| 31 | +if [[ "${idf_version}" == v6.1 ]]; then |
| 32 | + targets=(esp32s3 esp32 esp32c3 esp32c5 esp32c6 esp32p4) |
| 33 | +else |
| 34 | + targets=(esp32s3 esp32c6) |
| 35 | +fi |
| 36 | + |
| 37 | +collect_artifacts() { |
| 38 | + if [[ -n "${RUNNER_ARTIFACT_DIR:-}" ]]; then |
| 39 | + mkdir -p "${RUNNER_ARTIFACT_DIR}/esp-idf-consumer" |
| 40 | + cp "${build_dir}"/*.log "${RUNNER_ARTIFACT_DIR}/esp-idf-consumer/" || true |
| 41 | + fi |
| 42 | +} |
| 43 | +trap collect_artifacts EXIT |
| 44 | + |
| 45 | +# ESP-IDF's QEMU needs these libraries, and setup.sh fails its install check |
| 46 | +# without them. |
| 47 | +if [[ " ${targets[*]} " == *" esp32 "* ]] && ! python -c ' |
| 48 | +import ctypes |
| 49 | +ctypes.CDLL("libSDL2-2.0.so.0") |
| 50 | +ctypes.CDLL("libslirp.so.0")' 2> /dev/null; then |
| 51 | + sudo apt-get update |
| 52 | + sudo apt-get install -y --no-install-recommends libsdl2-2.0-0 libslirp0 |
| 53 | +fi |
| 54 | +bash examples/espressif/setup.sh --minimal --idf-version="${idf_version}" \ |
| 55 | + --targets="$(IFS=,; echo "${targets[*]}")" 2>&1 | tee "${build_dir}/setup.log" |
| 56 | +# shellcheck source=/dev/null |
| 57 | +source "${ESPRESSIF_TOOLS_DIR}/setup_path.sh" |
| 58 | + |
| 59 | +# Projects keep their models and kernel bundles in their tree, so these get a |
| 60 | +# path with a space too. |
| 61 | +assets="${build_dir}/consumer assets" |
| 62 | +rm -rf "${assets}" |
| 63 | +mkdir -p "${assets}" |
| 64 | +python examples/espressif/export_smoke_model.py --output "${assets}/smoke.bpte" \ |
| 65 | + 2>&1 | tee "${build_dir}/export.log" |
| 66 | +python examples/espressif/export_wakeword_model.py --output "${assets}/wakeword.bpte" \ |
| 67 | + --frames 40 2>&1 | tee -a "${build_dir}/export.log" |
| 68 | +python examples/espressif/idf_component_smoke/export_op_library_model.py \ |
| 69 | + --output "${assets}/mul.bpte" 2>&1 | tee -a "${build_dir}/export.log" |
| 70 | +python examples/espressif/export_espnn_test_programs.py --output-dir "${build_dir}/espnn" \ |
| 71 | + --only conv-act-pool-linear 2>&1 | tee -a "${build_dir}/export.log" |
| 72 | +cp "${build_dir}/espnn/conv-act-pool-linear.bpte" "${assets}/delegate.bpte" |
| 73 | +split=examples/espressif/idf_component_smoke/split_bundled_program.py |
| 74 | +python "${split}" "${assets}/smoke.bpte" --atol 0 --rtol 0 --repeat 2 |
| 75 | +python "${split}" "${assets}/wakeword.bpte" --atol 1e-6 --rtol 0 |
| 76 | +# test_mul.py's tolerance, torch.finfo(torch.float32).eps, rounded up. |
| 77 | +python "${split}" "${assets}/mul.bpte" --atol 0 --rtol 1.2e-7 |
| 78 | +read -r atol rtol < <(python -c ' |
| 79 | +import json, sys |
| 80 | +(entry,) = json.load(open(sys.argv[1])) |
| 81 | +print(entry["atol"], entry["rtol"]) |
| 82 | +' "${build_dir}/espnn/manifest.json") |
| 83 | +python "${split}" "${assets}/delegate.bpte" --atol "${atol}" --rtol "${rtol}" |
| 84 | + |
| 85 | +component_dir="${build_dir}/component/executorch" |
| 86 | +bash examples/espressif/build_idf_component.sh --out "${component_dir}" \ |
| 87 | + 2>&1 | tee "${build_dir}/generate.log" |
| 88 | +compote manifest lint "${component_dir}/idf_component.yml" |
| 89 | +version=$(sed -n 's/^version: "\(.*\)"$/\1/p' "${component_dir}/idf_component.yml") |
| 90 | +(cd "${component_dir}" && compote component pack --name executorch --dest-dir "${build_dir}/dist") |
| 91 | +archive="${build_dir}/dist/executorch_${version}.tgz" |
| 92 | +mirror="${build_dir}/mirror" |
| 93 | +rm -rf "${mirror}" |
| 94 | +python .ci/scripts/esp_idf_mirror.py "${archive}" "${mirror}" \ |
| 95 | + --namespace pytorch --name executorch |
| 96 | + |
| 97 | +gen_kernels() { |
| 98 | + local out=$1 |
| 99 | + shift |
| 100 | + python "${component_dir}/tools/gen_kernels.py" "$@" --out "${out}" \ |
| 101 | + 2>&1 | tee -a "${build_dir}/gen_kernels.log" |
| 102 | +} |
| 103 | +cpu_models="${assets}/smoke.pte;${assets}/wakeword.pte;${assets}/mul.pte" |
| 104 | +all_models="${cpu_models};${assets}/delegate.pte" |
| 105 | +gen_kernels "${assets}/smoke_kernels" --pte "${assets}/smoke.pte" |
| 106 | +gen_kernels "${assets}/cpu_kernels" --pte "${assets}/smoke.pte" \ |
| 107 | + --pte "${assets}/wakeword.pte" --pte "${assets}/mul.pte" |
| 108 | +gen_kernels "${assets}/all_kernels" --pte "${assets}/smoke.pte" \ |
| 109 | + --pte "${assets}/wakeword.pte" --pte "${assets}/mul.pte" --pte "${assets}/delegate.pte" |
| 110 | + |
| 111 | +# A fresh ESP-IDF Python environment, which cannot import executorch, and no |
| 112 | +# EXECUTORCH_PYTHON to bring one back. |
| 113 | +idf_python_env="${build_dir}/idf_python_env" |
| 114 | +mkdir -p "${idf_python_env}" |
| 115 | +IDF_PYTHON_ENV_PATH="${idf_python_env}" python "${IDF_PATH}/tools/idf_tools.py" \ |
| 116 | + install-python-env 2>&1 | tee "${build_dir}/idf_python_env.log" |
| 117 | +if "${idf_python_env}/bin/python" -c "import executorch" 2> /dev/null; then |
| 118 | + echo "FAIL: ESP-IDF's Python environment can import executorch" >&2 |
| 119 | + exit 1 |
| 120 | +fi |
| 121 | +unset EXECUTORCH_PYTHON |
| 122 | +# Built from a copy, so the component manager's state and the consumer's |
| 123 | +# generated manifest stay out of the source tree, under a path with a space. |
| 124 | +sources="${build_dir}/consumer sources" |
| 125 | +rm -rf "${sources}" |
| 126 | +mkdir -p "${sources}/idf_component_smoke" |
| 127 | +cp -r examples/espressif/idf_component_consumer "${sources}/" |
| 128 | +cp -r examples/espressif/idf_component_smoke/main \ |
| 129 | + examples/espressif/idf_component_smoke/sdkconfig.defaults "${sources}/idf_component_smoke/" |
| 130 | +project="${sources}/idf_component_consumer" |
| 131 | +firmware_root="${build_dir}/consumer builds" |
| 132 | + |
| 133 | +# Runs idf.py on the consumer with ESP-IDF's Python and the mirror: build name, |
| 134 | +# target, then idf.py arguments. |
| 135 | +consumer() { |
| 136 | + local name=$1 target=$2 |
| 137 | + shift 2 |
| 138 | + IDF_TARGET="${target}" IDF_PYTHON_ENV_PATH="${idf_python_env}" \ |
| 139 | + IDF_COMPONENT_STORAGE_URL="file://${mirror};default" \ |
| 140 | + "${idf_python_env}/bin/python" "${IDF_PATH}/tools/idf.py" -C "${project}" \ |
| 141 | + -B "${firmware_root}/${name}" -DSDKCONFIG="${firmware_root}/${name}/sdkconfig" \ |
| 142 | + -DET_COMPONENT_VERSION="${version}" "$@" |
| 143 | +} |
| 144 | + |
| 145 | +# Runs a build on its emulator and checks its output: build name, target, then |
| 146 | +# the per-model markers. |
| 147 | +run_firmware() { |
| 148 | + local name=$1 target=$2 |
| 149 | + shift 2 |
| 150 | + local firmware_dir="${firmware_root}/${name}" log="${build_dir}/emulator-${name}.log" |
| 151 | + if [[ "${target}" == esp32 ]]; then |
| 152 | + # QEMU has no exit-on string, so stop it once the app is done. |
| 153 | + setsid bash -c 'consumer "$@" qemu' _ "${name}" "${target}" < /dev/null > "${log}" 2>&1 & |
| 154 | + local pid=$! |
| 155 | + for _ in $(seq 240); do |
| 156 | + if grep -qF "Program complete." "${log}"; then |
| 157 | + break |
| 158 | + fi |
| 159 | + sleep 0.5 |
| 160 | + done |
| 161 | + kill -- "-${pid}" 2> /dev/null || true |
| 162 | + wait "${pid}" || true |
| 163 | + else |
| 164 | + consumer "${name}" "${target}" merge-bin > "${build_dir}/merge-${name}.log" 2>&1 |
| 165 | + timeout 130s esp-emu --chip "${target}" \ |
| 166 | + --firmware "${firmware_dir}/merged-binary.bin" \ |
| 167 | + --elf "${firmware_dir}/et_component_smoke.elf" \ |
| 168 | + --net user --timeout 120s --exit-on 'Program complete.' \ |
| 169 | + > "${log}" 2>&1 || true |
| 170 | + fi |
| 171 | + for marker in "$@" "ET_COMPONENT_SMOKE: PASS" "Program complete."; do |
| 172 | + if ! grep -qF "${marker}" "${log}"; then |
| 173 | + echo "FAIL: ${name} printed no '${marker}'; see emulator-${name}.log" >&2 |
| 174 | + exit 1 |
| 175 | + fi |
| 176 | + done |
| 177 | + echo "OK: ${name} passed on the ${target} emulator" |
| 178 | +} |
| 179 | +export -f consumer |
| 180 | +export idf_python_env mirror project firmware_root version |
| 181 | + |
| 182 | +for target in "${targets[@]}"; do |
| 183 | + if [[ "${target}" == esp32s3 ]]; then |
| 184 | + consumer "${target}" "${target}" -DET_SMOKE_MODELS="${all_models}" \ |
| 185 | + -DET_SMOKE_KERNELS="${assets}/all_kernels" build \ |
| 186 | + > "${build_dir}/firmware-${target}.log" 2>&1 |
| 187 | + run_firmware "${target}" "${target}" "smoke: PASS" "wakeword: PASS, 40/40 cases" \ |
| 188 | + "mul: PASS" "delegate: PASS" |
| 189 | + elif [[ "${target}" == esp32c6 ]]; then |
| 190 | + consumer "${target}" "${target}" -DET_SMOKE_MODELS="${cpu_models}" \ |
| 191 | + -DET_SMOKE_KERNELS="${assets}/cpu_kernels" build \ |
| 192 | + > "${build_dir}/firmware-${target}.log" 2>&1 |
| 193 | + run_firmware "${target}" "${target}" "smoke: PASS" "wakeword: PASS, 40/40 cases" \ |
| 194 | + "mul: PASS" |
| 195 | + else |
| 196 | + consumer "${target}" "${target}" -DET_SMOKE_MODELS="${assets}/smoke.pte" \ |
| 197 | + -DET_SMOKE_KERNELS="${assets}/smoke_kernels" build \ |
| 198 | + > "${build_dir}/firmware-${target}.log" 2>&1 |
| 199 | + run_firmware "${target}" "${target}" "smoke: PASS" |
| 200 | + fi |
| 201 | + python - "${project}/dependencies.lock" "${mirror}/components/pytorch/executorch.json" <<'PY' |
| 202 | +import json |
| 203 | +import sys |
| 204 | +
|
| 205 | +import yaml |
| 206 | +
|
| 207 | +entry = yaml.safe_load(open(sys.argv[1]))["dependencies"]["pytorch/executorch"] |
| 208 | +(mirrored,) = json.load(open(sys.argv[2]))["versions"] |
| 209 | +if (str(entry["version"]), entry.get("component_hash")) != ( |
| 210 | + mirrored["version"], |
| 211 | + mirrored["component_hash"], |
| 212 | +): |
| 213 | + sys.exit(f"FAIL: pytorch/executorch did not come from the mirror: {entry}") |
| 214 | +PY |
| 215 | +done |
| 216 | +echo "PASS: the packed ESP-IDF component built from a mirror and ran on ${targets[*]}." |
0 commit comments