File size: 16,037 Bytes
77146ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
//
// Qwen3CoreML.swift
// Qwen3 CoreML Example
//
// Swift Example for Qwen3 CoreML Integration
// Qwen3-0.6B CoreML integration with Stateful KV-Cache and Int4 quantization
//
// Requirements:
// - iOS 18.0+ / macOS 15.0+ (Apple Neural Engine support)
// - 400-500MB RAM for both models
// - swift-transformers package
//
// Usage:
// let qwen3 = Qwen3CoreML()
// await qwen3.loadModels()
// let response = await qwen3.generate("Hello, world!")
//
import Foundation
import CoreML
import Tokenizers
/// Qwen3-0.6B CoreML model wrapper with Stateful KV-Cache
@MainActor
public final class Qwen3CoreML {
// MARK: - Configuration
public struct Config {
public static let maxContextLength = 1024
public static let maxTokens = 512
public static let temperature: Float = 0.7
public static let topK = 40
public static let topP: Float = 0.9
// Model paths (relative to app bundle or absolute)
public static let prefillModelName = "Qwen3-0.6B-Prefill-Int4"
public static let decodeModelName = "Qwen3-0.6B-Decode-Int4"
public static let tokenizerModelId = "Qwen/Qwen3-0.6B"
}
// MARK: - State
private var prefillModel: MLModel?
private var decodeModel: MLModel?
private var tokenizer: Tokenizer?
private var decodeState: MLState?
private(set) var isModelsLoaded = false
private(set) var isGenerating = false
// Qwen3 special tokens
private let eosTokenIds: Set<Int> = [151643, 151645] // <|endoftext|>, <|im_end|>
private let bosTokenId = 151643
private let chatTemplate = "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n%@<|im_end|>\n<|im_start|>assistant\n"
// Performance tracking
private(set) var tokensPerSecond: Double = 0
private(set) var currentPosition = 0
// MARK: - Initialization
public init() {
print("π€ Qwen3CoreML initialized")
}
// MARK: - Model Loading
/// Load both Prefill and Decode CoreML models and tokenizer
public func loadModels() async throws {
guard !isModelsLoaded else {
print("π€ Qwen3: Models already loaded")
return
}
print("π€ Qwen3: Loading CoreML models and tokenizer...")
do {
// Load Prefill model
try await loadModel(named: Config.prefillModelName, into: &prefillModel)
print("β
Prefill model loaded")
// Load Decode model with state
try await loadModel(named: Config.decodeModelName, into: &decodeModel, withState: true)
print("β
Decode model loaded")
// Load tokenizer via Tokenizers framework
tokenizer = try await AutoTokenizer.from(pretrained: Config.tokenizerModelId)
print("β
Tokenizer loaded")
isModelsLoaded = true
print("π Qwen3 models loaded successfully")
} catch {
print("β Failed to load Qwen3 models: \(error.localizedDescription)")
throw Qwen3Error.modelLoadingFailed(error.localizedDescription)
}
}
/// Load a single CoreML model
private func loadModel(named modelName: String, into model: inout MLModel?, withState: Bool = false) async throws {
let config = MLModelConfiguration()
config.computeUnits = .cpuAndNeuralEngine // Use ANE when available
// Try Bundle first, then local paths
var modelURL: URL?
// Check main bundle
if let url = Bundle.main.url(forResource: modelName, withExtension: "mlpackage") {
modelURL = url
}
// Check app support directory
else if let appSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first {
let appDir = appSupport.appendingPathComponent("Qwen3CoreML")
let modelsDir = appDir.appendingPathComponent("Models")
let modelPath = modelsDir.appendingPathComponent("\(modelName).mlpackage")
if FileManager.default.fileExists(atPath: modelPath.path) {
modelURL = modelPath
}
}
guard let modelURL = modelURL else {
throw Qwen3Error.modelNotFound(modelName)
}
// Compile and load model
let compiledURL = try await MLModel.compileModel(at: modelURL)
model = try MLModel(contentsOf: compiledURL, configuration: config)
// Create state for decode model only
if withState {
decodeState = model?.makeState()
}
}
// MARK: - Text Generation
/// Generate text response for user message (streaming)
public func generate(
userMessage: String,
systemPrompt: String = "You are a helpful assistant.",
maxTokens: Int = Config.maxTokens,
temperature: Float = Config.temperature,
enableThinking: Bool = false
) -> AsyncStream<String> {
AsyncStream { continuation in
Task {
await generateInternal(
userMessage: userMessage,
systemPrompt: systemPrompt,
maxTokens: maxTokens,
temperature: temperature,
enableThinking: enableThinking,
continuation: continuation
)
}
}
}
/// Generate text response for user message (non-streaming)
public func generateSync(
userMessage: String,
systemPrompt: String = "You are a helpful assistant.",
maxTokens: Int = Config.maxTokens,
temperature: Float = Config.temperature,
enableThinking: Bool = false
) async throws -> String {
guard isModelsLoaded, let tokenizer = tokenizer else {
throw Qwen3Error.modelNotLoaded
}
var result = ""
for await chunk in generate(
userMessage: userMessage,
systemPrompt: systemPrompt,
maxTokens: maxTokens,
temperature: temperature,
enableThinking: enableThinking
) {
result += chunk
}
return result
}
/// Reset conversation and KV-Cache state
public func resetConversation() {
decodeState = decodeModel?.makeState()
currentPosition = 0
print("π Qwen3 conversation reset")
}
// MARK: - Private Generation
private func generateInternal(
userMessage: String,
systemPrompt: String,
maxTokens: Int,
temperature: Float,
enableThinking: Bool,
continuation: AsyncStream<String>.Continuation
) async {
guard isModelsLoaded,
let prefillModel = prefillModel,
let decodeModel = decodeModel,
let tokenizer = tokenizer,
var decodeState = decodeState else {
continuation.finish()
return
}
isGenerating = true
let startTime = Date()
defer {
isGenerating = false
continuation.finish()
}
do {
// Format chat prompt
let chatPrompt = formatChatPrompt(
userMessage: userMessage,
systemPrompt: systemPrompt,
enableThinking: enableThinking
)
// Tokenize prompt
let inputTokens = tokenizer.encode(text: chatPrompt)
// Check context length
guard inputTokens.count + maxTokens <= Config.maxContextLength else {
print("β οΈ Prompt too long, truncating...")
// Truncate if needed
let truncatedTokens = Array(inputTokens.suffix(Config.maxContextLength - maxTokens))
// Add BOS token if missing
let tokensToProcess = truncatedTokens.first == bosTokenId ? truncatedTokens : [bosTokenId] + truncatedTokens
try await processTokens(tokensToProcess, model: prefillModel)
}
// Process initial tokens with Prefill model
try await processTokens(inputTokens, model: prefillModel)
// Generate new tokens with Decode model
var generatedTokens: [Int] = []
var isInThinkingBlock = false
for _ in 0..<maxTokens {
let nextToken = try await generateNextToken(
temperature: temperature,
decodeModel: decodeModel,
decodeState: &decodeState
)
// Check for end of generation
if eosTokenIds.contains(nextToken) {
break
}
generatedTokens.append(nextToken)
// Handle thinking blocks (thinking mode)
if nextToken == 151667 { // <think>
isInThinkingBlock = true
} else if nextToken == 151668 { // </think>
isInThinkingBlock = false
if !enableThinking {
continue
}
}
// Decode token to text
let tokenText = tokenizer.decode(tokens: [nextToken])
// Stream token if not in thinking block or thinking enabled
if !isInThinkingBlock || enableThinking {
continuation.yield(tokenText)
}
}
// Calculate performance
let elapsed = Date().timeIntervalSince(startTime)
tokensPerSecond = Double(generatedTokens.count) / elapsed
print("π Generation: \(generatedTokens.count) tokens in \(String(format: "%.2f", elapsed))s (\(String(format: "%.1f", tokensPerSecond)) tok/s)")
} catch {
print("β Generation failed: \(error.localizedDescription)")
// Note: We don't throw here since continuation is already finished
}
}
/// Process initial tokens using Prefill model
private func processTokens(_ tokens: [Int], model: MLModel) async throws {
let seqLen = tokens.count
// Create causal mask for all tokens
let causalMask = createCausalMask(seqLen: seqLen, totalLen: seqLen)
let inputIdsTensor = MLTensor(
shape: [1, seqLen],
scalars: tokens.map { Int32($0) },
scalarType: Int32.self
)
let inputs = try MLDictionaryFeatureProvider(dictionary: [
"inputIds": MLFeatureValue(tensor: inputIdsTensor),
"causalMask": MLFeatureValue(tensor: causalMask)
])
// Run prefill inference
_ = try await model.prediction(from: inputs)
currentPosition = seqLen
}
/// Generate next token using Decode model
private func generateNextToken(
temperature: Float,
decodeModel: MLModel,
decodeState: inout MLState
) async throws -> Int {
// Current position as input
let positionIds = [Int32(currentPosition)]
let positionTensor = MLTensor(
shape: [1, 1],
scalars: positionIds,
scalarType: Int32.self
)
// We need a dummy input ID, actual logit generation uses past KV cache
let dummyInputTensor = MLTensor(
shape: [1, 1],
scalars: [Int32(0)], // Will be ignored in decode model
scalarType: Int32.self
)
let inputs = try MLDictionaryFeatureProvider(dictionary: [
"inputIds": MLFeatureValue(tensor: dummyInputTensor),
"positionIds": MLFeatureValue(tensor: positionTensor),
])
let output = try await decodeModel.prediction(from: inputs, using: decodeState)
guard let logitsTensor = output.featureValue(for: "logits")?.tensorValue(of: Float16.self) else {
throw Qwen3Error.inferenceError("No logits in model output")
}
// Sample from logits
let nextToken = sampleToken(from: logitsTensor, temperature: temperature)
// Update position for next step
currentPosition += 1
return nextToken
}
/// Sample next token from logits
private func sampleToken(from logitsTensor: MLTensor, temperature: Float) -> Int {
// Extract logits for the last token [1, 1, vocab_size] -> [vocab_size]
let vocabSize = logitsTensor.shape[2]
var logitsArray = [Float](repeating: 0, count: vocabSize)
logitsTensor.withUnsafeBufferPointer(of: Float16.self) { buffer in
for i in 0..<vocabSize {
logitsArray[i] = Float(buffer[vocabSize + i]) // Last token position
}
}
if temperature <= 0 {
// Greedy sampling
return logitsArray.enumerated().max(by: { $0.element < $1.element })?.offset ?? 0
}
// Apply temperature and sample
let scaledLogits = logitsArray.map { $0 / temperature }
let maxLogit = scaledLogits.max() ?? 0
let expLogits = scaledLogits.map { exp($0 - maxLogit) }
let sumExp = expLogits.reduce(0, +)
let probs = expLogits.map { $0 / sumExp }
// Sample from distribution
let random = Float.random(in: 0..<1)
var cumulative: Float = 0
for (index, prob) in probs.enumerated() {
cumulative += prob
if random < cumulative {
return index
}
}
return vocabSize - 1
}
/// Create causal attention mask
private func createCausalMask(seqLen: Int, totalLen: Int) -> MLTensor {
var maskData = [Float16](repeating: Float16(-Float.infinity), count: seqLen * totalLen)
for i in 0..<seqLen {
for j in 0..<(totalLen - seqLen + i + 1) {
maskData[i * totalLen + j] = Float16(0)
}
}
return MLTensor(
shape: [1, 1, seqLen, totalLen],
scalars: maskData,
scalarType: Float16.self
)
}
/// Format chat prompt using Qwen3 chat template
private func formatChatPrompt(userMessage: String, systemPrompt: String, enableThinking: Bool) -> String {
let chatTemplate = "<|im_start|>system\n\(systemPrompt)<|im_end|>\n<|im_start|>user\n\(userMessage)<|im_end|>\n<|im_start|>assistant\n"
if enableThinking {
return chatTemplate
} else {
return chatTemplate + "/no_think\n"
}
}
}
// MARK: - Errors
public enum Qwen3Error: LocalizedError {
case modelNotFound(String)
case modelNotLoaded
case modelLoadingFailed(String)
case inferenceError(String)
case tokenizationError
public var errorDescription: String? {
switch self {
case .modelNotFound(let modelName):
return "Model '\(modelName)' not found. Place it in app bundle or ~/Library/Application Support/Qwen3CoreML/Models/"
case .modelNotLoaded:
return "Models are not loaded. Call loadModels() first."
case .inferenceError(let message):
return "Inference error: \(message)"
case .tokenizationError:
return "Tokenization error"
}
}
}
// MARK: - Helper Methods
/// Extension with utility methods for text processing
extension Qwen3CoreML {
/// Correct text using Qwen3 (compatible with LLMRunner.correct())
public func correct(text: String) async throws -> String {
return try await generateSync(
userMessage: """
Please correct the following text by fixing punctuation, capitalization, and grammatical errors.
Keep the original language. Only output the corrected text, nothing else.
Text: \(text)
Corrected:
""",
systemPrompt: "You are a professional proofreader and text editor.",
maxTokens: 256,
temperature: 0.1 // Low temperature for consistent corrections
).trimmingCharacters(in: .whitespacesAndNewlines)
}
}
|