模型简介
模型特点
模型能力
使用案例
🚀 Llama-3.1-Nemotron-Nano-4B-v1.1
Llama-3.1-Nemotron-Nano-4B-v1.1是一款大型语言模型,它在模型准确性和效率之间取得了出色的平衡。该模型基于Llama 3.1进行压缩和优化,支持128K的上下文长度,可在单张RTX GPU上运行,适用于推理、人机对话等多种任务。
🚀 快速开始
推理模式控制
推理模式(开启/关闭)通过系统提示进行控制,所有指令应包含在用户提示中。示例如下:
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"
print(pipeline([{"role": "system", "content": f"detailed thinking {thinking}"}, {"role": "user", "content": "Solve x*(sin(x)+2)=0"}]))
运行带有工具调用支持的vLLM服务器
Llama-3.1-Nemotron-Nano-4B-v1.1支持工具调用。以下是一个启动带有工具调用支持的vLLM服务器的shell脚本示例:
#!/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服务器:
$ git clone https://huggingface.co/nvidia/Llama-3.1-Nemotron-Nano-4B-v1.1
$ conda create -n vllm python=3.12 -y
$ conda activate vllm
$ python -m vllm.entrypoints.openai.api_server \
--model Llama-3.1-Nemotron-Nano-4B-v1.1 \
--trust-remote-code \
--seed 1 \
--host "0.0.0.0" \
--port 5000 \
--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 "Llama-3.1-Nemotron-Nano-4B-v1.1/llama_nemotron_nano_toolcall_parser.py" \
--tool-call-parser "llama_nemotron_json" \
--chat-template "Llama-3.1-Nemotron-Nano-4B-v1.1/llama_nemotron_nano_generic_tool_calling.jinja"
启动vLLM服务器后,你可以使用以下Python脚本调用带有工具调用支持的服务器:
>>> 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')]
使用建议
- 推理开启模式:建议将温度设置为
0.6
,Top P设置为0.95
。 - 推理关闭模式:建议使用贪心解码。
✨ 主要特性
- 准确性与效率平衡:在模型准确性和效率之间取得了出色的平衡,适合在单张RTX GPU上运行。
- 多语言支持:支持英语和多种编码语言,也支持其他非英语语言,如德语、法语、意大利语、葡萄牙语、印地语、西班牙语和泰语。
- 长上下文支持:支持128K的上下文长度。
- 推理能力增强:经过多阶段的后训练,增强了推理和非推理能力。
- 工具调用支持:支持工具调用,可用于更复杂的任务。
📦 安装指南
本代码要求transformers
包的版本为4.44.2
或更高。
💻 使用示例
基础用法
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"
print(pipeline([{"role": "system", "content": f"detailed thinking {thinking}"}, {"role": "user", "content": "Solve x*(sin(x)+2)=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
# Thinking can be "on" or "off"
thinking = "off"
pipeline = transformers.pipeline(
"text-generation",
model=model_id,
tokenizer=tokenizer,
max_new_tokens=32768,
do_sample=False,
**model_kwargs
)
print(pipeline([{"role": "system", "content": f"detailed thinking {thinking}"}, {"role": "user", "content": "Solve x*(sin(x)+2)=0"}, {"role":"assistant", "content":"<think>\n</think>"}]))
📚 详细文档
模型概述
Llama-3.1-Nemotron-Nano-4B-v1.1是一款大型语言模型(LLM),它是nvidia/Llama-3.1-Minitron-4B-Width-Base的衍生模型,该基础模型是使用我们的LLM压缩技术从Llama 3.1 8B创建而来,在模型准确性和效率方面有所改进。它是一个推理模型,经过后训练以适应推理、人机对话偏好和各种任务,如RAG和工具调用。
该模型是Llama Nemotron系列的一部分,你可以在以下链接找到该系列的其他模型:
许可证
本模型的使用受NVIDIA开放模型许可证的约束。附加信息:Llama 3.1社区许可协议。本模型基于Llama构建。
模型开发者:NVIDIA
模型日期:2024年8月至2025年5月期间训练
数据新鲜度:预训练数据截止到2023年6月
使用场景
适用于设计AI代理系统、聊天机器人、RAG系统和其他AI应用的开发者,也适用于典型的指令跟随任务。在模型准确性和计算效率之间取得了良好的平衡(模型可在单张RTX GPU上运行,可本地使用)。
发布日期
2025年5月20日
参考资料
- [2408.11796] LLM Pruning and Distillation in Practice: The Minitron Approach
- [2502.00203] Reward-aware Preference Optimization: A Unified Mathematical Framework for Model Alignment
- [2505.00949] Llama-Nemotron: Efficient Reasoning Models
模型架构
属性 | 详情 |
---|---|
模型类型 | 密集解码器Transformer模型 |
网络架构 | Llama 3.1 Minitron Width 4B Base |
预期用途
Llama-3.1-Nemotron-Nano-4B-v1.1是一个通用的推理和聊天模型,旨在用于英语和编码语言,也支持其他非英语语言(德语、法语、意大利语、葡萄牙语、印地语、西班牙语和泰语)。
输入
- 输入类型:文本
- 输入格式:字符串
- 输入参数:一维(1D)
- 其他输入相关属性:上下文长度最大为131,072个标记
输出
- 输出类型:文本
- 输出格式:字符串
- 输出参数:一维(1D)
- 其他输出相关属性:上下文长度最大为131,072个标记
模型版本
1.1(2025年5月20日)
软件集成
- 运行时引擎:NeMo 24.12
- 推荐的硬件微架构兼容性:
- NVIDIA Hopper
- NVIDIA Ampere
推理
- 引擎:Transformers
- 测试硬件:
- BF16:
- 1x RTX 50系列GPU
- 1x RTX 40系列GPU
- 1x RTX 30系列GPU
- 1x H100 - 80GB GPU
- 1x A100 - 80GB GPU
- BF16:
- 首选/支持的操作系统:Linux
训练数据集
后训练管道使用了大量的训练数据,包括手动标注数据和合成数据。多阶段后训练阶段用于改进代码、数学和推理能力的数据是SFT和RL数据的汇编,支持改进原始Llama指令模型的数学、代码、一般推理和指令跟随能力。提示来自公共开放语料库或合成生成,响应由多种模型合成生成,一些提示包含推理开启和关闭模式的响应,以训练模型区分两种模式。
训练数据集的数据收集:混合:自动、人工、合成 训练数据集的数据标注:不适用
评估数据集
我们使用以下数据集评估Llama-3.1-Nemotron-Nano-4B-v1.1。
评估数据集的数据收集:混合:人工/合成 评估数据集的数据标注:混合:人工/合成/自动
评估结果
这些结果包含“推理开启”和“推理关闭”两种模式。我们建议在“推理开启”模式下使用温度=0.6
,top_p=0.95
,在“推理关闭”模式下使用贪心解码。所有评估均使用32k序列长度进行。我们最多运行16次基准测试并取平均分以提高准确性。
⚠️ 重要提示
适用时,将提供提示模板。完成基准测试时,请确保按照提供的提示解析正确的输出格式,以重现以下基准测试结果。
MT - Bench
推理模式 | 分数 |
---|---|
推理关闭 | 7.4 |
推理开启 | 8.0 |
MATH500
推理模式 | pass@1 |
---|---|
推理关闭 | 71.8% |
推理开启 | 96.2% |
用户提示模板:
"Below is a math question. I want you to reason through the steps and then give a final answer. Your final answer should be in \boxed{}.\nQuestion: {question}"
AIME25
推理模式 | pass@1 |
---|---|
推理关闭 | 13.3% |
推理开启 | 46.3% |
用户提示模板:
"Below is a math question. I want you to reason through the steps and then give a final answer. Your final answer should be in \boxed{}.\nQuestion: {question}"
GPQA - D
推理模式 | pass@1 |
---|---|
推理关闭 | 33.8% |
推理开启 | 55.1% |
用户提示模板:
"What is the correct answer to this question: {question}\nChoices:\nA. {option_A}\nB. {option_B}\nC. {option_C}\nD. {option_D}\nLet's think step by step, and put the final answer (should be a single letter A, B, C, or D) into a \boxed{}"
IFEval
推理模式 | 严格:提示 | 严格:指令 |
---|---|---|
推理关闭 | 70.1% | 78.5% |
推理开启 | 75.5% | 82.6% |
BFCL v2 Live
推理模式 | 分数 |
---|---|
推理关闭 | 63.6% |
推理开启 | 67.9% |
用户提示模板:
<AVAILABLE_TOOLS>{functions}</AVAILABLE_TOOLS>
{user_prompt}
MBPP 0 - shot
推理模式 | pass@1 |
---|---|
推理关闭 | 61.9% |
推理开启 | 85.8% |
用户提示模板:
You are an exceptionally intelligent coding assistant that consistently delivers accurate and reliable responses to user instructions.
@@ Instruction
Here is the given problem and test examples:
{prompt}
Please use the python programming language to solve this problem.
Please make sure that your code includes the functions from the test samples and that the input and output formats of these functions match the test samples.
Please return all completed codes in one code block.
This code block should be in the following format:
```python
# Your codes here
## 🔧 技术细节
该模型经过多阶段的后训练过程,包括监督微调阶段和多个强化学习阶段,使用Reward - aware Preference Optimization (RPO)算法进行聊天和指令跟随。最终的模型检查点是在合并最终的SFT和RPO检查点后获得的。
## 📄 许可证
本模型的使用受[NVIDIA开放模型许可证](https://www.nvidia.com/en-us/agreements/enterprise-software/nvidia-open-model-license/)的约束。附加信息:[Llama 3.1社区许可协议](https://www.llama.com/llama3_1/license/)。
## 🌐 伦理考虑
NVIDIA认为可信AI是一项共同责任,我们已经制定了政策和实践,以支持开发广泛的AI应用。当按照我们的服务条款下载或使用时,开发者应与内部模型团队合作,确保该模型满足相关行业和用例的要求,并解决不可预见的产品滥用问题。
有关此模型伦理考虑的更多详细信息,请参阅模型卡片++ [可解释性](explainability.md)、[偏差](bias.md)、[安全与保障](safety.md)和[隐私](privacy.md)子卡片。
请[在此](https://www.nvidia.com/en-us/support/submit-security-vulnerability/)报告安全漏洞或NVIDIA AI相关问题。



