Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions Libraries/MLXLLM/Models/DeepseekV4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,22 @@ public class DeepseekV4ModelInner: Module {
let hFlat2 = h.reshaped(h.dim(0), h.dim(1), -1) // for createAttentionMask
let mask = createAttentionMask(h: hFlat2, cache: firstCache)

// 43-layer forward through 256-expert MoE blocks overruns Metal's
// ~2s GPU command-buffer watchdog if dispatched as one big graph.
// Split per-layer with asyncEval so each layer commits its own
// command buffer. Mirrors Qwen35/NemotronH pattern.
let isPrefill = h.dim(1) > 1
for (i, layer) in layers.enumerated() {
h = layer(
h,
mask: mask,
cache: cache?[i],
inputIds: inputs)
if isPrefill, let c = cache?[i] {
var toEval: [MLXArray] = [h]
toEval.append(contentsOf: c.innerState())
asyncEval(toEval)
}
}

// HyperHead reduce: (B, L, hcMult, H) → (B, L, H)
Expand Down
6 changes: 6 additions & 0 deletions Libraries/MLXLMCommon/Load.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public func loadWeights(
return nil
}
}
// Force eager eval of the per-module fresh-quantize ops emitted by
// `quantize(model:)`. For very large MoE models (DeepSeek-V4 256
// experts × 43 layers) the lazy graph fuses these into one big
// command buffer that overruns Metal's GPU watchdog (~2s) at first
// forward. Splitting here lets the rest of load proceed normally.
eval(model.parameters())
}

// apply the loaded weights
Expand Down
Loading