Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
5e16bbe
feat: expose tokenId in Generation.chunk for channel detection
Mar 23, 2026
a830938
feat: add GPU/CPU layer partitioning support
Mar 27, 2026
f8f315b
feat: extend LayerPartitionable to 15 more architectures
Mar 28, 2026
a6e3950
fix: move LayerPartitionable to ApertusModelInner where layer loop lives
Mar 28, 2026
153447c
feat(moe): Implement SSD expert streaming via lazyLoad and partitione…
Mar 28, 2026
3fa1179
feat(3c): adopt StreamableMoE for remaining MoE architectures
Mar 28, 2026
225f8b3
fix(arch): use MLX module graph traversal instead of Mirror to find n…
Mar 28, 2026
d40af54
feat(vlm): implement LayerPartitionable and StreamableMoE on Qwen35La…
Mar 28, 2026
c21ac1d
refactor: point mlx-swift dependency to LocalPackages for C++ extensi…
Mar 28, 2026
ae4a74e
feat(lm): integrate MoE SwitchLayers streamedGatherMM primitive routing
Mar 29, 2026
8c9829d
fix(Evaluate): correctly use loop tokenCount rather than unmutated it…
Mar 29, 2026
772e00e
fix(ssd): Connect streamedGatherMM outputs to MLX allocator directly …
Mar 30, 2026
4e06901
fix(ssd): Use prefault+mmap slice instead of custom LoadSSDExpert blo…
Mar 30, 2026
de73306
feat(ssd): Restore SSD stream metrics around prefault() call
Mar 30, 2026
4f0c007
feat(ssd): Use preadInto() for 5GB/s SSD streaming with correct tenso…
Mar 30, 2026
2fa7627
feat(ssd): Restore streamedGatherMM fast path now that cache-key bug …
Mar 30, 2026
93a06f1
feat(turboquant): expose turboQuantEnabled on KVCacheSimple
Mar 30, 2026
56fdb76
fix(engine): lower TurboKV log threshold, suppress noisy SSD logs
Mar 30, 2026
a650436
fix(turbo-kv): compress from token 1, add head_dim guard for non-128 …
Mar 31, 2026
e2c29e5
fix(turbo-kv): update guard to allow head_dim 256 (C++ now handles it)
Mar 31, 2026
312897b
feat(turbo-kv): wire compressed KV history into SDPA attention
Mar 31, 2026
8602988
feat(turbo-kv): add telemetry logging for compression & decode events
Mar 31, 2026
44512f2
fix(turbo-kv): redesign to always-compress (reference pattern) + 10s …
Mar 31, 2026
c336189
fix(turbo-kv): remove double-counting record() call from AttentionUtils
Mar 31, 2026
71678dd
fix(turbo-kv): fix origBytes calc — use batch shape not cumulative of…
Mar 31, 2026
2d885b8
fix(turbo-kv): fix context duplication — slice prior history only bef…
Mar 31, 2026
b7307a4
fix(turbo-kv): hot-window eviction design — keep last 256 tokens as fp16
Mar 31, 2026
5ae653a
fix(turbo-kv): increase hot window 256→512 tokens
Mar 31, 2026
bdae997
fix(turbo-kv): fix prompt-cache context loss via state getter/setter
Mar 31, 2026
3ba94ec
fix(turbo-kv): add 2048-token minimum activation threshold
Mar 31, 2026
8483100
feat: iOS sequential I/O, expert streaming, Mistral4 + SwitchLayers h…
Apr 1, 2026
d6e09c6
feat: Add Gemma 4 Support with MoE Block and Per-Layer Residual Condi…
Apr 3, 2026
1df38f0
Fix TurboKV head_dim size validation to support varying model archite…
Apr 3, 2026
5564b8d
Support Gemma variations in ToolCallFormat matcher
Apr 3, 2026
3856165
Implement QK logit softcapping and global attention parsing for Gemma…
Apr 3, 2026
aa4b6fa
Update mlx-swift dependency to use remote GitHub URL
Apr 3, 2026
3271917
feat: Stabilize Gemma 4 inference, fix ProportionalRoPE, and add embe…
Apr 4, 2026
c879a1f
feat: TurboKV support for head_dim 512 via virtual head splitting
Apr 4, 2026
b184aaa
Fix Gemma4 inference, TurboKV telemetry, SiLU activation, and RoPE po…
Apr 4, 2026
70a0dc0
Fix RotatingKVCache mask/key shape mismatch in multi-token prefill
Apr 4, 2026
9389128
Add Gemma4 native tool call parser and weight mapping fix
Apr 4, 2026
6efb5be
Merge pull request #2 from SharpAI/feature/gemma-4-inference-aha-moment
solderzzc Apr 4, 2026
9cc0793
Merge upstream/main into feature/gemma-4-inference-aha-moment
Apr 4, 2026
10430bf
Fix tool call properties dropping during multi-turn LLM generation co…
Apr 4, 2026
3d782a3
Concurrency fixes and lazyLoad support
Apr 4, 2026
e8c474d
Merge pull request #3 from SharpAI/feature/gemma-4-inference-aha-moment
solderzzc Apr 4, 2026
af10407
Fix Swift 6 Strict Concurrency error for CIContext
Apr 4, 2026
9a9575b
Enable SSD Expert Streaming on Gemma 4 by conforming to StreamableMoE
Apr 4, 2026
af401e3
Restore MoE generation coherence by properly hydrating scale tokens
Apr 5, 2026
e48243f
fix: dynamic mask slicing for sliding window attention in Gemma4 MoE
Apr 5, 2026
249477e
chore: move debugging scripts to dedicated folder
Apr 5, 2026
2cd4c3b
fix(Gemma4): dynamically compute bit depth for embedTokens to support…
Apr 6, 2026
5deab6f
fix: resolve shape mismatch caused by VLM language_model. prefix duri…
Apr 6, 2026
72cf2f7
Merge pull request #4 from SharpAI/feature/gemma-4-inference-aha-moment
solderzzc Apr 6, 2026
6aa0023
Add GLMMoeDSA.swift stub
Apr 8, 2026
321993b
Register GLMMoeDSA stub into factory on glm5.1 branch
Apr 8, 2026
c141cec
Implement GLMMoeDSA architecture via Dense MLA and drop indexer in sa…
Apr 8, 2026
c40ed46
Fix syntax and repoint dependency inside Package.swift
Apr 8, 2026
d9d4c82
Finalize GLM-5.1 MoE port with robust parameter filtering and integra…
Apr 8, 2026
2b1c653
feat: permanently disable PAPPS N+1 heuristic to restore native Apple…
Apr 9, 2026
0e0f360
Merge pull request #6 from SharpAI/feat/papps-deactivation
solderzzc Apr 9, 2026
20189dd
fix: replace local path mlx-swift dependency with remote url to repai…
Apr 9, 2026
b71fad2
Merge pull request #7 from SharpAI/fix/spm-remote-dependency
solderzzc Apr 9, 2026
e6ba580
feat: SSD expert streaming 8.5x speedup (0.58 → 4.95 tok/s)
ericjlake Apr 11, 2026
2c71c6c
ssd-opt-v2: asyncEval + speculative pread pipeline
ericjlake Apr 11, 2026
31e0ee7
feat: runtime top-k expert override via SWIFTLM_TOP_K env var
ericjlake Apr 11, 2026
24b8ba7
feat: speculative decoding infrastructure + MambaCache rollback for h…
ericjlake Apr 12, 2026
f148955
Merge pull request #9 from ericjlake/feat/ssd-streaming-10x
solderzzc Apr 12, 2026
12d7528
fix(gemma4): make expertsBlock optional and fix enable_moe_block fall…
Apr 12, 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
4 changes: 2 additions & 2 deletions Libraries/IntegrationTestHelpers/IntegrationTestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public enum ChatSessionTests {
to: "What is the weather in San Francisco?", images: [], videos: [])
{
switch generation {
case .chunk(let text):
case .chunk(let text, _):
print(text, terminator: "")
responseText += text
case .toolCall(let toolCall):
Expand Down Expand Up @@ -557,7 +557,7 @@ public enum ToolCallTests {
var toolCalls: [ToolCall] = []
for try await generation in stream {
switch generation {
case .chunk(let chunk):
case .chunk(let chunk, _):
text += chunk
case .toolCall(let toolCall):
toolCalls.append(toolCall)
Expand Down
21 changes: 20 additions & 1 deletion Libraries/MLXLLM/LLMModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
import MLX
import MLXLMCommon

// MARK: - Prefill Progress Hook
//
// This global closure is set by Server.swift before each generate() call and
// cleared when the first decode token arrives. It mirrors llama-server's
// slot_update progress reporting: called after each 512-token prefill chunk
// with (n_past, n_total).
//
// Thread model: written by the server async task before generation starts
// (happens-before the generation Task reads it), and read only from the
// synchronous MLX evaluation thread inside prepare(). This is safe without
// a lock because writes precede all reads in time.
public nonisolated(unsafe) var activePrefillProgressHook: ((Int, Int) -> Void)? = nil

/// Marker protocol for LLMModels
public protocol LLMModel: LanguageModel, LoRAModel {

Expand All @@ -22,14 +35,20 @@ extension LLMModel {
-> PrepareResult
{
let prefillStepSize = windowSize ?? 512
let totalTokens = input.text.tokens.size
var y = input.text
var processed = 0

// Prepare the prompt in chunks if larger than the prefill size
// Prepare the prompt in chunks if larger than the prefill size.
// After each chunk, call the progress hook so the server can emit
// llama-server-style slot_update SSE events with real n_past.
while y.tokens.size > prefillStepSize {
let input = y[.newAxis, ..<prefillStepSize]
_ = self(input, cache: cache.isEmpty ? nil : cache, state: nil)
eval(cache)
y = y[prefillStepSize...]
processed += prefillStepSize
activePrefillProgressHook?(processed, totalTokens)
}

return .tokens(y)
Expand Down
27 changes: 25 additions & 2 deletions Libraries/MLXLLM/LLMModelFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public enum LLMTypeRegistry {
"gemma3": create(Gemma3TextConfiguration.self, Gemma3TextModel.init),
"gemma3_text": create(Gemma3TextConfiguration.self, Gemma3TextModel.init),
"gemma3n": create(Gemma3nTextConfiguration.self, Gemma3nTextModel.init),
"gemma4": create(Gemma4Configuration.self, Gemma4Model.init),
"qwen2": create(Qwen2Configuration.self, Qwen2Model.init),
"qwen3": create(Qwen3Configuration.self, Qwen3Model.init),
"qwen3_moe": create(Qwen3MoEConfiguration.self, Qwen3MoEModel.init),
Expand All @@ -53,6 +54,7 @@ public enum LLMTypeRegistry {
"glm4": create(GLM4Configuration.self, GLM4Model.init),
"glm4_moe": create(GLM4MoEConfiguration.self, GLM4MoEModel.init),
"glm4_moe_lite": create(GLM4MoELiteConfiguration.self, GLM4MoELiteModel.init),
"glm_moe_dsa": create(GLMMoeDSAConfiguration.self, GLMMoeDSAModel.init),
"acereason": create(Qwen2Configuration.self, Qwen2Model.init),
"falcon_h1": create(FalconH1Configuration.self, FalconH1Model.init),
"bitnet": create(BitnetConfiguration.self, BitnetModel.init),
Expand All @@ -72,7 +74,27 @@ public enum LLMTypeRegistry {
"nemotron_h": create(NemotronHConfiguration.self, NemotronHModel.init),
"afmoe": create(AfMoEConfiguration.self, AfMoEModel.init),
"jamba_3b": create(JambaConfiguration.self, JambaModel.init),
"mistral3": create(Mistral3TextConfiguration.self, Mistral3TextModel.init),
// "mistral3" is the outer model_type for two distinct text architectures:
// • text_config.model_type == "ministral3" → dense 8B (Mistral3TextModel)
// • text_config.model_type == "mistral4" → 119B MoE + MLA (Mistral4Model)
"mistral3": { data in
struct InnerType: Decodable {
struct TextConfig: Decodable {
let modelType: String?
enum CodingKeys: String, CodingKey { case modelType = "model_type" }
}
let textConfig: TextConfig?
enum CodingKeys: String, CodingKey { case textConfig = "text_config" }
}
let probe = try JSONDecoder().decode(InnerType.self, from: data)
if probe.textConfig?.modelType == "mistral4" {
let config = try JSONDecoder.json5().decode(Mistral4Configuration.self, from: data)
return Mistral4Model(config)
} else {
let config = try JSONDecoder.json5().decode(Mistral3TextConfiguration.self, from: data)
return Mistral3TextModel(config)
}
},
"apertus": create(ApertusConfiguration.self, ApertusModel.init),
])
}
Expand Down Expand Up @@ -529,7 +551,8 @@ public final class LLMModelFactory: ModelFactory {

try loadWeights(
modelDirectory: modelDirectory, model: model,
perLayerQuantization: baseConfig.perLayerQuantization)
perLayerQuantization: baseConfig.perLayerQuantization,
lazyLoad: configuration.lazyLoad)

let tokenizer = try await tokenizerTask

Expand Down
10 changes: 8 additions & 2 deletions Libraries/MLXLLM/Models/Apertus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,11 @@ private class ApertusBlock: Module {
}
}

private class ApertusModelInner: Module {
private class ApertusModelInner: Module, LayerPartitionable {

// LayerPartitionable
public var gpuLayerCount: Int?
public var totalLayerCount: Int { layers.count }

@ModuleInfo(key: "embed_tokens") var embedTokens: Embedding

Expand All @@ -321,7 +325,9 @@ private class ApertusModelInner: Module {
let mask = createAttentionMask(h: inputs, cache: cache?.first)

for (i, layer) in layers.enumerated() {
h = layer(h, mask: mask, cache: cache?[i])
h = partitionedLayerCall(index: i, gpuLayerCount: gpuLayerCount) {
layer(h, mask: mask, cache: cache?[i])
}
}

return norm(h)
Expand Down
11 changes: 9 additions & 2 deletions Libraries/MLXLLM/Models/BaichuanM1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ class BaichuanM1DecoderLayer: Module {
}
}

public class BaichuanM1ModelInner: Module {
public class BaichuanM1ModelInner: Module, LayerPartitionable {


// LayerPartitionable
public var gpuLayerCount: Int?
public var totalLayerCount: Int { layers.count }
let args: BaichuanM1Configuration
@ModuleInfo(key: "embed_tokens") var embedTokens: Embedding

Expand All @@ -223,7 +228,9 @@ public class BaichuanM1ModelInner: Module {
let mask = mask ?? createAttentionMask(h: x, cache: cache?.first)

for (i, layer) in layers.enumerated() {
x = layer(x, mask: mask, cache: cache?[i])
x = partitionedLayerCall(index: i, gpuLayerCount: gpuLayerCount) {
layer(x, mask: mask, cache: cache?[i])
}
}

return norm(x)
Expand Down
11 changes: 9 additions & 2 deletions Libraries/MLXLLM/Models/Cohere.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ class CohereTransformerBlock: Module {
}
}

public class CohereModelInner: Module {
public class CohereModelInner: Module, LayerPartitionable {


// LayerPartitionable
public var gpuLayerCount: Int?
public var totalLayerCount: Int { layers.count }

@ModuleInfo(key: "embed_tokens") var embedTokens: Embedding

Expand All @@ -137,7 +142,9 @@ public class CohereModelInner: Module {
let mask = createAttentionMask(h: h, cache: cache?.first)

for (i, layer) in layers.enumerated() {
h = layer(h, mask: mask, cache: cache?[i])
h = partitionedLayerCall(index: i, gpuLayerCount: gpuLayerCount) {
layer(h, mask: mask, cache: cache?[i])
}
}

return norm(h)
Expand Down
10 changes: 8 additions & 2 deletions Libraries/MLXLLM/Models/DeepseekV3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class DeepseekV3DecoderLayer: Module {
}
}

public class DeepseekV3ModelInner: Module {
public class DeepseekV3ModelInner: Module, LayerPartitionable, StreamableMoE {
var config: DeepseekV3Configuration
var vocabSize: Int
@ModuleInfo(key: "embed_tokens") var embedTokens: Embedding
Expand All @@ -383,6 +383,10 @@ public class DeepseekV3ModelInner: Module {
@ModuleInfo(key: "norm") var norm: RMSNorm
var pipelineRank: Int
var pipelineSize: Int

public var gpuLayerCount: Int? = nil
public var streamExperts: Bool = false
public var totalLayerCount: Int { layers.count }

init(config: DeepseekV3Configuration) {
self.config = config
Expand All @@ -406,7 +410,9 @@ public class DeepseekV3ModelInner: Module {
let attentionMask = createAttentionMask(h: h, cache: cache?.first)

for (i, layer) in layers.enumerated() {
h = layer(h, mask: attentionMask, cache: cache?[i])
h = partitionedLayerCall(index: i, gpuLayerCount: gpuLayerCount, stream: streamExperts) {
layer(h, mask: attentionMask, cache: cache?[i])
}
}

return norm(h)
Expand Down
11 changes: 9 additions & 2 deletions Libraries/MLXLLM/Models/Ernie4_5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ class Ernie45DecoderLayer: Module {
}
}

public class Ernie45ModelInner: Module {
public class Ernie45ModelInner: Module, LayerPartitionable {


// LayerPartitionable
public var gpuLayerCount: Int?
public var totalLayerCount: Int { layers.count }
@ModuleInfo(key: "embed_tokens") var embedTokens: Embedding
let layers: [Ernie45DecoderLayer]
let norm: RMSNorm
Expand All @@ -186,7 +191,9 @@ public class Ernie45ModelInner: Module {
let mask = createAttentionMask(h: h, cache: cache?.first)

for (i, layer) in layers.enumerated() {
h = layer(h, mask: mask, cache: cache?[i])
h = partitionedLayerCall(index: i, gpuLayerCount: gpuLayerCount) {
layer(h, mask: mask, cache: cache?[i])
}
}

return norm(h)
Expand Down
11 changes: 9 additions & 2 deletions Libraries/MLXLLM/Models/Exaone4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ class Exaone4TransformerBlock: Module {
}
}

public class Exaone4ModelInner: Module {
public class Exaone4ModelInner: Module, LayerPartitionable {


// LayerPartitionable
public var gpuLayerCount: Int?
public var totalLayerCount: Int { layers.count }
@ModuleInfo(key: "embed_tokens") var embedTokens: Embedding

fileprivate let layers: [Exaone4TransformerBlock]
Expand Down Expand Up @@ -168,7 +173,9 @@ public class Exaone4ModelInner: Module {
let mask = createAttentionMask(h: h, cache: cache?.first)

for (i, layer) in layers.enumerated() {
h = layer(h, mask: mask, cache: cache?[i])
h = partitionedLayerCall(index: i, gpuLayerCount: gpuLayerCount) {
layer(h, mask: mask, cache: cache?[i])
}
}

return norm(h)
Expand Down
11 changes: 9 additions & 2 deletions Libraries/MLXLLM/Models/GLM4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ class GLM4DecoderLayer: Module {
}
}

public class GLM4ModelInner: Module {
public class GLM4ModelInner: Module, LayerPartitionable {


// LayerPartitionable
public var gpuLayerCount: Int?
public var totalLayerCount: Int { layers.count }
@ModuleInfo(key: "embed_tokens") var embedTokens: Embedding

fileprivate let layers: [GLM4DecoderLayer]
Expand All @@ -150,7 +155,9 @@ public class GLM4ModelInner: Module {
let mask = createAttentionMask(h: h, cache: cache?.first)

for (i, layer) in layers.enumerated() {
h = layer(h, mask: mask, cache: cache?[i])
h = partitionedLayerCall(index: i, gpuLayerCount: gpuLayerCount) {
layer(h, mask: mask, cache: cache?[i])
}
}

return norm(h)
Expand Down
10 changes: 8 additions & 2 deletions Libraries/MLXLLM/Models/GLM4MOELite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,15 @@ class GLM4MoELiteDecoderLayer: Module {
}
}

public class GLM4MoELiteModelInner: Module {
public class GLM4MoELiteModelInner: Module, LayerPartitionable, StreamableMoE {
@ModuleInfo(key: "embed_tokens") var embedTokens: Embedding

let layers: [GLM4MoELiteDecoderLayer]
let norm: RMSNorm

public var gpuLayerCount: Int? = nil
public var streamExperts: Bool = false
public var totalLayerCount: Int { layers.count }

init(_ config: GLM4MoELiteConfiguration) {
precondition(config.vocabularySize > 0)
Expand All @@ -476,7 +480,9 @@ public class GLM4MoELiteModelInner: Module {
let mask = createAttentionMask(h: h, cache: cache?.first)

for (i, layer) in layers.enumerated() {
h = layer(h, mask: mask, cache: cache?[i])
h = partitionedLayerCall(index: i, gpuLayerCount: gpuLayerCount, stream: streamExperts) {
layer(h, mask: mask, cache: cache?[i])
}
}

return norm(h)
Expand Down
Loading