|
| 1 | +#!/bin/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 | +# Usage: |
| 9 | +# ./build/test_wheel_install.sh <path-to-whl>|<path-to-zipped-whl> |
| 10 | +# |
| 11 | +# Installs the wheel in a fresh conda environment named ${CONDA_ENV} and tries |
| 12 | +# creating and running an XNNPACK-delegated .pte file. |
| 13 | +# |
| 14 | +# Must be run from the root of an `executorch` repo so that it can use the |
| 15 | +# XNNPACK example code to create a .pte file. |
| 16 | + |
| 17 | +set -euxo pipefail |
| 18 | + |
| 19 | +CONDA_ENV=executorch-tmp |
| 20 | +PYTHON_VERSION=3.10.0 |
| 21 | + |
| 22 | +# Create a temp dir that we can create files under. |
| 23 | +WORK_DIR="$(mktemp -d /tmp/test-wheel-install-XXXXXXXXXX)" |
| 24 | +readonly WORK_DIR |
| 25 | + |
| 26 | +# Creates or resets a conda environment named ${CONDA_ENV} |
| 27 | +clean_conda() { |
| 28 | + eval "$(conda shell.bash hook)" |
| 29 | + conda activate base |
| 30 | + conda remove -y --name "${CONDA_ENV}" --all || echo "(ignoring error)" |
| 31 | + conda create -yn "${CONDA_ENV}" python="${PYTHON_VERSION}" |
| 32 | + conda activate "${CONDA_ENV}" |
| 33 | +} |
| 34 | + |
| 35 | +test_xnnpack_e2e() { |
| 36 | + # Create a .pte file that delegates to XNNPACK. |
| 37 | + local pte="mv2_xnnpack_fp32.pte" |
| 38 | + rm -f "${pte}" # Make sure it's a fresh one. |
| 39 | + python3 -m examples.xnnpack.aot_compiler --model_name="mv2" --delegate |
| 40 | + test -f "${pte}" |
| 41 | + |
| 42 | + # Test python script |
| 43 | + local test_py="${WORK_DIR}/test_xnnpack_e2e.py" |
| 44 | + cat > "${test_py}" << HERE |
| 45 | +from executorch.extension.pybindings import portable_lib |
| 46 | +m = portable_lib._load_for_executorch("${pte}") |
| 47 | +
|
| 48 | +# Import torch after importing and using portable_lib to demonstrate that |
| 49 | +# portable_lib works without importing this first. |
| 50 | +import torch |
| 51 | +t = torch.randn((1, 3, 224, 224)) |
| 52 | +
|
| 53 | +output = m.forward([t]) |
| 54 | +assert len(output) == 1, f"Unexpected output length {len(output)}" |
| 55 | +assert output[0].size() == torch.Size([1, 1000]), f"Unexpected output size {output[0].size()}" |
| 56 | +print("PASS") |
| 57 | +HERE |
| 58 | + ( |
| 59 | + set +x |
| 60 | + echo "===== BEGIN ${test_py} =====" |
| 61 | + cat "${test_py}" |
| 62 | + echo "===== END ${test_py} =====" |
| 63 | + ) |
| 64 | + |
| 65 | + python "${test_py}" |
| 66 | +} |
| 67 | + |
| 68 | +main() { |
| 69 | + if [ "$#" -ne 1 ]; then |
| 70 | + echo "Usage: $(basename "$0") <path-to-whl>|<path-to-zipped-whl>" >&2 |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + local wheel="$1" |
| 74 | + |
| 75 | + if [[ "${wheel}" == *".zip" ]]; then |
| 76 | + local unzip_dir="${WORK_DIR}/unzip" |
| 77 | + unzip -d "${unzip_dir}" "${wheel}" |
| 78 | + wheel="$(ls "${unzip_dir}"/*.whl | head -1)" |
| 79 | + fi |
| 80 | + |
| 81 | + # Create a fresh conda environment. |
| 82 | + clean_conda |
| 83 | + |
| 84 | + # Install the minimal deps. |
| 85 | + pip install --extra-index-url "https://download.pytorch.org/whl/test/cpu" \ |
| 86 | + torch=="2.3.0" \ |
| 87 | + torchvision=="0.18.0" |
| 88 | + |
| 89 | + # Install the provided wheel. |
| 90 | + pip install "${wheel}" |
| 91 | + |
| 92 | + # Try creating and running a .pte file that delegates to XNNPACK. |
| 93 | + test_xnnpack_e2e |
| 94 | + |
| 95 | + # Only delete on success so we can debug failures. |
| 96 | + rm -rf "${WORK_DIR}" |
| 97 | +} |
| 98 | + |
| 99 | +main "$@" |
0 commit comments