HuggingFace镜像/Qwen3-4B-Instruct-2507
模型介绍
文件和版本
分析

Unsloth Dynamic 2.0 实现了卓越的精度,性能超越其他领先的量化方法。

Qwen3-4B-Instruct-2507

Chat

亮点

我们推出 Qwen3-4B 非思考模式 的更新版本,命名为 Qwen3-4B-Instruct-2507,其主要增强功能如下:

  • 显著提升 通用能力,包括 指令遵循、逻辑推理、文本理解、数学、科学、编码及工具使用。
  • 大幅增加 多语言的长尾知识覆盖。
  • 在 主观和开放式任务 中与用户偏好的 对齐度明显提高,能够生成更有帮助的回复和更高质量的文本。
  • 增强 256K 长上下文理解能力。

image/jpeg

模型概述

Qwen3-4B-Instruct-2507 具有以下特点:

  • 类型:因果语言模型
  • 训练阶段:预训练与后训练
  • 参数数量:40 亿
  • 非嵌入参数数量:36 亿
  • 层数:36
  • 注意力头数量(GQA):Q 为 32 个,KV 为 8 个
  • 上下文长度:原生支持 262,144。

注意:此模型仅支持非思考模式,不会在输出中生成 </think>superscript: 块。同时,不再需要指定 enable_thinking=False。

有关基准测试评估、硬件要求和推理性能等更多详情,请参阅我们的 博客、GitHub 和 文档。

性能表现

GPT-4.1-nano-2025-04-14Qwen3-30B-A3B 无思考Qwen3-4B 无思考Qwen3-4B-Instruct-2507
知识能力
MMLU-Pro62.869.158.069.6
MMLU-Redux80.284.177.384.2
GPQA50.354.841.762.0
SuperGPQA32.242.232.042.8
推理能力
AIME2522.721.619.147.4
HMMT259.712.012.131.0
ZebraLogic14.833.235.280.2
LiveBench 2024112541.559.448.463.0
代码能力
LiveCodeBench v6 (25.02-25.05)31.529.026.435.1
MultiPL-E76.374.666.676.8
Aider-Polyglot9.824.413.812.9
对齐能力
IFEval74.583.781.283.4
Arena-Hard v2*15.924.89.543.4
Creative Writing v372.768.153.683.5
WritingBench66.972.268.583.4
智能体能力
BFCL-v353.058.657.661.9
TAU1-Retail23.538.324.348.7
TAU1-Airline14.018.016.032.0
TAU2-Retail-31.628.140.4
TAU2-Airline-18.012.024.0
TAU2-Telecom-18.417.513.2
多语言能力
MultiIF60.770.861.369.0
MMLU-ProX56.265.149.661.6
INCLUDE58.667.853.860.1
PolyMATH15.623.316.631.1

*:为保证可复现性,我们报告的胜率由 GPT-4.1 评估得出。

快速开始

Qwen3 的代码已集成到最新版的 Hugging Face transformers 中,建议您使用最新版本的 transformers。

若使用 transformers<4.51.0,您将遇到以下错误:

KeyError: 'qwen3'

以下是一个代码片段,展示了如何使用模型根据给定输入生成内容。

from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "Qwen/Qwen3-4B-Instruct-2507"

# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype="auto",
    device_map="auto"
)

# prepare the model input
prompt = "Give me a short introduction to large language model."
messages = [
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

# conduct text completion
generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=16384
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() 

content = tokenizer.decode(output_ids, skip_special_tokens=True)

print("content:", content)

在部署方面,您可以使用sglang>=0.4.6.post1或vllm>=0.8.5来创建兼容OpenAI的API端点:

  • SGLang:
    python -m sglang.launch_server --model-path Qwen/Qwen3-4B-Instruct-2507 --context-length 262144
  • vLLM:
    vllm serve Qwen/Qwen3-4B-Instruct-2507 --max-model-len 262144

注意:如果遇到内存不足(OOM)问题,请考虑将上下文长度减少到更小的值,例如32768。

在本地使用方面,Ollama、LMStudio、MLX-LM、llama.cpp和KTransformers等应用程序也已支持Qwen3。

智能体应用

Qwen3在工具调用能力方面表现出色。我们建议使用Qwen-Agent以充分发挥Qwen3的智能体能力。Qwen-Agent在内部封装了工具调用模板和工具调用解析器,大幅降低了编码复杂度。

要定义可用工具,您可以使用MCP配置文件、使用Qwen-Agent的集成工具,或自行集成其他工具。

from qwen_agent.agents import Assistant

# Define LLM
llm_cfg = {
    'model': 'Qwen3-4B-Instruct-2507',

    # Use a custom endpoint compatible with OpenAI API:
    'model_server': 'http://localhost:8000/v1',  # api_base
    'api_key': 'EMPTY',
}

# Define Tools
tools = [
    {'mcpServers': {  # You can specify the MCP configuration file
            'time': {
                'command': 'uvx',
                'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']
            },
            "fetch": {
                "command": "uvx",
                "args": ["mcp-server-fetch"]
            }
        }
    },
  'code_interpreter',  # Built-in tools
]

# Define Agent
bot = Assistant(llm=llm_cfg, function_list=tools)

# Streaming generation
messages = [{'role': 'user', 'content': 'https://qwenlm.github.io/blog/ Introduce the latest developments of Qwen'}]
for responses in bot.run(messages=messages):
    pass
print(responses)

最佳实践

为获得最佳性能,我们建议采用以下设置:

  1. 采样参数:

    • 建议使用 Temperature=0.7、TopP=0.8、TopK=20 和 MinP=0。
    • 对于支持的框架,可将 presence_penalty 参数在 0 到 2 之间进行调整,以减少无意义的重复。但需注意,较高的参数值偶尔可能导致语言混杂,并略微降低模型性能。
  2. 充足的输出长度:对于大多数查询,建议使用 16,384 个 tokens 的输出长度,这对于指令模型而言已足够。

  3. 标准化输出格式:在进行基准测试时,建议通过提示词来标准化模型的输出。

    • 数学问题:在提示词中包含“请逐步推理,并将最终答案放在 \boxed{} 内。”
    • 多项选择题:在提示词中添加以下 JSON 结构以标准化响应:“请在 answer 字段中仅用选项字母展示您的选择,例如:"answer": "C"。”

引用

如果您发现我们的工作对您有所帮助,欢迎引用。

@misc{qwen3technicalreport,
      title={Qwen3 Technical Report}, 
      author={Qwen Team},
      year={2025},
      eprint={2505.09388},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2505.09388}, 
}