airllm

库

lyogavin/airllm

在 4GB 显存单卡上运行 70B 大语言模型,无需量化。

项目简介

AirLLM 优化推理内存使用,使得 70B 等大语言模型可以在单张 4GB 显卡上运行,无需量化、蒸馏或剪枝。支持将 Llama 3.1 405B 部署到 8GB 显存,并提供分块量化加速。

README 预览

\n\n[**Quickstart**](#quickstart) | \n[**Configurations**](#configurations) | \n[**MacOS**](#macos) | \n[**Example notebooks**](#example-python-notebook) | \n[**FAQ**](#faq)\n\n**AirLLM** optimizes inference memory usage, allowing 70B large language models to run inference on a single 4GB GPU card without quantization, distillation and pruning. And you can run **405B Llama3.1** on **8GB vram** now.\n\n\n[](https://pepy.tech/project/airllm)\n\n[](https://github.com/LianjiaTech/BELLE/blob/main/LICENSE)\n[](https://static.aicompose.cn/static/wecom_barcode.png?t=1671918938)\n[](https://discord.gg/2xffU5sn)\n[\n](https://pypi.org/project/airllm/)\n[](https://medium.com/@lyo.gavin)\n[](https://gavinliblog.com)\n[](https://patreon.com/gavinli)\n[](https://github.com/sponsors/lyogavin)\n\n## AI Agents Recommendation:\n\n* [Best AI Game Sprite Generator](https://godmodeai.co)\n\n* [Best AI Facial Expression Editor](https://crazyfaceai.com)\n\n## Updates\n[2024/08/20] v2.11.0: Support Qwen2.5\n\n[2024/08/18] v2.10.1 Support CPU inference. Support non sharded models. Thanks @NavodPeiris for the great work! \n\n[2024/07/30] Support Llama3.1 **405B** ([example notebook](https://colab.research.google.com/github/lyogavin/airllm/blob/main/air_llm/examples/run_llama3.1_405B.ipynb)). Support **8bit/4bit quantization**.\n\n[2024/04/20] AirLLM supports Llama3 natively already. Run Llama3 70B on 4GB single GPU.\n\n[2023/12/25] v2.8.2: Support MacOS running 70B large language models.\n\n[2023/12/20] v2.7: Support AirLLMMixtral. \n\n[2023/12/20] v2.6: Added AutoModel, automatically detect model type, no need to provide model class to initialize model.\n\n[2023/12/18] v2.5: added prefetching to overlap the model loading and compute. 10% speed improvement.\n\n[2023/12/03] added support of **ChatGLM**, **QWen**, **Baichuan**, **Mistral**, **InternLM**!\n\n[2023/12/02] added support for safetensors. Now support all top 10 models in open llm leaderboard.\n\n[2023/12/01] airllm 2.0. Support com

常见问题 (4)

故障排除
在Mac上使用airllm运行llama模型时,如何修复“ValueError: Cannot index mlx array using the given type”错误?

此错误出现在使用airllm搭配MLX后端在Apple Silicon上运行时,因为模型期望MLX数组,但输入的token IDs是以PyTorch张量形式提供的。解决方法是将输入tokens进行转换:使用 import mlx.core as mx; generation_output = model.generate(mx.array(input_tokens['input_ids'])) 替代 input_tokens['input_ids'].cuda()。这样可以确保与MLX的嵌入层兼容。该问题在airllm v2.9.1、Python 3.12.4、mlx版本14.1–16.1、macOS 14.5及M1 Pro上被报告。

来源 Issue #167
informational
AirLLM 是否原生支持 Llama3 70B?

AirLLM 已原生支持 Llama3 70B,因为 Llama3 在架构上没有需要调整 AirLLM 现有 Llama 实现的变化。使用 AirLLM 运行 Llama3 70B 无需任何额外的功能开发。

来源 Issue #121
故障排除
如何修复在 macOS 上使用 transformers/airllm 加载模型时出现的 "ModuleNotFoundError: Could not import module 'LlamaForCausalLM'" 错误?

这个错误具有误导性——它是由损坏的 torchvision 安装所引发的被掩盖的导入失败,而不是缺少 Llama 模块。回溯(traceback)中真正的错误是 RuntimeError: operator torchvision::nms does not exist,当 torch 和 torchvision 版本在二进制层面不兼容时就会出现这种情况(在 Python 3.13 / Apple Silicon 上很常见)。Transformers 的惰性加载器会触发 image_utils → torchvision,于是崩溃便以令人困惑的 ModuleNotFoundError 的形式表现出来。

针对纯文本 LLM(Qwen、Llama 等)的修复方法——完全移除 torchvision:

uv pip uninstall torchvision
# 或者:pip uninstall -y torchvision

在没有 torchvision 的情况下,transformers 会跳过视觉导入路径,LlamaForCausalLM 即可正常加载。

如果确实需要视觉支持,请改为重新安装相互匹配的版本:

uv pip install -U --force-reinstall torch torchvision

确保两个包来自同一发布渠道(例如 torch 2.5.x 搭配 torchvision 0.20.x)。可以使用 python -c "import torchvision; print(torchvision.__version__)" 进行验证。

macOS 用户请注意:还需将 .cuda() 替换为 .to('mps'),或者直接在 CPU 上运行,因为 CUDA 在 Apple Silicon 上不可用。

来源 Issue #326
故障排除
如何修复 macOS Apple Silicon 上 AirLLM 的 "Cannot index mlx array using the given type" 错误?

AirLLM 的 MLX 后端(AirLLMLlamaMlx.generate())仅接受 mlx.array 索引,但 HuggingFace tokenizer 在使用 return_tensors="pt" 时返回的是 torch.LongTensor,这会导致 mlx.nn.Embedding 崩溃。由于 AutoModel 会将所有内容都路由到 MLX 类,因此该问题会影响 macOS 上的所有架构(Llama、Qwen、Mistral、Mixtral、GLM、Baichuan)。临时解决方法——在调用 generate() 之前,将 token ID 强制转换为 mlx 数组:

import mlx.core as mx
from airllm import AutoModel

model = AutoModel.from_pretrained("Qwen/Qwen2.5-7B-Instruct", compression=None)
tokens = model.tokenizer(["Hello world"], return_tensors="pt").input_ids
input_ids = mx.array(tokens.detach().cpu().numpy())  # torch -> numpy -> mlx
model.generate(input_ids, max_new_tokens=8)

或者,也可以使用 return_tensors="np" 进行分词,然后用 mx.array(...) 进行包装。一个永久修复方案(一个内部的 _coerce_to_mlx_array() 辅助函数,用于在 generate() 边界处处理 torch/numpy/mlx 输入)已经通过 PR 提出;在该 PR 被合并之前,仍需手动进行转换。

来源 Issue #280