柏拉图数据智能。
垂直搜索和人工智能。

Mistral AI 的 Mistral 7B 基础模型现已在 Amazon SageMaker JumpStart 中提供 | 亚马逊网络服务

日期:

今天,我们很高兴地宣布,由 Mistral AI 开发的 Mistral 7B 基础模型可供客户通过 亚马逊SageMaker JumpStart 一键部署以运行推理。 Mistral 7B 拥有 7 亿个参数,可以轻松定制并快速部署。 您可以使用 SageMaker JumpStart 尝试此模型,SageMaker JumpStart 是一个机器学习 (ML) 中心,提供对算法和模型的访问,以便您可以快速开始使用 ML。 在这篇文章中,我们将介绍如何发现和部署 Mistral 7B 模型。

什么是米斯特拉尔 7B

Mistral 7B是Mistral AI开发的基础模型,支持英文文本和代码生成能力。 它支持各种用例,例如文本摘要、分类、文本完成和代码完成。 为了演示模型的轻松可定制性,Mistral AI 还发布了用于聊天用例的 Mistral 7B Instruct 模型,并使用各种公开可用的对话数据集进行了微调。

Mistral 7B 是一个 Transformer 模型,使用分组查询注意力和滑动窗口注意力来实现更快的推理(低延迟)并处理更长的序列。 组查询注意力是一种结合了多查询和多头注意力的架构,以实现接近多头注意力的输出质量和与多查询注意力相当的速度。 滑动窗口注意力使用变压器的堆叠层来关注过去超出窗口大小的内容,以增加上下文长度。 Mistral 7B 具有 8,000 个令牌的上下文长度,具有低延迟和高吞吐量,并且与较大的模型替代方案相比具有强大的性能,在 7B 模型大小下提供较低的内存要求。 该模型是在许可下提供的 Apache 2.0许可证,使用不受限制。

什么是 SageMaker JumpStart

借助 SageMaker JumpStart,机器学习从业者可以从不断增长的性能最佳基础模型列表中进行选择。 机器学习从业者可以将基础模型部署到专用的 亚马逊SageMaker 网络隔离环境中的实例,并使用 SageMaker 自定义模型进行模型训练和部署。

您现在只需点击几下即可发现并部署 Mistral 7B 亚马逊SageMaker Studio 或通过 SageMaker Python SDK 以编程方式,使您能够利用 SageMaker 功能导出模型性能和 MLOps 控制,例如 Amazon SageMaker管道, Amazon SageMaker调试器,或容器日志。 该模型部署在 AWS 安全环境中并受您的 VPC 控制,有助于确保数据安全。

探索型号

您可以通过 SageMaker Studio UI 和 SageMaker Python SDK 中的 SageMaker JumpStart 访问 Mistral 7B 基础模型。 在本节中,我们将介绍如何在 SageMaker Studio 中发现模型。

SageMaker Studio 是一个集成开发环境 (IDE),提供基于 Web 的单一可视化界面,您可以在其中访问专用工具来执行所有 ML 开发步骤,从准备数据到构建、训练和部署 ML 模型。 有关如何开始和设置 SageMaker Studio 的更多详细信息,请参阅 亚马逊SageMaker Studio.

在 SageMaker Studio 中,您可以访问 SageMaker JumpStart,其中包含预训练的模型、笔记本和预构建的解决方案,位于 预构建和自动化解决方案.

在 SageMaker JumpStart 登录页面中,您可以浏览解决方案、模型、笔记本和其他资源。 您可以在以下位置找到米斯特拉尔 7B: 基础模型:文本生成 旋转木马

您还可以通过选择找到其他型号变体 探索所有文本模型 或搜索“米斯特拉尔”。

您可以选择模型卡来查看有关模型的详细信息,例如许可证、用于训练的数据以及如何使用。 您还会发现两个按钮, 部署打开笔记本,这将帮助您使用该模型(以下屏幕截图显示了 部署 选项)。

部署模型

当您选择时开始部署 部署。 或者,您可以通过选择时显示的示例笔记本进行部署 打开笔记本。 该示例笔记本提供了有关如何部署模型进行推理和清理资源的端到端指导。

要使用笔记本进行部署,我们首先选择 Mistral 7B 型号,由 model_id。 您可以使用以下代码在 SageMaker 上部署任何选定的模型:

from sagemaker.jumpstart.model import JumpStartModel model = JumpStartModel(model_id="huggingface-llm-mistral-7b-instruct")
predictor = model.deploy()

这会使用默认配置在 SageMaker 上部署模型,包括默认实例类型 (ml.g5.2xlarge) 和默认 VPC 配置。 您可以通过在中指定非默认值来更改这些配置 快速启动模型。 部署后,您可以通过 SageMaker 预测器对部署的终端节点运行推理:

payload = {"inputs": "<s>[INST] Hello! [/INST]"}
predictor.predict(payload)

优化部署配置

Mistral 模型使用文本生成推理(TGI 版本 1.1)模型服务。 使用TGI深度学习容器(DLC)部署模型时,您可以配置各种 启动器参数 部署端点时通过环境变量。 为了支持 Mistral 8,000B 模型的 7 个令牌上下文长度,SageMaker JumpStart 默认配置了其中一些参数:我们设置 MAX_INPUT_LENGTHMAX_TOTAL_TOKENS 分别为8191和8192。 您可以通过检查模型对象来查看完整列表:

print(model.env)

默认情况下,SageMaker JumpStart 不会通过环境变量限制并发用户 MAX_CONCURRENT_REQUESTS 小于 TGI 默认值 128。原因是某些用户可能具有负载上下文长度较小的典型工作负载并希望高并发性。 请注意,SageMaker TGI DLC 通过滚动批处理支持多个并发用户。 为应用程序部署端点时,您可能会考虑是否应该限制 MAX_TOTAL_TOKENS or MAX_CONCURRENT_REQUESTS 在部署之前为您的工作负载提供最佳性能:

model.env["MAX_CONCURRENT_REQUESTS"] = "4"

在这里,我们展示了模型性能对于典型端点工作负载可能有何不同。 在下表中,您可以观察到小型查询(128 个输入单词和 128 个输出令牌)在大量并发用户下性能相当好,达到每秒 1,000 个令牌的令牌吞吐量。 然而,随着输入字数增加到 512 个输入字,端点的批处理能力(允许同时处理的并发请求数)饱和,导致吞吐量停滞不前,并且从 16 个并发用户左右开始,延迟显着下降。 最后,当多个并发用户同时查询具有大型输入上下文(例如 6,400 个单词)的终端节点时,这种吞吐量稳定状态发生得相对较快,以至于您的 SageMaker 账户将开始遇到超载请求的 60 秒响应超时限制。

. 吞吐量(令牌/秒)
并发用户 1 2 4 8 16 32 64 128
模型 实例类型 输入单词 输出标记 .
米斯特拉尔-7b-指令 ml.g5.2xlarge 128 128 30 54 89 166 287 499 793 1030
512 128 29 50 80 140 210 315 383 458
6400 128 17 25 30 35 - - - -
. p50 延迟(毫秒/令牌)
并发用户 1 2 4 8 16 32 64 128
模型 实例类型 输入单词 输出标记 .
米斯特拉尔-7b-指令 ml.g5.2xlarge 128 128 32 33 34 36 41 46 59 88
512 128 34 36 39 43 54 71 112 213
6400 128 57 71 98 154 - - - -

推理和示例提示

米斯特拉尔7B

您可以像任何标准文本生成模型一样与基本 Mistral 7B 模型进行交互,其中模型处理输入序列并输出序列中预测的下一个单词。 以下是一个多镜头学习的简单示例,其中模型提供了多个示例,并且最终示例响应是根据前面示例的上下文知识生成的:

> Input
Tweet: "I get sad when my phone battery dies."
Sentiment: Negative
###
Tweet: "My day has been :+1:"
Sentiment: Positive
###
Tweet: "This is the link to the article"
Sentiment: Neutral
###
Tweet: "This new music video was incredibile"
Sentiment: > Output Positive

米斯特拉尔 7B 指令

Mistral 的指令调整版本接受格式化指令,其中对话角色必须以用户提示开始,并在用户和助手之间交替。 一个简单的用户提示可能如下所示:

<s>[INST] {user_prompt} [/INST]

多轮提示如下所示:

<s>[INST] {user_prompt_1} [/INST] {assistant_response_1} </s><s>[INST] {user_prompt_1} [/INST]

无论对话中有多少轮,这种模式都会重复。

在以下部分中,我们将探讨使用 Mistral 7B Instruct 模型的一些示例。

知识检索

下面是一个知识检索的例子:

> Input
<s>[INST] Which country has the most natural lakes? Answer with only the country name. [/INST] > Output
1. Canada

大上下文问答

为了演示如何使用此模型来支持大输入上下文长度,以下示例嵌入了 Robert Sullivan 的标题为“Rats”的段落(参考),从 MCAS 10 年级英语语言艺术阅读理解测试输入提示指令,并向模型询问有关文本的定向问题:

> Input
<s>[INST] A rat is a rodent, the most common mammal in the world. Rattus norvegicus is one of the approximately four hundred different kinds of rodents, and it is known by many names, each of which describes a trait or a perceived trait or sometimes a habitat: the earth rat, the roving rat, the barn rat, the fi eld rat, the migratory rat, the house rat, the sewer rat, the water rat, the wharf rat, the alley rat, the gray rat, the brown rat, and the common rat. The average brown rat is large and stocky; it grows to be approximately sixteen inches long from its nose to its tail—the size of a large adult human male’s foot—and weighs about a pound, though brown rats have been measured by scientists and exterminators at twenty inches and up to two pounds. The brown rat is sometimes confused with the black rat, or Rattus rattus, which is smaller and once inhabited New York City and all of the cities of America but, since Rattus norvegicus pushed it out, is now relegated to a minor role. (The two species still survive alongside each other in some Southern coastal cities and on the West Coast, in places like Los Angeles, for example, where the black rat lives in attics and palm trees.) The black rat is always a very dark gray, almost black, and the brown rat is gray or brown, with a belly that can be light gray, yellow, or even a pure-seeming white. One spring, beneath the Brooklyn Bridge, I saw a red-haired brown rat that had been run over by a car. Both pet rats and laboratory rats are Rattus norvegicus, but they are not wild and therefore, I would emphasize, not the subject of this book. Sometimes pet rats are called fancy rats. But if anyone has picked up this book to learn about fancy rats, then they should put this book down right away; none of the rats mentioned herein are at all fancy. Rats are nocturnal, and out in the night the brown rat’s eyes are small and black and shiny; when a fl ashlight shines into them in the dark, the eyes of a rat light up like the eyes of a deer. Though it forages* in darkness, the brown rat has poor eyesight. It makes up for this with, fi rst of all, an excellent sense of smell. . . . They have an excellent sense of taste, detecting the most minute amounts of poison, down to one part per million. A brown rat has strong feet, the two front paws each equipped with four clawlike nails, the rear paws even longer and stronger. It can run and climb with squirrel-like agility. It is an excellent swimmer, surviving in rivers and bays, in sewer streams and toilet bowls. The brown rat’s teeth are yellow, the front two incisors being especially long and sharp, like buckteeth. When the brown rat bites, its front two teeth spread apart. When it gnaws, a fl ap of skin plugs the space behind its incisors. Hence, when the rat gnaws on indigestible materials—concrete or steel, for example—the shavings don’t go down the rat’s throat and kill it. Its incisors grow at a rate of fi ve inches per year. Rats always gnaw, and no one is certain why—there are few modern rat studies. It is sometimes erroneously stated that the rat gnaws solely to limit the length of its incisors, which would otherwise grow out of its head, but this is not the case: the incisors wear down naturally. In terms of hardness, the brown rat’s teeth are stronger than aluminum, copper, lead, and iron. They are comparable to steel. With the alligator-like structure of their jaws, rats can exert a biting pressure of up to seven thousand pounds per square inch. Rats, like mice, seem to be attracted to wires—to utility wires, computer wires, wires in vehicles, in addition to gas and water pipes. One rat expert theorizes that wires may be attractive to rats because of their resemblance to vines and the stalks of plants; cables are the vines of the city. By one estimate, 26 percent of all electric-cable breaks and 18 percent of all phone-cable disruptions are caused by rats. According to one study, as many as 25 percent of all fi res of unknown origin are rat-caused. Rats chew electrical cables. Sitting in a nest of tattered rags and newspapers, in the fl oorboards of an old tenement, a rat gnaws the head of a match—the lightning in the city forest. When it is not gnawing or feeding on trash, the brown rat digs. Anywhere there is dirt in a city, brown rats are likely to be digging—in parks, in fl owerbeds, in little dirt-poor backyards. They dig holes to enter buildings and to make nests. Rat nests can be in the floorboards of apartments, in the waste-stuffed corners of subway stations, in sewers, or beneath old furniture in basements. “Cluttered and unkempt alleyways in cities provide ideal rat habitat, especially those alleyways associated with food-serving establishments,” writes Robert Corrigan in Rodent Control, a pest control manual. “Alley rats can forage safely within the shadows created by the alleyway, as well as quickly retreat to the safety of cover in these narrow channels.” Often, rats burrow under concrete sidewalk slabs. Entrance to a typical under-the-sidewalk rat’s nest is gained through a two-inch-wide hole—their skeletons collapse and they can squeeze into a hole as small as three quarters of an inch wide, the average width of their skull. This tunnel then travels about a foot down to where it widens into a nest or den. The den is lined with soft debris, often shredded plastic garbage or shopping bags, but sometimes even grasses or plants; some rat nests have been found stuffed with the gnawed shavings of the wood-based, spring-loaded snap traps that are used in attempts to kill them. The back of the den then narrows into a long tunnel that opens up on another hole back on the street. This second hole is called a bolt hole; it is an emergency exit. A bolt hole is typically covered lightly with dirt or trash—camoufl age. Sometimes there are networks of burrows, which can stretch beneath a few concrete squares on a sidewalk, or a number of backyards, or even an entire city block—when Rattus norvegicus fi rst came to Selkirk, England, in 1776, there were so many burrows that people feared the town might sink. Rats can also nest in basements, sewers, manholes, abandoned pipes of any kind, fl oorboards, or any hole or depression. “Often,” Robert Corrigan writes, “‘city rats’ will live unbeknownst to people right beneath their feet.” Rats also inhabit subways, as most people in New York City and any city with a subway system are well aware. Every once in a while, there are reports of rats boarding trains, but for the most part rats stay on the tracks—subway workers I have talked to refer to rats as “track rabbits.” People tend to think that the subways are fi lled with rats, but in fact rats are not everywhere in the system; they live in the subways according to the supply of discarded human food and sewer leaks. Sometimes, rats use the subway purely for nesting purposes; they fi nd ways through the walls of the subway stations leading from the tracks to the restaurants and stores on the street—the vibrations of subway trains tend to create rat-size cracks and holes. Many subway rats tend to live near stations that are themselves near fast-food restaurants. At the various subway stations near Herald Square, for example, people come down from the streets and throw the food that they have not eaten onto the tracks, along with newspapers and soda bottles and, I have noticed, thousands of nolonger-charged AA batteries, waiting to leak acid. The rats eat freely from the waste and sit at the side of the little streams of creamy brown sewery water that fl ows between the rails. They sip the water the way rats do, either with their front paws or by scooping it up with their incisors. What is the most likely reason the author states, "cables are the vines of the city"? [/INST] > Output
1. The author states that rats are attracted to wires, such as utility wires, computer wires, and vehicle wires, which may be due to their resemblance to vines and the stalks of plants.

数学与推理

米斯特拉尔模型还报告了数学准确性方面的优势。 Mistral 可以提供诸如以下数学逻辑的理解:

> Input
<s>[INST] I bought an ice cream for 6 kids. Each cone was $1.25 and I paid with a $10 bill. How many dollars did I get back? Explain first before answering. [/INST] > Output
1. First, let's figure out how much the ice cream cost in total. Since each cone was $1.25 and there were 6 kids, the total cost would be: Total cost = Number of cones * Cost per cone
Total cost = 6 * $1.25
Total cost = $7.50 2. Next, we'll subtract the total cost from the amount paid ($10) to find out how much money was returned: Money returned = Amount paid - Total cost
Money returned = $10 - $7.50
Money returned = $2.50 So, you got back $2.50 after buying the ice cream for the 6 kids.

编码

以下是编码提示的示例:

> Input
<s>[INST] In Bash, how do I list all text files in the current directory (excluding subdirectories) that have been modified in the last month? [/INST] > Output
1. Use the `find` command to search for text files in the current directory (excluding subdirectories) that have been modified in the last month. The `-type f` option specifies that we only want to search for files, not directories. The `-name "*.txt"` option specifies that we only want to search for files with the `.txt` extension. The `-mtime -30` option specifies that we only want to search for files that have been modified in the last 30 days (1 month). ```bash
find . -type f -name "*.txt" -mtime -30
``` This command will list all text files in the current directory (excluding subdirectories) that have been modified in the last month.

清理

运行完笔记本后,请确保删除在此过程中创建的所有资源,以便停止计费。 使用以下代码:

predictor.delete_model()
predictor.delete_endpoint()

结论

在这篇文章中,我们向您展示了如何在 SageMaker Studio 中开始使用 Mistral 7B 并部署模型进行推理。 由于基础模型是预先训练的,因此它们可以帮助降低培训和基础设施成本,并支持针对您的用例进行定制。 访问 亚马逊SageMaker JumpStart 现在开始。

资源


作者简介

凯尔乌尔里希博士 是 Amazon SageMaker JumpStart 团队的应用科学家。 他的研究兴趣包括可扩展的机器学习算法、计算机视觉、时间序列、贝叶斯非参数和高斯过程。 他拥有杜克大学博士学位,并在 NeurIPS、Cell 和 Neuron 上发表过论文。

Ashish Khetan 博士 是 Amazon SageMaker JumpStart 的高级应用科学家,帮助开发机器学习算法。 他在伊利诺伊大学厄巴纳-香槟分校获得博士学位。 他是机器学习和统计推断领域的活跃研究员,并在 NeurIPS、ICML、ICLR、JMLR、ACL 和 EMNLP 会议上发表了多篇论文。

Vivek Singh 是 Amazon SageMaker JumpStart 的产品经理。 他专注于帮助客户使用 SageMaker JumpStart,以简化和加速他们构建生成式 AI 应用程序的 ML 之旅。

罗伊·阿莱拉 是位于德国慕尼黑的 AWS 的高级 AI/ML 专家解决方案架构师。 Roy 帮助 AWS 客户(从小型初创公司到大型企业)在 AWS 上高效地训练和部署大型语言模型。 Roy 热衷于计算优化问题和提高人工智能工作负载的性能。

现货图片

最新情报

现货图片

在线答疑

你好呀! 我怎么帮你?