Llama 3.1 发布了!今天我们迎来了 Llama 家族的新成员 Llama 3.1 进入 Hugging Face 平台。我们很高兴与 Meta 合作,确保在 Hugging Face 生态系统中实现最佳集成。Hub 上现有八个开源权重模型 (3 个基础模型和 5 个微调模型)。
Llama 3.1 有三种规格: 8B 适合在消费者级 GPU 上进行高效部署和开发,70B 适合大规模 AI 原生应用,而 405B 则适用于合成数据、大语言模型 (LLM) 作为评判者或蒸馏。这三个规格都提供基础版和指令调优版。
除了六个生成模型,Meta 还发布了两个新模型: Llama Guard 3 和 Prompt Guard。Prompt Guard 是一个小型分类器,可以检测提示注入和越狱。Llama Guard 3 是一个保护模型,能够分类 LLM 输入和生成的内容。
此次发布的一些功能和集成包括:
Llama 3.1 为什么令人兴奋?在前代产品的基础上,Llama 3.1 增加了一些关键新功能:
让我们深入了解这些新功能!
Llama 3.1 版本引入了基于 Llama 3 架构的六个新开源 LLM 模型。它们有三种规格: 8B、70B 和 405B 参数,每种都有基础版 (预训练) 和指令调优版。所有版本都支持 128K token 的上下文长度和 8 种语言,包括英语、德语、法语、意大利语、葡萄牙语、印地语、西班牙语和泰语。Llama 3.1 继续使用分组查询注意力 (GQA),这是一种高效的表示方式,有助于处理更长的上下文。
除了这六个语言模型,还发布了 Llama Guard 3 和 Prompt Guard。
与 Llama 3 相比,Llama 3.1 的新特点是指令模型在工具调用方面进行了微调,适用于智能体用例。内置了两个工具 (搜索,使用 Wolfram Alpha 进行数学推理),可以扩展为自定义 JSON 功能。
Llama 3.1 模型在定制 GPU 集群上训练了超过 15 万亿 token,总计 39.3M GPU 小时 (8B 1.46M,70B 7.0M,405B 30.84M)。我们不知道训练数据集混合的具体细节,但我们猜测它在多语言方面有更广泛的策划。Llama 3.1 Instruct 已优化用于指令跟随,并在公开可用的指令数据集以及超过 2500 万合成生成的示例上进行监督微调 (SFT) 和人类反馈的强化学习 (RLHF)。Meta 开发了基于 LLM 的分类器,以在数据混合创建过程中过滤和策划高质量的提示和响应。
关于许可条款,Llama 3.1 具有非常相似的许可证,但有一个关键区别: 它允许使用模型输出来改进其他 LLM。这意味着合成数据生成和蒸馏是允许的,即使是不同的模型!这对 405B 模型尤其重要,如后面所讨论的。许可证允许再分发、微调和创建衍生作品,仍然要求派生模型在其名称的开头包括 “Llama”,并且任何衍生作品或服务必须提及 “Built with Llama”。有关完整详情,请确保阅读。
Llama 3.1 带来了令人兴奋的进步。然而,运行它需要仔细考虑硬件资源。我们分解了三种模型规格在训练和推理中的内存需求。
对于推理,内存需求取决于模型规格和权重的精度。以下是不同配置所需的近似内存:
| 模型规格 | FP16 | FP8 | INT4 |
| 8B | 16 GB | 8 GB | 4 GB |
| 70B | 140 GB | 70 GB | 35 GB |
| 405B | 810 GB | 405 GB | 203 GB |
注意: 上面引用的数字表示仅加载模型检查点所需的 GPU VRAM。它们不包括内核或 CUDA 图形的 torch 保留空间。
例如,一个 H100 节点 (8x H100) 有约 640GB 的 VRAM,因此 405B 模型需要在多节点设置中运行或以较低精度 (例如 FP8) 运行,这是推荐的方法。
请记住,较低精度 (例如 INT4) 可能会导致一些精度损失,但可以显著减少内存需求并提高推理速度。除了模型权重外,您还需要将 KV 缓存保持在内存中。它包含模型上下文中所有 token 的键和值,以便在生成新 token 时不需要重新计算。特别是当利用可用的长上下文长度时,它变得至关重要。在 FP16 中,KV 缓存内存需求如下:
| 模型规格 | 1k token | 16k token | 128k token |
| 8B | 0.125 GB | 1.95 GB | 15.62 GB |
| 70B | 0.313 GB | 4.88 GB | 39.06 GB |
| 405B | 0.984 GB | 15.38 | 123.05 GB |
特别是对于小规格模型,当接近上下文长度上限时,缓存使用的内存与权重一样多。
以下表格概述了使用不同技术训练 Llama 3.1 模型的大致内存需求:
| 模型规格 | 1k token | 16k token | 128k token |
| 8B | 0.125 GB | 1.95 GB | 15.62 GB |
| 70B | 0.313 GB | 4.88 GB | 39.06 GB |
| 405B | 0.984 GB | 15.38 | 123.05 GB |
注意: 这些是估计值,可能会根据具体实现细节和优化情况有所不同。
注意: 我们目前正在新的上单独评估 Llama 3.1,并将在今天晚些时候更新此部分。以下是 Meta 官方评估的摘录。
| 类别 | 基准 | 样本数 | 指标 | Llama 3 8B | Llama 3.1 8B | Llama 3 70B | Llama 3.1 70B | Llama 3.1 405B |
| 综合 | MMLU | 5 | 宏观平均/字符准确率 | 66.7 | 66.7 | 79.5 | 79.3 | 85.2 |
| MMLU PRO (CoT) | 5 | 宏观平均/字符准确率 | 36.2 | 37.1 | 55.0 | 53.8 | 61.6 | |
| AGIEval 英语 | 3-5 | 平均/字符准确率 | 47.1 | 47.8 | 63.0 | 64.6 | 71.6 | |
| CommonSenseQA | 7 | 字符准确率 | 72.6 | 75.0 | 83.8 | 84.1 | 85.8 | |
| Winogrande | 5 | 字符准确率 | - | 60.5 | - | 83.3 | 86.7 | |
| BIG-Bench Hard (CoT) | 3 | 平均/完全匹配 | 61.1 | 64.2 | 81.3 | 81.6 | 85.9 | |
| ARC-Challenge | 25 | 字符准确率 | 79.4 | 79.7 | 93.1 | 92.9 | 96.1 | |
| 知识推理 | TriviaQA-Wiki | 5 | 完全匹配 | 78.5 | 77.6 | 89.7 | 89.8 | 91.8 |
| SQuAD | 1 | 完全匹配 | 76.4 | 77.0 | 85.6 | 81.8 | 89.3 | |
| 阅读理解 | QuAC (F1) | 1 | F1 | 44.4 | 44.9 | 51.1 | 51.1 | 53.6 |
| BoolQ | 0 | 字符准确率 | 75.7 | 75.0 | 79.0 | 79.4 | 80.0 | |
| DROP (F1) | 3 | F1 | 58.4 | 59.5 | 79.7 | 79.6 | 84.8 |
Llama 3.1 需要进行少量建模更新,以有效处理 RoPE 缩放。使用 Transformers,您可以使用新的 Llama 3.1 模型,并利用 Hugging Face 生态系统中的所有工具。确保使用最新的 transformers 版本:
pipinstall"transformers>=4.43"--upgrade
几个细节:
<|eot_id|> 结尾,但我们还必须在找到常规 EOS token 时停止生成。我们可以通过在 eos_token_id 参数中提供终止符列表来提前停止生成。temperature 和 top_p )。我们还没有时间进行广泛测试,请随意探索!以下代码段显示了如何使用 meta-llama/Meta-Llama-3.1-8B-Instruct 。它大约需要 16 GB 的 VRAM,适合许多消费者级 GPU。相同的代码段适用于 meta-llama/Meta-Llama-3.1-70B-Instruct ,在 140GB VRAM 和 meta-llama/Meta-Llama-3.1-405B-Instruct (需要 810GB VRAM),使其成为生产用例的非常有趣的模型。可以通过以 8 位或 4 位模式加载进一步减少内存消耗。
fromtransformersimportpipeline
importtorch
model_id="meta-llama/Meta-Llama-3.1-8B-Instruct"
pipe=pipeline(
"text-generation",
model=model_id,
model_kwargs={"torch_dtype":torch.bfloat16},
device="cuda",
)
messages=[
{"role":"user","content":"Whoareyou?Please,answerinpirate-speak."},
]
outputs=pipe(
messages,
max_new_tokens=256,
do_sample=False,
)
assistant_response=outputs[0]["generated_text"][-1]["content"]
print(assistant_response)
#Arrrr,mehearty!Yerlookin'ferabito'informationaboutmeself,eh?Alrightthen,matey!Ibealanguage-generatin'swashbuckler,adigitalbuccaneerwithapenchantferspinnin'wordsintogolddoubloonso'knowledge!Menamebe...(dramaticpause)...Assistant!Aye,thatbemename,andIbeheretohelpyenavigatethesevenseaso'questionsandfindthehiddentreasureo'answers!Sohoistthesailsandsetcourseferadventure,mehearty!Whatbeyerfirstquestion?
您还可以自动量化模型,以 8 位甚至 4 位模式加载,使用 bitsandbytes。4 位加载大 70B 版本大约需要 34 GB 的内存运行。这是如何以 4 位模式加载生成管道:
pipeline=pipeline(
"text-generation",
model=model_id,
model_kwargs={
"torch_dtype":torch.bfloat16,
"quantization_config":{"load_in_4bit":True}
},
)
有关使用 transformers 模型的更多详细信息,请查看。
注意: Transformers 处理所有棘手的提示模板问题,如果您想了解更多关于提示的信息,请查看下一部分。
基础模型没有提示格式。像其他基础模型一样,它们可以用于继续输入序列并进行合理的延续或零样本/少样本推理。它们也是微调您自己用例的绝佳基础。
指令版本支持具有 4 个角色的对话格式:
system 和 user 提示中提供的上下文。指令版本使用以下对话结构进行简单对话:
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
{{system_prompt}}<|eot_id|><|start_header_id|>user<|end_header_id|>
{{user_msg_1}}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
{{model_answer_1}}<|eot_id|>
Llama 3.1 指令模型现在支持工具调用,包括三个内置工具 (brave_search、wolfram_alpha 和 code_interpreter) 和通过 JSON 函数调用的自定义工具调用。内置工具使用 Python 语法。生成 Python 代码以进行函数调用是代码解释器工具的一部分,必须在系统提示中使用 Environment 关键字启用,如下所示。
包括 "Environment: ipython" 会打开代码解释器模式,模型可以生成它期望被执行的 Python 代码。助手响应的消息体以特殊标记 <|python_tag|> 开头,以 <|eom_id|> 结尾,而不是标准 <|eot_id|>。后者表示回合结束,而前者表示继续多步推理。
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
Environment:ipython
Tools:brave_search,wolfram_alpha
CuttingKnowledgeDate:01March2023
Today'sDate:13July2024
YouareahelpfulAssistant.<|eot_id|><|start_header_id|>user<|end_header_id|>
WeatherinMenloPark,California<|eot_id|><|start_header_id|>assistant<|end_header_id|>
此时模型的响应将包括调用其中一个支持的工具 (在本例中为 brave_search ) 的 Python 代码:
<|python_tag|>brave_search.call(query="currentweatherinMenloPark,California")<|eom_id|>
执行调用的响应然后发送回模型以检索最终响应。为了简洁,以下内容将附加到前面代码段中显示的消息:
<|python_tag|>brave_search.call(query="MenloParkCaliforniaweather")<|eom_id|><|start_header_id|>ipython<|end_header_id|>
{"query":"MenloParkCaliforniaweather","top_k":[{"title":"10-DayWeatherForecastforWestMenloPark,CA-TheWeatherChannel|weather.com","url":"https://weather.com/weather/tenday/l/West+Menlo+Park+CA?canonicalCityId=b2375713aa1943aad7d1a13a85e1c0adad13c1b10563b2bbaad70734dc61cf11","description":"Bepreparedwiththemostaccurate10-dayforecastforWest<strong>Menlo</strong><strong>
ark</strong>,CAwithhighs,lows,chanceofprecipitationfromThe<strong>Weather</strong>Channeland<strong>Weather</strong>.com","type":"search_result"},....}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
LLM 的最终响应将是:
ThecurrentweatherinMenloPark,Californiaismostlysunnywithahighof77°Fandalowof56°F.<|eot_id|>
Llama 3.1 指令支持从单个用户消息中调用自定义函数。以下提示提供了如何从模型输出调用自定义函数的示例。在自定义函数调用中,模型输出 <|eot_id|> 而不是 <|eom_id|> 。需要调整系统提示以告知模型如何处理函数调用输出。
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
Youareahelpfulassistantwithtoolcallingcapabilities.Whenyoureceiveatoolcallresponse,usetheoutputtoformatananswertotheorginaluserquestion.<|eot_id|><|start_header_id|>user<|end_header_id|>
Giventhefollowingfunctions,pleaserespondwithaJSONforafunctioncallwithitsproperargumentsthatbestanswersthegivenprompt.
Respondintheformat{"name":functionname,"parameters":dictionaryofargumentnameanditsvalue}.Donotusevariables.
{
"type":"function",
"function":{
"name":"get_current_conditions",
"description":"Getthecurrentweatherconditionsforaspecificlocation",
"parameters":{
"type":"object",
"properties":{
"location":{
"type":"string",
"description":"Thecityandstate,e.g.,SanFrancisco,CA"
},
"unit":{
"type":"string",
"enum":["Celsius","Fahrenheit"],
"description":"Thetemperatureunittouse.Inferthisfromtheuser'slocation."
}
},
"required":["location","unit"]
}
}
}
Question:whatistheweatherlikeinMenloPark?<|eot_id|><|start_header_id|>assitant<|end_header_id|>
{"name":"get_current_conditions","parameters":{"location":"MenloPark,CA","unit":"Fahrenheit"}}<|eot_id|><|start_header_id|>ipython<|end_header_id|>
当我们从选定的工具检索输出时,我们将其传回模型,使用相同的 <|python_tag|> 分隔符。<|python_tag|> 不意味着使用 Python。它仅用于表示任何工具的输出开始。
<|python_tag|>{
"tool_call_id":"get_current_conditions"
"output":"CloudsgivingwaytosunHi:76°Tonight:Mainlyclearearly,thenareasoflowcloudsformingLo:56°"
}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
TheweatherinMenloParkiscurrentlycloudywithahighof76°andalowof56°,withclearskiesexpectedtonight.<|eot_id|>
这种格式必须精确复制才能有效使用。transformers 中可用的聊天模板使其易于正确格式化提示。
您可以在以下演示中试验三种指令模型:
整个堆栈都是开源的。Hugging Chat 由和提供支持。
Meta 创建了,精度损失最小。为实现这一目标,FP8 量化仅应用于模型的主要线性运算符,例如 FFNs 的门和上升及下降投影 (涵盖 75% 的推理 FLOPs)。我们共同努力,确保此 FP8 量化检查点在社区中兼容 (transformers, TGI, VLLM)。
此外,我们使用 AutoAWQ 和 AutoGPTQ 创建了 INT4 的 AWQ 和 GPTQ 量化变体。对于 AWQ,所有线性层都使用 GEMM 内核进行量化,将零点量化到 4 位,组大小为 128; 对于 GPTQ,相同的设置仅使用 GPTQ 内核。我们确保 INT4 检查点与 transformers 和 TGI 兼容,包括 Marlin 内核支持,以加快 TGI 中 GPTQ 量化的推理速度。
可用的 Llama 3.1 405B 的量化权重:
还包含 70B 和 8B 版本的量化检查点。
,托管 Llama 3.1 8B Instruct、Llama 3.1 70B Instruct 和 Llama 3.1 405B Instruct AWQ,由提供支持。所有版本都支持 Messages API,因此与 OpenAI 客户端库兼容,包括 LangChain 和 LlamaIndex。
注意: 使用 pip install "huggingface_hub>=0.24.1" 更新到最新的 huggingface_hub 版本。
fromhuggingface_hubimportInferenceClient
#初始化客户端,指向一个可用的模型
client=InferenceClient()
chat_completion=client.chat.completions.create(
model="meta-llama/Meta-Llama-3.1-405B-Instruct-FP8",
messages=[
{"role":"system","content":"Youareahelpfulandhonestprogrammingassistant."},
{"role":"user","content":"IsRustbetterthanPython?"},
],
stream=True,
max_tokens=500
)
#迭代并打印流
formessageinchat_completion:
print(message.choices[0].delta.content,end="")
有关使用 Messages API 的更多详细信息,请查看。
您可以在 Hugging Face 的上部署 Llama 3.1,它使用 Text Generation Inference 作为后端。Text Generation Inference 是 Hugging Face 开发的生产就绪推理容器,支持 FP8、连续批处理、token 流、张量并行,以便在多个 GPU 上快速推理。要部署 Llama 3.1,请转到并点击部署 -> 推理端点小部件:
推荐在 1x NVIDIA A10G 或 L4 GPU 上运行
推荐在 4x NVIDIA A100 或量化为 AWQ/GPTQ 在 2x A100 上运行
推荐在 8x NVIDIA H100 上以 FP 运行或量化为/在 8x A100 上运行
fromhuggingface_hubimportInferenceClient
#初始化客户端,指向一个可用的模型
client=InferenceClient(
base_url="<ENDPOINT_URL>",
)
#创建一个聊天完成
chat_completion=client.chat.completions.create(
model="ENDPOINT",
messages=[
{"role":"system","content":"Youareahelpfulandhonestprogrammingassistant."},
{"role":"user","content":"IsRustbetterthanPython?"},
],
stream=True,
max_tokens=500
)
#迭代并打印流
formessageinchat_completion:
print(message.choices[0].delta.content,end="")
注意: 我们目前正在与我们的合作伙伴 AWS、Google Cloud、Microsoft Azure 和 DELL 合作,将 Llama 3.1 8B、70B 和 405B 添加到 Amazon SageMaker、Google Kubernetes Engine、Vertex AI Model Catalog、Azure AI Studio、DELL Enterprise Hub。我们将在容器可用时更新此部分 - 您可以。
在本节中,我们将查看 Hugging Face 生态系统中可用的工具,以便在消费者级 GPU 上高效训练 Llama 3.1。下面是一个示例命令,用于在 OpenAssistant 的上微调 Llama 3.1 8B。我们使用 4 位量化和来节省内存,以针对所有注意力块的线性层。
首先,安装最新版本的 ? TRL 并克隆 repo 以访问:
pipinstall"transformers>=4.43"--upgrade
pipinstall--upgradebitsandbytes
pipinstall--ugpradepeft
pipinstallgit+https://github.com/huggingface/trl
gitclonehttps://github.com/huggingface/trl
cdtrl
然后你可以运行脚本:
python\
examples/scripts/sft.py\
--model_namemeta-llama/Meta-Llama-3.1-8B\
--dataset_nameOpenAssistant/oasst_top1_2023-08-25\
--dataset_text_field="text"\
--per_device_train_batch_size1\
--per_device_eval_batch_size1\
--gradient_accumulation_steps4\
--learning_rate2e-4\
--report_to"none"\
--bf16\
--max_seq_length1024\
--lora_r16--lora_alpha32\
--lora_target_modulesq_projk_projv_projo_proj\
--load_in_4bit\
--use_peft\
--attn_implementation"flash_attention_2"\
--logging_steps=10\
--gradient_checkpointing\
--output_dirllama31
如果您有更多的 GPU,可以使用 DeepSpeed 和 ZeRO Stage 3 运行训练:
acceleratelaunch--config_file=examples/accelerate_configs/deepspeed_zero3.yaml\
examples/scripts/sft.py\
--model_namemeta-llama/Meta-Llama-3.1-8B\
--dataset_nameOpenAssistant/oasst_top1_2023-08-25\
--dataset_text_field="text"\
--per_devicetrainbatch_size1\
--per_device_eval_batch_size1\
--gradient_accumulation_steps4\
--learning_rate2e-5\
--report_towandb\
--bf16\
--max_seq_length1024\
--attn_implementationeager\
--logging_steps=10\
--gradient_checkpointing\
--output_dirmodels/llama
Llama 3.1 许可证的一个重大变化是,它允许使用模型输出来改进其他 LLM,这意味着您可以使用 Llama 3.1 模型生成合成数据集,并使用它们来微调更小、更专业的模型。
让我们看一个示例,如何使用,一个用于生成合成数据的开源框架,生成一个偏好数据集。该数据集可用于使用 TRL 提供的偏好优化方法 (如 DPO 或 KTO) 微调模型。
首先安装最新的 distilabel 版本,包括 hf-inference-endpoints 额外组件,使用 pip 如下:
pipinstall“distilabel[hf-inference-endpoints]”--upgrade
然后定义一个管道:
请参阅下面的代码以定义管道,或使用此自行运行并探索生成的数据集。
fromdistilabel.llmsimportInferenceEndpointsLLM
fromdistilabel.pipelineimportPipeline
fromdistilabel.stepsimportLoadDataFromHub,CombineColumns
fromdistilabel.steps.tasksimportTextGeneration,UltraFeedback
llama70B=InferenceEndpointsLLM(
model_id="meta-llama/Meta-Llama-3.1-70B-Instruct"
)
llama405B=InferenceEndpointsLLM(
model_id="meta-llama/Meta-Llama-3.1-405B-Instruct-FP8"
)
withPipeline(name="synthetic-data-with-llama3")aspipeline:
#加载带有提示的数据集
load_dataset=LoadDataFromHub(
repo_id="argilla/10Kprompts-mini"
)
#为每个提示生成两个响应
generate=[
TextGeneration(llm=llama70B),
TextGeneration(llm=llama405B)
]
#将响应组合到一个列中
combine=CombineColumns(
columns=["generation","model_name"],
output_columns=["generations","model_names"]
)
#使用405BLLM-as-a-judge对响应进行评分
rate=UltraFeedback(aspect="overall-rating",llm=llama405B)
#定义管道
load_dataset>>generate>>combine>>rate
if__name__=="__main__":
distiset=pipeline.run()
接下来是什么?除了上述示例, distilabel 还提供了使用 LLM 在广泛的场景和主题中生成合成数据的令人兴奋的方法。它包括当前 SOTA 文献中的实现,用于任务如使用 LLM-as-a-judge 方法评估输出、进化指令、数据过滤以及定义自定义组件。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |