Skip to content

Commit

Permalink
Merge pull request ARM-software#289 from aditew01/aditew01/update_ope…
Browse files Browse the repository at this point in the history
…nblas
  • Loading branch information
nSircombe authored Feb 7, 2025
2 parents 05d203c + baa1776 commit 044647e
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 68 deletions.
4 changes: 3 additions & 1 deletion ML-Frameworks/pytorch-aarch64/build-wheel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ PYTORCH_HOST_DIR="${PWD}/pytorch"
ACL_HOST_DIR="${PWD}/ComputeLibrary"

PYTORCH_ROOT=/pytorch
UTILS="/utils"

if [ -f "$TORCH_BUILD_CONTAINER_ID_FILE" ]; then
TORCH_BUILD_CONTAINER=$(cat $TORCH_BUILD_CONTAINER_ID_FILE)
Expand Down Expand Up @@ -73,6 +74,7 @@ if ! docker container inspect $TORCH_BUILD_CONTAINER >/dev/null 2>&1 ; then
-v "${PYTORCH_HOST_DIR}:${PYTORCH_ROOT}" \
-v "${PYTORCH_FINAL_PACKAGE_DIR}:/artifacts" \
-v "${ACL_HOST_DIR}:/ComputeLibrary" \
-v "${PWD}/utils:${UTILS}" \
-w / \
"${IMAGE_NAME}")

Expand All @@ -85,7 +87,7 @@ if ! docker container inspect $TORCH_BUILD_CONTAINER >/dev/null 2>&1 ; then
docker exec -t $TORCH_BUILD_CONTAINER bash -c "yum install -y tbb tbb-devel"

# This must be in this if block because it cannot handle being called twice
docker exec -t $TORCH_BUILD_CONTAINER bash -c "bash $PYTORCH_ROOT/.ci/docker/common/install_openblas.sh"
docker exec -t $TORCH_BUILD_CONTAINER bash -c "bash $UTILS/install_openblas.sh"

echo "Storing torch build container id in $TORCH_BUILD_CONTAINER_ID_FILE for reuse: $TORCH_BUILD_CONTAINER"
echo $TORCH_BUILD_CONTAINER > "$TORCH_BUILD_CONTAINER_ID_FILE"
Expand Down
70 changes: 3 additions & 67 deletions ML-Frameworks/pytorch-aarch64/get-source.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# limitations under the License.
# *******************************************************************************

source utils/helper.sh

set -eux -o pipefail
PYTORCH_HASH=62ce3e6e84df516fdd5310d5095fa01251806f1d # From viable/strict
IDEEP_HASH=9873ffca18467b07f4fb6cbbd8742dc7c6588b72 # From ideep_pytorch
Expand All @@ -25,77 +27,10 @@ ACL_HASH=d9be9625ca86ebefcd171d049273d2ee295737a0 # From main
TORCH_AO_HASH=e1cb44ab84eee0a3573bb161d65c18661dc4a307 # From main
KLEIDI_AI_HASH=ef685a13cfbe8d418aa2ed34350e21e4938358b6 # From main

function git-shallow-clone {
(
repo_name=$(basename "$1" .git)
if ! cd "$repo_name" ; then
echo "$repo_name doesn't exist, so we are making"
mkdir "$repo_name"
cd "$repo_name"
git init
git remote add origin $1
fi
git fetch --recurse-submodules=no origin $2
# We do a force checkout + clean to overwrite previous patches
git checkout -f $2
git clean -fd
)
}

function apply-github-patch {
# Apply a specific commit from a specific GitHub PR
# $1 is the repo url, $2 is the PR number, and $3 is commit hash
set -u

# Look in the PR first
curl --silent -L $1/pull/$2/commits/$3.patch -o $3.patch

# If the PR has been updated, the commit may no longer be there and the .patch will be empty.
# Look in the full repo instead.
# If it can't be found, this time curl will error
if [[ ! -s $3.patch ]]; then
>&2 echo "Commit $3 not found in $1/pull/$2. Checking the full repository..."
curl --silent --fail -L $1/commit/$3.patch -o $3.patch
fi

# Apply the patch and tidy up.
patch -p1 < $3.patch
rm $3.patch
return 0
}

function apply-gerrit-patch {
# $1 must be the url to a specific patch set
# We get the repo by removing /c and chopping off the change number
# e.g. https://review.mlplatform.org/c/ml/ComputeLibrary/+/12818/1 -> https://review.mlplatform.org/ml/ComputeLibrary/
repo_url=$(echo "$1" | sed 's#/c/#/#' | cut -d'+' -f1)
# e.g. refs/changes/18/12818/1 Note that where the middle number is the last 2 digits of the patch number
refname=$(echo "$1" | awk -F'/' '{print "refs/changes/" substr($(NF-1),length($(NF-1))-1,2) "/" $(NF-1) "/" $(NF)}')
git fetch $repo_url $refname && git cherry-pick --no-commit FETCH_HEAD
}

function setup_submodule() {
local original_dir=$(pwd)
rm -rf "$2"
git clone $1 $2
cd $2
git checkout $3
cd $original_dir
}

function reset_submodule() {
if [ -d "$1" ]; then
rm -rf "$1"
fi
}

git-shallow-clone https://github.com/pytorch/pytorch.git $PYTORCH_HASH
(
cd pytorch

# Bump OpenBLAS version. Note that install_openblas.sh has to be rerun in the PyTorch builder Docker container
sed -i -e 's/v0.3.25/v0.3.28/g' .ci/docker/common/install_openblas.sh

apply-github-patch https://github.com/pytorch/pytorch 143190 f424c67660f45bfeaceb9bebfafc7e22638746c4 # Enable AArch64 CI scripts to be used for local dev

apply-github-patch https://github.com/pytorch/pytorch 145486 bd314eef0cb371607e714b3519f5564938490f4a # feat: add SVE dispatch for non-FBGEMM qembeddingbag (fixes illigal instruction failures on N1)
Expand All @@ -111,6 +46,7 @@ git-shallow-clone https://github.com/pytorch/pytorch.git $PYTORCH_HASH
apply-github-patch https://github.com/pytorch/pytorch 140159 8d3404ec5972528f606fe605887ad2254a174fbc # cpu: aarch64: enable gemm-bf16f32
apply-github-patch https://github.com/pytorch/pytorch 140159 ab4c191ef0de1e4eced6b4dd7b6e387f57034ad9 # cpu: aarch64: enable gemm-bf16f32
apply-github-patch https://github.com/pytorch/pytorch 140159 879ca72d54559a388db315eed40803d2f1c827b7 # cpu: aarch64: enable gemm-bf16f32
apply-github-patch https://github.com/pytorch/pytorch 140159 150f5d92fa79a57a580ac000f667d05787b650b3 # cpu: aarch64: enable gemm-bf16f32
apply-github-patch https://github.com/pytorch/pytorch 145942 3d05899222da2b93ed3d4c88c382d318e68eeec6 # Enable fast qlinear_dynamic path for AArch64 through Arm Compute Library directly
apply-github-patch https://github.com/pytorch/pytorch 141127 3f2fad0b4774126f228597ba03b68a472fc433cc # Enables static quantization for aarch64
apply-github-patch https://github.com/pytorch/pytorch 143666 8e5134e9c22cdb6150e425bee43015998ae55c59 # Extend Vec backend with SVE BF16
Expand Down
82 changes: 82 additions & 0 deletions ML-Frameworks/pytorch-aarch64/utils/helper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash

# *******************************************************************************
# Copyright 2025 Arm Limited and affiliates.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# *******************************************************************************

function git-shallow-clone {
(
repo_name=$(basename "$1" .git)
if ! cd "$repo_name" ; then
echo "$repo_name doesn't exist, so we are making"
mkdir "$repo_name"
cd "$repo_name"
git init
git remote add origin $1
fi
git fetch --recurse-submodules=no origin $2
# We do a force checkout + clean to overwrite previous patches
git checkout -f $2
git clean -fd
)
}

function apply-github-patch {
# Apply a specific commit from a specific GitHub PR
# $1 is the repo url, $2 is the PR number, and $3 is commit hash
set -u

# Look in the PR first
curl --silent -L $1/pull/$2/commits/$3.patch -o $3.patch

# If the PR has been updated, the commit may no longer be there and the .patch will be empty.
# Look in the full repo instead.
# If it can't be found, this time curl will error
if [[ ! -s $3.patch ]]; then
>&2 echo "Commit $3 not found in $1/pull/$2. Checking the full repository..."
curl --silent --fail -L $1/commit/$3.patch -o $3.patch
fi

# Apply the patch and tidy up.
patch -p1 < $3.patch
rm $3.patch
return 0
}

function apply-gerrit-patch {
# $1 must be the url to a specific patch set
# We get the repo by removing /c and chopping off the change number
# e.g. https://review.mlplatform.org/c/ml/ComputeLibrary/+/12818/1 -> https://review.mlplatform.org/ml/ComputeLibrary/
repo_url=$(echo "$1" | sed 's#/c/#/#' | cut -d'+' -f1)
# e.g. refs/changes/18/12818/1 Note that where the middle number is the last 2 digits of the patch number
refname=$(echo "$1" | awk -F'/' '{print "refs/changes/" substr($(NF-1),length($(NF-1))-1,2) "/" $(NF-1) "/" $(NF)}')
git fetch $repo_url $refname && git cherry-pick --no-commit FETCH_HEAD
}

function setup_submodule() {
local original_dir=$(pwd)
rm -rf "$2"
git clone $1 $2
cd $2
git checkout $3
cd $original_dir
}

function reset_submodule() {
if [ -d "$1" ]; then
rm -rf "$1"
fi
}
49 changes: 49 additions & 0 deletions ML-Frameworks/pytorch-aarch64/utils/install_openblas.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# *******************************************************************************
# Copyright 2025 Arm Limited and affiliates.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# *******************************************************************************

# This script is based on the upstream PyTorch repository.
# Reference: pytorch/pytorch
# https://github.com/pytorch/pytorch/blob/main/.ci/docker/common/install_openblas.sh
# TODO: discard this and use `pytorch/.ci/docker/common/install_openblas.sh` to build OpenBLAS after version upgrade

source /utils/helper.sh

set -ex
OPENBLAS_CHECKOUT_DIR="OpenBLAS"

cd /
git clone https://github.com/OpenMathLib/OpenBLAS.git -b develop --depth 1
(
cd $OPENBLAS_CHECKOUT_DIR
apply-github-patch https://github.com/OpenMathLib/OpenBLAS/ 5108 4379a6fbe37038082c657bba5be5c67331a0bd0b
apply-github-patch https://github.com/OpenMathLib/OpenBLAS/ 5108 c748e6a33871f0dfa3bf6569c88a676c9a387411
cd /
)

OPENBLAS_BUILD_FLAGS="
NUM_THREADS=128
USE_OPENMP=1
NO_SHARED=0
DYNAMIC_ARCH=1
TARGET=ARMV8
CFLAGS=-O3
BUILD_BFLOAT16=1
"
make -j$(nproc) ${OPENBLAS_BUILD_FLAGS} -C ${OPENBLAS_CHECKOUT_DIR}
make -j$(nproc) ${OPENBLAS_BUILD_FLAGS} install -C ${OPENBLAS_CHECKOUT_DIR}

0 comments on commit 044647e

Please # to comment.