小巧玲珑 Qwen3-0.6B

Qwen3-0.6B

Qwen3 亮点

Qwen3 是 Qwen 系列中最新一代的大型语言模型,提供了一系列密集型和混合专家(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面取得了突破性进展,具有以下关键特性:

  • 在同一模型内无缝切换思维模式(适用于复杂逻辑推理、数学和编程)和非思维模式(适用于高效、通用对话),确保在各种场景下的最佳性能。
  • 显著增强了其推理能力,在数学、代码生成和常识逻辑推理方面超越了之前的 QwQ(思维模式)和 Qwen2.5 指令模型(非思维模式)。
  • 更优的人类偏好对齐,在创意写作、角色扮演、多轮对话和指令执行方面表现出色,提供更加自然、引人入胜和沉浸式的对话体验。
  • 在代理能力方面的专长,能够在思维和非思维模式下与外部工具精确集成,并在复杂的基于代理的任务中达到开源模型中的领先性能。
  • 支持 100 多种语言和方言,具备强大的多语言指令执行翻译能力。

模型概述

Qwen3-0.6B 具有以下特点:

  • 类型:因果语言模型
  • 训练阶段:预训练 & 后训练
  • 参数数量:0.6B
  • 非嵌入参数数量:0.44B
  • 层数:28
  • 注意力头数(GQA):Q 为 16,KV 为 8
  • 上下文长度:32,768

更多详细信息,包括基准评估、硬件要求和推理性能,请参阅我们的 博客GitHub文档

[!TIP] 如果您遇到严重的无限重复问题,请参考 最佳实践 部分以获取最佳采样参数,并将 presence_penalty 设置为 1.5。

快速开始

Qwen3 的代码已经在最新的 Hugging Face transformers 中可用,建议您使用 transformers 的最新版本。

使用 transformers<4.51.0 时,您会遇到以下错误:

KeyError: 'qwen3'

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

from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "Qwen/Qwen3-0.6B"

# 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,
    enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

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

# parsing thinking content
try:
    # rindex finding 151668 (</think>)
    index = len(output_ids) - output_ids[::-1].index(151668)
except ValueError:
    index = 0

thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")

print("thinking content:", thinking_content)
print("content:", content)

对于部署,您可以使用 sglang>=0.4.6.post1vllm>=0.8.5 来创建一个与 OpenAI 兼容的 API 端点:

  • SGLang:
    python -m sglang.launch_server --model-path Qwen/Qwen3-0.6B --reasoning-parser qwen3
  • vLLM:
    vllm serve Qwen/Qwen3-0.6B --enable-reasoning --reasoning-parser deepseek_r1

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

切换思考与非思考模式

[!TIP] 在 SGLang 和 vLLM 创建的 API 中也可以使用 enable_thinking 开关。请参考我们的文档以获取 SGLangvLLM 用户的相关信息。

enable_thinking=True

默认情况下,Qwen3 启用了思考能力,类似于 QwQ-32B。这意味着模型将利用其推理能力来提高生成回复的质量。例如,当显式设置 enable_thinking=True 或在 tokenizer.apply_chat_template 中保持默认值时,模型将进入思考模式。

text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,
    enable_thinking=True  # True is the default value for enable_thinking
)

在这种模式下,模型会生成被 `

© 版权声明
THE END
喜欢就支持一下吧
点赞11赞赏
评论 抢沙发

请登录后发表评论

    暂无评论内容