Qwen3.5 Chat (LLM บนอุปกรณ์)

Qwen3.5-0.8B เป็นโมเดลไฮบริด DeltaNet (linear attention) + GatedAttention ขนาด 24 ชั้น (DeltaNet 18 ชั้น + GatedAttention 6 ชั้น) ลดความละเอียดเป็น INT4 สำหรับ MLX (GPU Metal) และ INT8 สำหรับ CoreML (Neural Engine) โมเดลทำงานบน Mac ผ่าน MLX หรือบน iPhone และ Mac ผ่าน CoreML โดยมีการสร้าง token แบบ streaming ออกแบบมาสำหรับ pipeline เสียงโดยที่ LLM บนอุปกรณ์ทำหน้าที่เป็น "สมอง" ระหว่าง ASR และ TTS

พร้อมสำหรับ pipeline เสียง

Qwen3.5 Chat ผสานเข้ากับ VoicePipeline ของ SpeechCore ในฐานะส่วนประกอบ LLM ในห่วงโซ่ ASR → LLM → TTS สถาปัตยกรรมไฮบริด DeltaNet ให้ attention เชิงเส้นที่มีประสิทธิภาพสำหรับบริบทยาว

เริ่มต้นอย่างรวดเร็ว

import Qwen3Chat

let chat = try await Qwen35MLXChat.fromPretrained()

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

// Streaming tokens
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 ชั้น: DeltaNet 18 ชั้น (linear attention พร้อม gated delta rule recurrence และ RMSNormGated) และ GatedAttention 6 ชั้น (scaled dot-product attention แบบมาตรฐาน) แบ็กเอนด์ MLX รัน inference บน GPU Metal ด้วยน้ำหนัก safetensors แบ็กเอนด์ CoreML ใช้สถาปัตยกรรมแบบสองโมเดล (prefill + decode) ที่ปรับให้เหมาะกับ Neural Engine ทั้งสองรองรับ KV cache พร้อม prompt caching และ sampling ที่กำหนดค่าได้ (temperature, top-k, top-p, repetition penalty)

อินพุต / เอาต์พุตของโมเดล

ทิศทางชื่อรูปทรงคำอธิบาย
อินพุตinput_ids[1, seq_len]ID ของ token (Int32)
อินพุตattention_mask[1, seq_len]Attention mask (Int32)
อินพุตkv_cacheต่อชั้นสถานะ cache key-value
เอาต์พุตlogits[1, 1, 151936]Logits ของ token ถัดไป (Float16)
เอาต์พุตkv_cache_outต่อชั้นKV cache ที่อัปเดตแล้ว

รูปแบบของโมเดล

รูปแบบการลดความละเอียดขนาดการประมวลผลHuggingFace
Qwen3.5-0.8B ChatINT4418 MBGPU Metal (MLX)aufklarer/Qwen3.5-0.8B-Chat-MLX
Qwen3.5-0.8B ChatINT8981 MBNeural Engine (CoreML)aufklarer/Qwen3.5-0.8B-Chat-CoreML

การตั้งค่า sampling

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 = greedy, 1 = สร้างสรรค์)
topK50เก็บผู้สมัครอันดับต้น K รายการ
topP0.95เกณฑ์ nucleus sampling
maxTokens512จำนวน token สูงสุดสำหรับการตอบกลับ
repetitionPenalty1.1ลงโทษ token ที่ซ้ำ
disableThinkingfalseข้ามโหมด thinking
maxThinkingTokens100จำกัด token ของ thinking

บทสนทนาหลายรอบ

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()  // Clear history and KV cache

การจัดการหน่วยความจำ

// Check memory state
print(chat.isLoaded)        // true
print(chat.memoryFootprint) // 438304768 (~418 MB)

// Free memory under pressure
chat.unload()
print(chat.isLoaded)        // false

// Reload when needed
let chat = try await Qwen35MLXChat.fromPretrained()
เคล็ดลับการจัดการหน่วยความจำบน iOS

บน iPhone การปลด LLM ก่อนทำ TTS inference จะคืนหน่วยความจำราว ~418 MB (INT4 MLX) หรือ ~981 MB (INT8 CoreML) ช่วยป้องกันไม่ให้ jetsam ยุติโปรเซสเมื่อรัน pipeline เต็มรูปแบบ ASR → LLM → TTS

ประสิทธิภาพ

อุปกรณ์PrefillDecodeToken/วินาที
M2 Max~50ms~65ms/tok~15 tok/s
iPhone 16 Pro~1.5s~450ms/tok~2.2 tok/s

การแปลง

น้ำหนัก MLX ถูกแปลงจาก checkpoint ต้นฉบับของ Qwen3.5-0.8B ด้วยสคริปต์การแปลง MLX โมเดล CoreML ใช้สคริปต์การแปลงแยกต่างหากสำหรับการ deploy บน Neural Engine น้ำหนักที่แปลงไว้ล่วงหน้ามีอยู่บน HuggingFace ที่ aufklarer/Qwen3.5-0.8B-Chat-MLX (INT4: 418 MB) และ aufklarer/Qwen3.5-0.8B-Chat-CoreML (INT8: 981 MB)