使用 Llama Nemotron Nano 4B 超越边缘人工智能,实现高精度推理
生成“思考”令牌的推理模型已被证明对于各种代理式人工智能应用至关重要。今年,我们在这个领域取得了显著进展,模型更小但精度更高。对功能强大且轻量级模型的日益增长的需求促使了推理小型语言模型(SLM)的出现,这些模型能够在内存和计算受限的情况下高效运行。
我们很高兴推出 Llama 3.1 Nemotron Nano 4B v1.1,Llama Nemotron 系列的最新、最紧凑的成员。Nano 4B v1.1 专为精度和效率而设计,为轻量级小型语言模型设定了新标准。尽管其尺寸更小,但它比其他最先进的开放模型(高达 80 亿参数)具有更高的精度和高达 50% 的吞吐量,使其成为需要速度和精度的广泛应用的理想选择。
🚀 推进设备端代理式人工智能的推理 SLM
Nano 4B 拥有可控的系统 1 和系统 2 推理能力,使其能够更有效地执行推理和非推理任务。它针对低成本推理进行了优化,使其成为寻求在有限资源下扩展人工智能的开发人员和企业的理想选择。
Nano 4B v1.1 仅有 40 亿个参数,结构紧凑,足以部署在 NVIDIA Jetson 和 NVIDIA RTX GPU 上的边缘设备。这可实现更快的响应时间、增强的数据隐私和更大的部署灵活性,同时保持较低的推理成本。无论您是在云端还是边缘构建人工智能驱动的应用程序,Nano 模型都能以显著的效率提供强大的性能。
如上图所示,Llama 3.1 Nemotron Nano 4B v1.1 在推理、编码和数学的大多数基准测试中超越了其他领先的 8B 或更少参数的开放模型,在定量、竞争和逻辑推理任务中表现出卓越的准确性。有关实时基准测试,请访问此链接。
凭借其更小的尺寸,Nano 4B v1.1 可以提供比流行的开源 8B 模型高 1.5 倍的吞吐量。
🧪 Llama 3.1 Nemotron Nano 4B v1.1 的训练方法
Llama 3.1 Nemotron Nano 4B v1.1 是从 Llama 3.1 8B 创建的,但它经历了漫长的推理和对齐训练过程。
Nano 4B v1.1 是 Llama 3.1 Minitron 4B Width Base 的微调模型,该模型使用 NVIDIA 的 Minitron LLM 压缩技术 从 Llama 3.1 8B Base 创建。Llama 3.1 Minitron 4B Width Base 的详细信息(包括效率改进)可在此博客中找到。
该模型经历了 SFT 阶段,该阶段使用了跨数学、编码、科学、安全和工具调用等多个领域的混合推理开启/关闭数据集。其中一些数据集最近作为 Llama-Nemotron-Post-Training-Dataset 的一部分开源。
对于 SFT,数据集使用 NeMo-Skills 库进行了序列打包,通过将较短的样本打包成单个批次来提高训练速度高达 10 倍,从而消除了序列填充。
该模型已使用 NeMo-Aligner 库进行了进一步微调,用于多个 RPO 阶段,其中使用在策略数据进行更稳定和有效的训练。为了改善长上下文,应用了 RoPe 缩放,随后进行了轻量级推理精度恢复 RPO 轮次。
有关更多详细信息,请参阅 Llama-Nemotron 技术报告和技术博客,以了解 Llama-Nemotron 模型系列,其中涵盖了 Llama 3.1 Nemotron Nano 8B v1 模型先前版本的训练方法和评估结果。
💻 如何使用 Llama 3.1 Nemotron Nano 4B v1.1
以下是推理开启和关闭用例的 HF 代码示例。您需要做的就是在系统提示符的开头添加短语“detailed thinking on”。您还可以向系统提示符添加额外内容(例如工具规范)。建议插入 \n\n
来分隔部分。
对于“推理开启”模式,建议的生成配置是 temperature=0.6
和 top_p=0.95
。强烈建议在此模型的“推理开启”模式下避免使用 temperature=0
。
import torch
import transformers
model_id = "nvidia/Llama-3.1-Nemotron-Nano-4B-v1.1"
model_kwargs = {"torch_dtype": torch.bfloat16, "device_map": "auto"}
tokenizer = transformers.AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token_id = tokenizer.eos_token_id
pipeline = transformers.pipeline(
"text-generation",
model=model_id,
tokenizer=tokenizer,
max_new_tokens=32768,
temperature=0.6,
top_p=0.95,
**model_kwargs
)
# Thinking can be "on" or "off"
thinking = "on"
# Users can optionally add additional system prompt
system_prompt = "You're a helpful assistant."
print(pipeline([{"role": "system", "content": f"detailed thinking {thinking}\n\n{system_prompt}"}, {"role": "user", "content": "Solve x*(sin(x)+2)=0"}]))
要使用推理关闭模式,您只需在系统提示符中使用“detailed thinking off”。请注意,对于“推理关闭模式”,建议的生成配置是 temperature=0
,并且 max_new_tokens
可以设置得更低(例如 8192),而不是在推理开启模式下。
pipeline = transformers.pipeline(
"text-generation",
model=model_id,
tokenizer=tokenizer,
max_new_tokens=8192,
temperature=0.0,
do_sample=False,
**model_kwargs
)
# Thinking can be "on" or "off"
thinking = "off”
# Users can optionally add additional system prompt
system_prompt = "You're a helpful assistant."
print(pipeline([{"role": "system", "content": f"detailed thinking {thinking}\n\n{system_prompt}"}, {"role": "user", "content": "Solve x*(sin(x)+2)=0"}]))
💻 使用 Llama 3.1 Nemotron Nano 4B v1.1 进行函数调用
您还可以使用模型启动支持函数调用的 vLLM 服务器。vLLM 原生函数调用所需的聊天模板 Jinja 文件 (llama_nemotron_nano_generic_tool_calling.jinja
) 和工具调用解析器 (llama_nemotron_nano_toolcall_parser.py
) 都托管在 HF 存储库中。此示例只是克隆存储库以使用下载的解析器和函数调用聊天模板。您必须使用 vllm/vllm openai:v0.6.6
或更高版本才能支持此模型。
要试用函数调用示例,请下载 Jupyter Notebook 并观看此 YouTube 教程。
#!/bin/bash
CWD=$(pwd)
PORT=5000
git clone https://huggingface.co/nvidia/Llama-3.1-Nemotron-Nano-4B-v1.1
docker run -it --rm \
--runtime=nvidia \
--gpus all \
--shm-size=16GB \
-p ${PORT}:${PORT} \
-v ${CWD}:${CWD} \
vllm/vllm-openai:v0.6.6 \
--model $CWD/Llama-3.1-Nemotron-Nano-4B-v1.1 \
--trust-remote-code \
--seed 1 \
--host "0.0.0.0" \
--port $PORT \
--served-model-name "Llama-Nemotron-Nano-4B-v1.1" \
--tensor-parallel-size 1 \
--max-model-len 131072 \
--gpu-memory-utilization 0.95 \
--enforce-eager \
--enable-auto-tool-choice \
--tool-parser-plugin "${CWD}/Llama-3.1-Nemotron-Nano-4B-v1.1/llama_nemotron_nano_toolcall_parser.py" \
--tool-call-parser "llama_nemotron_json" \
--chat-template "${CWD}/Llama-3.1-Nemotron-Nano-4B-v1.1/llama_nemotron_nano_generic_tool_calling.jinja"
启动 vLLM 服务器后,您可以使用 OpenAI 客户端进行函数调用。例如:
from openai import OpenAI
client = OpenAI(
base_url="http://0.0.0.0:5000/v1",
api_key="dummy",
)
completion = client.chat.completions.create(
model="Llama-Nemotron-Nano-4B-v1.1",
messages=[
{"role": "system", "content": "detailed thinking on"},
{"role": "user", "content": "My bill is $100. What will be the amount for 18% tip?"},
],
tools=[
{"type": "function", "function": {"name": "calculate_tip", "parameters": {"type": "object", "properties": {"bill_total": {"type": "integer", "description": "The total amount of the bill"}, "tip_percentage": {"type": "integer", "description": "The percentage of tip to be applied"}}, "required": ["bill_total", "tip_percentage"]}}},
{"type": "function", "function": {"name": "convert_currency", "parameters": {"type": "object", "properties": {"amount": {"type": "integer", "description": "The amount to be converted"}, "from_currency": {"type": "string", "description": "The currency code to convert from"}, "to_currency": {"type": "string", "description": "The currency code to convert to"}}, "required": ["from_currency", "amount", "to_currency"]}}},
],
)
completion.choices[0].message.content
# '<think>\nOkay, let\'s see. The user has a bill of $100 and wants to know the amount of a 18% tip. So, I need to calculate the tip amount. The available tools include calculate_tip, which requires bill_total and tip_percentage. The parameters are both integers. The bill_total is 100, and the tip percentage is 18. So, the function should multiply 100 by 18% and return 18.0. But wait, maybe the user wants the total including the tip? The question says "the amount for 18% tip," which could be interpreted as the tip amount itself. Since the function is called calculate_tip, it\'s likely that it\'s designed to compute the tip, not the total. So, using calculate_tip with bill_total=100 and tip_percentage=18 should give the correct result. The other function, convert_currency, isn\'t relevant here. So, I should call calculate_tip with those values.\n</think>\n\n'
completion.choices[0].message.tool_calls
# [ChatCompletionMessageToolCall(id='chatcmpl-tool-2972d86817344edc9c1e0f9cd398e999', function=Function(arguments='{"bill_total": 100, "tip_percentage": 18}', name='calculate_tip'), type='function')]
🚀 立即试用!
要开始使用,请查看 Llama Nemotron Nano 4B v1.1 Hugging Face 存储库并下载模型检查点。
NVIDIA 还将 Llama Nemotron Nano 打包为 NVIDIA NIM 推理微服务,针对高吞吐量和低延迟进行了优化。NVIDIA NIM 利用行业标准 API,在本地或云端提供无缝、可扩展的 AI 推理。
在此处试用 Llama Nemotron Nano 4B v1.1 NIM:https://build.nvidia.com/nvidia/llama-3_1-nemotron-nano-4b-v1_1。