Qwen3.5 Chat(端侧 LLM)

Qwen3.5-0.8B 是一个混合 DeltaNet(线性注意力)+ GatedAttention 模型,共 24 层(18 层 DeltaNet + 6 层 GatedAttention),为 MLX(Metal GPU)量化到 INT4,为 CoreML(Neural Engine)量化到 INT8。可在 Mac 上通过 MLX 运行,也可在 iPhone 和 Mac 上通过 CoreML 运行,并支持流式 token 生成。它为语音流水线而设计,在 ASR 与 TTS 之间充当端侧 LLM 的"大脑"。

语音流水线就绪

Qwen3.5 Chat 可作为 LLM 组件集成进 SpeechCore VoicePipeline,用于 ASR → LLM → TTS 链路。混合 DeltaNet 架构在长上下文上提供高效的线性时间注意力。

快速开始

import Qwen3Chat

let chat = try await Qwen35MLXChat.fromPretrained()

// 单次响应
let response = try chat.generate("What is Swift?", systemPrompt: "Answer briefly.")
print(response)

// 流式 token
let stream = chat.chatStream("Tell me a joke", systemPrompt: "Be funny.")
for try await token in stream {
    print(token, terminator: "")
}

架构

Qwen3.5-0.8B 是一个 24 层的混合模型:18 层 DeltaNet(带门控 delta 规则递推和 RMSNormGated 的线性注意力)和 6 层 GatedAttention(标准的 scaled dot-product attention)。MLX 后端使用 safetensors 权重在 Metal GPU 上运行推理。CoreML 后端采用针对 Neural Engine 优化的双模型架构(prefill + decode)。两者都支持带 prompt cache 的 KV cache,以及可配置的采样(温度、top-k、top-p、重复惩罚)。

模型 I/O

方向名称形状说明
输入input_ids[1, seq_len]Token ID(Int32)
输入attention_mask[1, seq_len]注意力掩码(Int32)
输入kv_cache每层Key-value cache 状态
输出logits[1, 1, 151936]下一个 token 的 logits(Float16)
输出kv_cache_out每层更新后的 KV cache

模型变体

变体量化大小计算HuggingFace
Qwen3.5-0.8B ChatINT4418 MBMetal GPU (MLX)aufklarer/Qwen3.5-0.8B-Chat-MLX
Qwen3.5-0.8B ChatINT8981 MBNeural Engine (CoreML)aufklarer/Qwen3.5-0.8B-Chat-CoreML

采样配置

let config = ChatSamplingConfig(
    temperature: 0.7,
    topK: 40,
    topP: 0.9,
    maxTokens: 128,
    repetitionPenalty: 1.1,
    disableThinking: false,
    maxThinkingTokens: 50
)
let response = try chat.generate("Explain gravity", sampling: config)
参数默认值说明
temperature0.6随机性(0 = 贪心,1 = 创造性)
topK50保留前 K 个候选
topP0.95Nucleus 采样阈值
maxTokens512响应的最大 token 数
repetitionPenalty1.1对重复 token 的惩罚
disableThinkingfalse跳过 thinking 模式
maxThinkingTokens100thinking token 上限

多轮对话

let chat = try await Qwen35MLXChat.fromPretrained()

let r1 = try chat.generate("My name is Alex", systemPrompt: "Remember the user's name.")
print(r1)  // "Nice to meet you, Alex!"

let r2 = try chat.generate("What's my name?")
print(r2)  // "Your name is Alex!"

chat.resetConversation()  // 清空历史和 KV cache

内存管理

// 查看内存状态
print(chat.isLoaded)        // true
print(chat.memoryFootprint) // 438304768 (~418 MB)

// 在内存紧张时释放
chat.unload()
print(chat.isLoaded)        // false

// 需要时重新加载
let chat = try await Qwen35MLXChat.fromPretrained()
iOS 内存小贴士

在 iPhone 上,在 TTS 推理之前卸载 LLM 可释放约 418 MB(INT4 MLX)或约 981 MB(INT8 CoreML),在运行完整的 ASR → LLM → TTS 流水线时可避免被 jetsam 终止。

性能

设备PrefillDecodeTokens/sec
M2 Max~50ms~65ms/tok~15 tok/s
iPhone 16 Pro~1.5s~450ms/tok~2.2 tok/s

模型转换

MLX 权重通过 MLX 转换脚本从原始的 Qwen3.5-0.8B checkpoint 转换而来。CoreML 模型使用单独的转换脚本部署到 Neural Engine。预转换好的权重可在 HuggingFace 的 aufklarer/Qwen3.5-0.8B-Chat-MLX(INT4:418 MB)和 aufklarer/Qwen3.5-0.8B-Chat-CoreML(INT8:981 MB)获取。