Skip to content

Commit b592e68

Browse files
committed
commit
0 parents  commit b592e68

14 files changed

+2326
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.so
2+
.ipynb_checkpoints
3+
*.o
4+
*.c
5+

BuildOnLinux.md

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
2+
## TVM Linux 安裝
3+
4+
### 安裝必要的工具和庫
5+
6+
Apache TVM 需要以下相依性:
7+
- CMake (>= 3.24.0)
8+
- LLVM(建議 >= 15)
9+
- git
10+
- C++ 編譯器至少支援 C++ 17
11+
- GCC 7.1
12+
- Clang 5.0
13+
- Apple Clang 9.3
14+
- Visual Studio 2019 (v16.7)
15+
- Python (>= 3.8)
16+
- 建議使用 Conda 建立 Python 環境
17+
18+
19+
若 cmake 編譯出現以下錯誤訊息,則需要安裝:
20+
21+
> Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
22+
23+
```
24+
sudo apt-get update
25+
sudo apt-get install zlib1g-dev
26+
```
27+
28+
管理依賴關係最簡單的方法是透過 conda,它維護一組跨平台的工具鏈,包括 LLVM。要建立這些建置依賴項的環境,輸入以下指令建立一個新的環境:
29+
30+
```
31+
# make sure to start with a fresh environment
32+
conda env remove -n tvm-build-venv
33+
# create the conda environment with build dependency
34+
conda create -n tvm-build-venv -c conda-forge \
35+
"llvmdev>=15" \
36+
"cmake>=3.24" \
37+
git \
38+
python=3.11
39+
# enter the build environment
40+
conda activate tvm-build-venv
41+
```
42+
43+
44+
### 下載 TVM
45+
下載 TVM 的 GitHub 儲存庫。
46+
47+
```
48+
git clone --recursive https://github.com/apache/tvm tvm
49+
```
50+
51+
52+
建立一個建置目錄,將cmake/config.cmake複製到該目錄。
53+
54+
```
55+
cd tvm
56+
rm -rf build && mkdir build && cd build
57+
# Specify the build configuration via CMake options
58+
cp ../cmake/config.cmake .
59+
```
60+
61+
修改 cmake 編譯的設定參數,可依據需求 ON/OFF。
62+
63+
```
64+
# controls default compilation flags (Candidates: Release, Debug, RelWithDebInfo)
65+
echo "set(CMAKE_BUILD_TYPE RelWithDebInfo)" >> config.cmake
66+
67+
# LLVM is a must dependency for compiler end
68+
echo "set(USE_LLVM \"llvm-config --ignore-libllvm --link-static\")" >> config.cmake
69+
echo "set(HIDE_PRIVATE_SYMBOLS ON)" >> config.cmake
70+
71+
# GPU SDKs, turn on if needed
72+
echo "set(USE_CUDA OFF)" >> config.cmake
73+
echo "set(USE_METAL OFF)" >> config.cmake
74+
echo "set(USE_VULKAN OFF)" >> config.cmake
75+
echo "set(USE_OPENCL OFF)" >> config.cmake
76+
77+
# cuBLAS, cuDNN, cutlass support, turn on if needed
78+
echo "set(USE_CUBLAS OFF)" >> config.cmake
79+
echo "set(USE_CUDNN OFF)" >> config.cmake
80+
echo "set(USE_CUTLASS OFF)" >> config.cmake
81+
82+
echo "set(USE_MICRO ON)" >> config.cmake
83+
```
84+
85+
設定好 config.cmake 之後,使用以下命令啟動建置:
86+
87+
88+
```
89+
cmake .. && cmake --build . --parallel $(nproc)
90+
```
91+
92+
93+
94+
亦可使用Ninja 來加速構建(與上指令兩者選一編譯即可):
95+
96+
```
97+
cmake .. -G Ninja && ninja
98+
```
99+
100+
成功編譯後應該在 build 目錄下會看到 `libtvm.so``libtvm_runtime.so`
101+
102+
103+
可以將編譯好的共享庫班一道系統路徑。
104+
```sh
105+
sudo cp build/*.so /usr/local/lib/
106+
```
107+
108+
109+
### 安裝Python套件(省略)
110+
111+
```
112+
cd ../python
113+
python setup.py install --user
114+
```
115+
116+
使用以下指令尋找是否有 runtime
117+
```
118+
ldconfig -p | grep libtvm_runtime
119+
```
120+
通常我們會將runtime系統路徑下,如果將runtime放置自己的資料夾可以輸入以下指令。
121+
122+
```
123+
export LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH
124+
125+
# 亦或是將 so 放到 /usr/local/lib/
126+
sudo ldconfig
127+
```
128+
129+
編譯指令
130+
131+
```
132+
g++ -std=c++17 -o main test.cpp -I../tvm/include -I../tvm/3rdparty/dlpack/include -I../tvm/3rdparty/dmlc-core/include ../tvm/build/libtvm_runtime.so -ldl -pthread
133+
```
134+
135+
136+
## Reference
137+
- [TVM 從原始碼安裝流程](https://tvm.hyper.ai/docs/install/from_source)
138+
- [TVM Install from Source](https://tvm.apache.org/docs/install/from_source.html)
139+
- [提到安裝LLVM ON](https://blog.csdn.net/justsolow/article/details/107371838)

BuildOnMac.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
## Reference
4+
- [How to install TVM on MAC OS](https://pyshine.com/How-to-install-TVM-in-MacOS/)

BuildOnWindows.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
## TVM Windows 安裝
3+
TVM 支援透過 MSVC 使用CMake 建置。需要有一個 Visual Studio 編譯器。 VS 的最低版本為Visual Studio Enterprise 2019。
4+
開始工具列搜尋 Developer Command Prompt for VS 2019 開啟終端機。
5+
6+
7+
### 下載 TVM
8+
下載 TVM 的 GitHub 儲存庫。
9+
10+
```
11+
git clone --recursive https://github.com/apache/tvm tvm
12+
```
13+
14+
15+
建立一個建置目錄,將cmake/config.cmake複製到該目錄。
16+
17+
```
18+
cd tvm
19+
mkdir build
20+
copy cmake\config.cmake build\
21+
```
22+
23+
建構TVM 及相關函式庫(使用Ninja 來加速構建):
24+
25+
```
26+
cd build
27+
cmake .. -G Ninja
28+
ninja
29+
```

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## 使用 TVM 進行模型編譯和優化
2+
將 scikit-learn 模型轉換為輕量級的 C 程式庫而不依賴 ONNX Runtime,可以使用 TVM 來精簡所需的庫或編譯過程。TVM 將模型轉換為更小的 C/C++ 程式碼,適合嵌入式應用並支援 scikit-learn 模型。
3+
4+
- Apache TVM 是一個開源的機器學習編譯框架,專門設計來編譯和優化模型,並生成針對不同硬體平台的高效程式碼。TVM 支援多種深度學習框架和 ONNX 格式。
5+
- 通過 TVM,您可以將 ONNX 模型轉換為精簡的 C/C++ 程式碼,並針對目標硬體進行優化。TVM 支援靜態編譯,因此可以生成無需額外依賴的輕量程式碼。
6+
7+
為了生成可跨平台執行的 C 原始碼並通過 C 進行推理,我們可以採取以下更可靠的方式:
8+
9+
- 使用 Hummingbird 將模型轉換為 ONNX 格式。
10+
- 使用 TVM 將 ONNX 模型導入並編譯為 C 代碼。
11+
- 編寫 C 程式載入並調用編譯後的模型進行推理。
12+
13+
## 安裝 TVM
14+
使用 pip 安裝 TVM。請在終端機中執行以下命令:
15+
16+
```sh
17+
pip install apache-tvm
18+
```
19+
20+
21+
22+
## Reference
23+
- [TVM Runtime 系统介紹 講解優勢](https://tvm.hyper.ai/docs/arch/arch/runtimes/)
24+
- [TVM學習(1)--建構環境 介紹到共享庫](https://sunicyosen.github.io/2019/08/02/TVM-Study-1-Setup-Environment)
25+
- [How to convert a network to GCC compilable C/C++ code? 提到BYOC](https://discuss.tvm.apache.org/t/how-to-convert-a-network-to-gcc-compilable-c-c-code/7846/6)
26+
- [microTVM:裸机上的 TVM](https://tvm.hyper.ai/docs/topic/microtvm/)
27+
- [microTVM 設計文檔](https://tvm.hyper.ai/docs/arch/arch/microtvm_design)
28+
29+
- [TVM等神經編譯器概覽:它是連接深度學習框架和硬體的橋樑](https://posts.careerengine.us/p/6050781ceee8dc2d4b00faca)

0 commit comments

Comments
 (0)