diff --git a/Libraries/MLXLLM/Models/DeepseekV4.swift b/Libraries/MLXLLM/Models/DeepseekV4.swift index 2780b24476..c716e9483a 100644 --- a/Libraries/MLXLLM/Models/DeepseekV4.swift +++ b/Libraries/MLXLLM/Models/DeepseekV4.swift @@ -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) diff --git a/Libraries/MLXLMCommon/Load.swift b/Libraries/MLXLMCommon/Load.swift index 91232c74c3..5979b1523e 100644 --- a/Libraries/MLXLMCommon/Load.swift +++ b/Libraries/MLXLMCommon/Load.swift @@ -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