常规编译cpp库(可选)
- 注:可选操作,可以直接跳到
快速编译cpp库
步骤,那个相对来说更快一些
- 安装kineto库(可选,可以消除一个警告)
git clone --recursive https://github.com/pytorch/kineto.git
cd kineto/libkineto
mkdir build && cd build
cmake ..
make -j8
make install
- 进入项目代码里面的
tensorrt_llm_july-release-v1/cpp
目录
cd tensorrt_llm_july-release-v1/cpp
- 加载一个配置,让系统能找到cuda/TensorRT所在位置。
export LD_LIBRARY_PATH="/usr/local/cuda/lib64/stubs:/usr/lib/x86_64-linux-gnu/:/usr/local/cuda/lib64/:${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH=/usr/local/TensorRT-9.0.0.2/lib/:$LD_LIBRARY_PATH
- 开始配置cmake, Build Type 可选None Debug Release RelWithDebInfo,这个过程大概需要3-5钟,会卡在
Using MPI_LIBRARIES
那里一会,不用着急。
mkdir build
cd build
cmake .. \
-D TRT_INCLUDE_DIR=/usr/local/TensorRT-9.0.0.2/include \
-D nvinfer_LIB_PATH=/usr/local/TensorRT-9.0.0.2/targets/x86_64-linux-gnu/lib/libnvinfer.so \
-D nvonnxparser_LIB_PATH=/usr/local/TensorRT-9.0.0.2/targets/x86_64-linux-gnu/lib/libnvonnxparser.so \
-D CMAKE_BUILD_TYPE=Release
- 正式编译(10可以改为你CPU最大支持的线程数)
make -j10
- 大概等待20-30分钟就差不多了
快速编译cpp库
- 进入容器,你的项目代码所在地,按照上一个教程,我的项目代码已经映射到
/root/workspace/trt2023
目录。
cd /root/workspace/trt2023
- 拷贝项目里面的件夹覆盖到
/root/workspace/tensorrt_llm_july-release-v1
,这里我只需要重新编译plugin,所以将本项目里面的plugins和kernels部分代码覆盖到/root/workspace/tensorrt_llm_july-release-v1
就行了,下面是rmsnormPlugin的一个简单示范,注意不要全局覆盖plugins和kenels,会导致build目录失效而重新编译,这样会很浪费时间。
cp -r tensorrt_llm_july-release-v1/cpp/tensorrt_llm/kernels/rmsnormKernels.* /root/workspace/tensorrt_llm_july-release-v1/cpp/tensorrt_llm/kernels/
cp -r tensorrt_llm_july-release-v1/cpp/tensorrt_llm/plugins/rmsnorm* /root/workspace/tensorrt_llm_july-release-v1/cpp/tensorrt_llm/plugins/
cp -r tensorrt_llm_july-release-v1/cpp/tensorrt_llm/plugins/CMakeLists.txt /root/workspace/tensorrt_llm_july-release-v1/cpp/tensorrt_llm/plugins/
cp -r tensorrt_llm_july-release-v1/cpp/tensorrt_llm/plugins/api/ /root/workspace/tensorrt_llm_july-release-v1/cpp/tensorrt_llm/plugins/
- 这里补充一下,.cu文件和.cu附带的.h文件放kernels目录,.cpp文件和.cpp附带的.h文件放plugins目录,因为kernels比plugins目录先编译。
- 需要修改
tensorrt_llm_july-release-v1/cpp/tensorrt_llm/plugins/CMakeLists.txt
,在set(PLUGIN_LISTS
里面加上你的plugin文件夹名称。 - 需要修改
tensorrt_llm_july-release-v1/cpp/tensorrt_llm/plugins/api/InferPlugin.cpp
,在里面加上你新增插件的头文件路径,以及在下面的代码里面用nvinfer1::initializePlugin
初始化对应的PluginCreator,否则无法注册。
- 进入该容器里面的
/root/workspace/tensorrt_llm_july-release-v1/cpp/build
目录
cd /root/workspace/tensorrt_llm_july-release-v1/cpp/build
- 进入Build目录,重新cmake一下,让cmake配置知道你放了新代码进来。
cmake ..
- 正式编译
make
- 编译成功的日志,可以看到新的plugin已经被链接到
libnvinfer_plugin.so
了
......
[ 77%] Built target kernels_src
Consolidate compiler generated dependencies of target layers_src
[ 79%] Built target layers_src
[ 80%] Linking CXX shared library libtensorrt_llm.so
[ 80%] Built target tensorrt_llm
[ 80%] Linking CXX static library libtensorrt_llm_static.a
[ 80%] Built target tensorrt_llm_static
Consolidate compiler generated dependencies of target th_utils
[ 81%] Built target th_utils
Consolidate compiler generated dependencies of target th_common
[ 82%] Linking CXX shared library libth_common.so
[ 83%] Built target th_common
Consolidate compiler generated dependencies of target nvinfer_plugin
[ 83%] Linking CXX shared library libnvinfer_plugin.so
[ 90%] Built target nvinfer_plugin
......
[100%] Linking CXX executable gptSessionBenchmark
[100%] Built target gptSessionBenchmark
编译whl
- 进入script目录
cd /root/workspace/tensorrt_llm_july-release-v1
- 直接python运行就可以编译了
python3 ./scripts/build_wheel.py
- 编译成功后,可以去build目录查看成品。
cd /root/workspace/tensorrt_llm_july-release-v1/build
- 观察whl文件时间
ls -lh tensorrt_llm-0.1.3-py3-none-any.whl
-rw-r--r-- 1 root root 120M Sep 15 04:18 tensorrt_llm-0.1.3-py3-none-any.whl
- 安装新版whl
pip install tensorrt_llm-0.1.3-py3-none-any.whl
- 观察一下最新lib库时间,貌似并不是刚刚编译的Lib库,原因系统已经装好了这个库了,所以直接跳过了。
ls /usr/local/lib/python3.8/dist-packages/tensorrt_llm/libs/ -lh
- 可以选择卸载
tensorrt_llm
再安装whl即可。
pip uninstall tensorrt-llm
pip install tensorrt_llm-0.1.3-py3-none-any.whl
- 再次观察lib库时间
ls /usr/local/lib/python3.8/dist-packages/tensorrt_llm/libs/ -lh
-rwxr-xr-x 1 root staff 417M Sep 16 02:53 libnvinfer_plugin_tensorrt_llm.so
-rwxr-xr-x 1 root staff 68M Sep 16 02:53 libth_common.so