Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a41674b
feat: generic native prefill bridge with multi-arch support
TheTom Apr 14, 2026
54712e8
feat: wire native prefill bridge for Llama, Qwen2, Qwen3
TheTom Apr 14, 2026
22069ed
feat: Phi-4 native prefill with fused QKV/gate_up splitting
TheTom Apr 14, 2026
a105751
fix: JSON float formatting for MoE bridge config
TheTom Apr 14, 2026
c2a0ad0
feat: GPT-OSS native prefill (YaRN RoPE + MXFP4 + attention sinks)
TheTom Apr 14, 2026
9795924
feat: Gemma-3/4 bridge prep (optional PLE, optional v_proj)
TheTom Apr 14, 2026
3fda399
feat: PrefillBench updates for multi-model testing
TheTom Apr 14, 2026
1f8a765
chore: gitignore bridge dylib build artifact
TheTom Apr 14, 2026
c4974ba
chore: bundle prebuilt bridge dylibs
TheTom Apr 14, 2026
3cccc0d
perf: clean up Qwen bridge embedding and MoE routing
TheTom Apr 14, 2026
97a12ea
feat: MLXServer — OpenAI-compatible inference server
TheTom Apr 14, 2026
7fa4506
test: MLXServer session cache validation script
TheTom Apr 14, 2026
8f089e2
fix: benchmark harness support for local model paths
TheTom Apr 14, 2026
58addf3
Revert "perf: clean up Qwen bridge embedding and MoE routing"
TheTom Apr 14, 2026
a6f4027
Reapply "perf: clean up Qwen bridge embedding and MoE routing"
TheTom Apr 14, 2026
9a3849b
feat: dedicated Qwen and Gemma prefill bridges with consistent naming
TheTom Apr 14, 2026
e1dfde5
perf: add per-stage profiling and experimental gate+up fusion to Qwen…
TheTom Apr 14, 2026
7a1399f
fix: match Swift graph topology in Qwen bridge attention and MoE sort
TheTom Apr 14, 2026
f5667d4
config: make native prefill bridge opt-in (NATIVE_PREFILL=1)
TheTom Apr 14, 2026
4d5116d
perf: remove intermediate eval barriers from Qwen bridge forward pass
TheTom Apr 14, 2026
82f9e45
perf: match Swift routing pattern — remove float32 gate cast, use pre…
TheTom Apr 14, 2026
f5c01ce
perf: async KV eval + remove unnecessary contiguous in Qwen bridge
TheTom Apr 14, 2026
f45d280
fix: benchmark template fallback strips system prompt for Mistral compat
TheTom Apr 14, 2026
a89c813
fix: register minimax_m2 model type + benchmark template fallback
TheTom Apr 14, 2026
10dd5f3
perf: dlopen bridge + NAX enable — 2.3x prefill speedup
TheTom Apr 14, 2026
eb9438e
fix: restore .contiguous() on bridge KV injection
TheTom Apr 14, 2026
02b27d7
feat: build environment checks + Gemma 4 template fix script
TheTom Apr 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Libraries/MLXLLM/LLMModelFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public enum LLMTypeRegistry {
"mimo": create(MiMoConfiguration.self, MiMoModel.init),
"mimo_v2_flash": create(MiMoV2FlashConfiguration.self, MiMoV2FlashModel.init),
"minimax": create(MiniMaxConfiguration.self, MiniMaxModel.init),
"minimax_m2": create(MiniMaxM2Configuration.self, MiniMaxM2Model.init),
"glm4": create(GLM4Configuration.self, GLM4Model.init),
"glm4_moe": create(GLM4MoEConfiguration.self, GLM4MoEModel.init),
"glm4_moe_lite": create(GLM4MoELiteConfiguration.self, GLM4MoELiteModel.init),
Expand Down
26 changes: 25 additions & 1 deletion Libraries/MLXLLM/Models/GPTOSS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// port of https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/gpt_oss.py

import Foundation
import NativePrefillBridge
import MLX
import MLXLMCommon
import MLXNN
Expand Down Expand Up @@ -436,9 +437,32 @@ public class GPTOSSModel: Module, LLMModel, KVCacheDimensionProvider {
public func prepare(_ input: LMInput, cache: [KVCache], windowSize: Int?) throws
-> PrepareResult
{
let prefillStepSize = max(windowSize ?? 512, 2048)
var y = input.text

if ProcessInfo.processInfo.environment["NATIVE_PREFILL"] == "1" {
let bridge = GenericPrefillBridge.shared
let json = """
{"model_type":"gpt_oss","hidden_size":\(configuration.hiddenSize),"num_hidden_layers":\(configuration.hiddenLayers),"num_attention_heads":\(configuration.attentionHeads),"num_key_value_heads":\(configuration.kvHeads),"head_dim":\(configuration.headDim),"intermediate_size":\(configuration.intermediateSize),"vocab_size":\(configuration.vocabularySize),"rms_norm_eps":\(String(format:"%.0e",Double(configuration.rmsNormEps))),"rope_theta":\(String(format:"%.0f",Double(configuration.ropeTheta))),"tie_word_embeddings":false,"num_local_experts":\(configuration.localExperts),"num_experts_per_tok":\(configuration.expertsPerToken),"rope_type":"\({ if case .string(let s) = configuration.ropeScaling?["rope_type"] { return s } else { return "" } }())","yarn_factor":\(configuration.ropeScaling?["factor"]?.asFloat() ?? 1.0),"yarn_beta_fast":\(configuration.ropeScaling?["beta_fast"]?.asFloat() ?? 32.0),"yarn_beta_slow":\(configuration.ropeScaling?["beta_slow"]?.asFloat() ?? 1.0),"yarn_original_max_pos":\(configuration.ropeScaling?["original_max_position_embeddings"]?.asInt() ?? 4096)}
"""
if bridge.ensureInitialized(modelType: "gpt_oss", model: model, config: json) {
let allTokens = input.text.tokens
let prefillCount = allTokens.size - 1
if prefillCount > 0 {
let tokenSlice = allTokens[0 ..< prefillCount].reshaped(-1)
let (ms, ok) = bridge.runAndInjectKV(
tokenArray: tokenSlice, cache: cache, numLayers: configuration.hiddenLayers)
if ok {
print(String(format: "[GenericPrefill] %d tokens in %.1fms (%.0f t/s)",
prefillCount, ms, Double(prefillCount) / (ms / 1000)))
let lastToken = allTokens[prefillCount ..< allTokens.size]
return .tokens(LMInput.Text(tokens: lastToken))
}
}
}
}

let prefillStepSize = max(windowSize ?? 512, 2048)

// Match Python mlx-lm: process every prefill token except the LAST one
// through chunked prefill so we always hit the eval+clearCache between
// chunks, even when the prompt fits in a single chunk.
Expand Down
23 changes: 23 additions & 0 deletions Libraries/MLXLLM/Models/Gemma3Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import Foundation
import MLX
import MLXLMCommon
import NativePrefillBridge
import MLXNN

public struct Gemma3TextConfiguration: Codable {
Expand Down Expand Up @@ -434,6 +435,28 @@ public class Gemma3TextModel: Module, LLMModel {
return .tokens(.init(tokens: emptyToken))
}

if ProcessInfo.processInfo.environment["NATIVE_PREFILL"] == "1" {
let bridge = GenericPrefillBridge.shared
let json = """
{"model_type":"gemma3_text","hidden_size":\(config.hiddenSize),"num_hidden_layers":\(config.hiddenLayers),"num_attention_heads":\(config.attentionHeads),"num_key_value_heads":\(config.kvHeads),"head_dim":\(config.headDim),"intermediate_size":\(config.intermediateSize),"vocab_size":\(config.vocabularySize),"rms_norm_eps":\(String(format:"%.0e",Double(config.rmsNormEps))),"rope_theta":\(String(format:"%.0f",Double(config.ropeTheta))),"sliding_window":\(config.slidingWindow),"sliding_pattern":\(config.slidingWindowPattern),"tie_word_embeddings":false}
"""
if bridge.ensureInitialized(modelType: "gemma3_text", model: model, config: json) {
let allTokens = input.text.tokens
let prefillCount = allTokens.size - 1
if prefillCount > 0 {
let tokenSlice = allTokens[0 ..< prefillCount].reshaped(-1)
let (ms, ok) = bridge.runAndInjectKV(
tokenArray: tokenSlice, cache: cache, numLayers: config.hiddenLayers)
if ok {
print(String(format: "[GenericPrefill] %d tokens in %.1fms (%.0f t/s)",
prefillCount, ms, Double(prefillCount) / (ms / 1000)))
let lastToken = allTokens[prefillCount ..< allTokens.size]
return .tokens(LMInput.Text(tokens: lastToken))
}
}
}
}

return .tokens(input.text)
}
}
Expand Down
Loading