Skip to content

Commit 1c203ca

Browse files
authored
Fix TritonSan build error with triton version e44bd1c (#359)
This PR resolves the TritonSan build error. The build script was broken due to several issues: - Removal of the Triton submodule: We now use triton-hash.txt to record the Triton version. The previous logic for locating Triton submodule was invalidated by this change. - LLVM-related issue: In the corresponding LLVM version, there is a known bug that causes [a compilation error when building OpenMP](llvm/llvm-project@62ff9ac). The build script now cherry-picks this patch to address the problem. This PR fixes all these issues. The build script will now automatically check out Triton and place it alongside triton-shared, LLVM, and the Python virtual environment. There are no changes to the usage of the build script or the TritonSan driver script.
1 parent f8c0e39 commit 1c203ca

6 files changed

Lines changed: 76 additions & 37 deletions

File tree

triton-san/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@ Usage: triton-san <sanitizer type> <original command used to launch the triton p
6666
Example: triton-san asan python ./my_triton_program.py
6767
```
6868

69-
**Note: before running TritonSan, please add the following import to the Triton program to specify the use of the CPU backend, which ensures all Triton kernels run on the CPU. The sanitizers require CPU backend in order to work.**
69+
**Note: before running TritonSan, please add the following import to the Triton program to specify the use of the CPU backend, which ensures all Triton kernels run on the CPU. The sanitizers require CPU backend in order to work. In addition, all desired GPU tensors in the Triton program need to be set to CPU**
7070

7171
```python
7272
from triton.backends.triton_shared.driver import CPUDriver
7373
triton.runtime.driver.set_active(CPUDriver())
74+
...
75+
76+
#output = torch.empty((size, )).to("gpu")
77+
output = torch.empty((size, )).to("cpu")
7478
```
7579

7680
## Example

triton-san/build.sh

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function check_required_files() {
1111
}
1212

1313
PARENT_FOLDER="$(realpath "$(dirname "$0")")"
14+
TRITON_SHARED_PATH="$(realpath "${PARENT_FOLDER}/..")"
1415
ROOT="$(realpath "${PARENT_FOLDER}/../..")"
1516
SCRIPT_FOLDER="$(realpath "${PARENT_FOLDER}/script")"
1617
VENV_PATH="${ROOT}/venv"
@@ -23,9 +24,42 @@ TRITON_SAN_INSTALL_DIR="${ROOT}"
2324
# include utility functions
2425
source "${SCRIPT_FOLDER}/utility.inc"
2526

27+
echo "================= Check out triton ================"
28+
TRITON_HASH_FILE="${TRITON_SHARED_PATH}/triton-hash.txt"
29+
if [ ! -e "${TRITON_HASH_FILE}" ]; then
30+
print_error_and_exit "${TRITON_HASH_FILE} does not exist."
31+
fi
32+
33+
EXPECT_HEAD_COMMIT=$(cat "${TRITON_HASH_FILE}")
34+
if [ -e "${TRITON_PATH}" ]; then
35+
warning_msg=("The path ${TRITON_PATH} already exists.")
36+
print_warning "${warning_msg[@]}"
37+
pushd "${TRITON_PATH}" > /dev/null
38+
HEAD_COMMIT=$(git rev-parse HEAD)
39+
if [ "${HEAD_COMMIT}" != "${EXPECT_HEAD_COMMIT}" ]; then
40+
error_msg=("The head commit of the existing Triton repository does not match the expected commit hash."
41+
"HEAD: ${HEAD_COMMIT}, EXPECTED: ${EXPECT_HEAD_COMMIT}")
42+
print_error_and_exit "${error_msg[@]}"
43+
fi
44+
EXIST_TRITON_BUILD="${TRITON_PATH}/build"
45+
if [ -e "${EXIST_TRITON_BUILD}" ]; then
46+
rm -rf "${TRITON_PATH}/build"
47+
echo "Remove ${TRITON_PATH}/build"
48+
fi
49+
popd > /dev/null
50+
else
51+
mkdir -p "${TRITON_PATH}"
52+
git clone https://github.com/triton-lang/triton.git "${TRITON_PATH}"
53+
pushd "${TRITON_PATH}" > /dev/null
54+
cd "${TRITON_PATH}" && git checkout ${EXPECT_HEAD_COMMIT}
55+
popd > /dev/null
56+
fi
57+
58+
echo -e "\n\n\n"
59+
2660
echo "================= Setup Python virtual environment ================"
2761
if [ ! -e "${VENV_PATH}" ]; then
28-
"${SCRIPT_FOLDER}/setup_prerequisite.sh" "${VENV_PATH}"
62+
"${SCRIPT_FOLDER}/setup_prerequisite.sh" "${TRITON_PATH}" "${VENV_PATH}"
2963
else
3064
required_file=(
3165
"${VENV_PATH}/bin/activate"
@@ -47,7 +81,7 @@ echo -e "\n\n\n"
4781
echo "=========================== Build LLVM ============================"
4882
if [ ! -e "${LLVM_PATH}" ]; then
4983
mkdir -p "${LLVM_PATH}"
50-
"${SCRIPT_FOLDER}/build_llvm.sh" "${LLVM_PATH}"
84+
"${SCRIPT_FOLDER}/build_llvm.sh" "${TRITON_PATH}" "${LLVM_PATH}"
5185
else
5286
required_file=(
5387
"${LLVM_PATH}/llvm-install/bin"
@@ -64,14 +98,9 @@ fi
6498
echo -e "\n\n\n"
6599

66100
echo "================== Build trion-shared and triton =================="
67-
if [ -e "${TRITON_PATH}" ]; then
68-
warning_msg=("The path ${TRITON_PATH} already exists and will be overwritten.")
69-
print_warning "${warning_msg[@]}"
70-
rm -rf "${TRITON_PATH}"
71-
fi
72-
mkdir -p "${TRITON_PATH}"
73-
"${SCRIPT_FOLDER}/build_triton_shared_with_triton_san.sh" "${LLVM_BUILD_PATH}" "${VENV_PATH}" "${TRITON_PATH}"
101+
export PATH="${LLVM_PATH}/llvm-install/bin:${PATH}"
102+
"${SCRIPT_FOLDER}/build_triton_shared_with_triton_san.sh" "${LLVM_BUILD_PATH}" "${VENV_PATH}" "${TRITON_PATH}" "${TRITON_SHARED_PATH}"
74103
echo -e "\n\n\n"
75104

76105
echo "======================== Install triton-san ======================="
77-
"${SCRIPT_FOLDER}/install_triton_san.sh" "${TRITON_SAN_INSTALL_DIR}" "${LLVM_INSTALL_DIR}" "${VENV_PATH}" "${TRITON_PATH}"
106+
"${SCRIPT_FOLDER}/install_triton_san.sh" "${LLVM_INSTALL_DIR}" "${VENV_PATH}" "${TRITON_PATH}" "${TRITON_SAN_INSTALL_DIR}"

triton-san/script/build_llvm.sh

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
set -e
77

88
if [ "$#" -lt 1 ]; then
9-
echo "Usage: $0 <desired path for LLVM installation>"
9+
echo "Usage: $0 <path to Triton source directory> <desired path for LLVM installation>"
1010
exit 1
1111
fi
1212

1313
PARENT_FOLDER="$(realpath "$(dirname "$0")")"
14-
TRITON_SHARED_PATH="$(realpath "${PARENT_FOLDER}/../..")"
15-
LLVM_PATH="$(realpath "$1")"
14+
TRITON_PATH="$(realpath "$1")"
15+
LLVM_PATH="$(realpath "$2")"
1616

1717
# include utility functions
1818
source "${PARENT_FOLDER}/utility.inc"
1919

2020
print_info "Installing LLVM to path: ${LLVM_PATH}."
2121
cd $LLVM_PATH
2222

23-
LLVM_HASH_FILE="${TRITON_SHARED_PATH}/triton/cmake/llvm-hash.txt"
23+
LLVM_HASH_FILE="${TRITON_PATH}/cmake/llvm-hash.txt"
2424
if [ ! -e "${LLVM_HASH_FILE}" ]; then
2525
print_error_and_exit "${LLVM_HASH_FILE} does not exist."
2626
fi
@@ -32,7 +32,7 @@ LLVM_SOURCE="${LLVM_SOURCE_DIR}/llvm"
3232

3333
# compiler-rt and clang are the sanitizer-specific LLVM projects
3434
# openmp is used for parallelizing the triton grid for ThreadSanitizer (TSan)
35-
LLVM_PROJECTS="clang;compiler-rt;openmp;mlir"
35+
LLVM_PROJECTS="clang;compiler-rt;openmp;mlir;lld"
3636

3737
# these are the targets supported by the Triton language
3838
# Triton's build script for LLVM uses these exact targets
@@ -49,6 +49,17 @@ LLVM_HASH=$(cat "${LLVM_HASH_FILE}")
4949
cd "${LLVM_SOURCE_DIR}"
5050
git checkout ${LLVM_HASH}
5151

52+
# Cherry-pick the patch to resolve the OpenMP build error. An incorrect setting in openmp/runtime/src/CMakeLists.txt
53+
# causes the generated omp-tools.h to be placed in Clang's include directory instead of OpenMP's, leading to a
54+
# compilation failure when locating omp-tools.h.
55+
# More details are available at this link: https://github.com/llvm/llvm-project/commit/62ff9ac4c68f48c089528105259c68943ab176de
56+
57+
# TODO: remove this once the LLVM hash for the next LLVM->Triton merge beyond this commit is confirmed.
58+
if ! git merge-base --is-ancestor 62ff9ac HEAD; then
59+
echo "cherry pick commit 62ff9ac to avoid OpenMP build failure"
60+
git cherry-pick 62ff9ac
61+
fi
62+
5263
export CXXFLAGS="-Wno-unused-command-line-argument $CXXFLAGS"
5364
export CFLAGS="-Wno-unused-command-line-argument $CFLAGS"
5465

@@ -64,4 +75,4 @@ cmake -GNinja -DCMAKE_BUILD_TYPE=Release \
6475
ninja -j$(nproc --all) -l$(nproc --all) || exit -1
6576
ninja -j$(nproc --all) -l$(nproc --all) install
6677

67-
print_info "LLVM installation succeeded."
78+
print_info "LLVM installation succeeded."

triton-san/script/build_triton_shared_with_triton_san.sh

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
set -e
55

66
if [ "$#" -lt 3 ]; then
7-
echo "Usage: $0 <path to LLVM build directory> <path to Python venv> <path to Triton source directory>"
7+
echo "Usage: $0 <path to LLVM build directory> <path to Python venv> <path to Triton source directory> <path to triton-shared source directory>"
88
exit 1
99
fi
1010

1111
PARENT_FOLDER="$(realpath "$(dirname "$0")")"
12-
TRITON_SHARED_PATH="$(realpath "${PARENT_FOLDER}/../..")"
1312
LLVM_BUILD_PATH="$(realpath "$1")"
1413
VENV_PATH="$(realpath "$2")"
1514
TRITON_PATH="$(realpath "$3")"
15+
TRITON_SHARED_PATH="$(realpath "$4")"
1616

1717
# include utility functions
1818
source "${PARENT_FOLDER}/utility.inc"
@@ -46,15 +46,10 @@ fi
4646
mkdir -p "${TRITON_HOME}"
4747

4848
# build triton-shared and triton with the custom LLVM
49-
TRITON_HASH_FILE="${TRITON_SHARED_PATH}/triton-hash.txt"
50-
if [ ! -e "${TRITON_HASH_FILE}" ]; then
51-
print_error_and_exit "${TRITON_HASH_FILE} does not exist."
52-
fi
53-
54-
git clone https://github.com/triton-lang/triton.git "${TRITON_PATH}"
55-
cd "${TRITON_PATH}" && git checkout $(cat "${TRITON_HASH_FILE}")
49+
pushd "${TRITON_PATH}" > /dev/null
5650
export TRITON_PLUGIN_DIRS="${TRITON_SHARED_PATH}"
5751
export LLVM_BUILD_DIR="${LLVM_BUILD_PATH}"
5852
LLVM_INCLUDE_DIRS="${LLVM_BUILD_DIR}/include" LLVM_LIBRARY_DIR="${LLVM_BUILD_DIR}/lib" LLVM_SYSPATH="${LLVM_BUILD_DIR}" TRITON_BUILD_WITH_CLANG_LLD=true TRITON_BUILD_WITH_CCACHE=false python3 -m pip install --no-build-isolation -vvv '.[tests]'
53+
popd > /dev/null
5954

60-
print_info "triton-shared and triton build completed."
55+
print_info "triton-shared and triton build completed."

triton-san/script/install_triton_san.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
set -e
55

66
if [ "$#" -lt 4 ]; then
7-
echo "Usage: $0 <desired path for triton-san installation> <path to LLVM install directory> <path to Python venv> <path to Triton source directory>"
7+
echo "Usage: $0 <path to LLVM install directory> <path to Python venv> <path to Triton source directory> <desired path for triton-san installation>"
88
exit 1
99
fi
1010

1111
PARENT_FOLDER="$(realpath "$(dirname "$0")")"
1212
TRITON_SAN_PATH="$(realpath "${PARENT_FOLDER}/..")"
13-
TRITON_SAN_INSTALL_DIR="$(realpath "$1")"
14-
LLVM_INSTALL_DIR="$(realpath "$2")"
15-
VENV_PATH="$(realpath "$3")"
16-
TRITON_PATH="$(realpath "$4")"
13+
LLVM_INSTALL_DIR="$(realpath "$1")"
14+
VENV_PATH="$(realpath "$2")"
15+
TRITON_PATH="$(realpath "$3")"
16+
TRITON_SAN_INSTALL_DIR="$(realpath "$4")"
1717

1818
# include utility functions
1919
source "${PARENT_FOLDER}/utility.inc"
@@ -57,4 +57,4 @@ cp -r "${TRITON_SAN_PATH}/examples" "${TRITON_SAN_INSTALL_ROOT}/examples"
5757
info_message=("triton-san has been installed into ${TRITON_SAN_INSTALL_ROOT} successfully."
5858
"To optionally enable global access, add triton-san to your PATH environment variable using:"
5959
'export PATH="${PATH}:'"${TRITON_SAN_INSTALL_ROOT}\"")
60-
print_info "${info_message[@]}"
60+
print_info "${info_message[@]}"

triton-san/script/setup_prerequisite.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
set -e
55

66
if [ "$#" -lt 1 ]; then
7-
echo "Usage: $0 <desired path to venv>"
7+
echo "Usage: $0 <path to Triton source directory> <desired path to venv>"
88
exit 1
99
fi
1010

1111
PARENT_FOLDER="$(realpath "$(dirname "$0")")"
1212
TRITON_SHARED_PATH="$(realpath "${PARENT_FOLDER}/../..")"
13-
TRITON_PATH="${TRITON_SHARED_PATH}/triton"
14-
VENV_PATH="$(realpath "$1")"
13+
TRITON_PATH="$(realpath "$1")"
14+
VENV_PATH="$(realpath "$2")"
1515

1616
# include utility functions
1717
source "${PARENT_FOLDER}/utility.inc"
@@ -36,4 +36,4 @@ python3 -m pip install pytest-xdist torch
3636
# echo to user to start virtual environment
3737
info_message=("Please start the virtual environment: source \"${VENV_PATH}/bin/activate\""
3838
"This export is recommended for subsequent triton-shared builds: export TRITON_PLUGIN_DIRS=\"${TRITON_SHARED_PATH}/triton_shared\"")
39-
print_info "${info_message[@]}"
39+
print_info "${info_message[@]}"

0 commit comments

Comments
 (0)