张裕 发表于 2025-1-15 08:19:26

T-MAC在Windows(64)上的摆设推理

文章目录

目录
文章目录
前言
一、安装环境和依赖
1.安装Conda + Visual studio
2.安装Clang-17.0.6
二、构建T-MAC
1.引入库
2.进入项目目录并创建环境
3.构建tvm
4.安装T-MAC
5.验证安装 
三、使用T-MAC推理
总结

前言

T-MAC是微软研究院提出的一种创新方法,通过查表法(LUT)在CPU上实现低比特LLM(即权重量化LLM)的高效推理
本文是初学者根据官方文档和网络资料调通T-MAC的过程以及遇到的题目
T-MAC github地点:GitHub - microsoft/T-MAC: Low-bit LLM inference on CPU with lookup table

一、安装环境和依赖

1.安装Conda + Visual studio

根据官方文档,在Windows上安装依赖推荐使用conda+visual studio的方式。
Anaconda下载地点:Download Anaconda Distribution | Anaconda
安装Visual Studio时注意选择使用C++的桌面开发,不要启用“实用于 Windows 的 C++ Clang 工具”,因为 Clang 版本大概不兼容。
https://i-blog.csdnimg.cn/direct/0c16a05e1f8c455c922f5a5943134cb3.png
因为我们要使用自界说的LLVM 工具集版本,因此需要在单个组件中搜刮clang并将"对 LLVM (clang-cl) 工具集的 MSBuild 支持"选中
https://i-blog.csdnimg.cn/direct/26afae1b0b2f43bda8ad5f6314638e79.png
然后举行安装即可
2.安装Clang-17.0.6

下载地点:Download LLVM releases
在LLVM官网的下载页面找到17.0.6版本并举行下载
https://i-blog.csdnimg.cn/direct/1e8aef975cf84507bb111b47c9b13821.png
安装时注意选择将路径添加至所有用户的系统环境变量之中
https://i-blog.csdnimg.cn/direct/2f56414f8eaa44a78c7b6dc826c9a803.png
安装后在高级系统设置-环境变量-系统变量-Path中可以找到LLVM的路径
3.设置自界说 LLVM 位置和工具集

要设置 LLVM 的自界说路径并为一个或多个项目设置自界说 LLVM 工具集版本,请创建Directory.build.props文件。 然后,将该文件添加到恣意项目的根文件夹。 可以将它添加到根解决方案文件夹,以将它应用于解决方案中的所有项目。 该文件应如以下示例所示(但使用现实的 LLVM 路径和版本号):
<Project>
<PropertyGroup>
    <LLVMInstallDir>E:\Program Files\LLVM</LLVMInstallDir>
    <LLVMToolsVersion>17.0.6</LLVMToolsVersion>
</PropertyGroup>
</Project> 二、构建T-MAC

1.引入库

打开Developer PowerShell for VS 20XX
https://i-blog.csdnimg.cn/direct/1ba9dcb879c84c60b5ff167e14e96fd9.png
输入命令:
git clone --recursive https://github.com/microsoft/T-MAC.git 注意需要加入参数--recursive以克隆子模块
克隆时间比较久,如果网络不理想建议使用代理服务器。
2.进入项目目录并创建环境

cd T-MAC
conda env create --file conda\tvm-build-environment.yaml
conda activate tvm-build 使用Developer PowerShell进入到T-MAC根目录,创建并激活环境
3.构建tvm

cd 3rdparty\tvm
mkdir build
cp cmake\config.cmake build 使用以上命令进入tvm文件夹,创建build文件夹,并将config.cmake复制于其中
 之后,打开config.cmake文件,在文件最后加入一行:
set(USE_LLVM llvm-config)
https://i-blog.csdnimg.cn/direct/c20eaf2d62ed4557903355605b11b4ed.png
然后实行
cd build
cmake .. -A x64
cmake --build . --config Release -- /m 如果在运行 cmake .. -A x64 构建 TVM 时遇到类似 string sub-command regex, mode replace: regex "$" matched an empty string. 的错误,不用担心,只需再次运行 cmake .. -A x64 即可。 
构建tvm时需要的时间较久,且期间CPU占用极高。
4.安装T-MAC

cd ..\..\..\# back to project root directory
$env:MANUAL_BUILD = "1"
$env:PYTHONPATH = "$pwd\3rdparty\tvm\python"
pip install -e . -v 5.验证安装 

安装完成后,可以实行命令验证
python -c "import t_mac; print(t_mac.__version__); from tvm.contrib.clang import find_clang; print(find_clang())"  输出如下内容
https://i-blog.csdnimg.cn/direct/61bd0a5b5f334e04b748162186fa6c03.png
注意,以后每次再重新打开Developer PowerShell时,需要重新激活tvm-build环境,并设置PATHONPATH环境变量
三、使用T-MAC推理

以下仅为官方文档内的一个示例,使用EfficientQAT(GPTQ格式)Llama-3-8b-instruct-w2-g128
huggingface-cli download ChenMnZ/Llama-3-8b-instruct-EfficientQAT-w2g128-GPTQ --local-dir ${model_dir}  起首需要使用以上命令下载模型,需要将其中的
${model_dir} 替换为具体路径 例如 D:\project\T-MAC\modeldir 下载模型时若网络不佳建议使用代理服务器,注意有没有下载失败的文件,也可以去huggingface网站下载。
下载完成后使用代码实行推理
python tools/run_pipeline.py -o ${model_dir} -m llama-3-8b-2bit -q int_n 仍然注意将其中的 ${model_dir} 替换为具体路径
以下为一个大概的示例输出:
Running STEP.0: Compile kernels
Running command in /Users/user/jianyu/T-MAC/deploy:
    python compile.py -o tuned -da -nt 4 -tb -gc -gs 128 -ags 64 -t -m hf-bitnet-3b -r
Running STEP.1: Build T-MAC C++ CMakeFiles
Running command in /Users/user/jianyu/T-MAC/build:
    cmake -DCMAKE_INSTALL_PREFIX=/Users/user/jianyu/T-MAC/install ..
Running STEP.2: Install T-MAC C++
Running command in /Users/user/jianyu/T-MAC/build:
    cmake --build . --target install --config Release
Running STEP.3: Convert HF to GGUF
Running command in /Users/user/jianyu/T-MAC/3rdparty/llama.cpp:
    python convert-hf-to-gguf-t-mac.py /Users/user/Downloads/test_models/hf-bitnet-3B --outtype i2 --outfile /Users/user/Downloads/test_models/hf-bitnet-3B/ggml-model.i2.gguf --kcfg /Users/user/jianyu/T-MAC/install/lib/kcfg.ini
Running STEP.4: Build llama.cpp CMakeFiles
Running command in /Users/user/jianyu/T-MAC/3rdparty/llama.cpp/build:
    cmake .. -DLLAMA_TMAC=ON -DCMAKE_PREFIX_PATH=/Users/user/jianyu/T-MAC/install/lib/cmake/t-mac -DCMAKE_BUILD_TYPE=Release -DLLAMA_LLAMAFILE_DEFAULT=OFF -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
Running STEP.5: Build llama.cpp
Running command in /Users/user/jianyu/T-MAC/3rdparty/llama.cpp/build:
    cmake --build . --target main --config Release
Running STEP.6: Run inference
Running command in /Users/user/jianyu/T-MAC/3rdparty/llama.cpp/build:
    /Users/user/jianyu/T-MAC/3rdparty/llama.cpp/build/bin/main -m /Users/user/Downloads/test_models/hf-bitnet-3B/ggml-model.i2.gguf -n 128 -t 4 -p Microsoft Corporation is an American multinational corporation and technology company headquartered in Redmond, Washington. -b 1 -ngl 0 -c 2048
Check logs/2024-07-15-17-10-11.log for inference output  本人在STEP.0时遇到了一次如下错误:
https://i-blog.csdnimg.cn/direct/389036ba191946cab69609c64726c9b7.png
其原因是端口被占用。
 通过任务管理器-性能-资源监视器-网络-侦听端口可以看到9001为system历程
https://i-blog.csdnimg.cn/direct/30c4bfeeea2b4bd790fbca9d19be466a.png
需要到T-MAC\3rdparty\tvm\python\tvm\autotvm\measure\measure_methods.py下修改端口位置
https://i-blog.csdnimg.cn/direct/f1fb195585234a0591b7bda7bd7e171f.png
初始为9000,修改为9010后解决报错

总结


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: T-MAC在Windows(64)上的摆设推理