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 Chat | INT4 | 418 MB | Metal GPU (MLX) | aufklarer/Qwen3.5-0.8B-Chat-MLX |
| Qwen3.5-0.8B Chat | INT8 | 981 MB | Neural 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)
| 参数 | 默认值 | 说明 |
|---|---|---|
temperature | 0.6 | 随机性(0 = 贪心,1 = 创造性) |
topK | 50 | 保留前 K 个候选 |
topP | 0.95 | Nucleus 采样阈值 |
maxTokens | 512 | 响应的最大 token 数 |
repetitionPenalty | 1.1 | 对重复 token 的惩罚 |
disableThinking | false | 跳过 thinking 模式 |
maxThinkingTokens | 100 | thinking 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()
在 iPhone 上,在 TTS 推理之前卸载 LLM 可释放约 418 MB(INT4 MLX)或约 981 MB(INT8 CoreML),在运行完整的 ASR → LLM → TTS 流水线时可避免被 jetsam 终止。
性能
| 设备 | Prefill | Decode | Tokens/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)获取。