Skip to content

Commit 0385e4f

Browse files
committed
refactor: remove nntc ppl so
- if cross_toolchians not exist, download it - if ppl so not exist, compile it Change-Id: I50e233ba2c7fbfd22331cfb470e62883446e7042
1 parent 64e646b commit 0385e4f

32 files changed

Lines changed: 53 additions & 145 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ tpulang_test_*/
2525
bmodel_fbs.py
2626
.ipynb_checkpoints/
2727
cross_toolchains/
28+
third_party/nntoolchain/ppl/
2829
dist/
2930
!/python/tools/soc_infer/debugger/bmodel_fbs.py
3031

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,18 @@ Create and enter the container:
8383
docker run --privileged --name tpu-mlir -v $PWD:/workspace -it sophgo/tpuc_dev:latest
8484
```
8585

86-
### 2a. Install the prebuilt wheel (recommended)
86+
### 2a. Install the prebuilt wheel
8787

8888
> Requires Python ≥ 3.10 on Ubuntu 22.04 (already satisfied inside the Docker image).
8989
9090
```shell
9191
pip install tpu_mlir
9292
```
9393

94-
### 2b. Build from source
94+
### 2b. Build from source (recommended)
9595

9696
```shell
97+
# git clone with --depth 1
9798
cd /workspace/tpu-mlir
9899
pip install -r requirements.txt
99100
source ./envsetup.sh

README_cn.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,18 @@ docker load -i tpuc_dev_v3.4.tar.gz
8383
docker run --privileged --name tpu-mlir -v $PWD:/workspace -it sophgo/tpuc_dev:latest
8484
```
8585

86-
### 2a. 安装预编译 wheel(推荐)
86+
### 2a. 安装预编译 wheel
8787

8888
> 要求 Python ≥ 3.10,Ubuntu 22.04(推荐直接使用上述 Docker 镜像)。
8989
9090
```shell
9191
pip install tpu_mlir
9292
```
9393

94-
### 2b. 从源码编译
94+
### 2b. 从源码编译 (推荐)
9595

9696
```shell
97+
# git clone with --depth 1
9798
cd /workspace/tpu-mlir
9899
pip install -r requirements.txt
99100
source ./envsetup.sh

build.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ BUILD_TYPE=""
2828
CXX_FLAGS="-O2"
2929
USE_CUDA="OFF"
3030
ENABLE_COVERAGE_FLAG="OFF"
31+
PPL_BUILD_TYPE="RELEASE"
3132

3233
if [ -n "$1" ]; then
3334
if [ "$1" = "DEBUG" ]; then
3435
BUILD_TYPE="Debug"
3536
CXX_FLAGS="-ggdb"
37+
PPL_BUILD_TYPE="DEBUG"
3638
elif [ "$1" != "RELEASE" ]; then
3739
echo "Invalid build mode: $1"
3840
usage
@@ -58,7 +60,6 @@ if [ "${ENABLE_COVERAGE}" = "True" ]; then
5860
fi
5961

6062
# prepare install/build dir
61-
rm -rf "${INSTALL_PATH}"
6263
cmake -G Ninja \
6364
-B "${BUILD_PATH}" \
6465
-DCMAKE_C_COMPILER=clang \
@@ -77,9 +78,8 @@ cmake --build "$BUILD_PATH" --target install -j${cpu_num}
7778

7879
cmake --build "$BUILD_PATH" --target passes_json_files builder_python install_passes_files
7980

80-
# build ppl code
81-
# bash lib/PplBackend/build.sh DEBUG
82-
bash lib/PplBackend/build.sh
81+
# build ppl code (after main install so INSTALL_PATH/lib/ is ready)
82+
bash lib/PplBackend/build.sh ${PPL_BUILD_TYPE}
8383

8484
# Clean up some files for release build
8585
if [ "$1" != "DEBUG" ]; then
Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,12 @@ function download_loong() {
4040
download_toolchain $tool_name $tool_addr $tool_file
4141
}
4242

43-
CROSS_TOOLCHAINS=${1:-$PROJECT_ROOT/../bm_prebuilt_toolchains}
44-
loongarch_host_toolchain=${2}
45-
46-
CURRENT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
47-
48-
if [ -z "$CROSS_TOOLCHAINS" ]; then
49-
export CROSS_TOOLCHAINS=$CROSS_TOOLCHAINS
50-
fi
51-
echo "export CROSS_TOOLCHAINS=$CROSS_TOOLCHAINS"
43+
CROSS_TOOLCHAINS=${1:-$PROJECT_ROOT/cross_toolchains}
5244

5345
mkdir -p $CROSS_TOOLCHAINS
54-
pushd $CROSS_TOOLCHAINS >>/dev/null
55-
56-
if [ x${CHIP} == "xbm1688" ]; then
57-
download_riscv_xuantie900
58-
if [ x${DEV_MODE} == "xsoc" ]; then
59-
download_gcc_arm
60-
fi
61-
elif [ x${CHIP} == "xbm1684x" ]; then
62-
if [ "x${loongarch_host_toolchain}" == "xloongarch64" ]; then
63-
download_loong
64-
else
65-
download_gcc_arm
66-
fi
67-
if [ x${DEV_MODE} == "xsoc" ]; then
68-
download_gcc_linaro
69-
fi
70-
elif [ x${CHIP} == "xbm1690" ]; then
71-
download_riscv_xuantie900
72-
else
73-
return 1
74-
fi
75-
popd >>/dev/null
46+
pushd $CROSS_TOOLCHAINS
47+
download_loong
48+
download_gcc_arm
49+
download_gcc_linaro
50+
download_riscv_xuantie900
51+
popd

envsetup.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export PATH=$PROJECT_ROOT/third_party/customlayer/python:$PATH
3232
export PATH=$PROJECT_ROOT/python/tools/train/test:$PATH
3333

3434
# ppl compiler path
35-
export PPL_PROJECT_ROOT=$INSTALL_PATH/ppl
35+
export PPL_PROJECT_ROOT=$PROJECT_ROOT/third_party/ppl
3636
export PPL_BUILD_PATH=$PPL_PROJECT_ROOT/build
3737
export PPL_INSTALL_PATH=$PPL_PROJECT_ROOT/install
3838
export PPL_RUNTIME_PATH=$PPL_PROJECT_ROOT/deps
@@ -91,8 +91,17 @@ function use_chip_cmodel() {
9191
}
9292

9393
# only used to build libatomic_exec_aarch64.so for soc_infer
94-
export CROSS_TOOLCHAINS=$PROJECT_ROOT/../bm_prebuilt_toolchains
94+
# fallback to local cross_toolchains if bm_prebuilt_toolchains is not available
95+
if [ -d "$PROJECT_ROOT/../bm_prebuilt_toolchains" ]; then
96+
export CROSS_TOOLCHAINS=$PROJECT_ROOT/../bm_prebuilt_toolchains
97+
elif [ -d "$PROJECT_ROOT/cross_toolchains" ]; then
98+
export CROSS_TOOLCHAINS=$PROJECT_ROOT/cross_toolchains
99+
else
100+
export CROSS_TOOLCHAINS=$PROJECT_ROOT/cross_toolchains
101+
$PROJECT_ROOT/download_toolchains.sh $CROSS_TOOLCHAINS
102+
fi
95103
export LIBSOPHON_ROOT=$PROJECT_ROOT/../libsophon
104+
96105
function rebuild_atomic_exec_alone() {
97106
export ATOMIC_EXEC_ALONE=1
98107
if [ -z "$USE_CROSS_TOOLCHAINS" ]; then

lib/PplBackend/.md5

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
__BUILD_MODE__ RELEASE
2-
CMakeLists.txt 514d48f2ba804cde1313adda0a7ccfe9
3-
Dynkernel.cmake c873cf00c2a1603bff54415dbf294dbb
1+
CMakeLists.txt c2a811021063e1a2a85c965127e56535
2+
Dynkernel.cmake e06b63a6111d1848334e3f63dc95e33e
43
include/helper.h cad814a6c86baa99b65e622ffbe306b8
54
include/local_base.h 61192f8ca0ab0f5bd7f71d69e481dd15
65
include/ppl_dyn_fw.h 2924e996c92f4bb25ca3b016d9c2697a

lib/PplBackend/CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
1111

1212
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--no-undefined")
1313
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--no-undefined")
14-
set(NNTOOLCHAIN_DIRECTORY $ENV{PROJECT_ROOT}/third_party/nntoolchain)
1514

1615
if(NOT DEFINED ENV{PPL_PROJECT_ROOT})
1716
message(FATAL_ERROR "Please set environ PPL_PROJECT_ROOT to ppl release path")
@@ -35,6 +34,7 @@ set(HOST_TOP ${PPL_TOP}/deps/common/host)
3534

3635
include_directories(${KERNEL_TOP})
3736
include_directories(${HOST_TOP}/include)
37+
include_directories($ENV{PROJECT_ROOT}/include)
3838
include_directories($ENV{TPUC_ROOT}/include)
3939
include_directories(include)
4040
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/${BUILD_DIR}/include)
@@ -49,9 +49,6 @@ if(BUILD_STATIC)
4949
add_library(ppl_host SHARED ${TILING_SRC} ${PL_SRC} ${ADDITIONAL_SRC})
5050

5151
install(TARGETS ppl_host DESTINATION lib)
52-
if(NOT DEBUG)
53-
install(FILES $<TARGET_FILE:ppl_host> DESTINATION ${NNTOOLCHAIN_DIRECTORY}/ppl)
54-
endif()
5552
else()
5653
if(DEFINED DEV_MODE)
5754
message(NOTICE "DEV_MODE: ${DEV_MODE}")
@@ -70,8 +67,5 @@ else()
7067
set(ADDITIONAL_SRC ${HOST_TOP}/src/host_utils.cpp)
7168
add_library(${DYN_HOST_NAME} SHARED ${TILING_SRC} ${PL_SRC} ${ADDITIONAL_SRC})
7269
install(TARGETS ${DYN_HOST_NAME} DESTINATION lib)
73-
if(NOT DEBUG)
74-
install(FILES $<TARGET_FILE:${DYN_HOST_NAME}> DESTINATION ${NNTOOLCHAIN_DIRECTORY}/ppl)
75-
endif()
7670
endif()
7771
endif()

lib/PplBackend/Dynkernel.cmake

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,7 @@ else() # soc pcie
3333
set(NAME_SUFFIX _${CHIP})
3434
# try download cross toolchain
3535
if(NOT DEFINED ENV{CROSS_TOOLCHAINS})
36-
message("CROSS_TOOLCHAINS was not defined, try source download_toolchain.sh")
37-
execute_process(
38-
COMMAND bash -c "CHIP=${CHIP} DEV_MODE=${DEV_MODE} source ${CMAKE_CURRENT_LIST_DIR}/download_toolchain.sh && env"
39-
RESULT_VARIABLE result
40-
OUTPUT_VARIABLE output
41-
)
42-
if(NOT result EQUAL "0")
43-
message(FATAL_ERROR "Not able to source download_toolchain.sh: ${output}")
44-
endif()
45-
string(REGEX MATCH "CROSS_TOOLCHAINS=([^\n]*)" _ ${output})
46-
set(ENV{CROSS_TOOLCHAINS} "${CMAKE_MATCH_1}")
36+
message(FATAL_ERROR "CROSS_TOOLCHAINS is not set. Source envsetup.sh first, or set the env var manually.")
4737
endif()
4838
set(RISCV_FAMLIY bm1688 bm1690 bm1690e sg2260e sg2260erv bm1684x2)
4939
if(${CHIP} IN_LIST RISCV_FAMLIY)
@@ -54,17 +44,7 @@ else() # soc pcie
5444
set(CMAKE_STRIP $ENV{CROSS_TOOLCHAINS}/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-strip)
5545
endif()
5646
if(NOT EXISTS ${CMAKE_C_COMPILER})
57-
message("CROSS_TOOLCHAINS was not defined, try source download_toolchain.sh")
58-
execute_process(
59-
COMMAND bash -c "CHIP=${CHIP} DEV_MODE=${DEV_MODE} source ${CMAKE_CURRENT_LIST_DIR}/download_toolchain.sh && env"
60-
RESULT_VARIABLE result
61-
OUTPUT_VARIABLE output
62-
)
63-
if(NOT result EQUAL "0")
64-
message(FATAL_ERROR "Not able to source download_toolchain.sh: ${output}")
65-
endif()
66-
string(REGEX MATCH "CROSS_TOOLCHAINS=([^\n]*)" _ ${output})
67-
set(ENV{CROSS_TOOLCHAINS} "${CMAKE_MATCH_1}")
47+
message(FATAL_ERROR "Cross compiler not found at ${CMAKE_C_COMPILER}. Source envsetup.sh to download toolchains, or set CROSS_TOOLCHAINS manually.")
6848
endif()
6949
endif()
7050

@@ -183,4 +163,3 @@ if(NOT DEBUG)
183163
COMMENT "Stripping symbols from $<TARGET_FILE:${SHARED_LIBRARY_OUTPUT_FILE}>"
184164
)
185165
endif()
186-
install(FILES $<TARGET_FILE:${SHARED_LIBRARY_OUTPUT_FILE}> DESTINATION ${NNTOOLCHAIN_DIRECTORY}/ppl)

lib/PplBackend/build.sh

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ clean_up() {
3737
mkdir -p "$build_dir"
3838
}
3939

40+
# Ensure the target lib directory exists
41+
mkdir -p "${INSTALL_PATH}/lib"
42+
4043
# Parallel ppl-compile over all *.pl files in ../src
4144
# Args: chip [extra ppl-compile flags...]
4245
ppl_compile_all() {
@@ -76,17 +79,13 @@ file_changed=false
7679
# headers, the CMake build description, and the build script itself.
7780
mapfile -t md5_list < <(generate_md5_list \
7881
src src_dyn include CMakeLists.txt Dynkernel.cmake)
79-
# RELEASE vs DEBUG produces different artifacts, so treat the build mode as
80-
# an input by embedding it as a sentinel line in the md5 list.
81-
md5_list=("__BUILD_MODE__ $BUILD_MODE" "${md5_list[@]}")
8282
if [ ! -f "$MD5FILE" ] || has_md5_changes "$MD5FILE" "${md5_list[@]}"; then
8383
file_changed=true
8484
fi
8585
# get latest nntoolchain version
8686
lib_changed=false
8787
NNTC_LIB_PATH=${PROJECT_ROOT}/third_party/nntoolchain/lib
8888
PPL_VER_PATH=${PROJECT_ROOT}/third_party/ppl/version
89-
VER_FILE=${PROJECT_ROOT}/third_party/nntoolchain/ppl/version
9089
LIBS=("libcmodel_bm1684x.a" "libbm1684x_kernel_module.a"
9190
"libcmodel_bm1688.a" "libbmtpulv60_kernel_module.a"
9291
"libcmodel_bm1690.a" "libbm1690_kernel_module.a"
@@ -101,13 +100,17 @@ mapfile -t libs_md5_list < <(
101100
done
102101
)
103102
# check whether the third_party/nntoolchain/lib or ppl is updated
104-
if [ ! -f "$VER_FILE" ] || has_md5_changes "$VER_FILE" "${libs_md5_list[@]}" || ! grep -Fxq -f "$PPL_VER_PATH" "$VER_FILE"; then
103+
# use a version file under the install path for cache tracking
104+
VER_DIR="${INSTALL_PATH}/share/ppl"
105+
mkdir -p "$VER_DIR"
106+
VER_FILE="${VER_DIR}/.version"
107+
if [ ! -f "$VER_FILE" ] || [ "$(head -n1 "$VER_FILE")" != "$BUILD_MODE" ] || has_md5_changes "$VER_FILE" "${libs_md5_list[@]}" || ! grep -Fxq -f "$PPL_VER_PATH" "$VER_FILE"; then
105108
lib_changed=true
106109
fi
107110
if [ "$lib_changed" = false ] && [ "$file_changed" = false ]; then
108111
exit 0
109112
fi
110-
# build third_party/nntoolchain/ppl lib
113+
# build ppl libs and install directly to INSTALL_PATH
111114
echo "rebuilding ppl..."
112115
pushd "$DIR"
113116
# wipe ppl compile cache ONCE for the whole rebuild (not per-chip)
@@ -176,8 +179,8 @@ fi
176179
if [ "$file_changed" = true ]; then
177180
printf '%s\n' "${md5_list[@]}" > "$MD5FILE"
178181
fi
179-
# VER_FILE records the nntoolchain/ppl versions that the installed RELEASE
180-
# artifacts were built against, so only update it on RELEASE builds.
182+
# VER_FILE records the nntoolchain lib versions that the installed artifacts
183+
# were built against, so only update it on RELEASE builds.
181184
if [ "$lib_changed" = true ]; then
182185
printf '%s\n' "$BUILD_MODE" > "$VER_FILE"
183186
cat "$PPL_VER_PATH" >> "$VER_FILE"

0 commit comments

Comments
 (0)