AIFeed

欢迎 Qwen 4:端侧前沿智能

Translation(model translation, not the source's own text)

欢迎 Qwen 4:端侧的前沿智能

阿里巴巴 Qwen 团队打造的 Qwen4-35B-A3B 已在 Hugging Face 上线!

继 Qwen3.8-Flash-Next 中的架构预览之后,Qwen4-35B-A3B 将 Qwen 完整的下一代混合设计投入生产。它是一个原生多模态的混合专家(MoE)模型,主参数约 35B,每 token 激活参数约 3.8B,逐层 N-gram 嵌入(PLE)参数约 20.5B(总计约 56B)。它支持原生文本、图像和视频输入,具备 1M token 上下文窗口,采用 Apache-2.0 许可。

我们与 Qwen 团队合作,在 transformers、llama.cpp、Llama App、vLLM、SGLang、MLX、Inference Providers 和 Inference Endpoints 上实现了 day-0 支持。本文介绍该架构的显著特性、如何运行它,以及如何针对你自己的任务对其进行微调。

  • 架构:多模态 MoE,包含 256 个路由专家,参数约 35B、激活约 3.8B,另有约 20.5B 逐层 n-gram 嵌入。
  • 模态与上下文:原生文本、图像和视频输入 → 文本输出,具备 1M token 原生上下文窗口。
  • 检查点:BF16、FP8、OMLX、GGUF 量化版本
  • 推理:面向 GGUF 和 OMLX 的 Multi-Token Prediction (MTP) drafter,可调节的 reasoning_effort 级别(“xhigh”、“high”、“low”、“off”)。
  • Day-0 生态支持:transformers、llama.cpp / Llama App、vLLM、SGLang、TRL、Inference Providers 和 Inference Endpoints。

Qwen4 有什么新变化?

Qwen4-35B-A3B 架构,展示文本与视觉输入、稀疏注意力、门控残差连接和专家路由。"
Qwen4-35B-A3B 架构,展示文本与视觉输入、稀疏注意力、门控残差连接和专家路由。"

Qwen4-35B-A3B 是一个具备 1M token 上下文窗口的多模态混合专家模型。文本解码器将相同的四层模式重复十次。前三层使用 Gated DeltaNet,它在读取每个 token 时更新固定大小的记忆。第四层使用 Qwen Sparse Attention (QSA),让当前 token 能够回看上下文中任意位置的单个 token。为决定回看哪里,一个小型索引器将 token 按每四个一组进行分组并对其键取平均。对于每个查询,它最多选取 512 个组。随后注意力层读取这些组中的单个 token,以及最后一个未完成组中的任何 token。这样它最多可选择 2,048 个 token,并在末尾至多再多三个。

Qwen4-35B-A3B 还改变了信息在层间流动的方式。每个 token 以四份其 2,048 维嵌入的副本开始。这些副本成为四条残差流。在每个注意力或 MoE 块之前,学习到的门控将这些流混合成单一输入。八个选中的专家和共享专家处理该输入并生成一个合并输出。随后每条流将同一输出的一个缩放副本加到自身状态上。一个学习到的门控为每条流单独设置缩放比例。模型最大的组成部分之一位于第二个解码器层。该层有一个逐层嵌入表(PLE),包含 20.5B 参数。每个 token 仅读取 PLE 表中的 16 行。八次查找编码最后两个 token,另外八次查找编码最后三个 token(当前 token 及其前面的两个 token)。该层将这 16 个嵌入合并为一个,对它们对每条残差流的贡献进行门控,并使用一个短卷积混入邻近模式。

该模型采用视觉 transformer 来编码图像和视频。ViT 将图像读入 16 × 16 的 patch,并将每 2 × 2 一组空间 patch 合并为一个视觉 token,传递给文本解码器。对于视频,处理器每秒采样 2 帧,并将连续采样的帧两两分组。随后视觉 transformer 让每个 patch 与帧对内的所有其他 patch 进行注意力计算。

基准测试结果

文章标题:欢迎 Qwen 4:端侧的前沿智能

正文: Qwen 团队已在模型卡中发布了模型基准测试结果,我们在下方挑选了不同任务中社区最喜爱的项目。Qwen4-35B-A3B 在各项基准测试中的表现远超其规模和级别。例如,它可以与规模大得多的 Qwen 3.8 Flash Next 相媲美,在某些任务上甚至更胜一筹。例如,它可以与规模大得多的 Qwen 3.8 Flash Next 相媲美,在某些任务上甚至更胜一筹。

"八项 Frontier List 基准测试,比较 Qwen4-35B-A3B、Qwen3.8-Flash-Next、Qwen3.8-27B 和 Inkling2 Mini。
"八项 Frontier List 基准测试,比较 Qwen4-35B-A3B、Qwen3.8-Flash-Next、Qwen3.8-27B 和 Inkling2 Mini。

推理支持

Qwen4-35B-A3B 在 transformers、llama.cpp、vLLM、SGLang、Inference Providers 和 Inference Endpoints 中均提供首日支持。

Transformers

运行该模型最快的方式是使用 any-to-any pipeline:


        from transformers import pipeline

pipe = pipeline("image-text-to-text", model="Qwen/Qwen4-35B-A3B", processor="huggingface/Qwen4-35B-A3B-processor", device_map="auto")

messages = [{"role": "user", "content": "Who are you?"}]
outputs = pipe(messages, max_new_tokens=2048, return_full_text=False)
print(outputs[0]["generated_text"])
      

再往下深入一层,你可以使用自动类 AutoModelForMultimodalLM 和 AutoProcessor 加载模型和处理器。你可以直接控制推理强度,它接受四个值("xhigh"、"high"、"low" 或 "off"),如果未提供值则默认为 high。


        from transformers import AutoModelForMultimodalLM, AutoProcessor

model_id = "Qwen/Qwen4-35B-A3B"

processor = AutoProcessor.from_pretrained("huggingface/Qwen4-35B-A3B-processor")
model = AutoModelForMultimodalLM.from_pretrained(
    model_id,
    device_map="auto"
)

messages = [{"role": "user", "content": "Who are you?"}]
inputs = processor.apply_chat_template(
    messages,
    add_generation_prompt=True,
    tokenize=True,
    return_dict=True,
    return_tensors="pt",
    reasoning_effort="off", # "xhigh", "high", "low", or "off
).to(model.device)

outputs = model.generate(**inputs, max_new_tokens=512)
print(processor.batch_decode(outputs, skip_special_tokens=True))
      

使用不同模态进行推理

在以下各节中,我们将展示如何在不同模态上运行推理。所有各节的通用设置都是加载模型和处理器。


        from transformers import AutoModelForMultimodalLM, AutoProcessor

model_id = "Qwen/Qwen4-35B-A3B"

processor = AutoProcessor.from_pretrained("huggingface/Qwen4-35B-A3B-processor")
model = AutoModelForMultimodalLM.from_pretrained(
    model_id,
    device_map="auto"
)
      

纯文本推理


        messages = [
    {"role": "system", "content": "You should only answer with a number."},
    {"role": "user", "content": "What is 17 * 23?"},
]

inputs = processor.apply_chat_template(
    messages,
    add_generation_prompt=True,
    tokenize=True,
    return_dict=True,
    return_tensors="pt",
    reasoning_effort="off", # "xhigh", "high", "low", or "off
).to(model.device)

outputs = model.generate(**inputs, max_new_tokens=512)
print(processor.batch_decode(outputs, skip_special_tokens=True))
      

图像与文本

要使用图像和文本进行推理,你只需将图像添加到 messages 中,并将它们传递给 apply_chat_template。


        messages = [
    {
    "role": "user",
    "content": [
        {"type": "image", "image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/llama.cpp/pills.jpg"},
        {"type": "text", "text": "Do the components of these drugs interact with each other?"},
    ],
    }
]

inputs = processor.apply_chat_template(
    messages,
    add_generation_prompt=True,
    tokenize=True,
    return_dict=True,
    return_tensors="pt",
    reasoning_effort="off",
).to(model.device)

outputs = model.generate(**inputs, max_new_tokens=512)
print(processor.batch_decode(outputs, skip_special_tokens=True))
      

视频推理

同样地,你也可以向消息中添加视频,并将它们传给 apply_chat_template。注意你需要安装 torchcodec 和 ffmpeg。


        messages = [
    {
        "role": "user",
        "content": [
            {"type": "video", "video": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/llama.cpp/pf.mp4"},
            {"type": "text", "text": "这个视频讲的是什么?"},
        ],
    }
]

inputs = processor.apply_chat_template(
    messages,
    add_generation_prompt=True,
    tokenize=True,
    return_dict=True,
    return_tensors="pt",
    reasoning_effort="off",
).to(model.device)

outputs = model.generate(**inputs, max_new_tokens=512)
print(processor.batch_decode(outputs, skip_special_tokens=True))
      

工具调用

我们也可以提供工具,让模型使用这些工具来解答请求。具体做法如下。


        def get_current_temperature(location: str, unit: str):
    """
    获取某个位置的当前温度。

Args:
        location: 要获取温度的位置,格式为 "City, Country"
        unit: 返回温度的单位。(choices: ["celsius", "fahrenheit"])
    """
    return 22.0  # 真实函数会在这里调用天气 API

messages = [{"role": "user", "content": "嘿,巴黎现在的温度是多少?"}]
inputs = processor.apply_chat_template(
    messages,
    tools=[get_current_temperature],
    add_generation_prompt=True,
    tokenize=True,
    return_dict=True,
    return_tensors="pt",
    reasoning_effort="off",
).to(model.device)

outputs = model.generate(**inputs, max_new_tokens=512)
print(processor.batch_decode(outputs, skip_special_tokens=True))
      

llama.cpp

Qwen4-35B-A3B 提供 day-0 的 llama.cpp 和 Llama App 支持。我们在 Hub 上发布了 3 个量化版本,具体为 Q4_K_M 4-bit、Q6_K 6-bit 和 Q8_0 8-bit。你可以在该 GGUF 仓库中找到它们。


        # llama serve
llama serve -hf ggml-org/Qwen4-35B-A3B-GGUF:Q6_K
      

        # llama cli
llama cli -hf ggml-org/Qwen4-35B-A3B-GGUF:Q6_K
      

开箱即支持 MTP 投机解码。你只需使用 --spec-type 和 --spec-draft-n-max 选项启用它。我们使用 3 个 draft token 获得了很好的结果:在搭载 Q4 量化的 Apple Silicon M5 上,生成速度提升了 22%。


        llama serve -hf ggml-org/Qwen4-35B-A3B-GGUF:Q4_K_M --spec-type draft-mtp --spec-draft-n-max 3
      

MLX

Qwen4-35B-A3B 可在 Apple Silicon 上通过 oMLX 和 mlx-lm 运行。

oMLX 为 QSA 和 gated residual 使用自定义 Metal kernel。它还支持图像输入、MTP 以及将 PLE 卸载到 SSD,并提供针对 oMLX 和 mlx-lm 优化的 oQ 量化。

mlx-lm 对该语言模型的支持正在进入官方仓库。传入 --chat-template-config '{"reasoning_effort": "low"}' 来更改推理强度。


        mlx_lm.generate --model mlx-community/Qwen4-35B-A3B-4bit-text \
  --prompt "Explain sparse attention like I am 5" \
  --max-tokens 4096
      

SGLang 和 vLLM

我们建议前往 SGLang 文档和 vLLM 文档,了解如何在这些推理引擎上高效运行该模型。

使用 TRL 对 Qwen 4 进行网络防御强化学习

使用 Qwen4 harness-adapter recipe 训练一个模型,对安全告警进行分诊,并引用每项决策背后的证据。

流程

我们将使用 TRL 中的 AsyncGRPOTrainer 训练 Qwen4-35B-A3B 标注漏洞,并引用代码库中的证据。

分诊训练循环中,使用 LoRA 的 Qwen4 采样回复,验证器检查标签和证据,GRPO 更新适配器。
分诊训练循环中,使用 LoRA 的 Qwen4 采样回复,验证器检查标签和证据,GRPO 更新适配器。

首先,我们将一个案例发送给 Qwen4。给它一个经过脱敏的告警及其证据记录。例如,在一个反复登录失败且设备数据不完整的案例中,分析师预期的回复可能是升级并引用诸如“A17”之类的事件。

接下来,我们对回复进行采样和评分。vLLM 针对同一告警采样多条回复。当回复的分诊标签和引用与分析人员的标准一致时,该回复获得奖励 1,否则为 0。

然后我们更新适配器。TRL 的异步 GRPO 训练器利用每组回复内的相对奖励来更新 LoRA 适配器。

最后,我们评估留出的事件。给基础模型和保存的适配器相同的案例和解码设置。比较分诊标签准确率、引用准确率和漏报升级。

在 Hugging Face Jobs 上运行流水线

使用双 GPU Job 和包含训练器、vLLM 及分诊验证器的 CUDA 镜像。bucket 中有一个脚本,可在一个 GPU 上运行 vLLM,在另一个 GPU 上进行训练。

使用 hf auth login 登录,然后将 <your-org> 替换为可写 triage-runs bucket 的命名空间。Jobs 将模型和分诊数据集作为输入挂载,并挂载 bucket 用于奖励和检查点。


        hf jobs run --name qwen4-alert-triage \
  --flavor h200x2 --timeout 3h \
  --secrets HF_TOKEN \
  --volume hf://Qwen/qwen4-35b-a3b:/model \
  --volume hf://datasets/burtenshaw/alert-triage:/triage \
  --volume 'hf://buckets/<your-org>/triage-runs:/outputs' \
  ghcr.io/openenv/qwen4-triage:latest \
  /opt/qwen4/train-triage /model /triage /outputs
      

使用 hf jobs logs JOB_ID 跟踪训练进度,并从 bucket 中获取保存的检查点。

训练奖励与网络安全评估

每个点表示在该日志记录步骤中评分的训练 rollout 的平均奖励。验证器奖励成功的结果,并为使用更少的工具调用添加少量奖励。使用留出的 CyberDefense Bench 结果来帮助选择保留哪个检查点。

"Qwen4-35B-A3B Pi v3 平均 rollout 奖励随优化器更新变化,在更新 122 时呈略微上升趋势。"
"Qwen4-35B-A3B Pi v3 平均 rollout 奖励随优化器更新变化,在更新 122 时呈略微上升趋势。"

在相同的 12 个留出 CyberDefense Bench 任务上,基础模型通过了 4/12,update-50 适配器通过了 6/12,update-100 适配器通过了 3/12。在这个样本上,较早的检查点表现最好,尽管整体训练奖励呈上升趋势。

如果你训练自己的适配器,请在 hub 上分享它!

演示

我们使用 Qwen4-35B-A3B 搭配 Pi 编码智能体,并为其配备了一些技能,看看它在零样本下能做什么。

所有 html 文件和视频都可以在这里找到。

动画视频

我们让 Qwen 为我们开发的一款推测解码基准测试工具制作介绍视频。它从 Google 学习了前端设计技能,从 Vercel 学习了网页动画技能,并制作了一个介绍视频,你可以在下面找到。

我们让 Qwen4-35B-A3B 仅用这个提示制作演示文稿:“make an educational slide deck on how llama app and llama.cpp relates to each other, here's the docs use the similar branding on the website for the slide deck”

Qwen 学习了 frontend-slides 技能,并就信息密度和受众向我们提出了后续问题,我们要求它为开发者制作一个高信息密度的幻灯片,它制作了下面的幻灯片。

实时场景生成

我们让 Qwen4-35B-A3B 在没有任何技能的情况下生成一个飞越山脉和云层的电影感场景。你可以在 traces 中找到提示。

总结

我们非常高兴与 Qwen 团队合作发布 Qwen4-35B-A3B,这是一款适用于本地部署的高质量中型模型。我们很期待看到你能用它做什么,试一试,并在评论中告诉我们你的想法!

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Hugging Face
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗