阿里发布 Qwen4-35B-A3B 原生多模态 MoE 模型

原标题:Welcome Qwen 4: Cutting-edge intelligence on-device
78AI 研判分

阿里 Qwen 团队在 Hugging Face 发布 Qwen4-35B-A3B,将下一代混合架构正式落地为可部署模型。该模型为原生多模态混合专家结构,总参数 35B、激活参数 3B,定位面向本地部署的中等规模高质量模型。Hugging Face 同步提供推理与技能调用示例,便于开发者直接上手。

报道正文

本文目录15 个章节

返回文章列表

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

发布于 2026 年 12 月 4 日 在 GitHub 上更新 点赞 6

文本
 [merve merve  关注](https://huggingface.co/merve)   [Adina Yakefu AdinaY  关注](https://huggingface.co/AdinaY)   [Pedro Cuenca pcuenq  关注](https://huggingface.co/pcuenq)   [Albert Villanova del Moral albertvillanova  关注](https://huggingface.co/albertvillanova)   [Jun Kim Jundot  关注](https://huggingface.co/Jundot)   [Anton Vlasjuk AntonV  关注](https://huggingface.co/AntonV)   [Xuan-Son Nguyen ngxson  关注](https://huggingface.co/ngxson)   [ben burtenshaw burtenshaw  关注](https://huggingface.co/burtenshaw)   [Aritra Roy Gosthipaty ariG23498  关注](https://huggingface.co/ariG23498)       

由阿里巴巴 Qwen 团队推出的 Qwen4-35B-A3B 已在 Hugging Face 上线!

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

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

摘要

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

Qwen4 有哪些新特性?

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

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

Qwen4-35B-A3B 还改变了信息在层间的传递方式。每个 token 以其 2,048 维嵌入的四个副本开始。这些副本成为四条残差流。在每个注意力块或 MoE 块之前,学习到的门控将这些流混合为单一输入。八个选中的专家和共享专家处理该输入,并产生一个合并输出。随后,每条流将同一输出的一个缩放副本加到自身状态上。一个学习到的门控为每条流单独设定缩放比例。

模型中最大的部分之一位于第二个解码器层。该层有一个逐层嵌入表(PLE),包含 205 亿参数。每个 token 仅读取 PLE 表中的 16 行。八次查找编码最后两个 token,另外八次查找编码最后三个 token(当前 token 及其紧邻的前两个 token)。该层将这 16 个嵌入合并为一个,对它们向每条残差流的贡献进行门控,并使用一个短卷积来混入邻近模式。

该模型采用视觉 Transformer 来编码图像和视频。ViT 将图像读取为 16 × 16 的图块,并将每 2 × 2 组空间图块合并为一个视觉 token,再传递给文本解码器。对于视频,处理器每秒采样 2 帧,并将连续采样的帧分组为帧对。随后,视觉 Transformer 让每个图块在该帧对内关注其他所有图块。

基准测试结果

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

'Eight Frontier List benchmarks comparing Qwen4-35B-A3B, Qwen3.8-Flash-Next, Qwen3.8-27B, and Inkling2 Mini.'

推理支持

Qwen4-35B-A3B 在发布首日即获得 transformers、llama.cpp、vLLM、SGLang、Inference Providers 和 Inference Endpoints 的支持。

Transformers

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

PYTHON
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。

PYTHON
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))

使用不同模态进行推理

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

PYTHON
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"
)

纯文本推理

PYTHON
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 即可。

PYTHON
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))

视频推理

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

PYTHON
messages = [
    {
        "role": "user",
        "content": [
            {"type": "video", "video": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/llama.cpp/pf.mp4"},
            {"type": "text", "text": "What is this video about?"},
        ],
    }
]

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))

工具调用

我们还可以提供模型可用于解决请求的工具。具体做法如下。

PYTHON
def get_current_temperature(location: str, unit: str):
    """
    Get the current temperature at a location.

    Args:
        location: The location to get the temperature for, in the format "City, Country"
        unit: The unit to return the temperature in. (choices: ["celsius", "fahrenheit"])
    """
    return 22.0  # A real function would call a weather API here

messages = [{"role": "user", "content": "Hey, what's the temperature in Paris right now?"}]
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 从发布首日即支持 llama.cpp 和 Llama App。我们在 Hub 上提供了 3 个量化版本,分别是 Q4_K_M 4-bit、Q6_K 6-bit 和 Q8_0 8-bit。你可以在这个 GGUF 仓库中找到它们。

BASH
# llama serve  
llama serve -hf ggml-org/Qwen4-35B-A3B-GGUF:Q6_K
BASH
# 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%。

BASH
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 可通过 oMLX 和 mlx-lm 在 Apple Silicon 上运行。

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

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

BASH
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 配方 训练一个模型,使其能够对安全告警进行分诊,并为每个决策引用相应证据。

流程

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

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

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

接下来,我们采样并评分回复。vLLM 针对同一告警采样多条回复。当回复的分诊标签和引用与分析师的判定标准匹配时,每条回复获得 1 分奖励,否则为 0 分。

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

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

在 Hugging Face Jobs 上运行该流程

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

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

BASH
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 之前呈略微上升趋势。'

在同样的 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——一款面向本地部署的高质量中型模型。我们期待看到你能用它做出什么,赶快试一试,并在评论中告诉我们你的想法!