MLC-LLM 是一种高性能通用部署解决方案,允许使用具有编译器加速功能的本机 API 来本机部署任何大型语言模型。该项目的使命是让每个人都能利用机器学习编译技术在每个人的设备上本地开发、优化和部署人工智能模型。
AMD GPU | NVIDIA GPU | Apple GPU | Intel GPU | |
---|---|---|---|---|
Linux / Win | ✅ Vulkan, ROCm | ✅ Vulkan, CUDA | N/A | ✅ Vulkan |
macOS | ✅ Metal (dGPU) | N/A | ✅ Metal | ✅ Metal (iGPU) |
Web Browser | ✅ WebGPU and WASM | |||
iOS / iPadOS | ✅ Metal on Apple A-series GPU | |||
Android | ✅ OpenCL on Adreno GPU | ✅ OpenCL on Mali GPU |
环境安装
conda create --name mlc python=3.11
conda activate mlc
python3 -m pip install --pre -U -f https://mlc.ai/wheels mlc-chat-nightly-cu121 mlc-ai-nightly-cu121
python -c "import mlc_chat; print(mlc_chat)"
模型转换
模型转换分为两步:
- 转换模型权重
- 生成mlc chat 的配置
下面以qwen模型为例,暂时不支持qwen2
转换模型权重
mlc_chat convert_weight /home/chuan/models/qwen/Qwen-7B-Chat \
--quantization q4f16_1 \
-o /home/chuan/models/qwen/Qwen-7B-Chat/mlc
参数列表
–CONFIG
It can be one of the following:
- Path to a HuggingFace model directory that contains a
config.json
or - Path to
config.json
in HuggingFace format, or - The name of a pre-defined model architecture.
–quantization QUANTIZATION_MODE
可选项 q0f16
, q0f32
, q3f16_1
, q4f16_1
, q4f32_1
, and q4f16_awq
.推荐使用q4f16_1
–model-type MODEL_TYPE
Model architecture such as “llama”. If not set, it is inferred from config.json
–device DEVICE
The device used to do quantization such as “cuda” or “cuda:0”. Will detect from local available GPUs if not specified.
–source SOURCE
The path to original model weight, infer from config
if missing.
–source-format SOURCE_FORMAT
The format of source model weight, infer from config
if missing.
–output OUTPUT
The output directory to save the quantized model weight. Will create params_shard_*.bin
and ndarray-cache.json
in this directory.
生成mlc chat 的配置
mlc_chat gen_config /home/chuan/models/qwen/Qwen-7B-Chat \
--quantization q4f16_1 --conv-template chatml \
-o /home/chuan/models/qwen/Qwen-7B-Chat/mlc
注意
conv-template的值参照github.com/mlc-ai/mlc-…,
如果不包含你的模型也可以自定义,但是要从源代码重新编译mlc
gen_config的参数列表
–CONFIG
It can be one of the following:
- Path to a HuggingFace model directory that contains a
config.json
or - Path to
config.json
in HuggingFace format, or - The name of a pre-defined model architecture.
–quantization QUANTIZATION_MODE
–model-type MODEL_TYPE
–conv-template CONV_TEMPLATE
–context-window-size CONTEXT_WINDOW_SIZE
最大句子长度
–output OUTPUT
其他不重要的参数没有列出来
运行mlc
from mlc_chat import ChatModule
cm = ChatModule(model="/home/chuan/models/qwen/Qwen1___5-7B-Chat/mlc")
print(cm.generate("hello"))
把qwen模型编译成android app
我已经编译好了一个版本的app, 欢迎下载试用,需要科学上网
- 首先安装android studio,下载ndk、cmake,如图所示:
- 设置环境变量
export ANDROID_NDK=/home/chuan/Android/Sdk/ndk/26.2.11394342
export TVM_NDK_CC=/home/chuan/Android/Sdk/ndk/26.2.11394342/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android34-clang
export TVM_HOME=/home/chuan/github/mlc-llm/3rdparty/tvm
export JAVA_HOME=/home/chuan/tools/jdk-17.0.10
- 下载mlc ,并编译模型
git clone --recursive https://github.com/mlc-ai/mlc-llm/
cd ./mlc-llm/
MODEL_NAME=/home/chuan/models/qwen/Qwen-7B-Chat
QUANTIZATION=q4f16_1
mlc_chat convert_weight $MODEL_NAME --quantization $QUANTIZATION -o $MODEL_NAME/mlc
mlc_chat gen_config $MODEL_NAME --quantization $QUANTIZATION \
--conv-template chatml --context-window-size 768 -o $MODEL_NAME/mlc
mlc_chat compile $MODEL_NAME/mlc/mlc-chat-config.json \
--device android -o $MODEL_NAME/mlc/Qwen-7B-Chat-${QUANTIZATION}-android.tar
- 将模型上传到huggingface
git clone https://huggingface.co/chuan-niy/qwen_q4f16_1
git config user.name chuan-niy
git config user.email 1500546481@qq.com
cd qwen_q4f16_1
cp /home/chuan/models/qwen/Qwen-7B-Chat/mlc/* ./
git add . && git commit -m "Add qwen model weights for android"
git push origin main
- 编译android 库
cd ./android/library
vim ./src/main/assets/app-config.json
{
"model_list": [
{
"model_url": "https://huggingface.co/chuan-niy/qwen_q4f16_1",
"model_lib": "qwen_q4f16_1",
"estimated_vram_bytes": 4348727787,
"model_id": "Qwen-7B-Chat-hf-q4f16_1"
}
],
"model_lib_path_for_prepare_libs": {
"Qwen-7B-Chat-hf-q4f16_1": "/home/chuan/models/qwen/Qwen-7B-Chat/mlc/Qwen-7B-Chat-q4f16_1-android.tar"
}
}
在CMakeLists.txt中添加以下信息
vi CMakeLists.txt
set(JAVA_AWT_LIBRARY "/home/chuan/tools/jdk-17.0.10/include/linux")
set(JAVA_JVM_LIBRARY "/home/chuan/tools/jdk-17.0.10/include/linux")
set(JAVA_INCLUDE_PATH "/home/chuan/tools/jdk-17.0.10/include")
set(JAVA_INCLUDE_PATH2 "/home/chuan/tools/jdk-17.0.10/include/linux")
set(JAVA_AWT_INCLUDE_PATH "/home/chuan/tools/jdk-17.0.10/include")
需要修改#ifdef TVM4J_ANDROID代码的地方
vi mlc-llm/3rdparty/tvm/jvm/native/src/main/native/org_apache_tvm_native_c_api.cc
#ifdef TVM4J_ANDROID
_jvm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr);
#else
_jvm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr);
最后运行编译
./prepare_libs.sh
- 接着需要用android studio 打开android项目,连接手机调试,然后运行
在手机上运行结果如下:
大模型&AI产品经理如何学习
求大家的点赞和收藏,我花2万买的大模型学习资料免费共享给你们,来看看有哪些东西。
1.学习路线图
第一阶段: 从大模型系统设计入手,讲解大模型的主要方法;
第二阶段: 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用;
第三阶段: 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统;
第四阶段: 大模型知识库应用开发以LangChain框架为例,构建物流行业咨询智能问答系统;
第五阶段: 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型;
第六阶段: 以SD多模态大模型为主,搭建了文生图小程序案例;
第七阶段: 以大模型平台应用与开发为主,通过星火大模型,文心大模型等成熟大模型构建大模型行业应用。
2.视频教程
网上虽然也有很多的学习资源,但基本上都残缺不全的,这是我自己整理的大模型视频教程,上面路线图的每一个知识点,我都有配套的视频讲解。
(都打包成一块的了,不能一一展开,总共300多集)
因篇幅有限,仅展示部分资料,需要点击下方图片前往获取
3.技术文档和电子书
这里主要整理了大模型相关PDF书籍、行业报告、文档,有几百本,都是目前行业最新的。
4.LLM面试题和面经合集
这里主要整理了行业目前最新的大模型面试题和各种大厂offer面经合集。
👉学会后的收获:👈
• 基于大模型全栈工程实现(前端、后端、产品经理、设计、数据分析等),通过这门课可获得不同能力;
• 能够利用大模型解决相关实际项目需求: 大数据时代,越来越多的企业和机构需要处理海量数据,利用大模型技术可以更好地处理这些数据,提高数据分析和决策的准确性。因此,掌握大模型应用开发技能,可以让程序员更好地应对实际项目需求;
• 基于大模型和企业数据AI应用开发,实现大模型理论、掌握GPU算力、硬件、LangChain开发框架和项目实战技能, 学会Fine-tuning垂直训练大模型(数据准备、数据蒸馏、大模型部署)一站式掌握;
• 能够完成时下热门大模型垂直领域模型训练能力,提高程序员的编码能力: 大模型应用开发需要掌握机器学习算法、深度学习框架等技术,这些技术的掌握可以提高程序员的编码能力和分析能力,让程序员更加熟练地编写高质量的代码。
1.AI大模型学习路线图
2.100套AI大模型商业化落地方案
3.100集大模型视频教程
4.200本大模型PDF书籍
5.LLM面试题合集
6.AI产品经理资源合集
👉获取方式:
😝有需要的小伙伴,可以保存图片到wx扫描二v码免费领取【保证100%免费】🆓