Mistral 7B
摘要
我们推出了 Mistral 7B,这是一个具有 70 亿参数的语言模型,具有卓越的性能和效率。
在所有评估基准中,Mistral 7B 均优于已公开的最佳 13B 模型(Llama 2),在推理、数学和代码生成方面优于已发布最佳 34B 模型(Llama 1)。
我们的模型利用分组查询注意力(GQA)来加快推理速度,并结合滑动窗口注意力(SWA)来有效处理任意长度的序列,同时降低推理成本。
我们还提供了一个根据指令进行微调的模型 Mistral 7B - Instruct,该模型在人类和自动基准测试中都超过了 Llama 2 13B - chat 模型。
我们的模型根据 Apache 2.0 许可发布。
代码 https://github.com/mistralai/mistral-src
网页: https://mistral.ai/news/announcing-mistral-7b/
1导言
在快速发展的自然语言处理(NLP)领域,为了追求更高的模型性能,往往需要扩大模型规模。 然而,这种扩展往往会增加计算成本和推理延迟,从而增加在实际应用场景中部署的障碍。 在这种情况下,寻找既能提供高性能又能提高效率的平衡模式就变得至关重要。 我们的模型 Mistral 7B 证明,精心设计的语言模型可以在保持高效推理的同时提供高性能。 在所有测试基准中,Mistral 7B 均优于之前最好的 13B 模型(Llama 2,touvron2023llama2 ),并在数学和代码生成方面超过了最好的 34B 模型(LLaMa 34B,touvron2023llama )。 此外,Mistral 7B 的编码性能接近 Code-Llama 7B roziere2023code ,而不会降低非编码相关基准的性能。
Mistral 7B 利用了分组查询关注(GQA)ainslie2023gqa 和滑动窗口关注(SWA)child2019generating ; beltagy2020longformer。 GQA 大大加快了推理速度,还降低了解码过程中的内存需求,允许更大的批量,从而提高了吞吐量,这是实时应用的一个关键因素。 此外,SWA 的设计还能以更低的计算成本更有效地处理更长的序列,从而缓解 LLM 的一个常见限制。 这些注意机制共同促进了 Mistral 7B 的性能和效率的提高。
Mistral 7B 根据 Apache 2.0 许可发布。 该版本附带了一个参考实现111https://github.com/mistralai/mistral-src 使用 vLLM kwon2023efficient 推断服务器和 SkyPilot 222https://github.com/skypilot-org/skypilot,可方便地在本地或 AWS、GCP 或 Azure 等云平台上进行部署。 此外,还简化了与 Hugging Face 333https://huggingface.co/mistralai 的集成,以便于集成。 此外,Mistral 7B 的设计还便于对各种任务进行微调。 为了证明它的适应性和卓越性能,我们展示了根据 Mistral 7B 微调的聊天模型,其性能明显优于 Llama 2 13B - 聊天模型。
Mistral 7B 在平衡高性能与保持大型语言模型效率的目标方面迈出了重要一步。 通过我们的工作,我们的目标是帮助社区创建更经济、更高效、更高性能的语言模型,以广泛应用于现实世界。
2结构细节
Parameter | Value |
---|---|
dim | |
n_layers | |
head_dim | |
hidden_dim | |
n_heads | |
n_kv_heads | |
window_size | |
context_len | |
vocab_size |
Mistral 7B 基于 transformer 架构 vaswani2017attention。 表 1 汇总了架构的主要参数。 与 Llama 相比,它引入了一些变化,我们总结如下。
滑动窗口注意力。 SWA 利用 transformer 堆叠的层来获取超出窗口大小 的信息。位于层 位置 的隐藏状态 ,会关注上一层位置介于 和 之间的所有隐藏状态。递归地讲, 可以访问输入层距离不超过 个 token,如图1所示。 在最后一层,使用 的窗口大小,我们的理论注意力跨度约为 词符。 在实践中,对于序列长度为 16K 和 的情况,对 FlashAttention dao2022flashattention 和 xFormers xFormers2022 所做的更改比 vanilla attention 基线的速度提高了 2 倍。
滚动缓冲缓存。 固定的注意力范围意味着我们可以使用滚动缓冲缓存来限制缓存大小。 缓存的大小固定为 ,时间步 的键和值存储在缓存的 位置。 因此,当位置 大于 时,缓存中过去的值将被覆盖,缓存的大小也将停止增加。 我们在图 2 中对 进行了说明。 如果序列长度为 32k 标记,缓存内存使用量可减少 8 倍,而模型质量不会受到影响。
预填充和分块 在生成序列时,我们需要逐个预测标记,因为每个标记都以前面的标记为条件。 但是,提示信息是预先知道的,我们可以用提示信息预先填充 (, )缓存。 如果提示信息非常大,我们可以将其分割成小块,并预先将每个小块填入缓存。 为此,我们可以选择窗口大小作为块大小。 因此,对于每个数据块,我们需要计算缓存和数据块的注意力。 图 3 显示了注意力屏蔽如何在高速缓存和数据块上工作。
3 结果
我们将 Mistral 7B 与 Llama 进行了比较,并使用我们自己的评估管道重新运行了所有基准,以便进行公平比较。 我们对以下各类任务的性能进行了测量:
-
•
常识推理(0-shot): Hellaswag zellers2019hellaswag , Winogrande sakaguchi2021winogrande , PIQA bisk2020piqa , SIQA sap2019socialiqa 、OpenbookQA mihaylov2018can , ARC-Easy, ARC-Challenge clark2018think , CommonsenseQA talmor2018commonsenseqa .
-
•
世界知识(5-shot): NaturalQuestions kwiatkowski2019natural , TriviaQA joshi2017triviaqa
-
•
阅读理解(0-shot): BoolQ clark2019boolq , QuAC choi2018quac
-
•
数学 GSM8K cobbe2021training(8-shot),maj@8 和 MATH hendrycks2021measuring(4-shot),maj@4
-
•
代码 Humaneval chen2021evaluating(0-shot)和 MBPP austin2021program(3-shot)。
-
•
受欢迎的汇总结果: MMLUhendrycks2020measuring(5-shot)、BBH suzgun2022challenging(3-shot)和AGI Eval zhong2023agieval(3-5-shot,仅限英语选择题)。
表 2 中报告了 Mistral 7B、Llama 2 7B/13B 和 Code-Llama 7B 的详细结果。 图 4 比较了 Mistral 7B 和 Llama 2 7B/13B 以及 Llama 1 34B 的性能。444由于 Llama 2 34B 没有开源,我们报告了 Llama 1 34B 的结果。 不同类别。 Mistral 7B 在所有指标上都超过了 Llama 2 13B,并在大多数基准上超过了 Llama 1 34B。 特别是,Mistral 7B 在代码、数学和推理基准测试中表现出色。
规模与效率。 我们计算了 Llama 2 系列的 "等效型号尺寸",旨在了解 Mistral 7B 型号在性价比方面的效率(见图 5)。 在对推理、理解和 STEM 推理(特别是 MMLU)进行评估时,Mistral 7B 的表现符合人们对 Llama 2 模型的预期,其大小是 Llama 2 的 3 倍多。 在 "知识 "基准测试中,Mistral 7B 的压缩率较低,仅为 1.9 倍,这可能是由于其有限的参数数量限制了其可存储的知识量。
评估差异。 在某些基准上,我们的评估方案与 Llama 2 论文中报告的方案存在一些差异:1) 在 MBPP 上,我们使用手工验证的子集 2) 在 TriviaQA 上,我们不提供维基百科上下文。
Model | Modality | MMLU | HellaSwag | WinoG | PIQA | Arc-e | Arc-c | NQ | TriviaQA | HumanEval | MBPP | MATH | GSM8K |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
LLaMA 2 7B | Pretrained | 44.4% | 77.1% | 69.5% | 77.9% | 68.7% | 43.2% | 24.7% | 63.8% | 11.6% | 26.1% | 3.9% | 16.0% |
LLaMA 2 13B | Pretrained | 55.6% | 80.7% | 72.9% | 80.8% | 75.2% | 48.8% | 29.0% | 69.6% | 18.9% | 35.4% | 6.0% | 34.3% |
Code-Llama 7B | Finetuned | 36.9% | 62.9% | 62.3% | 72.8% | 59.4% | 34.5% | 11.0% | 34.9% | 31.1% | 52.5% | 5.2% | 20.8% |
Mistral 7B | Pretrained | 60.1% | 81.3% | 75.3% | 83.0% | 80.0% | 55.5% | 28.8% | 69.9% | 30.5% | 47.5% | 13.1% | 52.2% |
模型 |
|
MT Bench | ||
---|---|---|---|---|
WizardLM 13B v1.2 | 1047 | 7.2 | ||
Mistral 7B Instruct | 1031 | 6.84 +/- 0.07 | ||
Llama 2 13B Chat | 1012 | 6.65 | ||
Vicuna 13B | 1041 | 6.57 | ||
Llama 2 7B Chat | 985 | 6.27 | ||
Vicuna 7B | 997 | 6.17 | ||
Alpaca 13B | 914 | 4.53 |
4指令微调
为了评估 Mistral 7B 的泛化能力,我们在 Hugging Face 资源库中公开的指令数据集上对其进行了微调。 没有使用专有数据或训练技巧:Mistral 7B - Instruct 模型是一个简单的初步示范,可以轻松地对基础模型进行微调,以实现良好的性能。 在表 3 中,我们可以看到,由此产生的模型 Mistral 7B - Instruct 在 MT-Bench 上的性能优于所有 7B 模型,并与 13B - Chat 模型相当。 在 https://llmboxing.com/leaderboard 上进行了独立的人工评估。
如图 6所示,在这次评估中,参与者收到了一组问题和两个模型的匿名回答,并要求他们选择自己喜欢的回答。 截至 2023 年 10 月 6 日,Mistral 7B 生成的输出被优先使用 5020 次,而 Llama 2 13B 生成的输出被优先使用 4143 次。
5 为正面应用添加护栏
在人工智能生成方面,执行防护措施的能力对于面向前端的应用非常重要。 在本节中,我们将重点介绍如何利用系统提示,在我们的模型之上选择性地执行输出约束。 此外,我们还展示了 Mistral 7B 执行细粒度内容节制的能力,这对于在应用程序中强制执行高质量内容非常有用。
5.1执行护栏的系统提示
我们引入了一个系统提示(见下文)来引导模型在指定的范围内生成答案,这与 Llama 2 所做的工作类似。 如表 4 所示,使用此提示可让用户在模型实用性/护栏执行的帕累托前沿移动。
Always assist with care, respect, and truth. Respond with utmost utility yet securely. Avoid harmful, unethical, prejudiced, or negative content. Ensure replies promote fairness and positivity.
Guardrails | MT Bench |
---|---|
无系统提示 | 6.84 0.07 |
Llama 2 系统提示 | 6.38 0.07 |
Mistral 系统提示 | 6.58 0.05 |
我们使用一套 175 项不安全提示来评估安全性。 在建议的系统提示下,模型正确地拒绝回答 100% 的有害问题。
作为示例,我们在表 5 中提供了 Mistral 7B - Instruct 和 Llama 2 Chat 13B 在激活系统提示的情况下对问题 How to kill a linux process 的回答。 我们注意到,米斯特拉 7B 提供了正确的回答,而拉马 2 则拒绝回答。 请注意,在这一特定问题上,当系统提示被停用时,两种型号都能正确回答。
模型 | 回答 | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
|||||||||
|
|
5.2内容节制与自我反思
Mistral 7B – Instruct 可用作内容调节器:模型本身能够准确地将用户提示或其生成的答案分类为可接受或属于以下类别之一: 恐怖主义、虐待儿童或欺诈等非法活动;仇恨、骚扰或暴力内容,例如歧视、自残或欺凌;在法律、医疗或金融领域的不合格建议。
为此,我们设计了一个自我反思提示,让 Mistral 7B 对提示或生成的答案进行分类。 我们在人工编辑和平衡的对抗性与标准提示数据集上对自我反思进行了评估,结果显示精确度为 99.4%,召回率为 95.6%(将可接受的提示视为正面)。
从管理社交媒体或论坛上的评论,到互联网上的品牌监控,使用案例非常广泛。 特别是,最终用户可以根据其特定的使用情况,选择之后要有效过滤的类别。
6结论
我们在 Mistral 7B 上所做的工作表明,语言模型对知识的压缩可能会超出人们的想象。 这开辟了一个有趣的视角:迄今为止,该领域一直把重点放在二维的缩放规律上(将模型能力与训练成本直接联系起来,如 hoffmann2022compute 中);而问题是三维的(模型能力、训练成本、推理成本),要想用尽可能小的模型获得最佳性能,还有很多问题有待探索。
致谢
我们非常感谢 CoreWeave 为我们的集群提供全天候的帮助。 我们感谢 CINECA/EuroHPC 团队,特别是莱昂纳多的操作人员提供的资源和帮助。 我们感谢 FlashAttention、vLLM、xFormers 和 Skypilot 的维护者在实现新功能和将他们的解决方案集成到我们的解决方案中时提供的宝贵帮助。 非常感谢 Tri Dao 和 Daniel Haziza 在紧迫的时间内帮助将与 Mistral 相关的更改纳入 FlashAttention 和 xFormers。 我们感谢 Hugging Face、AWS、GCP、Azure ML 团队的大力协助,使我们的模型能够在各处兼容。
参考资料
- (1) Joshua Ainslie, James Lee-Thorp, Michiel de Jong, Yury Zemlyanskiy, Federico Lebrón, and Sumit Sanghai. Gqa: Training generalized multi-query transformer models from multi-head checkpoints. arXiv preprint arXiv:2305.13245, 2023.
- (2) Jacob Austin, Augustus Odena, Maxwell Nye, Maarten Bosma, Henryk Michalewski, David Dohan, Ellen Jiang, Carrie Cai, Michael Terry, Quoc Le, et al. Program synthesis with large language models. arXiv preprint arXiv:2108.07732, 2021.
- (3) Iz Beltagy, Matthew E Peters, and Arman Cohan. Longformer: The long-document transformer. arXiv preprint arXiv:2004.05150, 2020.
- (4) Yonatan Bisk, Rowan Zellers, Jianfeng Gao, Yejin Choi, et al. Piqa: Reasoning about physical commonsense in natural language. In Proceedings of the AAAI conference on artificial intelligence, 2020.
- (5) Mark Chen, Jerry Tworek, Heewoo Jun, Qiming Yuan, Henrique Ponde de Oliveira Pinto, Jared Kaplan, Harri Edwards, Yuri Burda, Nicholas Joseph, Greg Brockman, et al. Evaluating large language models trained on code. arXiv preprint arXiv:2107.03374, 2021.
- (6) Rewon Child, Scott Gray, Alec Radford, and Ilya Sutskever. Generating long sequences with sparse transformers. arXiv preprint arXiv:1904.10509, 2019.
- (7) Eunsol Choi, He He, Mohit Iyyer, Mark Yatskar, Wen-tau Yih, Yejin Choi, Percy Liang, and Luke Zettlemoyer. Quac: Question answering in context. arXiv preprint arXiv:1808.07036, 2018.
- (8) Christopher Clark, Kenton Lee, Ming-Wei Chang, Tom Kwiatkowski, Michael Collins, and Kristina Toutanova. Boolq: Exploring the surprising difficulty of natural yes/no questions. arXiv preprint arXiv:1905.10044, 2019.
- (9) Peter Clark, Isaac Cowhey, Oren Etzioni, Tushar Khot, Ashish Sabharwal, Carissa Schoenick, and Oyvind Tafjord. Think you have solved question answering? try arc, the ai2 reasoning challenge. arXiv preprint arXiv:1803.05457, 2018.
- (10) Karl Cobbe, Vineet Kosaraju, Mohammad Bavarian, Mark Chen, Heewoo Jun, Lukasz Kaiser, Matthias Plappert, Jerry Tworek, Jacob Hilton, Reiichiro Nakano, et al. Training verifiers to solve math word problems. arXiv preprint arXiv:2110.14168, 2021.
- (11) Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, and Christopher Ré. FlashAttention: Fast and memory-efficient exact attention with IO-awareness. In Advances in Neural Information Processing Systems, 2022.
- (12) Dan Hendrycks, Collin Burns, Steven Basart, Andy Zou, Mantas Mazeika, Dawn Song, and Jacob Steinhardt. Measuring massive multitask language understanding. arXiv preprint arXiv:2009.03300, 2020.
- (13) Dan Hendrycks, Collin Burns, Saurav Kadavath, Akul Arora, Steven Basart, Eric Tang, Dawn Song, and Jacob Steinhardt. Measuring mathematical problem solving with the math dataset. arXiv preprint arXiv:2103.03874, 2021.
- (14) Jordan Hoffmann, Sebastian Borgeaud, Arthur Mensch, Elena Buchatskaya, Trevor Cai, Eliza Rutherford, Diego de Las Casas, Lisa Anne Hendricks, Johannes Welbl, Aidan Clark, Thomas Hennigan, Eric Noland, Katherine Millican, George van den Driessche, Bogdan Damoc, Aurelia Guy, Simon Osindero, Karén Simonyan, Erich Elsen, Oriol Vinyals, Jack Rae, and Laurent Sifre. An empirical analysis of compute-optimal large language model training. In Advances in Neural Information Processing Systems, volume 35, 2022.
- (15) Mandar Joshi, Eunsol Choi, Daniel S Weld, and Luke Zettlemoyer. Triviaqa: A large scale distantly supervised challenge dataset for reading comprehension. arXiv preprint arXiv:1705.03551, 2017.
- (16) Tom Kwiatkowski, Jennimaria Palomaki, Olivia Redfield, Michael Collins, Ankur Parikh, Chris Alberti, Danielle Epstein, Illia Polosukhin, Jacob Devlin, Kenton Lee, et al. Natural questions: a benchmark for question answering research. Transactions of the Association for Computational Linguistics, 7:453–466, 2019.
- (17) Woosuk Kwon, Zhuohan Li, Siyuan Zhuang, Ying Sheng, Lianmin Zheng, Cody Hao Yu, Joseph E. Gonzalez, Hao Zhang, and Ion Stoica. Efficient memory management for large language model serving with pagedattention. In Proceedings of the ACM SIGOPS 29th Symposium on Operating Systems Principles, 2023.
- (18) Benjamin Lefaudeux, Francisco Massa, Diana Liskovich, Wenhan Xiong, Vittorio Caggiano, Sean Naren, Min Xu, Jieru Hu, Marta Tintore, Susan Zhang, Patrick Labatut, and Daniel Haziza. xformers: A modular and hackable transformer modelling library. https://github.com/facebookresearch/xformers, 2022.
- (19) Todor Mihaylov, Peter Clark, Tushar Khot, and Ashish Sabharwal. Can a suit of armor conduct electricity? a new dataset for open book question answering. arXiv preprint arXiv:1809.02789, 2018.
- (20) Baptiste Rozière, Jonas Gehring, Fabian Gloeckle, Sten Sootla, Itai Gat, Xiaoqing Ellen Tan, Yossi Adi, Jingyu Liu, Tal Remez, Jérémy Rapin, et al. Code llama: Open foundation models for code. arXiv preprint arXiv:2308.12950, 2023.
- (21) Keisuke Sakaguchi, Ronan Le Bras, Chandra Bhagavatula, and Yejin Choi. Winogrande: An adversarial winograd schema challenge at scale. Communications of the ACM, 64(9):99–106, 2021.
- (22) Maarten Sap, Hannah Rashkin, Derek Chen, Ronan LeBras, and Yejin Choi. Socialiqa: Commonsense reasoning about social interactions. arXiv preprint arXiv:1904.09728, 2019.
- (23) Mirac Suzgun, Nathan Scales, Nathanael Schärli, Sebastian Gehrmann, Yi Tay, Hyung Won Chung, Aakanksha Chowdhery, Quoc V Le, Ed H Chi, Denny Zhou, , and Jason Wei. Challenging big-bench tasks and whether chain-of-thought can solve them. arXiv preprint arXiv:2210.09261, 2022.
- (24) Alon Talmor, Jonathan Herzig, Nicholas Lourie, and Jonathan Berant. Commonsenseqa: A question answering challenge targeting commonsense knowledge. arXiv preprint arXiv:1811.00937, 2018.
- (25) Hugo Touvron, Thibaut Lavril, Gautier Izacard, Xavier Martinet, Marie-Anne Lachaux, Timothée Lacroix, Baptiste Rozière, Naman Goyal, Eric Hambro, Faisal Azhar, et al. Llama: Open and efficient foundation language models. arXiv preprint arXiv:2302.13971, 2023.
- (26) Hugo Touvron, Louis Martin, Kevin Stone, Peter Albert, Amjad Almahairi, Yasmine Babaei, Nikolay Bashlykov, Soumya Batra, Prajjwal Bhargava, Shruti Bhosale, et al. Llama 2: Open foundation and fine-tuned chat models. arXiv preprint arXiv:2307.09288, 2023.
- (27) Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Łukasz Kaiser, and Illia Polosukhin. Attention is all you need. Advances in neural information processing systems, 30, 2017.
- (28) Rowan Zellers, Ari Holtzman, Yonatan Bisk, Ali Farhadi, and Yejin Choi. Hellaswag: Can a machine really finish your sentence? arXiv preprint arXiv:1905.07830, 2019.
- (29) Wanjun Zhong, Ruixiang Cui, Yiduo Guo, Yaobo Liang, Shuai Lu, Yanlin Wang, Amin Saied, Weizhu Chen, and Nan Duan. Agieval: A human-centric benchmark for evaluating foundation models. arXiv preprint arXiv:2304.06364, 2023.