Skip to content

Commit 511a85d

Browse files
huydhnfacebook-github-bot
authored andcommitted
Refactor pull workflow to avoid code duplication and add quantization export test (#84)
Summary: This PR addresses the second part of T160762924 and refactors the pull workflows in the following parts: * Make buck2 and cmake part of the build matrix, so we don't need to have 2 separate jobs for them * Run `test-custom-ops-[linux,macos]` in their own jobs. Atm, they are run multiple times for each example models * Add quantization export for supporting [models](https://github.com/pytorch/executorch/blob/main/examples/quantization/example.py#L99) Pull Request resolved: #84 Test Plan: https://github.com/pytorch/executorch/actions/runs/5931450236 Reviewed By: larryliu0820 Differential Revision: D48534520 Pulled By: huydhn fbshipit-source-id: 33dbb4033e23cae5f5677a8dd518f09295c7ad9f
1 parent 6c0dbd9 commit 511a85d

File tree

8 files changed

+156
-109
lines changed

8 files changed

+156
-109
lines changed

.ci/scripts/gather_test_models.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
from typing import Any
1111

1212
from examples.models import MODEL_NAME_TO_MODEL
13+
from examples.quantization.example import QUANT_MODEL_NAME_TO_MODEL
14+
15+
BUILD_TOOLS = [
16+
"buck2",
17+
"cmake",
18+
]
1319

1420

1521
def set_output(name: str, val: Any) -> None:
@@ -31,7 +37,13 @@ def export_models_for_ci() -> None:
3137
"""
3238
# This is the JSON syntax for configuration matrix used by GitHub
3339
# https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs
34-
models = {"include": [{"model": name} for name in MODEL_NAME_TO_MODEL.keys()]}
40+
models = {"include": []}
41+
for name in MODEL_NAME_TO_MODEL.keys():
42+
quantization = name in QUANT_MODEL_NAME_TO_MODEL
43+
for build_tool in BUILD_TOOLS:
44+
models["include"].append(
45+
{"build-tool": build_tool, "model": name, "quantization": quantization}
46+
)
3547
set_output("models", json.dumps(models))
3648

3749

.ci/scripts/setup-linux.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
set -exu
9+
10+
# shellcheck source=/dev/null
11+
source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
12+
13+
BUILD_TOOL=$1
14+
if [[ -z "${BUILD_TOOL:-}" ]]; then
15+
echo "Missing build tool (require buck2 or cmake), exiting..."
16+
exit 1
17+
else
18+
echo "Setup Linux for ${BUILD_TOOL} ..."
19+
fi
20+
21+
# As Linux job is running inside a Docker container, all of its dependencies
22+
# have already been installed
23+
install_executorch
24+
build_executorch_runner "${BUILD_TOOL}"

.ci/scripts/setup-macos.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ set -exu
1010
# shellcheck source=/dev/null
1111
source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
1212

13+
BUILD_TOOL=$1
14+
if [[ -z "${BUILD_TOOL:-}" ]]; then
15+
echo "Missing build tool (require buck2 or cmake), exiting..."
16+
exit 1
17+
else
18+
echo "Setup MacOS for ${BUILD_TOOL} ..."
19+
fi
20+
1321
install_buck() {
1422
if ! command -v zstd &> /dev/null; then
1523
brew install zstd
@@ -36,6 +44,10 @@ install_buck() {
3644
fi
3745
}
3846

39-
install_buck
47+
if [[ "${BUILD_TOOL}" == "buck2" ]]; then
48+
install_buck
49+
fi
4050
install_conda
4151
install_pip_dependencies
52+
install_executorch
53+
build_executorch_runner "${BUILD_TOOL}"

.ci/scripts/test-cmake.sh

Lines changed: 0 additions & 54 deletions
This file was deleted.

.ci/scripts/test.sh

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,46 @@ MODEL_NAME=$1
1414
if [[ -z "${MODEL_NAME:-}" ]]; then
1515
echo "Missing model name, exiting..."
1616
exit 1
17-
else
18-
echo "Testing ${MODEL_NAME} ..."
17+
fi
18+
19+
BUILD_TOOL=$2
20+
if [[ -z "${BUILD_TOOL:-}" ]]; then
21+
echo "Missing build tool (require buck2 or cmake), exiting..."
22+
exit 1
23+
fi
24+
25+
QUANTIZATION=$3
26+
if [[ -z "${QUANTIZATION:-}" ]]; then
27+
QUANTIZATION=false
1928
fi
2029

2130
test_model() {
2231
python -m examples.export.export_example --model_name="${MODEL_NAME}"
2332

2433
# Run test model
25-
buck2 run //examples/executor_runner:executor_runner -- --model_path "./${MODEL_NAME}.pte"
34+
if [[ "${BUILD_TOOL}" == "buck2" ]]; then
35+
buck2 run //examples/executor_runner:executor_runner -- --model_path "./${MODEL_NAME}.pte"
36+
elif [[ "${BUILD_TOOL}" == "cmake" ]]; then
37+
CMAKE_OUTPUT_DIR=cmake-out
38+
./"${CMAKE_OUTPUT_DIR}"/executor_runner --model_path "./${MODEL_NAME}.pte"
39+
else
40+
echo "Invalid build tool ${BUILD_TOOL}. Only buck2 and cmake are supported atm"
41+
exit 1
42+
fi
2643
}
2744

28-
build_and_test_executorch() {
29-
# Build executorch runtime
30-
buck2 build //examples/executor_runner:executor_runner
31-
32-
which python
33-
# Test the select model
34-
test_model
45+
test_quantized_model() {
46+
python -m examples.quantization.example --model_name="${MODEL_NAME}"
3547
}
3648

37-
install_executorch
38-
build_and_test_executorch
49+
which python
50+
51+
echo "Testing ${MODEL_NAME} with ${BUILD_TOOL}..."
52+
# Test the select model
53+
test_model
54+
55+
if [[ "${QUANTIZATION}" == true ]]; then
56+
test_quantized_model
57+
else
58+
echo "The model ${MODEL_NAME} doesn't support quantization yet"
59+
fi

.ci/scripts/utils.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,36 @@ install_pip_dependencies() {
3838
--index-url https://download.pytorch.org/whl/nightly/cpu
3939
popd || return
4040
}
41+
42+
build_executorch_runner_buck2() {
43+
# Build executorch runtime
44+
buck2 build //examples/executor_runner:executor_runner
45+
}
46+
47+
build_executorch_runner_cmake() {
48+
CMAKE_OUTPUT_DIR=cmake-out
49+
# Build executorch runtime using cmake
50+
rm -rf "${CMAKE_OUTPUT_DIR}" && mkdir "${CMAKE_OUTPUT_DIR}"
51+
52+
pushd "${CMAKE_OUTPUT_DIR}" || return
53+
cmake -DBUCK2=buck2 -DPYTHON_EXECUTABLE="${PYTHON_EXECUTABLE}" ..
54+
popd || return
55+
56+
if [ "$(uname)" == "Darwin" ]; then
57+
CMAKE_JOBS=$(( $(sysctl -n hw.ncpu) - 1 ))
58+
else
59+
CMAKE_JOBS=$(( $(nproc) - 1 ))
60+
fi
61+
cmake --build "${CMAKE_OUTPUT_DIR}" -j "${CMAKE_JOBS}"
62+
}
63+
64+
build_executorch_runner() {
65+
if [[ $1 == "buck2" ]]; then
66+
build_executorch_runner_buck2
67+
elif [[ $1 == "cmake" ]]; then
68+
build_executorch_runner_cmake
69+
else
70+
echo "Invalid build tool $1. Only buck2 and cmake are supported atm"
71+
exit 1
72+
fi
73+
}

.github/workflows/pull.yml

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ jobs:
3535
3636
PYTHONPATH="${PWD}" python .ci/scripts/gather_test_models.py
3737
38-
buck-build-test-linux:
39-
name: buck-build-test-linux
38+
test-models-linux:
39+
name: test-models-linux
4040
uses: pytorch/test-infra/.github/workflows/linux_job.yml@main
4141
needs: gather-models
4242
strategy:
@@ -53,15 +53,16 @@ jobs:
5353
# here, as it's there in the container
5454
export PATH="/opt/conda/envs/py_${PYTHON_VERSION}/bin:${PATH}"
5555
56-
# The name of model we are going to test
5756
MODEL_NAME=${{ matrix.model }}
57+
BUILD_TOOL=${{ matrix.build-tool }}
58+
QUANTIZATION=${{ matrix.quantization }}
59+
60+
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-linux.sh "${BUILD_TOOL}"
5861
# Build and test Executorch
59-
bash .ci/scripts/test.sh "${MODEL_NAME}"
60-
# Test custom ops
61-
bash examples/custom_ops/test_custom_ops.sh buck2
62+
PYTHON_EXECUTABLE=python bash .ci/scripts/test.sh "${MODEL_NAME}" "${BUILD_TOOL}" "${QUANTIZATION}"
6263
63-
buck-build-test-macos:
64-
name: buck-build-test-macos
64+
test-models-macos:
65+
name: test-models-macos
6566
uses: pytorch/test-infra/.github/workflows/macos_job.yml@main
6667
needs: gather-models
6768
strategy:
@@ -73,25 +74,26 @@ jobs:
7374
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
7475
script: |
7576
WORKSPACE=$(pwd)
76-
7777
pushd "${WORKSPACE}/pytorch/executorch"
78-
# Setup MacOS dependencies as there is no Docker support on MacOS atm
79-
bash .ci/scripts/setup-macos.sh
8078
81-
# The name of model we are going to test
8279
MODEL_NAME=${{ matrix.model }}
80+
BUILD_TOOL=${{ matrix.build-tool }}
81+
QUANTIZATION=${{ matrix.quantization }}
82+
83+
# Setup MacOS dependencies as there is no Docker support on MacOS atm
84+
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-macos.sh "${BUILD_TOOL}"
8385
# Build and test Executorch
84-
PYTHON_EXECUTABLE=python bash .ci/scripts/test.sh "${MODEL_NAME}"
85-
# Test custom ops
86-
PYTHON_EXECUTABLE=python bash examples/custom_ops/test_custom_ops.sh buck2
86+
PYTHON_EXECUTABLE=python bash .ci/scripts/test.sh "${MODEL_NAME}" "${BUILD_TOOL}" "${QUANTIZATION}"
8787
popd
8888
89-
cmake-build-test-linux:
90-
name: cmake-build-test-linux
89+
test-custom-ops-linux:
90+
name: test-custom-ops-linux
9191
uses: pytorch/test-infra/.github/workflows/linux_job.yml@main
92-
needs: gather-models
9392
strategy:
94-
matrix: ${{ fromJSON(needs.gather-models.outputs.models) }}
93+
matrix:
94+
include:
95+
- build-tool: buck2
96+
- build-tool: cmake
9597
fail-fast: false
9698
with:
9799
runner: linux.2xlarge
@@ -104,19 +106,19 @@ jobs:
104106
# here, as it's there in the container
105107
export PATH="/opt/conda/envs/py_${PYTHON_VERSION}/bin:${PATH}"
106108
107-
# The name of model we are going to test
108-
MODEL_NAME=${{ matrix.model }}
109-
# Build and test Executorch
110-
bash .ci/scripts/test-cmake.sh "${MODEL_NAME}"
111-
# Build and test custom ops
112-
bash examples/custom_ops/test_custom_ops.sh cmake
109+
BUILD_TOOL=${{ matrix.build-tool }}
110+
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-linux.sh "${BUILD_TOOL}"
111+
# Test custom ops
112+
PYTHON_EXECUTABLE=python bash examples/custom_ops/test_custom_ops.sh "${BUILD_TOOL}"
113113
114-
cmake-build-test-macos:
115-
name: cmake-build-test-macos
114+
test-custom-ops-macos:
115+
name: test-custom-ops-macos
116116
uses: pytorch/test-infra/.github/workflows/macos_job.yml@main
117-
needs: gather-models
118117
strategy:
119-
matrix: ${{ fromJSON(needs.gather-models.outputs.models) }}
118+
matrix:
119+
include:
120+
- build-tool: buck2
121+
- build-tool: cmake
120122
fail-fast: false
121123
with:
122124
runner: macos-m1-12
@@ -126,13 +128,10 @@ jobs:
126128
WORKSPACE=$(pwd)
127129
128130
pushd "${WORKSPACE}/pytorch/executorch"
129-
# Setup MacOS dependencies as there is no Docker support on MacOS atm
130-
bash .ci/scripts/setup-macos.sh
131131
132-
# The name of model we are going to test
133-
MODEL_NAME=${{ matrix.model }}
134-
# Build and test Executorch
135-
PYTHON_EXECUTABLE=python bash .ci/scripts/test-cmake.sh "${MODEL_NAME}"
132+
BUILD_TOOL=${{ matrix.build-tool }}
133+
# Setup MacOS dependencies as there is no Docker support on MacOS atm
134+
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-macos.sh "${BUILD_TOOL}"
136135
# Build and test custom ops
137-
PYTHON_EXECUTABLE=python bash examples/custom_ops/test_custom_ops.sh cmake
136+
PYTHON_EXECUTABLE=python bash examples/custom_ops/test_custom_ops.sh "${BUILD_TOOL}"
138137
popd

examples/quantization/example.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030

3131
from ..models import MODEL_NAME_TO_MODEL
3232

33+
# Note: for mv3, the mul op is not supported in XNNPACKQuantizer, that could be supported soon
34+
QUANT_MODEL_NAME_TO_MODEL = {
35+
name: MODEL_NAME_TO_MODEL[name] for name in ["linear", "add", "add_mul", "mv2"]
36+
}
37+
3338

3439
def quantize(model_name, model, example_inputs):
3540
"""This is the official recommended flow for quantization in pytorch 2.0 export"""
@@ -94,11 +99,6 @@ def verify_xnnpack_quantizer_matching_fx_quant_model(model_name, model, example_
9499

95100

96101
if __name__ == "__main__":
97-
# Note: for mv3, the mul op is not supported in XNNPACKQuantizer, that could be supported soon
98-
QUANT_MODEL_NAME_TO_MODEL = {
99-
name: MODEL_NAME_TO_MODEL[name] for name in ["linear", "add", "add_mul", "mv2"]
100-
}
101-
102102
parser = argparse.ArgumentParser()
103103
parser.add_argument(
104104
"-m",

0 commit comments

Comments
 (0)