TII 发布 Falcon Mamba:首个强性能无注意力 7B 模型
Welcome Falcon Mamba: The first strong attention-free 7B model
阿布扎比 TII 发布 Falcon Mamba 7B,称其为首个大规模通用纯 Mamba 模型,采用开放许可并已上线 Hugging Face 生态。该模型基于原始 Mamba 架构并加入额外 RMS 归一化层以稳定大规模训练,用约 5500GT 数据训练,可处理任意长度序列且生成每个 token 耗时恒定,单张 A10 24GB GPU 即可运行。
TII 发布首个大规模纯 Mamba 架构 7B 模型,并给出与同规模 Transformer 的基准和长序列内存对比。
译文尚不完整,完整内容请切换到原文。
Falcon Mamba 是由阿布扎比的 Technology Innovation Institute (TII) 开发的新模型,依据 TII Falcon Mamba 7B License 1.0 发布。该模型开放获取,可在 Hugging Face 生态系统中此处获取,供任何人用于研究或应用目的。
在本博客中,我们将探讨该模型背后的设计决策、该模型如何与其他现有 SoTA 模型相比具有竞争力,以及如何在 Hugging Face 生态系统中使用它。
首个通用大规模纯 Mamba 模型
基于注意力机制的 Transformer 是当今所有最强大型语言模型所使用的主导架构。然而,由于计算和内存成本随序列长度增加,注意力机制在处理长序列时从根本上受到限制。各种替代架构,特别是状态空间语言模型(SSLMs),试图解决序列扩展限制,但在性能上落后于 SoTA Transformer。
借助 Falcon Mamba,我们证明序列扩展限制确实可以在不损失性能的情况下被克服。Falcon Mamba 基于 Mamba: Linear-Time Sequence Modeling with Selective State Spaces 中提出的原始 Mamba 架构,并增加了额外的 RMS 归一化层,以确保大规模训练时的稳定性。这种架构选择确保 Falcon Mamba:
- 可以处理任意长度的序列而不会增加内存存储,特别是可适配单张 A10 24GB GPU。
- 生成新 token 所需时间恒定,无论上下文大小如何(参见此部分)
模型训练
Falcon Mamba 使用约 5500GT 的数据进行训练,主要由 RefinedWeb 数据组成,并加入了来自公共来源的高质量技术数据和代码数据。我们在大部分训练中使用恒定学习率,随后是一个相对较短的学习率衰减阶段。在最后阶段,我们还加入了一小部分高质量精选数据,以进一步增强模型性能。
评估
我们使用 lm-evaluation-harness 包在新版排行榜的所有基准上评估我们的模型,然后使用 Hugging Face 分数归一化对评估结果进行归一化。
model name |
IFEval |
BBH |
MATH LvL5 |
GPQA |
MUSR |
MMLU-PRO |
Average |
|---|---|---|---|---|---|---|---|
| 纯 SSM 模型 | |||||||
Falcon Mamba-7B |
33.36 | 19.88 | 3.63 | 8.05 | 10.86 | 14.47 | 15.04 |
TRI-ML/mamba-7b-rw* |
22.46 | 6.71 | 0.45 | 1.12 | 5.51 | 1.69 | 6.25 |
| SSM-注意力混合模型 | |||||||
recurrentgemma-9b |
30.76 | 14.80 | 4.83 | 4.70 | 6.60 | 17.88 | 13.20 |
Zyphra/Zamba-7B-v1* |
24.06 | 21.12 | 3.32 | 3.03 | 7.74 | 16.02 | 12.55 |
| Transformer 模型 | |||||||
Falcon2-11B |
32.61 | 21.94 | 2.34 | 2.80 | 7.53 | 15.44 | 13.78 |
Meta-Llama-3-8B |
14.55 | 24.50 | 3.25 | 7.38 | 6.24 | 24.55 | 13.41 |
Meta-Llama-3.1-8B |
12.70 | 25.29 | 4.61 | 6.15 | 8.98 | 24.95 | 13.78 |
Mistral-7B-v0.1 |
23.86 | 22.02 | 2.49 | 5.59 | 10.68 | 22.36 | 14.50 |
Mistral-Nemo-Base-2407 (12B) |
16.83 | 29.37 | 4.98 | 5.82 | 6.52 | 27.46 | 15.08 |
gemma-7B |
26.59 | 21.12 | 6.42 | 4.92 | 10.98 | 21.64 | 15.28 |
此外,我们使用 lighteval 在第一版 LLM 排行榜的基准上评估我们的模型。
model name |
ARC |
HellaSwag |
MMLU |
Winogrande |
TruthfulQA |
GSM8K |
Average |
|---|---|---|---|---|---|---|---|
| 纯 SSM 模型 | |||||||
Falcon Mamba-7B* |
62.03 | 80.82 | 62.11 | 73.64 | 53.42 | 52.54 | 64.09 |
TRI-ML/mamba-7b-rw* |
51.25 | 80.85 | 33.41 | 71.11 | 32.08 | 4.70 | 45.52 |
| SSM-注意力混合模型 | |||||||
recurrentgemma-9b** |
52.00 | 80.40 | 60.50 | 73.60 | 38.60 | 42.60 | 57.95 |
Zyphra/Zamba-7B-v1* |
56.14 | 82.23 | 58.11 | 79.87 | 52.88 | 30.78 | 60.00 |
| Transformer 模型 | |||||||
Falcon2-11B |
59.73 | 82.91 | 58.37 | 78.30 | 52.56 | 53.83 | 64.28 |
Meta-Llama-3-8B |
60.24 | 82.23 | 66.70 | 78.45 | 42.93 | 45.19 | 62.62 |
Meta-Llama-3.1-8B |
58.53 | 82.13 | 66.43 | 74.35 | 44.29 | 47.92 | 62.28 |
Mistral-7B-v0.1 |
59.98 | 83.31 | 64.16 | 78.37 | 42.15 | 37.83 | 60.97 |
gemma-7B |
61.09 | 82.20 | 64.56 | 79.01 | 44.79 | 50.87 | 63.75 |
对于标有星号的模型,我们在内部评估了任务,而对于标有两个星号的模型,结果取自论文或模型卡。
处理长序列
基于 SSM 模型在处理长序列上的理论效率,我们使用 optimum-benchmark 库对 Falcon Mamba 与流行的 transformer 模型在内存占用和生成吞吐量方面进行了比较。为了公平比较,我们将所有 transformer 模型的词表大小重新缩放以匹配 Falcon Mamba,因为词表大小对模型的内存需求有很大影响。
在展示结果之前,我们先讨论序列中提示(预填充)部分和生成(解码)部分之间的区别。正如我们将看到的,预填充的细节对状态空间模型比对 transformer 模型更为重要。当 transformer 生成下一个 token 时,它需要关注上下文中所有先前 token 的键和值。这意味着内存需求和生成时间都随上下文长度线性增长。状态空间模型只关注并存储其循环状态,因此不需要额外的内存或时间来生成大序列。虽然这解释了 SSM 在解码阶段相对于 transformer 所声称的优势,但预填充阶段需要额外努力才能充分利用 SSM 架构。
预填充的标准方法是并行处理整个提示以充分利用 GPU。这种方法在 optimum-benchmark 库中使用,我们将其称为并行预填充。并行预填充需要在内存中存储提示中每个 token 的隐藏状态。对于 transformer,这些额外内存主要由存储的 KV 缓存的内存主导。对于 SSM 模型,不需要缓存,存储隐藏状态的内存成为唯一与提示长度成比例的部分。因此,内存需求将随提示长度增长,SSM 模型将失去处理任意长序列的能力,与 transformer 类似。
并行预填充的替代方案是逐 token 处理提示,我们将其称为顺序预填充。类似于序列并行,它也可以在提示的更大块上而不是单个 token 上进行,以更好地利用 GPU。虽然顺序预填充对 transformer 意义不大,但它为 SSM 模型重新带来了处理任意长提示的可能性。
With these remarks in mind, we first test the largest sequence length that can fit on a single 24 GB A10 GPU, putting the results on the figure below. The batch size is fixed at 1, and we are using float32 precision. Even for parallel prefill, Falcon Mamba can fit larger sequences than a transformer, while in sequential prefill, it unlocks its full potential and can process arbitrary long prompt
Next, we measure the generation throughput in a setting with a prompt of length 1 and up to 130k generated tokens, using batch size 1 and H100 GPU. The results are reported in the figure below. We observe that our Falcon Mamba is generating all the tokens at constant throughput and without any increase in CUDA peak memory. For the transformer model, the peak memory grows, and generation speed slows down as the number of generated tokens grows.
如何在 Hugging Face transformers 中使用它?
Falcon Mamba 架构将在下一版 Hugging Face transformers 库(>4.45.0)中提供。要使用该模型,请确保安装最新版本的 Hugging Face transformers,或从源代码安装该库。
Falcon Mamba 与您熟悉的 Hugging Face 提供的大多数 API 兼容,例如 AutoModelForCausalLM 或 pipeline:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_id = "tiiuae/falcon-mamba-7b"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
inputs = tokenizer("Hello world, today", return_tensors="pt").to(0)
output = model.generate(**inputs, max_new_tokens=100, do_sample=True)
print(tokenizer.decode(Output[0], skip_special_tokens=True))
由于模型较大,它还支持 bitsandbytes 量化等功能,以便在较小的 GPU 内存限制下运行模型,例如:
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
model_id = "tiiuae/falcon-mamba-7b"
tokenizer = AutoTokenizer.from_pretrained(model_id)
quantization_config = BitsAndBytesConfig(load_in_4bit=True)
model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=quantization_config)
inputs = tokenizer("Hello world, today", return_tensors="pt").to(0)
output = model.generate(**inputs, max_new_tokens=100, do_sample=True)
print(tokenizer.decode(output[0], skip_special_tokens=True))
我们也很高兴推出 Falcon Mamba 的指令微调版本,该版本使用额外的 50 亿个 token 的监督微调(SFT)数据进行了微调。这种扩展训练增强了模型以更高的精度和有效性执行指令任务的能力。您可以通过我们的演示体验 instruct 模型的能力,演示位于此处。对于聊天模板,我们使用以下格式:
<|im_start|>user
prompt<|im_end|>
<|im_start|>assistant
您还可以直接使用基础模型和指令模型的 4 位转换版本。请确保能够访问与 bitsandbytes 库兼容的 GPU 来运行量化模型。
您还可以通过 torch.compile 受益于更快的推理;只需在加载模型后调用 model = torch.compile(model)。
致谢
本博客文章的作者感谢 Hugging Face 团队在其生态系统内提供的顺利支持和集成,特别是
- Alina Lozovskaya 和 Clementine Fourrier 帮助我们在排行榜上评估模型
- Arthur Zucker 负责 transformers 集成
- Vaibhav Srivastav、hysts 和 Omar Sanseviero 对 Hub 相关问题的支持
作者还要感谢 Tri Dao 和 Albert Gu 为社区实现并开源了 Mamba 架构。
来源:Hugging Face Blog · huggingface.co

