Skip to content

Commit 9a3eebb

Browse files
committed
Add ccache support.
Signed-off-by: Puneet Matharu <puneet.matharu@arm.com>
1 parent 8830aff commit 9a3eebb

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed

.github/workflows/pytorch.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ jobs:
6767
{ name: c8g, label: ah-ubuntu_22_04-c8g_8x }
6868
]
6969
runs-on: ${{ matrix.config.label }}
70+
env:
71+
CCACHE_HOST_DIR: ${{ github.workspace }}/Tool-Solutions/ML-Frameworks/pytorch-aarch64/.ccache
7072
steps:
7173
- name: Checkout Tool-Solutions
7274
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
@@ -76,11 +78,34 @@ jobs:
7678
- name: Set up Docker
7779
uses: docker/setup-docker-action@v4
7880

81+
- name: Create unique cache key from the year and week (YYYY-WW)
82+
id: cache_suffix
83+
run: echo "week=$(date -u +%G-%V)" >> "$GITHUB_OUTPUT"
84+
85+
# Restore cache if available. GitHub automatically evicts cache entries that have not been
86+
# accessed for over 7 days. We rotate the cache key weekly; if no cache exists for the
87+
# current week, a cache from a previous week (via the prefix restore key) will be restored
88+
# and then saved under the current week's key at the end of the job. This effectively limits
89+
# the cache to at most two weeks of cache data.
90+
- name: Restore ccache cache
91+
uses: actions/cache@v5
92+
with:
93+
path: ${{ env.CCACHE_HOST_DIR }}
94+
key: ccache-${{ matrix.config.name }}-${{ steps.cache_suffix.outputs.week }}
95+
restore-keys: |
96+
ccache-${{ matrix.config.name }}-
97+
7998
- name: Build Tool-Solutions PyTorch
8099
working-directory: ${{ github.workspace }}/Tool-Solutions/ML-Frameworks/pytorch-aarch64
81100
run: ${{ github.workspace }}/Tool-Solutions/ML-Frameworks/pytorch-aarch64/build.sh
82101
env:
83102
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
103+
USE_CCACHE: 1
104+
CCACHE_HOST_DIR: ${{ env.CCACHE_HOST_DIR }}
105+
CCACHE_MAXSIZE: 2G
106+
107+
- name: Report ccache disk usage
108+
run: du -sh "${{ env.CCACHE_HOST_DIR }}" || true
84109

85110
- name: Save image as a artifact
86111
run: docker save toolsolutions-pytorch:latest -o toolsolutions-pytorch-image-${{ matrix.config.name }}.tar

ML-Frameworks/pytorch-aarch64/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ where `YY` is the year, and `MM` the month of the increment.
88
## [unreleased]
99

1010
### Added
11+
- Adds PyTorch [PR #170600](https://github.com/pytorch/pytorch/pull/170600), to patch incremental build support.
12+
- Adds PyTorch [PR #170062](https://github.com/pytorch/pytorch/pull/170062), to add ccache support to ACL/OpenBLAS and manywheel build script.
1113

1214
### Changed
1315

ML-Frameworks/pytorch-aarch64/build-wheel.sh

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,40 @@ UTILS=/utils
5555
COMMON_UTILS=/common_utils
5656
PYTORCH_FINAL_PACKAGE_DIR=/artifacts
5757

58+
# Enable ccache support by default; disable via env var or command line flag
59+
# NOTE: The intention is to have a project-specific cache directory that we cache build artefacts
60+
# inside and can be easily wiped. These build artefacts are specific to the manylinux builder
61+
# container (and thus compilers) that we use to build the torch wheel. As such, you may not want
62+
# to populate the global ccache cache with them. However, if you wish to do so, simply set
63+
# CCACHE_HOST_DIR to that directory.
64+
USE_CCACHE=${USE_CCACHE:-1}
65+
if [[ "$*" == *--enable-ccache* ]]; then USE_CCACHE=1; fi
66+
if [[ "$*" == *--disable-ccache* ]]; then USE_CCACHE=0; fi
67+
CCACHE_HOST_DIR=${CCACHE_HOST_DIR:-"${PWD}/.ccache"}
68+
CCACHE_ROOT=/.ccache
69+
CCACHE_MAXSIZE=${CCACHE_MAXSIZE:-}
70+
71+
# If the user wants to use ccache for build caching
72+
ccache_args=(-e USE_CCACHE="${USE_CCACHE}")
73+
if [ "${USE_CCACHE}" == "1" ]; then
74+
echo "Using ccache for build caching"
75+
if [ -d "${CCACHE_HOST_DIR}" ]; then
76+
echo "ccache directory exists at ${CCACHE_HOST_DIR}"
77+
else
78+
echo "Creating ccache directory at ${CCACHE_HOST_DIR}"
79+
mkdir -p "${CCACHE_HOST_DIR}"
80+
fi
81+
82+
# ccache-specific docker run args
83+
ccache_args+=(
84+
-e CCACHE_DIR="${CCACHE_ROOT}"
85+
-v "${CCACHE_HOST_DIR}:${CCACHE_ROOT}"
86+
)
87+
if [ -n "${CCACHE_MAXSIZE}" ]; then
88+
ccache_args+=(-e CCACHE_MAXSIZE="${CCACHE_MAXSIZE}")
89+
fi
90+
fi
91+
5892
# Want a CPU build
5993
DESIRED_CUDA=cpu
6094
GPU_ARCH_TYPE=cpu-aarch64
@@ -89,13 +123,26 @@ if ! docker container inspect $TORCH_BUILD_CONTAINER >/dev/null 2>&1 ; then
89123
-e SKIP_ALL_TESTS=1 \
90124
-e OPENSSL_ROOT_DIR="${OPENSSL_HOST_DIR}" \
91125
-e CMAKE_INCLUDE_PATH="${OPENSSL_HOST_DIR}/include" \
126+
"${ccache_args[@]}" \
92127
-v "${PYTORCH_HOST_DIR}:${PYTORCH_ROOT}" \
93128
-v "${PYTORCH_FINAL_PACKAGE_HOST_DIR}:${PYTORCH_FINAL_PACKAGE_DIR}" \
94129
-v "${PWD}/utils:${UTILS}" \
95130
-v "${PWD}/../utils:${COMMON_UTILS}" \
96131
-w / \
97132
"${IMAGE_NAME}")
98133

134+
# Provide ccache support
135+
if [ "${USE_CCACHE}" == "1" ]; then
136+
docker exec "$TORCH_BUILD_CONTAINER" yum install -y ccache || true
137+
if [ -n "${CCACHE_MAXSIZE}" ]; then
138+
docker exec "$TORCH_BUILD_CONTAINER" ccache --max-size="$CCACHE_MAXSIZE" || true
139+
fi
140+
docker exec "$TORCH_BUILD_CONTAINER" ccache -z || true
141+
docker exec "$TORCH_BUILD_CONTAINER" ccache -o compression=true || true
142+
docker exec "$TORCH_BUILD_CONTAINER" ccache -o compression_level=6 || true
143+
docker exec "$TORCH_BUILD_CONTAINER" ccache -s || true
144+
fi
145+
99146
# Currently changes in these scripts will not be applied without a clean
100147
# build, which is not ideal for dev work. But we have to balance this with
101148
# extra time/network traffic when rebuilding many times.
@@ -131,12 +178,25 @@ build_date=$(cd $PYTORCH_HOST_DIR && git log --pretty=format:%cs -1 | tr -d '-')
131178
version=$(cat $PYTORCH_HOST_DIR/version.txt| tr -d "[:space:]" )
132179
OVERRIDE_PACKAGE_VERSION="${version%??}.dev${build_date}${TORCH_RELEASE_ID:+"+$TORCH_RELEASE_ID"}"
133180

181+
if [ "${USE_CCACHE}" == "1" ]; then
182+
echo "Zero-ing ccache stats."
183+
docker exec "$TORCH_BUILD_CONTAINER" ccache -z || true
184+
fi
185+
134186
docker exec $TORCH_BUILD_CONTAINER bash -lc "
135187
source /tmp/env &&
136188
BUILD_TEST=0 \
189+
DO_SETUP_PY_CLEAN_BEFORE_BUILD=0 \
190+
WIPE_RH_CUDA_AFTER_BUILD=0 \
137191
OVERRIDE_PACKAGE_VERSION=$OVERRIDE_PACKAGE_VERSION \
138192
bash ${PYTORCH_ROOT}/.ci/manywheel/build.sh
139193
"
140194

195+
if [ "${USE_CCACHE}" == "1" ]; then
196+
echo "Final ccache stats:"
197+
docker exec "$TORCH_BUILD_CONTAINER" ccache -s || true
198+
fi
199+
141200
# directories generated by the docker container are owned by root, so transfer ownership to user
142-
docker exec $TORCH_BUILD_CONTAINER chown -R "$(id -u)":"$(id -g)" "${PYTORCH_ROOT}" /artifacts
201+
docker exec "$TORCH_BUILD_CONTAINER" chown -R "$(id -u)":"$(id -g)" \
202+
"${PYTORCH_ROOT}" "${PYTORCH_FINAL_PACKAGE_DIR}" "${CCACHE_ROOT}"

ML-Frameworks/pytorch-aarch64/build.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ if ! [[ $* == *--use-existing-sources* ]]; then
8383
./get-source.sh
8484
fi
8585

86+
# Use ccache either via env var or command line flag
87+
USE_CCACHE=${USE_CCACHE:-1}
88+
if [[ "$*" == *--enable-ccache* ]]; then USE_CCACHE=1; fi
89+
if [[ "$*" == *--disable-ccache* ]]; then USE_CCACHE=0; fi
90+
export USE_CCACHE="${USE_CCACHE}"
91+
8692
./build-wheel.sh
8793

8894
[[ $* == *--wheel-only* ]] && exit 0

ML-Frameworks/pytorch-aarch64/get-source.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ git-shallow-clone https://github.com/pytorch/pytorch.git $PYTORCH_HASH
3333
cd pytorch
3434

3535
# https://github.com/pytorch/pytorch/pull/167829 - Refactor ACL and OpenBLAS install scripts on AArch64
36-
# Note: as part of this patch, setuptools is pinned to ~= 78.1.1 which is not affected by
37-
# CVE-2025-47273 and CVE-2024-6345
38-
apply-github-patch pytorch/pytorch 69db12b465887df96d27fe2bb93746ac334577f1
39-
apply-github-patch pytorch/pytorch 5184c373a8bc77809b6e59361e191d4e78d6a824
36+
apply-github-patch pytorch/pytorch f5e7b3ab44b14902f1e44ac138006b04bd9b7728
37+
38+
# https://github.com/pytorch/pytorch/pull/170062 - Add ccache support to ACL/OpenBLAS and manywheel
39+
# build script.
40+
apply-github-patch pytorch/pytorch 327b118078869b85d979d9f7eb1038b8a53c8a49
41+
42+
# https://github.com/pytorch/pytorch/pull/170600 - Gate deletion of clean-up steps in build_common.sh
43+
apply-github-patch pytorch/pytorch e368ec2693b8b2b8ba35d0913f1d663ba2fdc804
4044

4145
# FIXME: Temporarily disabled; to be updated in a later PR
4246
# # https://github.com/pytorch/pytorch/pull/160184 - Draft: separate reqs for manywheel build and pin

0 commit comments

Comments
 (0)