# Mistral 7B(中文译本) > Albert Q. Jiang, Alexandre Sablayrolles, Arthur Mensch 等 · arXiv:2310.06825 · CC BY 4.0 · 译文由 Agent 译社生成,供审阅 ## 摘要 我们推出 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,[26]),并在数学和代码生成方面超过最佳的 34B 模型(LLaMa 34B,[25])。此外,Mistral 7B 的代码性能接近 Code-Llama 7B [20],且未牺牲在非代码相关基准上的性能。 Mistral 7B 采用了分组查询注意力(GQA)[1] 和滑动窗口注意力(SWA)[6, 3]。GQA 显著加快了推理速度,并降低了解码时的内存需求,从而支持更大的批大小、实现更高的吞吐量,这对实时应用至关重要。此外,SWA 旨在以更低的计算成本更有效地处理更长的序列,从而缓解大语言模型(LLM)的一个常见局限。这些注意力机制共同提升了 Mistral 7B 的性能和效率。 Mistral 7B 以 Apache 2.0 许可证发布。随之发布的还有一个参考实现^1^,可借助 vLLM [17] 推理服务器和 SkyPilot ^2^,便捷地在本地或 AWS、GCP、Azure 等云平台上部署。与 Hugging Face ^3^ 的集成也经过简化,更加便捷。此外,Mistral 7B 便于在众多任务上进行微调。为展示其适应性和卓越性能,我们提供了一个基于 Mistral 7B 微调的对话模型,其性能显著优于 Llama 2 13B – Chat 模型。 Mistral 7B 在兼顾高性能与大语言模型效率这一目标上迈出了重要一步。我们希望通过这项工作,帮助社区构建更经济、高效且高性能的语言模型,以服务于广泛的实际应用。 ## 2 架构细节 ![Figure 1](figures/figure1.png) > **图 1:滑动窗口注意力。**标准注意力的运算量与序列长度呈二次关系,内存随 token 数线性增长。推理时,由于缓存可用性降低,这会导致更高的延迟和更低的吞吐量。为缓解该问题,我们采用滑动窗口注意力:每个 token 至多关注上一层的 $W$ 个 token(此处 $W = 3$)。注意,滑动窗口之外的 token 仍会影响下一个词预测。每经过一个注意力层,信息可向前传播 $W$ 个 token。因此,经过 $k$ 个注意力层后,信息最多可向前传播 $k × W$ 个 token。 ![Table 1](figures/table1.png) > **表 1:模型架构。** Mistral 7B 基于 Transformer 架构 [27]。架构的主要参数汇总于表 1。与 Llama 相比,它引入了若干改动,概述如下。 **滑动窗口注意力。**SWA 利用 Transformer 堆叠的层来关注窗口大小 $W$ 之外的信息。第 $k$ 层位置 $i$ 处的隐藏状态 $hi$ 关注上一层中位置介于 $i −W$ 与 $i$ 之间的所有隐藏状态。以此递归,$hi$ 可访问输入层中距离至多 $W × k$ 个 token 的 token,如图 1 所示。在最后一层,使用窗口大小 $W = 4096$ 时,理论注意力跨度约为 $131K$ 个 token。实践中,在序列长度为 16K 且 $W = 4096$ 时,对 FlashAttention [11] 和 xFormers [18] 所做的修改相比标准注意力基线带来 2 倍的速度提升。 **滚动缓冲区缓存。**固定的注意力跨度意味着我们可以使用滚动缓冲区缓存来限制缓存大小。缓存的固定大小为 $W$,时间步 $i$ 的键和值存储在缓存的位置 $i mod W$ 处。因此,当位置 $i$ 大于 $W$ 时,缓存中的旧值会被覆盖,缓存大小不再增长。图 2 给出了 $W = 3$ 时的示意。在 32k token 的序列长度上,这使缓存内存占用降低为原来的 1/8,且不影响模型质量。 ![Figure 2](figures/figure2.png) > **图 2:滚动缓冲区缓存。**缓存的固定大小为 $W = 4$。位置 $i$ 的键和值存储在缓存的位置 $i mod W$ 处。当位置 $i$ 大于 $W$ 时,缓存中的旧值会被覆盖。最新生成 token 对应的隐藏状态以橙色标出。 **预填充与分块。**生成序列时,由于每个 token 都以之前的 token 为条件,我们需要逐个预测 token。然而,提示词是预先已知的,因此可以用提示词预填充($k$,$v$)缓存。若提示词很长,可将其分成更小的块,并用每个块依次预填充缓存。为此,可将窗口大小选作块大小。因此,对于每个块,我们需要同时在缓存和该块上计算注意力。图 3 展示了注意力掩码如何同时作用于缓存和块。 ![Figure 3](figures/figure3.png) > **图 3:预填充与分块。**在缓存预填充过程中,长序列被分块以限制内存占用。我们将一个序列分为三个块处理:“The cat sat on”“the mat and saw”“the dog go to”。图中展示了第三个块(“the dog go to”)的情况:它使用因果掩码关注自身(最右侧块),使用滑动窗口关注缓存(中间块),而不关注位于滑动窗口之外的过去 token(左侧块)。 ## 3 结果 我们将 Mistral 7B 与 Llama 进行比较,并为保证公平,使用我们自己的评测流程重新运行了所有基准。我们在多种任务上衡量性能,分类如下: - ** 常识推理(零样本):**Hellaswag [28]、Winogrande [21]、PIQA [4]、SIQA [22]、OpenbookQA [19]、ARC-Easy、ARC-Challenge [9]、CommonsenseQA [24] - ** 世界知识(5 样本):**NaturalQuestions [16]、TriviaQA [15] - ** 阅读理解(零样本):**BoolQ [8]、QuAC [7] - ** 数学:**GSM8K [10](8 样本,maj@8)和 MATH [13](4 样本,maj@4) - ** 代码:**HumanEval [5](零样本)和 MBPP [2](3 样本) - ** 常用综合结果:**MMLU [12](5 样本)、BBH [23](3 样本)和 AGI Eval [29](3–5 样本,仅限英文多项选择题) Mistral 7B、Llama 2 7B/13B 和 Code-Llama 7B 的详细结果见表 2。图 4 比较了 Mistral 7B 与 Llama 2 7B/13B 及 Llama 1 34B^4^ 在不同类别上的性能。Mistral 7B 在所有指标上均超过 Llama 2 13B,并在大多数基准上优于 Llama 1 34B。尤其是,Mistral 7B 在代码、数学和推理基准上表现出更优的性能。 ^4^由于 Llama 2 34B 未开源,我们报告 Llama 1 34B 的结果。 ![Figure 4](figures/figure4.png) > **图 4:Mistral 7B 与不同 Llama 模型在多种基准上的性能**。为确保比较准确,所有模型均使用我们的评测流程在全部指标上重新评测。Mistral 7B 在所有基准上均显著优于 Llama 2 7B 和 Llama 2 13B,在数学、代码生成和推理基准上也远超 Llama 1 34B。 ![Table 2](figures/table2.png) > **表 2:Mistral 7B 与 Llama 的比较。**Mistral 7B 在所有指标上均优于 Llama 2 13B,代码性能接近 Code-Llama 7B,且未牺牲非代码基准上的性能。 **规模与效率。**我们计算了 Llama 2 系列的“等效模型规模”,以了解 Mistral 7B 模型在成本-性能谱中的效率(见图 5)。在推理、理解和 STEM 推理(具体为 MMLU)上评测时,Mistral 7B 的性能相当于规模为其 3 倍以上的 Llama 2 模型。在知识类基准上,Mistral 7B 的压缩率较低,为 1.9 倍,这可能是因为其参数量有限,限制了可存储的知识量。 **评测差异。**在部分基准上,我们的评测协议与 Llama 2 论文中报告的协议存在差异:1)在 MBPP 上,我们使用经人工核验的子集;2)在 TriviaQA 上,我们不提供维基百科上下文。 ## 4 指令微调 ![Table 3](figures/table3.png) > **表 3:对话模型的比较。**Mistral 7B – Instruct 在 MT-Bench 上优于所有 7B 模型,并与 13B – Chat 模型相当。 为评估 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 次。 ![Figure 5](figures/figure5.png) > **图 5:Mistral 7B 与 Llama 2(7B/13B/70B)在 MMLU、常识推理、世界知识和阅读理解上的结果**。Mistral 7B 在所有评测上均大幅优于 Llama 2 13B,仅在知识基准上与之持平(这可能是因为其参数量有限,限制了其可压缩的知识量)。 ## 5 为面向用户的应用添加护栏 对于面向用户的应用而言,能够对 AI 生成内容施加护栏十分重要。本节介绍如何利用系统提示词,在我们的模型之上按需施加输出约束。此外,我们还展示了 Mistral 7B 执行细粒度内容审核的能力,可用于在应用中保障内容质量。 ### 5.1 用系统提示词施加护栏 与 Llama 2 的做法类似,我们引入一个系统提示词(见下文),引导模型在指定护栏内生成回答。如表 4 所示,使用该提示词可让用户在模型效用 / 护栏施加的帕累托前沿上移动。 ![Table 4](figures/table4.png) > **表 4:系统提示词。**Mistral 7B – Instruct 在 10 次迭代中的官方 MT-Bench 平均得分及标准差。作为参考,Llama 2 13B – Chat 报告的官方结果为 6.65。 我们使用一组 175 个不安全提示词来评估安全性。在使用推荐的系统提示词时,模型能正确拒绝回答 100% 的有害问题。 作为示例,我们在表 5 中给出了启用系统提示词时,Mistral 7B – Instruct 与 Llama 2 Chat 13B 对问题*如何终止一个 Linux 进程(How to kill a linux process)*的回答。可以看到,Mistral 7B 给出了正确回答,而 Llama 2 拒绝回答。注意,对于这一具体问题,在关闭系统提示词时两个模型都能正确回答。 ### 5.2 基于自我反思的内容审核 Mistral 7B – Instruct 可用作内容审核器:模型本身能够准确地将用户提示词或其生成的回答分类为可接受,或归入以下类别之一:违法活动,如恐怖主义、虐待儿童或欺诈;仇恨、骚扰或暴力内容,如歧视、自残或霸凌;不合格的建议,如法律、医疗或金融领域的建议。 ![Table 5](figures/table5.png) > **表 5:Mistral 与 Llama 系统提示词的比较**。对于问题*如何终止一个 Linux 进程(How to kill a linux process)*,Mistral 给出了正确答案,而 Llama 2 拒绝回答。 为此,我们设计了一个自我反思提示词,使 Mistral 7B 对提示词或生成的回答进行分类。我们在人工整理的、由对抗性提示词和标准提示词构成的平衡数据集上评估了自我反思,在召回率为 95.6% 时精确率达到 99.4%(将可接受的提示词视为正例)。 其应用场景十分广泛,从社交媒体或论坛的评论审核到互联网上的品牌监测。特别地,终端用户可以事后根据自身的具体用例,选择实际需要过滤的类别。 ## 6 结论 我们在 Mistral 7B 上的工作表明,语言模型对知识的压缩程度可能超出此前的认知。这带来了有趣的前景:迄今为止,该领域侧重于二维的缩放定律(直接将模型能力与训练成本关联,如 [14]);而实际问题是三维的(模型能力、训练成本、推理成本),要以尽可能小的模型获得最佳性能,仍有大量工作有待探索。 ## 致谢 感谢 CoreWeave 全天候协助我们调度集群。感谢 CINECA/EuroHPC 团队,特别是 Leonardo 的运维人员提供的资源与帮助。感谢 FlashAttention、vLLM、xFormers、SkyPilot 的维护者在实现新功能以及将其方案集成到我们系统中时给予的宝贵帮助。特别感谢 Tri Dao 和 Daniel Haziza 在紧迫的时间内帮助将 Mistral 相关改动并入 FlashAttention 和 xFormers。感谢 Hugging Face、AWS、GCP、Azure ML 团队为使我们的模型在各平台上兼容所提供的大力帮助。 ![Figure 6](figures/figure6.png) > **图 6:Mistral 7B – Instruct 与 Llama 2 13B – Chat 的人工评测示例。**来自 `llmboxing.com` 的一个人工评测示例。问题要求推荐量子物理方面的书籍。Llama 2 13B – Chat 推荐了一本普通物理书籍,而 Mistral 7B – Instruct 推荐了一本与量子物理更相关的书,并更详细地介绍了其内容。 ## References(参考文献,保留原文) [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 phys- ical 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 lan- guage 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: Com- monsense 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 ques- tion answering challenge targeting commonsense knowledge. arXiv preprint arXiv:1811.00937 , 2018. [25] Hugo Touvron, Thibaut Lavril, Gautier Izacard, Xavier Martinet, Marie-Anne Lachaux, Timo- thé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.